tif_thunder.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* $Id: tif_thunder.c,v 1.13 2016-09-04 21:32:56 erouault 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++;
  92. cc--;
  93. switch (n & THUNDER_CODE) {
  94. case THUNDER_RUN: /* pixel run */
  95. /*
  96. * Replicate the last pixel n times,
  97. * where n is the lower-order 6 bits.
  98. */
  99. if (npixels & 1) {
  100. op[0] |= lastpixel;
  101. lastpixel = *op++; npixels++; n--;
  102. } else
  103. lastpixel |= lastpixel << 4;
  104. npixels += n;
  105. if (npixels < maxpixels) {
  106. for (; n > 0; n -= 2)
  107. *op++ = (uint8) lastpixel;
  108. }
  109. if (n == -1)
  110. *--op &= 0xf0;
  111. lastpixel &= 0xf;
  112. break;
  113. case THUNDER_2BITDELTAS: /* 2-bit deltas */
  114. if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP)
  115. SETPIXEL(op, lastpixel + twobitdeltas[delta]);
  116. if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
  117. SETPIXEL(op, lastpixel + twobitdeltas[delta]);
  118. if ((delta = (n & 3)) != DELTA2_SKIP)
  119. SETPIXEL(op, lastpixel + twobitdeltas[delta]);
  120. break;
  121. case THUNDER_3BITDELTAS: /* 3-bit deltas */
  122. if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
  123. SETPIXEL(op, lastpixel + threebitdeltas[delta]);
  124. if ((delta = (n & 7)) != DELTA3_SKIP)
  125. SETPIXEL(op, lastpixel + threebitdeltas[delta]);
  126. break;
  127. case THUNDER_RAW: /* raw data */
  128. SETPIXEL(op, n);
  129. break;
  130. }
  131. }
  132. tif->tif_rawcp = (uint8*) bp;
  133. tif->tif_rawcc = cc;
  134. if (npixels != maxpixels) {
  135. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  136. TIFFErrorExt(tif->tif_clientdata, module,
  137. "%s data at scanline %lu (%I64u != %I64u)",
  138. npixels < maxpixels ? "Not enough" : "Too much",
  139. (unsigned long) tif->tif_row,
  140. (unsigned __int64) npixels,
  141. (unsigned __int64) maxpixels);
  142. #else
  143. TIFFErrorExt(tif->tif_clientdata, module,
  144. "%s data at scanline %lu (%llu != %llu)",
  145. npixels < maxpixels ? "Not enough" : "Too much",
  146. (unsigned long) tif->tif_row,
  147. (unsigned long long) npixels,
  148. (unsigned long long) maxpixels);
  149. #endif
  150. return (0);
  151. }
  152. return (1);
  153. }
  154. static int
  155. ThunderDecodeRow(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
  156. {
  157. static const char module[] = "ThunderDecodeRow";
  158. uint8* row = buf;
  159. (void) s;
  160. if (occ % tif->tif_scanlinesize)
  161. {
  162. TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
  163. return (0);
  164. }
  165. while (occ > 0) {
  166. if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
  167. return (0);
  168. occ -= tif->tif_scanlinesize;
  169. row += tif->tif_scanlinesize;
  170. }
  171. return (1);
  172. }
  173. int
  174. TIFFInitThunderScan(TIFF* tif, int scheme)
  175. {
  176. (void) scheme;
  177. tif->tif_setupdecode = ThunderSetupDecode;
  178. tif->tif_decoderow = ThunderDecodeRow;
  179. tif->tif_decodestrip = ThunderDecodeRow;
  180. return (1);
  181. }
  182. #endif /* THUNDER_SUPPORT */
  183. /* vim: set ts=8 sts=8 sw=8 noet: */
  184. /*
  185. * Local Variables:
  186. * mode: c
  187. * c-basic-offset: 8
  188. * fill-column: 78
  189. * End:
  190. */