Search code examples
c++iosobjective-ciphonedicom

How to convert/create a dicom image from jpeg using Imebra library?


I want to create of convert a jpeg image to a dicom image using imebra library on iOS platform. In the objective c examples of the library the steps to convert or read a dicom into jpeg are given, however, I am going through the documentation and trying to achieve vice versa (i.e. jpeg to dicom). I have no background of c++ so any help would be appreciated. Links to the library and documentation:

1.https://imebra.com

2.https://imebra.com/wp-content/uploads/documentation/imebra_build_2013-10-30_10-00-11/html/dicom2jpeg_2dicom2jpeg_8cpp-example.html

Some basic code below, thanks in advance.

-(void)createDcmFromJpg
{

     NSString *imagePath = [[NSBundle mainBundle]pathForResource:@"test2" ofType:@"jpeg"];

    //Create read stream
    using namespace puntoexe;
    ptr<stream> readStream(new stream);
    readStream->openFile(NSStringToStringW(imagePath), std::ios::in);

   //create dataset
    ptr<streamReader> reader(new streamReader(readStream));
    ptr<imebra::dataSet> testDataSet = imebra::codecs::codecFactory::getCodecFactory()->load(reader);

    //set Tags
    testDataSet->setString(0x0010, 0, 0x0010, 0, "testStr");
    testDataSet->setString(0x0010, 0, 0x0010, 1, "testStr1");

   //Next step/steps ?
}

Solution

  • The imebra::dataSet represents a collection of Dicom tags.

    When you read a file (jpeg or dicom), then Imebra creates a dataSet which is a set of Dicom tags.

    When reading a Dicom file then the dataSet will mirror exactly the tags stored in the Dicom file, while when reading a jpeg image Imebra will create a dataSet containing the proper dicom tag that embeds the jpeg image: all the needed Dicom tags will be already in the dataSet and you can add your own (like patient name, etc).

    The next step consists in saving the dataSet to a Dicom stream:

    with Imebra V4 (current):

    // Load jpeg
    std::unique_ptr<imebra::Dataset> testDataset(imebra::CodecFactory::load("/path/to/jpegfile.jpg"));
    
    // Save as Dicom
    imebra::CodecFactory::save(testDataset, "/path/to/file.dcm", imebra::codecType_t::dicom);
    

    with Imebra 2015 (legacy):

    // Open the Dicom file for writing
    ptr<stream> writeStream(new stream);
    writeStream->openFile(NSStringToStringW(dicomfilepath), std::ios::out);
    
    // Declare the writer (will take care of writing into the stream)
    ptr<streamWriter> writer (new streamWriter(writeStream));
    
    // Use the dicom codec to write the dataSet into the file
    ptr<imebra::codecs::codec> dicomCodec(new imebra::codecs::dicomCodec);
    dicomCodec->write(writer, testDataSet);