Skip to content

Instantly share code, notes, and snippets.

@JulesGorny
Last active August 29, 2015 14:06
Show Gist options
  • Save JulesGorny/ac77ccb92ee304767dfa to your computer and use it in GitHub Desktop.
Save JulesGorny/ac77ccb92ee304767dfa to your computer and use it in GitHub Desktop.
char *file = "C:/Folder1/Folder2/dicomfile";
//Open the DICOM file and get the dictionary
DcmFileFormat fileformat;
OFCondition status = fileformat.loadFile(file);
DcmDataset *dictionary;
if (status.good())
dictionary = fileformat.getDataset();
//Get the transfer syntax to check if we need jpeg codecs
E_TransferSyntax xfer = dictionary->getOriginalXfer();
if(xfer >= EXS_JPEGProcess1TransferSyntax && xfer <= EXS_JPEGProcess14SV1TransferSyntax )
DJDecoderRegistration::registerCodecs(); // register JPEG codecs
Uint16 samplePerPixel = 1; //Initialisation in case we can't get a proper value
dictionary->findAndGetUint16(DCM_SamplesPerPixel, samplePerPixel);
DicomImage *di = new DicomImage(&fileformat, xfer, 0UL, 0, 1);
//Usually the value of the text can be the biggest value even if the image itself use only 1/10 of the possible values.
//This function automatically render the image correctly without the problem of outer values
di->setMinMaxWindow(1);
if( samplePerPixel == 1 )
di->writeBMP("output.bmp", 8, 0);
else if( samplePerPixel == 3 )
di->writeBMP("output.bmp", 24, 0);
delete di;
//Here you can do what you want with the BMP file containing the pixels of the DICOM image
if(xfer >= EXS_JPEGProcess1TransferSyntax && xfer <= EXS_JPEGProcess14SV1TransferSyntax )
DJDecoderRegistration::cleanup();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment