Index: GraphicsMagick-1.2.5/coders/tiff.c
===================================================================
--- GraphicsMagick-1.2.5.orig/coders/tiff.c	2017-03-21 18:07:00.771854003 +0100
+++ GraphicsMagick-1.2.5/coders/tiff.c	2017-03-21 18:33:26.732120954 +0100
@@ -894,6 +894,7 @@ static MagickPassFail InitializeImageCol
 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,
@@ -1013,12 +1014,12 @@ static MagickPassFail QuantumTransferMod
               }
             else
               {
-                if (image->matte)
+                if (image->matte && samples_per_pixel >= 5)
                   {
                     *quantum_type=CMYKAQuantum;
                     *quantum_samples=5;
                   }
-                else
+                else if (samples_per_pixel >= 4)
                   {
                     *quantum_type=CMYKQuantum;
                     *quantum_samples=4;
@@ -1677,7 +1678,7 @@ static Image *ReadTIFFImage(const ImageI
         method=RGBAPuntMethod;
         quantum_type=UndefinedQuantum;
         quantum_samples=0;
-        if (QuantumTransferMode(image,photometric,sample_format,planar_config,0,
+        if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,planar_config,0,
                                 &quantum_type,&quantum_samples) == MagickPass)
           {
             method=ScanLineMethod;
@@ -1784,7 +1785,7 @@ static Image *ReadTIFFImage(const ImageI
             max_sample=1;
             if (planar_config == PLANARCONFIG_SEPARATE)
               {
-                if (QuantumTransferMode(image,photometric,sample_format,
+                if (QuantumTransferMode(image,photometric,sample_format, samples_per_pixel,
                                         PLANARCONFIG_CONTIG,0,&quantum_type,
                                         &quantum_samples)
                     == MagickPass)
@@ -1816,7 +1817,7 @@ static Image *ReadTIFFImage(const ImageI
                     /*
                       Determine quantum parse method.
                     */
-                    if (QuantumTransferMode(image,photometric,sample_format,
+                    if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
                                             planar_config,sample,&quantum_type,
                                             &quantum_samples) == MagickFail)
                       {
@@ -1928,7 +1929,7 @@ static Image *ReadTIFFImage(const ImageI
             max_sample=1;
             if (planar_config == PLANARCONFIG_SEPARATE)
               {
-                if (QuantumTransferMode(image,photometric,sample_format,
+                if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
                                         PLANARCONFIG_CONTIG,0,&quantum_type,
                                         &quantum_samples) == MagickPass)
                   max_sample=quantum_samples;
@@ -1946,7 +1947,7 @@ static Image *ReadTIFFImage(const ImageI
                 /*
                   Determine quantum parse method.
                 */
-                if (QuantumTransferMode(image,photometric,sample_format,
+                if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
                                         planar_config,sample,&quantum_type,
                                         &quantum_samples) == MagickFail)
                   {
@@ -2124,7 +2125,7 @@ static Image *ReadTIFFImage(const ImageI
             max_sample=1;
             if (planar_config == PLANARCONFIG_SEPARATE)
               {
-                if (QuantumTransferMode(image,photometric,sample_format,
+                if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
                                         PLANARCONFIG_CONTIG,0,&quantum_type,
                                         &quantum_samples) == MagickPass)
                   max_sample=quantum_samples;
@@ -2167,7 +2168,7 @@ static Image *ReadTIFFImage(const ImageI
                         /*
                           Determine quantum parse method.
                         */
-                        if (QuantumTransferMode(image,photometric,sample_format,
+                        if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
                                                 planar_config,sample,
                                                 &quantum_type,&quantum_samples)
                             == MagickFail)
@@ -4123,7 +4124,7 @@ static MagickPassFail WriteTIFFImage(con
             max_sample=1;
             if (planar_config == PLANARCONFIG_SEPARATE)
               {
-                if (QuantumTransferMode(image,photometric,sample_format,
+                if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
                                         PLANARCONFIG_CONTIG,0,&quantum_type,
                                         &quantum_samples) == MagickPass)
                   max_sample=quantum_samples;
@@ -4136,7 +4137,7 @@ static MagickPassFail WriteTIFFImage(con
                 /*
                   Determine quantum parse method.
                 */
-                if (QuantumTransferMode(image,photometric,sample_format,
+                if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
                                         planar_config,sample,&quantum_type,
                                         &quantum_samples) == MagickFail)
                   {
@@ -4308,7 +4309,7 @@ static MagickPassFail WriteTIFFImage(con
             max_sample=1;
             if (planar_config == PLANARCONFIG_SEPARATE)
               {
-                if (QuantumTransferMode(image,photometric,sample_format,
+                if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
                                         PLANARCONFIG_CONTIG,0,&quantum_type,
                                         &quantum_samples) == MagickPass)
                   max_sample=quantum_samples;
@@ -4354,7 +4355,7 @@ static MagickPassFail WriteTIFFImage(con
                         /*
                           Determine quantum parse method.
                         */
-                        if (QuantumTransferMode(image,photometric,sample_format,
+                        if (QuantumTransferMode(image,photometric,sample_format,samples_per_pixel,
                                                 planar_config,sample,&quantum_type,
                                                 &quantum_samples) == MagickFail)
                           {
