]> granicus.if.org Git - imagemagick/blobdiff - coders/tiff.c
(no commit message)
[imagemagick] / coders / tiff.c
index b3c92d3a004bca49f1723766b552ebfa1452b774..229639dd06fcc360c613c4344a5607899ca50781 100644 (file)
@@ -46,6 +46,7 @@
 #include "magick/cache.h"
 #include "magick/color.h"
 #include "magick/color-private.h"
+#include "magick/colormap.h"
 #include "magick/colorspace.h"
 #include "magick/constitute.h"
 #include "magick/enhance.h"
@@ -262,7 +263,7 @@ static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
 %
 */
 
-static inline size_t WriteLSBLong(FILE *file,const unsigned int value)
+static inline size_t WriteLSBLong(FILE *file,const size_t value)
 {
   unsigned char
     buffer[4];
@@ -340,25 +341,25 @@ static Image *ReadGROUP4Image(const ImageInfo *image_info,
   length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
   length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
   strip_offset=10+(12*14)+4+8;
-  length=WriteLSBLong(file,(unsigned long) strip_offset);
+  length=WriteLSBLong(file,(size_t) strip_offset);
   length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
-  length=WriteLSBLong(file,(unsigned long) image_info->orientation);
+  length=WriteLSBLong(file,(size_t) image_info->orientation);
   length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
   length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
   length=WriteLSBLong(file,image->rows);
   length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
   offset=(ssize_t) ftell(file)-4;
   length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
-  length=WriteLSBLong(file,(unsigned long) (strip_offset-8));
+  length=WriteLSBLong(file,(size_t) (strip_offset-8));
   length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
-  length=WriteLSBLong(file,(unsigned long) (strip_offset-8));
+  length=WriteLSBLong(file,(size_t) (strip_offset-8));
   length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
   length=fwrite("\000\000\000\000",1,4,file);
   length=WriteLSBLong(file,image->x_resolution);
   length=WriteLSBLong(file,1);
   for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
     (void) fputc(c,file);
-  offset=(ssize_t) fseek(file,(long) offset,SEEK_SET);
+  offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
   length=WriteLSBLong(file,(unsigned int) length);
   (void) fclose(file);
   (void) CloseBlob(image);
@@ -420,7 +421,7 @@ static inline size_t MagickMax(const size_t x,const size_t y)
   return(y);
 }
 
-static inline long MagickMin(const long x,const long y)
+static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
 {
   if (x < y)
     return(x);
@@ -428,12 +429,12 @@ static inline long MagickMin(const long x,const long y)
 }
 
 static MagickBooleanType ReadProfile(Image *image,const char *name,
-  unsigned char *datum,long length)
+  unsigned char *datum,ssize_t length)
 {
   MagickBooleanType
     status;
 
-  register long
+  register ssize_t
     i;
 
   StringInfo
@@ -490,8 +491,8 @@ static void TIFFErrors(const char *module,const char *format,va_list error)
   (void) ConcatenateMagickString(message,".",MaxTextExtent);
   exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
   if (exception != (ExceptionInfo *) NULL)
-    (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
-      message,"`%s'",module);
+    (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
+      "`%s'",module);
 }
 
 static void TIFFGetProfiles(TIFF *tiff,Image *image)
@@ -505,30 +506,30 @@ static void TIFFGetProfiles(TIFF *tiff,Image *image)
 #if defined(TIFFTAG_ICCPROFILE)
   length=0;
   if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
-    (void) ReadProfile(image,"icc",profile,(long) length);
+    (void) ReadProfile(image,"icc",profile,(ssize_t) length);
 #endif
 #if defined(TIFFTAG_PHOTOSHOP)
   length=0;
   if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
-    (void) ReadProfile(image,"8bim",profile,(long) length);
+    (void) ReadProfile(image,"8bim",profile,(ssize_t) length);
 #endif
 #if defined(TIFFTAG_RICHTIFFIPTC)
   length=0;
   if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
     {
       if (TIFFIsByteSwapped(tiff) != 0)
-        TIFFSwabArrayOfLong((uint32 *) profile,(unsigned long) length);
+        TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
       (void) ReadProfile(image,"iptc",profile,4L*length);
     }
 #endif
 #if defined(TIFFTAG_XMLPACKET)
   length=0;
   if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
-    (void) ReadProfile(image,"xmp",profile,(long) length);
+    (void) ReadProfile(image,"xmp",profile,(ssize_t) length);
 #endif
   length=0;
   if (TIFFGetField(tiff,37724,&length,&profile) == 1)
-    (void) ReadProfile(image,"tiff:37724",profile,(long) length);
+    (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length);
 }
 
 static void TIFFGetProperties(TIFF *tiff,Image *image)
@@ -564,7 +565,7 @@ static void TIFFGetEXIFProperties(TIFF *tiff,Image *image)
   char
     value[MaxTextExtent];
 
-  register long
+  register ssize_t
     i;
 
   tdir_t
@@ -607,10 +608,10 @@ static void TIFFGetEXIFProperties(TIFF *tiff,Image *image)
       case TIFF_LONG:
       {
         uint32
-          longy;
+          ssize_ty;
 
-        if (TIFFGetField(tiff,exif_info[i].tag,&longy) != 0)
-          (void) FormatMagickString(value,MaxTextExtent,"%d",longy);
+        if (TIFFGetField(tiff,exif_info[i].tag,&ssize_ty) != 0)
+          (void) FormatMagickString(value,MaxTextExtent,"%d",ssize_ty);
         break;
       }
       case TIFF_RATIONAL:
@@ -656,8 +657,8 @@ static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
   return(count);
 }
 
-static int32 TIFFReadPixels(TIFF *tiff,unsigned long bits_per_sample,
-  tsample_t sample,long row,tdata_t scanline)
+static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
+  tsample_t sample,ssize_t row,tdata_t scanline)
 {
   int32
     status;
@@ -744,10 +745,11 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
   Image
     *image;
 
-  long
+  ssize_t
     y;
 
   MagickBooleanType
+    associated_alpha,
     debug,
     status;
 
@@ -760,7 +762,7 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
   QuantumType
     quantum_type;
 
-  register long
+  register ssize_t
     i;
 
   size_t
@@ -802,7 +804,7 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
   unsigned char
     *pixels;
 
-  unsigned long
+  size_t
     lsb_first;
 
   /*
@@ -841,7 +843,7 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
       /*
         Generate blank images for subimage specification (e.g. image.tif[4].
       */
-      for (i=0; i < (long) image_info->scene; i++)
+      for (i=0; i < (ssize_t) image_info->scene; i++)
       {
         (void) TIFFReadDirectory(tiff);
         AcquireNextImage(image_info,image);
@@ -857,6 +859,8 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
   {
     if (0 && (image_info->verbose != MagickFalse))
       TIFFPrintDirectory(tiff,stdout,MagickFalse);
+    (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
+      "lsb" : "msb");
     (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
     (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
     (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
@@ -900,6 +904,11 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
         (void) SetImageProperty(image,"tiff:photometric","separated");
         break;
       }
+      case PHOTOMETRIC_YCBCR:
+      {
+        (void) SetImageProperty(image,"tiff:photometric","YCBCR");
+        break;
+      }
       default:
       {
         (void) SetImageProperty(image,"tiff:photometric","unknown");
@@ -921,12 +930,12 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
         (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
           "interpretation: %s",GetImageProperty(image,"tiff:photometric"));
       }
-    image->columns=(unsigned long) width;
-    image->rows=(unsigned long) height;
-    image->depth=(unsigned long) bits_per_sample;
+    image->columns=(size_t) width;
+    image->rows=(size_t) height;
+    image->depth=(size_t) bits_per_sample;
     if (image->debug != MagickFalse)
-      (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %lu",
-        image->depth);
+      (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
+        (double) image->depth);
     lsb_first=1;
     image->endian=MSBEndian;
     if ((int) (*(char *) &lsb_first) != 0)
@@ -948,8 +957,8 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
     y_position=(float) image->page.y/y_resolution;
     (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
     (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
-    image->page.x=(long) (x_position*x_resolution+0.5);
-    image->page.y=(long) (y_position*y_resolution+0.5);
+    image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
+    image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
     image->orientation=(OrientationType) orientation;
     chromaticity=(float *) NULL;
     (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
@@ -1029,7 +1038,10 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
     }
     quantum_info=AcquireQuantumInfo(image_info,image);
     if (quantum_info == (QuantumInfo *) NULL)
-      ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+      {
+        TIFFClose(tiff);
+        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+      }
     if (sample_format == SAMPLEFORMAT_UINT)
       status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
     if (sample_format == SAMPLEFORMAT_INT)
@@ -1037,7 +1049,10 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
     if (sample_format == SAMPLEFORMAT_IEEEFP)
       status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
     if (status == MagickFalse)
-      ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+      {
+        TIFFClose(tiff);
+        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+      }
     status=MagickTrue;
     switch (photometric)
     {
@@ -1054,6 +1069,7 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
       default:
         break;
     }
+    associated_alpha=MagickFalse;
     extra_samples=0;
     (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
       &sample_info);
@@ -1066,27 +1082,23 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
       for (i=0; i < extra_samples; i++)
       {
         image->matte=MagickTrue;
-        image->alpha_channel=UndefinedAlphaChannel;
-        (void) SetImageProperty(image,"tiff:alpha","undefined");
-        if (sample_info[i] == EXTRASAMPLE_UNASSALPHA)
-          {
-            image->alpha_channel=UnassociatedAlphaChannel;
-            (void) SetImageProperty(image,"tiff:alpha","unassociated");
-          }
         if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
-          {
-            image->alpha_channel=AssociatedAlphaChannel;
-            SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
-            (void) SetImageProperty(image,"tiff:alpha","associated");
-          }
+          SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
       }
+    option=GetImageOption(image_info,"tiff:alpha");
+    if (option != (const char *) NULL)
+      associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
+        MagickFalse;
+    if (image->matte != MagickFalse)
+      (void) SetImageProperty(image,"tiff:alpha",
+        associated_alpha != MagickFalse ? "associated" : "unassociated");
     if ((photometric == PHOTOMETRIC_PALETTE) &&
         (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
       {
-        unsigned long
+        size_t
           colors;
 
-        colors=(unsigned long) GetQuantumRange(bits_per_sample)+1;
+        colors=(size_t) GetQuantumRange(bits_per_sample)+1;
         if (AcquireImageColormap(image,colors) == MagickFalse)
           {
             TIFFClose(tiff);
@@ -1134,6 +1146,8 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
     if ((photometric != PHOTOMETRIC_SEPARATED) &&
         (interlace == PLANARCONFIG_SEPARATE))
       method=ReadGenericMethod;
+    if (image->compression == JPEGCompression)
+      method=ReadGenericMethod;
     if (TIFFIsTiled(tiff) != MagickFalse)
       method=ReadTileMethod;
     quantum_type=RGBQuantum;
@@ -1148,7 +1162,7 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
         if ((image->storage_class == PseudoClass) &&
             (photometric == PHOTOMETRIC_PALETTE))
           {
-            unsigned long
+            size_t
               range;
 
             uint16
@@ -1162,14 +1176,14 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
             (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
               &green_colormap,&blue_colormap);
             range=255;  /* might be old style 8-bit colormap */
-            for (i=0; i < (long) image->colors; i++)
+            for (i=0; i < (ssize_t) image->colors; i++)
               if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
                   (blue_colormap[i] >= 256))
                 {
                   range=65535;
                   break;
                 }
-            for (i=0; i < (long) image->colors; i++)
+            for (i=0; i < (ssize_t) image->colors; i++)
             {
               image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
                 red_colormap[i])/range);
@@ -1203,9 +1217,12 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
             }
         status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
         if (status == MagickFalse)
-          ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+          {
+            TIFFClose(tiff);
+            ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+          }
         pixels=GetQuantumPixels(quantum_info);
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
           int
             status;
@@ -1225,7 +1242,8 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
             break;
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,LoadImageTag,y,image->rows);
+              status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
@@ -1256,9 +1274,12 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
           }
         status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
         if (status == MagickFalse)
-          ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+          {
+            TIFFClose(tiff);
+            ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+          }
         pixels=GetQuantumPixels(quantum_info);
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
           int
             status;
@@ -1278,7 +1299,8 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
             break;
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,LoadImageTag,y,image->rows);
+              status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
@@ -1290,9 +1312,9 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
         /*
           Convert TIFF image to DirectClass MIFF image.
         */
-        for (i=0; i < (long) samples_per_pixel; i++)
+        for (i=0; i < (ssize_t) samples_per_pixel; i++)
         {
-          for (y=0; y < (long) image->rows; y++)
+          for (y=0; y < (ssize_t) image->rows; y++)
           {
             register PixelPacket
               *restrict q;
@@ -1333,7 +1355,8 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
           }
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,LoadImageTag,y,image->rows);
+              status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
@@ -1350,9 +1373,9 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
         */
         i=0;
         p=(unsigned char *) NULL;
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register long
+          register ssize_t
             x;
 
           register PixelPacket
@@ -1365,11 +1388,12 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
             {
               if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
                 break;
-              i=(long) MagickMin((long) rows_per_strip,(long) image->rows-y);
+              i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
+                image->rows-y);
             }
           i--;
           p=pixels+image->columns*i;
-          for (x=0; x < (long) image->columns; x++)
+          for (x=0; x < (ssize_t) image->columns; x++)
           {
             q->red=ScaleCharToQuantum((unsigned char) (TIFFGetR(*p)));
             q->green=ScaleCharToQuantum((unsigned char) (TIFFGetG(*p)));
@@ -1383,7 +1407,8 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
             break;
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,LoadImageTag,y,image->rows);
+              status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
@@ -1400,7 +1425,7 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
           columns,
           rows;
 
-        unsigned long
+        size_t
           number_pixels;
 
         /*
@@ -1421,38 +1446,38 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
             TIFFClose(tiff);
             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
           }
-        for (y=0; y < (long) image->rows; y+=rows)
+        for (y=0; y < (ssize_t) image->rows; y+=rows)
         {
           PixelPacket
             *tile;
 
-          register long
+          register ssize_t
             x;
 
           register PixelPacket
             *restrict q;
 
-          unsigned long
+          size_t
             columns_remaining,
             rows_remaining;
 
           rows_remaining=image->rows-y;
-          if ((long) (y+rows) < (long) image->rows)
+          if ((ssize_t) (y+rows) < (ssize_t) image->rows)
             rows_remaining=rows;
           tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
             exception);
           if (tile == (PixelPacket *) NULL)
             break;
-          for (x=0; x < (long) image->columns; x+=columns)
+          for (x=0; x < (ssize_t) image->columns; x+=columns)
           {
-            unsigned long
+            size_t
               column,
               row;
 
             if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
               break;
             columns_remaining=image->columns-x;
-            if ((long) (x+columns) < (long) image->columns)
+            if ((ssize_t) (x+columns) < (ssize_t) image->columns)
               columns_remaining=columns;
             p=tile_pixels+(rows-rows_remaining)*columns;
             q=tile+(image->columns*(rows_remaining-1)+x);
@@ -1486,7 +1511,8 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
             break;
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,LoadImageTag,y,image->rows);
+              status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
@@ -1526,9 +1552,9 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
           Convert image to DirectClass pixel packets.
         */
         p=pixels+number_pixels-1;
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
-          register long
+          register ssize_t
             x;
 
           register PixelPacket
@@ -1538,13 +1564,14 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
           if (q == (PixelPacket *) NULL)
             break;
           q+=image->columns-1;
-          for (x=0; x < (long) image->columns; x++)
+          for (x=0; x < (ssize_t) image->columns; x++)
           {
             q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
             q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
             q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
             if (image->matte != MagickFalse)
-              q->opacity=ScaleCharToQuantum((unsigned char) TIFFGetA(*p));
+              q->opacity=(Quantum) (QuantumRange-
+                ScaleCharToQuantum((unsigned char) TIFFGetA(*p)));
             p--;
             q--;
           }
@@ -1552,7 +1579,8 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
             break;
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,LoadImageTag,y,image->rows);
+              status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
@@ -1637,10 +1665,10 @@ static Image *ReadTIFFImage(const ImageInfo *image_info,
 %
 %  The format of the RegisterTIFFImage method is:
 %
-%      unsigned long RegisterTIFFImage(void)
+%      size_t RegisterTIFFImage(void)
 %
 */
-ModuleExport unsigned long RegisterTIFFImage(void)
+ModuleExport size_t RegisterTIFFImage(void)
 {
 #define TIFFDescription  "Tagged Image File Format"
 
@@ -1669,7 +1697,7 @@ ModuleExport unsigned long RegisterTIFFImage(void)
     const char
       *p;
 
-    register long int
+    register ssize_t
       i;
 
     p=TIFFGetVersion();
@@ -1687,6 +1715,7 @@ ModuleExport unsigned long RegisterTIFFImage(void)
   entry->raw=MagickTrue;
   entry->endian_support=MagickTrue;
   entry->adjoin=MagickFalse;
+  entry->format_type=ImplicitFormatType;
   entry->seekable_stream=MagickTrue;
   entry->thread_support=NoThreadSupport;
   entry->description=ConstantString("Raw CCITT Group4");
@@ -1832,7 +1861,7 @@ static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
   MagickBooleanType
     status;
 
-  register long
+  register ssize_t
     i;
 
   ssize_t
@@ -1918,7 +1947,7 @@ static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
       return(MagickFalse);
     }
   strip_size=byte_count[0];
-  for (i=1; i < (long) TIFFNumberOfStrips(tiff); i++)
+  for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
     if (byte_count[i] > strip_size)
       strip_size=byte_count[i];
   buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
@@ -1935,7 +1964,7 @@ static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
   /*
     Compress runlength encoded to 2D Huffman pixels.
   */
-  for (i=0; i < (long) TIFFNumberOfStrips(tiff); i++)
+  for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
   {
     count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
     if (WriteBlob(image,(size_t) count,buffer) != count)
@@ -1992,7 +2021,7 @@ static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
   MagickBooleanType
     status;
 
-  unsigned long
+  size_t
     columns,
     rows;
 
@@ -2085,6 +2114,10 @@ static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
   MagickStatusType
     flags;
 
+  uint32
+    tile_columns,
+    tile_rows;
+
   assert(tiff_info != (TIFFInfo *) NULL);
   (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
   option=GetImageOption(image_info,"tiff:tile-geometry");
@@ -2093,15 +2126,17 @@ static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
   flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
   if ((flags & HeightValue) == 0)
     tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
-  (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,(uint32)
-    tiff_info->tile_geometry.width);
-  (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,(uint32)
-    tiff_info->tile_geometry.height);
+  tile_columns=(uint32) tiff_info->tile_geometry.width;
+  tile_rows=(uint32) tiff_info->tile_geometry.height;
+  TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
+  (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
+  (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
+  tiff_info->tile_geometry.width=tile_columns;
+  tiff_info->tile_geometry.height=tile_rows;
   tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
-    tiff_info->tile_geometry.height*TIFFScanlineSize(tiff),
-    sizeof(*tiff_info->scanlines));
+    tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
   tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
-    TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
+    tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
   if ((tiff_info->scanlines == (unsigned char *) NULL) ||
       (tiff_info->pixels == (unsigned char *) NULL))
     {
@@ -2111,26 +2146,26 @@ static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
   return(MagickTrue);
 }
 
-static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,long row,
+static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
   tsample_t sample,Image *image)
 {
   int32
     status;
 
-  long
+  ssize_t
     bytes_per_pixel,
     j,
     k,
     l;
 
-  register long
+  register ssize_t
     i;
 
   register unsigned char
     *p,
     *q;
 
-  unsigned long
+  size_t
     number_tiles,
     tile_width;
 
@@ -2139,26 +2174,26 @@ static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,long row,
   /*
     Fill scanlines to tile height.
   */
-  i=(long) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
+  i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
   (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
     (size_t) TIFFScanlineSize(tiff));
-  if (((unsigned long) (row % tiff_info->tile_geometry.height) !=
-      (tiff_info->tile_geometry.height-1)) && (row != (long) (image->rows-1)))
+  if (((size_t) (row % tiff_info->tile_geometry.height) !=
+      (tiff_info->tile_geometry.height-1)) && (row != (ssize_t) (image->rows-1)))
     return(0);
   /*
     Write tile to TIFF image.
   */
   status=0;
-  bytes_per_pixel=TIFFTileSize(tiff)/(long) (tiff_info->tile_geometry.height*
+  bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (tiff_info->tile_geometry.height*
     tiff_info->tile_geometry.width);
   number_tiles=(image->columns+tiff_info->tile_geometry.width)/
     tiff_info->tile_geometry.width;
-  for (i=0; i < (long) number_tiles; i++)
+  for (i=0; i < (ssize_t) number_tiles; i++)
   {
-    tile_width=(i == (long) (number_tiles-1)) ? image->columns-(i*
+    tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
       tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
-    for (j=0; j < (long) ((row % tiff_info->tile_geometry.height)+1); j++)
-      for (k=0; k < (long) tile_width; k++)
+    for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
+      for (k=0; k < (ssize_t) tile_width; k++)
       {
         if (bytes_per_pixel == 0)
           {
@@ -2174,10 +2209,11 @@ static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,long row,
         for (l=0; l < bytes_per_pixel; l++)
           *q++=(*p++);
       }
-    status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
-      tiff_info->tile_geometry.width),(uint32) ((row/
-      tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
-      sample);
+    if ((i*tiff_info->tile_geometry.width) != image->columns)
+      status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
+        tiff_info->tile_geometry.width),(uint32) ((row/
+        tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
+        sample);
     if (status < 0)
       break;
   }
@@ -2244,6 +2280,7 @@ static void TIFFSetProperties(TIFF *tiff,Image *image)
   const char
     *value;
 
+  (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
   value=GetImageProperty(image,"tiff:hostcomputer");
   if (value != (const char *) NULL)
     (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
@@ -2259,9 +2296,9 @@ static void TIFFSetProperties(TIFF *tiff,Image *image)
   value=GetImageProperty(image,"tiff:model");
   if (value != (const char *) NULL)
     (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
-  (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,
-    GetMagickVersion((unsigned long *) NULL));
-  (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
+  value=GetImageProperty(image,"tiff:software");
+  if (value != (const char *) NULL)
+    (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
   value=GetImageProperty(image,"tiff:copyright");
   if (value != (const char *) NULL)
     (void) TIFFSetField(tiff,33432,value);
@@ -2285,7 +2322,7 @@ static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
   const char
     *value;
 
-  register long
+  register ssize_t
     i;
 
   uint32
@@ -2320,10 +2357,10 @@ static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
       case TIFF_LONG:
       {
         uint16
-          longy;
+          ssize_ty;
 
-        longy=(uint16) StringToLong(value);
-        (void) TIFFSetField(tiff,exif_info[i].tag,longy);
+        ssize_ty=(uint16) StringToLong(value);
+        (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
         break;
       }
       case TIFF_RATIONAL:
@@ -2358,13 +2395,13 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
     *mode,
     *option;
 
-  const char
-    *value;
-
   CompressionType
     compression;
 
-  long
+  EndianType
+    endian_type;
+
+  ssize_t
     y;
 
   MagickBooleanType
@@ -2380,7 +2417,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
   QuantumType
     quantum_type;
 
-  register long
+  register ssize_t
     i;
 
   size_t
@@ -2408,7 +2445,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
   unsigned char
     *pixels;
 
-  unsigned long
+  size_t
     lsb_first;
 
   /*
@@ -2426,7 +2463,16 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
   (void) MagickSetThreadValue(tiff_exception,&image->exception);
   error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
   warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
-  switch (image_info->endian)
+  endian_type=UndefinedEndian;
+  option=GetImageOption(image_info,"tiff:endian");
+  if (option != (const char *) NULL)
+    {
+      if (LocaleNCompare(option,"msb",3) == 0)
+        endian_type=MSBEndian;
+      if (LocaleNCompare(option,"lsb",3) == 0)
+        endian_type=LSBEndian;;
+    }
+  switch (endian_type)
   {
     case LSBEndian: mode="wl"; break;
     case MSBEndian: mode="wb"; break;
@@ -2434,7 +2480,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
   }
 #if defined(TIFF_VERSION_BIG)
   if (LocaleCompare(image_info->magick,"TIFF64") == 0)
-    switch (image_info->endian)
+    switch (endian_type)
     {
       case LSBEndian: mode="wl8"; break;
       case MSBEndian: mode="wb8"; break;
@@ -2529,7 +2575,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
       {
         (void) ThrowMagickException(&image->exception,GetMagickModule(),
           CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
-          MagickCompressOptions,(long) compression));
+          MagickCompressOptions,(ssize_t) compression));
         compress_tag=COMPRESSION_NONE;
         compression=NoCompression;
       }
@@ -2558,7 +2604,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
         {
           (void) ThrowMagickException(&image->exception,GetMagickModule(),
             CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
-              MagickCompressOptions,(long) compression));
+              MagickCompressOptions,(ssize_t) compression));
           compress_tag=COMPRESSION_NONE;
           compression=NoCompression;
           break;
@@ -2600,6 +2646,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
             {
               photometric=PHOTOMETRIC_YCBCR;
               (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
+              (void) SetImageStorageClass(image,DirectClass);
               (void) SetImageDepth(image,8);
             }
           else
@@ -2619,7 +2666,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
                   MagickFalse ? PHOTOMETRIC_MINISWHITE :
                   PHOTOMETRIC_MINISBLACK);
                 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
-                if ((image_info->depth == 0) &&
+                if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
                     (IsMonochromeImage(image,&image->exception) != MagickFalse))
                   {
                     status=SetQuantumDepth(image,quantum_info,1);
@@ -2631,7 +2678,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
             else
               if (image->storage_class == PseudoClass)
                 {
-                  unsigned long
+                  size_t
                     depth;
 
                   /*
@@ -2672,22 +2719,27 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
     image->endian=MSBEndian;
     if ((int) (*(char *) &lsb_first) != 0)
       image->endian=LSBEndian;
-    if ((compress_tag == COMPRESSION_JPEG) && (photometric != PHOTOMETRIC_RGB))
-      compress_tag=COMPRESSION_NONE;
+    if ((compress_tag == COMPRESSION_CCITTFAX3) &&
+        (photometric != PHOTOMETRIC_MINISWHITE))
+      {
+        compress_tag=COMPRESSION_NONE;
+        endian=FILLORDER_MSB2LSB;
+      }
     else
-      if ((compress_tag == COMPRESSION_CCITTFAX3) &&
-          (photometric != PHOTOMETRIC_MINISWHITE))
-        {
-          compress_tag=COMPRESSION_NONE;
+      if ((compress_tag == COMPRESSION_CCITTFAX4) &&
+         (photometric != PHOTOMETRIC_MINISWHITE))
+       {
+         compress_tag=COMPRESSION_NONE;
+         endian=FILLORDER_MSB2LSB;
+       }
+    option=GetImageProperty(image,"tiff:fill-order");
+    if (option != (const char *) NULL)
+      {
+        if (LocaleNCompare(option,"msb",3) == 0)
           endian=FILLORDER_MSB2LSB;
-        }
-      else
-        if ((compress_tag == COMPRESSION_CCITTFAX4) &&
-           (photometric != PHOTOMETRIC_MINISWHITE))
-         {
-           compress_tag=COMPRESSION_NONE;
-           endian=FILLORDER_MSB2LSB;
-         }
+        if (LocaleNCompare(option,"lsb",3) == 0)
+          endian=FILLORDER_LSB2MSB;
+      }
     (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
     (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
     (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
@@ -2746,11 +2798,11 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
         (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
     rows_per_strip=1;
     if (TIFFScanlineSize(tiff) != 0)
-      rows_per_strip=(unsigned long) MagickMax((size_t)
-        TIFFDefaultStripSize(tiff,-1),1);
+      rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
+        1);
     option=GetImageOption(image_info,"tiff:rows-per-strip");
     if (option != (const char *) NULL)
-      rows_per_strip=(unsigned long) strtol(option,(char **) NULL,10);
+      rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
     switch (compress_tag)
     {
       case COMPRESSION_JPEG:
@@ -2773,6 +2825,9 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
         (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
         if (image->colorspace == RGBColorspace)
           {
+            const char
+              *value;
+
             (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
             sampling_factor=(const char *) NULL;
             value=GetImageProperty(image,"jpeg:sampling-factor");
@@ -2790,8 +2845,9 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
                 flags=ParseGeometry(sampling_factor,&geometry_info);
                 if ((flags & SigmaValue) == 0)
                   geometry_info.sigma=geometry_info.rho;
-                (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
-                  geometry_info.rho,(uint16) geometry_info.sigma);
+                if (image->colorspace == YCbCrColorspace)
+                  (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
+                    geometry_info.rho,(uint16) geometry_info.sigma);
               }
           }
         if (bits_per_sample == 12)
@@ -2801,16 +2857,16 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
       }
       case COMPRESSION_ADOBE_DEFLATE:
       {
-        rows_per_strip=image->rows;
+        rows_per_strip=(uint32) image->rows;
         (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
           &bits_per_sample);
         if (((photometric == PHOTOMETRIC_RGB) ||
              (photometric == PHOTOMETRIC_MINISBLACK)) &&
             ((bits_per_sample == 8) || (bits_per_sample == 16)))
           (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,2);
-        (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,image_info->quality ==
-          UndefinedCompressionQuality ? 7 : MagickMin(1L*image_info->quality/10,
-          9));
+        (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
+          image_info->quality == UndefinedCompressionQuality ? 7 :
+          MagickMin((ssize_t) image_info->quality/10,9)));
         break;
       }
       case COMPRESSION_CCITTFAX3:
@@ -2818,13 +2874,13 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
         /*
           Byte-aligned EOL.
         */
-        rows_per_strip=image->rows;
+        rows_per_strip=(uint32) image->rows;
         (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
         break;
       }
       case COMPRESSION_CCITTFAX4:
       {
-        rows_per_strip=image->rows;
+        rows_per_strip=(uint32) image->rows;
         break;
       }
       case COMPRESSION_LZW:
@@ -2840,7 +2896,9 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
       default:
         break;
     }
-    (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
+    option=GetImageOption(image_info,"tiff:tile-geometry");
+    if (option == (const char *) NULL)
+      (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
     if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
       {
         unsigned short
@@ -2903,7 +2961,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
         pages;
 
       page=(uint16) scene;
-      pages=GetImageListLength(image);
+      pages=(uint16) GetImageListLength(image);
       if ((image_info->adjoin != MagickFalse) && (pages > 1))
         (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
       (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
@@ -2935,7 +2993,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
             quantum_type=RGBQuantum;
             if (image->matte != MagickFalse)
               quantum_type=RGBAQuantum;
-            for (y=0; y < (long) image->rows; y++)
+            for (y=0; y < (ssize_t) image->rows; y++)
             {
               register const PixelPacket
                 *restrict p;
@@ -2950,7 +3008,8 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
                 break;
               if (image->previous == (Image *) NULL)
                 {
-                  status=SetImageProgress(image,SaveImageTag,y,image->rows);
+                  status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
+                    y,image->rows);
                   if (status == MagickFalse)
                     break;
                 }
@@ -2963,7 +3022,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
             /*
               Plane interlacing:  RRRRRR...GGGGGG...BBBBBB...
             */
-            for (y=0; y < (long) image->rows; y++)
+            for (y=0; y < (ssize_t) image->rows; y++)
             {
               register const PixelPacket
                 *restrict p;
@@ -2983,7 +3042,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
                 if (status == MagickFalse)
                   break;
               }
-            for (y=0; y < (long) image->rows; y++)
+            for (y=0; y < (ssize_t) image->rows; y++)
             {
               register const PixelPacket
                 *restrict p;
@@ -3003,7 +3062,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
                 if (status == MagickFalse)
                   break;
               }
-            for (y=0; y < (long) image->rows; y++)
+            for (y=0; y < (ssize_t) image->rows; y++)
             {
               register const PixelPacket
                 *restrict p;
@@ -3024,7 +3083,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
                   break;
               }
             if (image->matte != MagickFalse)
-              for (y=0; y < (long) image->rows; y++)
+              for (y=0; y < (ssize_t) image->rows; y++)
               {
                 register const PixelPacket
                   *restrict p;
@@ -3059,7 +3118,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
           quantum_type=CMYKAQuantum;
         if (image->colorspace != CMYKColorspace)
           (void) TransformImageColorspace(image,CMYKColorspace);
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
           register const PixelPacket
             *restrict p;
@@ -3073,7 +3132,8 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
             break;
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,SaveImageTag,y,image->rows);
+              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }
@@ -3102,7 +3162,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
         (void) ResetMagickMemory(red,0,65536*sizeof(*red));
         (void) ResetMagickMemory(green,0,65536*sizeof(*green));
         (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
-        for (i=0; i < (long) image->colors; i++)
+        for (i=0; i < (ssize_t) image->colors; i++)
         {
           red[i]=ScaleQuantumToShort(image->colormap[i].red);
           green[i]=ScaleQuantumToShort(image->colormap[i].green);
@@ -3129,7 +3189,7 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
          else
            if (photometric != PHOTOMETRIC_PALETTE)
              quantum_type=GrayQuantum;
-        for (y=0; y < (long) image->rows; y++)
+        for (y=0; y < (ssize_t) image->rows; y++)
         {
           register const PixelPacket
             *restrict p;
@@ -3143,7 +3203,8 @@ static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
             break;
           if (image->previous == (Image *) NULL)
             {
-              status=SetImageProgress(image,SaveImageTag,y,image->rows);
+              status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+                image->rows);
               if (status == MagickFalse)
                 break;
             }