comparison Plugin/DecodedImageAdapter.cpp @ 160:b0910ae2ace5

author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 09 Jun 2017 16:13:05 +0200
parents 5dc54316d68b
children 330ecfd96aec
comparison
equal deleted inserted replaced
159:5dc54316d68b 160:b0910ae2ace5
35 #include <boost/regex.hpp> 35 #include <boost/regex.hpp>
36 36
37 37
38 namespace OrthancPlugins 38 namespace OrthancPlugins
39 { 39 {
40 bool DecodedImageAdapter::ParseUri(CompressionType& type,
41 uint8_t& compressionLevel,
42 std::string& instanceId,
43 unsigned int& frameIndex,
44 const std::string& uri)
45 {
46 boost::regex pattern("^([a-z0-9]+)-([a-z0-9-]+)_([0-9]+)$");
47
48 boost::cmatch what;
49 if (!regex_match(uri.c_str(), what, pattern))
50 {
51 return false;
52 }
53
54 std::string compression(what[1]);
55 instanceId = what[2];
56 frameIndex = boost::lexical_cast<unsigned int>(what[3]);
57
58 if (compression == "deflate")
59 {
60 type = CompressionType_Deflate;
61 }
62 else if (boost::starts_with(compression, "jpeg"))
63 {
64 type = CompressionType_Jpeg;
65 int level = boost::lexical_cast<int>(compression.substr(4));
66 if (level <= 0 || level > 100)
67 {
68 return false;
69 }
70
71 compressionLevel = static_cast<uint8_t>(level);
72 }
73 else
74 {
75 return false;
76 }
77
78 return true;
79 }
80
81
82
83 bool DecodedImageAdapter::Create(std::string& content,
84 const std::string& uri)
85 {
86 std::string message = "Decoding DICOM instance: " + uri;
87 OrthancPluginLogInfo(context_, message.c_str());
88
89 CompressionType type;
90 uint8_t level;
91 std::string instanceId;
92 unsigned int frameIndex;
93
94 if (!ParseUri(type, level, instanceId, frameIndex, uri))
95 {
96 return false;
97 }
98
99
100 bool ok = false;
101
102 Json::Value tags;
103 std::string dicom;
104 if (!GetStringFromOrthanc(dicom, context_, "/instances/" + instanceId + "/file") ||
105 !GetJsonFromOrthanc(tags, context_, "/instances/" + instanceId + "/tags"))
106 {
107 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource);
108 }
109
110 std::auto_ptr<OrthancImageWrapper> image(new OrthancImageWrapper(context_, OrthancPluginDecodeDicomImage(context_, dicom.c_str(), dicom.size(), frameIndex)));
111
112 Json::Value json;
113 if (GetCornerstoneMetadata(json, tags, *image))
114 {
115 if (type == CompressionType_Deflate)
116 {
117 ok = EncodeUsingDeflate(json, *image, 9);
118 }
119 else if (type == CompressionType_Jpeg)
120 {
121 ok = EncodeUsingJpeg(json, *image, level);
122 }
123 }
124
125 if (ok)
126 {
127 Json::FastWriter writer;
128 content = writer.write(json);
129 return true;
130 }
131 else
132 {
133 char msg[1024];
134 sprintf(msg, "Unable to decode the following instance: %s", uri.c_str());
135 OrthancPluginLogWarning(context_, msg);
136 return false;
137 }
138 }
139
140
141 static bool GetStringTag(std::string& value, 40 static bool GetStringTag(std::string& value,
142 const Json::Value& tags, 41 const Json::Value& tags,
143 const std::string& tag) 42 const std::string& tag)
144 { 43 {
145 if (tags.type() == Json::objectValue && 44 if (tags.type() == Json::objectValue &&
178 } 77 }
179 78
180 return defaultValue; 79 return defaultValue;
181 } 80 }
182 81
82
83 bool DecodedImageAdapter::ParseUri(CompressionType& type,
84 uint8_t& compressionLevel,
85 std::string& instanceId,
86 unsigned int& frameIndex,
87 const std::string& uri)
88 {
89 boost::regex pattern("^([a-z0-9]+)-([a-z0-9-]+)_([0-9]+)$");
90
91 boost::cmatch what;
92 if (!regex_match(uri.c_str(), what, pattern))
93 {
94 return false;
95 }
96
97 std::string compression(what[1]);
98 instanceId = what[2];
99 frameIndex = boost::lexical_cast<unsigned int>(what[3]);
100
101 if (compression == "deflate")
102 {
103 type = CompressionType_Deflate;
104 }
105 else if (boost::starts_with(compression, "jpeg"))
106 {
107 type = CompressionType_Jpeg;
108 int level = boost::lexical_cast<int>(compression.substr(4));
109 if (level <= 0 || level > 100)
110 {
111 return false;
112 }
113
114 compressionLevel = static_cast<uint8_t>(level);
115 }
116 else
117 {
118 return false;
119 }
120
121 return true;
122 }
123
124
125
126 bool DecodedImageAdapter::Create(std::string& content,
127 const std::string& uri)
128 {
129 std::string message = "Decoding DICOM instance: " + uri;
130 OrthancPluginLogInfo(context_, message.c_str());
131
132 CompressionType type;
133 uint8_t level;
134 std::string instanceId;
135 unsigned int frameIndex;
136
137 if (!ParseUri(type, level, instanceId, frameIndex, uri))
138 {
139 return false;
140 }
141
142
143 bool ok = false;
144
145 Json::Value tags;
146 std::string dicom;
147 if (!GetStringFromOrthanc(dicom, context_, "/instances/" + instanceId + "/file") ||
148 !GetJsonFromOrthanc(tags, context_, "/instances/" + instanceId + "/tags"))
149 {
150 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource);
151 }
152
153 std::auto_ptr<OrthancImageWrapper> image(new OrthancImageWrapper(context_, OrthancPluginDecodeDicomImage(context_, dicom.c_str(), dicom.size(), frameIndex)));
154
155 Json::Value json;
156 if (GetCornerstoneMetadata(json, tags, *image))
157 {
158 if (type == CompressionType_Deflate)
159 {
160 ok = EncodeUsingDeflate(json, *image, 9);
161 }
162 else if (type == CompressionType_Jpeg)
163 {
164 ok = EncodeUsingJpeg(json, *image, level);
165 }
166 }
167
168 if (ok)
169 {
170 std::string photometric;
171 if (GetStringTag(photometric, tags, "0028,0004"))
172 {
173 json["Orthanc"]["PhotometricInterpretation"] = photometric;
174 }
175
176 Json::FastWriter writer;
177 content = writer.write(json);
178 return true;
179 }
180 else
181 {
182 char msg[1024];
183 sprintf(msg, "Unable to decode the following instance: %s", uri.c_str());
184 OrthancPluginLogWarning(context_, msg);
185 return false;
186 }
187 }
183 188
184 189
185 bool DecodedImageAdapter::GetCornerstoneMetadata(Json::Value& result, 190 bool DecodedImageAdapter::GetCornerstoneMetadata(Json::Value& result,
186 const Json::Value& tags, 191 const Json::Value& tags,
187 OrthancImageWrapper& image) 192 OrthancImageWrapper& image)