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