comparison OrthancFramework/Sources/DicomParsing/ParsedDicomFile.cpp @ 5348:303e930fff0f

"/tools/create-dicom" can now be used to create Encapsulated 3D Manufacturing Model IODs (MTL, OBJ, or STL)
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 02 Jul 2023 16:24:56 +0200
parents 138e9d0c08c1
children c5f1865aaa3b
comparison
equal deleted inserted replaced
5347:4f686f6150c7 5348:303e930fff0f
1201 case MimeType_Binary: 1201 case MimeType_Binary:
1202 EmbedImage(mime, content); 1202 EmbedImage(mime, content);
1203 break; 1203 break;
1204 1204
1205 case MimeType_Pdf: 1205 case MimeType_Pdf:
1206 EmbedPdf(content); 1206 {
1207 if (content.size() < 5 || // (*)
1208 strncmp("%PDF-", content.c_str(), 5) != 0)
1209 {
1210 throw OrthancException(ErrorCode_BadFileFormat, "Not a PDF file");
1211 }
1212
1213 EncapsulateDocument(MimeType_Pdf, content);
1214
1215 // In Orthanc <= 1.9.7, the "Modality" would have always be overwritten as "OT"
1216 // https://groups.google.com/g/orthanc-users/c/eNSddNrQDtM/m/wc1HahimAAAJ
1217
1218 SetIfAbsent(DICOM_TAG_SOP_CLASS_UID, UID_EncapsulatedPDFStorage);
1219 SetIfAbsent(DICOM_TAG_MODALITY, "OT");
1220 SetIfAbsent(FromDcmtkBridge::Convert(DCM_ConversionType), "WSD");
1221 //SetIfAbsent(FromDcmtkBridge::Convert(DCM_SeriesNumber), "1");
1222
1223 break;
1224 }
1225
1226 case MimeType_Mtl:
1227 EncapsulateDocument(mime, content);
1228 SetIfAbsent(DICOM_TAG_SOP_CLASS_UID, "1.2.840.10008.5.1.4.1.1.104.5");
1229 SetIfAbsent(DICOM_TAG_MODALITY, "M3D");
1230 break;
1231
1232 case MimeType_Obj:
1233 EncapsulateDocument(mime, content);
1234 SetIfAbsent(DICOM_TAG_SOP_CLASS_UID, "1.2.840.10008.5.1.4.1.1.104.4");
1235 SetIfAbsent(DICOM_TAG_MODALITY, "M3D");
1236 break;
1237
1238 case MimeType_Stl:
1239 EncapsulateDocument(mime, content);
1240 SetIfAbsent(DICOM_TAG_SOP_CLASS_UID, "1.2.840.10008.5.1.4.1.1.104.3");
1241 SetIfAbsent(DICOM_TAG_MODALITY, "M3D");
1207 break; 1242 break;
1208 1243
1209 default: 1244 default:
1210 throw OrthancException(ErrorCode_NotImplemented, 1245 throw OrthancException(ErrorCode_NotImplemented,
1211 "Unsupported MIME type for the content of a new DICOM file: " + 1246 "Unsupported MIME type for the content of a new DICOM file: " +
1524 DcmTag key(tag.GetGroup(), tag.GetElement()); 1559 DcmTag key(tag.GetGroup(), tag.GetElement());
1525 return GetDcmtkObjectConst().getDataset()->tagExists(key); 1560 return GetDcmtkObjectConst().getDataset()->tagExists(key);
1526 } 1561 }
1527 1562
1528 1563
1529 void ParsedDicomFile::EmbedPdf(const std::string& pdf) 1564 void ParsedDicomFile::EncapsulateDocument(MimeType mime,
1530 { 1565 const std::string& document)
1531 if (pdf.size() < 5 || // (*) 1566 {
1532 strncmp("%PDF-", pdf.c_str(), 5) != 0)
1533 {
1534 throw OrthancException(ErrorCode_BadFileFormat, "Not a PDF file");
1535 }
1536
1537 InvalidateCache(); 1567 InvalidateCache();
1538 1568
1539 // In Orthanc <= 1.9.7, the "Modality" would have always be overwritten as "OT" 1569 ReplacePlainString(FromDcmtkBridge::Convert(DCM_MIMETypeOfEncapsulatedDocument), EnumerationToString(mime));
1540 // https://groups.google.com/g/orthanc-users/c/eNSddNrQDtM/m/wc1HahimAAAJ
1541
1542 ReplacePlainString(DICOM_TAG_SOP_CLASS_UID, UID_EncapsulatedPDFStorage);
1543 SetIfAbsent(FromDcmtkBridge::Convert(DCM_Modality), "OT");
1544 SetIfAbsent(FromDcmtkBridge::Convert(DCM_ConversionType), "WSD");
1545 SetIfAbsent(FromDcmtkBridge::Convert(DCM_MIMETypeOfEncapsulatedDocument), MIME_PDF);
1546 //SetIfAbsent(FromDcmtkBridge::Convert(DCM_SeriesNumber), "1");
1547 1570
1548 std::unique_ptr<DcmPolymorphOBOW> element(new DcmPolymorphOBOW(DCM_EncapsulatedDocument)); 1571 std::unique_ptr<DcmPolymorphOBOW> element(new DcmPolymorphOBOW(DCM_EncapsulatedDocument));
1549 1572
1550 size_t s = pdf.size(); 1573 size_t s = document.size();
1551 if (s & 1) 1574 if (s & 1)
1552 { 1575 {
1553 // The size of the buffer must be even 1576 // The size of the buffer must be even
1554 s += 1; 1577 s += 1;
1555 } 1578 }
1559 if (!result.good() || bytes == NULL) 1582 if (!result.good() || bytes == NULL)
1560 { 1583 {
1561 throw OrthancException(ErrorCode_NotEnoughMemory); 1584 throw OrthancException(ErrorCode_NotEnoughMemory);
1562 } 1585 }
1563 1586
1564 // Blank pad byte (no access violation, as "pdf.size() >= 5" because of (*) ) 1587 if (s > 0)
1565 bytes[s - 1] = 0; 1588 {
1566 1589 bytes[s - 1] = 0;
1567 memcpy(bytes, pdf.c_str(), pdf.size()); 1590 }
1591
1592 memcpy(bytes, document.c_str(), document.size());
1568 1593
1569 DcmPolymorphOBOW* obj = element.release(); 1594 DcmPolymorphOBOW* obj = element.release();
1570 result = GetDcmtkObject().getDataset()->insert(obj); 1595 result = GetDcmtkObject().getDataset()->insert(obj);
1571 1596
1572 if (!result.good()) 1597 if (!result.good())