comparison OrthancServer/ParsedDicomFile.cpp @ 1818:1065401501fb worklists

ParsedDicomFile::CreateFromJson
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 24 Nov 2015 16:48:23 +0100
parents f08978b1f45b
children b530c3dfe2a6
comparison
equal deleted inserted replaced
1817:9a6de24209cf 1818:1065401501fb
78 #define NOMINMAX 78 #define NOMINMAX
79 #endif 79 #endif
80 80
81 #include "ParsedDicomFile.h" 81 #include "ParsedDicomFile.h"
82 82
83 #include "OrthancInitialization.h"
83 #include "ServerToolbox.h" 84 #include "ServerToolbox.h"
84 #include "FromDcmtkBridge.h" 85 #include "FromDcmtkBridge.h"
85 #include "ToDcmtkBridge.h" 86 #include "ToDcmtkBridge.h"
86 #include "Internals/DicomImageDecoder.h" 87 #include "Internals/DicomImageDecoder.h"
87 #include "../Core/DicomFormat/DicomIntegerPixelAccessor.h" 88 #include "../Core/DicomFormat/DicomIntegerPixelAccessor.h"
591 } 592 }
592 593
593 594
594 void ParsedDicomFile::Insert(const DicomTag& tag, 595 void ParsedDicomFile::Insert(const DicomTag& tag,
595 const Json::Value& value, 596 const Json::Value& value,
596 bool decodeBinaryTags) 597 bool decodeDataUriScheme)
597 { 598 {
598 std::auto_ptr<DcmElement> element(FromDcmtkBridge::FromJson(tag, value, decodeBinaryTags, GetEncoding())); 599 std::auto_ptr<DcmElement> element(FromDcmtkBridge::FromJson(tag, value, decodeDataUriScheme, GetEncoding()));
599 InsertInternal(*pimpl_->file_->getDataset(), element.release()); 600 InsertInternal(*pimpl_->file_->getDataset(), element.release());
600 } 601 }
601 602
602 603
603 static void ReplaceInternal(DcmDataset& dicom, 604 static void ReplaceInternal(DcmDataset& dicom,
628 } 629 }
629 630
630 631
631 void ParsedDicomFile::UpdateStorageUid(const DicomTag& tag, 632 void ParsedDicomFile::UpdateStorageUid(const DicomTag& tag,
632 const std::string& utf8Value, 633 const std::string& utf8Value,
633 bool decodeBinaryTags) 634 bool decodeDataUriScheme)
634 { 635 {
635 if (tag != DICOM_TAG_SOP_CLASS_UID && 636 if (tag != DICOM_TAG_SOP_CLASS_UID &&
636 tag != DICOM_TAG_SOP_INSTANCE_UID) 637 tag != DICOM_TAG_SOP_INSTANCE_UID)
637 { 638 {
638 return; 639 return;
639 } 640 }
640 641
641 std::string binary; 642 std::string binary;
642 const std::string* decoded = &utf8Value; 643 const std::string* decoded = &utf8Value;
643 644
644 if (decodeBinaryTags && 645 if (decodeDataUriScheme &&
645 boost::starts_with(utf8Value, "data:application/octet-stream;base64,")) 646 boost::starts_with(utf8Value, "data:application/octet-stream;base64,"))
646 { 647 {
647 std::string mime; 648 std::string mime;
648 Toolbox::DecodeDataUriScheme(mime, binary, utf8Value); 649 Toolbox::DecodeDataUriScheme(mime, binary, utf8Value);
649 decoded = &binary; 650 decoded = &binary;
690 } 691 }
691 692
692 693
693 void ParsedDicomFile::Replace(const DicomTag& tag, 694 void ParsedDicomFile::Replace(const DicomTag& tag,
694 const Json::Value& value, 695 const Json::Value& value,
695 bool decodeBinaryTags, 696 bool decodeDataUriScheme,
696 DicomReplaceMode mode) 697 DicomReplaceMode mode)
697 { 698 {
698 std::auto_ptr<DcmElement> element(FromDcmtkBridge::FromJson(tag, value, decodeBinaryTags, GetEncoding())); 699 std::auto_ptr<DcmElement> element(FromDcmtkBridge::FromJson(tag, value, decodeDataUriScheme, GetEncoding()));
699 ReplaceInternal(*pimpl_->file_->getDataset(), element, mode); 700 ReplaceInternal(*pimpl_->file_->getDataset(), element, mode);
700 701
701 if (tag == DICOM_TAG_SOP_CLASS_UID || 702 if (tag == DICOM_TAG_SOP_CLASS_UID ||
702 tag == DICOM_TAG_SOP_INSTANCE_UID) 703 tag == DICOM_TAG_SOP_INSTANCE_UID)
703 { 704 {
704 if (value.type() != Json::stringValue) 705 if (value.type() != Json::stringValue)
705 { 706 {
706 throw OrthancException(ErrorCode_BadParameterType); 707 throw OrthancException(ErrorCode_BadParameterType);
707 } 708 }
708 709
709 UpdateStorageUid(tag, value.asString(), decodeBinaryTags); 710 UpdateStorageUid(tag, value.asString(), decodeDataUriScheme);
710 } 711 }
711 } 712 }
712 713
713 714
714 void ParsedDicomFile::Answer(RestApiOutput& output) 715 void ParsedDicomFile::Answer(RestApiOutput& output)
726 const DicomTag& tag) 727 const DicomTag& tag)
727 { 728 {
728 DcmTagKey k(tag.GetGroup(), tag.GetElement()); 729 DcmTagKey k(tag.GetGroup(), tag.GetElement());
729 DcmDataset& dataset = *pimpl_->file_->getDataset(); 730 DcmDataset& dataset = *pimpl_->file_->getDataset();
730 731
731 if (FromDcmtkBridge::IsPrivateTag(tag) || 732 if (tag.IsPrivate() ||
732 FromDcmtkBridge::IsUnknownTag(tag) || 733 FromDcmtkBridge::IsUnknownTag(tag) ||
733 tag == DICOM_TAG_PIXEL_DATA || 734 tag == DICOM_TAG_PIXEL_DATA ||
734 tag == DICOM_TAG_ENCAPSULATED_DOCUMENT) 735 tag == DICOM_TAG_ENCAPSULATED_DOCUMENT)
735 { 736 {
736 const Uint8* data = NULL; // This is freed in the destructor of the dataset 737 const Uint8* data = NULL; // This is freed in the destructor of the dataset
1250 1251
1251 void ParsedDicomFile::Convert(DicomMap& tags) 1252 void ParsedDicomFile::Convert(DicomMap& tags)
1252 { 1253 {
1253 FromDcmtkBridge::Convert(tags, *pimpl_->file_->getDataset()); 1254 FromDcmtkBridge::Convert(tags, *pimpl_->file_->getDataset());
1254 } 1255 }
1256
1257
1258 ParsedDicomFile* ParsedDicomFile::CreateFromJson(const Json::Value& json,
1259 DicomFromJsonFlags flags)
1260 {
1261 std::string tmp = Configuration::GetGlobalStringParameter("DefaultEncoding", "Latin1");
1262 Encoding encoding = StringToEncoding(tmp.c_str());
1263
1264 Json::Value::Members tags = json.getMemberNames();
1265
1266 for (size_t i = 0; i < tags.size(); i++)
1267 {
1268 DicomTag tag = FromDcmtkBridge::ParseTag(tags[i]);
1269 if (tag == DICOM_TAG_SPECIFIC_CHARACTER_SET)
1270 {
1271 const Json::Value& value = json[tags[i]];
1272 if (value.type() != Json::stringValue ||
1273 !GetDicomEncoding(encoding, value.asCString()))
1274 {
1275 LOG(ERROR) << "Unknown encoding while creating DICOM from JSON: " << value;
1276 throw OrthancException(ErrorCode_BadRequest);
1277 }
1278 }
1279 }
1280
1281 const bool generateIdentifiers = (flags & DicomFromJsonFlags_GenerateIdentifiers);
1282 const bool decodeDataUriScheme = (flags & DicomFromJsonFlags_DecodeDataUriScheme);
1283
1284 std::auto_ptr<ParsedDicomFile> result(new ParsedDicomFile(generateIdentifiers));
1285 result->SetEncoding(encoding);
1286
1287 for (size_t i = 0; i < tags.size(); i++)
1288 {
1289 DicomTag tag = FromDcmtkBridge::ParseTag(tags[i]);
1290 const Json::Value& value = json[tags[i]];
1291
1292 if (tag == DICOM_TAG_PIXEL_DATA ||
1293 tag == DICOM_TAG_ENCAPSULATED_DOCUMENT)
1294 {
1295 if (value.type() != Json::stringValue)
1296 {
1297 throw OrthancException(ErrorCode_BadRequest);
1298 }
1299 else
1300 {
1301 result->EmbedContent(value.asString());
1302 }
1303 }
1304 else if (tag != DICOM_TAG_SPECIFIC_CHARACTER_SET)
1305 {
1306 result->Replace(tag, value, decodeDataUriScheme);
1307 }
1308 }
1309
1310 return result.release();
1311 }
1255 } 1312 }