changeset 6336:0942549f7185

fix decoding of LUT with less than 256 entries
author Alain Mazy <am@orthanc.team>
date Tue, 07 Oct 2025 12:04:31 +0200
parents 4866ab5476a8
children ba833555bd70
files NEWS OrthancFramework/Sources/DicomParsing/Internals/DicomImageDecoder.cpp
diffstat 2 files changed, 20 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Mon Oct 06 09:51:39 2025 +0200
+++ b/NEWS	Tue Oct 07 12:04:31 2025 +0200
@@ -39,6 +39,8 @@
        empty element in a sequence.  This was preventing access to /dicom-web/../metadata routes and prevented
        visualization in e.g. the Stone Web viewer and OHIF.
 * Fix issue 252: Disallow colons in HTTP basic usernames
+* Fix: Decoding of LUT with less than 256 entries:
+       https://discourse.orthanc-server.org/t/cannot-preview-dicom-file/6275
 * Upgraded dependencies for static builds:
   - civetweb 1.16, including patch for CVE-2025-55763
 
--- a/OrthancFramework/Sources/DicomParsing/Internals/DicomImageDecoder.cpp	Mon Oct 06 09:51:39 2025 +0200
+++ b/OrthancFramework/Sources/DicomParsing/Internals/DicomImageDecoder.cpp	Tue Oct 07 12:04:31 2025 +0200
@@ -428,19 +428,27 @@
         lutBlue == NULL ||
         pixelData == NULL)
     {
-      throw OrthancException(ErrorCode_NotImplemented);
+      throw OrthancException(ErrorCode_NotImplemented, "Inconsistent Palette Color Lookup Table");
     }
 
     switch (format)
     {
       case PixelFormat_RGB24:
       {
-        if (r != "256\\0\\16" ||
-            rc != 256 ||
-            gc != 256 ||
-            bc != 256)
+        std::vector<std::string> splitR;
+        Toolbox::SplitString(splitR, r.c_str(), '\\');
+        if (splitR.size() != 3)
         {
-          throw OrthancException(ErrorCode_NotImplemented);
+          throw OrthancException(ErrorCode_NotImplemented, std::string("Palette Color Lookup Table Descriptor invalid length: '") + r.c_str() + "'");
+        }
+        
+        unsigned long paletteSize = boost::lexical_cast<unsigned long>(splitR[0]);
+        unsigned long nbBitsUsedInLut = boost::lexical_cast<unsigned long>(splitR[2]);  // The LUT is always 16bits but only part of it might be used
+        unsigned long offsetBits = nbBitsUsedInLut - 8; // Since the LUT is 16bits and the target color value is 8bpp
+
+        if (rc != paletteSize || gc != paletteSize || bc != paletteSize)
+        {
+          throw OrthancException(ErrorCode_NotImplemented, std::string("Palette Color Lookup Table Descriptor invalid palette size: '") + r.c_str() + "'");
         }
 
         if (pixelLength != target->GetWidth() * target->GetHeight())
@@ -477,9 +485,9 @@
 
           for (unsigned int x = 0; x < width; x++)
           {
-            p[0] = lutRed[*source] >> 8;
-            p[1] = lutGreen[*source] >> 8;
-            p[2] = lutBlue[*source] >> 8;
+            p[0] = lutRed[*source] >> offsetBits;
+            p[1] = lutGreen[*source] >> offsetBits;
+            p[2] = lutBlue[*source] >> offsetBits;
             source++;
             p += 3;
           }
@@ -496,7 +504,7 @@
             bc != 65536 ||
             pixelLength != 2 * target->GetWidth() * target->GetHeight())
         {
-          throw OrthancException(ErrorCode_NotImplemented);
+          throw OrthancException(ErrorCode_NotImplemented, std::string("Palette Color Lookup Table Descriptor not supported: '") + r.c_str() + "'");
         }
 
         const uint16_t* source = reinterpret_cast<const uint16_t*>(pixelData);