Index: GraphicsMagick-1.2.5/coders/tiff.c
===================================================================
--- GraphicsMagick-1.2.5.orig/coders/tiff.c	2018-10-22 15:54:46.773972736 +0200
+++ GraphicsMagick-1.2.5/coders/tiff.c	2018-10-23 09:00:30.702003734 +0200
@@ -960,23 +960,86 @@ static MagickPassFail InitializeImageCol
   consumed by one pixel. Returns MagickPass if the photometric is
   supported.
 */
-static MagickPassFail QuantumTransferMode(const Image *image,
-                                          const uint16 photometric,
-                                          const uint16 sample_format,
-                                          const unsigned int samples_per_pixel,
-                                          const uint16 planar_config,
-                                          const unsigned int plane,
-                                          QuantumType *quantum_type,
-                                          int *quantum_samples)
+
+static MagickPassFail
+QuantumTransferMode(const Image *image,
+                    const uint16 photometric,
+                    const uint16 compress_tag,
+                    const uint16 sample_format,
+                    const unsigned int samples_per_pixel,
+                    const uint16 planar_config,
+                    const unsigned int plane,
+                    QuantumType *quantum_type,
+                    int *quantum_samples,
+                    ExceptionInfo *exception)
 {
+  ARG_NOT_USED(exception);
+
   *quantum_type=UndefinedQuantum;
   *quantum_samples=0;
-  if ((sample_format == SAMPLEFORMAT_UINT) ||
+  if ((sample_format == SAMPLEFORMAT_INT) ||
+      (sample_format == SAMPLEFORMAT_UINT) ||
       (sample_format == SAMPLEFORMAT_VOID) ||
       (sample_format == SAMPLEFORMAT_IEEEFP))
     {
       switch (photometric)
         {
+        case PHOTOMETRIC_CIELAB:
+          {
+            /* samples_per_pixel may be 1 or 3 */
+            if (planar_config == PLANARCONFIG_SEPARATE)
+              {
+                switch (plane)
+                  {
+                  case 0:
+                    if (samples_per_pixel == 1)
+                      *quantum_type=GrayQuantum;
+                      else
+                        *quantum_type=RedQuantum;
+                    break;
+                  case 1:
+                    *quantum_type=GreenQuantum;
+                    break;
+                  case 2:
+                    *quantum_type=BlueQuantum;
+                    break;
+                  case 3:
+                    *quantum_type=AlphaQuantum;
+                    break;
+                  }
+                *quantum_samples=1;
+              }
+            else
+              {
+                if (samples_per_pixel == 1)
+                  {
+                    if (image->matte)
+                      {
+                        *quantum_type=GrayAlphaQuantum;
+                        *quantum_samples=2;
+                      }
+                    else
+                      {
+                        *quantum_type=GrayQuantum;
+                        *quantum_samples=1;
+                      }
+                  }
+                else
+                  {
+                    if (image->matte)
+                      {
+                        *quantum_type=RGBAQuantum;
+                        *quantum_samples=4;
+                      }
+                    else
+                      {
+                        *quantum_type=RGBQuantum;
+                        *quantum_samples=3;
+                      }
+                  }
+              }
+            break;
+          }
         case PHOTOMETRIC_LOGL:
           {
             *quantum_type=CIEYQuantum;
@@ -985,22 +1048,48 @@ static MagickPassFail QuantumTransferMod
           }
         case PHOTOMETRIC_LOGLUV:
           {
-            *quantum_type=CIEXYZQuantum;
-            *quantum_samples=3;
+            if (samples_per_pixel == 1)
+              {
+                /* FIXME: this might not work. */
+                *quantum_type=CIEYQuantum;
+                *quantum_samples=1;
+              }
+            else
+              {
+                *quantum_type=CIEXYZQuantum;
+                *quantum_samples=3;
+              }
             break;
           }
         case PHOTOMETRIC_MINISBLACK:
         case PHOTOMETRIC_MINISWHITE:
           {
-            if (image->matte)
+            if (planar_config == PLANARCONFIG_SEPARATE)
               {
-                *quantum_type=GrayAlphaQuantum;
-                *quantum_samples=2;
+                switch (plane)
+                  {
+                  case 0:
+                    *quantum_type=GrayQuantum;
+                    *quantum_samples=1;
+                    break;
+                  case 1:
+                    *quantum_type=AlphaQuantum;
+                    *quantum_samples=1;
+                    break;
+                  }
               }
             else
               {
-                *quantum_type=GrayQuantum;
-                *quantum_samples=1;
+                if (image->matte)
+                  {
+                    *quantum_type=GrayAlphaQuantum;
+                    *quantum_samples=2;
+                  }
+                else
+                  {
+                    *quantum_type=GrayQuantum;
+                    *quantum_samples=1;
+                  }
               }
             break;
           }
@@ -1023,36 +1112,39 @@ static MagickPassFail QuantumTransferMod
           }
         case PHOTOMETRIC_RGB:
           {
-            if (planar_config == PLANARCONFIG_SEPARATE)
+            if (compress_tag != COMPRESSION_OJPEG)
               {
-                switch (plane)
+                if (planar_config == PLANARCONFIG_SEPARATE)
                   {
-                  case 0:
-                    *quantum_type=RedQuantum;
-                    break;
-                  case 1:
-                    *quantum_type=GreenQuantum;
-                    break;
-                  case 2:
-                    *quantum_type=BlueQuantum;
-                    break;
-                  case 3:
-                    *quantum_type=AlphaQuantum;
-                    break;
-                  }
-                *quantum_samples=1;
-              }
-            else
-              {
-                if (image->matte)
-                  {
-                    *quantum_type=RGBAQuantum;
-                    *quantum_samples=4;
+                    switch (plane)
+                      {
+                      case 0:
+                        *quantum_type=RedQuantum;
+                        break;
+                      case 1:
+                        *quantum_type=GreenQuantum;
+                        break;
+                      case 2:
+                        *quantum_type=BlueQuantum;
+                        break;
+                      case 3:
+                        *quantum_type=AlphaQuantum;
+                        break;
+                      }
+                    *quantum_samples=1;
                   }
                 else
                   {
-                    *quantum_type=RGBQuantum;
-                    *quantum_samples=3;
+                    if (image->matte)
+                      {
+                        *quantum_type=RGBAQuantum;
+                        *quantum_samples=4;
+                      }
+                    else
+                      {
+                        *quantum_type=RGBQuantum;
+                        *quantum_samples=3;
+                      }
                   }
               }
             break;
@@ -1083,12 +1175,12 @@ static MagickPassFail QuantumTransferMod
               }
             else
               {
-                if (image->matte && samples_per_pixel >= 5)
+                if (image->matte)
                   {
                     *quantum_type=CMYKAQuantum;
                     *quantum_samples=5;
                   }
-                else if (samples_per_pixel >= 4)
+                else
                   {
                     *quantum_type=CMYKQuantum;
                     *quantum_samples=4;
@@ -1096,12 +1188,45 @@ static MagickPassFail QuantumTransferMod
               }
             break;
           }
+        case PHOTOMETRIC_YCBCR:
+          {
+            /*
+              This is here to support JPEGCOLORMODE_RGB which claims a
+              YCbCr photometric, but passes RGB to libtiff.
+            */
+            if (compress_tag == COMPRESSION_JPEG)
+              {
+                *quantum_type=RGBQuantum;
+                *quantum_samples=3;
+              }
+            break;
+          }
         }
     }
-  
+  /* fprintf(stderr,"Quantum Type: %d Quantum Samples: %d\n",(int) *quantum_type,*quantum_samples); */
+  /* FIXME: We do need to support YCbCr! */
+
+  if (*quantum_samples != 0)
+    {
+      /*
+        Enforce that there are no buffer-overruns.
+      */
+      if (((planar_config == PLANARCONFIG_SEPARATE) && (*quantum_samples != 1)) ||
+          ((unsigned int) (*quantum_samples) > samples_per_pixel))
+        {
+          if (image->logging)
+            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+                                  "Expected >= %u samples per pixel, have only %d!",
+                                  *quantum_samples, samples_per_pixel);
+          ThrowException(exception,CorruptImageError,ImproperImageHeader,
+                         image->filename);
+          *quantum_type=UndefinedQuantum;
+          *quantum_samples=0;
+        }
+    }
+
   return (*quantum_samples != 0 ? MagickPass : MagickFail);
 }
-
 /*
   Compact samples to only contain raster data. This may seem
   inefficient, but it allows us to easily deal with contiguous images
@@ -1752,8 +1877,8 @@ static Image *ReadTIFFImage(const ImageI
         method=RGBAPuntMethod;
         quantum_type=UndefinedQuantum;
         quantum_samples=0;
-        if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,planar_config,0,
-                                &quantum_type,&quantum_samples) == MagickPass)
+        if (QuantumTransferMode(image,photometric,compress_tag,sample_format,samples_per_pixel,planar_config,0,
+                                &quantum_type,&quantum_samples,exception) == MagickPass)
           {
             method=ScanLineMethod;
             if (compress_tag == COMPRESSION_JPEG)
@@ -1873,9 +1998,9 @@ static Image *ReadTIFFImage(const ImageI
             max_sample=1;
             if (planar_config == PLANARCONFIG_SEPARATE)
               {
-                if (QuantumTransferMode(image,photometric,sample_format, samples_per_pixel,
+                if (QuantumTransferMode(image,photometric,compress_tag,sample_format, samples_per_pixel,
                                         PLANARCONFIG_CONTIG,0,&quantum_type,
-                                        &quantum_samples)
+                                        &quantum_samples,exception)
                     == MagickPass)
                   max_sample=quantum_samples;
               }
@@ -1905,9 +2030,9 @@ static Image *ReadTIFFImage(const ImageI
                     /*
                       Determine quantum parse method.
                     */
-                    if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
+                    if (QuantumTransferMode(image,photometric,compress_tag,sample_format,samples_per_pixel,
                                             planar_config,sample,&quantum_type,
-                                            &quantum_samples) == MagickFail)
+                                            &quantum_samples,exception) == MagickFail)
                       {
                         CopyException(exception,&image->exception);
                         status=MagickFail;
@@ -2031,9 +2156,9 @@ static Image *ReadTIFFImage(const ImageI
             max_sample=1;
             if (planar_config == PLANARCONFIG_SEPARATE)
               {
-                if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
+                if (QuantumTransferMode(image,photometric,compress_tag,sample_format,samples_per_pixel,
                                         PLANARCONFIG_CONTIG,0,&quantum_type,
-                                        &quantum_samples) == MagickPass)
+                                        &quantum_samples,exception) == MagickPass)
                   max_sample=quantum_samples;
               }
             /*
@@ -2049,9 +2174,9 @@ static Image *ReadTIFFImage(const ImageI
                 /*
                   Determine quantum parse method.
                 */
-                if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
+                if (QuantumTransferMode(image,photometric,compress_tag,sample_format,samples_per_pixel,
                                         planar_config,sample,&quantum_type,
-                                        &quantum_samples) == MagickFail)
+                                        &quantum_samples,exception) == MagickFail)
                   {
                     CopyException(exception,&image->exception);
                     status=MagickFail;
@@ -2245,9 +2370,9 @@ static Image *ReadTIFFImage(const ImageI
             max_sample=1;
             if (planar_config == PLANARCONFIG_SEPARATE)
               {
-                if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
+                if (QuantumTransferMode(image,photometric,compress_tag,sample_format,samples_per_pixel,
                                         PLANARCONFIG_CONTIG,0,&quantum_type,
-                                        &quantum_samples) == MagickPass)
+                                        &quantum_samples,exception) == MagickPass)
                   max_sample=quantum_samples;
               }
             /*
@@ -2288,9 +2413,9 @@ static Image *ReadTIFFImage(const ImageI
                         /*
                           Determine quantum parse method.
                         */
-                        if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
+                        if (QuantumTransferMode(image,photometric,compress_tag,sample_format,samples_per_pixel,
                                                 planar_config,sample,
-                                                &quantum_type,&quantum_samples)
+                                                &quantum_type,&quantum_samples,exception)
                             == MagickFail)
                           {
                             CopyException(exception,&image->exception);
@@ -4258,9 +4383,9 @@ static MagickPassFail WriteTIFFImage(con
             max_sample=1;
             if (planar_config == PLANARCONFIG_SEPARATE)
               {
-                if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
+                if (QuantumTransferMode(image,photometric,compress_tag,sample_format,samples_per_pixel,
                                         PLANARCONFIG_CONTIG,0,&quantum_type,
-                                        &quantum_samples) == MagickPass)
+                                        &quantum_samples,&image->exception) == MagickPass)
                   max_sample=quantum_samples;
               }
             /*
@@ -4271,9 +4396,9 @@ static MagickPassFail WriteTIFFImage(con
                 /*
                   Determine quantum parse method.
                 */
-                if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
+                if (QuantumTransferMode(image,photometric,compress_tag,sample_format,samples_per_pixel,
                                         planar_config,sample,&quantum_type,
-                                        &quantum_samples) == MagickFail)
+                                        &quantum_samples,&image->exception) == MagickFail)
                   {
                     status=MagickFail;
                     break;
@@ -4443,9 +4568,9 @@ static MagickPassFail WriteTIFFImage(con
             max_sample=1;
             if (planar_config == PLANARCONFIG_SEPARATE)
               {
-                if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
+                if (QuantumTransferMode(image,photometric,compress_tag,sample_format,samples_per_pixel,
                                         PLANARCONFIG_CONTIG,0,&quantum_type,
-                                        &quantum_samples) == MagickPass)
+                                        &quantum_samples,&image->exception) == MagickPass)
                   max_sample=quantum_samples;
               }
             /*
@@ -4489,9 +4614,9 @@ static MagickPassFail WriteTIFFImage(con
                         /*
                           Determine quantum parse method.
                         */
-                        if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
+                        if (QuantumTransferMode(image,photometric,compress_tag,sample_format,samples_per_pixel,
                                                 planar_config,sample,&quantum_type,
-                                                &quantum_samples) == MagickFail)
+                                                &quantum_samples,&image->exception) == MagickFail)
                           {
                             status=MagickFail;
                             break;
