comparison OrthancServer/OrthancRestApi/OrthancRestResources.cpp @ 1902:8b0ee8d5e6d0

Refactoring leading to speedups with custom image decoders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 05 Jan 2016 13:26:51 +0100
parents b1291df2f780
children d7c1cb559431
comparison
equal deleted inserted replaced
1901:50234539a0dd 1902:8b0ee8d5e6d0
264 namespace 264 namespace
265 { 265 {
266 class ImageToEncode 266 class ImageToEncode
267 { 267 {
268 private: 268 private:
269 IDicomImageDecoder& decoder_; 269 std::auto_ptr<ImageAccessor>& image_;
270 std::string format_; 270 ImageExtractionMode mode_;
271 std::string encoded_; 271 std::string format_;
272 ParsedDicomFile& dicom_; 272 std::string answer_;
273 unsigned int frame_;
274 ImageExtractionMode mode_;
275 273
276 public: 274 public:
277 ImageToEncode(IDicomImageDecoder& decoder, 275 ImageToEncode(std::auto_ptr<ImageAccessor>& image,
278 ParsedDicomFile& dicom,
279 unsigned int frame,
280 ImageExtractionMode mode) : 276 ImageExtractionMode mode) :
281 decoder_(decoder), 277 image_(image),
282 dicom_(dicom),
283 frame_(frame),
284 mode_(mode) 278 mode_(mode)
285 { 279 {
286 } 280 }
287 281
288 ParsedDicomFile& GetDicom() const
289 {
290 return dicom_;
291 }
292
293 unsigned int GetFrame() const
294 {
295 return frame_;
296 }
297
298 ImageExtractionMode GetMode() const
299 {
300 return mode_;
301 }
302
303 void SetFormat(const std::string& format)
304 {
305 format_ = format;
306 }
307
308 std::string& GetTarget()
309 {
310 return encoded_;
311 }
312
313 void Answer(RestApiOutput& output) 282 void Answer(RestApiOutput& output)
314 { 283 {
315 output.AnswerBuffer(encoded_, format_); 284 output.AnswerBuffer(answer_, format_);
316 } 285 }
317 286
318 IDicomImageDecoder& GetDecoder() const 287 void EncodeUsingPng()
319 { 288 {
320 return decoder_; 289 format_ = "image/png";
290 DicomImageDecoder::ExtractPngImage(answer_, image_, mode_);
291 }
292
293 void EncodeUsingJpeg(uint8_t quality)
294 {
295 format_ = "image/jpeg";
296 DicomImageDecoder::ExtractJpegImage(answer_, image_, mode_, quality);
321 } 297 }
322 }; 298 };
323 299
324 class EncodePng : public HttpContentNegociation::IHandler 300 class EncodePng : public HttpContentNegociation::IHandler
325 { 301 {
334 virtual void Handle(const std::string& type, 310 virtual void Handle(const std::string& type,
335 const std::string& subtype) 311 const std::string& subtype)
336 { 312 {
337 assert(type == "image"); 313 assert(type == "image");
338 assert(subtype == "png"); 314 assert(subtype == "png");
339 image_.GetDicom().ExtractPngImage(image_.GetTarget(), image_.GetDecoder(), 315 image_.EncodeUsingPng();
340 image_.GetFrame(), image_.GetMode());
341 image_.SetFormat("image/png");
342 } 316 }
343 }; 317 };
344 318
345 class EncodeJpeg : public HttpContentNegociation::IHandler 319 class EncodeJpeg : public HttpContentNegociation::IHandler
346 { 320 {
375 virtual void Handle(const std::string& type, 349 virtual void Handle(const std::string& type,
376 const std::string& subtype) 350 const std::string& subtype)
377 { 351 {
378 assert(type == "image"); 352 assert(type == "image");
379 assert(subtype == "jpeg"); 353 assert(subtype == "jpeg");
380 image_.GetDicom().ExtractJpegImage(image_.GetTarget(), image_.GetDecoder(), 354 image_.EncodeUsingJpeg(quality_);
381 image_.GetFrame(), image_.GetMode(), quality_);
382 image_.SetFormat("image/jpeg");
383 } 355 }
384 }; 356 };
385 } 357 }
386 358
387 359
403 } 375 }
404 376
405 std::string publicId = call.GetUriComponent("id", ""); 377 std::string publicId = call.GetUriComponent("id", "");
406 std::string dicomContent; 378 std::string dicomContent;
407 context.ReadFile(dicomContent, publicId, FileContentType_Dicom); 379 context.ReadFile(dicomContent, publicId, FileContentType_Dicom);
408
409 ParsedDicomFile dicom(dicomContent);
410 380
411 try 381 try
412 { 382 {
413 #if ORTHANC_PLUGINS_ENABLED == 1 383 #if ORTHANC_PLUGINS_ENABLED == 1
414 IDicomImageDecoder& decoder = context.GetPlugins(); 384 IDicomImageDecoder& decoder = context.GetPlugins();
415 #else 385 #else
416 DicomImageDecoder decoder; // This is Orthanc's built-in decoder 386 DefaultDicomImageDecoder decoder; // This is Orthanc's built-in decoder
417 #endif 387 #endif
418 388
419 ImageToEncode image(decoder, dicom, frame, mode); 389 std::auto_ptr<ImageAccessor> decoded(decoder.Decode(dicomContent.c_str(), dicomContent.size(), frame));
390
391 ImageToEncode image(decoded, mode);
420 392
421 HttpContentNegociation negociation; 393 HttpContentNegociation negociation;
422 EncodePng png(image); negociation.Register("image/png", png); 394 EncodePng png(image); negociation.Register("image/png", png);
423 EncodeJpeg jpeg(image, call); negociation.Register("image/jpeg", jpeg); 395 EncodeJpeg jpeg(image, call); negociation.Register("image/jpeg", jpeg);
424 396
469 context.ReadFile(dicomContent, publicId, FileContentType_Dicom); 441 context.ReadFile(dicomContent, publicId, FileContentType_Dicom);
470 442
471 #if ORTHANC_PLUGINS_ENABLED == 1 443 #if ORTHANC_PLUGINS_ENABLED == 1
472 IDicomImageDecoder& decoder = context.GetPlugins(); 444 IDicomImageDecoder& decoder = context.GetPlugins();
473 #else 445 #else
474 DicomImageDecoder decoder; // This is Orthanc's built-in decoder 446 DefaultDicomImageDecoder decoder; // This is Orthanc's built-in decoder
475 #endif 447 #endif
476 448
477 ParsedDicomFile dicom(dicomContent); 449 std::auto_ptr<ImageAccessor> decoded(decoder.Decode(dicomContent.c_str(), dicomContent.size(), frame));
478 std::auto_ptr<ImageAccessor> decoded(dicom.ExtractImage(decoder, frame));
479 450
480 std::string result; 451 std::string result;
481 decoded->ToMatlabString(result); 452 decoded->ToMatlabString(result);
482 453
483 call.GetOutput().AnswerBuffer(result, "text/plain"); 454 call.GetOutput().AnswerBuffer(result, "text/plain");