]> git.saurik.com Git - wxWidgets.git/blobdiff - src/tiff/libtiff/tif_predict.c
Reflect changes in stc.cpp in stc.cpp.in from which it's generated.
[wxWidgets.git] / src / tiff / libtiff / tif_predict.c
index b4a7dfbe690629427c7503271c85187c086a7240..a29e02c644f1aa1ca431846ce12ab363cc2cd28f 100644 (file)
@@ -1,4 +1,3 @@
-/* $Id$ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
 
 #define        PredictorState(tif)     ((TIFFPredictorState*) (tif)->tif_data)
 
-static void horAcc8(TIFF*, tidata_t, tsize_t);
-static void horAcc16(TIFF*, tidata_t, tsize_t);
-static void swabHorAcc16(TIFF*, tidata_t, tsize_t);
-static void horDiff8(TIFF*, tidata_t, tsize_t);
-static void horDiff16(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);
-static int PredictorDecodeTile(TIFF*, tidata_t, tsize_t, tsample_t);
-static int PredictorEncodeRow(TIFF*, tidata_t, tsize_t, tsample_t);
-static int PredictorEncodeTile(TIFF*, tidata_t, tsize_t, tsample_t);
+static void horAcc8(TIFF* tif, uint8* cp0, tmsize_t cc);
+static void horAcc16(TIFF* tif, uint8* cp0, tmsize_t cc);
+static void horAcc32(TIFF* tif, uint8* cp0, tmsize_t cc);
+static void swabHorAcc16(TIFF* tif, uint8* cp0, tmsize_t cc);
+static void swabHorAcc32(TIFF* tif, uint8* cp0, tmsize_t cc);
+static void horDiff8(TIFF* tif, uint8* cp0, tmsize_t cc);
+static void horDiff16(TIFF* tif, uint8* cp0, tmsize_t cc);
+static void horDiff32(TIFF* tif, uint8* cp0, tmsize_t cc);
+static void fpAcc(TIFF* tif, uint8* cp0, tmsize_t cc);
+static void fpDiff(TIFF* tif, uint8* cp0, tmsize_t cc);
+static int PredictorDecodeRow(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
+static int PredictorDecodeTile(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
+static int PredictorEncodeRow(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
+static int PredictorEncodeTile(TIFF* tif, uint8* bp0, tmsize_t cc0, uint16 s);
 
 static int
 PredictorSetup(TIFF* tif)
@@ -60,25 +62,26 @@ 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);
+                                   "Horizontal differencing \"Predictor\" not supported with %d-bit samples",
+                                   td->td_bitspersample);
                                return 0;
                        }
                        break;
                case PREDICTOR_FLOATINGPOINT:
                        if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP) {
                                TIFFErrorExt(tif->tif_clientdata, module,
-       "Floating point \"Predictor\" not supported with %d data format",
-                                         td->td_sampleformat);
+                                   "Floating point \"Predictor\" not supported with %d data format",
+                                   td->td_sampleformat);
                                return 0;
                        }
                        break;
                default:
                        TIFFErrorExt(tif->tif_clientdata, module,
-                                 "\"Predictor\" value %d not supported",
-                                 sp->predictor);
+                           "\"Predictor\" value %d not supported",
+                           sp->predictor);
                        return 0;
        }
        sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
@@ -90,6 +93,8 @@ PredictorSetup(TIFF* tif)
                sp->rowsize = TIFFTileRowSize(tif);
        else
                sp->rowsize = TIFFScanlineSize(tif);
+       if (sp->rowsize == 0)
+               return 0;
 
        return 1;
 }
@@ -105,19 +110,24 @@ 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 +136,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 +189,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;
@@ -207,7 +230,7 @@ PredictorSetupEncode(TIFF* tif)
 
 #define REPEAT4(n, op)         \
     switch (n) {               \
-    default: { int i; for (i = n-4; i > 0; i--) { op; } } \
+    default: { tmsize_t i; for (i = n-4; i > 0; i--) { op; } } \
     case 4:  op;               \
     case 3:  op;               \
     case 2:  op;               \
@@ -216,13 +239,13 @@ PredictorSetupEncode(TIFF* tif)
     }
 
 static void
-horAcc8(TIFF* tif, tidata_t cp0, tsize_t cc)
+horAcc8(TIFF* tif, uint8* cp0, tmsize_t cc)
 {
-       tsize_t stride = PredictorState(tif)->stride;
+       tmsize_t stride = PredictorState(tif)->stride;
 
        char* cp = (char*) cp0;
+       assert((cc%stride)==0);
        if (cc > stride) {
-               cc -= stride;
                /*
                 * Pipeline the most common cases.
                 */
@@ -230,40 +253,49 @@ horAcc8(TIFF* tif, tidata_t cp0, tsize_t cc)
                        unsigned int cr = cp[0];
                        unsigned int cg = cp[1];
                        unsigned int cb = cp[2];
-                       do {
-                               cc -= 3, cp += 3;
+                       cc -= 3;
+                       cp += 3;
+                       while (cc>0) {
                                cp[0] = (char) (cr += cp[0]);
                                cp[1] = (char) (cg += cp[1]);
                                cp[2] = (char) (cb += cp[2]);
-                       } while ((int32) cc > 0);
+                               cc -= 3;
+                               cp += 3;
+                       }
                } else if (stride == 4)  {
                        unsigned int cr = cp[0];
                        unsigned int cg = cp[1];
                        unsigned int cb = cp[2];
                        unsigned int ca = cp[3];
-                       do {
-                               cc -= 4, cp += 4;
+                       cc -= 4;
+                       cp += 4;
+                       while (cc>0) {
                                cp[0] = (char) (cr += cp[0]);
                                cp[1] = (char) (cg += cp[1]);
                                cp[2] = (char) (cb += cp[2]);
                                cp[3] = (char) (ca += cp[3]);
-                       } while ((int32) cc > 0);
+                               cc -= 4;
+                               cp += 4;
+                       }
                } else  {
+                       cc -= stride;
                        do {
                                REPEAT4(stride, cp[stride] =
                                        (char) (cp[stride] + *cp); cp++)
                                cc -= stride;
-                       } while ((int32) cc > 0);
+                       } while (cc>0);
                }
        }
 }
 
 static void
-swabHorAcc16(TIFF* tif, tidata_t cp0, tsize_t cc)
+swabHorAcc16(TIFF* tif, uint8* cp0, tmsize_t cc)
 {
-       tsize_t stride = PredictorState(tif)->stride;
+       tmsize_t stride = PredictorState(tif)->stride;
        uint16* wp = (uint16*) cp0;
-       tsize_t wc = cc / 2;
+       tmsize_t wc = cc / 2;
+
+       assert((cc%(2*stride))==0);
 
        if (wc > stride) {
                TIFFSwabArrayOfShort(wp, wc);
@@ -271,23 +303,62 @@ swabHorAcc16(TIFF* tif, tidata_t cp0, tsize_t cc)
                do {
                        REPEAT4(stride, wp[stride] += wp[0]; wp++)
                        wc -= stride;
-               } while ((int32) wc > 0);
+               } while (wc > 0);
        }
 }
 
 static void
-horAcc16(TIFF* tif, tidata_t cp0, tsize_t cc)
+horAcc16(TIFF* tif, uint8* cp0, tmsize_t cc)
 {
-       tsize_t stride = PredictorState(tif)->stride;
+       tmsize_t stride = PredictorState(tif)->stride;
        uint16* wp = (uint16*) cp0;
-       tsize_t wc = cc / 2;
+       tmsize_t wc = cc / 2;
+
+       assert((cc%(2*stride))==0);
+
+       if (wc > stride) {
+               wc -= stride;
+               do {
+                       REPEAT4(stride, wp[stride] += wp[0]; wp++)
+                       wc -= stride;
+               } while (wc > 0);
+       }
+}
+
+static void
+swabHorAcc32(TIFF* tif, uint8* cp0, tmsize_t cc)
+{
+       tmsize_t stride = PredictorState(tif)->stride;
+       uint32* wp = (uint32*) cp0;
+       tmsize_t wc = cc / 4;
+
+       assert((cc%(4*stride))==0);
+
+       if (wc > stride) {
+               TIFFSwabArrayOfLong(wp, wc);
+               wc -= stride;
+               do {
+                       REPEAT4(stride, wp[stride] += wp[0]; wp++)
+                       wc -= stride;
+               } while (wc > 0);
+       }
+}
+
+static void
+horAcc32(TIFF* tif, uint8* cp0, tmsize_t cc)
+{
+       tmsize_t stride = PredictorState(tif)->stride;
+       uint32* wp = (uint32*) cp0;
+       tmsize_t wc = cc / 4;
+
+       assert((cc%(4*stride))==0);
 
        if (wc > stride) {
                wc -= stride;
                do {
                        REPEAT4(stride, wp[stride] += wp[0]; wp++)
                        wc -= stride;
-               } while ((int32) wc > 0);
+               } while (wc > 0);
        }
 }
 
@@ -295,15 +366,17 @@ horAcc16(TIFF* tif, tidata_t cp0, tsize_t cc)
  * Floating point predictor accumulation routine.
  */
 static void
-fpAcc(TIFF* tif, tidata_t cp0, tsize_t cc)
+fpAcc(TIFF* tif, uint8* cp0, tmsize_t cc)
 {
-       tsize_t stride = PredictorState(tif)->stride;
+       tmsize_t stride = PredictorState(tif)->stride;
        uint32 bps = tif->tif_dir.td_bitspersample / 8;
-       tsize_t wc = cc / bps;
-       tsize_t count = cc;
+       tmsize_t wc = cc / bps;
+       tmsize_t count = cc;
        uint8 *cp = (uint8 *) cp0;
        uint8 *tmp = (uint8 *)_TIFFmalloc(cc);
 
+       assert((cc%(bps*stride))==0);
+
        if (!tmp)
                return;
 
@@ -317,12 +390,12 @@ fpAcc(TIFF* tif, tidata_t cp0, tsize_t cc)
        for (count = 0; count < wc; count++) {
                uint32 byte;
                for (byte = 0; byte < bps; byte++) {
-#ifdef WORDS_BIGENDIAN
+                       #ifdef WORDS_BIGENDIAN
                        cp[bps * count + byte] = tmp[byte * wc + count];
-#else
+                       #else
                        cp[bps * count + byte] =
                                tmp[(bps - byte - 1) * wc + count];
-#endif
+                       #endif
                }
        }
        _TIFFfree(tmp);
@@ -332,16 +405,16 @@ fpAcc(TIFF* tif, tidata_t cp0, tsize_t cc)
  * Decode a scanline and apply the predictor routine.
  */
 static int
-PredictorDecodeRow(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
+PredictorDecodeRow(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 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;
@@ -355,19 +428,20 @@ PredictorDecodeRow(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
  * strip/tile dimensions.
  */
 static int
-PredictorDecodeTile(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
+PredictorDecodeTile(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
 {
        TIFFPredictorState *sp = PredictorState(tif);
 
        assert(sp != NULL);
-       assert(sp->codetile != NULL);
+       assert(sp->decodetile != NULL);
 
-       if ((*sp->codetile)(tif, op0, occ0, s)) {
-               tsize_t rowsize = sp->rowsize;
+       if ((*sp->decodetile)(tif, op0, occ0, s)) {
+               tmsize_t rowsize = sp->rowsize;
                assert(rowsize > 0);
-               assert(sp->pfunc != NULL);
-               while ((long)occ0 > 0) {
-                       (*sp->pfunc)(tif, op0, (tsize_t) rowsize);
+               assert((occ0%rowsize)==0);
+               assert(sp->decodepfunc != NULL);
+               while (occ0 > 0) {
+                       (*sp->decodepfunc)(tif, op0, rowsize);
                        occ0 -= rowsize;
                        op0 += rowsize;
                }
@@ -377,12 +451,14 @@ PredictorDecodeTile(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
 }
 
 static void
-horDiff8(TIFF* tif, tidata_t cp0, tsize_t cc)
+horDiff8(TIFF* tif, uint8* cp0, tmsize_t cc)
 {
        TIFFPredictorState* sp = PredictorState(tif);
-       tsize_t stride = sp->stride;
+       tmsize_t stride = sp->stride;
        char* cp = (char*) cp0;
 
+       assert((cc%stride)==0);
+
        if (cc > stride) {
                cc -= stride;
                /*
@@ -398,7 +474,7 @@ horDiff8(TIFF* tif, tidata_t cp0, tsize_t cc)
                                g1 = cp[4]; cp[4] = g1-g2; g2 = g1;
                                b1 = cp[5]; cp[5] = b1-b2; b2 = b1;
                                cp += 3;
-                       } while ((int32)(cc -= 3) > 0);
+                       } while ((cc -= 3) > 0);
                } else if (stride == 4) {
                        int r1, g1, b1, a1;
                        int r2 = cp[0];
@@ -411,23 +487,45 @@ horDiff8(TIFF* tif, tidata_t cp0, tsize_t cc)
                                b1 = cp[6]; cp[6] = b1-b2; b2 = b1;
                                a1 = cp[7]; cp[7] = a1-a2; a2 = a1;
                                cp += 4;
-                       } while ((int32)(cc -= 4) > 0);
+                       } while ((cc -= 4) > 0);
                } else {
                        cp += cc - 1;
                        do {
                                REPEAT4(stride, cp[stride] -= cp[0]; cp--)
-                       } while ((int32)(cc -= stride) > 0);
+                       } while ((cc -= stride) > 0);
                }
        }
 }
 
 static void
-horDiff16(TIFF* tif, tidata_t cp0, tsize_t cc)
+horDiff16(TIFF* tif, uint8* cp0, tmsize_t cc)
 {
        TIFFPredictorState* sp = PredictorState(tif);
-       tsize_t stride = sp->stride;
+       tmsize_t stride = sp->stride;
        int16 *wp = (int16*) cp0;
-       tsize_t wc = cc/2;
+       tmsize_t wc = cc/2;
+
+       assert((cc%(2*stride))==0);
+
+       if (wc > stride) {
+               wc -= stride;
+               wp += wc - 1;
+               do {
+                       REPEAT4(stride, wp[stride] -= wp[0]; wp--)
+                       wc -= stride;
+               } while (wc > 0);
+       }
+}
+
+static void
+horDiff32(TIFF* tif, uint8* cp0, tmsize_t cc)
+{
+       TIFFPredictorState* sp = PredictorState(tif);
+       tmsize_t stride = sp->stride;
+       int32 *wp = (int32*) cp0;
+       tmsize_t wc = cc/4;
+
+       assert((cc%(4*stride))==0);
 
        if (wc > stride) {
                wc -= stride;
@@ -435,7 +533,7 @@ horDiff16(TIFF* tif, tidata_t cp0, tsize_t cc)
                do {
                        REPEAT4(stride, wp[stride] -= wp[0]; wp--)
                        wc -= stride;
-               } while ((int32) wc > 0);
+               } while (wc > 0);
        }
 }
 
@@ -443,15 +541,17 @@ horDiff16(TIFF* tif, tidata_t cp0, tsize_t cc)
  * Floating point predictor differencing routine.
  */
 static void
-fpDiff(TIFF* tif, tidata_t cp0, tsize_t cc)
+fpDiff(TIFF* tif, uint8* cp0, tmsize_t cc)
 {
-       tsize_t stride = PredictorState(tif)->stride;
+       tmsize_t stride = PredictorState(tif)->stride;
        uint32 bps = tif->tif_dir.td_bitspersample / 8;
-       tsize_t wc = cc / bps;
-       tsize_t count;
+       tmsize_t wc = cc / bps;
+       tmsize_t count;
        uint8 *cp = (uint8 *) cp0;
        uint8 *tmp = (uint8 *)_TIFFmalloc(cc);
 
+       assert((cc%(bps*stride))==0);
+
        if (!tmp)
                return;
 
@@ -459,12 +559,12 @@ fpDiff(TIFF* tif, tidata_t cp0, tsize_t cc)
        for (count = 0; count < wc; count++) {
                uint32 byte;
                for (byte = 0; byte < bps; byte++) {
-#ifdef WORDS_BIGENDIAN
-                       cp[byte * wc + count] = tmp[bps * count + byte];
-#else
+                       #ifdef WORDS_BIGENDIAN
+                       cp[byte * wc + count] = tmp[bps * count + byte];
+                       #else
                        cp[(bps - byte - 1) * wc + count] =
                                tmp[bps * count + byte];
-#endif
+                       #endif
                }
        }
        _TIFFfree(tmp);
@@ -476,50 +576,71 @@ fpDiff(TIFF* tif, tidata_t cp0, tsize_t cc)
 }
 
 static int
-PredictorEncodeRow(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
+PredictorEncodeRow(TIFF* tif, uint8* bp, tmsize_t cc, uint16 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)
+PredictorEncodeTile(TIFF* tif, uint8* bp0, tmsize_t cc0, uint16 s)
 {
+       static const char module[] = "PredictorEncodeTile";
        TIFFPredictorState *sp = PredictorState(tif);
-       tsize_t cc = cc0, rowsize;
-       unsigned char* bp = bp0;
+        uint8 *working_copy;
+       tmsize_t cc = cc0, rowsize;
+       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 " TIFF_SSIZE_FORMAT " 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 */
 
-static const TIFFFieldInfo predictFieldInfo[] = {
-    { TIFFTAG_PREDICTOR,        1, 1, TIFF_SHORT,      FIELD_PREDICTOR,
-      FALSE,   FALSE,  "Predictor" },
+static const TIFFField predictFields[] = {
+    { TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UINT16, FIELD_PREDICTOR, FALSE, FALSE, "Predictor", NULL },
 };
-#define        N(a)    (sizeof (a) / sizeof (a[0]))
 
 static int
-PredictorVSetField(TIFF* tif, ttag_t tag, va_list ap)
+PredictorVSetField(TIFF* tif, uint32 tag, va_list ap)
 {
        TIFFPredictorState *sp = PredictorState(tif);
 
@@ -528,7 +649,7 @@ PredictorVSetField(TIFF* tif, ttag_t tag, va_list ap)
 
        switch (tag) {
        case TIFFTAG_PREDICTOR:
-               sp->predictor = (uint16) va_arg(ap, int);
+               sp->predictor = (uint16) va_arg(ap, uint16_vap);
                TIFFSetFieldBit(tif, FIELD_PREDICTOR);
                break;
        default:
@@ -539,7 +660,7 @@ PredictorVSetField(TIFF* tif, ttag_t tag, va_list ap)
 }
 
 static int
-PredictorVGetField(TIFF* tif, ttag_t tag, va_list ap)
+PredictorVGetField(TIFF* tif, uint32 tag, va_list ap)
 {
        TIFFPredictorState *sp = PredictorState(tif);
 
@@ -565,9 +686,9 @@ PredictorPrintDir(TIFF* tif, FILE* fd, long flags)
        if (TIFFFieldSet(tif,FIELD_PREDICTOR)) {
                fprintf(fd, "  Predictor: ");
                switch (sp->predictor) {
-               case 1: fprintf(fd, "none "); break;
-               case 2: fprintf(fd, "horizontal differencing "); break;
-               case 3: fprintf(fd, "floating point predictor "); break;
+                       case 1: fprintf(fd, "none "); break;
+                       case 2: fprintf(fd, "horizontal differencing "); break;
+                       case 3: fprintf(fd, "floating point predictor "); break;
                }
                fprintf(fd, "%u (0x%x)\n", sp->predictor, sp->predictor);
        }
@@ -583,16 +704,24 @@ TIFFPredictorInit(TIFF* tif)
        assert(sp != 0);
 
        /*
-        * Merge codec-specific tag information and
-        * override parent get/set field methods.
+        * Merge codec-specific tag information.
+        */
+       if (!_TIFFMergeFields(tif, predictFields,
+                             TIFFArrayCount(predictFields))) {
+               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 */
        sp->vsetparent = tif->tif_tagmethods.vsetfield;
        tif->tif_tagmethods.vsetfield =
-            PredictorVSetField;/* hook for predictor tag */
+           PredictorVSetField;/* hook for predictor tag */
        sp->printdir = tif->tif_tagmethods.printdir;
        tif->tif_tagmethods.printdir =
             PredictorPrintDir; /* hook for predictor tag */
@@ -603,7 +732,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;
 }
 
@@ -624,3 +754,10 @@ TIFFPredictorCleanup(TIFF* tif)
 }
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */