comparison Core/DicomParsing/FromDcmtkBridge.cpp @ 3896:210af28c4087 transcoding

merge
author Alain Mazy <alain@mazy.be>
date Thu, 07 May 2020 11:32:15 +0200
parents 37cf1889667a e23026566536
children 74eeadf5d51d
comparison
equal deleted inserted replaced
3895:37cf1889667a 3896:210af28c4087
1204 throw OrthancException(ErrorCode_ParameterOutOfRange); 1204 throw OrthancException(ErrorCode_ParameterOutOfRange);
1205 } 1205 }
1206 } 1206 }
1207 1207
1208 1208
1209 1209
1210 static bool SaveToMemoryBufferInternal(std::string& buffer, 1210 static bool SaveToMemoryBufferInternal(std::string& buffer,
1211 DcmFileFormat& dicom, 1211 DcmFileFormat& dicom,
1212 E_TransferSyntax xfer) 1212 E_TransferSyntax xfer)
1213 { 1213 {
1214 E_EncodingType encodingType = /*opt_sequenceType*/ EET_ExplicitLength; 1214 E_EncodingType encodingType = /*opt_sequenceType*/ EET_ExplicitLength;
1267 * Note that up to Orthanc 0.7.1 (inclusive), the 1267 * Note that up to Orthanc 0.7.1 (inclusive), the
1268 * "EXS_LittleEndianExplicit" was always used to save the DICOM 1268 * "EXS_LittleEndianExplicit" was always used to save the DICOM
1269 * dataset into memory. We now keep the original transfer syntax 1269 * dataset into memory. We now keep the original transfer syntax
1270 * (if available). 1270 * (if available).
1271 **/ 1271 **/
1272 E_TransferSyntax xfer = dataSet.getOriginalXfer(); 1272 E_TransferSyntax xfer = dataSet.getCurrentXfer();
1273 if (xfer == EXS_Unknown) 1273 if (xfer == EXS_Unknown)
1274 { 1274 {
1275 // No information about the original transfer syntax: This is 1275 // No information about the original transfer syntax: This is
1276 // most probably a DICOM dataset that was read from memory. 1276 // most probably a DICOM dataset that was read from memory.
1277 xfer = EXS_LittleEndianExplicit; 1277 xfer = EXS_LittleEndianExplicit;
1284 1284
1285 return SaveToMemoryBufferInternal(buffer, ff, xfer); 1285 return SaveToMemoryBufferInternal(buffer, ff, xfer);
1286 } 1286 }
1287 1287
1288 1288
1289 bool FromDcmtkBridge::SaveToMemoryBuffer(std::string& buffer, 1289 bool FromDcmtkBridge::Transcode(DcmFileFormat& dicom,
1290 DcmFileFormat& dicom)
1291 {
1292 E_TransferSyntax xfer = dicom.getDataset()->getOriginalXfer();
1293 if (xfer == EXS_Unknown)
1294 {
1295 throw OrthancException(ErrorCode_InternalError,
1296 "Cannot write a DICOM instance with unknown transfer syntax");
1297 }
1298 else if (!dicom.validateMetaInfo(xfer).good())
1299 {
1300 throw OrthancException(ErrorCode_InternalError,
1301 "Cannot setup the transfer syntax to write a DICOM instance");
1302 }
1303 else
1304 {
1305 return SaveToMemoryBufferInternal(buffer, dicom, xfer);
1306 }
1307 }
1308
1309
1310 bool FromDcmtkBridge::Transcode(std::string& buffer,
1311 DcmFileFormat& dicom,
1312 DicomTransferSyntax syntax, 1290 DicomTransferSyntax syntax,
1313 const DcmRepresentationParameter* representation) 1291 const DcmRepresentationParameter* representation)
1314 { 1292 {
1315 E_TransferSyntax xfer; 1293 E_TransferSyntax xfer;
1316 if (!LookupDcmtkTransferSyntax(xfer, syntax)) 1294 if (!LookupDcmtkTransferSyntax(xfer, syntax))
1317 { 1295 {
1318 throw OrthancException(ErrorCode_InternalError); 1296 throw OrthancException(ErrorCode_InternalError);
1319 } 1297 }
1320 else 1298 else
1321 { 1299 {
1300 DicomTransferSyntax sourceSyntax;
1301 bool known = LookupOrthancTransferSyntax(sourceSyntax, dicom);
1302
1322 if (!dicom.getDataset()->chooseRepresentation(xfer, representation).good() || 1303 if (!dicom.getDataset()->chooseRepresentation(xfer, representation).good() ||
1323 !dicom.getDataset()->canWriteXfer(xfer) || 1304 !dicom.getDataset()->canWriteXfer(xfer) ||
1324 !dicom.validateMetaInfo(xfer, EWM_updateMeta).good()) 1305 !dicom.validateMetaInfo(xfer, EWM_updateMeta).good())
1325 { 1306 {
1326 return false; 1307 return false;
1327 } 1308 }
1328 1309 else
1329 dicom.removeInvalidGroups(); 1310 {
1330 1311 dicom.removeInvalidGroups();
1331 return SaveToMemoryBufferInternal(buffer, dicom, xfer); 1312
1313 if (known)
1314 {
1315 LOG(INFO) << "Transcoded an image from transfer syntax "
1316 << GetTransferSyntaxUid(sourceSyntax) << " to "
1317 << GetTransferSyntaxUid(syntax);
1318 }
1319 else
1320 {
1321 LOG(INFO) << "Transcoded an image from unknown transfer syntax to "
1322 << GetTransferSyntaxUid(syntax);
1323 }
1324
1325 return true;
1326 }
1332 } 1327 }
1333 } 1328 }
1334 1329
1335 1330
1336 ValueRepresentation FromDcmtkBridge::LookupValueRepresentation(const DicomTag& tag) 1331 ValueRepresentation FromDcmtkBridge::LookupValueRepresentation(const DicomTag& tag)
1785 } 1780 }
1786 1781
1787 DcmPixelData& pixelData = dynamic_cast<DcmPixelData&>(*element); 1782 DcmPixelData& pixelData = dynamic_cast<DcmPixelData&>(*element);
1788 DcmPixelSequence* pixelSequence = NULL; 1783 DcmPixelSequence* pixelSequence = NULL;
1789 if (!pixelData.getEncapsulatedRepresentation 1784 if (!pixelData.getEncapsulatedRepresentation
1790 (dataset.getOriginalXfer(), NULL, pixelSequence).good()) 1785 (dataset.getCurrentXfer(), NULL, pixelSequence).good())
1791 { 1786 {
1792 return NULL; 1787 return NULL;
1793 } 1788 }
1794 else 1789 else
1795 { 1790 {
2034 { 2029 {
2035 ChangeStringEncoding(*sequence.getItem(j), source, hasSourceCodeExtensions, target); 2030 ChangeStringEncoding(*sequence.getItem(j), source, hasSourceCodeExtensions, target);
2036 } 2031 }
2037 } 2032 }
2038 } 2033 }
2039 }
2040 }
2041
2042
2043 bool FromDcmtkBridge::LookupTransferSyntax(std::string& result,
2044 DcmFileFormat& dicom)
2045 {
2046 const char* value = NULL;
2047
2048 if (dicom.getMetaInfo() != NULL &&
2049 dicom.getMetaInfo()->findAndGetString(DCM_TransferSyntaxUID, value).good() &&
2050 value != NULL)
2051 {
2052 result.assign(value);
2053 return true;
2054 }
2055 else
2056 {
2057 return false;
2058 } 2034 }
2059 } 2035 }
2060 2036
2061 2037
2062 #if ORTHANC_ENABLE_LUA == 1 2038 #if ORTHANC_ENABLE_LUA == 1
2653 std::vector<size_t> parentIndexes; 2629 std::vector<size_t> parentIndexes;
2654 bool hasCodeExtensions; 2630 bool hasCodeExtensions;
2655 Encoding encoding = DetectEncoding(hasCodeExtensions, dataset, defaultEncoding); 2631 Encoding encoding = DetectEncoding(hasCodeExtensions, dataset, defaultEncoding);
2656 ApplyVisitorToDataset(dataset, visitor, parentTags, parentIndexes, encoding, hasCodeExtensions); 2632 ApplyVisitorToDataset(dataset, visitor, parentTags, parentIndexes, encoding, hasCodeExtensions);
2657 } 2633 }
2634
2635
2636
2637 bool FromDcmtkBridge::LookupOrthancTransferSyntax(DicomTransferSyntax& target,
2638 DcmFileFormat& dicom)
2639 {
2640 if (dicom.getDataset() == NULL)
2641 {
2642 throw OrthancException(ErrorCode_InternalError);
2643 }
2644
2645 DcmDataset& dataset = *dicom.getDataset();
2646
2647 E_TransferSyntax xfer = dataset.getCurrentXfer();
2648 if (xfer == EXS_Unknown)
2649 {
2650 dataset.updateOriginalXfer();
2651 xfer = dataset.getOriginalXfer();
2652 if (xfer == EXS_Unknown)
2653 {
2654 throw OrthancException(ErrorCode_BadFileFormat,
2655 "Cannot determine the transfer syntax of the DICOM instance");
2656 }
2657 }
2658
2659 return FromDcmtkBridge::LookupOrthancTransferSyntax(target, xfer);
2660 }
2658 } 2661 }
2659 2662
2660 2663
2661 #include "./FromDcmtkBridge_TransferSyntaxes.impl.h" 2664 #include "./FromDcmtkBridge_TransferSyntaxes.impl.h"