LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_jbig.c
Go to the documentation of this file.
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
38#include "tiffiop.h"
39
40#ifdef JBIG_SUPPORT
41#ifdef __cplusplus
42extern "C"
43{
44#endif
45#include "jbig.h"
46#ifdef __cplusplus
47}
48#endif
49
61static int JBIGSetupDecode(TIFF *tif)
62{
63 if (TIFFNumberOfStrips(tif) != 1)
64 {
65 TIFFErrorExtR(tif, "JBIG",
66 "Multistrip images not supported in decoder");
67 return 0;
68 }
69
70 return 1;
71}
72
98static int JBIGDecode(TIFF *tif, uint8_t *buffer, tmsize_t size, uint16_t s)
99{
100 struct jbg_dec_state decoder;
101 int decodeStatus = 0;
102 unsigned char *pImage = NULL;
103 unsigned long decodedSizeUL;
104 uint64_t decodedSize64;
105 tmsize_t decodedSize;
106 (void)s;
107
108 if (isFillOrder(tif, tif->tif_dir.td_fillorder))
109 {
111 }
112
113 jbg_dec_init(&decoder);
114
115#if defined(HAVE_JBG_NEWLEN)
116 jbg_newlen(tif->tif_rawcp, (size_t)tif->tif_rawcc);
117 /*
118 * I do not check the return status of jbg_newlen because even if this
119 * function fails it does not necessarily mean that decoding the image
120 * will fail. It is generally only needed for received fax images
121 * that do not contain the actual length of the image in the BIE
122 * header. I do not log when an error occurs because that will cause
123 * problems when converting JBIG encoded TIFF's to
124 * PostScript. As long as the actual image length is contained in the
125 * BIE header jbg_dec_in should succeed.
126 */
127#endif /* HAVE_JBG_NEWLEN */
128
129 decodeStatus = jbg_dec_in(&decoder, (unsigned char *)tif->tif_rawcp,
130 (size_t)tif->tif_rawcc, NULL);
131 if (JBG_EOK != decodeStatus)
132 {
133 /*
134 * XXX: JBG_EN constant was defined in pre-2.0 releases of the
135 * JBIG-KIT. Since the 2.0 the error reporting functions were
136 * changed. We will handle both cases here.
137 */
138 TIFFErrorExtR(tif, "JBIG", "Error (%d) decoding: %s", decodeStatus,
139#if defined(JBG_EN)
140 jbg_strerror(decodeStatus, JBG_EN)
141#else
142 jbg_strerror(decodeStatus)
143#endif
144 );
145 if (size > 0)
146 memset(buffer, 0, (size_t)size);
147 jbg_dec_free(&decoder);
148 return 0;
149 }
150
151 /* jbg_dec_getsize() returns unsigned long while the destination size is
152 * tmsize_t. Down-casting the decoded size to tmsize_t for the bounds
153 * compare turns oversize values negative, which bypasses the compare and
154 * lets the copy/zeroing below write out of bounds. Compare in the
155 * unsigned domain first and only then narrow to tmsize_t. The compare
156 * uses a uint64_t local rather than casts so that it is not a
157 * constant-range compare on LLP64, where unsigned long is 32 bits.
158 */
159 decodedSizeUL = jbg_dec_getsize(&decoder);
160 decodedSize64 = decodedSizeUL;
161 if (decodedSize64 > (uint64_t)TIFF_TMSIZE_T_MAX)
162 {
163 TIFFErrorExtR(tif, "JBIG",
164 "Decoded %lu bytes, which exceeds the maximum supported "
165 "size of %" TIFF_SSIZE_FORMAT " bytes",
166 decodedSizeUL, (tmsize_t)TIFF_TMSIZE_T_MAX);
167 if (size > 0)
168 memset(buffer, 0, (size_t)size);
169 jbg_dec_free(&decoder);
170 return 0;
171 }
172 decodedSize = (tmsize_t)decodedSizeUL;
173 if (decodedSize > size)
174 {
175 TIFFErrorExtR(tif, "JBIG",
176 "Decoded %lu bytes, whereas %" TIFF_SSIZE_FORMAT
177 " were requested",
178 decodedSizeUL, size);
179 if (size > 0)
180 memset(buffer, 0, (size_t)size);
181 jbg_dec_free(&decoder);
182 return 0;
183 }
184 if (decodedSize < size)
185 {
186 memset(buffer + decodedSize, 0, (size_t)(size - decodedSize));
187 TIFFWarningExtR(tif, "JBIG",
188 "Only decoded %lu bytes, whereas %" TIFF_SSIZE_FORMAT
189 " requested",
190 decodedSizeUL, size);
191 }
192 if (decodedSize > 0)
193 {
194 pImage = jbg_dec_getimage(&decoder, 0);
195 if (pImage == NULL)
196 {
197 TIFFErrorExtR(tif, "JBIG", "JBIG decoder returned no image data");
198 if (size > 0)
199 memset(buffer, 0, (size_t)size);
200 jbg_dec_free(&decoder);
201 return 0;
202 }
203 _TIFFmemcpy(buffer, pImage, decodedSize);
204 }
205 jbg_dec_free(&decoder);
206
207 tif->tif_rawcp += tif->tif_rawcc;
208 tif->tif_rawcc = 0;
209
210 return 1;
211}
212
223static int JBIGSetupEncode(TIFF *tif)
224{
225 if (TIFFNumberOfStrips(tif) != 1)
226 {
227 TIFFErrorExtR(tif, "JBIG",
228 "Multistrip images not supported in encoder");
229 return 0;
230 }
231
232 return 1;
233}
234
253static int JBIGCopyEncodedData(TIFF *tif, unsigned char *pp, size_t cc,
254 uint16_t s)
255{
256 (void)s;
257 while (cc > 0)
258 {
259 tmsize_t n = (tmsize_t)cc;
260
261 if (tif->tif_rawcc + n > tif->tif_rawdatasize)
262 {
263 n = tif->tif_rawdatasize - tif->tif_rawcc;
264 }
265
266 assert(n > 0);
267 _TIFFmemcpy(tif->tif_rawcp, pp, n);
268 tif->tif_rawcp += n;
269 tif->tif_rawcc += n;
270 pp += n;
271 cc -= (size_t)n;
272 if (tif->tif_rawcc >= tif->tif_rawdatasize && !TIFFFlushData1(tif))
273 {
274 return (-1);
275 }
276 }
277
278 return (1);
279}
280
298static void JBIGOutputBie(unsigned char *buffer, size_t len, void *userData)
299{
300 TIFF *tif = (TIFF *)userData;
301
302 if (isFillOrder(tif, tif->tif_dir.td_fillorder))
303 {
304 TIFFReverseBits(buffer, (tmsize_t)len);
305 }
306
307 JBIGCopyEncodedData(tif, buffer, len, 0);
308}
309
334static int JBIGEncode(TIFF *tif, uint8_t *buffer, tmsize_t size, uint16_t s)
335{
336 static const char module[] = "JBIG";
337 TIFFDirectory *dir = &tif->tif_dir;
338 struct jbg_enc_state encoder;
339 uint64_t rowbytes64;
340 uint64_t required64;
341 tmsize_t required;
342
343 (void)s;
344
345 if (dir->td_bitspersample != 1 || dir->td_samplesperpixel != 1)
346 {
347 TIFFErrorExtR(tif, module,
348 "JBIG encoder supports only single-sample 1-bit image "
349 "planes");
350 return 0;
351 }
352
353 if (TIFFNumberOfStrips(tif) != 1)
354 {
355 TIFFErrorExtR(tif, module,
356 "Multistrip images not supported in encoder");
357 return 0;
358 }
359
360 rowbytes64 = TIFFhowmany8_64(dir->td_imagewidth);
361 required64 = _TIFFMultiply64(tif, rowbytes64, dir->td_imagelength, module);
362 required = _TIFFCastUInt64ToSSize(tif, required64, module);
363 if (required == 0)
364 return 0;
365 if (size < required)
366 {
367 TIFFErrorExtR(tif, module,
368 "Application buffer too small for JBIG source image "
369 "plane: got %" TIFF_SSIZE_FORMAT
370 " bytes, need %" TIFF_SSIZE_FORMAT,
371 size, required);
372 return 0;
373 }
374
375 jbg_enc_init(&encoder, dir->td_imagewidth, dir->td_imagelength, 1, &buffer,
376 JBIGOutputBie, tif);
377 /*
378 * jbg_enc_out does the "real" encoding. As data is encoded,
379 * JBIGOutputBie is called, which writes the data to the directory.
380 */
381 jbg_enc_out(&encoder);
382 jbg_enc_free(&encoder);
383
384 return 1;
385}
386
387int TIFFInitJBIG(TIFF *tif, int scheme)
388{
389 (void)scheme;
390 assert(scheme == COMPRESSION_JBIG);
391
392 /*
393 * These flags are set so the JBIG Codec can control when to reverse
394 * bits and when not to and to allow the jbig decoder and bit reverser
395 * to write to memory when necessary.
396 *
397 * Clearing TIFF_MAPPED does not unmap tif_base. TIFFCleanup() calls
398 * tif_unmapproc only while TIFF_MAPPED is set, so a view mapped when the
399 * file was opened is never released (known defect; behaviour unchanged).
400 */
401 tif->tif_flags |= TIFF_NOBITREV;
402 tif->tif_flags &= ~TIFF_MAPPED;
403 /* We may have read from a previous IFD and thus set TIFF_BUFFERMMAP and
404 * cleared TIFF_MYBUFFER. It is necessary to restore them to their initial
405 * value to be consistent with the state of a non-memory mapped file.
406 */
407 if (tif->tif_flags & TIFF_BUFFERMMAP)
408 {
409 tif->tif_rawdata = NULL;
410 tif->tif_rawdatasize = 0;
411 tif->tif_flags &= ~TIFF_BUFFERMMAP;
412 tif->tif_flags |= TIFF_MYBUFFER;
413 }
414
415 /* Setup the function pointers for encode, decode, and cleanup. */
418
421
422 return 1;
423}
424
425#endif /* JBIG_SUPPORT */
tmsize_t _TIFFCastUInt64ToSSize(TIFF *tif, uint64_t val, const char *module)
Convert an unsigned 64-bit value after checking the destination range.
Definition tif_aux.c:140
uint64_t _TIFFMultiply64(TIFF *tif, uint64_t first, uint64_t second, const char *where)
Multiply two unsigned values with an overflow check.
Definition tif_aux.c:46
#define TIFF_SSIZE_FORMAT
Provide the printf conversion fragment for tmsize_t, without a percent sign.
Definition tif_config.h:294
int TIFFFlushData1(TIFF *tif)
Append buffered encoded bytes without invoking postencode recursively.
Definition tif_write.c:1213
static int JBIGSetupDecode(TIFF *tif)
Require one strip for JBIG decoding.
Definition tif_jbig.c:61
static void JBIGOutputBie(unsigned char *buffer, size_t len, void *userData)
Reverse and forward a dependency-produced JBIG output fragment.
Definition tif_jbig.c:298
int TIFFInitJBIG(TIFF *tif, int scheme)
Install single-strip JBIG callbacks and mutable raw-input policy.
Definition tif_jbig.c:387
static int JBIGDecode(TIFF *tif, uint8_t *buffer, tmsize_t size, uint16_t s)
Decode one complete JBIG plane into caller storage.
Definition tif_jbig.c:98
static int JBIGCopyEncodedData(TIFF *tif, unsigned char *pp, size_t cc, uint16_t s)
Copy JBIG output fragments into the buffered raw stream.
Definition tif_jbig.c:253
static int JBIGSetupEncode(TIFF *tif)
Require one strip for JBIG encoding.
Definition tif_jbig.c:223
static int JBIGEncode(TIFF *tif, uint8_t *buffer, tmsize_t size, uint16_t s)
Encode a complete single-sample one-bit JBIG image plane.
Definition tif_jbig.c:334
#define TIFF_MYBUFFER
Mark a raw buffer owned and released by the handle.
Definition tiffiop.h:451
#define isFillOrder(tif, o)
Test whether selected bit-fill-order bits are present in a handle.
Definition tiffiop.h:858
#define TIFF_NOBITREV
Suppress automatic bit reversal.
Definition tiffiop.h:446
#define TIFF_BUFFERMMAP
Mark raw data that borrows mapped-file storage.
Definition tiffiop.h:528
#define TIFFhowmany8_64(x)
Compute the byte count needed to hold a 64-bit bit count.
Definition tiffiop.h:1127
void TIFFReverseBits(uint8_t *cp, tmsize_t n)
Reverse the bit order within each byte of a buffer.
Definition tif_swab.c:330
void TIFFWarningExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a warning diagnostic to the configured handlers.
Definition tif_warning.c:85
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
Definition tif_error.c:112
uint32_t TIFFNumberOfStrips(TIFF *tif)
Compute the strip count from current image geometry.
Definition tif_strip.c:82
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Copy a non-overlapping byte range.
Definition tif_unix.c:497
#define COMPRESSION_JBIG
ISO JBIG.
Definition tiff.h:356
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
#define TIFF_TMSIZE_T_MAX
Define the largest positive size representable by tmsize_t.
Definition tiffio.h:91
Store the current directory values and image traversal state.
Definition tif_dir.h:143
uint16_t td_fillorder
FILLORDER_* bit order within file bytes.
Definition tif_dir.h:178
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
TIFFCodeMethod tif_encodestrip
Codec callback encoding strip data.
Definition tiffiop.h:649
tmsize_t tif_rawcc
Raw-buffer byte count interpreted according to read/write state.
Definition tiffiop.h:696
TIFFCodeMethod tif_decodestrip
Codec callback decoding strip data.
Definition tiffiop.h:647
TIFFBoolMethod tif_setupencode
Codec setup callback run before preencode.
Definition tiffiop.h:632
TIFFDirectory tif_dir
Inline representation of the current image directory.
Definition tiffiop.h:572
uint8_t * tif_rawcp
Borrowed current-position pointer into the raw buffer.
Definition tiffiop.h:692
TIFFBoolMethod tif_setupdecode
Codec setup callback run before predecode.
Definition tiffiop.h:628
uint8_t * tif_rawdata
Raw buffer; ownership/mapping follow TIFF_MYBUFFER and TIFF_BUFFERMMAP.
Definition tiffiop.h:681
tmsize_t tif_rawdatasize
Raw-buffer capacity in bytes; kept in step with tif_rawdata, including zero whenever tif_rawdata is N...
Definition tiffiop.h:686
uint32_t tif_flags
Bitmask combining the TIFF_* handle-state flags declared below.
Definition tiffiop.h:404
Declare private library state, callbacks, and implementation helpers.