Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/libtiff-src/tif_jbig.c

raw
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 <[email protected]>
30 *
31 */
32
33 #include "tiffiop.h"
34
35 #ifdef JBIG_SUPPORT
36 #include "jbig.h"
37
38 static int JBIGSetupDecode(TIFF *tif)
39 {
40 if (TIFFNumberOfStrips(tif) != 1)
41 {
42 TIFFErrorExtR(tif, "JBIG",
43 "Multistrip images not supported in decoder");
44 return 0;
45 }
46
47 return 1;
48 }
49
50 static int JBIGDecode(TIFF *tif, uint8_t *buffer, tmsize_t size, uint16_t s)
51 {
52 struct jbg_dec_state decoder;
53 int decodeStatus = 0;
54 unsigned char *pImage = NULL;
55 unsigned long decodedSize;
56 (void)s;
57
58 if (isFillOrder(tif, tif->tif_dir.td_fillorder))
59 {
60 TIFFReverseBits(tif->tif_rawcp, tif->tif_rawcc);
61 }
62
63 jbg_dec_init(&decoder);
64
65 #if defined(HAVE_JBG_NEWLEN)
66 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 #endif /* HAVE_JBG_NEWLEN */
78
79 decodeStatus = jbg_dec_in(&decoder, (unsigned char *)tif->tif_rawcp,
80 (size_t)tif->tif_rawcc, NULL);
81 if (JBG_EOK != decodeStatus)
82 {
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 TIFFErrorExtR(tif, "JBIG", "Error (%d) decoding: %s", decodeStatus,
89 #if defined(JBG_EN)
90 jbg_strerror(decodeStatus, JBG_EN)
91 #else
92 jbg_strerror(decodeStatus)
93 #endif
94 );
95 memset(buffer, 0, (size_t)size);
96 jbg_dec_free(&decoder);
97 return 0;
98 }
99
100 decodedSize = jbg_dec_getsize(&decoder);
101 if ((tmsize_t)decodedSize < size)
102 {
103 memset(buffer + decodedSize, 0, (size_t)(size - decodedSize));
104 TIFFWarningExtR(tif, "JBIG",
105 "Only decoded %lu bytes, whereas %" TIFF_SSIZE_FORMAT
106 " requested",
107 decodedSize, size);
108 }
109 else if ((tmsize_t)decodedSize > size)
110 {
111 TIFFErrorExtR(tif, "JBIG",
112 "Decoded %lu bytes, whereas %" TIFF_SSIZE_FORMAT
113 " were requested",
114 decodedSize, size);
115 jbg_dec_free(&decoder);
116 return 0;
117 }
118 pImage = jbg_dec_getimage(&decoder, 0);
119 _TIFFmemcpy(buffer, pImage, decodedSize);
120 jbg_dec_free(&decoder);
121
122 tif->tif_rawcp += tif->tif_rawcc;
123 tif->tif_rawcc = 0;
124
125 return 1;
126 }
127
128 static int JBIGSetupEncode(TIFF *tif)
129 {
130 if (TIFFNumberOfStrips(tif) != 1)
131 {
132 TIFFErrorExtR(tif, "JBIG",
133 "Multistrip images not supported in encoder");
134 return 0;
135 }
136
137 return 1;
138 }
139
140 static int JBIGCopyEncodedData(TIFF *tif, unsigned char *pp, size_t cc,
141 uint16_t s)
142 {
143 (void)s;
144 while (cc > 0)
145 {
146 tmsize_t n = (tmsize_t)cc;
147
148 if (tif->tif_rawcc + n > tif->tif_rawdatasize)
149 {
150 n = tif->tif_rawdatasize - tif->tif_rawcc;
151 }
152
153 assert(n > 0);
154 _TIFFmemcpy(tif->tif_rawcp, pp, n);
155 tif->tif_rawcp += n;
156 tif->tif_rawcc += n;
157 pp += n;
158 cc -= (size_t)n;
159 if (tif->tif_rawcc >= tif->tif_rawdatasize && !TIFFFlushData1(tif))
160 {
161 return (-1);
162 }
163 }
164
165 return (1);
166 }
167
168 static void JBIGOutputBie(unsigned char *buffer, size_t len, void *userData)
169 {
170 TIFF *tif = (TIFF *)userData;
171
172 if (isFillOrder(tif, tif->tif_dir.td_fillorder))
173 {
174 TIFFReverseBits(buffer, (tmsize_t)len);
175 }
176
177 JBIGCopyEncodedData(tif, buffer, len, 0);
178 }
179
180 static int JBIGEncode(TIFF *tif, uint8_t *buffer, tmsize_t size, uint16_t s)
181 {
182 TIFFDirectory *dir = &tif->tif_dir;
183 struct jbg_enc_state encoder;
184
185 (void)size, (void)s;
186
187 jbg_enc_init(&encoder, dir->td_imagewidth, dir->td_imagelength, 1, &buffer,
188 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 jbg_enc_out(&encoder);
194 jbg_enc_free(&encoder);
195
196 return 1;
197 }
198
199 int TIFFInitJBIG(TIFF *tif, int scheme)
200 {
201 (void)scheme;
202 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 tif->tif_flags |= TIFF_NOBITREV;
210 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 if (tif->tif_flags & TIFF_BUFFERMMAP)
216 {
217 tif->tif_rawdata = NULL;
218 tif->tif_rawdatasize = 0;
219 tif->tif_flags &= ~TIFF_BUFFERMMAP;
220 tif->tif_flags |= TIFF_MYBUFFER;
221 }
222
223 /* Setup the function pointers for encode, decode, and cleanup. */
224 tif->tif_setupdecode = JBIGSetupDecode;
225 tif->tif_decodestrip = JBIGDecode;
226
227 tif->tif_setupencode = JBIGSetupEncode;
228 tif->tif_encodestrip = JBIGEncode;
229
230 return 1;
231 }
232
233 #endif /* JBIG_SUPPORT */