Update kdu copy
[openjpeg.git] / thirdparty / libtiff / tif_thunder.c
index 8e7a1258415c4b3dafd3ef9b8c0d994165eca486..390891c98bde1a07c593e6560f754826f656f11a 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tif_thunder.c,v 1.5.2.1 2010-06-08 18:50:43 bfriesen Exp $ */
+/* $Id: tif_thunder.c,v 1.12 2011-04-02 20:54:09 bfriesen Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
@@ -25,6 +25,7 @@
  */
 
 #include "tiffiop.h"
+#include <assert.h>
 #ifdef THUNDER_SUPPORT
 /*
  * TIFF Library.
 static const int twobitdeltas[4] = { 0, 1, 0, -1 };
 static const int threebitdeltas[8] = { 0, 1, 2, 3, 0, -3, -2, -1 };
 
-#define        SETPIXEL(op, v) { \
-       lastpixel = (v) & 0xf; \
-       if (npixels++ & 1) \
-           *op++ |= lastpixel; \
-       else \
-           op[0] = (tidataval_t) (lastpixel << 4); \
+#define        SETPIXEL(op, v) {                     \
+       lastpixel = (v) & 0xf;                \
+        if ( npixels < maxpixels )         \
+        {                                     \
+         if (npixels++ & 1)                  \
+           *op++ |= lastpixel;               \
+         else                                \
+           op[0] = (uint8) (lastpixel << 4); \
+        }                                     \
 }
 
 static int
-ThunderDecode(TIFF* tif, tidata_t op, tsize_t maxpixels)
+ThunderSetupDecode(TIFF* tif)
 {
+       static const char module[] = "ThunderSetupDecode";
+
+        if( tif->tif_dir.td_bitspersample != 4 )
+        {
+                TIFFErrorExt(tif->tif_clientdata, module,
+                             "Wrong bitspersample value (%d), Thunder decoder only supports 4bits per sample.",
+                             (int) tif->tif_dir.td_bitspersample );
+                return 0;
+        }
+        
+
+       return (1);
+}
+
+static int
+ThunderDecode(TIFF* tif, uint8* op, tmsize_t maxpixels)
+{
+       static const char module[] = "ThunderDecode";
        register unsigned char *bp;
-       register tsize_t cc;
+       register tmsize_t cc;
        unsigned int lastpixel;
-       tsize_t npixels;
+       tmsize_t npixels;
 
        bp = (unsigned char *)tif->tif_rawcp;
        cc = tif->tif_rawcc;
@@ -93,7 +115,7 @@ ThunderDecode(TIFF* tif, tidata_t op, tsize_t maxpixels)
                        npixels += n;
                        if (npixels < maxpixels) {
                                for (; n > 0; n -= 2)
-                                       *op++ = (tidataval_t) lastpixel;
+                                       *op++ = (uint8) lastpixel;
                        }
                        if (n == -1)
                                *--op &= 0xf0;
@@ -118,25 +140,43 @@ ThunderDecode(TIFF* tif, tidata_t op, tsize_t maxpixels)
                        break;
                }
        }
-       tif->tif_rawcp = (tidata_t) bp;
+       tif->tif_rawcp = (uint8*) bp;
        tif->tif_rawcc = cc;
        if (npixels != maxpixels) {
-               TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
-                   "ThunderDecode: %s data at scanline %ld (%lu != %lu)",
-                   npixels < maxpixels ? "Not enough" : "Too much",
-                   (long) tif->tif_row, (long) npixels, (long) maxpixels);
+#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
+               TIFFErrorExt(tif->tif_clientdata, module,
+                            "%s data at scanline %lu (%I64u != %I64u)",
+                            npixels < maxpixels ? "Not enough" : "Too much",
+                            (unsigned long) tif->tif_row,
+                            (unsigned __int64) npixels,
+                            (unsigned __int64) maxpixels);
+#else
+               TIFFErrorExt(tif->tif_clientdata, module,
+                            "%s data at scanline %lu (%llu != %llu)",
+                            npixels < maxpixels ? "Not enough" : "Too much",
+                            (unsigned long) tif->tif_row,
+                            (unsigned long long) npixels,
+                            (unsigned long long) maxpixels);
+#endif
                return (0);
        }
-       return (1);
+
+        return (1);
 }
 
 static int
-ThunderDecodeRow(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
+ThunderDecodeRow(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
 {
-       tidata_t row = buf;
+       static const char module[] = "ThunderDecodeRow";
+       uint8* row = buf;
        
        (void) s;
-       while ((long)occ > 0) {
+       if (occ % tif->tif_scanlinesize)
+       {
+               TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
+               return (0);
+       }
+       while (occ > 0) {
                if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
                        return (0);
                occ -= tif->tif_scanlinesize;
@@ -149,8 +189,10 @@ int
 TIFFInitThunderScan(TIFF* tif, int scheme)
 {
        (void) scheme;
+
+        tif->tif_setupdecode = ThunderSetupDecode;
        tif->tif_decoderow = ThunderDecodeRow;
-       tif->tif_decodestrip = ThunderDecodeRow;
+       tif->tif_decodestrip = ThunderDecodeRow; 
        return (1);
 }
 #endif /* THUNDER_SUPPORT */