2
0

tif_jbig.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* $Id: tif_jbig.c,v 1.15 2010-03-10 18:56:48 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. /*
  26. * TIFF Library.
  27. *
  28. * JBIG Compression Algorithm Support.
  29. * Contributed by Lee Howard <faxguy@deanox.com>
  30. *
  31. */
  32. #include "tiffiop.h"
  33. #ifdef JBIG_SUPPORT
  34. #include "jbig.h"
  35. static int JBIGSetupDecode(TIFF* tif)
  36. {
  37. if (TIFFNumberOfStrips(tif) != 1)
  38. {
  39. TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in decoder");
  40. return 0;
  41. }
  42. return 1;
  43. }
  44. static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
  45. {
  46. struct jbg_dec_state decoder;
  47. int decodeStatus = 0;
  48. unsigned char* pImage = NULL;
  49. (void) size, (void) s;
  50. if (isFillOrder(tif, tif->tif_dir.td_fillorder))
  51. {
  52. TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdatasize);
  53. }
  54. jbg_dec_init(&decoder);
  55. #if defined(HAVE_JBG_NEWLEN)
  56. jbg_newlen(tif->tif_rawdata, (size_t)tif->tif_rawdatasize);
  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_rawdata,
  69. (size_t)tif->tif_rawdatasize, 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. return 0;
  87. }
  88. pImage = jbg_dec_getimage(&decoder, 0);
  89. _TIFFmemcpy(buffer, pImage, jbg_dec_getsize(&decoder));
  90. jbg_dec_free(&decoder);
  91. return 1;
  92. }
  93. static int JBIGSetupEncode(TIFF* tif)
  94. {
  95. if (TIFFNumberOfStrips(tif) != 1)
  96. {
  97. TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in encoder");
  98. return 0;
  99. }
  100. return 1;
  101. }
  102. static int JBIGCopyEncodedData(TIFF* tif, unsigned char* pp, size_t cc, uint16 s)
  103. {
  104. (void) s;
  105. while (cc > 0)
  106. {
  107. tmsize_t n = (tmsize_t)cc;
  108. if (tif->tif_rawcc + n > tif->tif_rawdatasize)
  109. {
  110. n = tif->tif_rawdatasize - tif->tif_rawcc;
  111. }
  112. assert(n > 0);
  113. _TIFFmemcpy(tif->tif_rawcp, pp, n);
  114. tif->tif_rawcp += n;
  115. tif->tif_rawcc += n;
  116. pp += n;
  117. cc -= (size_t)n;
  118. if (tif->tif_rawcc >= tif->tif_rawdatasize &&
  119. !TIFFFlushData1(tif))
  120. {
  121. return (-1);
  122. }
  123. }
  124. return (1);
  125. }
  126. static void JBIGOutputBie(unsigned char* buffer, size_t len, void* userData)
  127. {
  128. TIFF* tif = (TIFF*)userData;
  129. if (isFillOrder(tif, tif->tif_dir.td_fillorder))
  130. {
  131. TIFFReverseBits(buffer, (tmsize_t)len);
  132. }
  133. JBIGCopyEncodedData(tif, buffer, len, 0);
  134. }
  135. static int JBIGEncode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
  136. {
  137. TIFFDirectory* dir = &tif->tif_dir;
  138. struct jbg_enc_state encoder;
  139. (void) size, (void) s;
  140. jbg_enc_init(&encoder,
  141. dir->td_imagewidth,
  142. dir->td_imagelength,
  143. 1,
  144. &buffer,
  145. JBIGOutputBie,
  146. tif);
  147. /*
  148. * jbg_enc_out does the "real" encoding. As data is encoded,
  149. * JBIGOutputBie is called, which writes the data to the directory.
  150. */
  151. jbg_enc_out(&encoder);
  152. jbg_enc_free(&encoder);
  153. return 1;
  154. }
  155. int TIFFInitJBIG(TIFF* tif, int scheme)
  156. {
  157. assert(scheme == COMPRESSION_JBIG);
  158. /*
  159. * These flags are set so the JBIG Codec can control when to reverse
  160. * bits and when not to and to allow the jbig decoder and bit reverser
  161. * to write to memory when necessary.
  162. */
  163. tif->tif_flags |= TIFF_NOBITREV;
  164. tif->tif_flags &= ~TIFF_MAPPED;
  165. /* Setup the function pointers for encode, decode, and cleanup. */
  166. tif->tif_setupdecode = JBIGSetupDecode;
  167. tif->tif_decodestrip = JBIGDecode;
  168. tif->tif_setupencode = JBIGSetupEncode;
  169. tif->tif_encodestrip = JBIGEncode;
  170. return 1;
  171. }
  172. #endif /* JBIG_SUPPORT */
  173. /* vim: set ts=8 sts=8 sw=8 noet: */
  174. /*
  175. * Local Variables:
  176. * mode: c
  177. * c-basic-offset: 8
  178. * fill-column: 78
  179. * End:
  180. */