roentgen b75cab
/* $Id: tif_predict.c,v 1.32 2010-03-10 18:56:49 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 1988-1997 Sam Leffler
roentgen b75cab
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
roentgen b75cab
 *
roentgen b75cab
 * Permission to use, copy, modify, distribute, and sell this software and 
roentgen b75cab
 * its documentation for any purpose is hereby granted without fee, provided
roentgen b75cab
 * that (i) the above copyright notices and this permission notice appear in
roentgen b75cab
 * all copies of the software and related documentation, and (ii) the names of
roentgen b75cab
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
roentgen b75cab
 * publicity relating to the software without the specific, prior written
roentgen b75cab
 * permission of Sam Leffler and Silicon Graphics.
roentgen b75cab
 * 
roentgen b75cab
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
roentgen b75cab
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
roentgen b75cab
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
roentgen b75cab
 * 
roentgen b75cab
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
roentgen b75cab
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
roentgen b75cab
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
roentgen b75cab
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
roentgen b75cab
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
roentgen b75cab
 * OF THIS SOFTWARE.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * TIFF Library.
roentgen b75cab
 *
roentgen b75cab
 * Predictor Tag Support (used by multiple codecs).
roentgen b75cab
 */
roentgen b75cab
#include "tiffiop.h"
roentgen b75cab
#include "tif_predict.h"
roentgen b75cab
roentgen b75cab
#define	PredictorState(tif)	((TIFFPredictorState*) (tif)->tif_data)
roentgen b75cab
roentgen b75cab
static void horAcc8(TIFF* tif, uint8* cp0, tmsize_t cc);
roentgen b75cab
static void horAcc16(TIFF* tif, uint8* cp0, tmsize_t cc);
roentgen b75cab
static void horAcc32(TIFF* tif, uint8* cp0, tmsize_t cc);
roentgen b75cab
static void swabHorAcc16(TIFF* tif, uint8* cp0, tmsize_t cc);
roentgen b75cab
static void swabHorAcc32(TIFF* tif, uint8* cp0, tmsize_t cc);
roentgen b75cab
static void horDiff8(TIFF* tif, uint8* cp0, tmsize_t cc);
roentgen b75cab
static void horDiff16(TIFF* tif, uint8* cp0, tmsize_t cc);
roentgen b75cab
static void horDiff32(TIFF* tif, uint8* cp0, tmsize_t cc);
roentgen b75cab
static void fpAcc(TIFF* tif, uint8* cp0, tmsize_t cc);
roentgen b75cab
static void fpDiff(TIFF* tif, uint8* cp0, tmsize_t cc);
roentgen b75cab
static int PredictorDecodeRow(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
roentgen b75cab
static int PredictorDecodeTile(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
roentgen b75cab
static int PredictorEncodeRow(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
roentgen b75cab
static int PredictorEncodeTile(TIFF* tif, uint8* bp0, tmsize_t cc0, uint16 s);
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
PredictorSetup(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "PredictorSetup";
roentgen b75cab
roentgen b75cab
	TIFFPredictorState* sp = PredictorState(tif);
roentgen b75cab
	TIFFDirectory* td = &tif->tif_dir;
roentgen b75cab
roentgen b75cab
	switch (sp->predictor)		/* no differencing */
roentgen b75cab
	{
roentgen b75cab
		case PREDICTOR_NONE:
roentgen b75cab
			return 1;
roentgen b75cab
		case PREDICTOR_HORIZONTAL:
roentgen b75cab
			if (td->td_bitspersample != 8
roentgen b75cab
			    && td->td_bitspersample != 16
roentgen b75cab
			    && td->td_bitspersample != 32) {
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
				    "Horizontal differencing \"Predictor\" not supported with %d-bit samples",
roentgen b75cab
				    td->td_bitspersample);
roentgen b75cab
				return 0;
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		case PREDICTOR_FLOATINGPOINT:
roentgen b75cab
			if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP) {
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
				    "Floating point \"Predictor\" not supported with %d data format",
roentgen b75cab
				    td->td_sampleformat);
roentgen b75cab
				return 0;
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		default:
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			    "\"Predictor\" value %d not supported",
roentgen b75cab
			    sp->predictor);
roentgen b75cab
			return 0;
roentgen b75cab
	}
roentgen b75cab
	sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
roentgen b75cab
	    td->td_samplesperpixel : 1);
roentgen b75cab
	/*
roentgen b75cab
	 * Calculate the scanline/tile-width size in bytes.
roentgen b75cab
	 */
roentgen b75cab
	if (isTiled(tif))
roentgen b75cab
		sp->rowsize = TIFFTileRowSize(tif);
roentgen b75cab
	else
roentgen b75cab
		sp->rowsize = TIFFScanlineSize(tif);
roentgen b75cab
	if (sp->rowsize == 0)
roentgen b75cab
		return 0;
roentgen b75cab
roentgen b75cab
	return 1;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
PredictorSetupDecode(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	TIFFPredictorState* sp = PredictorState(tif);
roentgen b75cab
	TIFFDirectory* td = &tif->tif_dir;
roentgen b75cab
roentgen b75cab
	if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif))
roentgen b75cab
		return 0;
roentgen b75cab
roentgen b75cab
	if (sp->predictor == 2) {
roentgen b75cab
		switch (td->td_bitspersample) {
roentgen b75cab
			case 8:  sp->decodepfunc = horAcc8; break;
roentgen b75cab
			case 16: sp->decodepfunc = horAcc16; break;
roentgen b75cab
			case 32: sp->decodepfunc = horAcc32; break;
roentgen b75cab
		}
roentgen b75cab
		/*
roentgen b75cab
		 * Override default decoding method with one that does the
roentgen b75cab
		 * predictor stuff.
roentgen b75cab
		 */
roentgen b75cab
                if( tif->tif_decoderow != PredictorDecodeRow )
roentgen b75cab
                {
roentgen b75cab
                    sp->decoderow = tif->tif_decoderow;
roentgen b75cab
                    tif->tif_decoderow = PredictorDecodeRow;
roentgen b75cab
                    sp->decodestrip = tif->tif_decodestrip;
roentgen b75cab
                    tif->tif_decodestrip = PredictorDecodeTile;
roentgen b75cab
                    sp->decodetile = tif->tif_decodetile;
roentgen b75cab
                    tif->tif_decodetile = PredictorDecodeTile;
roentgen b75cab
                }
roentgen b75cab
roentgen b75cab
		/*
roentgen b75cab
		 * If the data is horizontally differenced 16-bit data that
roentgen b75cab
		 * requires byte-swapping, then it must be byte swapped before
roentgen b75cab
		 * the accumulation step.  We do this with a special-purpose
roentgen b75cab
		 * routine and override the normal post decoding logic that
roentgen b75cab
		 * the library setup when the directory was read.
roentgen b75cab
		 */
roentgen b75cab
		if (tif->tif_flags & TIFF_SWAB) {
roentgen b75cab
			if (sp->decodepfunc == horAcc16) {
roentgen b75cab
				sp->decodepfunc = swabHorAcc16;
roentgen b75cab
				tif->tif_postdecode = _TIFFNoPostDecode;
roentgen b75cab
            } else if (sp->decodepfunc == horAcc32) {
roentgen b75cab
				sp->decodepfunc = swabHorAcc32;
roentgen b75cab
				tif->tif_postdecode = _TIFFNoPostDecode;
roentgen b75cab
            }
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	else if (sp->predictor == 3) {
roentgen b75cab
		sp->decodepfunc = fpAcc;
roentgen b75cab
		/*
roentgen b75cab
		 * Override default decoding method with one that does the
roentgen b75cab
		 * predictor stuff.
roentgen b75cab
		 */
roentgen b75cab
                if( tif->tif_decoderow != PredictorDecodeRow )
roentgen b75cab
                {
roentgen b75cab
                    sp->decoderow = tif->tif_decoderow;
roentgen b75cab
                    tif->tif_decoderow = PredictorDecodeRow;
roentgen b75cab
                    sp->decodestrip = tif->tif_decodestrip;
roentgen b75cab
                    tif->tif_decodestrip = PredictorDecodeTile;
roentgen b75cab
                    sp->decodetile = tif->tif_decodetile;
roentgen b75cab
                    tif->tif_decodetile = PredictorDecodeTile;
roentgen b75cab
                }
roentgen b75cab
		/*
roentgen b75cab
		 * The data should not be swapped outside of the floating
roentgen b75cab
		 * point predictor, the accumulation routine should return
roentgen b75cab
		 * byres in the native order.
roentgen b75cab
		 */
roentgen b75cab
		if (tif->tif_flags & TIFF_SWAB) {
roentgen b75cab
			tif->tif_postdecode = _TIFFNoPostDecode;
roentgen b75cab
		}
roentgen b75cab
		/*
roentgen b75cab
		 * Allocate buffer to keep the decoded bytes before
roentgen b75cab
		 * rearranging in the ight order
roentgen b75cab
		 */
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	return 1;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
PredictorSetupEncode(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	TIFFPredictorState* sp = PredictorState(tif);
roentgen b75cab
	TIFFDirectory* td = &tif->tif_dir;
roentgen b75cab
roentgen b75cab
	if (!(*sp->setupencode)(tif) || !PredictorSetup(tif))
roentgen b75cab
		return 0;
roentgen b75cab
roentgen b75cab
	if (sp->predictor == 2) {
roentgen b75cab
		switch (td->td_bitspersample) {
roentgen b75cab
			case 8:  sp->encodepfunc = horDiff8; break;
roentgen b75cab
			case 16: sp->encodepfunc = horDiff16; break;
roentgen b75cab
			case 32: sp->encodepfunc = horDiff32; break;
roentgen b75cab
		}
roentgen b75cab
		/*
roentgen b75cab
		 * Override default encoding method with one that does the
roentgen b75cab
		 * predictor stuff.
roentgen b75cab
		 */
roentgen b75cab
                if( tif->tif_encoderow != PredictorEncodeRow )
roentgen b75cab
                {
roentgen b75cab
                    sp->encoderow = tif->tif_encoderow;
roentgen b75cab
                    tif->tif_encoderow = PredictorEncodeRow;
roentgen b75cab
                    sp->encodestrip = tif->tif_encodestrip;
roentgen b75cab
                    tif->tif_encodestrip = PredictorEncodeTile;
roentgen b75cab
                    sp->encodetile = tif->tif_encodetile;
roentgen b75cab
                    tif->tif_encodetile = PredictorEncodeTile;
roentgen b75cab
                }
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	else if (sp->predictor == 3) {
roentgen b75cab
		sp->encodepfunc = fpDiff;
roentgen b75cab
		/*
roentgen b75cab
		 * Override default encoding method with one that does the
roentgen b75cab
		 * predictor stuff.
roentgen b75cab
		 */
roentgen b75cab
                if( tif->tif_encoderow != PredictorEncodeRow )
roentgen b75cab
                {
roentgen b75cab
                    sp->encoderow = tif->tif_encoderow;
roentgen b75cab
                    tif->tif_encoderow = PredictorEncodeRow;
roentgen b75cab
                    sp->encodestrip = tif->tif_encodestrip;
roentgen b75cab
                    tif->tif_encodestrip = PredictorEncodeTile;
roentgen b75cab
                    sp->encodetile = tif->tif_encodetile;
roentgen b75cab
                    tif->tif_encodetile = PredictorEncodeTile;
roentgen b75cab
                }
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	return 1;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#define REPEAT4(n, op)		\
roentgen b75cab
    switch (n) {		\
roentgen b75cab
    default: { tmsize_t i; for (i = n-4; i > 0; i--) { op; } } \
roentgen b75cab
    case 4:  op;		\
roentgen b75cab
    case 3:  op;		\
roentgen b75cab
    case 2:  op;		\
roentgen b75cab
    case 1:  op;		\
roentgen b75cab
    case 0:  ;			\
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
horAcc8(TIFF* tif, uint8* cp0, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
	tmsize_t stride = PredictorState(tif)->stride;
roentgen b75cab
roentgen b75cab
	char* cp = (char*) cp0;
roentgen b75cab
	assert((cc%stride)==0);
roentgen b75cab
	if (cc > stride) {
roentgen b75cab
		/*
roentgen b75cab
		 * Pipeline the most common cases.
roentgen b75cab
		 */
roentgen b75cab
		if (stride == 3)  {
roentgen b75cab
			unsigned int cr = cp[0];
roentgen b75cab
			unsigned int cg = cp[1];
roentgen b75cab
			unsigned int cb = cp[2];
roentgen b75cab
			cc -= 3;
roentgen b75cab
			cp += 3;
roentgen b75cab
			while (cc>0) {
roentgen b75cab
				cp[0] = (char) (cr += cp[0]);
roentgen b75cab
				cp[1] = (char) (cg += cp[1]);
roentgen b75cab
				cp[2] = (char) (cb += cp[2]);
roentgen b75cab
				cc -= 3;
roentgen b75cab
				cp += 3;
roentgen b75cab
			}
roentgen b75cab
		} else if (stride == 4)  {
roentgen b75cab
			unsigned int cr = cp[0];
roentgen b75cab
			unsigned int cg = cp[1];
roentgen b75cab
			unsigned int cb = cp[2];
roentgen b75cab
			unsigned int ca = cp[3];
roentgen b75cab
			cc -= 4;
roentgen b75cab
			cp += 4;
roentgen b75cab
			while (cc>0) {
roentgen b75cab
				cp[0] = (char) (cr += cp[0]);
roentgen b75cab
				cp[1] = (char) (cg += cp[1]);
roentgen b75cab
				cp[2] = (char) (cb += cp[2]);
roentgen b75cab
				cp[3] = (char) (ca += cp[3]);
roentgen b75cab
				cc -= 4;
roentgen b75cab
				cp += 4;
roentgen b75cab
			}
roentgen b75cab
		} else  {
roentgen b75cab
			cc -= stride;
roentgen b75cab
			do {
roentgen b75cab
				REPEAT4(stride, cp[stride] =
roentgen b75cab
					(char) (cp[stride] + *cp); cp++)
roentgen b75cab
				cc -= stride;
roentgen b75cab
			} while (cc>0);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
swabHorAcc16(TIFF* tif, uint8* cp0, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
	tmsize_t stride = PredictorState(tif)->stride;
roentgen b75cab
	uint16* wp = (uint16*) cp0;
roentgen b75cab
	tmsize_t wc = cc / 2;
roentgen b75cab
roentgen b75cab
	assert((cc%(2*stride))==0);
roentgen b75cab
roentgen b75cab
	if (wc > stride) {
roentgen b75cab
		TIFFSwabArrayOfShort(wp, wc);
roentgen b75cab
		wc -= stride;
roentgen b75cab
		do {
roentgen b75cab
			REPEAT4(stride, wp[stride] += wp[0]; wp++)
roentgen b75cab
			wc -= stride;
roentgen b75cab
		} while (wc > 0);
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
horAcc16(TIFF* tif, uint8* cp0, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
	tmsize_t stride = PredictorState(tif)->stride;
roentgen b75cab
	uint16* wp = (uint16*) cp0;
roentgen b75cab
	tmsize_t wc = cc / 2;
roentgen b75cab
roentgen b75cab
	assert((cc%(2*stride))==0);
roentgen b75cab
roentgen b75cab
	if (wc > stride) {
roentgen b75cab
		wc -= stride;
roentgen b75cab
		do {
roentgen b75cab
			REPEAT4(stride, wp[stride] += wp[0]; wp++)
roentgen b75cab
			wc -= stride;
roentgen b75cab
		} while (wc > 0);
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
swabHorAcc32(TIFF* tif, uint8* cp0, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
	tmsize_t stride = PredictorState(tif)->stride;
roentgen b75cab
	uint32* wp = (uint32*) cp0;
roentgen b75cab
	tmsize_t wc = cc / 4;
roentgen b75cab
roentgen b75cab
	assert((cc%(4*stride))==0);
roentgen b75cab
roentgen b75cab
	if (wc > stride) {
roentgen b75cab
		TIFFSwabArrayOfLong(wp, wc);
roentgen b75cab
		wc -= stride;
roentgen b75cab
		do {
roentgen b75cab
			REPEAT4(stride, wp[stride] += wp[0]; wp++)
roentgen b75cab
			wc -= stride;
roentgen b75cab
		} while (wc > 0);
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
horAcc32(TIFF* tif, uint8* cp0, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
	tmsize_t stride = PredictorState(tif)->stride;
roentgen b75cab
	uint32* wp = (uint32*) cp0;
roentgen b75cab
	tmsize_t wc = cc / 4;
roentgen b75cab
roentgen b75cab
	assert((cc%(4*stride))==0);
roentgen b75cab
roentgen b75cab
	if (wc > stride) {
roentgen b75cab
		wc -= stride;
roentgen b75cab
		do {
roentgen b75cab
			REPEAT4(stride, wp[stride] += wp[0]; wp++)
roentgen b75cab
			wc -= stride;
roentgen b75cab
		} while (wc > 0);
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Floating point predictor accumulation routine.
roentgen b75cab
 */
roentgen b75cab
static void
roentgen b75cab
fpAcc(TIFF* tif, uint8* cp0, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
	tmsize_t stride = PredictorState(tif)->stride;
roentgen b75cab
	uint32 bps = tif->tif_dir.td_bitspersample / 8;
roentgen b75cab
	tmsize_t wc = cc / bps;
roentgen b75cab
	tmsize_t count = cc;
roentgen b75cab
	uint8 *cp = (uint8 *) cp0;
roentgen b75cab
	uint8 *tmp = (uint8 *)_TIFFmalloc(cc);
roentgen b75cab
roentgen b75cab
	assert((cc%(bps*stride))==0);
roentgen b75cab
roentgen b75cab
	if (!tmp)
roentgen b75cab
		return;
roentgen b75cab
roentgen b75cab
	while (count > stride) {
roentgen b75cab
		REPEAT4(stride, cp[stride] += cp[0]; cp++)
roentgen b75cab
		count -= stride;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	_TIFFmemcpy(tmp, cp0, cc);
roentgen b75cab
	cp = (uint8 *) cp0;
roentgen b75cab
	for (count = 0; count < wc; count++) {
roentgen b75cab
		uint32 byte;
roentgen b75cab
		for (byte = 0; byte < bps; byte++) {
roentgen b75cab
			#if WORDS_BIGENDIAN
roentgen b75cab
			cp[bps * count + byte] = tmp[byte * wc + count];
roentgen b75cab
			#else
roentgen b75cab
			cp[bps * count + byte] =
roentgen b75cab
				tmp[(bps - byte - 1) * wc + count];
roentgen b75cab
			#endif
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	_TIFFfree(tmp);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Decode a scanline and apply the predictor routine.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
PredictorDecodeRow(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
roentgen b75cab
{
roentgen b75cab
	TIFFPredictorState *sp = PredictorState(tif);
roentgen b75cab
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
	assert(sp->decoderow != NULL);
roentgen b75cab
	assert(sp->decodepfunc != NULL);  
roentgen b75cab
roentgen b75cab
	if ((*sp->decoderow)(tif, op0, occ0, s)) {
roentgen b75cab
		(*sp->decodepfunc)(tif, op0, occ0);
roentgen b75cab
		return 1;
roentgen b75cab
	} else
roentgen b75cab
		return 0;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Decode a tile/strip and apply the predictor routine.
roentgen b75cab
 * Note that horizontal differencing must be done on a
roentgen b75cab
 * row-by-row basis.  The width of a "row" has already
roentgen b75cab
 * been calculated at pre-decode time according to the
roentgen b75cab
 * strip/tile dimensions.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
PredictorDecodeTile(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
roentgen b75cab
{
roentgen b75cab
	TIFFPredictorState *sp = PredictorState(tif);
roentgen b75cab
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
	assert(sp->decodetile != NULL);
roentgen b75cab
roentgen b75cab
	if ((*sp->decodetile)(tif, op0, occ0, s)) {
roentgen b75cab
		tmsize_t rowsize = sp->rowsize;
roentgen b75cab
		assert(rowsize > 0);
roentgen b75cab
		assert((occ0%rowsize)==0);
roentgen b75cab
		assert(sp->decodepfunc != NULL);
roentgen b75cab
		while (occ0 > 0) {
roentgen b75cab
			(*sp->decodepfunc)(tif, op0, rowsize);
roentgen b75cab
			occ0 -= rowsize;
roentgen b75cab
			op0 += rowsize;
roentgen b75cab
		}
roentgen b75cab
		return 1;
roentgen b75cab
	} else
roentgen b75cab
		return 0;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
horDiff8(TIFF* tif, uint8* cp0, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
	TIFFPredictorState* sp = PredictorState(tif);
roentgen b75cab
	tmsize_t stride = sp->stride;
roentgen b75cab
	char* cp = (char*) cp0;
roentgen b75cab
roentgen b75cab
	assert((cc%stride)==0);
roentgen b75cab
roentgen b75cab
	if (cc > stride) {
roentgen b75cab
		cc -= stride;
roentgen b75cab
		/*
roentgen b75cab
		 * Pipeline the most common cases.
roentgen b75cab
		 */
roentgen b75cab
		if (stride == 3) {
roentgen b75cab
			int r1, g1, b1;
roentgen b75cab
			int r2 = cp[0];
roentgen b75cab
			int g2 = cp[1];
roentgen b75cab
			int b2 = cp[2];
roentgen b75cab
			do {
roentgen b75cab
				r1 = cp[3]; cp[3] = r1-r2; r2 = r1;
roentgen b75cab
				g1 = cp[4]; cp[4] = g1-g2; g2 = g1;
roentgen b75cab
				b1 = cp[5]; cp[5] = b1-b2; b2 = b1;
roentgen b75cab
				cp += 3;
roentgen b75cab
			} while ((cc -= 3) > 0);
roentgen b75cab
		} else if (stride == 4) {
roentgen b75cab
			int r1, g1, b1, a1;
roentgen b75cab
			int r2 = cp[0];
roentgen b75cab
			int g2 = cp[1];
roentgen b75cab
			int b2 = cp[2];
roentgen b75cab
			int a2 = cp[3];
roentgen b75cab
			do {
roentgen b75cab
				r1 = cp[4]; cp[4] = r1-r2; r2 = r1;
roentgen b75cab
				g1 = cp[5]; cp[5] = g1-g2; g2 = g1;
roentgen b75cab
				b1 = cp[6]; cp[6] = b1-b2; b2 = b1;
roentgen b75cab
				a1 = cp[7]; cp[7] = a1-a2; a2 = a1;
roentgen b75cab
				cp += 4;
roentgen b75cab
			} while ((cc -= 4) > 0);
roentgen b75cab
		} else {
roentgen b75cab
			cp += cc - 1;
roentgen b75cab
			do {
roentgen b75cab
				REPEAT4(stride, cp[stride] -= cp[0]; cp--)
roentgen b75cab
			} while ((cc -= stride) > 0);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
horDiff16(TIFF* tif, uint8* cp0, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
	TIFFPredictorState* sp = PredictorState(tif);
roentgen b75cab
	tmsize_t stride = sp->stride;
roentgen b75cab
	int16 *wp = (int16*) cp0;
roentgen b75cab
	tmsize_t wc = cc/2;
roentgen b75cab
roentgen b75cab
	assert((cc%(2*stride))==0);
roentgen b75cab
roentgen b75cab
	if (wc > stride) {
roentgen b75cab
		wc -= stride;
roentgen b75cab
		wp += wc - 1;
roentgen b75cab
		do {
roentgen b75cab
			REPEAT4(stride, wp[stride] -= wp[0]; wp--)
roentgen b75cab
			wc -= stride;
roentgen b75cab
		} while (wc > 0);
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
horDiff32(TIFF* tif, uint8* cp0, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
	TIFFPredictorState* sp = PredictorState(tif);
roentgen b75cab
	tmsize_t stride = sp->stride;
roentgen b75cab
	int32 *wp = (int32*) cp0;
roentgen b75cab
	tmsize_t wc = cc/4;
roentgen b75cab
roentgen b75cab
	assert((cc%(4*stride))==0);
roentgen b75cab
roentgen b75cab
	if (wc > stride) {
roentgen b75cab
		wc -= stride;
roentgen b75cab
		wp += wc - 1;
roentgen b75cab
		do {
roentgen b75cab
			REPEAT4(stride, wp[stride] -= wp[0]; wp--)
roentgen b75cab
			wc -= stride;
roentgen b75cab
		} while (wc > 0);
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Floating point predictor differencing routine.
roentgen b75cab
 */
roentgen b75cab
static void
roentgen b75cab
fpDiff(TIFF* tif, uint8* cp0, tmsize_t cc)
roentgen b75cab
{
roentgen b75cab
	tmsize_t stride = PredictorState(tif)->stride;
roentgen b75cab
	uint32 bps = tif->tif_dir.td_bitspersample / 8;
roentgen b75cab
	tmsize_t wc = cc / bps;
roentgen b75cab
	tmsize_t count;
roentgen b75cab
	uint8 *cp = (uint8 *) cp0;
roentgen b75cab
	uint8 *tmp = (uint8 *)_TIFFmalloc(cc);
roentgen b75cab
roentgen b75cab
	assert((cc%(bps*stride))==0);
roentgen b75cab
roentgen b75cab
	if (!tmp)
roentgen b75cab
		return;
roentgen b75cab
roentgen b75cab
	_TIFFmemcpy(tmp, cp0, cc);
roentgen b75cab
	for (count = 0; count < wc; count++) {
roentgen b75cab
		uint32 byte;
roentgen b75cab
		for (byte = 0; byte < bps; byte++) {
roentgen b75cab
			#if WORDS_BIGENDIAN
roentgen b75cab
			cp[byte * wc + count] = tmp[bps * count + byte];
roentgen b75cab
			#else
roentgen b75cab
			cp[(bps - byte - 1) * wc + count] =
roentgen b75cab
				tmp[bps * count + byte];
roentgen b75cab
			#endif
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	_TIFFfree(tmp);
roentgen b75cab
roentgen b75cab
	cp = (uint8 *) cp0;
roentgen b75cab
	cp += cc - stride - 1;
roentgen b75cab
	for (count = cc; count > stride; count -= stride)
roentgen b75cab
		REPEAT4(stride, cp[stride] -= cp[0]; cp--)
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
PredictorEncodeRow(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
roentgen b75cab
{
roentgen b75cab
	TIFFPredictorState *sp = PredictorState(tif);
roentgen b75cab
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
	assert(sp->encodepfunc != NULL);
roentgen b75cab
	assert(sp->encoderow != NULL);
roentgen b75cab
roentgen b75cab
	/* XXX horizontal differencing alters user's data XXX */
roentgen b75cab
	(*sp->encodepfunc)(tif, bp, cc);
roentgen b75cab
	return (*sp->encoderow)(tif, bp, cc, s);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
PredictorEncodeTile(TIFF* tif, uint8* bp0, tmsize_t cc0, uint16 s)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "PredictorEncodeTile";
roentgen b75cab
	TIFFPredictorState *sp = PredictorState(tif);
roentgen b75cab
        uint8 *working_copy;
roentgen b75cab
	tmsize_t cc = cc0, rowsize;
roentgen b75cab
	unsigned char* bp;
roentgen b75cab
        int result_code;
roentgen b75cab
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
	assert(sp->encodepfunc != NULL);
roentgen b75cab
	assert(sp->encodetile != NULL);
roentgen b75cab
roentgen b75cab
        /* 
roentgen b75cab
         * Do predictor manipulation in a working buffer to avoid altering
roentgen b75cab
         * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965
roentgen b75cab
         */
roentgen b75cab
        working_copy = (uint8*) _TIFFmalloc(cc0);
roentgen b75cab
        if( working_copy == NULL )
roentgen b75cab
        {
roentgen b75cab
            TIFFErrorExt(tif->tif_clientdata, module, 
roentgen b75cab
                         "Out of memory allocating " TIFF_SSIZE_FORMAT " byte temp buffer.",
roentgen b75cab
                         cc0 );
roentgen b75cab
            return 0;
roentgen b75cab
        }
roentgen b75cab
        memcpy( working_copy, bp0, cc0 );
roentgen b75cab
        bp = working_copy;
roentgen b75cab
roentgen b75cab
	rowsize = sp->rowsize;
roentgen b75cab
	assert(rowsize > 0);
roentgen b75cab
	assert((cc0%rowsize)==0);
roentgen b75cab
	while (cc > 0) {
roentgen b75cab
		(*sp->encodepfunc)(tif, bp, rowsize);
roentgen b75cab
		cc -= rowsize;
roentgen b75cab
		bp += rowsize;
roentgen b75cab
	}
roentgen b75cab
	result_code = (*sp->encodetile)(tif, working_copy, cc0, s);
roentgen b75cab
roentgen b75cab
        _TIFFfree( working_copy );
roentgen b75cab
roentgen b75cab
        return result_code;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#define	FIELD_PREDICTOR	(FIELD_CODEC+0)		/* XXX */
roentgen b75cab
roentgen b75cab
static const TIFFField predictFields[] = {
roentgen b75cab
    { TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UINT16, FIELD_PREDICTOR, FALSE, FALSE, "Predictor", NULL },
roentgen b75cab
};
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
PredictorVSetField(TIFF* tif, uint32 tag, va_list ap)
roentgen b75cab
{
roentgen b75cab
	TIFFPredictorState *sp = PredictorState(tif);
roentgen b75cab
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
	assert(sp->vsetparent != NULL);
roentgen b75cab
roentgen b75cab
	switch (tag) {
roentgen b75cab
	case TIFFTAG_PREDICTOR:
roentgen b75cab
		sp->predictor = (uint16) va_arg(ap, uint16_vap);
roentgen b75cab
		TIFFSetFieldBit(tif, FIELD_PREDICTOR);
roentgen b75cab
		break;
roentgen b75cab
	default:
roentgen b75cab
		return (*sp->vsetparent)(tif, tag, ap);
roentgen b75cab
	}
roentgen b75cab
	tif->tif_flags |= TIFF_DIRTYDIRECT;
roentgen b75cab
	return 1;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
PredictorVGetField(TIFF* tif, uint32 tag, va_list ap)
roentgen b75cab
{
roentgen b75cab
	TIFFPredictorState *sp = PredictorState(tif);
roentgen b75cab
roentgen b75cab
	assert(sp != NULL);
roentgen b75cab
	assert(sp->vgetparent != NULL);
roentgen b75cab
roentgen b75cab
	switch (tag) {
roentgen b75cab
	case TIFFTAG_PREDICTOR:
roentgen b75cab
		*va_arg(ap, uint16*) = sp->predictor;
roentgen b75cab
		break;
roentgen b75cab
	default:
roentgen b75cab
		return (*sp->vgetparent)(tif, tag, ap);
roentgen b75cab
	}
roentgen b75cab
	return 1;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static void
roentgen b75cab
PredictorPrintDir(TIFF* tif, FILE* fd, long flags)
roentgen b75cab
{
roentgen b75cab
	TIFFPredictorState* sp = PredictorState(tif);
roentgen b75cab
roentgen b75cab
	(void) flags;
roentgen b75cab
	if (TIFFFieldSet(tif,FIELD_PREDICTOR)) {
roentgen b75cab
		fprintf(fd, "  Predictor: ");
roentgen b75cab
		switch (sp->predictor) {
roentgen b75cab
			case 1: fprintf(fd, "none "); break;
roentgen b75cab
			case 2: fprintf(fd, "horizontal differencing "); break;
roentgen b75cab
			case 3: fprintf(fd, "floating point predictor "); break;
roentgen b75cab
		}
roentgen b75cab
		fprintf(fd, "%u (0x%x)\n", sp->predictor, sp->predictor);
roentgen b75cab
	}
roentgen b75cab
	if (sp->printdir)
roentgen b75cab
		(*sp->printdir)(tif, fd, flags);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
TIFFPredictorInit(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	TIFFPredictorState* sp = PredictorState(tif);
roentgen b75cab
roentgen b75cab
	assert(sp != 0);
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * Merge codec-specific tag information.
roentgen b75cab
	 */
roentgen b75cab
	if (!_TIFFMergeFields(tif, predictFields,
roentgen b75cab
			      TIFFArrayCount(predictFields))) {
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, "TIFFPredictorInit",
roentgen b75cab
		    "Merging Predictor codec-specific tags failed");
roentgen b75cab
		return 0;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * Override parent get/set field methods.
roentgen b75cab
	 */
roentgen b75cab
	sp->vgetparent = tif->tif_tagmethods.vgetfield;
roentgen b75cab
	tif->tif_tagmethods.vgetfield =
roentgen b75cab
            PredictorVGetField;/* hook for predictor tag */
roentgen b75cab
	sp->vsetparent = tif->tif_tagmethods.vsetfield;
roentgen b75cab
	tif->tif_tagmethods.vsetfield =
roentgen b75cab
	    PredictorVSetField;/* hook for predictor tag */
roentgen b75cab
	sp->printdir = tif->tif_tagmethods.printdir;
roentgen b75cab
	tif->tif_tagmethods.printdir =
roentgen b75cab
            PredictorPrintDir;	/* hook for predictor tag */
roentgen b75cab
roentgen b75cab
	sp->setupdecode = tif->tif_setupdecode;
roentgen b75cab
	tif->tif_setupdecode = PredictorSetupDecode;
roentgen b75cab
	sp->setupencode = tif->tif_setupencode;
roentgen b75cab
	tif->tif_setupencode = PredictorSetupEncode;
roentgen b75cab
roentgen b75cab
	sp->predictor = 1;			/* default value */
roentgen b75cab
	sp->encodepfunc = NULL;			/* no predictor routine */
roentgen b75cab
	sp->decodepfunc = NULL;			/* no predictor routine */
roentgen b75cab
	return 1;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
TIFFPredictorCleanup(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	TIFFPredictorState* sp = PredictorState(tif);
roentgen b75cab
roentgen b75cab
	assert(sp != 0);
roentgen b75cab
roentgen b75cab
	tif->tif_tagmethods.vgetfield = sp->vgetparent;
roentgen b75cab
	tif->tif_tagmethods.vsetfield = sp->vsetparent;
roentgen b75cab
	tif->tif_tagmethods.printdir = sp->printdir;
roentgen b75cab
	tif->tif_setupdecode = sp->setupdecode;
roentgen b75cab
	tif->tif_setupencode = sp->setupencode;
roentgen b75cab
roentgen b75cab
	return 1;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/* vim: set ts=8 sts=8 sw=8 noet: */
roentgen b75cab
/*
roentgen b75cab
 * Local Variables:
roentgen b75cab
 * mode: c
roentgen b75cab
 * c-basic-offset: 8
roentgen b75cab
 * fill-column: 78
roentgen b75cab
 * End:
roentgen b75cab
 */