tif_thunder.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* $Id: tif_thunder.c,v 1.12 2011-04-02 20:54:09 bfriesen Exp $ */
  2. /*
  3. * Copyright (c) 1988-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. #include "tiffiop.h"
  26. #include <assert.h>
  27. #ifdef THUNDER_SUPPORT
  28. /*
  29. * TIFF Library.
  30. *
  31. * ThunderScan 4-bit Compression Algorithm Support
  32. */
  33. /*
  34. * ThunderScan uses an encoding scheme designed for
  35. * 4-bit pixel values. Data is encoded in bytes, with
  36. * each byte split into a 2-bit code word and a 6-bit
  37. * data value. The encoding gives raw data, runs of
  38. * pixels, or pixel values encoded as a delta from the
  39. * previous pixel value. For the latter, either 2-bit
  40. * or 3-bit delta values are used, with the deltas packed
  41. * into a single byte.
  42. */
  43. #define THUNDER_DATA 0x3f /* mask for 6-bit data */
  44. #define THUNDER_CODE 0xc0 /* mask for 2-bit code word */
  45. /* code values */
  46. #define THUNDER_RUN 0x00 /* run of pixels w/ encoded count */
  47. #define THUNDER_2BITDELTAS 0x40 /* 3 pixels w/ encoded 2-bit deltas */
  48. #define DELTA2_SKIP 2 /* skip code for 2-bit deltas */
  49. #define THUNDER_3BITDELTAS 0x80 /* 2 pixels w/ encoded 3-bit deltas */
  50. #define DELTA3_SKIP 4 /* skip code for 3-bit deltas */
  51. #define THUNDER_RAW 0xc0 /* raw data encoded */
  52. static const int twobitdeltas[4] = { 0, 1, 0, -1 };
  53. static const int threebitdeltas[8] = { 0, 1, 2, 3, 0, -3, -2, -1 };
  54. #define SETPIXEL(op, v) { \
  55. lastpixel = (v) & 0xf; \
  56. if ( npixels < maxpixels ) \
  57. { \
  58. if (npixels++ & 1) \
  59. *op++ |= lastpixel; \
  60. else \
  61. op[0] = (uint8) (lastpixel << 4); \
  62. } \
  63. }
  64. static int
  65. ThunderSetupDecode(TIFF* tif)
  66. {
  67. static const char module[] = "ThunderSetupDecode";
  68. if( tif->tif_dir.td_bitspersample != 4 )
  69. {
  70. TIFFErrorExt(tif->tif_clientdata, module,
  71. "Wrong bitspersample value (%d), Thunder decoder only supports 4bits per sample.",
  72. (int) tif->tif_dir.td_bitspersample );
  73. return 0;
  74. }
  75. return (1);
  76. }
  77. static int
  78. ThunderDecode(TIFF* tif, uint8* op, tmsize_t maxpixels)
  79. {
  80. static const char module[] = "ThunderDecode";
  81. register unsigned char *bp;
  82. register tmsize_t cc;
  83. unsigned int lastpixel;
  84. tmsize_t npixels;
  85. bp = (unsigned char *)tif->tif_rawcp;
  86. cc = tif->tif_rawcc;
  87. lastpixel = 0;
  88. npixels = 0;
  89. while (cc > 0 && npixels < maxpixels) {
  90. int n, delta;
  91. n = *bp++, cc--;
  92. switch (n & THUNDER_CODE) {
  93. case THUNDER_RUN: /* pixel run */
  94. /*
  95. * Replicate the last pixel n times,
  96. * where n is the lower-order 6 bits.
  97. */
  98. if (npixels & 1) {
  99. op[0] |= lastpixel;
  100. lastpixel = *op++; npixels++; n--;
  101. } else
  102. lastpixel |= lastpixel << 4;
  103. npixels += n;
  104. if (npixels < maxpixels) {
  105. for (; n > 0; n -= 2)
  106. *op++ = (uint8) lastpixel;
  107. }
  108. if (n == -1)
  109. *--op &= 0xf0;
  110. lastpixel &= 0xf;
  111. break;
  112. case THUNDER_2BITDELTAS: /* 2-bit deltas */
  113. if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP)
  114. SETPIXEL(op, lastpixel + twobitdeltas[delta]);
  115. if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
  116. SETPIXEL(op, lastpixel + twobitdeltas[delta]);
  117. if ((delta = (n & 3)) != DELTA2_SKIP)
  118. SETPIXEL(op, lastpixel + twobitdeltas[delta]);
  119. break;
  120. case THUNDER_3BITDELTAS: /* 3-bit deltas */
  121. if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
  122. SETPIXEL(op, lastpixel + threebitdeltas[delta]);
  123. if ((delta = (n & 7)) != DELTA3_SKIP)
  124. SETPIXEL(op, lastpixel + threebitdeltas[delta]);
  125. break;
  126. case THUNDER_RAW: /* raw data */
  127. SETPIXEL(op, n);
  128. break;
  129. }
  130. }
  131. tif->tif_rawcp = (uint8*) bp;
  132. tif->tif_rawcc = cc;
  133. if (npixels != maxpixels) {
  134. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  135. TIFFErrorExt(tif->tif_clientdata, module,
  136. "%s data at scanline %lu (%I64u != %I64u)",
  137. npixels < maxpixels ? "Not enough" : "Too much",
  138. (unsigned long) tif->tif_row,
  139. (unsigned __int64) npixels,
  140. (unsigned __int64) maxpixels);
  141. #else
  142. TIFFErrorExt(tif->tif_clientdata, module,
  143. "%s data at scanline %lu (%llu != %llu)",
  144. npixels < maxpixels ? "Not enough" : "Too much",
  145. (unsigned long) tif->tif_row,
  146. (unsigned long long) npixels,
  147. (unsigned long long) maxpixels);
  148. #endif
  149. return (0);
  150. }
  151. return (1);
  152. }
  153. static int
  154. ThunderDecodeRow(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
  155. {
  156. static const char module[] = "ThunderDecodeRow";
  157. uint8* row = buf;
  158. (void) s;
  159. if (occ % tif->tif_scanlinesize)
  160. {
  161. TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
  162. return (0);
  163. }
  164. while (occ > 0) {
  165. if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
  166. return (0);
  167. occ -= tif->tif_scanlinesize;
  168. row += tif->tif_scanlinesize;
  169. }
  170. return (1);
  171. }
  172. int
  173. TIFFInitThunderScan(TIFF* tif, int scheme)
  174. {
  175. (void) scheme;
  176. tif->tif_setupdecode = ThunderSetupDecode;
  177. tif->tif_decoderow = ThunderDecodeRow;
  178. tif->tif_decodestrip = ThunderDecodeRow;
  179. return (1);
  180. }
  181. #endif /* THUNDER_SUPPORT */
  182. /* vim: set ts=8 sts=8 sw=8 noet: */
  183. /*
  184. * Local Variables:
  185. * mode: c
  186. * c-basic-offset: 8
  187. * fill-column: 78
  188. * End:
  189. */