Skip to content

Instantly share code, notes, and snippets.

@JulesGorny
Last active August 29, 2015 14:06
Show Gist options
  • Save JulesGorny/4a2d44bc83dc9145e009 to your computer and use it in GitHub Desktop.
Save JulesGorny/4a2d44bc83dc9145e009 to your computer and use it in GitHub Desktop.
//Open the DICOM file and get the dictionary
DcmFileFormat fileformat;
OFCondition status = fileformat.loadFile(file);
DcmDataset *dictionary;
if (status.good())
dictionary = fileformat.getDataset();
/////////////////////////////////////////
// Modify anything in the DICOM file //
/////////////////////////////////////////
//Get the transfer syntax to check if we need jpeg codecs
E_TransferSyntax xfer = dictionary->getOriginalXfer();
if(xfer >= EXS_JPEGProcess1TransferSyntax && xfer <= EXS_JPEGProcess14SV1TransferSyntax )
{
DJEncoderRegistration::registerCodecs(); // register JPEG codecs
DJ_RPLossless params; // codec parameters, we use the defaults
// this causes the lossless JPEG version of the dataset to be created
dictionary->chooseRepresentation(xfer, &params);
}
DcmItem *metaInfo = fileformat->getMetaInfo();
// force the meta-header UIDs to be re-generated when storing the file
// since the UIDs in the data set may have changed
delete metaInfo->remove(DCM_MediaStorageSOPClassUID);
delete metaInfo->remove(DCM_MediaStorageSOPInstanceUID);
fileformat->loadAllDataIntoMemory();
if( EC_Normal != fileformat->saveFile(filePath.toStdString().c_str(), xfer) )
std::cout << "Cannot update the image." std::endl;
if(xfer >= EXS_JPEGProcess1TransferSyntax && xfer <= EXS_JPEGProcess14SV1TransferSyntax )
DJEncoderRegistration::cleanup();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment