changeset 276:ef8a673b5fb9 iiif

clarification of RawTile::Answer()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 12 Jul 2023 14:51:46 +0200
parents 801790c81f20
children ac1508b438b1
files ViewerPlugin/Plugin.cpp ViewerPlugin/RawTile.cpp ViewerPlugin/RawTile.h
diffstat 3 files changed, 36 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/ViewerPlugin/Plugin.cpp	Wed Jul 12 12:35:04 2023 +0200
+++ b/ViewerPlugin/Plugin.cpp	Wed Jul 12 14:51:46 2023 +0200
@@ -151,20 +151,30 @@
   std::unique_ptr<OrthancWSI::RawTile> rawTile;
 
   {
+    // NB: Don't call "rawTile" while the Locker is around, as
+    // "Answer()" can be a costly operation.
     OrthancWSI::DicomPyramidCache::Locker locker(seriesId);
+
     rawTile.reset(new OrthancWSI::RawTile(locker.GetPyramid(),
                                           static_cast<unsigned int>(level),
                                           static_cast<unsigned int>(tileX),
                                           static_cast<unsigned int>(tileY)));
   }
 
-  /**
-   * In the case the DICOM file doesn't use the JPEG transfer syntax,
-   * transfer the tile (which is presumably lossless) as a PNG image
-   * so as to prevent lossy compression. Don't call "rawTile" while
-   * the Locker is around, as "Answer()" can be a costly operation.
-   **/
-  rawTile->Answer(output, Orthanc::MimeType_Png);
+  if (rawTile->GetCompression() == OrthancWSI::ImageCompression_Jpeg)
+  {
+    // The tile is already a JPEG image. In such a case, we can
+    // serve it as such, because any Web browser can handle JPEG.
+    rawTile->Answer(output, Orthanc::MimeType_Jpeg);
+  }
+  else
+  {
+    // This is a lossless frame (coming from a JPEG2000 or an
+    // uncompressed DICOM instance), which is not a DICOM-JPEG
+    // instance. We need to decompress the raw tile, then transcode it
+    // to the PNG/JPEG, depending on the "encoding".
+    rawTile->Answer(output, Orthanc::MimeType_Png);
+  }
 }
 
 
--- a/ViewerPlugin/RawTile.cpp	Wed Jul 12 12:35:04 2023 +0200
+++ b/ViewerPlugin/RawTile.cpp	Wed Jul 12 14:51:46 2023 +0200
@@ -85,9 +85,9 @@
 
   void RawTile::EncodeInternal(std::string& encoded,
                                const Orthanc::ImageAccessor& decoded,
-                               Orthanc::MimeType transcodingType)
+                               Orthanc::MimeType encoding)
   {
-    switch (transcodingType)
+    switch (encoding)
     {
       case Orthanc::MimeType_Png:
       {
@@ -128,22 +128,17 @@
 
 
   void RawTile::Answer(OrthancPluginRestOutput* output,
-                       Orthanc::MimeType transcodingType)
+                       Orthanc::MimeType encoding)
   {
-    if (compression_ == ImageCompression_Jpeg)
+    if ((compression_ == ImageCompression_Jpeg && encoding == Orthanc::MimeType_Jpeg) ||
+        (compression_ == ImageCompression_Jpeg2000 && encoding == Orthanc::MimeType_Jpeg2000))
     {
-      // The tile is already a JPEG image. In such a case, we can
-      // serve it as such, because any Web browser can handle JPEG.
+      // No transcoding is needed, the tile can be served as such
       OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, tile_.c_str(),
-                                tile_.size(), Orthanc::EnumerationToString(Orthanc::MimeType_Jpeg));
+                                tile_.size(), Orthanc::EnumerationToString(encoding));
     }
     else
     {
-      // This is a lossless frame (coming from a JPEG2000 or an
-      // uncompressed DICOM instance), which is not a DICOM-JPEG
-      // instance. We need to decompress the raw tile, then transcode
-      // it to the PNG/JPEG, depending on the "transcodingType".
-
       std::string transcoded;
 
       {
@@ -151,11 +146,11 @@
         Orthanc::Semaphore::Locker locker(*transcoderSemaphore_);
 
         std::unique_ptr<Orthanc::ImageAccessor> decoded(DecodeInternal());
-        EncodeInternal(transcoded, *decoded, transcodingType);
+        EncodeInternal(transcoded, *decoded, encoding);
       }
 
       OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, transcoded.c_str(),
-                                transcoded.size(), Orthanc::EnumerationToString(transcodingType));
+                                transcoded.size(), Orthanc::EnumerationToString(encoding));
     }
   }
 
@@ -169,10 +164,10 @@
 
   void RawTile::Encode(std::string& encoded,
                        const Orthanc::ImageAccessor& decoded,
-                       Orthanc::MimeType transcodingType)
+                       Orthanc::MimeType encoding)
   {
     Orthanc::Semaphore::Locker locker(*transcoderSemaphore_);
-    EncodeInternal(encoded, decoded, transcodingType);
+    EncodeInternal(encoded, decoded, encoding);
   }
 
 
--- a/ViewerPlugin/RawTile.h	Wed Jul 12 12:35:04 2023 +0200
+++ b/ViewerPlugin/RawTile.h	Wed Jul 12 14:51:46 2023 +0200
@@ -44,7 +44,7 @@
 
     static void EncodeInternal(std::string& encoded,
                                const Orthanc::ImageAccessor& decoded,
-                               Orthanc::MimeType transcodingType);
+                               Orthanc::MimeType encoding);
 
   public:
     RawTile(ITiledPyramid& pyramid,
@@ -52,14 +52,19 @@
             unsigned int tileX,
             unsigned int tileY);
 
+    ImageCompression GetCompression() const
+    {
+      return compression_;
+    }
+
     void Answer(OrthancPluginRestOutput* output,
-                Orthanc::MimeType transcodingType);
+                Orthanc::MimeType encoding);
 
     Orthanc::ImageAccessor* Decode();
 
     static void Encode(std::string& encoded,
                        const Orthanc::ImageAccessor& decoded,
-                       Orthanc::MimeType transcodingType);
+                       Orthanc::MimeType encoding);
 
     // This semaphore is used to implement throttling for the
     // decoding/encoding of tiles