libtiff: * tools/tiffcrop.c: Patch from Richard Nolde. Reject YCbCr

https://github.com/libsdl-org/libtiff/commit/c25146d07b4ca0fe4adcaf81ffb4d881cb67497f

From c25146d07b4ca0fe4adcaf81ffb4d881cb67497f Mon Sep 17 00:00:00 2001
From: Bob Friesenhahn <[EMAIL REDACTED]>
Date: Wed, 16 Jun 2010 01:54:25 +0000
Subject: [PATCH] * tools/tiffcrop.c: Patch from Richard Nolde. Reject YCbCr
 subsampled data since tiffcrop currently doesn't support it.  Fix JPEG
 support.

---
 ChangeLog        |   6 ++
 tools/tiffcrop.c | 248 ++++++++++++++++++++++++-----------------------
 2 files changed, 133 insertions(+), 121 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 2411f2c9..c5510b7a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2010-06-15  Bob Friesenhahn  <bfriesen@simple.dallas.tx.us>
+
+	* tools/tiffcrop.c: Patch from Richard Nolde. Reject YCbCr
+	subsampled data since tiffcrop currently doesn't support it.  Fix
+	JPEG support.
+
 2010-06-15  Bob Friesenhahn  <bfriesen@simple.dallas.tx.us>
 
 	* libtiff 3.9.4 released.
diff --git a/tools/tiffcrop.c b/tools/tiffcrop.c
index fa057c56..69e403d3 100644
--- a/tools/tiffcrop.c
+++ b/tools/tiffcrop.c
@@ -1,4 +1,4 @@
-/* $Id: tiffcrop.c,v 1.3.2.12 2010-06-11 22:24:23 bfriesen Exp $ */
+/* $Id: tiffcrop.c,v 1.3.2.13 2010-06-16 01:54:25 bfriesen Exp $ */
 
 /* tiffcrop.c -- a port of tiffcp.c extended to include manipulations of
  * the image data through additional options listed below
@@ -107,8 +107,8 @@
  *                of messages to monitor progess without enabling dump logs.
  */
 
-static   char tiffcrop_version_id[] = "2.2";
-static   char tiffcrop_rev_date[] = "11-03-2009";
+static   char tiffcrop_version_id[] = "2.3";
+static   char tiffcrop_rev_date[] = "06-15-2010";
 
 #include "tif_config.h"
 #include "tiffiop.h"
@@ -375,10 +375,10 @@ struct paperdef PaperTable[MAX_PAPERNAMES] = {
   {"c4",              9.016,  12.756,  0.707},
   {"c5",              6.378,   9.016,  0.707},
   {"c6",              4.488,   6.378,  0.704},
-  {"",                0.000,   0.000,  1.000},
+  {"",                0.000,   0.000,  1.000}
 };
 
-/* Structure to define in input image parameters */
+/* Structure to define input image parameters */
 struct image_data {
   float  xres;
   float  yres;
@@ -390,6 +390,7 @@ struct image_data {
   uint16 planar;
   uint16 photometric;
   uint16 orientation;
+  uint16 compression;
   uint16 adjustments;
 };
 
@@ -434,7 +435,8 @@ static uint32 g3opts = 0;
 static int    ignore = FALSE;		/* if true, ignore read errors */
 static uint32 defg3opts = (uint32) -1;
 static int    quality = 100;		/* JPEG quality */
-static int    jpegcolormode = -1;       /* was JPEGCOLORMODE_RGB; */
+/* static int    jpegcolormode = -1;        was JPEGCOLORMODE_RGB;  */
+static int    jpegcolormode = JPEGCOLORMODE_RGB;
 static uint16 defcompression = (uint16) -1;
 static uint16 defpredictor = (uint16) -1;
 static int    pageNum = 0;
@@ -608,7 +610,7 @@ static int  dump_buffer (FILE *, int, uint32, uint32, uint32, unsigned char *);
 /* Functions derived in whole or in part from tiffcp */
 /* The following functions are taken largely intact from tiffcp */
 
-static   char* stuff[] = {
+static   char* usage_info[] = {
 "usage: tiffcrop [options] source1 ... sourceN  destination",
 "where options are:",
 " -h		Print this syntax listing",
@@ -629,13 +631,13 @@ static   char* stuff[] = {
 " -f lsb2msb	Force lsb-to-msb FillOrder for output",
 " -f msb2lsb	Force msb-to-lsb FillOrder for output",
 "",
-" -c lzw[:opts]	Compress output with Lempel-Ziv & Welch encoding",
-" -c zip[:opts]	Compress output with deflate encoding",
-" -c jpeg[:opts]	compress output with JPEG encoding",
-" -c packbits	Compress output with packbits encoding",
-" -c g3[:opts]	Compress output with CCITT Group 3 encoding",
-" -c g4		Compress output with CCITT Group 4 encoding",
-" -c none	Use no compression algorithm on output",
+" -c lzw[:opts]	 Compress output with Lempel-Ziv & Welch encoding",
+" -c zip[:opts]	 Compress output with deflate encoding",
+" -c jpeg[:opts] Compress output with JPEG encoding",
+" -c packbits	 Compress output with packbits encoding",
+" -c g3[:opts]	 Compress output with CCITT Group 3 encoding",
+" -c g4		 Compress output with CCITT Group 4 encoding",
+" -c none	 Use no compression algorithm on output",
 " ",
 "Group 3 options:",
 " 1d		Use default CCITT Group 3 1D-encoding",
@@ -645,9 +647,9 @@ static   char* stuff[] = {
 " ",
 "JPEG options:",
 " #		Set compression quality level (0-100, default 100)",
-" r		Output color image as raw RGB rather than YCbCr",
-" a		Output color image as RGB or YCbCr with auto detection",
-"For example, -c jpeg:r:50 to get JPEG-encoded RGB data with 50% comp. quality",
+" raw		Output color image as raw YCbCr",
+" rgb		Output color image as RGB",
+"For example, -c jpeg:rgb:50 to get JPEG-encoded RGB data with 50% comp. quality",
 " ",
 "LZW and deflate options:",
 " #		Set predictor value",
@@ -1345,16 +1347,6 @@ processCompressOptions(char* opt)
   if (strneq(opt, "none",4))
     {
     defcompression = COMPRESSION_NONE;
-    /* DELETE ME:  This should not be needed */
-    cp = strchr(opt, ':');
-    if (cp)
-      {
-      if (cp[1] == 'r' )
-	jpegcolormode = JPEGCOLORMODE_RAW;
-      else if (cp[1] == 'a' )
-	jpegcolormode = JPEGCOLORMODE_RGB;
-      }
-    /* end DELETE ME: */
     }
   else if (streq(opt, "packbits"))
     {
@@ -1364,17 +1356,18 @@ processCompressOptions(char* opt)
     {
     cp = strchr(opt, ':');
     defcompression = COMPRESSION_JPEG;
-    while ( cp )
+
+    while (cp)
       {
       if (isdigit((int)cp[1]))
-	quality = atoi(cp+1);
-      else if (cp[1] == 'r' )
+	quality = atoi(cp + 1);
+      else if (strneq(cp + 1, "raw", 3 ))
 	jpegcolormode = JPEGCOLORMODE_RAW;
-      else if (cp[1] == 'a' )
+      else if (strneq(cp + 1, "rgb", 3 ))
 	jpegcolormode = JPEGCOLORMODE_RGB;
       else
-        usage();
-      cp = strchr(cp+1,':');
+	usage();
+      cp = strchr(cp + 1, ':');
       }
     }
   else if (strneq(opt, "g3", 2))
@@ -1409,13 +1402,11 @@ processCompressOptions(char* opt)
 static void
 usage(void)
   {
-  char buf[BUFSIZ];
   int i;
 
-  setbuf(stderr, buf);
   fprintf(stderr, "\n%s\n", TIFFGetVersion());
-  for (i = 0; stuff[i] != NULL; i++)
-    fprintf(stderr, "%s\n", stuff[i]);
+  for (i = 0; usage_info[i] != NULL; i++)
+    fprintf(stderr, "%s\n", usage_info[i]);
   exit(-1);
   }
 
@@ -1633,7 +1624,7 @@ void  process_command_opts (int argc, char *argv[], char *mp, char *mode, uint32
 			   tiffcrop_version_id, tiffcrop_rev_date);
  	        TIFFError ("Tiffcp code", "Copyright (c) 1988-1997 Sam Leffler");
 		TIFFError ("           ", "Copyright (c) 1991-1997 Silicon Graphics, Inc");
-                TIFFError ("Tiffcrop additions", "Copyright (c) 2007-2009 Richard Nolde");
+                TIFFError ("Tiffcrop additions", "Copyright (c) 2007-2010 Richard Nolde");
 	        exit (0);
 		break;
       case 'w':	/* tile width */
@@ -4903,6 +4894,7 @@ initImageData (struct image_data *image)
   image->planar = 0;
   image->photometric = 0;
   image->orientation = 0;
+  image->compression = COMPRESSION_NONE;
   image->adjustments = 0;
   }
 
@@ -5683,7 +5675,9 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
   float    xres = 0.0, yres = 0.0;
   uint16   nstrips = 0, ntiles = 0, planar = 0;
   uint16   bps = 0, spp = 0, res_unit = 0;
-  uint16   photometric = 0, orientation = 0, input_compression = 0;
+  uint16   orientation = 0;
+  uint16   input_compression = 0, input_photometric = 0;
+  uint16   subsampling_horiz, subsampling_vert;
   uint32   width = 0, length = 0;
   uint32   stsize = 0, tlsize = 0, buffsize = 0, scanlinesize = 0;
   uint32   tw = 0, tl = 0;       /* Tile width and length */
@@ -5697,7 +5691,7 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
   TIFFGetFieldDefaulted(in, TIFFTAG_SAMPLESPERPIXEL, &spp);
   TIFFGetFieldDefaulted(in, TIFFTAG_PLANARCONFIG, &planar);
   TIFFGetFieldDefaulted(in, TIFFTAG_ORIENTATION, &orientation);
-  if (! TIFFGetFieldDefaulted(in, TIFFTAG_PHOTOMETRIC, &photometric))
+  if (! TIFFGetFieldDefaulted(in, TIFFTAG_PHOTOMETRIC, &input_photometric))
     TIFFError("loadImage","Image lacks Photometric interpreation tag");
   if (! TIFFGetField(in, TIFFTAG_IMAGEWIDTH,  &width))
     TIFFError("loadimage","Image lacks image width tag");
@@ -5713,72 +5707,72 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
 #ifdef DEBUG2
   char compressionid[16];
 
-  switch (compression)
+  switch (input_compression)
     {
     case COMPRESSION_NONE:	/* 1  dump mode */
-         stcrcpy ("None/dump", compressionid);
+	 strcpy (compressionid, "None/dump");
          break;         
     case COMPRESSION_CCITTRLE:	  /* 2 CCITT modified Huffman RLE */
-         stcrcpy ("Huffman RLE", compressionid);
+	 strcpy (compressionid, "Huffman RLE");
          break;         
     case COMPRESSION_CCITTFAX3:	  /* 3 CCITT Group 3 fax encoding */
-    case COMPRESSION_CCITT_T4:    /* 3 CCITT T.4 (TIFF 6 name) */
-         stcrcpy ("Group3 Fax", compressionid);
+	 strcpy (compressionid, "Group3 Fax");
          break;         
     case COMPRESSION_CCITTFAX4:	  /* 4 CCITT Group 4 fax encoding */
-    case COMPRESSION_CCITT_T6:    /* 4 CCITT T.6 (TIFF 6 name) */
-         stcrcpy ("Group4 Fax", compressionid);
+	 strcpy (compressionid, "Group4 Fax");
          break;         
     case COMPRESSION_LZW:	  /* 5 Lempel-Ziv  & Welch */
-         stcrcpy ("LZW", compressionid);
+	 strcpy (compressionid, "LZW");
          break;         
     case COMPRESSION_OJPEG:	  /* 6 !6.0 JPEG */
-         stcrcpy ("Old Jpeg", compressionid);
+	 strcpy (compressionid, "Old Jpeg");
          break;         
     case COMPRESSION_JPEG:	  /* 7 %JPEG DCT compression */
-         stcrcpy ("New Jpeg", compressionid);
+	 strcpy (compressionid, "New Jpeg");
          break;         
     case COMPRESSION_NEXT:	  /* 32766 NeXT 2-bit RLE */
-         stcrcpy ("Next RLE", compressionid);
+	 strcpy (compressionid, "Next RLE");
          break;         
     case COMPRESSION_CCITTRLEW:   /* 32771 #1 w/ word alignment */
-         stcrcpy ("CITTRLEW", compressionid);
+	 strcpy (compressionid, "CITTRLEW");
          break;         
     case COMPRESSION_PACKBITS:	  /* 32773 Macintosh RLE */
-         stcrcpy ("Mac Packbits", compressionid);
+	 strcpy (compressionid, "Mac Packbits");
          break;         
     case COMPRESSION_THUNDERSCAN: /* 32809 ThunderScan RLE */
-         stcrcpy ("Thunderscan", compressionid);
+	 strcpy (compressionid, "Thunderscan");
          break;         
     case COMPRESSION_IT8CTPAD:	  /* 32895 IT8 CT w/padding */
-         stcrcpy ("IT8 padded", compressionid);
+	 strcpy (compressionid, "IT8 padded");
          break;         
     case COMPRESSION_IT8LW:	  /* 32896 IT8 Linework RLE */
-         stcrcpy ("IT8 RLE", compressionid);
+	 strcpy (compressionid, "IT8 RLE");
          break;         
     case COMPRESSION_IT8MP:	  /* 32897 IT8 Monochrome picture */
-         stcrcpy ("IT8 mono", compressionid);
+	 strcpy (compressionid, "IT8 mono");
          break;         
     case COMPRESSION_IT8BL:	  /* 32898 IT8 Binary line art */
-         stcrcpy ("IT8 lineart", compressionid);
+	 strcpy (compressionid, "IT8 lineart");
          break;         
     case COMPRESSION_PIXARFILM:	  /* 32908 Pixar companded 10bit LZW */
-         stcrcpy ("Pixar 10 bit", compressionid);
+	 strcpy (compressionid, "Pixar 10 bit");
          break;         
     case COMPRESSION_PIXARLOG:	  /* 32909 Pixar companded 11bit ZIP */
-         stcrcpy ("Pixar 11bit", compressionid);
+	 strcpy (compressionid, "Pixar 11bit");
          break;         
     case COMPRESSION_DEFLATE:	  /* 32946 Deflate compression */
-         stcrcpy ("Deflate", compressionid);
+	 strcpy (compressionid, "Deflate");
          break;         
     case COMPRESSION_ADOBE_DEFLATE: /* 8 Deflate compression */
-         stcrcpy ("Adobe deflate", compressionid);
+	 strcpy (compressionid, "Adobe deflate");
          break;         
     default:
-         stcrcpy ("None/unknown", compressionid);
+	 strcpy (compressionid, "None/unknown");
          break;         
     }
+  TIFFError("loadImage", "Input compression %s", compressionid);
 #endif
+
   scanlinesize = TIFFScanlineSize(in);
   image->bps = bps;
   image->spp = spp;
@@ -5788,12 +5782,12 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
   image->xres = xres;
   image->yres = yres;
   image->res_unit = res_unit;
-  image->photometric = photometric;
-
+  image->compression = input_compression;
+  image->photometric = input_photometric;
 #ifdef DEBUG2
-  char photmetricid[12];
+  char photometricid[12];
 
-  switch (photometric)
+  switch (input_photometric)
     {
     case PHOTOMETRIC_MINISWHITE:
          strcpy (photometricid, "MinIsWhite");
@@ -5836,8 +5830,8 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
          break;
     }
   TIFFError("loadImage", "Input photometric interpretation %s", photometricid);
-#endif
 
+#endif
   image->orientation = orientation;
   switch (orientation)
     {
@@ -5888,7 +5882,8 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
 
     tile_rowsize  = TIFFTileRowSize(in);      
     buffsize = tlsize * ntiles;
-    
+
+        
     if (buffsize < (uint32)(ntiles * tl * tile_rowsize))
       {
       buffsize = ntiles * tl * tile_rowsize;
@@ -5911,6 +5906,7 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
     stsize = TIFFStripSize(in);
     nstrips = TIFFNumberOfStrips(in);
     buffsize = stsize * nstrips;
+    
     if (buffsize < (uint32) (((length * width * spp * bps) + 7) / 8))
       {
       buffsize =  ((length * width * spp * bps) + 7) / 8;
@@ -5920,7 +5916,7 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
                 stsize, (unsigned long)buffsize);
 #endif
       }
-
+    
     if (dump->infile != NULL)
       dump_info (dump->infile, dump->format, "",
                  "Stripsize: %u, Number of Strips: %u, Rows per Strip: %u, Scanline size: %u",
@@ -5928,10 +5924,27 @@ loadImage(TIFF* in, struct image_data *image, struct dump_opts *dump, unsigned c
     }
   
   if (input_compression == COMPRESSION_JPEG)
-    {
+    {  /* Force conversion to RGB */
     jpegcolormode = JPEGCOLORMODE_RGB;
     TIFFSetField(in, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
     }
+  /* The clause up to the read statement are taken from Tom Lane's tiffcp patch */
+  else 
+    {   /* Otherwise, can't handle subsampled input */
+    if (input_photometric == PHOTOMETRIC_YCBCR)
+      {
+      TIFFGetFieldDefaulted(in, TIFFTAG_YCBCRSUBSAMPLING,
+ 		           &subsampling_horiz, &subsampling_vert);
+      if (subsampling_horiz != 1 || subsampling_vert != 1)
+        {
+	TIFFError("loadImage", 
+		"Can't copy/convert subsampled image with subsampling %d horiz %d vert.\n",
+                subsampling_horiz, subsampling_vert);
+        return (-1);
+        }
+	}
+    }
+ 
 
   read_buff = *read_ptr;
   if (!read_buff)
@@ -6836,21 +6849,27 @@ writeSingleSection(TIFF *in, TIFF *out, struct image_data *image,
   {
   uint16 bps, spp;
   uint16 input_compression, input_photometric;
-  uint16 input_jpeg_colormode, input_planar;
+  uint16 input_planar;
   struct cpTag* p;
 
-  TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &input_photometric);
-  TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &spp);
-  TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &bps);
+  /*  Calling this seems to reset the compression mode on the TIFF *in file.
+  TIFFGetField(in, TIFFTAG_JPEGCOLORMODE, &input_jpeg_colormode);
+  */
+  input_compression = image->compression;
+  input_photometric = image->photometric;
 
+  spp = image->spp;
+  bps = image->bps;
   TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
   TIFFSetField(out, TIFFTAG_IMAGELENGTH, length);
+  TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bps);
+  TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
 
-  CopyField(TIFFTAG_BITSPERSAMPLE, bps);
-  CopyField(TIFFTAG_SAMPLESPERPIXEL, spp);
-
-
-  TIFFGetField(in, TIFFTAG_COMPRESSION, &input_compression);
+#ifdef DEBUG2
+  TIFFError("writeSingleSection", "Input compression: %s",
+	    (input_compression == COMPRESSION_OJPEG) ? "Old Jpeg" :
+	    ((input_compression == COMPRESSION_JPEG) ?  "New Jpeg" : "Non Jpeg"));
+#endif
   /* This is the global variable compression which is set 
    * if the user has specified a command line option for 
    * a compression option.  Should be passed around in one
@@ -6862,23 +6881,18 @@ writeSingleSection(TIFF *in, TIFF *out, struct image_data *image,
     { /* OJPEG is no longer supported for writing so upgrade to JPEG */
     if (input_compression == COMPRESSION_OJPEG)
       {
-      TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_JPEG);
       compression = COMPRESSION_JPEG;
+      jpegcolormode = JPEGCOLORMODE_RAW;
+      TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_JPEG);
       }
     else /* Use the compression from the input file */
-      CopyField(TIFFTAG_COMPRESSION, compression);
+      TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
     }
 
-  TIFFGetField(in, TIFFTAG_JPEGCOLORMODE, &input_jpeg_colormode);
-#ifdef DEBUG2
-  TIFFError("writeSingleSection", "Input compression: %s",
-	    (input_compression == COMPRESSION_OJPEG) ? "Old Jpeg" :
-	    ((input_compression == COMPRESSION_JPEG) ?  "New Jpeg" : "Non Jpeg"));
-#endif
   if (compression == COMPRESSION_JPEG)
     {
     if ((input_photometric == PHOTOMETRIC_PALETTE) ||  /* color map indexed */
-        (input_photometric == PHOTOMETRIC_MASK))       /* $holdout mask */
+        (input_photometric == PHOTOMETRIC_MASK))       /* holdout mask */
       {
       TIFFError ("writeSingleSection",
                  "JPEG compression cannot be used with %s image data",
@@ -6886,13 +6900,9 @@ writeSingleSection(TIFF *in, TIFF *out, struct image_data *image,
                  "palette" : "mask");
       return (-1);
       }
-    if (input_photometric == PHOTOMETRIC_RGB)
-      {
-      if (jpegcolormode == JPEGCOLORMODE_RGB)
-	TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
-      else
-	TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
-      } 
+    if ((input_photometric == PHOTOMETRIC_RGB) &&
+	(jpegcolormode == JPEGCOLORMODE_RGB))
+      TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
     else
 	TIFFSetField(out, TIFFTAG_PHOTOMETRIC, input_photometric);
     }
@@ -6907,8 +6917,8 @@ writeSingleSection(TIFF *in, TIFF *out, struct image_data *image,
 
 #ifdef DEBUG2
   TIFFError("writeSingleSection", "Input photometric: %s",
-	    (input_photmetric == PHOTMETRIC_RGB) ? "RGB" :
-	    ((input_photometric == PHOTOMETRIC_YCBCR) ?  "YCbCr" : "Not RGB or YCrCr"));
+	    (input_photometric == PHOTOMETRIC_RGB) ? "RGB" :
+	    ((input_photometric == PHOTOMETRIC_YCBCR) ?  "YCbCr" : "Not RGB or YCbCr"));
 #endif
 
   if (((input_photometric == PHOTOMETRIC_LOGL) ||
@@ -6916,7 +6926,7 @@ writeSingleSection(TIFF *in, TIFF *out, struct image_data *image,
       ((compression != COMPRESSION_SGILOG) && 
        (compression != COMPRESSION_SGILOG24)))
     {
-    TIFFError("writeCroppedImage",
+    TIFFError("writeSingleSection",
               "LogL and LogLuv source data require SGI_LOG or SGI_LOG24 compression");
     return (-1);
     }
@@ -7515,39 +7525,39 @@ writeCroppedImage(TIFF *in, TIFF *out, struct image_data *image,
   {
   uint16 bps, spp;
   uint16 input_compression, input_photometric;
-  uint16 input_jpeg_colormode, input_planar;
+  uint16 input_planar;
   struct cpTag* p;
 
-  TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &input_photometric);
-  TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &spp);
-  TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &bps);
+  input_compression = image->compression;
+  input_photometric = image->photometric;
+  spp = image->spp;
+  bps = image->bps;
 
   TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
   TIFFSetField(out, TIFFTAG_IMAGELENGTH, length);
+  TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bps);
+  TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
 
-  CopyField(TIFFTAG_BITSPERSAMPLE, bps);
-  CopyField(TIFFTAG_SAMPLESPERPIXEL, spp);
+#ifdef DEBUG2
+  TIFFError("writeCroppedImage", "Input compression: %s",
+	    (input_compression == COMPRESSION_OJPEG) ? "Old Jpeg" :
+	    ((input_compression == COMPRESSION_JPEG) ?  "New Jpeg" : "Non Jpeg"));
+#endif
 
-  TIFFGetField(in, TIFFTAG_COMPRESSION, &input_compression);
   if (compression != (uint16)-1)
     TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
   else
     {
     if (input_compression == COMPRESSION_OJPEG)
       {
-      TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_JPEG);
       compression = COMPRESSION_JPEG;
+      jpegcolormode = JPEGCOLORMODE_RAW;
+      TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_JPEG);
       }
     else
       CopyField(TIFFTAG_COMPRESSION, compression);
     }
 
-  TIFFGetField(in, TIFFTAG_JPEGCOLORMODE, &input_jpeg_colormode);
-#ifdef DEBUG2
-  TIFFError("writeCroppedImage", "Input compression: %s",
-	    (input_compression == COMPRESSION_OJPEG) ? "Old Jpeg" :
-	    ((input_compression == COMPRESSION_JPEG) ?  "New Jpeg" : "Non Jpeg"));
-#endif
   if (compression == COMPRESSION_JPEG)
     {
     if ((input_photometric == PHOTOMETRIC_PALETTE) ||  /* color map indexed */
@@ -7559,15 +7569,11 @@ writeCroppedImage(TIFF *in, TIFF *out, struct image_data *image,
                  "palette" : "mask");
       return (-1);
       }
-    if (input_photometric == PHOTOMETRIC_RGB)
-      {
-      if (jpegcolormode == JPEGCOLORMODE_RGB)
-	TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
-      else
-        TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
-      } 
+    if ((input_photometric == PHOTOMETRIC_RGB) &&
+	(jpegcolormode == JPEGCOLORMODE_RGB))
+      TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);
     else
-      TIFFSetField(out, TIFFTAG_PHOTOMETRIC, input_photometric);
+	TIFFSetField(out, TIFFTAG_PHOTOMETRIC, input_photometric);
     }
   else
     {