comparison OrthancServer/ParsedDicomFile.cpp @ 1555:d6a93e12b1c1

Creation of DICOM files with encapsulated PDF
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 20 Aug 2015 15:18:13 +0200
parents c388502a066d
children b8dc2f855a83
comparison
equal deleted inserted replaced
1554:89ab71a68fcf 1555:d6a93e12b1c1
105 #include <dcmtk/dcmdata/dcdict.h> 105 #include <dcmtk/dcmdata/dcdict.h>
106 #include <dcmtk/dcmdata/dcfilefo.h> 106 #include <dcmtk/dcmdata/dcfilefo.h>
107 #include <dcmtk/dcmdata/dcistrmb.h> 107 #include <dcmtk/dcmdata/dcistrmb.h>
108 #include <dcmtk/dcmdata/dcuid.h> 108 #include <dcmtk/dcmdata/dcuid.h>
109 #include <dcmtk/dcmdata/dcmetinf.h> 109 #include <dcmtk/dcmdata/dcmetinf.h>
110 #include <dcmtk/dcmdata/dcdeftag.h>
110 111
111 #include <dcmtk/dcmdata/dcvrae.h> 112 #include <dcmtk/dcmdata/dcvrae.h>
112 #include <dcmtk/dcmdata/dcvras.h> 113 #include <dcmtk/dcmdata/dcvras.h>
113 #include <dcmtk/dcmdata/dcvrcs.h> 114 #include <dcmtk/dcmdata/dcvrcs.h>
114 #include <dcmtk/dcmdata/dcvrda.h> 115 #include <dcmtk/dcmdata/dcvrda.h>
1131 } 1132 }
1132 1133
1133 1134
1134 void ParsedDicomFile::EmbedImage(const std::string& dataUriScheme) 1135 void ParsedDicomFile::EmbedImage(const std::string& dataUriScheme)
1135 { 1136 {
1136 std::string mime, content; 1137 std::string mime, base64;
1137 Toolbox::DecodeDataUriScheme(mime, content, dataUriScheme); 1138 Toolbox::DecodeDataUriScheme(mime, base64, dataUriScheme);
1138 1139
1139 std::string decoded; 1140 std::string content;
1140 Toolbox::DecodeBase64(decoded, content); 1141 Toolbox::DecodeBase64(content, base64);
1141 1142
1143 EmbedImage(mime, content);
1144 }
1145
1146
1147 void ParsedDicomFile::EmbedImage(const std::string& mime,
1148 const std::string& content)
1149 {
1142 if (mime == "image/png") 1150 if (mime == "image/png")
1143 { 1151 {
1144 PngReader reader; 1152 PngReader reader;
1145 reader.ReadFromMemory(decoded); 1153 reader.ReadFromMemory(content);
1146 EmbedImage(reader); 1154 EmbedImage(reader);
1147 } 1155 }
1148 else 1156 else
1149 { 1157 {
1150 throw OrthancException(ErrorCode_NotImplemented); 1158 throw OrthancException(ErrorCode_NotImplemented);
1406 else 1414 else
1407 { 1415 {
1408 FromDcmtkBridge::ToJson(target, *pimpl_->file_->getDataset()); 1416 FromDcmtkBridge::ToJson(target, *pimpl_->file_->getDataset());
1409 } 1417 }
1410 } 1418 }
1419
1420
1421 bool ParsedDicomFile::HasTag(const DicomTag& tag) const
1422 {
1423 DcmTag key(tag.GetGroup(), tag.GetElement());
1424 return pimpl_->file_->getDataset()->tagExists(key);
1425 }
1426
1427
1428 void ParsedDicomFile::EmbedPdf(const std::string& pdf)
1429 {
1430 if (pdf.size() < 5 || // (*)
1431 strncmp("%PDF-", pdf.c_str(), 5) != 0)
1432 {
1433 LOG(ERROR) << "Not a PDF file";
1434 throw OrthancException(ErrorCode_BadFileFormat);
1435 }
1436
1437 Replace(DICOM_TAG_SOP_CLASS_UID, UID_EncapsulatedPDFStorage);
1438 Replace(FromDcmtkBridge::Convert(DCM_Modality), "OT");
1439 Replace(FromDcmtkBridge::Convert(DCM_ConversionType), "WSD");
1440 Replace(FromDcmtkBridge::Convert(DCM_MIMETypeOfEncapsulatedDocument), "application/pdf");
1441 Replace(FromDcmtkBridge::Convert(DCM_SeriesNumber), "1");
1442
1443 std::auto_ptr<DcmPolymorphOBOW> element(new DcmPolymorphOBOW(DCM_EncapsulatedDocument));
1444
1445 size_t s = pdf.size();
1446 if (s & 1)
1447 {
1448 // The size of the buffer must be even
1449 s += 1;
1450 }
1451
1452 Uint8* bytes = NULL;
1453 OFCondition result = element->createUint8Array(s, bytes);
1454 if (!result.good() || bytes == NULL)
1455 {
1456 throw OrthancException(ErrorCode_NotEnoughMemory);
1457 }
1458
1459 // Blank pad byte (no access violation, as "pdf.size() >= 5" because of (*) )
1460 bytes[s - 1] = 0;
1461
1462 memcpy(bytes, pdf.c_str(), pdf.size());
1463
1464 DcmPolymorphOBOW* obj = element.release();
1465 result = pimpl_->file_->getDataset()->insert(obj);
1466
1467 if (!result.good())
1468 {
1469 delete obj;
1470 throw OrthancException(ErrorCode_NotEnoughMemory);
1471 }
1472 }
1473
1411 } 1474 }