tif_thunder.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. { \
  55. lastpixel = (v)&0xf; \
  56. if (npixels < maxpixels) \
  57. { \
  58. if (npixels++ & 1) \
  59. *op++ |= lastpixel; \
  60. else \
  61. op[0] = (uint8_t)(lastpixel << 4); \
  62. } \
  63. }
  64. static int ThunderSetupDecode(TIFF *tif)
  65. {
  66. static const char module[] = "ThunderSetupDecode";
  67. if (tif->tif_dir.td_bitspersample != 4)
  68. {
  69. TIFFErrorExtR(tif, module,
  70. "Wrong bitspersample value (%d), Thunder decoder only "
  71. "supports 4bits per sample.",
  72. (int)tif->tif_dir.td_bitspersample);
  73. return 0;
  74. }
  75. return (1);
  76. }
  77. static int ThunderDecode(TIFF *tif, uint8_t *op0, 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. uint8_t *op = op0;
  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. {
  91. int n, delta;
  92. n = *bp++;
  93. cc--;
  94. switch (n & THUNDER_CODE)
  95. {
  96. case THUNDER_RUN: /* pixel run */
  97. /*
  98. * Replicate the last pixel n times,
  99. * where n is the lower-order 6 bits.
  100. */
  101. if (n == 0)
  102. break;
  103. if (npixels & 1)
  104. {
  105. op[0] |= lastpixel;
  106. lastpixel = *op++;
  107. npixels++;
  108. n--;
  109. }
  110. else
  111. lastpixel |= lastpixel << 4;
  112. npixels += n;
  113. if (npixels > maxpixels)
  114. break;
  115. for (; n > 0; n -= 2)
  116. *op++ = (uint8_t)lastpixel;
  117. if (n == -1)
  118. *--op &= 0xf0;
  119. lastpixel &= 0xf;
  120. break;
  121. case THUNDER_2BITDELTAS: /* 2-bit deltas */
  122. if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP)
  123. SETPIXEL(op,
  124. (unsigned)((int)lastpixel + twobitdeltas[delta]));
  125. if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
  126. SETPIXEL(op,
  127. (unsigned)((int)lastpixel + twobitdeltas[delta]));
  128. if ((delta = (n & 3)) != DELTA2_SKIP)
  129. SETPIXEL(op,
  130. (unsigned)((int)lastpixel + twobitdeltas[delta]));
  131. break;
  132. case THUNDER_3BITDELTAS: /* 3-bit deltas */
  133. if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
  134. SETPIXEL(
  135. op, (unsigned)((int)lastpixel + threebitdeltas[delta]));
  136. if ((delta = (n & 7)) != DELTA3_SKIP)
  137. SETPIXEL(
  138. op, (unsigned)((int)lastpixel + threebitdeltas[delta]));
  139. break;
  140. case THUNDER_RAW: /* raw data */
  141. SETPIXEL(op, n);
  142. break;
  143. }
  144. }
  145. tif->tif_rawcp = (uint8_t *)bp;
  146. tif->tif_rawcc = cc;
  147. if (npixels != maxpixels)
  148. {
  149. uint8_t *op_end = op0 + (maxpixels + 1) / 2;
  150. memset(op, 0, (size_t)(op_end - op));
  151. TIFFErrorExtR(tif, module,
  152. "%s data at scanline %lu (%" PRIu64 " != %" PRIu64 ")",
  153. npixels < maxpixels ? "Not enough" : "Too much",
  154. (unsigned long)tif->tif_row, (uint64_t)npixels,
  155. (uint64_t)maxpixels);
  156. return (0);
  157. }
  158. return (1);
  159. }
  160. static int ThunderDecodeRow(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
  161. {
  162. static const char module[] = "ThunderDecodeRow";
  163. uint8_t *row = buf;
  164. (void)s;
  165. if (occ % tif->tif_scanlinesize)
  166. {
  167. TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read");
  168. return (0);
  169. }
  170. while (occ > 0)
  171. {
  172. if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
  173. return (0);
  174. occ -= tif->tif_scanlinesize;
  175. row += tif->tif_scanlinesize;
  176. }
  177. return (1);
  178. }
  179. int TIFFInitThunderScan(TIFF *tif, int scheme)
  180. {
  181. (void)scheme;
  182. tif->tif_setupdecode = ThunderSetupDecode;
  183. tif->tif_decoderow = ThunderDecodeRow;
  184. tif->tif_decodestrip = ThunderDecodeRow;
  185. return (1);
  186. }
  187. #endif /* THUNDER_SUPPORT */