diff options
Diffstat (limited to 'src/libtiff/tif_predict.c')
-rw-r--r-- | src/libtiff/tif_predict.c | 219 |
1 files changed, 161 insertions, 58 deletions
diff --git a/src/libtiff/tif_predict.c b/src/libtiff/tif_predict.c index e738ca3..ca5c091 100644 --- a/src/libtiff/tif_predict.c +++ b/src/libtiff/tif_predict.c @@ -1,4 +1,4 @@ -/* $Id: tif_predict.c,v 1.1 2008/10/17 06:16:07 scuri Exp $ */ +/* $Id: tif_predict.c,v 1.2 2009/08/21 04:01:59 scuri Exp $ */ /* * Copyright (c) 1988-1997 Sam Leffler @@ -36,9 +36,12 @@ static void horAcc8(TIFF*, tidata_t, tsize_t); static void horAcc16(TIFF*, tidata_t, tsize_t); +static void horAcc32(TIFF*, tidata_t, tsize_t); static void swabHorAcc16(TIFF*, tidata_t, tsize_t); +static void swabHorAcc32(TIFF*, tidata_t, tsize_t); static void horDiff8(TIFF*, tidata_t, tsize_t); static void horDiff16(TIFF*, tidata_t, tsize_t); +static void horDiff32(TIFF*, tidata_t, tsize_t); static void fpAcc(TIFF*, tidata_t, tsize_t); static void fpDiff(TIFF*, tidata_t, tsize_t); static int PredictorDecodeRow(TIFF*, tidata_t, tsize_t, tsample_t); @@ -60,7 +63,8 @@ PredictorSetup(TIFF* tif) return 1; case PREDICTOR_HORIZONTAL: if (td->td_bitspersample != 8 - && td->td_bitspersample != 16) { + && td->td_bitspersample != 16 + && td->td_bitspersample != 32) { TIFFErrorExt(tif->tif_clientdata, module, "Horizontal differencing \"Predictor\" not supported with %d-bit samples", td->td_bitspersample); @@ -105,19 +109,23 @@ PredictorSetupDecode(TIFF* tif) if (sp->predictor == 2) { switch (td->td_bitspersample) { - case 8: sp->pfunc = horAcc8; break; - case 16: sp->pfunc = horAcc16; break; + case 8: sp->decodepfunc = horAcc8; break; + case 16: sp->decodepfunc = horAcc16; break; + case 32: sp->decodepfunc = horAcc32; break; } /* * Override default decoding method with one that does the * predictor stuff. */ - sp->coderow = tif->tif_decoderow; - tif->tif_decoderow = PredictorDecodeRow; - sp->codestrip = tif->tif_decodestrip; - tif->tif_decodestrip = PredictorDecodeTile; - sp->codetile = tif->tif_decodetile; - tif->tif_decodetile = PredictorDecodeTile; + if( tif->tif_decoderow != PredictorDecodeRow ) + { + sp->decoderow = tif->tif_decoderow; + tif->tif_decoderow = PredictorDecodeRow; + sp->decodestrip = tif->tif_decodestrip; + tif->tif_decodestrip = PredictorDecodeTile; + sp->decodetile = tif->tif_decodetile; + tif->tif_decodetile = PredictorDecodeTile; + } /* * If the data is horizontally differenced 16-bit data that * requires byte-swapping, then it must be byte swapped before @@ -126,25 +134,31 @@ PredictorSetupDecode(TIFF* tif) * the library setup when the directory was read. */ if (tif->tif_flags & TIFF_SWAB) { - if (sp->pfunc == horAcc16) { - sp->pfunc = swabHorAcc16; + if (sp->decodepfunc == horAcc16) { + sp->decodepfunc = swabHorAcc16; tif->tif_postdecode = _TIFFNoPostDecode; - } /* else handle 32-bit case... */ + } else if (sp->decodepfunc == horAcc32) { + sp->decodepfunc = swabHorAcc32; + tif->tif_postdecode = _TIFFNoPostDecode; + } } } else if (sp->predictor == 3) { - sp->pfunc = fpAcc; + sp->decodepfunc = fpAcc; /* * Override default decoding method with one that does the * predictor stuff. */ - sp->coderow = tif->tif_decoderow; - tif->tif_decoderow = PredictorDecodeRow; - sp->codestrip = tif->tif_decodestrip; - tif->tif_decodestrip = PredictorDecodeTile; - sp->codetile = tif->tif_decodetile; - tif->tif_decodetile = PredictorDecodeTile; + if( tif->tif_decoderow != PredictorDecodeRow ) + { + sp->decoderow = tif->tif_decoderow; + tif->tif_decoderow = PredictorDecodeRow; + sp->decodestrip = tif->tif_decodestrip; + tif->tif_decodestrip = PredictorDecodeTile; + sp->decodetile = tif->tif_decodetile; + tif->tif_decodetile = PredictorDecodeTile; + } /* * The data should not be swapped outside of the floating * point predictor, the accumulation routine should return @@ -173,33 +187,40 @@ PredictorSetupEncode(TIFF* tif) if (sp->predictor == 2) { switch (td->td_bitspersample) { - case 8: sp->pfunc = horDiff8; break; - case 16: sp->pfunc = horDiff16; break; + case 8: sp->encodepfunc = horDiff8; break; + case 16: sp->encodepfunc = horDiff16; break; + case 32: sp->encodepfunc = horDiff32; break; } /* * Override default encoding method with one that does the * predictor stuff. */ - sp->coderow = tif->tif_encoderow; - tif->tif_encoderow = PredictorEncodeRow; - sp->codestrip = tif->tif_encodestrip; - tif->tif_encodestrip = PredictorEncodeTile; - sp->codetile = tif->tif_encodetile; - tif->tif_encodetile = PredictorEncodeTile; + if( tif->tif_encoderow != PredictorEncodeRow ) + { + sp->encoderow = tif->tif_encoderow; + tif->tif_encoderow = PredictorEncodeRow; + sp->encodestrip = tif->tif_encodestrip; + tif->tif_encodestrip = PredictorEncodeTile; + sp->encodetile = tif->tif_encodetile; + tif->tif_encodetile = PredictorEncodeTile; + } } else if (sp->predictor == 3) { - sp->pfunc = fpDiff; + sp->encodepfunc = fpDiff; /* * Override default encoding method with one that does the * predictor stuff. */ - sp->coderow = tif->tif_encoderow; - tif->tif_encoderow = PredictorEncodeRow; - sp->codestrip = tif->tif_encodestrip; - tif->tif_encodestrip = PredictorEncodeTile; - sp->codetile = tif->tif_encodetile; - tif->tif_encodetile = PredictorEncodeTile; + if( tif->tif_encoderow != PredictorEncodeRow ) + { + sp->encoderow = tif->tif_encoderow; + tif->tif_encoderow = PredictorEncodeRow; + sp->encodestrip = tif->tif_encodestrip; + tif->tif_encodestrip = PredictorEncodeTile; + sp->encodetile = tif->tif_encodetile; + tif->tif_encodetile = PredictorEncodeTile; + } } return 1; @@ -291,6 +312,39 @@ horAcc16(TIFF* tif, tidata_t cp0, tsize_t cc) } } +static void +swabHorAcc32(TIFF* tif, tidata_t cp0, tsize_t cc) +{ + tsize_t stride = PredictorState(tif)->stride; + uint32* wp = (uint32*) cp0; + tsize_t wc = cc / 4; + + if (wc > stride) { + TIFFSwabArrayOfLong(wp, wc); + wc -= stride; + do { + REPEAT4(stride, wp[stride] += wp[0]; wp++) + wc -= stride; + } while ((int32) wc > 0); + } +} + +static void +horAcc32(TIFF* tif, tidata_t cp0, tsize_t cc) +{ + tsize_t stride = PredictorState(tif)->stride; + uint32* wp = (uint32*) cp0; + tsize_t wc = cc / 4; + + if (wc > stride) { + wc -= stride; + do { + REPEAT4(stride, wp[stride] += wp[0]; wp++) + wc -= stride; + } while ((int32) wc > 0); + } +} + /* * Floating point predictor accumulation routine. */ @@ -337,11 +391,11 @@ PredictorDecodeRow(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s) TIFFPredictorState *sp = PredictorState(tif); assert(sp != NULL); - assert(sp->coderow != NULL); - assert(sp->pfunc != NULL); + assert(sp->decoderow != NULL); + assert(sp->decodepfunc != NULL); - if ((*sp->coderow)(tif, op0, occ0, s)) { - (*sp->pfunc)(tif, op0, occ0); + if ((*sp->decoderow)(tif, op0, occ0, s)) { + (*sp->decodepfunc)(tif, op0, occ0); return 1; } else return 0; @@ -360,14 +414,14 @@ PredictorDecodeTile(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s) TIFFPredictorState *sp = PredictorState(tif); assert(sp != NULL); - assert(sp->codetile != NULL); + assert(sp->decodetile != NULL); - if ((*sp->codetile)(tif, op0, occ0, s)) { + if ((*sp->decodetile)(tif, op0, occ0, s)) { tsize_t rowsize = sp->rowsize; assert(rowsize > 0); - assert(sp->pfunc != NULL); + assert(sp->decodepfunc != NULL); while ((long)occ0 > 0) { - (*sp->pfunc)(tif, op0, (tsize_t) rowsize); + (*sp->decodepfunc)(tif, op0, (tsize_t) rowsize); occ0 -= rowsize; op0 += rowsize; } @@ -439,6 +493,24 @@ horDiff16(TIFF* tif, tidata_t cp0, tsize_t cc) } } +static void +horDiff32(TIFF* tif, tidata_t cp0, tsize_t cc) +{ + TIFFPredictorState* sp = PredictorState(tif); + tsize_t stride = sp->stride; + int32 *wp = (int32*) cp0; + tsize_t wc = cc/4; + + if (wc > stride) { + wc -= stride; + wp += wc - 1; + do { + REPEAT4(stride, wp[stride] -= wp[0]; wp--) + wc -= stride; + } while ((int32) wc > 0); + } +} + /* * Floating point predictor differencing routine. */ @@ -481,33 +553,56 @@ PredictorEncodeRow(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s) TIFFPredictorState *sp = PredictorState(tif); assert(sp != NULL); - assert(sp->pfunc != NULL); - assert(sp->coderow != NULL); + assert(sp->encodepfunc != NULL); + assert(sp->encoderow != NULL); /* XXX horizontal differencing alters user's data XXX */ - (*sp->pfunc)(tif, bp, cc); - return (*sp->coderow)(tif, bp, cc, s); + (*sp->encodepfunc)(tif, bp, cc); + return (*sp->encoderow)(tif, bp, cc, s); } static int PredictorEncodeTile(TIFF* tif, tidata_t bp0, tsize_t cc0, tsample_t s) { + static const char module[] = "PredictorEncodeTile"; TIFFPredictorState *sp = PredictorState(tif); + uint8 *working_copy; tsize_t cc = cc0, rowsize; - unsigned char* bp = bp0; + unsigned char* bp; + int result_code; assert(sp != NULL); - assert(sp->pfunc != NULL); - assert(sp->codetile != NULL); + assert(sp->encodepfunc != NULL); + assert(sp->encodetile != NULL); + + /* + * Do predictor manipulation in a working buffer to avoid altering + * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965 + */ + working_copy = (uint8*) _TIFFmalloc(cc0); + if( working_copy == NULL ) + { + TIFFErrorExt(tif->tif_clientdata, module, + "Out of memory allocating %d byte temp buffer.", + cc0 ); + return 0; + } + memcpy( working_copy, bp0, cc0 ); + bp = working_copy; rowsize = sp->rowsize; assert(rowsize > 0); - while ((long)cc > 0) { - (*sp->pfunc)(tif, bp, (tsize_t) rowsize); + assert((cc0%rowsize)==0); + while (cc > 0) { + (*sp->encodepfunc)(tif, bp, rowsize); cc -= rowsize; bp += rowsize; } - return (*sp->codetile)(tif, bp0, cc0, s); + result_code = (*sp->encodetile)(tif, working_copy, cc0, s); + + _TIFFfree( working_copy ); + + return result_code; } #define FIELD_PREDICTOR (FIELD_CODEC+0) /* XXX */ @@ -516,7 +611,6 @@ static const TIFFFieldInfo predictFieldInfo[] = { { TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, FIELD_PREDICTOR, FALSE, FALSE, "Predictor" }, }; -#define N(a) (sizeof (a) / sizeof (a[0])) static int PredictorVSetField(TIFF* tif, ttag_t tag, va_list ap) @@ -583,10 +677,18 @@ TIFFPredictorInit(TIFF* tif) assert(sp != 0); /* - * Merge codec-specific tag information and - * override parent get/set field methods. + * Merge codec-specific tag information. + */ + if (!_TIFFMergeFieldInfo(tif, predictFieldInfo, + TIFFArrayCount(predictFieldInfo))) { + TIFFErrorExt(tif->tif_clientdata, "TIFFPredictorInit", + "Merging Predictor codec-specific tags failed"); + return 0; + } + + /* + * Override parent get/set field methods. */ - _TIFFMergeFieldInfo(tif, predictFieldInfo, N(predictFieldInfo)); sp->vgetparent = tif->tif_tagmethods.vgetfield; tif->tif_tagmethods.vgetfield = PredictorVGetField;/* hook for predictor tag */ @@ -603,7 +705,8 @@ TIFFPredictorInit(TIFF* tif) tif->tif_setupencode = PredictorSetupEncode; sp->predictor = 1; /* default value */ - sp->pfunc = NULL; /* no predictor routine */ + sp->encodepfunc = NULL; /* no predictor routine */ + sp->decodepfunc = NULL; /* no predictor routine */ return 1; } |