libtiff: removed UaToAa, and Bitmap16to8 items from TIFFRGBAImage (#1979)

From 8b19ed3c86784c4e23b3a0506291fdf56e956b7a Mon Sep 17 00:00:00 2001
From: Frank Warmerdam <[EMAIL REDACTED]>
Date: Thu, 18 Dec 2008 21:51:43 +0000
Subject: [PATCH] removed UaToAa, and Bitmap16to8 items from TIFFRGBAImage
 (#1979)

---
 ChangeLog              |   6 ++
 libtiff/tif_getimage.c | 148 +++++++++--------------------------------
 libtiff/tiffio.h       |  60 ++++++++---------
 3 files changed, 67 insertions(+), 147 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index a2f8d6ae..ce9c0a72 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2008-12-18  Frank Warmerdam  <warmerdam@pobox.com>
 
+	* libtiff/tif_getimage.c,tiffio.h: removed all use of UaToAa and
+	Bitmap16to8 arrays in TIFFRGBAImage structure to restore ABI
+	compatability.  These were just an attempt to speed up processing
+	with precalculated tables.
+	  http://bugzilla.maptools.org/show_bug.cgi?id=1979
+
 	* libtiff/tif_codec.c: Avoid printing c->name if it does not exist.
 
 2008-10-21  Andrey Kiselev  <dron@ak4719.spb.edu>
diff --git a/libtiff/tif_getimage.c b/libtiff/tif_getimage.c
index 74d8a98c..0e92a0de 100644
--- a/libtiff/tif_getimage.c
+++ b/libtiff/tif_getimage.c
@@ -1,4 +1,4 @@
-/* $Id: tif_getimage.c,v 1.63 2007-03-08 03:07:42 joris Exp $ */
+/* $Id: tif_getimage.c,v 1.63.2.1 2008-12-18 21:51:43 fwarmerdam Exp $ */
 
 /*
  * Copyright (c) 1991-1997 Sam Leffler
@@ -38,10 +38,6 @@ static int gtStripContig(TIFFRGBAImage*, uint32*, uint32, uint32);
 static int gtStripSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
 static int PickContigCase(TIFFRGBAImage*);
 static int PickSeparateCase(TIFFRGBAImage*);
-
-static int BuildMapUaToAa(TIFFRGBAImage* img);
-static int BuildMapBitdepth16To8(TIFFRGBAImage* img);
-
 static const char photoTag[] = "PhotometricInterpretation";
 
 /* 
@@ -206,11 +202,6 @@ TIFFRGBAImageEnd(TIFFRGBAImage* img)
 		_TIFFfree(img->ycbcr), img->ycbcr = NULL;
 	if (img->cielab)
 		_TIFFfree(img->cielab), img->cielab = NULL;
-	if (img->UaToAa)
-		_TIFFfree(img->UaToAa), img->UaToAa = NULL;
-	if (img->Bitdepth16To8)
-		_TIFFfree(img->Bitdepth16To8), img->Bitdepth16To8 = NULL;
-
 	if( img->redcmap ) {
 		_TIFFfree( img->redcmap );
 		_TIFFfree( img->greencmap );
@@ -438,8 +429,6 @@ TIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
 	img->PALmap = NULL;
 	img->ycbcr = NULL;
 	img->cielab = NULL;
-	img->UaToAa = NULL;
-	img->Bitdepth16To8 = NULL;
 	TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
 	TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
 	TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
@@ -1047,7 +1036,6 @@ gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
 #define	PACK4(r,g,b,a)	\
 	((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|((uint32)(a)<<24))
 #define W2B(v) (((v)>>8)&0xff)
-/* TODO: PACKW should have be made redundant in favor of Bitdepth16To8 LUT */
 #define	PACKW(r,g,b)	\
 	((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|A1)
 #define	PACKW4(r,g,b,a)	\
@@ -1278,13 +1266,11 @@ DECLAREContigPutFunc(putRGBUAcontig8bittile)
 	fromskew *= samplesperpixel;
 	while (h-- > 0) {
 		uint32 r, g, b, a;
-		uint8* m;
 		for (x = w; x-- > 0;) {
 			a = pp[3];
-			m = img->UaToAa+(a<<8);
-			r = m[pp[0]];
-			g = m[pp[1]];
-			b = m[pp[2]];
+                        r = (a*pp[0] + 127) / 255;
+                        g = (a*pp[1] + 127) / 255;
+                        b = (a*pp[2] + 127) / 255;
 			*cp++ = PACK4(r,g,b,a);
 			pp += samplesperpixel;
 		}
@@ -1304,10 +1290,8 @@ DECLAREContigPutFunc(putRGBcontig16bittile)
 	fromskew *= samplesperpixel;
 	while (h-- > 0) {
 		for (x = w; x-- > 0;) {
-			*cp++ = PACK(img->Bitdepth16To8[wp[0]],
-			    img->Bitdepth16To8[wp[1]],
-			    img->Bitdepth16To8[wp[2]]);
-			wp += samplesperpixel;
+                    *cp++ = PACKW(wp[0],wp[1],wp[2]);
+                    wp += samplesperpixel;
 		}
 		cp += toskew;
 		wp += fromskew;
@@ -1326,11 +1310,8 @@ DECLAREContigPutFunc(putRGBAAcontig16bittile)
 	fromskew *= samplesperpixel;
 	while (h-- > 0) {
 		for (x = w; x-- > 0;) {
-			*cp++ = PACK4(img->Bitdepth16To8[wp[0]],
-			    img->Bitdepth16To8[wp[1]],
-			    img->Bitdepth16To8[wp[2]],
-			    img->Bitdepth16To8[wp[3]]);
-			wp += samplesperpixel;
+                    *cp++ = PACKW4(wp[0],wp[1],wp[2],wp[3]);
+                    wp += samplesperpixel;
 		}
 		cp += toskew;
 		wp += fromskew;
@@ -1349,15 +1330,13 @@ DECLAREContigPutFunc(putRGBUAcontig16bittile)
 	fromskew *= samplesperpixel;
 	while (h-- > 0) {
 		uint32 r,g,b,a;
-		uint8* m;
 		for (x = w; x-- > 0;) {
-			a = img->Bitdepth16To8[wp[3]];
-			m = img->UaToAa+(a<<8);
-			r = m[img->Bitdepth16To8[wp[0]]];
-			g = m[img->Bitdepth16To8[wp[1]]];
-			b = m[img->Bitdepth16To8[wp[2]]];
-			*cp++ = PACK4(r,g,b,a);
-			wp += samplesperpixel;
+                    a = W2B(wp[3]);
+                    r = (a*W2B(wp[0]) + 127) / 255;
+                    g = (a*W2B(wp[1]) + 127) / 255;
+                    b = (a*W2B(wp[2]) + 127) / 255;
+                    *cp++ = PACK4(r,g,b,a);
+                    wp += samplesperpixel;
 		}
 		cp += toskew;
 		wp += fromskew;
@@ -1460,13 +1439,11 @@ DECLARESepPutFunc(putRGBUAseparate8bittile)
 	(void) img; (void) y;
 	while (h-- > 0) {
 		uint32 rv, gv, bv, av;
-		uint8* m;
 		for (x = w; x-- > 0;) {
 			av = *a++;
-			m = img->UaToAa+(av<<8);
-			rv = m[*r++];
-			gv = m[*g++];
-			bv = m[*b++];
+                        rv = (av* *r++ + 127) / 255;
+                        gv = (av* *g++ + 127) / 255;
+                        bv = (av* *b++ + 127) / 255;
 			*cp++ = PACK4(rv,gv,bv,av);
 		}
 		SKEW4(r, g, b, a, fromskew);
@@ -1485,9 +1462,7 @@ DECLARESepPutFunc(putRGBseparate16bittile)
 	(void) img; (void) y; (void) a;
 	while (h-- > 0) {
 		for (x = 0; x < w; x++)
-			*cp++ = PACK(img->Bitdepth16To8[*wr++],
-			    img->Bitdepth16To8[*wg++],
-			    img->Bitdepth16To8[*wb++]);
+                    *cp++ = PACKW(*wr++,*wg++,*wb++);
 		SKEW(wr, wg, wb, fromskew);
 		cp += toskew;
 	}
@@ -1505,10 +1480,7 @@ DECLARESepPutFunc(putRGBAAseparate16bittile)
 	(void) img; (void) y;
 	while (h-- > 0) {
 		for (x = 0; x < w; x++)
-			*cp++ = PACK4(img->Bitdepth16To8[*wr++],
-			    img->Bitdepth16To8[*wg++],
-			    img->Bitdepth16To8[*wb++],
-			    img->Bitdepth16To8[*wa++]);
+                    *cp++ = PACKW4(*wr++,*wg++,*wb++,*wa++);
 		SKEW4(wr, wg, wb, wa, fromskew);
 		cp += toskew;
 	}
@@ -1526,14 +1498,12 @@ DECLARESepPutFunc(putRGBUAseparate16bittile)
 	(void) img; (void) y;
 	while (h-- > 0) {
 		uint32 r,g,b,a;
-		uint8* m;
 		for (x = w; x-- > 0;) {
-			a = img->Bitdepth16To8[*wa++];
-			m = img->UaToAa+(a<<8);
-			r = m[img->Bitdepth16To8[*wr++]];
-			g = m[img->Bitdepth16To8[*wg++]];
-			b = m[img->Bitdepth16To8[*wb++]];
-			*cp++ = PACK4(r,g,b,a);
+                    a = W2B(*wa++);
+                    r = (a*W2B(*wr++) + 127) / 255;
+                    g = (a*W2B(*wg++) + 127) / 255;
+                    b = (a*W2B(*wb++) + 127) / 255;
+                    *cp++ = PACK4(r,g,b,a);
 		}
 		SKEW4(wr, wg, wb, wa, fromskew);
 		cp += toskew;
@@ -2355,28 +2325,23 @@ PickContigCase(TIFFRGBAImage* img)
 						img->put.contig = putRGBAAcontig8bittile;
 					else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
 					{
-						if (BuildMapUaToAa(img))
-							img->put.contig = putRGBUAcontig8bittile;
+                                            img->put.contig = putRGBUAcontig8bittile;
 					}
 					else
-						img->put.contig = putRGBcontig8bittile;
+                                            img->put.contig = putRGBcontig8bittile;
 					break;
 				case 16:
 					if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
 					{
-						if (BuildMapBitdepth16To8(img))
-							img->put.contig = putRGBAAcontig16bittile;
+                                            img->put.contig = putRGBAAcontig16bittile;
 					}
 					else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
 					{
-						if (BuildMapBitdepth16To8(img) &&
-						    BuildMapUaToAa(img))
-							img->put.contig = putRGBUAcontig16bittile;
+                                            img->put.contig = putRGBUAcontig16bittile;
 					}
 					else
 					{
-						if (BuildMapBitdepth16To8(img))
-							img->put.contig = putRGBcontig16bittile;
+                                            img->put.contig = putRGBcontig16bittile;
 					}
 					break;
 			}
@@ -2501,8 +2466,7 @@ PickSeparateCase(TIFFRGBAImage* img)
 						img->put.separate = putRGBAAseparate8bittile;
 					else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
 					{
-						if (BuildMapUaToAa(img))
-							img->put.separate = putRGBUAseparate8bittile;
+                                            img->put.separate = putRGBUAseparate8bittile;
 					}
 					else
 						img->put.separate = putRGBseparate8bittile;
@@ -2510,19 +2474,15 @@ PickSeparateCase(TIFFRGBAImage* img)
 				case 16:
 					if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
 					{
-						if (BuildMapBitdepth16To8(img))
-							img->put.separate = putRGBAAseparate16bittile;
+                                            img->put.separate = putRGBAAseparate16bittile;
 					}
 					else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
 					{
-						if (BuildMapBitdepth16To8(img) &&
-						    BuildMapUaToAa(img))
-							img->put.separate = putRGBUAseparate16bittile;
+                                            img->put.separate = putRGBUAseparate16bittile;
 					}
 					else
 					{
-						if (BuildMapBitdepth16To8(img))
-							img->put.separate = putRGBseparate16bittile;
+                                            img->put.separate = putRGBseparate16bittile;
 					}
 					break;
 			}
@@ -2547,48 +2507,6 @@ PickSeparateCase(TIFFRGBAImage* img)
 	return ((img->get!=NULL) && (img->put.separate!=NULL));
 }
 
-static int
-BuildMapUaToAa(TIFFRGBAImage* img)
-{
-	static const char module[]="BuildMapUaToAa";
-	uint8* m;
-	uint16 na,nv;
-	assert(img->UaToAa==NULL);
-	img->UaToAa=_TIFFmalloc(65536);
-	if (img->UaToAa==NULL)
-	{
-		TIFFErrorExt(img->tif,module,"Out of memory");
-		return(0);
-	}
-	m=img->UaToAa;
-	for (na=0; na<256; na++)
-	{
-		for (nv=0; nv<256; nv++)
-			*m++=(nv*na+127)/255;
-	}
-	return(1);
-}
-
-static int
-BuildMapBitdepth16To8(TIFFRGBAImage* img)
-{
-	static const char module[]="BuildMapBitdepth16To8";
-	uint8* m;
-	uint32 n;
-	assert(img->Bitdepth16To8==NULL);
-	img->Bitdepth16To8=_TIFFmalloc(65536);
-	if (img->Bitdepth16To8==NULL)
-	{
-		TIFFErrorExt(img->tif,module,"Out of memory");
-		return(0);
-	}
-	m=img->Bitdepth16To8;
-	for (n=0; n<65536; n++)
-		*m++=(n+128)/257;
-	return(1);
-}
-
-
 /*
  * Read a whole strip off data from the file, and convert to RGBA form.
  * If this is the last strip, then it will only contain the portion of
diff --git a/libtiff/tiffio.h b/libtiff/tiffio.h
index b5097f45..29c99f1d 100644
--- a/libtiff/tiffio.h
+++ b/libtiff/tiffio.h
@@ -1,21 +1,21 @@
-/* $Id: tiffio.h,v 1.57 2007-03-17 04:41:29 joris Exp $ */
+/* $Id: tiffio.h,v 1.56.2.1 2008-12-18 21:51:43 fwarmerdam Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  *
- * Permission to use, copy, modify, distribute, and sell this software and
+ * Permission to use, copy, modify, distribute, and sell this software and 
  * its documentation for any purpose is hereby granted without fee, provided
  * that (i) the above copyright notices and this permission notice appear in
  * all copies of the software and related documentation, and (ii) the names of
  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  * publicity relating to the software without the specific, prior written
  * permission of Sam Leffler and Silicon Graphics.
- *
- * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
- * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
- *
+ * 
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
+ * 
  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
@@ -37,7 +37,7 @@
  * TIFF is defined as an incomplete type to hide the
  * library's internal data structures from clients.
  */
-typedef struct tiff TIFF;
+typedef	struct tiff TIFF;
 
 /*
  * The following typedefs define the intrinsic size of
@@ -218,9 +218,6 @@ struct _TIFFRGBAImage {
 	TIFFYCbCrToRGB* ycbcr;                  /* YCbCr conversion state */
 	TIFFCIELabToRGB* cielab;                /* CIE L*a*b conversion state */
 
-	uint8* UaToAa;                          /* Unassociated alpha to associated alpha convertion LUT */
-	uint8* Bitdepth16To8;                   /* LUT for conversion from 16bit to 8bit values */
-
 	int row_offset;
 	int col_offset;
 };
@@ -259,15 +256,15 @@ typedef struct {
 #if defined(c_plusplus) || defined(__cplusplus)
 extern "C" {
 #endif
-typedef void (*TIFFErrorHandler)(const char*, const char*, va_list);
-typedef void (*TIFFErrorHandlerExt)(thandle_t, const char*, const char*, va_list);
-typedef uint32 (*TIFFReadWriteProc)(thandle_t, tdata_t, uint32);
-typedef uint64 (*TIFFSeekProc)(thandle_t, uint64, int);
-typedef int (*TIFFCloseProc)(thandle_t);
-typedef uint64 (*TIFFSizeProc)(thandle_t);
-typedef int (*TIFFMapFileProc)(thandle_t, tdata_t*, toff_t*);
-typedef void (*TIFFUnmapFileProc)(thandle_t, tdata_t, toff_t);
-typedef void (*TIFFExtendProc)(TIFF*);
+typedef	void (*TIFFErrorHandler)(const char*, const char*, va_list);
+typedef	void (*TIFFErrorHandlerExt)(thandle_t, const char*, const char*, va_list);
+typedef	tsize_t (*TIFFReadWriteProc)(thandle_t, tdata_t, tsize_t);
+typedef	toff_t (*TIFFSeekProc)(thandle_t, toff_t, int);
+typedef	int (*TIFFCloseProc)(thandle_t);
+typedef	toff_t (*TIFFSizeProc)(thandle_t);
+typedef	int (*TIFFMapFileProc)(thandle_t, tdata_t*, toff_t*);
+typedef	void (*TIFFUnmapFileProc)(thandle_t, tdata_t, toff_t);
+typedef	void (*TIFFExtendProc)(TIFF*); 
 
 extern	const char* TIFFGetVersion(void);
 
@@ -464,18 +461,17 @@ extern	tsize_t TIFFWriteEncodedStrip(TIFF*, tstrip_t, tdata_t, tsize_t);
 extern	tsize_t TIFFWriteRawStrip(TIFF*, tstrip_t, tdata_t, tsize_t);
 extern	tsize_t TIFFWriteEncodedTile(TIFF*, ttile_t, tdata_t, tsize_t);
 extern	tsize_t TIFFWriteRawTile(TIFF*, ttile_t, tdata_t, tsize_t);
-extern int TIFFDataWidth(TIFFDataType);    /* table of tag datatype widths */
-extern void TIFFSetWriteOffset(TIFF*, toff_t);
-extern void TIFFSwabShort(uint16*);
-extern void TIFFSwabLong(uint32*);
-extern void TIFFSwabLong8(uint64*);
-extern void TIFFSwabDouble(double*);
-extern void TIFFSwabArrayOfShort(uint16*, unsigned long);
-extern void TIFFSwabArrayOfTriples(uint8*, unsigned long);
-extern void TIFFSwabArrayOfLong(uint32*, unsigned long);
-extern void TIFFSwabArrayOfDouble(double*, unsigned long);
-extern void TIFFReverseBits(unsigned char *, unsigned long);
-extern const unsigned char* TIFFGetBitRevTable(int);
+extern	int TIFFDataWidth(TIFFDataType);    /* table of tag datatype widths */
+extern	void TIFFSetWriteOffset(TIFF*, toff_t);
+extern	void TIFFSwabShort(uint16*);
+extern	void TIFFSwabLong(uint32*);
+extern	void TIFFSwabDouble(double*);
+extern	void TIFFSwabArrayOfShort(uint16*, unsigned long);
+extern	void TIFFSwabArrayOfTriples(uint8*, unsigned long);
+extern	void TIFFSwabArrayOfLong(uint32*, unsigned long);
+extern	void TIFFSwabArrayOfDouble(double*, unsigned long);
+extern	void TIFFReverseBits(unsigned char *, unsigned long);
+extern	const unsigned char* TIFFGetBitRevTable(int);
 
 #ifdef LOGLUV_PUBLIC
 #define U_NEU		0.210526316