tif_thunder.c 7.1 KB

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