comparison Plugin/Plugin.cpp @ 95:dbe7c97b6b4a refactoring

replace built-in decoder
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 27 Nov 2015 16:19:09 +0100
parents c44ab3266343
children a6ba21a083e5
comparison
equal deleted inserted replaced
94:63396b070632 95:dbe7c97b6b4a
27 #include "ViewerToolbox.h" 27 #include "ViewerToolbox.h"
28 #include "ViewerPrefetchPolicy.h" 28 #include "ViewerPrefetchPolicy.h"
29 #include "DecodedImageAdapter.h" 29 #include "DecodedImageAdapter.h"
30 #include "InstanceInformationAdapter.h" 30 #include "InstanceInformationAdapter.h"
31 #include "SeriesInformationAdapter.h" 31 #include "SeriesInformationAdapter.h"
32 32 #include "../Orthanc/Plugins/Samples/GdcmDecoder/GdcmImageDecoder.h"
33
34 #if (ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER <= 0 && ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER <= 9 && ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER <= 4)
35 # define RETURN_TYPE int32_t
36 # define RETURN_SUCCESS 0
37 # define RETURN_FAILURE -1
38 #else
39 # define RETURN_TYPE OrthancPluginErrorCode
40 # define RETURN_SUCCESS OrthancPluginErrorCode_Success
41 # define RETURN_FAILURE OrthancPluginErrorCode_Plugin
42 #endif
43 33
44 34
45 static OrthancPluginContext* context_ = NULL; 35 static OrthancPluginContext* context_ = NULL;
46 36
47 37
142 132
143 static CacheContext* cache_ = NULL; 133 static CacheContext* cache_ = NULL;
144 134
145 135
146 136
147 static RETURN_TYPE OnChangeCallback(OrthancPluginChangeType changeType, 137 static OrthancPluginErrorCode OnChangeCallback(OrthancPluginChangeType changeType,
148 OrthancPluginResourceType resourceType, 138 OrthancPluginResourceType resourceType,
149 const char* resourceId) 139 const char* resourceId)
150 { 140 {
151 try 141 try
152 { 142 {
153 if (changeType == OrthancPluginChangeType_NewInstance && 143 if (changeType == OrthancPluginChangeType_NewInstance &&
154 resourceType == OrthancPluginResourceType_Instance) 144 resourceType == OrthancPluginResourceType_Instance)
155 { 145 {
156 cache_->SignalNewInstance(resourceId); 146 cache_->SignalNewInstance(resourceId);
157 } 147 }
158 148
159 return RETURN_SUCCESS; 149 return OrthancPluginErrorCode_Success;
160 } 150 }
161 catch (std::runtime_error& e) 151 catch (std::runtime_error& e)
162 { 152 {
163 OrthancPluginLogError(context_, e.what()); 153 OrthancPluginLogError(context_, e.what());
164 return RETURN_SUCCESS; // Ignore error 154 return OrthancPluginErrorCode_Success; // Ignore error
165 } 155 }
166 } 156 }
167 157
168 158
169 159
170 template <enum OrthancPlugins::CacheBundle bundle> 160 template <enum OrthancPlugins::CacheBundle bundle>
171 static RETURN_TYPE ServeCache(OrthancPluginRestOutput* output, 161 static OrthancPluginErrorCode ServeCache(OrthancPluginRestOutput* output,
172 const char* url, 162 const char* url,
173 const OrthancPluginHttpRequest* request) 163 const OrthancPluginHttpRequest* request)
174 { 164 {
175 try 165 try
176 { 166 {
177 if (request->method != OrthancPluginHttpMethod_Get) 167 if (request->method != OrthancPluginHttpMethod_Get)
178 { 168 {
179 OrthancPluginSendMethodNotAllowed(context_, output, "GET"); 169 OrthancPluginSendMethodNotAllowed(context_, output, "GET");
180 return RETURN_SUCCESS; 170 return OrthancPluginErrorCode_Success;
181 } 171 }
182 172
183 const std::string id = request->groups[0]; 173 const std::string id = request->groups[0];
184 std::string content; 174 std::string content;
185 175
190 else 180 else
191 { 181 {
192 OrthancPluginSendHttpStatusCode(context_, output, 404); 182 OrthancPluginSendHttpStatusCode(context_, output, 404);
193 } 183 }
194 184
195 return RETURN_SUCCESS; 185 return OrthancPluginErrorCode_Success;
196 } 186 }
197 catch (Orthanc::OrthancException& e) 187 catch (Orthanc::OrthancException& e)
198 { 188 {
199 OrthancPluginLogError(context_, e.What()); 189 OrthancPluginLogError(context_, e.What());
200 return RETURN_FAILURE; 190 return OrthancPluginErrorCode_Plugin;
201 } 191 }
202 catch (std::runtime_error& e) 192 catch (std::runtime_error& e)
203 { 193 {
204 OrthancPluginLogError(context_, e.what()); 194 OrthancPluginLogError(context_, e.what());
205 return RETURN_FAILURE; 195 return OrthancPluginErrorCode_Plugin;
206 } 196 }
207 catch (boost::bad_lexical_cast&) 197 catch (boost::bad_lexical_cast&)
208 { 198 {
209 OrthancPluginLogError(context_, "Bad lexical cast"); 199 OrthancPluginLogError(context_, "Bad lexical cast");
210 return RETURN_FAILURE; 200 return OrthancPluginErrorCode_Plugin;
211 } 201 }
212 } 202 }
213 203
214 204
215 205
246 #endif 236 #endif
247 237
248 238
249 239
250 template <enum Orthanc::EmbeddedResources::DirectoryResourceId folder> 240 template <enum Orthanc::EmbeddedResources::DirectoryResourceId folder>
251 static RETURN_TYPE ServeEmbeddedFolder(OrthancPluginRestOutput* output, 241 static OrthancPluginErrorCode ServeEmbeddedFolder(OrthancPluginRestOutput* output,
252 const char* url, 242 const char* url,
253 const OrthancPluginHttpRequest* request) 243 const OrthancPluginHttpRequest* request)
254 { 244 {
255 if (request->method != OrthancPluginHttpMethod_Get) 245 if (request->method != OrthancPluginHttpMethod_Get)
256 { 246 {
257 OrthancPluginSendMethodNotAllowed(context_, output, "GET"); 247 OrthancPluginSendMethodNotAllowed(context_, output, "GET");
258 return RETURN_SUCCESS; 248 return OrthancPluginErrorCode_Success;
259 } 249 }
260 250
261 std::string path = "/" + std::string(request->groups[0]); 251 std::string path = "/" + std::string(request->groups[0]);
262 const char* mime = OrthancPlugins::GetMimeType(path); 252 const char* mime = OrthancPlugins::GetMimeType(path);
263 253
267 Orthanc::EmbeddedResources::GetDirectoryResource(s, folder, path.c_str()); 257 Orthanc::EmbeddedResources::GetDirectoryResource(s, folder, path.c_str());
268 258
269 const char* resource = s.size() ? s.c_str() : NULL; 259 const char* resource = s.size() ? s.c_str() : NULL;
270 OrthancPluginAnswerBuffer(context_, output, resource, s.size(), mime); 260 OrthancPluginAnswerBuffer(context_, output, resource, s.size(), mime);
271 261
272 return RETURN_SUCCESS; 262 return OrthancPluginErrorCode_Success;
273 } 263 }
274 catch (std::runtime_error&) 264 catch (std::runtime_error&)
275 { 265 {
276 std::string s = "Unknown static resource in plugin: " + std::string(request->groups[0]); 266 std::string s = "Unknown static resource in plugin: " + std::string(request->groups[0]);
277 OrthancPluginLogError(context_, s.c_str()); 267 OrthancPluginLogError(context_, s.c_str());
278 OrthancPluginSendHttpStatusCode(context_, output, 404); 268 OrthancPluginSendHttpStatusCode(context_, output, 404);
279 return RETURN_SUCCESS; 269 return OrthancPluginErrorCode_Success;
280 } 270 }
281 } 271 }
282 272
283 273
284 274
285 static RETURN_TYPE IsStableSeries(OrthancPluginRestOutput* output, 275 static OrthancPluginErrorCode IsStableSeries(OrthancPluginRestOutput* output,
286 const char* url, 276 const char* url,
287 const OrthancPluginHttpRequest* request) 277 const OrthancPluginHttpRequest* request)
288 { 278 {
289 try 279 try
290 { 280 {
291 if (request->method != OrthancPluginHttpMethod_Get) 281 if (request->method != OrthancPluginHttpMethod_Get)
292 { 282 {
293 OrthancPluginSendMethodNotAllowed(context_, output, "GET"); 283 OrthancPluginSendMethodNotAllowed(context_, output, "GET");
294 return RETURN_SUCCESS; 284 return OrthancPluginErrorCode_Success;
295 } 285 }
296 286
297 const std::string id = request->groups[0]; 287 const std::string id = request->groups[0];
298 Json::Value series; 288 Json::Value series;
299 289
308 else 298 else
309 { 299 {
310 OrthancPluginSendHttpStatusCode(context_, output, 404); 300 OrthancPluginSendHttpStatusCode(context_, output, 404);
311 } 301 }
312 302
313 return RETURN_SUCCESS; 303 return OrthancPluginErrorCode_Success;
314 } 304 }
315 catch (Orthanc::OrthancException& e) 305 catch (Orthanc::OrthancException& e)
316 { 306 {
317 OrthancPluginLogError(context_, e.What()); 307 OrthancPluginLogError(context_, e.What());
318 return RETURN_FAILURE; 308 return OrthancPluginErrorCode_Plugin;
319 } 309 }
320 catch (std::runtime_error& e) 310 catch (std::runtime_error& e)
321 { 311 {
322 OrthancPluginLogError(context_, e.what()); 312 OrthancPluginLogError(context_, e.what());
323 return RETURN_FAILURE; 313 return OrthancPluginErrorCode_Plugin;
324 } 314 }
325 catch (boost::bad_lexical_cast&) 315 catch (boost::bad_lexical_cast&)
326 { 316 {
327 OrthancPluginLogError(context_, "Bad lexical cast"); 317 OrthancPluginLogError(context_, "Bad lexical cast");
328 return RETURN_FAILURE; 318 return OrthancPluginErrorCode_Plugin;
319 }
320 }
321
322
323 static OrthancPluginErrorCode DecodeImageCallback(OrthancPluginImage** target,
324 const void* dicom,
325 const uint32_t size,
326 uint32_t frameIndex)
327 {
328 try
329 {
330 #if 1
331 // Do not use the cache
332 OrthancPlugins::GdcmImageDecoder decoder(dicom, size);
333 *target = decoder.Decode(context_, frameIndex);
334 #else
335 *target = cache_.Decode(context_, dicom, size, frameIndex);
336 #endif
337
338 return OrthancPluginErrorCode_Success;
339 }
340 catch (std::runtime_error& e)
341 {
342 *target = NULL;
343
344 std::string s = "Cannot decode image using GDCM: " + std::string(e.what());
345 OrthancPluginLogError(context_, s.c_str());
346 return OrthancPluginErrorCode_Plugin;
329 } 347 }
330 } 348 }
331 349
332 350
333 extern "C" 351 extern "C"
352 OrthancPluginLogError(context_, info); 370 OrthancPluginLogError(context_, info);
353 return -1; 371 return -1;
354 } 372 }
355 373
356 OrthancPluginSetDescription(context_, "Provides a Web viewer of DICOM series within Orthanc."); 374 OrthancPluginSetDescription(context_, "Provides a Web viewer of DICOM series within Orthanc.");
375
376
377 // Replace the default decoder of DICOM images that is built in Orthanc
378 OrthancPluginRegisterDecodeImageCallback(context_, DecodeImageCallback);
357 379
358 380
359 /* By default, use half of the available processing cores for the decoding of DICOM images */ 381 /* By default, use half of the available processing cores for the decoding of DICOM images */
360 int decodingThreads = boost::thread::hardware_concurrency() / 2; 382 int decodingThreads = boost::thread::hardware_concurrency() / 2;
361 if (decodingThreads == 0) 383 if (decodingThreads == 0)