tif_jbig.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. /*
  25. * TIFF Library.
  26. *
  27. * JBIG Compression Algorithm Support.
  28. * Contributed by Lee Howard <faxguy@deanox.com>
  29. *
  30. */
  31. #include "tiffiop.h"
  32. #ifdef JBIG_SUPPORT
  33. #error #include "jbig.h"
  34. static int JBIGSetupDecode(TIFF *tif)
  35. {
  36. if (TIFFNumberOfStrips(tif) != 1)
  37. {
  38. TIFFErrorExtR(tif, "JBIG",
  39. "Multistrip images not supported in decoder");
  40. return 0;
  41. }
  42. return 1;
  43. }
  44. static int JBIGDecode(TIFF *tif, uint8_t *buffer, tmsize_t size, uint16_t s)
  45. {
  46. struct jbg_dec_state decoder;
  47. int decodeStatus = 0;
  48. unsigned char *pImage = NULL;
  49. unsigned long decodedSize;
  50. (void)s;
  51. if (isFillOrder(tif, tif->tif_dir.td_fillorder))
  52. {
  53. TIFFReverseBits(tif->tif_rawcp, tif->tif_rawcc);
  54. }
  55. jbg_dec_init(&decoder);
  56. #if defined(HAVE_JBG_NEWLEN)
  57. jbg_newlen(tif->tif_rawcp, (size_t)tif->tif_rawcc);
  58. /*
  59. * I do not check the return status of jbg_newlen because even if this
  60. * function fails it does not necessarily mean that decoding the image
  61. * will fail. It is generally only needed for received fax images
  62. * that do not contain the actual length of the image in the BIE
  63. * header. I do not log when an error occurs because that will cause
  64. * problems when converting JBIG encoded TIFF's to
  65. * PostScript. As long as the actual image length is contained in the
  66. * BIE header jbg_dec_in should succeed.
  67. */
  68. #endif /* HAVE_JBG_NEWLEN */
  69. decodeStatus = jbg_dec_in(&decoder, (unsigned char *)tif->tif_rawcp,
  70. (size_t)tif->tif_rawcc, NULL);
  71. if (JBG_EOK != decodeStatus)
  72. {
  73. /*
  74. * XXX: JBG_EN constant was defined in pre-2.0 releases of the
  75. * JBIG-KIT. Since the 2.0 the error reporting functions were
  76. * changed. We will handle both cases here.
  77. */
  78. TIFFErrorExtR(tif, "JBIG", "Error (%d) decoding: %s", decodeStatus,
  79. #if defined(JBG_EN)
  80. jbg_strerror(decodeStatus, JBG_EN)
  81. #else
  82. jbg_strerror(decodeStatus)
  83. #endif
  84. );
  85. jbg_dec_free(&decoder);
  86. return 0;
  87. }
  88. decodedSize = jbg_dec_getsize(&decoder);
  89. if ((tmsize_t)decodedSize < size)
  90. {
  91. TIFFWarningExtR(tif, "JBIG",
  92. "Only decoded %lu bytes, whereas %" TIFF_SSIZE_FORMAT
  93. " requested",
  94. decodedSize, size);
  95. }
  96. else if ((tmsize_t)decodedSize > size)
  97. {
  98. TIFFErrorExtR(tif, "JBIG",
  99. "Decoded %lu bytes, whereas %" TIFF_SSIZE_FORMAT
  100. " were requested",
  101. decodedSize, size);
  102. jbg_dec_free(&decoder);
  103. return 0;
  104. }
  105. pImage = jbg_dec_getimage(&decoder, 0);
  106. _TIFFmemcpy(buffer, pImage, decodedSize);
  107. jbg_dec_free(&decoder);
  108. tif->tif_rawcp += tif->tif_rawcc;
  109. tif->tif_rawcc = 0;
  110. return 1;
  111. }
  112. static int JBIGSetupEncode(TIFF *tif)
  113. {
  114. if (TIFFNumberOfStrips(tif) != 1)
  115. {
  116. TIFFErrorExtR(tif, "JBIG",
  117. "Multistrip images not supported in encoder");
  118. return 0;
  119. }
  120. return 1;
  121. }
  122. static int JBIGCopyEncodedData(TIFF *tif, unsigned char *pp, size_t cc,
  123. uint16_t s)
  124. {
  125. (void)s;
  126. while (cc > 0)
  127. {
  128. tmsize_t n = (tmsize_t)cc;
  129. if (tif->tif_rawcc + n > tif->tif_rawdatasize)
  130. {
  131. n = tif->tif_rawdatasize - tif->tif_rawcc;
  132. }
  133. assert(n > 0);
  134. _TIFFmemcpy(tif->tif_rawcp, pp, n);
  135. tif->tif_rawcp += n;
  136. tif->tif_rawcc += n;
  137. pp += n;
  138. cc -= (size_t)n;
  139. if (tif->tif_rawcc >= tif->tif_rawdatasize && !TIFFFlushData1(tif))
  140. {
  141. return (-1);
  142. }
  143. }
  144. return (1);
  145. }
  146. static void JBIGOutputBie(unsigned char *buffer, size_t len, void *userData)
  147. {
  148. TIFF *tif = (TIFF *)userData;
  149. if (isFillOrder(tif, tif->tif_dir.td_fillorder))
  150. {
  151. TIFFReverseBits(buffer, (tmsize_t)len);
  152. }
  153. JBIGCopyEncodedData(tif, buffer, len, 0);
  154. }
  155. static int JBIGEncode(TIFF *tif, uint8_t *buffer, tmsize_t size, uint16_t s)
  156. {
  157. TIFFDirectory *dir = &tif->tif_dir;
  158. struct jbg_enc_state encoder;
  159. (void)size, (void)s;
  160. jbg_enc_init(&encoder, dir->td_imagewidth, dir->td_imagelength, 1, &buffer,
  161. JBIGOutputBie, tif);
  162. /*
  163. * jbg_enc_out does the "real" encoding. As data is encoded,
  164. * JBIGOutputBie is called, which writes the data to the directory.
  165. */
  166. jbg_enc_out(&encoder);
  167. jbg_enc_free(&encoder);
  168. return 1;
  169. }
  170. int TIFFInitJBIG(TIFF *tif, int scheme)
  171. {
  172. (void)scheme;
  173. assert(scheme == COMPRESSION_JBIG);
  174. /*
  175. * These flags are set so the JBIG Codec can control when to reverse
  176. * bits and when not to and to allow the jbig decoder and bit reverser
  177. * to write to memory when necessary.
  178. */
  179. tif->tif_flags |= TIFF_NOBITREV;
  180. tif->tif_flags &= ~TIFF_MAPPED;
  181. /* We may have read from a previous IFD and thus set TIFF_BUFFERMMAP and
  182. * cleared TIFF_MYBUFFER. It is necessary to restore them to their initial
  183. * value to be consistent with the state of a non-memory mapped file.
  184. */
  185. if (tif->tif_flags & TIFF_BUFFERMMAP)
  186. {
  187. tif->tif_rawdata = NULL;
  188. tif->tif_rawdatasize = 0;
  189. tif->tif_flags &= ~TIFF_BUFFERMMAP;
  190. tif->tif_flags |= TIFF_MYBUFFER;
  191. }
  192. /* Setup the function pointers for encode, decode, and cleanup. */
  193. tif->tif_setupdecode = JBIGSetupDecode;
  194. tif->tif_decodestrip = JBIGDecode;
  195. tif->tif_setupencode = JBIGSetupEncode;
  196. tif->tif_encodestrip = JBIGEncode;
  197. return 1;
  198. }
  199. #endif /* JBIG_SUPPORT */