]> git.saurik.com Git - wxWidgets.git/blobdiff - src/tiff/libtiff/tif_pixarlog.c
Workaround for #15404: wxRichTextCtrl: caret does not disappear when focus is lost...
[wxWidgets.git] / src / tiff / libtiff / tif_pixarlog.c
index 817081d8a945f23d9a238456bd3df400eaefc7a0..e7e7ca82efca2896f2aa811e36c837c99abcaa40 100644 (file)
@@ -1,4 +1,3 @@
-/* $Id$ */
 
 /*
  * Copyright (c) 1996-1997 Sam Leffler
 
 /*
  * Copyright (c) 1996-1997 Sam Leffler
  * The codec also handle byte swapping the encoded values as necessary
  * since the library does not have the information necessary
  * to know the bit depth of the raw unencoded buffer.
  * The codec also handle byte swapping the encoded values as necessary
  * since the library does not have the information necessary
  * to know the bit depth of the raw unencoded buffer.
- * 
+ *
+ * NOTE: This decoder does not appear to update tif_rawcp, and tif_rawcc.
+ * This can cause problems with the implementation of CHUNKY_STRIP_READ_SUPPORT
+ * as noted in http://trac.osgeo.org/gdal/ticket/3894.   FrankW - Jan'11
  */
 
 #include "tif_predict.h"
  */
 
 #include "tif_predict.h"
@@ -108,7 +110,7 @@ static float  LogK1, LogK2;
 #define REPEAT(n, op)   { int i; i=n; do { i--; op; } while (i>0); }
 
 static void
 #define REPEAT(n, op)   { int i; i=n; do { i--; op; } while (i>0); }
 
 static void
-horizontalAccumulateF(uint16 *wp, int n, int stride, float *op, 
+horizontalAccumulateF(uint16 *wp, int n, int stride, float *op,
        float *ToLinearF)
 {
     register unsigned int  cr, cg, cb, ca, mask;
        float *ToLinearF)
 {
     register unsigned int  cr, cg, cb, ca, mask;
@@ -117,9 +119,9 @@ horizontalAccumulateF(uint16 *wp, int n, int stride, float *op,
     if (n >= stride) {
        mask = CODE_MASK;
        if (stride == 3) {
     if (n >= stride) {
        mask = CODE_MASK;
        if (stride == 3) {
-           t0 = ToLinearF[cr = wp[0]];
-           t1 = ToLinearF[cg = wp[1]];
-           t2 = ToLinearF[cb = wp[2]];
+           t0 = ToLinearF[cr = (wp[0] & mask)];
+           t1 = ToLinearF[cg = (wp[1] & mask)];
+           t2 = ToLinearF[cb = (wp[2] & mask)];
            op[0] = t0;
            op[1] = t1;
            op[2] = t2;
            op[0] = t0;
            op[1] = t1;
            op[2] = t2;
@@ -136,10 +138,10 @@ horizontalAccumulateF(uint16 *wp, int n, int stride, float *op,
                op[2] = t2;
            }
        } else if (stride == 4) {
                op[2] = t2;
            }
        } else if (stride == 4) {
-           t0 = ToLinearF[cr = wp[0]];
-           t1 = ToLinearF[cg = wp[1]];
-           t2 = ToLinearF[cb = wp[2]];
-           t3 = ToLinearF[ca = wp[3]];
+           t0 = ToLinearF[cr = (wp[0] & mask)];
+           t1 = ToLinearF[cg = (wp[1] & mask)];
+           t2 = ToLinearF[cb = (wp[2] & mask)];
+           t3 = ToLinearF[ca = (wp[3] & mask)];
            op[0] = t0;
            op[1] = t1;
            op[2] = t2;
            op[0] = t0;
            op[1] = t1;
            op[2] = t2;
@@ -183,9 +185,9 @@ horizontalAccumulate12(uint16 *wp, int n, int stride, int16 *op,
     if (n >= stride) {
        mask = CODE_MASK;
        if (stride == 3) {
     if (n >= stride) {
        mask = CODE_MASK;
        if (stride == 3) {
-           t0 = ToLinearF[cr = wp[0]] * SCALE12;
-           t1 = ToLinearF[cg = wp[1]] * SCALE12;
-           t2 = ToLinearF[cb = wp[2]] * SCALE12;
+           t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12;
+           t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12;
+           t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12;
            op[0] = CLAMP12(t0);
            op[1] = CLAMP12(t1);
            op[2] = CLAMP12(t2);
            op[0] = CLAMP12(t0);
            op[1] = CLAMP12(t1);
            op[2] = CLAMP12(t2);
@@ -202,10 +204,10 @@ horizontalAccumulate12(uint16 *wp, int n, int stride, int16 *op,
                op[2] = CLAMP12(t2);
            }
        } else if (stride == 4) {
                op[2] = CLAMP12(t2);
            }
        } else if (stride == 4) {
-           t0 = ToLinearF[cr = wp[0]] * SCALE12;
-           t1 = ToLinearF[cg = wp[1]] * SCALE12;
-           t2 = ToLinearF[cb = wp[2]] * SCALE12;
-           t3 = ToLinearF[ca = wp[3]] * SCALE12;
+           t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12;
+           t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12;
+           t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12;
+           t3 = ToLinearF[ca = (wp[3] & mask)] * SCALE12;
            op[0] = CLAMP12(t0);
            op[1] = CLAMP12(t1);
            op[2] = CLAMP12(t2);
            op[0] = CLAMP12(t0);
            op[1] = CLAMP12(t1);
            op[2] = CLAMP12(t2);
@@ -247,9 +249,9 @@ horizontalAccumulate16(uint16 *wp, int n, int stride, uint16 *op,
     if (n >= stride) {
        mask = CODE_MASK;
        if (stride == 3) {
     if (n >= stride) {
        mask = CODE_MASK;
        if (stride == 3) {
-           op[0] = ToLinear16[cr = wp[0]];
-           op[1] = ToLinear16[cg = wp[1]];
-           op[2] = ToLinear16[cb = wp[2]];
+           op[0] = ToLinear16[cr = (wp[0] & mask)];
+           op[1] = ToLinear16[cg = (wp[1] & mask)];
+           op[2] = ToLinear16[cb = (wp[2] & mask)];
            n -= 3;
            while (n > 0) {
                wp += 3;
            n -= 3;
            while (n > 0) {
                wp += 3;
@@ -260,10 +262,10 @@ horizontalAccumulate16(uint16 *wp, int n, int stride, uint16 *op,
                op[2] = ToLinear16[(cb += wp[2]) & mask];
            }
        } else if (stride == 4) {
                op[2] = ToLinear16[(cb += wp[2]) & mask];
            }
        } else if (stride == 4) {
-           op[0] = ToLinear16[cr = wp[0]];
-           op[1] = ToLinear16[cg = wp[1]];
-           op[2] = ToLinear16[cb = wp[2]];
-           op[3] = ToLinear16[ca = wp[3]];
+           op[0] = ToLinear16[cr = (wp[0] & mask)];
+           op[1] = ToLinear16[cg = (wp[1] & mask)];
+           op[2] = ToLinear16[cb = (wp[2] & mask)];
+           op[3] = ToLinear16[ca = (wp[3] & mask)];
            n -= 4;
            while (n > 0) {
                wp += 4;
            n -= 4;
            while (n > 0) {
                wp += 4;
@@ -327,7 +329,7 @@ horizontalAccumulate11(uint16 *wp, int n, int stride, uint16 *op)
            while (n > 0) {
                REPEAT(stride,
                    wp[stride] += *wp; *op = *wp&mask; wp++; op++)
            while (n > 0) {
                REPEAT(stride,
                    wp[stride] += *wp; *op = *wp&mask; wp++; op++)
-               n -= stride;
+               n -= stride;
            }
        }
     }
            }
        }
     }
@@ -342,9 +344,9 @@ horizontalAccumulate8(uint16 *wp, int n, int stride, unsigned char *op,
     if (n >= stride) {
        mask = CODE_MASK;
        if (stride == 3) {
     if (n >= stride) {
        mask = CODE_MASK;
        if (stride == 3) {
-           op[0] = ToLinear8[cr = wp[0]];
-           op[1] = ToLinear8[cg = wp[1]];
-           op[2] = ToLinear8[cb = wp[2]];
+           op[0] = ToLinear8[cr = (wp[0] & mask)];
+           op[1] = ToLinear8[cg = (wp[1] & mask)];
+           op[2] = ToLinear8[cb = (wp[2] & mask)];
            n -= 3;
            while (n > 0) {
                n -= 3;
            n -= 3;
            while (n > 0) {
                n -= 3;
@@ -355,10 +357,10 @@ horizontalAccumulate8(uint16 *wp, int n, int stride, unsigned char *op,
                op[2] = ToLinear8[(cb += wp[2]) & mask];
            }
        } else if (stride == 4) {
                op[2] = ToLinear8[(cb += wp[2]) & mask];
            }
        } else if (stride == 4) {
-           op[0] = ToLinear8[cr = wp[0]];
-           op[1] = ToLinear8[cg = wp[1]];
-           op[2] = ToLinear8[cb = wp[2]];
-           op[3] = ToLinear8[ca = wp[3]];
+           op[0] = ToLinear8[cr = (wp[0] & mask)];
+           op[1] = ToLinear8[cg = (wp[1] & mask)];
+           op[2] = ToLinear8[cb = (wp[2] & mask)];
+           op[3] = ToLinear8[ca = (wp[3] & mask)];
            n -= 4;
            while (n > 0) {
                n -= 4;
            n -= 4;
            while (n > 0) {
                n -= 4;
@@ -393,9 +395,9 @@ horizontalAccumulate8abgr(uint16 *wp, int n, int stride, unsigned char *op,
        mask = CODE_MASK;
        if (stride == 3) {
            op[0] = 0;
        mask = CODE_MASK;
        if (stride == 3) {
            op[0] = 0;
-           t1 = ToLinear8[cb = wp[2]];
-           t2 = ToLinear8[cg = wp[1]];
-           t3 = ToLinear8[cr = wp[0]];
+           t1 = ToLinear8[cb = (wp[2] & mask)];
+           t2 = ToLinear8[cg = (wp[1] & mask)];
+           t3 = ToLinear8[cr = (wp[0] & mask)];
            op[1] = t1;
            op[2] = t2;
            op[3] = t3;
            op[1] = t1;
            op[2] = t2;
            op[3] = t3;
@@ -413,10 +415,10 @@ horizontalAccumulate8abgr(uint16 *wp, int n, int stride, unsigned char *op,
                op[3] = t3;
            }
        } else if (stride == 4) {
                op[3] = t3;
            }
        } else if (stride == 4) {
-           t0 = ToLinear8[ca = wp[3]];
-           t1 = ToLinear8[cb = wp[2]];
-           t2 = ToLinear8[cg = wp[1]];
-           t3 = ToLinear8[cr = wp[0]];
+           t0 = ToLinear8[ca = (wp[3] & mask)];
+           t1 = ToLinear8[cb = (wp[2] & mask)];
+           t2 = ToLinear8[cg = (wp[1] & mask)];
+           t3 = ToLinear8[cr = (wp[0] & mask)];
            op[0] = t0;
            op[1] = t1;
            op[2] = t2;
            op[0] = t0;
            op[1] = t1;
            op[2] = t2;
@@ -587,13 +589,12 @@ PixarLogMakeTables(PixarLogState *sp)
     return 1;
 }
 
     return 1;
 }
 
-#define        DecoderState(tif)       ((PixarLogState*) (tif)->tif_data)
-#define        EncoderState(tif)       ((PixarLogState*) (tif)->tif_data)
+#define DecoderState(tif)      ((PixarLogState*) (tif)->tif_data)
+#define EncoderState(tif)      ((PixarLogState*) (tif)->tif_data)
 
 
-static int PixarLogEncode(TIFF*, tidata_t, tsize_t, tsample_t);
-static int PixarLogDecode(TIFF*, tidata_t, tsize_t, tsample_t);
+static int PixarLogEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
+static int PixarLogDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
 
 
-#define N(a)   (sizeof(a)/sizeof(a[0]))
 #define PIXARLOGDATAFMT_UNKNOWN        -1
 
 static int
 #define PIXARLOGDATAFMT_UNKNOWN        -1
 
 static int
@@ -631,10 +632,10 @@ PixarLogGuessDataFmt(TIFFDirectory *td)
        return guess;
 }
 
        return guess;
 }
 
-static uint32
-multiply(size_t m1, size_t m2)
+static tmsize_t
+multiply_ms(tmsize_t m1, tmsize_t m2)
 {
 {
-       uint32  bytes = m1 * m2;
+       tmsize_t bytes = m1 * m2;
 
        if (m1 && bytes / m1 != m2)
                bytes = 0;
 
        if (m1 && bytes / m1 != m2)
                bytes = 0;
@@ -642,29 +643,36 @@ multiply(size_t m1, size_t m2)
        return bytes;
 }
 
        return bytes;
 }
 
+static int
+PixarLogFixupTags(TIFF* tif)
+{
+       (void) tif;
+       return (1);
+}
+
 static int
 PixarLogSetupDecode(TIFF* tif)
 {
 static int
 PixarLogSetupDecode(TIFF* tif)
 {
+       static const char module[] = "PixarLogSetupDecode";
        TIFFDirectory *td = &tif->tif_dir;
        PixarLogState* sp = DecoderState(tif);
        TIFFDirectory *td = &tif->tif_dir;
        PixarLogState* sp = DecoderState(tif);
-       tsize_t tbuf_size;
-       static const char module[] = "PixarLogSetupDecode";
+       tmsize_t tbuf_size;
 
        assert(sp != NULL);
 
        /* Make sure no byte swapping happens on the data
         * after decompression. */
 
        assert(sp != NULL);
 
        /* Make sure no byte swapping happens on the data
         * after decompression. */
-       tif->tif_postdecode = _TIFFNoPostDecode;
+       tif->tif_postdecode = _TIFFNoPostDecode;  
 
        /* for some reason, we can't do this in TIFFInitPixarLog */
 
        sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
            td->td_samplesperpixel : 1);
 
        /* for some reason, we can't do this in TIFFInitPixarLog */
 
        sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
            td->td_samplesperpixel : 1);
-       tbuf_size = multiply(multiply(multiply(sp->stride, td->td_imagewidth),
+       tbuf_size = multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
                                      td->td_rowsperstrip), sizeof(uint16));
        if (tbuf_size == 0)
                                      td->td_rowsperstrip), sizeof(uint16));
        if (tbuf_size == 0)
-               return (0);
-       sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size);
+               return (0);   /* TODO: this is an error return without error report through TIFFErrorExt */
+       sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size+sizeof(uint16)*sp->stride);
        if (sp->tbuf == NULL)
                return (0);
        if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
        if (sp->tbuf == NULL)
                return (0);
        if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
@@ -677,7 +685,7 @@ PixarLogSetupDecode(TIFF* tif)
        }
 
        if (inflateInit(&sp->stream) != Z_OK) {
        }
 
        if (inflateInit(&sp->stream) != Z_OK) {
-               TIFFErrorExt(tif->tif_clientdata, module, "%s: %s", tif->tif_name, sp->stream.msg);
+               TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
                return (0);
        } else {
                sp->state |= PLSTATE_INIT;
                return (0);
        } else {
                sp->state |= PLSTATE_INIT;
@@ -689,24 +697,36 @@ PixarLogSetupDecode(TIFF* tif)
  * Setup state for decoding a strip.
  */
 static int
  * Setup state for decoding a strip.
  */
 static int
-PixarLogPreDecode(TIFF* tif, tsample_t s)
+PixarLogPreDecode(TIFF* tif, uint16 s)
 {
 {
+       static const char module[] = "PixarLogPreDecode";
        PixarLogState* sp = DecoderState(tif);
 
        (void) s;
        assert(sp != NULL);
        sp->stream.next_in = tif->tif_rawdata;
        PixarLogState* sp = DecoderState(tif);
 
        (void) s;
        assert(sp != NULL);
        sp->stream.next_in = tif->tif_rawdata;
-       sp->stream.avail_in = tif->tif_rawcc;
+       assert(sizeof(sp->stream.avail_in)==4);  /* if this assert gets raised,
+           we need to simplify this code to reflect a ZLib that is likely updated
+           to deal with 8byte memory sizes, though this code will respond
+           apropriately even before we simplify it */
+       sp->stream.avail_in = (uInt) tif->tif_rawcc;
+       if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
+       {
+               TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
+               return (0);
+       }
        return (inflateReset(&sp->stream) == Z_OK);
 }
 
 static int
        return (inflateReset(&sp->stream) == Z_OK);
 }
 
 static int
-PixarLogDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
+PixarLogDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
 {
 {
+       static const char module[] = "PixarLogDecode";
        TIFFDirectory *td = &tif->tif_dir;
        PixarLogState* sp = DecoderState(tif);
        TIFFDirectory *td = &tif->tif_dir;
        PixarLogState* sp = DecoderState(tif);
-       static const char module[] = "PixarLogDecode";
-       int i, nsamples, llen;
+       tmsize_t i;
+       tmsize_t nsamples;
+       int llen;
        uint16 *up;
 
        switch (sp->user_datafmt) {
        uint16 *up;
 
        switch (sp->user_datafmt) {
@@ -723,7 +743,7 @@ PixarLogDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
                nsamples = occ;
                break;
        default:
                nsamples = occ;
                break;
        default:
-               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+               TIFFErrorExt(tif->tif_clientdata, module,
                        "%d bit input not supported in PixarLog",
                        td->td_bitspersample);
                return 0;
                        "%d bit input not supported in PixarLog",
                        td->td_bitspersample);
                return 0;
@@ -734,7 +754,16 @@ PixarLogDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
        (void) s;
        assert(sp != NULL);
        sp->stream.next_out = (unsigned char *) sp->tbuf;
        (void) s;
        assert(sp != NULL);
        sp->stream.next_out = (unsigned char *) sp->tbuf;
-       sp->stream.avail_out = nsamples * sizeof(uint16);
+       assert(sizeof(sp->stream.avail_out)==4);  /* if this assert gets raised,
+           we need to simplify this code to reflect a ZLib that is likely updated
+           to deal with 8byte memory sizes, though this code will respond
+           apropriately even before we simplify it */
+       sp->stream.avail_out = (uInt) (nsamples * sizeof(uint16));
+       if (sp->stream.avail_out != nsamples * sizeof(uint16))
+       {
+               TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
+               return (0);
+       }
        do {
                int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
                if (state == Z_STREAM_END) {
        do {
                int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
                if (state == Z_STREAM_END) {
@@ -742,15 +771,15 @@ PixarLogDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
                }
                if (state == Z_DATA_ERROR) {
                        TIFFErrorExt(tif->tif_clientdata, module,
                }
                if (state == Z_DATA_ERROR) {
                        TIFFErrorExt(tif->tif_clientdata, module,
-                           "%s: Decoding error at scanline %d, %s",
-                           tif->tif_name, tif->tif_row, sp->stream.msg);
+                           "Decoding error at scanline %lu, %s",
+                           (unsigned long) tif->tif_row, sp->stream.msg);
                        if (inflateSync(&sp->stream) != Z_OK)
                                return (0);
                        continue;
                }
                if (state != Z_OK) {
                        if (inflateSync(&sp->stream) != Z_OK)
                                return (0);
                        continue;
                }
                if (state != Z_OK) {
-                       TIFFErrorExt(tif->tif_clientdata, module, "%s: zlib error: %s",
-                           tif->tif_name, sp->stream.msg);
+                       TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
+                           sp->stream.msg);
                        return (0);
                }
        } while (sp->stream.avail_out > 0);
                        return (0);
                }
        } while (sp->stream.avail_out > 0);
@@ -758,8 +787,8 @@ PixarLogDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
        /* hopefully, we got all the bytes we needed */
        if (sp->stream.avail_out != 0) {
                TIFFErrorExt(tif->tif_clientdata, module,
        /* hopefully, we got all the bytes we needed */
        if (sp->stream.avail_out != 0) {
                TIFFErrorExt(tif->tif_clientdata, module,
-                   "%s: Not enough data at scanline %d (short %d bytes)",
-                   tif->tif_name, tif->tif_row, sp->stream.avail_out);
+                   "Not enough data at scanline %lu (short " TIFF_UINT64_FORMAT " bytes)",
+                   (unsigned long) tif->tif_row, (TIFF_UINT64_T) sp->stream.avail_out);
                return (0);
        }
 
                return (0);
        }
 
@@ -768,6 +797,18 @@ PixarLogDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
        if (tif->tif_flags & TIFF_SWAB)
                TIFFSwabArrayOfShort(up, nsamples);
 
        if (tif->tif_flags & TIFF_SWAB)
                TIFFSwabArrayOfShort(up, nsamples);
 
+       /*
+        * if llen is not an exact multiple of nsamples, the decode operation
+        * may overflow the output buffer, so truncate it enough to prevent
+        * that but still salvage as much data as possible.
+        */
+       if (nsamples % llen) { 
+               TIFFWarningExt(tif->tif_clientdata, module,
+                       "stride %lu is not a multiple of sample count, "
+                       "%lu, data truncated.", (unsigned long) llen, (unsigned long) nsamples);
+               nsamples -= nsamples % llen;
+       }
+
        for (i = 0; i < nsamples; i += llen, up += llen) {
                switch (sp->user_datafmt)  {
                case PIXARLOGDATAFMT_FLOAT:
        for (i = 0; i < nsamples; i += llen, up += llen) {
                switch (sp->user_datafmt)  {
                case PIXARLOGDATAFMT_FLOAT:
@@ -801,8 +842,8 @@ PixarLogDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
                        op += llen * sizeof(unsigned char);
                        break;
                default:
                        op += llen * sizeof(unsigned char);
                        break;
                default:
-                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
-                                 "PixarLogDecode: unsupported bits/sample: %d", 
+                       TIFFErrorExt(tif->tif_clientdata, module,
+                                 "Unsupported bits/sample: %d",
                                  td->td_bitspersample);
                        return (0);
                }
                                  td->td_bitspersample);
                        return (0);
                }
@@ -814,10 +855,10 @@ PixarLogDecode(TIFF* tif, tidata_t op, tsize_t occ, tsample_t s)
 static int
 PixarLogSetupEncode(TIFF* tif)
 {
 static int
 PixarLogSetupEncode(TIFF* tif)
 {
+       static const char module[] = "PixarLogSetupEncode";
        TIFFDirectory *td = &tif->tif_dir;
        PixarLogState* sp = EncoderState(tif);
        TIFFDirectory *td = &tif->tif_dir;
        PixarLogState* sp = EncoderState(tif);
-       tsize_t tbuf_size;
-       static const char module[] = "PixarLogSetupEncode";
+       tmsize_t tbuf_size;
 
        assert(sp != NULL);
 
 
        assert(sp != NULL);
 
@@ -825,10 +866,10 @@ PixarLogSetupEncode(TIFF* tif)
 
        sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
            td->td_samplesperpixel : 1);
 
        sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
            td->td_samplesperpixel : 1);
-       tbuf_size = multiply(multiply(multiply(sp->stride, td->td_imagewidth),
+       tbuf_size = multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
                                      td->td_rowsperstrip), sizeof(uint16));
        if (tbuf_size == 0)
                                      td->td_rowsperstrip), sizeof(uint16));
        if (tbuf_size == 0)
-               return (0);
+               return (0);  /* TODO: this is an error return without error report through TIFFErrorExt */
        sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size);
        if (sp->tbuf == NULL)
                return (0);
        sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size);
        if (sp->tbuf == NULL)
                return (0);
@@ -840,7 +881,7 @@ PixarLogSetupEncode(TIFF* tif)
        }
 
        if (deflateInit(&sp->stream, sp->quality) != Z_OK) {
        }
 
        if (deflateInit(&sp->stream, sp->quality) != Z_OK) {
-               TIFFErrorExt(tif->tif_clientdata, module, "%s: %s", tif->tif_name, sp->stream.msg);
+               TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
                return (0);
        } else {
                sp->state |= PLSTATE_INIT;
                return (0);
        } else {
                sp->state |= PLSTATE_INIT;
@@ -852,21 +893,30 @@ PixarLogSetupEncode(TIFF* tif)
  * Reset encoding state at the start of a strip.
  */
 static int
  * Reset encoding state at the start of a strip.
  */
 static int
-PixarLogPreEncode(TIFF* tif, tsample_t s)
+PixarLogPreEncode(TIFF* tif, uint16 s)
 {
 {
+       static const char module[] = "PixarLogPreEncode";
        PixarLogState *sp = EncoderState(tif);
 
        (void) s;
        assert(sp != NULL);
        sp->stream.next_out = tif->tif_rawdata;
        PixarLogState *sp = EncoderState(tif);
 
        (void) s;
        assert(sp != NULL);
        sp->stream.next_out = tif->tif_rawdata;
+       assert(sizeof(sp->stream.avail_out)==4);  /* if this assert gets raised,
+           we need to simplify this code to reflect a ZLib that is likely updated
+           to deal with 8byte memory sizes, though this code will respond
+           apropriately even before we simplify it */
        sp->stream.avail_out = tif->tif_rawdatasize;
        sp->stream.avail_out = tif->tif_rawdatasize;
+       if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
+       {
+               TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
+               return (0);
+       }
        return (deflateReset(&sp->stream) == Z_OK);
 }
 
 static void
 horizontalDifferenceF(float *ip, int n, int stride, uint16 *wp, uint16 *FromLT2)
 {
        return (deflateReset(&sp->stream) == Z_OK);
 }
 
 static void
 horizontalDifferenceF(float *ip, int n, int stride, uint16 *wp, uint16 *FromLT2)
 {
-
     int32 r1, g1, b1, a1, r2, g2, b2, a2, mask;
     float fltsize = Fltsize;
 
     int32 r1, g1, b1, a1, r2, g2, b2, a2, mask;
     float fltsize = Fltsize;
 
@@ -1031,12 +1081,14 @@ horizontalDifference8(unsigned char *ip, int n, int stride,
  * Encode a chunk of pixels.
  */
 static int
  * Encode a chunk of pixels.
  */
 static int
-PixarLogEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
+PixarLogEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
 {
 {
+       static const char module[] = "PixarLogEncode";
        TIFFDirectory *td = &tif->tif_dir;
        PixarLogState *sp = EncoderState(tif);
        TIFFDirectory *td = &tif->tif_dir;
        PixarLogState *sp = EncoderState(tif);
-       static const char module[] = "PixarLogEncode";
-       int     i, n, llen;
+       tmsize_t i;
+       tmsize_t n;
+       int llen;
        unsigned short * up;
 
        (void) s;
        unsigned short * up;
 
        (void) s;
@@ -1055,7 +1107,7 @@ PixarLogEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
                n = cc;
                break;
        default:
                n = cc;
                break;
        default:
-               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+               TIFFErrorExt(tif->tif_clientdata, module,
                        "%d bit input not supported in PixarLog",
                        td->td_bitspersample);
                return 0;
                        "%d bit input not supported in PixarLog",
                        td->td_bitspersample);
                return 0;
@@ -1081,7 +1133,7 @@ PixarLogEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
                        bp += llen * sizeof(unsigned char);
                        break;
                default:
                        bp += llen * sizeof(unsigned char);
                        break;
                default:
-                       TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
+                       TIFFErrorExt(tif->tif_clientdata, module,
                                "%d bit input not supported in PixarLog",
                                td->td_bitspersample);
                        return 0;
                                "%d bit input not supported in PixarLog",
                                td->td_bitspersample);
                        return 0;
@@ -1089,19 +1141,29 @@ PixarLogEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
        }
  
        sp->stream.next_in = (unsigned char *) sp->tbuf;
        }
  
        sp->stream.next_in = (unsigned char *) sp->tbuf;
-       sp->stream.avail_in = n * sizeof(uint16);
+       assert(sizeof(sp->stream.avail_in)==4);  /* if this assert gets raised,
+           we need to simplify this code to reflect a ZLib that is likely updated
+           to deal with 8byte memory sizes, though this code will respond
+           apropriately even before we simplify it */
+       sp->stream.avail_in = (uInt) (n * sizeof(uint16));
+       if ((sp->stream.avail_in / sizeof(uint16)) != (uInt) n)
+       {
+               TIFFErrorExt(tif->tif_clientdata, module,
+                            "ZLib cannot deal with buffers this size");
+               return (0);
+       }
 
        do {
                if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
 
        do {
                if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
-                       TIFFErrorExt(tif->tif_clientdata, module, "%s: Encoder error: %s",
-                           tif->tif_name, sp->stream.msg);
+                       TIFFErrorExt(tif->tif_clientdata, module, "Encoder error: %s",
+                           sp->stream.msg);
                        return (0);
                }
                if (sp->stream.avail_out == 0) {
                        tif->tif_rawcc = tif->tif_rawdatasize;
                        TIFFFlushData1(tif);
                        sp->stream.next_out = tif->tif_rawdata;
                        return (0);
                }
                if (sp->stream.avail_out == 0) {
                        tif->tif_rawcc = tif->tif_rawdatasize;
                        TIFFFlushData1(tif);
                        sp->stream.next_out = tif->tif_rawdata;
-                       sp->stream.avail_out = tif->tif_rawdatasize;
+                       sp->stream.avail_out = (uInt) tif->tif_rawdatasize;  /* this is a safe typecast, as check is made already in PixarLogPreEncode */
                }
        } while (sp->stream.avail_in > 0);
        return (1);
                }
        } while (sp->stream.avail_in > 0);
        return (1);
@@ -1115,8 +1177,8 @@ PixarLogEncode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
 static int
 PixarLogPostEncode(TIFF* tif)
 {
 static int
 PixarLogPostEncode(TIFF* tif)
 {
-       PixarLogState *sp = EncoderState(tif);
        static const char module[] = "PixarLogPostEncode";
        static const char module[] = "PixarLogPostEncode";
+       PixarLogState *sp = EncoderState(tif);
        int state;
 
        sp->stream.avail_in = 0;
        int state;
 
        sp->stream.avail_in = 0;
@@ -1126,17 +1188,17 @@ PixarLogPostEncode(TIFF* tif)
                switch (state) {
                case Z_STREAM_END:
                case Z_OK:
                switch (state) {
                case Z_STREAM_END:
                case Z_OK:
-                   if (sp->stream.avail_out != (uint32)tif->tif_rawdatasize) {
+                   if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) {
                            tif->tif_rawcc =
                                tif->tif_rawdatasize - sp->stream.avail_out;
                            TIFFFlushData1(tif);
                            sp->stream.next_out = tif->tif_rawdata;
                            tif->tif_rawcc =
                                tif->tif_rawdatasize - sp->stream.avail_out;
                            TIFFFlushData1(tif);
                            sp->stream.next_out = tif->tif_rawdata;
-                           sp->stream.avail_out = tif->tif_rawdatasize;
+                           sp->stream.avail_out = (uInt) tif->tif_rawdatasize;  /* this is a safe typecast, as check is made already in PixarLogPreEncode */
                    }
                    break;
                default:
                    }
                    break;
                default:
-                       TIFFErrorExt(tif->tif_clientdata, module, "%s: zlib error: %s",
-                       tif->tif_name, sp->stream.msg);
+                       TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
+                       sp->stream.msg);
                    return (0);
                }
        } while (state != Z_STREAM_END);
                    return (0);
                }
        } while (state != Z_STREAM_END);
@@ -1148,9 +1210,11 @@ PixarLogClose(TIFF* tif)
 {
        TIFFDirectory *td = &tif->tif_dir;
 
 {
        TIFFDirectory *td = &tif->tif_dir;
 
-       /* In a really sneaky maneuver, on close, we covertly modify both
-        * bitspersample and sampleformat in the directory to indicate
-        * 8-bit linear.  This way, the decode "just works" even for
+       /* In a really sneaky (and really incorrect, and untruthfull, and
+        * troublesome, and error-prone) maneuver that completely goes against
+        * the spirit of TIFF, and breaks TIFF, on close, we covertly
+        * modify both bitspersample and sampleformat in the directory to
+        * indicate 8-bit linear.  This way, the decode "just works" even for
         * readers that don't know about PixarLog, or how to set
         * the PIXARLOGDATFMT pseudo-tag.
         */
         * readers that don't know about PixarLog, or how to set
         * the PIXARLOGDATFMT pseudo-tag.
         */
@@ -1191,26 +1255,26 @@ PixarLogCleanup(TIFF* tif)
 }
 
 static int
 }
 
 static int
-PixarLogVSetField(TIFF* tif, ttag_t tag, va_list ap)
+PixarLogVSetField(TIFF* tif, uint32 tag, va_list ap)
 {
 {
+    static const char module[] = "PixarLogVSetField";
     PixarLogState *sp = (PixarLogState *)tif->tif_data;
     int result;
     PixarLogState *sp = (PixarLogState *)tif->tif_data;
     int result;
-    static const char module[] = "PixarLogVSetField";
 
     switch (tag) {
      case TIFFTAG_PIXARLOGQUALITY:
 
     switch (tag) {
      case TIFFTAG_PIXARLOGQUALITY:
-               sp->quality = va_arg(ap, int);
+               sp->quality = (int) va_arg(ap, int);
                if (tif->tif_mode != O_RDONLY && (sp->state&PLSTATE_INIT)) {
                        if (deflateParams(&sp->stream,
                            sp->quality, Z_DEFAULT_STRATEGY) != Z_OK) {
                if (tif->tif_mode != O_RDONLY && (sp->state&PLSTATE_INIT)) {
                        if (deflateParams(&sp->stream,
                            sp->quality, Z_DEFAULT_STRATEGY) != Z_OK) {
-                               TIFFErrorExt(tif->tif_clientdata, module, "%s: zlib error: %s",
-                                       tif->tif_name, sp->stream.msg);
+                               TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
+                                       sp->stream.msg);
                                return (0);
                        }
                }
                return (1);
      case TIFFTAG_PIXARLOGDATAFMT:
                                return (0);
                        }
                }
                return (1);
      case TIFFTAG_PIXARLOGDATAFMT:
-       sp->user_datafmt = va_arg(ap, int);
+       sp->user_datafmt = (int) va_arg(ap, int);
        /* Tweak the TIFF header so that the rest of libtiff knows what
         * size of data will be passed between app and library, and
         * assume that the app knows what it is doing and is not
        /* Tweak the TIFF header so that the rest of libtiff knows what
         * size of data will be passed between app and library, and
         * assume that the app knows what it is doing and is not
@@ -1242,7 +1306,7 @@ PixarLogVSetField(TIFF* tif, ttag_t tag, va_list ap)
        /*
         * Must recalculate sizes should bits/sample change.
         */
        /*
         * Must recalculate sizes should bits/sample change.
         */
-       tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tsize_t) -1;
+       tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
        tif->tif_scanlinesize = TIFFScanlineSize(tif);
        result = 1;             /* NB: pseudo tag */
        break;
        tif->tif_scanlinesize = TIFFScanlineSize(tif);
        result = 1;             /* NB: pseudo tag */
        break;
@@ -1253,7 +1317,7 @@ PixarLogVSetField(TIFF* tif, ttag_t tag, va_list ap)
 }
 
 static int
 }
 
 static int
-PixarLogVGetField(TIFF* tif, ttag_t tag, va_list ap)
+PixarLogVGetField(TIFF* tif, uint32 tag, va_list ap)
 {
     PixarLogState *sp = (PixarLogState *)tif->tif_data;
 
 {
     PixarLogState *sp = (PixarLogState *)tif->tif_data;
 
@@ -1270,22 +1334,34 @@ PixarLogVGetField(TIFF* tif, ttag_t tag, va_list ap)
     return (1);
 }
 
     return (1);
 }
 
-static const TIFFFieldInfo pixarlogFieldInfo[] = {
-    {TIFFTAG_PIXARLOGDATAFMT,0,0,TIFF_ANY,  FIELD_PSEUDO,FALSE,FALSE,""},
-    {TIFFTAG_PIXARLOGQUALITY,0,0,TIFF_ANY,  FIELD_PSEUDO,FALSE,FALSE,""}
+static const TIFFField pixarlogFields[] = {
+    {TIFFTAG_PIXARLOGDATAFMT, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL},
+    {TIFFTAG_PIXARLOGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL}
 };
 
 int
 TIFFInitPixarLog(TIFF* tif, int scheme)
 {
 };
 
 int
 TIFFInitPixarLog(TIFF* tif, int scheme)
 {
+       static const char module[] = "TIFFInitPixarLog";
+
        PixarLogState* sp;
 
        assert(scheme == COMPRESSION_PIXARLOG);
 
        PixarLogState* sp;
 
        assert(scheme == COMPRESSION_PIXARLOG);
 
+       /*
+        * Merge codec-specific tag information.
+        */
+       if (!_TIFFMergeFields(tif, pixarlogFields,
+                             TIFFArrayCount(pixarlogFields))) {
+               TIFFErrorExt(tif->tif_clientdata, module,
+                            "Merging PixarLog codec-specific tags failed");
+               return 0;
+       }
+
        /*
         * Allocate state block so tag methods have storage to record values.
         */
        /*
         * Allocate state block so tag methods have storage to record values.
         */
-       tif->tif_data = (tidata_t) _TIFFmalloc(sizeof (PixarLogState));
+       tif->tif_data = (uint8*) _TIFFmalloc(sizeof (PixarLogState));
        if (tif->tif_data == NULL)
                goto bad;
        sp = (PixarLogState*) tif->tif_data;
        if (tif->tif_data == NULL)
                goto bad;
        sp = (PixarLogState*) tif->tif_data;
@@ -1296,22 +1372,22 @@ TIFFInitPixarLog(TIFF* tif, int scheme)
        /*
         * Install codec methods.
         */
        /*
         * Install codec methods.
         */
+       tif->tif_fixuptags = PixarLogFixupTags; 
        tif->tif_setupdecode = PixarLogSetupDecode;
        tif->tif_predecode = PixarLogPreDecode;
        tif->tif_decoderow = PixarLogDecode;
        tif->tif_setupdecode = PixarLogSetupDecode;
        tif->tif_predecode = PixarLogPreDecode;
        tif->tif_decoderow = PixarLogDecode;
-       tif->tif_decodestrip = PixarLogDecode;
+       tif->tif_decodestrip = PixarLogDecode;  
        tif->tif_decodetile = PixarLogDecode;
        tif->tif_setupencode = PixarLogSetupEncode;
        tif->tif_preencode = PixarLogPreEncode;
        tif->tif_postencode = PixarLogPostEncode;
        tif->tif_decodetile = PixarLogDecode;
        tif->tif_setupencode = PixarLogSetupEncode;
        tif->tif_preencode = PixarLogPreEncode;
        tif->tif_postencode = PixarLogPostEncode;
-       tif->tif_encoderow = PixarLogEncode;
+       tif->tif_encoderow = PixarLogEncode;  
        tif->tif_encodestrip = PixarLogEncode;
        tif->tif_encodestrip = PixarLogEncode;
-       tif->tif_encodetile = PixarLogEncode;
+       tif->tif_encodetile = PixarLogEncode;  
        tif->tif_close = PixarLogClose;
        tif->tif_cleanup = PixarLogCleanup;
 
        /* Override SetField so we can handle our private pseudo-tag */
        tif->tif_close = PixarLogClose;
        tif->tif_cleanup = PixarLogCleanup;
 
        /* Override SetField so we can handle our private pseudo-tag */
-       _TIFFMergeFieldInfo(tif, pixarlogFieldInfo, N(pixarlogFieldInfo));
        sp->vgetparent = tif->tif_tagmethods.vgetfield;
        tif->tif_tagmethods.vgetfield = PixarLogVGetField;   /* hook for codec tags */
        sp->vsetparent = tif->tif_tagmethods.vsetfield;
        sp->vgetparent = tif->tif_tagmethods.vgetfield;
        tif->tif_tagmethods.vgetfield = PixarLogVGetField;   /* hook for codec tags */
        sp->vsetparent = tif->tif_tagmethods.vsetfield;
@@ -1333,10 +1409,17 @@ TIFFInitPixarLog(TIFF* tif, int scheme)
 
        return (1);
 bad:
 
        return (1);
 bad:
-       TIFFErrorExt(tif->tif_clientdata, "TIFFInitPixarLog",
+       TIFFErrorExt(tif->tif_clientdata, module,
                     "No space for PixarLog state block");
        return (0);
 }
 #endif /* PIXARLOG_SUPPORT */
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
                     "No space for PixarLog state block");
        return (0);
 }
 #endif /* PIXARLOG_SUPPORT */
 
 /* vim: set ts=8 sts=8 sw=8 noet: */
+/*
+ * Local Variables:
+ * mode: c
+ * c-basic-offset: 8
+ * fill-column: 78
+ * End:
+ */