1 /* $Id: tif_jbig.c,v 1.15 2010-03-10 18:56:48 bfriesen Exp $ */
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
30 * JBIG Compression Algorithm Support.
31 * Contributed by Lee Howard <faxguy@deanox.com>
40 static int JBIGSetupDecode(TIFF* tif)
42 if (TIFFNumberOfStrips(tif) != 1)
44 TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in decoder");
51 static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
53 struct jbg_dec_state decoder;
55 unsigned char* pImage = NULL;
56 (void) size, (void) s;
58 if (isFillOrder(tif, tif->tif_dir.td_fillorder))
60 TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdatasize);
63 jbg_dec_init(&decoder);
65 #if defined(HAVE_JBG_NEWLEN)
66 jbg_newlen(tif->tif_rawdata, (size_t)tif->tif_rawdatasize);
68 * I do not check the return status of jbg_newlen because even if this
69 * function fails it does not necessarily mean that decoding the image
70 * will fail. It is generally only needed for received fax images
71 * that do not contain the actual length of the image in the BIE
72 * header. I do not log when an error occurs because that will cause
73 * problems when converting JBIG encoded TIFF's to
74 * PostScript. As long as the actual image length is contained in the
75 * BIE header jbg_dec_in should succeed.
77 #endif /* HAVE_JBG_NEWLEN */
79 decodeStatus = jbg_dec_in(&decoder, (unsigned char*)tif->tif_rawdata,
80 (size_t)tif->tif_rawdatasize, NULL);
81 if (JBG_EOK != decodeStatus)
84 * XXX: JBG_EN constant was defined in pre-2.0 releases of the
85 * JBIG-KIT. Since the 2.0 the error reporting functions were
86 * changed. We will handle both cases here.
88 TIFFErrorExt(tif->tif_clientdata,
89 "JBIG", "Error (%d) decoding: %s",
92 jbg_strerror(decodeStatus, JBG_EN)
94 jbg_strerror(decodeStatus)
100 pImage = jbg_dec_getimage(&decoder, 0);
101 _TIFFmemcpy(buffer, pImage, jbg_dec_getsize(&decoder));
102 jbg_dec_free(&decoder);
106 static int JBIGSetupEncode(TIFF* tif)
108 if (TIFFNumberOfStrips(tif) != 1)
110 TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in encoder");
117 static int JBIGCopyEncodedData(TIFF* tif, unsigned char* pp, size_t cc, uint16 s)
122 tmsize_t n = (tmsize_t)cc;
124 if (tif->tif_rawcc + n > tif->tif_rawdatasize)
126 n = tif->tif_rawdatasize - tif->tif_rawcc;
130 _TIFFmemcpy(tif->tif_rawcp, pp, n);
135 if (tif->tif_rawcc >= tif->tif_rawdatasize &&
136 !TIFFFlushData1(tif))
145 static void JBIGOutputBie(unsigned char* buffer, size_t len, void* userData)
147 TIFF* tif = (TIFF*)userData;
149 if (isFillOrder(tif, tif->tif_dir.td_fillorder))
151 TIFFReverseBits(buffer, (tmsize_t)len);
154 JBIGCopyEncodedData(tif, buffer, len, 0);
157 static int JBIGEncode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
159 TIFFDirectory* dir = &tif->tif_dir;
160 struct jbg_enc_state encoder;
162 (void) size, (void) s;
164 jbg_enc_init(&encoder,
172 * jbg_enc_out does the "real" encoding. As data is encoded,
173 * JBIGOutputBie is called, which writes the data to the directory.
175 jbg_enc_out(&encoder);
176 jbg_enc_free(&encoder);
181 int TIFFInitJBIG(TIFF* tif, int scheme)
183 assert(scheme == COMPRESSION_JBIG);
186 * These flags are set so the JBIG Codec can control when to reverse
187 * bits and when not to and to allow the jbig decoder and bit reverser
188 * to write to memory when necessary.
190 tif->tif_flags |= TIFF_NOBITREV;
191 tif->tif_flags &= ~TIFF_MAPPED;
193 /* Setup the function pointers for encode, decode, and cleanup. */
194 tif->tif_setupdecode = JBIGSetupDecode;
195 tif->tif_decodestrip = JBIGDecode;
197 tif->tif_setupencode = JBIGSetupEncode;
198 tif->tif_encodestrip = JBIGEncode;
203 #endif /* JBIG_SUPPORT */
205 /* vim: set ts=8 sts=8 sw=8 noet: */