tif_packbits.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* $Id: tif_packbits.c,v 1.20 2010-03-10 18:56:49 bfriesen 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. #ifdef PACKBITS_SUPPORT
  27. /*
  28. * TIFF Library.
  29. *
  30. * PackBits Compression Algorithm Support
  31. */
  32. #include <stdio.h>
  33. static int
  34. PackBitsPreEncode(TIFF* tif, uint16 s)
  35. {
  36. (void) s;
  37. if (!(tif->tif_data = (uint8*)_TIFFmalloc(sizeof(tmsize_t))))
  38. return (0);
  39. /*
  40. * Calculate the scanline/tile-width size in bytes.
  41. */
  42. if (isTiled(tif))
  43. *(tmsize_t*)tif->tif_data = TIFFTileRowSize(tif);
  44. else
  45. *(tmsize_t*)tif->tif_data = TIFFScanlineSize(tif);
  46. return (1);
  47. }
  48. static int
  49. PackBitsPostEncode(TIFF* tif)
  50. {
  51. if (tif->tif_data)
  52. _TIFFfree(tif->tif_data);
  53. return (1);
  54. }
  55. /*
  56. * Encode a run of pixels.
  57. */
  58. static int
  59. PackBitsEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
  60. {
  61. unsigned char* bp = (unsigned char*) buf;
  62. uint8* op;
  63. uint8* ep;
  64. uint8* lastliteral;
  65. long n, slop;
  66. int b;
  67. enum { BASE, LITERAL, RUN, LITERAL_RUN } state;
  68. (void) s;
  69. op = tif->tif_rawcp;
  70. ep = tif->tif_rawdata + tif->tif_rawdatasize;
  71. state = BASE;
  72. lastliteral = 0;
  73. while (cc > 0) {
  74. /*
  75. * Find the longest string of identical bytes.
  76. */
  77. b = *bp++, cc--, n = 1;
  78. for (; cc > 0 && b == *bp; cc--, bp++)
  79. n++;
  80. again:
  81. if (op + 2 >= ep) { /* insure space for new data */
  82. /*
  83. * Be careful about writing the last
  84. * literal. Must write up to that point
  85. * and then copy the remainder to the
  86. * front of the buffer.
  87. */
  88. if (state == LITERAL || state == LITERAL_RUN) {
  89. slop = (long)(op - lastliteral);
  90. tif->tif_rawcc += (tmsize_t)(lastliteral - tif->tif_rawcp);
  91. if (!TIFFFlushData1(tif))
  92. return (-1);
  93. op = tif->tif_rawcp;
  94. while (slop-- > 0)
  95. *op++ = *lastliteral++;
  96. lastliteral = tif->tif_rawcp;
  97. } else {
  98. tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp);
  99. if (!TIFFFlushData1(tif))
  100. return (-1);
  101. op = tif->tif_rawcp;
  102. }
  103. }
  104. switch (state) {
  105. case BASE: /* initial state, set run/literal */
  106. if (n > 1) {
  107. state = RUN;
  108. if (n > 128) {
  109. *op++ = (uint8) -127;
  110. *op++ = (uint8) b;
  111. n -= 128;
  112. goto again;
  113. }
  114. *op++ = (uint8)(-(n-1));
  115. *op++ = (uint8) b;
  116. } else {
  117. lastliteral = op;
  118. *op++ = 0;
  119. *op++ = (uint8) b;
  120. state = LITERAL;
  121. }
  122. break;
  123. case LITERAL: /* last object was literal string */
  124. if (n > 1) {
  125. state = LITERAL_RUN;
  126. if (n > 128) {
  127. *op++ = (uint8) -127;
  128. *op++ = (uint8) b;
  129. n -= 128;
  130. goto again;
  131. }
  132. *op++ = (uint8)(-(n-1)); /* encode run */
  133. *op++ = (uint8) b;
  134. } else { /* extend literal */
  135. if (++(*lastliteral) == 127)
  136. state = BASE;
  137. *op++ = (uint8) b;
  138. }
  139. break;
  140. case RUN: /* last object was run */
  141. if (n > 1) {
  142. if (n > 128) {
  143. *op++ = (uint8) -127;
  144. *op++ = (uint8) b;
  145. n -= 128;
  146. goto again;
  147. }
  148. *op++ = (uint8)(-(n-1));
  149. *op++ = (uint8) b;
  150. } else {
  151. lastliteral = op;
  152. *op++ = 0;
  153. *op++ = (uint8) b;
  154. state = LITERAL;
  155. }
  156. break;
  157. case LITERAL_RUN: /* literal followed by a run */
  158. /*
  159. * Check to see if previous run should
  160. * be converted to a literal, in which
  161. * case we convert literal-run-literal
  162. * to a single literal.
  163. */
  164. if (n == 1 && op[-2] == (uint8) -1 &&
  165. *lastliteral < 126) {
  166. state = (((*lastliteral) += 2) == 127 ?
  167. BASE : LITERAL);
  168. op[-2] = op[-1]; /* replicate */
  169. } else
  170. state = RUN;
  171. goto again;
  172. }
  173. }
  174. tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp);
  175. tif->tif_rawcp = op;
  176. return (1);
  177. }
  178. /*
  179. * Encode a rectangular chunk of pixels. We break it up
  180. * into row-sized pieces to insure that encoded runs do
  181. * not span rows. Otherwise, there can be problems with
  182. * the decoder if data is read, for example, by scanlines
  183. * when it was encoded by strips.
  184. */
  185. static int
  186. PackBitsEncodeChunk(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  187. {
  188. tmsize_t rowsize = *(tmsize_t*)tif->tif_data;
  189. while (cc > 0) {
  190. tmsize_t chunk = rowsize;
  191. if( cc < chunk )
  192. chunk = cc;
  193. if (PackBitsEncode(tif, bp, chunk, s) < 0)
  194. return (-1);
  195. bp += chunk;
  196. cc -= chunk;
  197. }
  198. return (1);
  199. }
  200. static int
  201. PackBitsDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
  202. {
  203. static const char module[] = "PackBitsDecode";
  204. char *bp;
  205. tmsize_t cc;
  206. long n;
  207. int b;
  208. (void) s;
  209. bp = (char*) tif->tif_rawcp;
  210. cc = tif->tif_rawcc;
  211. while (cc > 0 && occ > 0) {
  212. n = (long) *bp++, cc--;
  213. /*
  214. * Watch out for compilers that
  215. * don't sign extend chars...
  216. */
  217. if (n >= 128)
  218. n -= 256;
  219. if (n < 0) { /* replicate next byte -n+1 times */
  220. if (n == -128) /* nop */
  221. continue;
  222. n = -n + 1;
  223. if( occ < (tmsize_t)n )
  224. {
  225. TIFFWarningExt(tif->tif_clientdata, module,
  226. "Discarding %lu bytes to avoid buffer overrun",
  227. (unsigned long) ((tmsize_t)n - occ));
  228. n = (long)occ;
  229. }
  230. occ -= n;
  231. b = *bp++, cc--; /* TODO: may be reading past input buffer here when input data is corrupt or ends prematurely */
  232. while (n-- > 0)
  233. *op++ = (uint8) b;
  234. } else { /* copy next n+1 bytes literally */
  235. if (occ < (tmsize_t)(n + 1))
  236. {
  237. TIFFWarningExt(tif->tif_clientdata, module,
  238. "Discarding %lu bytes to avoid buffer overrun",
  239. (unsigned long) ((tmsize_t)n - occ + 1));
  240. n = (long)occ - 1;
  241. }
  242. _TIFFmemcpy(op, bp, ++n); /* TODO: may be reading past input buffer here when input data is corrupt or ends prematurely */
  243. op += n; occ -= n;
  244. bp += n; cc -= n;
  245. }
  246. }
  247. tif->tif_rawcp = (uint8*) bp;
  248. tif->tif_rawcc = cc;
  249. if (occ > 0) {
  250. TIFFErrorExt(tif->tif_clientdata, module,
  251. "Not enough data for scanline %lu",
  252. (unsigned long) tif->tif_row);
  253. return (0);
  254. }
  255. return (1);
  256. }
  257. int
  258. TIFFInitPackBits(TIFF* tif, int scheme)
  259. {
  260. (void) scheme;
  261. tif->tif_decoderow = PackBitsDecode;
  262. tif->tif_decodestrip = PackBitsDecode;
  263. tif->tif_decodetile = PackBitsDecode;
  264. tif->tif_preencode = PackBitsPreEncode;
  265. tif->tif_postencode = PackBitsPostEncode;
  266. tif->tif_encoderow = PackBitsEncode;
  267. tif->tif_encodestrip = PackBitsEncodeChunk;
  268. tif->tif_encodetile = PackBitsEncodeChunk;
  269. return (1);
  270. }
  271. #endif /* PACKBITS_SUPPORT */
  272. /* vim: set ts=8 sts=8 sw=8 noet: */
  273. /*
  274. * Local Variables:
  275. * mode: c
  276. * c-basic-offset: 8
  277. * fill-column: 78
  278. * End:
  279. */