comparison Plugin/DecodedImageAdapter.cpp @ 102:21123729ac71 refactoring

simplification
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 30 Nov 2015 11:02:50 +0100
parents 2932473a9b19
children d5396fcd80bb
comparison
equal deleted inserted replaced
101:2932473a9b19 102:21123729ac71
21 #include "DecodedImageAdapter.h" 21 #include "DecodedImageAdapter.h"
22 22
23 #include "../Orthanc/Core/Images/ImageBuffer.h" 23 #include "../Orthanc/Core/Images/ImageBuffer.h"
24 #include "../Orthanc/Core/Images/ImageProcessing.h" 24 #include "../Orthanc/Core/Images/ImageProcessing.h"
25 #include "../Orthanc/Core/OrthancException.h" 25 #include "../Orthanc/Core/OrthancException.h"
26 #include "../Orthanc/Core/Toolbox.h"
26 #include "../Orthanc/Plugins/Samples/GdcmDecoder/OrthancImageWrapper.h" 27 #include "../Orthanc/Plugins/Samples/GdcmDecoder/OrthancImageWrapper.h"
27 #include "../Orthanc/Resources/ThirdParty/base64/base64.h" 28 #include "../Orthanc/Resources/ThirdParty/base64/base64.h"
28 #include "ViewerToolbox.h" 29 #include "ViewerToolbox.h"
29 30
30 #include <boost/lexical_cast.hpp> 31 #include <boost/lexical_cast.hpp>
102 !GetJsonFromOrthanc(tags, context_, "/instances/" + instanceId + "/tags")) 103 !GetJsonFromOrthanc(tags, context_, "/instances/" + instanceId + "/tags"))
103 { 104 {
104 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource); 105 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource);
105 } 106 }
106 107
107 std::auto_ptr<OrthancImageWrapper> image(decoderCache_.Decode(context_, dicom.c_str(), dicom.size(), frameIndex)); 108 std::auto_ptr<OrthancImageWrapper> image(new OrthancImageWrapper(context_, OrthancPluginDecodeDicomImage(context_, dicom.c_str(), dicom.size(), frameIndex)));
108 109
109 Json::Value json; 110 Json::Value json;
110 if (GetCornerstoneMetadata(json, tags, *image)) 111 if (GetCornerstoneMetadata(json, tags, *image))
111 { 112 {
112 if (type == CompressionType_Deflate) 113 if (type == CompressionType_Deflate)
133 return false; 134 return false;
134 } 135 }
135 } 136 }
136 137
137 138
138 static bool GetTagValue(std::string& value, 139 static bool GetStringTag(std::string& value,
139 const Json::Value& tags, 140 const Json::Value& tags,
140 const std::string& tag) 141 const std::string& tag)
141 { 142 {
142 if (tags.type() == Json::objectValue && 143 if (tags.type() == Json::objectValue &&
143 tags.isMember(tag) && 144 tags.isMember(tag) &&
144 tags[tag].type() == Json::objectValue && 145 tags[tag].type() == Json::objectValue &&
145 tags[tag].isMember("Type") && 146 tags[tag].isMember("Type") &&
153 } 154 }
154 else 155 else
155 { 156 {
156 return false; 157 return false;
157 } 158 }
159 }
160
161
162 static float GetFloatTag(const Json::Value& tags,
163 const std::string& tag,
164 float defaultValue)
165 {
166 std::string tmp;
167 if (GetStringTag(tmp, tags, tag))
168 {
169 try
170 {
171 return boost::lexical_cast<float>(tmp);
172 }
173 catch (boost::bad_lexical_cast&)
174 {
175 }
176 }
177
178 return defaultValue;
158 } 179 }
159 180
160 181
161 182
162 bool DecodedImageAdapter::GetCornerstoneMetadata(Json::Value& result, 183 bool DecodedImageAdapter::GetCornerstoneMetadata(Json::Value& result,
207 228
208 default: 229 default:
209 return false; 230 return false;
210 } 231 }
211 232
212 result["slope"] = image.GetSlope(); 233 float slope = GetFloatTag(tags, "0028,1053", 1.0f);
213 result["intercept"] = image.GetIntercept(); 234 float intercept = GetFloatTag(tags, "0028,1052", 1.0f);
235
236 result["slope"] = slope;
237 result["intercept"] = intercept;
214 result["rows"] = image.GetHeight(); 238 result["rows"] = image.GetHeight();
215 result["columns"] = image.GetWidth(); 239 result["columns"] = image.GetWidth();
216 result["height"] = image.GetHeight(); 240 result["height"] = image.GetHeight();
217 result["width"] = image.GetWidth(); 241 result["width"] = image.GetWidth();
218 result["columnPixelSpacing"] = image.GetColumnPixelSpacing(); 242
219 result["rowPixelSpacing"] = image.GetRowPixelSpacing(); 243 bool ok = false;
220 244 std::string pixelSpacing;
221 result["windowCenter"] = windowCenter * image.GetSlope() + image.GetIntercept(); 245 if (GetStringTag(pixelSpacing, tags, "0028,0030"))
222 result["windowWidth"] = windowWidth * image.GetSlope(); 246 {
223 247 std::vector<std::string> tokens;
224 try 248 Orthanc::Toolbox::TokenizeString(tokens, pixelSpacing, '\\');
225 { 249
226 std::string width, center; 250 if (tokens.size() >= 2)
227 if (GetTagValue(center, tags, "0028,1050" /*DICOM_TAG_WINDOW_CENTER*/) && 251 {
228 GetTagValue(width, tags, "0028,1051" /*DICOM_TAG_WINDOW_WIDTH*/)) 252 try
229 { 253 {
230 float a = boost::lexical_cast<float>(width); 254 result["columnPixelSpacing"] = boost::lexical_cast<float>(tokens[1]);
231 float b = boost::lexical_cast<float>(center); 255 result["rowPixelSpacing"] = boost::lexical_cast<float>(tokens[0]);
232 result["windowWidth"] = a; 256 ok = true;
233 result["windowCenter"] = b; 257 }
234 } 258 catch (boost::bad_lexical_cast&)
235 } 259 {
236 catch (boost::bad_lexical_cast&) 260 }
237 { 261 }
238 } 262 }
263
264 if (!ok)
265 {
266 result["columnPixelSpacing"] = 1.0f;
267 result["rowPixelSpacing"] = 1.0f;
268 }
269
270 result["windowCenter"] = GetFloatTag(tags, "0028,1050", windowCenter * slope + intercept);
271 result["windowWidth"] = GetFloatTag(tags, "0028,1051", windowWidth * slope);
239 272
240 return true; 273 return true;
241 } 274 }
242 275
243 276