File GraphicsMagick-dcm.c-update.patch of Package GraphicsMagick.10463

Index: GraphicsMagick-1.3.29/coders/dcm.c
===================================================================
--- GraphicsMagick-1.3.29.orig/coders/dcm.c	2018-04-29 20:01:26.000000000 +0200
+++ GraphicsMagick-1.3.29/coders/dcm.c	2018-06-13 20:03:24.671464110 +0200
@@ -183,7 +183,7 @@ typedef struct _DicomStream
     lower_lim;
 
   Quantum
-    *rescale_map;
+    *rescale_map; /* Allocated with dcm->max_value_in+1 entries */
 
 #if defined(USE_GRAYMAP)
   unsigned short
@@ -3336,6 +3336,10 @@ static MagickPassFail funcDCM_Palette(Im
       return MagickFail;
     }
 
+  if (image->logging)
+    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+                          "Palette with %" MAGICK_SIZE_T_F "u entries...",
+                          (MAGICK_SIZE_T) dcm->length);
   /*
     Initialize colormap (entries are always 16 bit)
     1201/2/3 = red/green/blue palette
@@ -3781,21 +3785,38 @@ static MagickPassFail DCM_SetupRescaleMa
     Xw_min,
     Xw_max;
 
-  unsigned long
+  unsigned int
     i;
 
   if (dcm->rescaling == DCM_RS_NONE)
     return MagickPass;
 
+  if (image->logging)
+    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+                          "Set up rescale map for input range of %u"
+                          " (%u entries)...",
+                          dcm->max_value_in+1,MaxMap+1);
+
+  /*
+    The rescale map must be limited to MaxMap+1 entries, which is 256
+    or 65536, depending on QuantumDepth.  Using a QuantumDepth less
+    than 16 for DICOM is a bad idea.
+
+    The dcm->significant_bits value is limited to 16 (larger values
+    are outright rejected) so dcm->max_value_in and dcm->max_value_out
+    are limited to 65535.
+  */
+
   if (dcm->rescale_map == (Quantum *) NULL)
     {
-      dcm->rescale_map=MagickAllocateArray(Quantum *,dcm->max_value_in+1,sizeof(Quantum));
+      size_t num_entries = Max(MaxMap+1,dcm->max_value_in+1);
+      dcm->rescale_map=MagickAllocateArray(Quantum *,num_entries,sizeof(Quantum));
       if (dcm->rescale_map == NULL)
         {
           ThrowException(exception,ResourceLimitError,MemoryAllocationFailed,image->filename);
           return MagickFail;
         }
-      (void) memset(dcm->rescale_map,0,(size_t) dcm->max_value_in+1*sizeof(Quantum));
+      (void) memset(dcm->rescale_map,0,num_entries*sizeof(Quantum));
     }
 
   if (dcm->window_width == 0)
@@ -3837,8 +3858,9 @@ static MagickPassFail DCM_SetupRescaleMa
         dcm->rescale_map[i]=(Quantum)(((Xr-Xw_min)/(win_width-1))*dcm->max_value_out+0.5);
     }
   if (dcm->phot_interp == DCM_PI_MONOCHROME1)
-    for (i=0; i < (dcm->max_value_in+1); i++)
+    for (i=0; i <= dcm->max_value_in; i++)
       dcm->rescale_map[i]=dcm->max_value_out-dcm->rescale_map[i];
+
   return MagickPass;
 }
 
@@ -3904,7 +3926,6 @@ void DCM_SetRescaling(DicomStream *dcm,i
   dcm->rescaling=DCM_RS_PRE;
 }
 
-#if 0
 /*
   FIXME: This code is totally broken since DCM_SetupRescaleMap
   populates dcm->rescale_map and dcm->rescale_map has
@@ -4023,14 +4044,13 @@ static MagickPassFail DCM_PostRescaleIma
     }
   return MagickPass;
 }
-#endif
 
 static MagickPassFail DCM_ReadPaletteImage(Image *image,DicomStream *dcm,ExceptionInfo *exception)
 {
-  long
+  unsigned long
     y;
 
-  register long
+  register unsigned long
     x;
 
   register PixelPacket
@@ -4051,13 +4071,13 @@ static MagickPassFail DCM_ReadPaletteIma
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
                           "Reading Palette image...");
 
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < image->rows; y++)
     {
       q=SetImagePixels(image,0,y,image->columns,1);
       if (q == (PixelPacket *) NULL)
         return MagickFail;
       indexes=AccessMutableIndexes(image);
-      for (x=0; x < (long) image->columns; x++)
+      for (x=0; x < image->columns; x++)
         {
           if (dcm->bytes_per_pixel == 1)
             {
@@ -4113,6 +4133,8 @@ static MagickPassFail DCM_ReadPaletteIma
               index=(IndexPacket) (index);
               VerifyColormapIndex(image,index);
               indexes[x]=index;
+              *q=image->colormap[index];
+              q++;
             }
 
           if (EOFBlob(image))
@@ -4135,16 +4157,16 @@ static MagickPassFail DCM_ReadPaletteIma
 
 static MagickPassFail DCM_ReadGrayscaleImage(Image *image,DicomStream *dcm,ExceptionInfo *exception)
 {
-  long
+  unsigned long
     y;
 
-  register long
+  register unsigned long
     x;
 
   register PixelPacket
     *q;
 
-#if defined(GRAYSCALE_USES_PALETTE)
+#if defined(GRAYSCALE_USES_PALETTE) /* not used */
   register IndexPacket
     *indexes;
 #endif
@@ -4157,20 +4179,27 @@ static MagickPassFail DCM_ReadGrayscaleI
 
   if (image->logging)
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
-                          "Reading Grayscale image...");
+                          "Reading Grayscale %lux%lu image...",image->columns,image->rows);
+
+#if !defined(GRAYSCALE_USES_PALETTE)
+  /*
+    If a palette was provided, the image may be in PseudoClass
+  */
+  image->storage_class=DirectClass;
+#endif
 
   dcm->lower_lim = dcm->max_value_in;
   dcm->upper_lim = -(dcm->lower_lim);
   byte=0;
-  for (y=0; y < (long) image->rows; y++)
+  for (y=0; y < image->rows; y++)
     {
-      q=SetImagePixels(image,0,y,image->columns,1);
+      q=SetImagePixelsEx(image,0,y,image->columns,1,exception);
       if (q == (PixelPacket *) NULL)
         return MagickFail;
-#if defined(GRAYSCALE_USES_PALETTE)
+#if defined(GRAYSCALE_USES_PALETTE) /* not used */
       indexes=AccessMutableIndexes(image);
 #endif
-      for (x=0; x < (long) image->columns; x++)
+      for (x=0; x < image->columns; x++)
         {
           if (dcm->bytes_per_pixel == 1)
             {
@@ -4223,7 +4252,7 @@ static MagickPassFail DCM_ReadGrayscaleI
               if ((int) l > dcm->upper_lim)
                 dcm->upper_lim = l;
             }
-#if defined(GRAYSCALE_USES_PALETTE)
+#if defined(GRAYSCALE_USES_PALETTE) /* not used */
           if (dcm->rescaling == DCM_RS_PRE)
             indexes[x]=dcm->rescale_map[index];
           else
@@ -4231,10 +4260,13 @@ static MagickPassFail DCM_ReadGrayscaleI
 #else
           if ((dcm->rescaling == DCM_RS_PRE) &&
               (dcm->rescale_map != (Quantum *) NULL))
-            index=dcm->rescale_map[index];
+            {
+              index=dcm->rescale_map[index];
+            }
           q->red=index;
           q->green=index;
           q->blue=index;
+          q->opacity=OpaqueOpacity;
           q++;
 #endif
           if (EOFBlob(image))
@@ -4243,7 +4275,7 @@ static MagickPassFail DCM_ReadGrayscaleI
               return MagickFail;
             }
         }
-      if (!SyncImagePixels(image))
+      if (!SyncImagePixelsEx(image,exception))
         return MagickFail;
       if (image->previous == (Image *) NULL)
         if (QuantumTick(y,image->rows))
@@ -4270,6 +4302,12 @@ static MagickPassFail DCM_ReadPlanarRGBI
                           "Reading Planar RGB %s compressed image with %u planes...",
                           (dcm->transfer_syntax == DCM_TS_RLE ? "RLE" : "not"),
                           dcm->samples_per_pixel);
+  /*
+    Force image to DirectClass since we are only updating DirectClass
+    representation.  The image may be in PseudoClass if we were
+    previously provided with a Palette.
+  */
+  image->storage_class=DirectClass;
 
   for (plane=0; plane < dcm->samples_per_pixel; plane++)
     {
@@ -4353,6 +4391,13 @@ static MagickPassFail DCM_ReadRGBImage(I
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
                           "Reading RGB image...");
 
+  /*
+    Force image to DirectClass since we are only updating DirectClass
+    representation.  The image may be in PseudoClass if we were
+    previously provided with a Palette.
+  */
+  image->storage_class=DirectClass;
+
   for (y=0; y < image->rows; y++)
     {
       q=GetImagePixels(image,0,y,image->columns,1);
@@ -4406,6 +4451,7 @@ static MagickPassFail DCM_ReadRGBImage(I
           q->red=(Quantum) red;
           q->green=(Quantum) green;
           q->blue=(Quantum) blue;
+          q->opacity=OpaqueOpacity;
           q++;
           if (EOFBlob(image))
             {
@@ -4622,7 +4668,7 @@ static MagickPassFail DCM_ReadNonNativeI
                   dcm->bytes_per_pixel=2;
                 dcm->max_value_in=MaxValueGivenBits(dcm->significant_bits);
                 dcm->max_value_out=dcm->max_value_in;
-                /*status=DCM_PostRescaleImage(next_image,dcm,True,exception);*/
+                status=DCM_PostRescaleImage(next_image,dcm,True,exception);
               }
           if (status == MagickPass)
             {
@@ -4928,9 +4974,9 @@ static Image *ReadDCMImage(const ImageIn
               if (image->logging)
                 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
                                       "Rescaling image channels...");
-              /*status = DCM_PostRescaleImage(image,&dcm,False,exception);
-                if (status != MagickPass)
-                break;*/
+              status = DCM_PostRescaleImage(image,&dcm,False,exception);
+              if (status != MagickPass)
+                break;
             }
         }
 
openSUSE Build Service is sponsored by