tif_thunder.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 1988-1997 Sam Leffler
  3. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and
  6. * its documentation for any purpose is hereby granted without fee, provided
  7. * that (i) the above copyright notices and this permission notice appear in
  8. * all copies of the software and related documentation, and (ii) the names of
  9. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  10. * publicity relating to the software without the specific, prior written
  11. * permission of Sam Leffler and Silicon Graphics.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. #include "tiffiop.h"
  25. #include <assert.h>
  26. #ifdef THUNDER_SUPPORT
  27. /*
  28. * TIFF Library.
  29. *
  30. * ThunderScan 4-bit Compression Algorithm Support
  31. */
  32. /*
  33. * ThunderScan uses an encoding scheme designed for
  34. * 4-bit pixel values. Data is encoded in bytes, with
  35. * each byte split into a 2-bit code word and a 6-bit
  36. * data value. The encoding gives raw data, runs of
  37. * pixels, or pixel values encoded as a delta from the
  38. * previous pixel value. For the latter, either 2-bit
  39. * or 3-bit delta values are used, with the deltas packed
  40. * into a single byte.
  41. */
  42. #define THUNDER_DATA 0x3f /* mask for 6-bit data */
  43. #define THUNDER_CODE 0xc0 /* mask for 2-bit code word */
  44. /* code values */
  45. #define THUNDER_RUN 0x00 /* run of pixels w/ encoded count */
  46. #define THUNDER_2BITDELTAS 0x40 /* 3 pixels w/ encoded 2-bit deltas */
  47. #define DELTA2_SKIP 2 /* skip code for 2-bit deltas */
  48. #define THUNDER_3BITDELTAS 0x80 /* 2 pixels w/ encoded 3-bit deltas */
  49. #define DELTA3_SKIP 4 /* skip code for 3-bit deltas */
  50. #define THUNDER_RAW 0xc0 /* raw data encoded */
  51. static const int twobitdeltas[4] = { 0, 1, 0, -1 };
  52. static const int threebitdeltas[8] = { 0, 1, 2, 3, 0, -3, -2, -1 };
  53. #define SETPIXEL(op, v) { \
  54. lastpixel = (v) & 0xf; \
  55. if ( npixels < maxpixels ) \
  56. { \
  57. if (npixels++ & 1) \
  58. *op++ |= lastpixel; \
  59. else \
  60. op[0] = (uint8) (lastpixel << 4); \
  61. } \
  62. }
  63. static int
  64. ThunderSetupDecode(TIFF* tif)
  65. {
  66. static const char module[] = "ThunderSetupDecode";
  67. if( tif->tif_dir.td_bitspersample != 4 )
  68. {
  69. TIFFErrorExt(tif->tif_clientdata, module,
  70. "Wrong bitspersample value (%d), Thunder decoder only supports 4bits per sample.",
  71. (int) tif->tif_dir.td_bitspersample );
  72. return 0;
  73. }
  74. return (1);
  75. }
  76. static int
  77. ThunderDecode(TIFF* tif, uint8* op, tmsize_t maxpixels)
  78. {
  79. static const char module[] = "ThunderDecode";
  80. register unsigned char *bp;
  81. register tmsize_t cc;
  82. unsigned int lastpixel;
  83. tmsize_t npixels;
  84. bp = (unsigned char *)tif->tif_rawcp;
  85. cc = tif->tif_rawcc;
  86. lastpixel = 0;
  87. npixels = 0;
  88. while (cc > 0 && npixels < maxpixels) {
  89. int n, delta;
  90. n = *bp++;
  91. 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, (unsigned)((int)lastpixel + twobitdeltas[delta]));
  115. if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
  116. SETPIXEL(op, (unsigned)((int)lastpixel + twobitdeltas[delta]));
  117. if ((delta = (n & 3)) != DELTA2_SKIP)
  118. SETPIXEL(op, (unsigned)((int)lastpixel + twobitdeltas[delta]));
  119. break;
  120. case THUNDER_3BITDELTAS: /* 3-bit deltas */
  121. if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
  122. SETPIXEL(op, (unsigned)((int)lastpixel + threebitdeltas[delta]));
  123. if ((delta = (n & 7)) != DELTA3_SKIP)
  124. SETPIXEL(op, (unsigned)((int)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. */