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