libtiff: Added support 16-bit images as per bug

From c63b5ef021619a449c373511fc25cf8eccca1d47 Mon Sep 17 00:00:00 2001
From: Andrey Kiselev <[EMAIL REDACTED]>
Date: Tue, 3 Jul 2007 15:59:06 +0000
Subject: [PATCH] Added support 16-bit images as per bug
 http://bugzilla.remotesensing.org/show_bug.cgi?id=1566 Patch from William
 Bader.

---
 tools/tiff2ps.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 48 insertions(+), 2 deletions(-)

diff --git a/tools/tiff2ps.c b/tools/tiff2ps.c
index d7b2f0b6..559da2ab 100644
--- a/tools/tiff2ps.c
+++ b/tools/tiff2ps.c
@@ -1,4 +1,4 @@
-/* $Id: tiff2ps.c,v 1.35 2006-02-23 14:50:32 dron Exp $ */
+/* $Id: tiff2ps.c,v 1.35.2.1 2007-07-03 15:59:06 dron Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -350,6 +350,7 @@ checkImage(TIFF* tif)
 	switch (bitspersample) {
 	case 1: case 2:
 	case 4: case 8:
+	case 16:
 		break;
 	default:
 		TIFFError(filename, "Can not handle %d-bit/sample image",
@@ -434,7 +435,11 @@ setupPageState(TIFF* tif, uint32* pw, uint32* ph, double* pprw, double* pprh)
 		break;
 	case RESUNIT_NONE:
 	default:
-		xres *= PS_UNIT_SIZE, yres *= PS_UNIT_SIZE;
+		/*
+		 * check that the resolution is not inches before scaling it
+		 */
+		if (xres != PS_UNIT_SIZE || yres != PS_UNIT_SIZE)
+			xres *= PS_UNIT_SIZE, yres *= PS_UNIT_SIZE;
 		break;
 	}
 	*pprh = PSUNITS(*ph, yres);
@@ -1173,6 +1178,26 @@ PS_Lvl2ImageDict(FILE* fd, TIFF* tif, uint32 w, uint32 h)
 	return(use_rawdata);
 }
 
+/* Flip the byte order of buffers with 16 bit samples */
+static void
+PS_FlipBytes(unsigned char* buf, int count)
+{
+	int i;
+	unsigned char temp;
+
+	if (count <= 0 || bitspersample <= 8) {
+		return;
+	}
+
+	count--;
+
+	for (i = 0; i < count; i += 2) {
+		temp = buf[i];
+		buf[i] = buf[i + 1];
+		buf[i + 1] = temp;
+	}
+}
+
 #define MAXLINE		36
 
 int
@@ -1278,6 +1303,13 @@ PS_Lvl2page(FILE* fd, TIFF* tif, uint32 w, uint32 h)
 			if (ascii85)
 				Ascii85Put('\0', fd);
 		}
+		/*
+		 * for 16 bits, the two bytes must be most significant
+		 * byte first
+		 */
+		if (bitspersample == 16 && !TIFFIsBigEndian(tif)) {
+			PS_FlipBytes(buf_data, byte_count);
+		}
 		/*
 		 * For images with alpha, matte against a white background;
 		 * i.e. Cback * (1 - Aimage) where Cback = 1. We will fill the
@@ -1476,6 +1508,13 @@ PSDataColorContig(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc)
 		if (TIFFReadScanline(tif, tf_buf, row, 0) < 0)
 			break;
 		cp = tf_buf;
+		/*
+		 * for 16 bits, the two bytes must be most significant
+		 * byte first
+		 */
+		if (bitspersample == 16 && !HOST_BIGENDIAN) {
+			PS_FlipBytes(cp, tf_bytesperrow);
+		}
 		if (alpha) {
 			int adjust;
 			cc = 0;
@@ -1677,6 +1716,13 @@ PSDataBW(FILE* fd, TIFF* tif, uint32 w, uint32 h)
 				*cp = ~*cp;
 			cp++;
 		}
+		/*
+		 * for 16 bits, the two bytes must be most significant
+		 * byte first
+		 */
+		if (bitspersample == 16 && !HOST_BIGENDIAN) {
+			PS_FlipBytes(cp, cc);
+		}
 		if (ascii85) {
 #if defined( EXP_ASCII85ENCODER )
 			if (alpha) {