/src/libtiff/libtiff/tif_jbig.c
Line | Count | Source |
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 | | /* |
26 | | * TIFF Library. |
27 | | * |
28 | | * JBIG Compression Algorithm Support. |
29 | | * Contributed by Lee Howard <faxguy@deanox.com> |
30 | | * |
31 | | */ |
32 | | |
33 | | #include "tiffiop.h" |
34 | | |
35 | | #ifdef JBIG_SUPPORT |
36 | | #include "jbig.h" |
37 | | |
38 | | static int JBIGSetupDecode(TIFF *tif) |
39 | 1 | { |
40 | 1 | if (TIFFNumberOfStrips(tif) != 1) |
41 | 0 | { |
42 | 0 | TIFFErrorExtR(tif, "JBIG", |
43 | 0 | "Multistrip images not supported in decoder"); |
44 | 0 | return 0; |
45 | 0 | } |
46 | | |
47 | 1 | return 1; |
48 | 1 | } |
49 | | |
50 | | static int JBIGDecode(TIFF *tif, uint8_t *buffer, tmsize_t size, uint16_t s) |
51 | 0 | { |
52 | 0 | struct jbg_dec_state decoder; |
53 | 0 | int decodeStatus = 0; |
54 | 0 | unsigned char *pImage = NULL; |
55 | 0 | unsigned long decodedSize; |
56 | 0 | (void)s; |
57 | |
|
58 | 0 | if (isFillOrder(tif, tif->tif_dir.td_fillorder)) |
59 | 0 | { |
60 | 0 | TIFFReverseBits(tif->tif_rawcp, tif->tif_rawcc); |
61 | 0 | } |
62 | |
|
63 | 0 | jbg_dec_init(&decoder); |
64 | |
|
65 | 0 | #if defined(HAVE_JBG_NEWLEN) |
66 | 0 | jbg_newlen(tif->tif_rawcp, (size_t)tif->tif_rawcc); |
67 | | /* |
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. |
76 | | */ |
77 | 0 | #endif /* HAVE_JBG_NEWLEN */ |
78 | |
|
79 | 0 | decodeStatus = jbg_dec_in(&decoder, (unsigned char *)tif->tif_rawcp, |
80 | 0 | (size_t)tif->tif_rawcc, NULL); |
81 | 0 | if (JBG_EOK != decodeStatus) |
82 | 0 | { |
83 | | /* |
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. |
87 | | */ |
88 | 0 | TIFFErrorExtR(tif, "JBIG", "Error (%d) decoding: %s", decodeStatus, |
89 | | #if defined(JBG_EN) |
90 | | jbg_strerror(decodeStatus, JBG_EN) |
91 | | #else |
92 | 0 | jbg_strerror(decodeStatus) |
93 | 0 | #endif |
94 | 0 | ); |
95 | 0 | memset(buffer, 0, (size_t)size); |
96 | 0 | jbg_dec_free(&decoder); |
97 | 0 | return 0; |
98 | 0 | } |
99 | | |
100 | 0 | decodedSize = jbg_dec_getsize(&decoder); |
101 | 0 | if ((tmsize_t)decodedSize < size) |
102 | 0 | { |
103 | 0 | memset(buffer + decodedSize, 0, (size_t)(size - decodedSize)); |
104 | 0 | TIFFWarningExtR(tif, "JBIG", |
105 | 0 | "Only decoded %lu bytes, whereas %" TIFF_SSIZE_FORMAT |
106 | 0 | " requested", |
107 | 0 | decodedSize, size); |
108 | 0 | } |
109 | 0 | else if ((tmsize_t)decodedSize > size) |
110 | 0 | { |
111 | 0 | TIFFErrorExtR(tif, "JBIG", |
112 | 0 | "Decoded %lu bytes, whereas %" TIFF_SSIZE_FORMAT |
113 | 0 | " were requested", |
114 | 0 | decodedSize, size); |
115 | 0 | jbg_dec_free(&decoder); |
116 | 0 | return 0; |
117 | 0 | } |
118 | 0 | pImage = jbg_dec_getimage(&decoder, 0); |
119 | 0 | _TIFFmemcpy(buffer, pImage, decodedSize); |
120 | 0 | jbg_dec_free(&decoder); |
121 | |
|
122 | 0 | tif->tif_rawcp += tif->tif_rawcc; |
123 | 0 | tif->tif_rawcc = 0; |
124 | |
|
125 | 0 | return 1; |
126 | 0 | } |
127 | | |
128 | | static int JBIGSetupEncode(TIFF *tif) |
129 | 0 | { |
130 | 0 | if (TIFFNumberOfStrips(tif) != 1) |
131 | 0 | { |
132 | 0 | TIFFErrorExtR(tif, "JBIG", |
133 | 0 | "Multistrip images not supported in encoder"); |
134 | 0 | return 0; |
135 | 0 | } |
136 | | |
137 | 0 | return 1; |
138 | 0 | } |
139 | | |
140 | | static int JBIGCopyEncodedData(TIFF *tif, unsigned char *pp, size_t cc, |
141 | | uint16_t s) |
142 | 0 | { |
143 | 0 | (void)s; |
144 | 0 | while (cc > 0) |
145 | 0 | { |
146 | 0 | tmsize_t n = (tmsize_t)cc; |
147 | |
|
148 | 0 | if (tif->tif_rawcc + n > tif->tif_rawdatasize) |
149 | 0 | { |
150 | 0 | n = tif->tif_rawdatasize - tif->tif_rawcc; |
151 | 0 | } |
152 | |
|
153 | 0 | assert(n > 0); |
154 | 0 | _TIFFmemcpy(tif->tif_rawcp, pp, n); |
155 | 0 | tif->tif_rawcp += n; |
156 | 0 | tif->tif_rawcc += n; |
157 | 0 | pp += n; |
158 | 0 | cc -= (size_t)n; |
159 | 0 | if (tif->tif_rawcc >= tif->tif_rawdatasize && !TIFFFlushData1(tif)) |
160 | 0 | { |
161 | 0 | return (-1); |
162 | 0 | } |
163 | 0 | } |
164 | | |
165 | 0 | return (1); |
166 | 0 | } |
167 | | |
168 | | static void JBIGOutputBie(unsigned char *buffer, size_t len, void *userData) |
169 | 0 | { |
170 | 0 | TIFF *tif = (TIFF *)userData; |
171 | |
|
172 | 0 | if (isFillOrder(tif, tif->tif_dir.td_fillorder)) |
173 | 0 | { |
174 | 0 | TIFFReverseBits(buffer, (tmsize_t)len); |
175 | 0 | } |
176 | |
|
177 | 0 | JBIGCopyEncodedData(tif, buffer, len, 0); |
178 | 0 | } |
179 | | |
180 | | static int JBIGEncode(TIFF *tif, uint8_t *buffer, tmsize_t size, uint16_t s) |
181 | 0 | { |
182 | 0 | TIFFDirectory *dir = &tif->tif_dir; |
183 | 0 | struct jbg_enc_state encoder; |
184 | |
|
185 | 0 | (void)size, (void)s; |
186 | |
|
187 | 0 | jbg_enc_init(&encoder, dir->td_imagewidth, dir->td_imagelength, 1, &buffer, |
188 | 0 | JBIGOutputBie, tif); |
189 | | /* |
190 | | * jbg_enc_out does the "real" encoding. As data is encoded, |
191 | | * JBIGOutputBie is called, which writes the data to the directory. |
192 | | */ |
193 | 0 | jbg_enc_out(&encoder); |
194 | 0 | jbg_enc_free(&encoder); |
195 | |
|
196 | 0 | return 1; |
197 | 0 | } |
198 | | |
199 | | int TIFFInitJBIG(TIFF *tif, int scheme) |
200 | 1 | { |
201 | 1 | (void)scheme; |
202 | 1 | assert(scheme == COMPRESSION_JBIG); |
203 | | |
204 | | /* |
205 | | * These flags are set so the JBIG Codec can control when to reverse |
206 | | * bits and when not to and to allow the jbig decoder and bit reverser |
207 | | * to write to memory when necessary. |
208 | | */ |
209 | 1 | tif->tif_flags |= TIFF_NOBITREV; |
210 | 1 | tif->tif_flags &= ~TIFF_MAPPED; |
211 | | /* We may have read from a previous IFD and thus set TIFF_BUFFERMMAP and |
212 | | * cleared TIFF_MYBUFFER. It is necessary to restore them to their initial |
213 | | * value to be consistent with the state of a non-memory mapped file. |
214 | | */ |
215 | 1 | if (tif->tif_flags & TIFF_BUFFERMMAP) |
216 | 0 | { |
217 | 0 | tif->tif_rawdata = NULL; |
218 | 0 | tif->tif_rawdatasize = 0; |
219 | 0 | tif->tif_flags &= ~TIFF_BUFFERMMAP; |
220 | 0 | tif->tif_flags |= TIFF_MYBUFFER; |
221 | 0 | } |
222 | | |
223 | | /* Setup the function pointers for encode, decode, and cleanup. */ |
224 | 1 | tif->tif_setupdecode = JBIGSetupDecode; |
225 | 1 | tif->tif_decodestrip = JBIGDecode; |
226 | | |
227 | 1 | tif->tif_setupencode = JBIGSetupEncode; |
228 | 1 | tif->tif_encodestrip = JBIGEncode; |
229 | | |
230 | 1 | return 1; |
231 | 1 | } |
232 | | |
233 | | #endif /* JBIG_SUPPORT */ |