comparison Core/DicomParsing/FromDcmtkBridge.cpp @ 2954:d924f9bb61cc

taking advantage of details in OrthancException
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 03 Dec 2018 14:35:34 +0100
parents ae20fccdd867
children 4e43e67f8ecf
comparison
equal deleted inserted replaced
2953:210d5afd8f2b 2954:d924f9bb61cc
117 TemporaryFile tmp; 117 TemporaryFile tmp;
118 tmp.Write(content); 118 tmp.Write(content);
119 119
120 if (!dictionary.loadDictionary(tmp.GetPath().c_str())) 120 if (!dictionary.loadDictionary(tmp.GetPath().c_str()))
121 { 121 {
122 LOG(ERROR) << "Cannot read embedded dictionary. Under Windows, make sure that " 122 throw OrthancException(ErrorCode_InternalError,
123 << "your TEMP directory does not contain special characters."; 123 "Cannot read embedded dictionary. Under Windows, make sure that "
124 throw OrthancException(ErrorCode_InternalError); 124 "your TEMP directory does not contain special characters.");
125 } 125 }
126 #else 126 #else
127 if (!dictionary.loadFromMemory(content)) 127 if (!dictionary.loadFromMemory(content))
128 { 128 {
129 LOG(ERROR) << "Cannot read embedded dictionary. Under Windows, make sure that " 129 throw OrthancException(ErrorCode_InternalError,
130 << "your TEMP directory does not contain special characters."; 130 "Cannot read embedded dictionary. Under Windows, make sure that "
131 throw OrthancException(ErrorCode_InternalError); 131 "your TEMP directory does not contain special characters.");
132 } 132 }
133 #endif 133 #endif
134 } 134 }
135 135
136 #else 136 #else
287 } 287 }
288 288
289 /* make sure data dictionary is loaded */ 289 /* make sure data dictionary is loaded */
290 if (!dcmDataDict.isDictionaryLoaded()) 290 if (!dcmDataDict.isDictionaryLoaded())
291 { 291 {
292 LOG(ERROR) << "No DICOM dictionary loaded, check environment variable: " << DCM_DICT_ENVIRONMENT_VARIABLE; 292 throw OrthancException(ErrorCode_InternalError,
293 throw OrthancException(ErrorCode_InternalError); 293 "No DICOM dictionary loaded, check environment variable: " +
294 std::string(DCM_DICT_ENVIRONMENT_VARIABLE));
294 } 295 }
295 296
296 { 297 {
297 // Test the dictionary with a simple DICOM tag 298 // Test the dictionary with a simple DICOM tag
298 DcmTag key(0x0010, 0x1030); // This is PatientWeight 299 DcmTag key(0x0010, 0x1030); // This is PatientWeight
299 if (key.getEVR() != EVR_DS) 300 if (key.getEVR() != EVR_DS)
300 { 301 {
301 LOG(ERROR) << "The DICOM dictionary has not been correctly read"; 302 throw OrthancException(ErrorCode_InternalError,
302 throw OrthancException(ErrorCode_InternalError); 303 "The DICOM dictionary has not been correctly read");
303 } 304 }
304 } 305 }
305 } 306 }
306 307
307 308
368 tag.GetGroup() == 0xffff) 369 tag.GetGroup() == 0xffff)
369 { 370 {
370 char buf[128]; 371 char buf[128];
371 sprintf(buf, "Trying to register private tag (%04x,%04x), but it must have an odd group >= 0x0009", 372 sprintf(buf, "Trying to register private tag (%04x,%04x), but it must have an odd group >= 0x0009",
372 tag.GetGroup(), tag.GetElement()); 373 tag.GetGroup(), tag.GetElement());
373 LOG(ERROR) << buf; 374 throw OrthancException(ErrorCode_ParameterOutOfRange, std::string(buf));
374 throw OrthancException(ErrorCode_ParameterOutOfRange);
375 } 375 }
376 376
377 entry.reset(new DcmDictEntry(tag.GetGroup(), 377 entry.reset(new DcmDictEntry(tag.GetGroup(),
378 tag.GetElement(), 378 tag.GetElement(),
379 evr, name.c_str(), 379 evr, name.c_str(),
390 { 390 {
391 DictionaryLocker locker; 391 DictionaryLocker locker;
392 392
393 if (locker->findEntry(name.c_str())) 393 if (locker->findEntry(name.c_str()))
394 { 394 {
395 LOG(ERROR) << "Cannot register two tags with the same symbolic name \"" << name << "\""; 395 throw OrthancException(ErrorCode_AlreadyExistingTag,
396 throw OrthancException(ErrorCode_AlreadyExistingTag); 396 "Cannot register two tags with the same symbolic name \"" + name + "\"");
397 } 397 }
398 398
399 locker->addEntry(entry.release()); 399 locker->addEntry(entry.release());
400 } 400 }
401 } 401 }
1671 ok = false; 1671 ok = false;
1672 } 1672 }
1673 1673
1674 if (!ok) 1674 if (!ok)
1675 { 1675 {
1676 LOG(ERROR) << "While creating a DICOM instance, tag (" << tag.Format() 1676 throw OrthancException(ErrorCode_BadFileFormat,
1677 << ") has out-of-range value: \"" << *decoded << "\""; 1677 "While creating a DICOM instance, tag (" + tag.Format() +
1678 throw OrthancException(ErrorCode_BadFileFormat); 1678 ") has out-of-range value: \"" + (*decoded) + "\"");
1679 } 1679 }
1680 } 1680 }
1681 1681
1682 1682
1683 DcmElement* FromDcmtkBridge::FromJson(const DicomTag& tag, 1683 DcmElement* FromDcmtkBridge::FromJson(const DicomTag& tag,
1798 const Json::Value& value = json[tags[i]]; 1798 const Json::Value& value = json[tags[i]];
1799 if (value.type() != Json::stringValue || 1799 if (value.type() != Json::stringValue ||
1800 (value.asString().length() != 0 && 1800 (value.asString().length() != 0 &&
1801 !GetDicomEncoding(encoding, value.asCString()))) 1801 !GetDicomEncoding(encoding, value.asCString())))
1802 { 1802 {
1803 LOG(ERROR) << "Unknown encoding while creating DICOM from JSON: " << value; 1803 throw OrthancException(ErrorCode_BadRequest,
1804 throw OrthancException(ErrorCode_BadRequest); 1804 "Unknown encoding while creating DICOM from JSON: " +
1805 value.toStyledString());
1805 } 1806 }
1806 1807
1807 if (value.asString().length() == 0) 1808 if (value.asString().length() == 0)
1808 { 1809 {
1809 return defaultEncoding; 1810 return defaultEncoding;
1922 std::auto_ptr<DcmFileFormat> result(new DcmFileFormat); 1923 std::auto_ptr<DcmFileFormat> result(new DcmFileFormat);
1923 1924
1924 result->transferInit(); 1925 result->transferInit();
1925 if (!result->read(is).good()) 1926 if (!result->read(is).good())
1926 { 1927 {
1927 LOG(ERROR) << "Cannot parse an invalid DICOM file (size: " << size << " bytes)"; 1928 throw OrthancException(ErrorCode_BadFileFormat,
1928 throw OrthancException(ErrorCode_BadFileFormat); 1929 "Cannot parse an invalid DICOM file (size: " +
1930 boost::lexical_cast<std::string>(size) + " bytes)");
1929 } 1931 }
1930 1932
1931 result->loadAllDataIntoMemory(); 1933 result->loadAllDataIntoMemory();
1932 result->transferEnd(); 1934 result->transferEnd();
1933 1935
2042 return; 2044 return;
2043 } 2045 }
2044 2046
2045 if (output.type() != Json::objectValue) 2047 if (output.type() != Json::objectValue)
2046 { 2048 {
2047 LOG(ERROR) << "Lua: IncomingFindRequestFilter must return a table"; 2049 throw OrthancException(ErrorCode_LuaBadOutput,
2048 throw OrthancException(ErrorCode_LuaBadOutput); 2050 "Lua: IncomingFindRequestFilter must return a table");
2049 } 2051 }
2050 2052
2051 Json::Value::Members members = output.getMemberNames(); 2053 Json::Value::Members members = output.getMemberNames();
2052 2054
2053 for (size_t i = 0; i < members.size(); i++) 2055 for (size_t i = 0; i < members.size(); i++)
2054 { 2056 {
2055 if (output[members[i]].type() != Json::stringValue) 2057 if (output[members[i]].type() != Json::stringValue)
2056 { 2058 {
2057 LOG(ERROR) << "Lua: IncomingFindRequestFilter must return a table mapping names of DICOM tags to strings"; 2059 throw OrthancException(ErrorCode_LuaBadOutput,
2058 throw OrthancException(ErrorCode_LuaBadOutput); 2060 "Lua: IncomingFindRequestFilter must return a table "
2061 "mapping names of DICOM tags to strings");
2059 } 2062 }
2060 2063
2061 DicomTag tag(ParseTag(members[i])); 2064 DicomTag tag(ParseTag(members[i]));
2062 target.SetValue(tag, output[members[i]].asString(), false); 2065 target.SetValue(tag, output[members[i]].asString(), false);
2063 } 2066 }
2184 case ITagVisitor::Action_Replace: 2187 case ITagVisitor::Action_Replace:
2185 { 2188 {
2186 std::string s = Toolbox::ConvertFromUtf8(newValue, encoding); 2189 std::string s = Toolbox::ConvertFromUtf8(newValue, encoding);
2187 if (element.putString(s.c_str()) != EC_Normal) 2190 if (element.putString(s.c_str()) != EC_Normal)
2188 { 2191 {
2189 LOG(ERROR) << "Cannot replace value of tag: " << tag.Format(); 2192 throw OrthancException(ErrorCode_InternalError,
2190 throw OrthancException(ErrorCode_InternalError); 2193 "Cannot replace value of tag: " + tag.Format());
2191 } 2194 }
2192 2195
2193 break; 2196 break;
2194 } 2197 }
2195 2198