/src/FreeImage/Source/LibTIFF/tif_thunder.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 189 lines · 119 code · 18 blank · 52 comment · 27 complexity · 2dc1cc1f0fb4ff2d11d330dd5b302b8d MD5 · raw file

  1. /* $Id: tif_thunder.c,v 1.37 2011/04/10 17:14:09 drolon 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] = (tidataval_t) (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, tidata_t op, tsize_t maxpixels)
  79. {
  80. register unsigned char *bp;
  81. register tsize_t cc;
  82. unsigned int lastpixel;
  83. tsize_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++, cc--;
  91. switch (n & THUNDER_CODE) {
  92. case THUNDER_RUN: /* pixel run */
  93. /*
  94. * Replicate the last pixel n times,
  95. * where n is the lower-order 6 bits.
  96. */
  97. if (npixels & 1) {
  98. op[0] |= lastpixel;
  99. lastpixel = *op++; npixels++; n--;
  100. } else
  101. lastpixel |= lastpixel << 4;
  102. npixels += n;
  103. if (npixels < maxpixels) {
  104. for (; n > 0; n -= 2)
  105. *op++ = (tidataval_t) lastpixel;
  106. }
  107. if (n == -1)
  108. *--op &= 0xf0;
  109. lastpixel &= 0xf;
  110. break;
  111. case THUNDER_2BITDELTAS: /* 2-bit deltas */
  112. if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP)
  113. SETPIXEL(op, lastpixel + twobitdeltas[delta]);
  114. if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
  115. SETPIXEL(op, lastpixel + twobitdeltas[delta]);
  116. if ((delta = (n & 3)) != DELTA2_SKIP)
  117. SETPIXEL(op, lastpixel + twobitdeltas[delta]);
  118. break;
  119. case THUNDER_3BITDELTAS: /* 3-bit deltas */
  120. if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
  121. SETPIXEL(op, lastpixel + threebitdeltas[delta]);
  122. if ((delta = (n & 7)) != DELTA3_SKIP)
  123. SETPIXEL(op, lastpixel + threebitdeltas[delta]);
  124. break;
  125. case THUNDER_RAW: /* raw data */
  126. SETPIXEL(op, n);
  127. break;
  128. }
  129. }
  130. tif->tif_rawcp = (tidata_t) bp;
  131. tif->tif_rawcc = cc;
  132. if (npixels != maxpixels) {
  133. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  134. "ThunderDecode: %s data at scanline %ld (%lu != %lu)",
  135. npixels < maxpixels ? "Not enough" : "Too much",
  136. (long) tif->tif_row, (long) npixels, (long) maxpixels);
  137. return (0);
  138. }
  139. return (1);
  140. }
  141. static int
  142. ThunderDecodeRow(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
  143. {
  144. tidata_t row = buf;
  145. (void) s;
  146. while ((long)occ > 0) {
  147. if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
  148. return (0);
  149. occ -= tif->tif_scanlinesize;
  150. row += tif->tif_scanlinesize;
  151. }
  152. return (1);
  153. }
  154. int
  155. TIFFInitThunderScan(TIFF* tif, int scheme)
  156. {
  157. (void) scheme;
  158. tif->tif_decoderow = ThunderDecodeRow;
  159. tif->tif_decodestrip = ThunderDecodeRow;
  160. tif->tif_setupdecode = ThunderSetupDecode;
  161. return (1);
  162. }
  163. #endif /* THUNDER_SUPPORT */
  164. /* vim: set ts=8 sts=8 sw=8 noet: */
  165. /*
  166. * Local Variables:
  167. * mode: c
  168. * c-basic-offset: 8
  169. * fill-column: 78
  170. * End:
  171. */