diff OrthancServer/OrthancRestApi/OrthancRestResources.cpp @ 1906:d7c1cb559431

optimization for multi-frame images
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 05 Jan 2016 17:45:27 +0100
parents 8b0ee8d5e6d0
children 6c73df12ca51
line wrap: on
line diff
--- a/OrthancServer/OrthancRestApi/OrthancRestResources.cpp	Tue Jan 05 17:25:01 2016 +0100
+++ b/OrthancServer/OrthancRestApi/OrthancRestResources.cpp	Tue Jan 05 17:45:27 2016 +0100
@@ -374,29 +374,28 @@
       return;
     }
 
-    std::string publicId = call.GetUriComponent("id", "");
-    std::string dicomContent;
-    context.ReadFile(dicomContent, publicId, FileContentType_Dicom);
+    std::auto_ptr<ImageAccessor> decoded;
 
     try
     {
+      std::string publicId = call.GetUriComponent("id", "");
+
 #if ORTHANC_PLUGINS_ENABLED == 1
-      IDicomImageDecoder& decoder = context.GetPlugins();
-#else
-      DefaultDicomImageDecoder decoder;  // This is Orthanc's built-in decoder
+      if (context.GetPlugins().HasCustomImageDecoder())
+      {
+        // TODO create a cache of file
+        std::string dicomContent;
+        context.ReadFile(dicomContent, publicId, FileContentType_Dicom);
+        decoded.reset(context.GetPlugins().Decode(dicomContent.c_str(), dicomContent.size(), frame));
+      }
 #endif
 
-      std::auto_ptr<ImageAccessor> decoded(decoder.Decode(dicomContent.c_str(), dicomContent.size(), frame));
-
-      ImageToEncode image(decoded, mode);
-
-      HttpContentNegociation negociation;
-      EncodePng png(image);          negociation.Register("image/png", png);
-      EncodeJpeg jpeg(image, call);  negociation.Register("image/jpeg", jpeg);
-
-      if (negociation.Apply(call.GetHttpHeaders()))
+      if (decoded.get() == NULL)
       {
-        image.Answer(call.GetOutput());
+        // Use Orthanc's built-in decoder, using the cache to speed-up
+        // things on multi-frame images
+        ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), publicId);
+        decoded.reset(DicomImageDecoder::Decode(locker.GetDicom(), frame));
       }
     }
     catch (OrthancException& e)
@@ -417,6 +416,17 @@
         call.GetOutput().Redirect(root + "app/images/unsupported.png");
       }
     }
+
+    ImageToEncode image(decoded, mode);
+
+    HttpContentNegociation negociation;
+    EncodePng png(image);          negociation.Register("image/png", png);
+    EncodeJpeg jpeg(image, call);  negociation.Register("image/jpeg", jpeg);
+
+    if (negociation.Apply(call.GetHttpHeaders()))
+    {
+      image.Answer(call.GetOutput());
+    }
   }