tif_jbig.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. memset(buffer, 0, (size_t)size);
  86. jbg_dec_free(&decoder);
  87. return 0;
  88. }
  89. decodedSize = jbg_dec_getsize(&decoder);
  90. if ((tmsize_t)decodedSize < size)
  91. {
  92. memset(buffer + decodedSize, 0, (size_t)(size - decodedSize));
  93. TIFFWarningExtR(tif, "JBIG",
  94. "Only decoded %lu bytes, whereas %" TIFF_SSIZE_FORMAT
  95. " requested",
  96. decodedSize, size);
  97. }
  98. else if ((tmsize_t)decodedSize > size)
  99. {
  100. TIFFErrorExtR(tif, "JBIG",
  101. "Decoded %lu bytes, whereas %" TIFF_SSIZE_FORMAT
  102. " were requested",
  103. decodedSize, size);
  104. jbg_dec_free(&decoder);
  105. return 0;
  106. }
  107. pImage = jbg_dec_getimage(&decoder, 0);
  108. _TIFFmemcpy(buffer, pImage, decodedSize);
  109. jbg_dec_free(&decoder);
  110. tif->tif_rawcp += tif->tif_rawcc;
  111. tif->tif_rawcc = 0;
  112. return 1;
  113. }
  114. static int JBIGSetupEncode(TIFF *tif)
  115. {
  116. if (TIFFNumberOfStrips(tif) != 1)
  117. {
  118. TIFFErrorExtR(tif, "JBIG",
  119. "Multistrip images not supported in encoder");
  120. return 0;
  121. }
  122. return 1;
  123. }
  124. static int JBIGCopyEncodedData(TIFF *tif, unsigned char *pp, size_t cc,
  125. uint16_t s)
  126. {
  127. (void)s;
  128. while (cc > 0)
  129. {
  130. tmsize_t n = (tmsize_t)cc;
  131. if (tif->tif_rawcc + n > tif->tif_rawdatasize)
  132. {
  133. n = tif->tif_rawdatasize - tif->tif_rawcc;
  134. }
  135. assert(n > 0);
  136. _TIFFmemcpy(tif->tif_rawcp, pp, n);
  137. tif->tif_rawcp += n;
  138. tif->tif_rawcc += n;
  139. pp += n;
  140. cc -= (size_t)n;
  141. if (tif->tif_rawcc >= tif->tif_rawdatasize && !TIFFFlushData1(tif))
  142. {
  143. return (-1);
  144. }
  145. }
  146. return (1);
  147. }
  148. static void JBIGOutputBie(unsigned char *buffer, size_t len, void *userData)
  149. {
  150. TIFF *tif = (TIFF *)userData;
  151. if (isFillOrder(tif, tif->tif_dir.td_fillorder))
  152. {
  153. TIFFReverseBits(buffer, (tmsize_t)len);
  154. }
  155. JBIGCopyEncodedData(tif, buffer, len, 0);
  156. }
  157. static int JBIGEncode(TIFF *tif, uint8_t *buffer, tmsize_t size, uint16_t s)
  158. {
  159. TIFFDirectory *dir = &tif->tif_dir;
  160. struct jbg_enc_state encoder;
  161. (void)size, (void)s;
  162. jbg_enc_init(&encoder, dir->td_imagewidth, dir->td_imagelength, 1, &buffer,
  163. JBIGOutputBie, tif);
  164. /*
  165. * jbg_enc_out does the "real" encoding. As data is encoded,
  166. * JBIGOutputBie is called, which writes the data to the directory.
  167. */
  168. jbg_enc_out(&encoder);
  169. jbg_enc_free(&encoder);
  170. return 1;
  171. }
  172. int TIFFInitJBIG(TIFF *tif, int scheme)
  173. {
  174. (void)scheme;
  175. assert(scheme == COMPRESSION_JBIG);
  176. /*
  177. * These flags are set so the JBIG Codec can control when to reverse
  178. * bits and when not to and to allow the jbig decoder and bit reverser
  179. * to write to memory when necessary.
  180. */
  181. tif->tif_flags |= TIFF_NOBITREV;
  182. tif->tif_flags &= ~TIFF_MAPPED;
  183. /* We may have read from a previous IFD and thus set TIFF_BUFFERMMAP and
  184. * cleared TIFF_MYBUFFER. It is necessary to restore them to their initial
  185. * value to be consistent with the state of a non-memory mapped file.
  186. */
  187. if (tif->tif_flags & TIFF_BUFFERMMAP)
  188. {
  189. tif->tif_rawdata = NULL;
  190. tif->tif_rawdatasize = 0;
  191. tif->tif_flags &= ~TIFF_BUFFERMMAP;
  192. tif->tif_flags |= TIFF_MYBUFFER;
  193. }
  194. /* Setup the function pointers for encode, decode, and cleanup. */
  195. tif->tif_setupdecode = JBIGSetupDecode;
  196. tif->tif_decodestrip = JBIGDecode;
  197. tif->tif_setupencode = JBIGSetupEncode;
  198. tif->tif_encodestrip = JBIGEncode;
  199. return 1;
  200. }
  201. #endif /* JBIG_SUPPORT */