/src/freeimage-svn/FreeImage/trunk/Source/LibTIFF4/tif_fax3.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 1990-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 | | #include "tiffiop.h" |
26 | | #ifdef CCITT_SUPPORT |
27 | | /* |
28 | | * TIFF Library. |
29 | | * |
30 | | * CCITT Group 3 (T.4) and Group 4 (T.6) Compression Support. |
31 | | * |
32 | | * This file contains support for decoding and encoding TIFF |
33 | | * compression algorithms 2, 3, 4, and 32771. |
34 | | * |
35 | | * Decoder support is derived, with permission, from the code |
36 | | * in Frank Cringle's viewfax program; |
37 | | * Copyright (C) 1990, 1995 Frank D. Cringle. |
38 | | */ |
39 | | #include "tif_fax3.h" |
40 | | #define G3CODES |
41 | | #include "t4.h" |
42 | | #include <stdio.h> |
43 | | |
44 | | /* |
45 | | * Compression+decompression state blocks are |
46 | | * derived from this ``base state'' block. |
47 | | */ |
48 | | typedef struct |
49 | | { |
50 | | int rw_mode; /* O_RDONLY for decode, else encode */ |
51 | | int mode; /* operating mode */ |
52 | | tmsize_t rowbytes; /* bytes in a decoded scanline */ |
53 | | uint32_t rowpixels; /* pixels in a scanline */ |
54 | | |
55 | | uint16_t cleanfaxdata; /* CleanFaxData tag */ |
56 | | uint32_t badfaxrun; /* BadFaxRun tag */ |
57 | | uint32_t badfaxlines; /* BadFaxLines tag */ |
58 | | uint32_t groupoptions; /* Group 3/4 options tag */ |
59 | | |
60 | | TIFFVGetMethod vgetparent; /* super-class method */ |
61 | | TIFFVSetMethod vsetparent; /* super-class method */ |
62 | | TIFFPrintMethod printdir; /* super-class method */ |
63 | | } Fax3BaseState; |
64 | 0 | #define Fax3State(tif) ((Fax3BaseState *)(tif)->tif_data) |
65 | | |
66 | | typedef enum |
67 | | { |
68 | | G3_1D, |
69 | | G3_2D |
70 | | } Ttag; |
71 | | typedef struct |
72 | | { |
73 | | Fax3BaseState b; |
74 | | |
75 | | /* Decoder state info */ |
76 | | const unsigned char *bitmap; /* bit reversal table */ |
77 | | uint32_t data; /* current i/o byte/word */ |
78 | | int bit; /* current i/o bit in byte */ |
79 | | int EOLcnt; /* count of EOL codes recognized */ |
80 | | TIFFFaxFillFunc fill; /* fill routine */ |
81 | | uint32_t *runs; /* b&w runs for current/previous row */ |
82 | | uint32_t nruns; /* size of the refruns / curruns arrays */ |
83 | | uint32_t *refruns; /* runs for reference line */ |
84 | | uint32_t *curruns; /* runs for current line */ |
85 | | |
86 | | /* Encoder state info */ |
87 | | Ttag tag; /* encoding state */ |
88 | | unsigned char *refline; /* reference line for 2d decoding */ |
89 | | int k; /* #rows left that can be 2d encoded */ |
90 | | int maxk; /* max #rows that can be 2d encoded */ |
91 | | |
92 | | int line; |
93 | | } Fax3CodecState; |
94 | 0 | #define DecoderState(tif) ((Fax3CodecState *)Fax3State(tif)) |
95 | 0 | #define EncoderState(tif) ((Fax3CodecState *)Fax3State(tif)) |
96 | | |
97 | 0 | #define is2DEncoding(sp) (sp->b.groupoptions & GROUP3OPT_2DENCODING) |
98 | 0 | #define isAligned(p, t) ((((size_t)(p)) & (sizeof(t) - 1)) == 0) |
99 | | |
100 | | /* |
101 | | * Group 3 and Group 4 Decoding. |
102 | | */ |
103 | | |
104 | | /* |
105 | | * These macros glue the TIFF library state to |
106 | | * the state expected by Frank's decoder. |
107 | | */ |
108 | | #define DECLARE_STATE(tif, sp, mod) \ |
109 | 0 | static const char module[] = mod; \ |
110 | 0 | Fax3CodecState *sp = DecoderState(tif); \ |
111 | 0 | int a0; /* reference element */ \ |
112 | 0 | int lastx = sp->b.rowpixels; /* last element in row */ \ |
113 | 0 | uint32_t BitAcc; /* bit accumulator */ \ |
114 | 0 | int BitsAvail; /* # valid bits in BitAcc */ \ |
115 | 0 | int RunLength; /* length of current run */ \ |
116 | 0 | unsigned char *cp; /* next byte of input data */ \ |
117 | 0 | unsigned char *ep; /* end of input data */ \ |
118 | 0 | uint32_t *pa; /* place to stuff next run */ \ |
119 | 0 | uint32_t *thisrun; /* current row's run array */ \ |
120 | 0 | int EOLcnt; /* # EOL codes recognized */ \ |
121 | 0 | const unsigned char *bitmap = sp->bitmap; /* input data bit reverser */ \ |
122 | 0 | const TIFFFaxTabEnt *TabEnt |
123 | | #define DECLARE_STATE_2D(tif, sp, mod) \ |
124 | 0 | DECLARE_STATE(tif, sp, mod); \ |
125 | 0 | int b1; /* next change on prev line */ \ |
126 | 0 | uint32_t \ |
127 | 0 | *pb /* next run in reference line */ /* \ |
128 | | * Load any state that may be \ |
129 | | * changed during decoding. \ |
130 | | */ |
131 | | #define CACHE_STATE(tif, sp) \ |
132 | 0 | do \ |
133 | 0 | { \ |
134 | 0 | BitAcc = sp->data; \ |
135 | 0 | BitsAvail = sp->bit; \ |
136 | 0 | EOLcnt = sp->EOLcnt; \ |
137 | 0 | cp = (unsigned char *)tif->tif_rawcp; \ |
138 | 0 | ep = cp + tif->tif_rawcc; \ |
139 | 0 | } while (0) |
140 | | /* |
141 | | * Save state possibly changed during decoding. |
142 | | */ |
143 | | #define UNCACHE_STATE(tif, sp) \ |
144 | 0 | do \ |
145 | 0 | { \ |
146 | 0 | sp->bit = BitsAvail; \ |
147 | 0 | sp->data = BitAcc; \ |
148 | 0 | sp->EOLcnt = EOLcnt; \ |
149 | 0 | tif->tif_rawcc -= (tmsize_t)((uint8_t *)cp - tif->tif_rawcp); \ |
150 | 0 | tif->tif_rawcp = (uint8_t *)cp; \ |
151 | 0 | } while (0) |
152 | | |
153 | | /* |
154 | | * Setup state for decoding a strip. |
155 | | */ |
156 | | static int Fax3PreDecode(TIFF *tif, uint16_t s) |
157 | 0 | { |
158 | 0 | Fax3CodecState *sp = DecoderState(tif); |
159 | |
|
160 | 0 | (void)s; |
161 | 0 | assert(sp != NULL); |
162 | 0 | sp->bit = 0; /* force initial read */ |
163 | 0 | sp->data = 0; |
164 | 0 | sp->EOLcnt = 0; /* force initial scan for EOL */ |
165 | | /* |
166 | | * Decoder assumes lsb-to-msb bit order. Note that we select |
167 | | * this here rather than in Fax3SetupState so that viewers can |
168 | | * hold the image open, fiddle with the FillOrder tag value, |
169 | | * and then re-decode the image. Otherwise they'd need to close |
170 | | * and open the image to get the state reset. |
171 | | */ |
172 | 0 | sp->bitmap = |
173 | 0 | TIFFGetBitRevTable(tif->tif_dir.td_fillorder != FILLORDER_LSB2MSB); |
174 | 0 | sp->curruns = sp->runs; |
175 | 0 | if (sp->refruns) |
176 | 0 | { /* init reference line to white */ |
177 | 0 | sp->refruns = sp->runs + sp->nruns; |
178 | 0 | sp->refruns[0] = (uint32_t)sp->b.rowpixels; |
179 | 0 | sp->refruns[1] = 0; |
180 | 0 | } |
181 | 0 | sp->line = 0; |
182 | 0 | return (1); |
183 | 0 | } |
184 | | |
185 | | /* |
186 | | * Routine for handling various errors/conditions. |
187 | | * Note how they are "glued into the decoder" by |
188 | | * overriding the definitions used by the decoder. |
189 | | */ |
190 | | |
191 | | static void Fax3Unexpected(const char *module, TIFF *tif, uint32_t line, |
192 | | uint32_t a0) |
193 | 0 | { |
194 | 0 | TIFFErrorExtR(tif, module, |
195 | 0 | "Bad code word at line %" PRIu32 " of %s %" PRIu32 |
196 | 0 | " (x %" PRIu32 ")", |
197 | 0 | line, isTiled(tif) ? "tile" : "strip", |
198 | 0 | (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0); |
199 | 0 | } |
200 | 0 | #define unexpected(table, a0) Fax3Unexpected(module, tif, sp->line, a0) |
201 | | |
202 | | static void Fax3Extension(const char *module, TIFF *tif, uint32_t line, |
203 | | uint32_t a0) |
204 | 0 | { |
205 | 0 | TIFFErrorExtR(tif, module, |
206 | 0 | "Uncompressed data (not supported) at line %" PRIu32 |
207 | 0 | " of %s %" PRIu32 " (x %" PRIu32 ")", |
208 | 0 | line, isTiled(tif) ? "tile" : "strip", |
209 | 0 | (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0); |
210 | 0 | } |
211 | 0 | #define extension(a0) Fax3Extension(module, tif, sp->line, a0) |
212 | | |
213 | | static void Fax3BadLength(const char *module, TIFF *tif, uint32_t line, |
214 | | uint32_t a0, uint32_t lastx) |
215 | 0 | { |
216 | 0 | TIFFWarningExtR(tif, module, |
217 | 0 | "%s at line %" PRIu32 " of %s %" PRIu32 " (got %" PRIu32 |
218 | 0 | ", expected %" PRIu32 ")", |
219 | 0 | a0 < lastx ? "Premature EOL" : "Line length mismatch", line, |
220 | 0 | isTiled(tif) ? "tile" : "strip", |
221 | 0 | (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0, |
222 | 0 | lastx); |
223 | 0 | } |
224 | 0 | #define badlength(a0, lastx) Fax3BadLength(module, tif, sp->line, a0, lastx) |
225 | | |
226 | | static void Fax3PrematureEOF(const char *module, TIFF *tif, uint32_t line, |
227 | | uint32_t a0) |
228 | 0 | { |
229 | 0 | TIFFWarningExtR(tif, module, |
230 | 0 | "Premature EOF at line %" PRIu32 " of %s %" PRIu32 |
231 | 0 | " (x %" PRIu32 ")", |
232 | 0 | line, isTiled(tif) ? "tile" : "strip", |
233 | 0 | (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0); |
234 | 0 | } |
235 | 0 | #define prematureEOF(a0) Fax3PrematureEOF(module, tif, sp->line, a0) |
236 | | |
237 | | #define Nop |
238 | | |
239 | | /** |
240 | | * Decode the requested amount of G3 1D-encoded data. |
241 | | * @param buf destination buffer |
242 | | * @param occ available bytes in destination buffer |
243 | | * @param s number of planes (ignored) |
244 | | * @returns 1 for success, -1 in case of error |
245 | | */ |
246 | | static int Fax3Decode1D(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s) |
247 | 0 | { |
248 | 0 | DECLARE_STATE(tif, sp, "Fax3Decode1D"); |
249 | 0 | (void)s; |
250 | 0 | if (occ % sp->b.rowbytes) |
251 | 0 | { |
252 | 0 | TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read"); |
253 | 0 | return (-1); |
254 | 0 | } |
255 | 0 | CACHE_STATE(tif, sp); |
256 | 0 | thisrun = sp->curruns; |
257 | 0 | while (occ > 0) |
258 | 0 | { |
259 | 0 | a0 = 0; |
260 | 0 | RunLength = 0; |
261 | 0 | pa = thisrun; |
262 | | #ifdef FAX3_DEBUG |
263 | | printf("\nBitAcc=%08" PRIX32 ", BitsAvail = %d\n", BitAcc, BitsAvail); |
264 | | printf("-------------------- %" PRIu32 "\n", tif->tif_row); |
265 | | fflush(stdout); |
266 | | #endif |
267 | 0 | SYNC_EOL(EOF1D); |
268 | 0 | EXPAND1D(EOF1Da); |
269 | 0 | (*sp->fill)(buf, thisrun, pa, lastx); |
270 | 0 | buf += sp->b.rowbytes; |
271 | 0 | occ -= sp->b.rowbytes; |
272 | 0 | sp->line++; |
273 | 0 | continue; |
274 | 0 | EOF1D: /* premature EOF */ |
275 | 0 | CLEANUP_RUNS(); |
276 | 0 | EOF1Da: /* premature EOF */ |
277 | 0 | (*sp->fill)(buf, thisrun, pa, lastx); |
278 | 0 | UNCACHE_STATE(tif, sp); |
279 | 0 | return (-1); |
280 | 0 | } |
281 | 0 | UNCACHE_STATE(tif, sp); |
282 | 0 | return (1); |
283 | 0 | } |
284 | | |
285 | | #define SWAP(t, a, b) \ |
286 | 0 | { \ |
287 | 0 | t x; \ |
288 | 0 | x = (a); \ |
289 | 0 | (a) = (b); \ |
290 | 0 | (b) = x; \ |
291 | 0 | } |
292 | | /* |
293 | | * Decode the requested amount of G3 2D-encoded data. |
294 | | */ |
295 | | static int Fax3Decode2D(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s) |
296 | 0 | { |
297 | 0 | DECLARE_STATE_2D(tif, sp, "Fax3Decode2D"); |
298 | 0 | int is1D; /* current line is 1d/2d-encoded */ |
299 | 0 | (void)s; |
300 | 0 | if (occ % sp->b.rowbytes) |
301 | 0 | { |
302 | 0 | TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read"); |
303 | 0 | return (-1); |
304 | 0 | } |
305 | 0 | CACHE_STATE(tif, sp); |
306 | 0 | while (occ > 0) |
307 | 0 | { |
308 | 0 | a0 = 0; |
309 | 0 | RunLength = 0; |
310 | 0 | pa = thisrun = sp->curruns; |
311 | | #ifdef FAX3_DEBUG |
312 | | printf("\nBitAcc=%08" PRIX32 ", BitsAvail = %d EOLcnt = %d", BitAcc, |
313 | | BitsAvail, EOLcnt); |
314 | | #endif |
315 | 0 | SYNC_EOL(EOF2D); |
316 | 0 | NeedBits8(1, EOF2D); |
317 | 0 | is1D = GetBits(1); /* 1D/2D-encoding tag bit */ |
318 | 0 | ClrBits(1); |
319 | | #ifdef FAX3_DEBUG |
320 | | printf(" %s\n-------------------- %" PRIu32 "\n", is1D ? "1D" : "2D", |
321 | | tif->tif_row); |
322 | | fflush(stdout); |
323 | | #endif |
324 | 0 | pb = sp->refruns; |
325 | 0 | b1 = *pb++; |
326 | 0 | if (is1D) |
327 | 0 | EXPAND1D(EOF2Da); |
328 | 0 | else |
329 | 0 | EXPAND2D(EOF2Da); |
330 | 0 | (*sp->fill)(buf, thisrun, pa, lastx); |
331 | 0 | if (pa < thisrun + sp->nruns) |
332 | 0 | { |
333 | 0 | SETVALUE(0); /* imaginary change for reference */ |
334 | 0 | } |
335 | 0 | SWAP(uint32_t *, sp->curruns, sp->refruns); |
336 | 0 | buf += sp->b.rowbytes; |
337 | 0 | occ -= sp->b.rowbytes; |
338 | 0 | sp->line++; |
339 | 0 | continue; |
340 | 0 | EOF2D: /* premature EOF */ |
341 | 0 | CLEANUP_RUNS(); |
342 | 0 | EOF2Da: /* premature EOF */ |
343 | 0 | (*sp->fill)(buf, thisrun, pa, lastx); |
344 | 0 | UNCACHE_STATE(tif, sp); |
345 | 0 | return (-1); |
346 | 0 | } |
347 | 0 | UNCACHE_STATE(tif, sp); |
348 | 0 | return (1); |
349 | 0 | } |
350 | | #undef SWAP |
351 | | |
352 | | #define FILL(n, cp) \ |
353 | 0 | for (int32_t ifill = 0; ifill < (n); ++ifill) \ |
354 | 0 | { \ |
355 | 0 | (cp)[ifill] = 0xff; \ |
356 | 0 | } \ |
357 | 0 | (cp) += (n); |
358 | | |
359 | | #define ZERO(n, cp) \ |
360 | 0 | for (int32_t izero = 0; izero < (n); ++izero) \ |
361 | 0 | { \ |
362 | 0 | (cp)[izero] = 0; \ |
363 | 0 | } \ |
364 | 0 | (cp) += (n); |
365 | | |
366 | | /* |
367 | | * Bit-fill a row according to the white/black |
368 | | * runs generated during G3/G4 decoding. |
369 | | */ |
370 | | void _TIFFFax3fillruns(unsigned char *buf, uint32_t *runs, uint32_t *erun, |
371 | | uint32_t lastx) |
372 | 0 | { |
373 | 0 | static const unsigned char _fillmasks[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0, |
374 | 0 | 0xf8, 0xfc, 0xfe, 0xff}; |
375 | 0 | unsigned char *cp; |
376 | 0 | uint32_t x, bx, run; |
377 | 0 | int32_t n, nw; |
378 | 0 | int64_t *lp; |
379 | |
|
380 | 0 | if ((erun - runs) & 1) |
381 | 0 | *erun++ = 0; |
382 | 0 | x = 0; |
383 | 0 | for (; runs < erun; runs += 2) |
384 | 0 | { |
385 | 0 | run = runs[0]; |
386 | 0 | if (x + run > lastx || run > lastx) |
387 | 0 | run = runs[0] = (uint32_t)(lastx - x); |
388 | 0 | if (run) |
389 | 0 | { |
390 | 0 | cp = buf + (x >> 3); |
391 | 0 | bx = x & 7; |
392 | 0 | if (run > 8 - bx) |
393 | 0 | { |
394 | 0 | if (bx) |
395 | 0 | { /* align to byte boundary */ |
396 | 0 | *cp++ &= 0xff << (8 - bx); |
397 | 0 | run -= 8 - bx; |
398 | 0 | } |
399 | 0 | if ((n = run >> 3) != 0) |
400 | 0 | { /* multiple bytes to fill */ |
401 | 0 | if ((n / sizeof(int64_t)) > 1) |
402 | 0 | { |
403 | | /* |
404 | | * Align to int64_tword boundary and fill. |
405 | | */ |
406 | 0 | for (; n && !isAligned(cp, int64_t); n--) |
407 | 0 | *cp++ = 0x00; |
408 | 0 | lp = (int64_t *)cp; |
409 | 0 | nw = (int32_t)(n / sizeof(int64_t)); |
410 | 0 | n -= nw * sizeof(int64_t); |
411 | 0 | do |
412 | 0 | { |
413 | 0 | *lp++ = 0L; |
414 | 0 | } while (--nw); |
415 | 0 | cp = (unsigned char *)lp; |
416 | 0 | } |
417 | 0 | ZERO(n, cp); |
418 | 0 | run &= 7; |
419 | 0 | } |
420 | 0 | if (run) |
421 | 0 | cp[0] &= 0xff >> run; |
422 | 0 | } |
423 | 0 | else |
424 | 0 | cp[0] &= ~(_fillmasks[run] >> bx); |
425 | 0 | x += runs[0]; |
426 | 0 | } |
427 | 0 | run = runs[1]; |
428 | 0 | if (x + run > lastx || run > lastx) |
429 | 0 | run = runs[1] = lastx - x; |
430 | 0 | if (run) |
431 | 0 | { |
432 | 0 | cp = buf + (x >> 3); |
433 | 0 | bx = x & 7; |
434 | 0 | if (run > 8 - bx) |
435 | 0 | { |
436 | 0 | if (bx) |
437 | 0 | { /* align to byte boundary */ |
438 | 0 | *cp++ |= 0xff >> bx; |
439 | 0 | run -= 8 - bx; |
440 | 0 | } |
441 | 0 | if ((n = run >> 3) != 0) |
442 | 0 | { /* multiple bytes to fill */ |
443 | 0 | if ((n / sizeof(int64_t)) > 1) |
444 | 0 | { |
445 | | /* |
446 | | * Align to int64_t boundary and fill. |
447 | | */ |
448 | 0 | for (; n && !isAligned(cp, int64_t); n--) |
449 | 0 | *cp++ = 0xff; |
450 | 0 | lp = (int64_t *)cp; |
451 | 0 | nw = (int32_t)(n / sizeof(int64_t)); |
452 | 0 | n -= nw * sizeof(int64_t); |
453 | 0 | do |
454 | 0 | { |
455 | 0 | *lp++ = -1L; |
456 | 0 | } while (--nw); |
457 | 0 | cp = (unsigned char *)lp; |
458 | 0 | } |
459 | 0 | FILL(n, cp); |
460 | 0 | run &= 7; |
461 | 0 | } |
462 | | /* Explicit 0xff masking to make icc -check=conversions happy */ |
463 | 0 | if (run) |
464 | 0 | cp[0] = (unsigned char)((cp[0] | (0xff00 >> run)) & 0xff); |
465 | 0 | } |
466 | 0 | else |
467 | 0 | cp[0] |= _fillmasks[run] >> bx; |
468 | 0 | x += runs[1]; |
469 | 0 | } |
470 | 0 | } |
471 | 0 | assert(x == lastx); |
472 | 0 | } |
473 | | #undef ZERO |
474 | | #undef FILL |
475 | | |
476 | | static int Fax3FixupTags(TIFF *tif) |
477 | 0 | { |
478 | 0 | (void)tif; |
479 | 0 | return (1); |
480 | 0 | } |
481 | | |
482 | | /* |
483 | | * Setup G3/G4-related compression/decompression state |
484 | | * before data is processed. This routine is called once |
485 | | * per image -- it sets up different state based on whether |
486 | | * or not decoding or encoding is being done and whether |
487 | | * 1D- or 2D-encoded data is involved. |
488 | | */ |
489 | | static int Fax3SetupState(TIFF *tif) |
490 | 0 | { |
491 | 0 | static const char module[] = "Fax3SetupState"; |
492 | 0 | TIFFDirectory *td = &tif->tif_dir; |
493 | 0 | Fax3BaseState *sp = Fax3State(tif); |
494 | 0 | int needsRefLine; |
495 | 0 | Fax3CodecState *dsp = (Fax3CodecState *)Fax3State(tif); |
496 | 0 | tmsize_t rowbytes; |
497 | 0 | uint32_t rowpixels; |
498 | |
|
499 | 0 | if (td->td_bitspersample != 1) |
500 | 0 | { |
501 | 0 | TIFFErrorExtR(tif, module, |
502 | 0 | "Bits/sample must be 1 for Group 3/4 encoding/decoding"); |
503 | 0 | return (0); |
504 | 0 | } |
505 | | /* |
506 | | * Calculate the scanline/tile widths. |
507 | | */ |
508 | 0 | if (isTiled(tif)) |
509 | 0 | { |
510 | 0 | rowbytes = TIFFTileRowSize(tif); |
511 | 0 | rowpixels = td->td_tilewidth; |
512 | 0 | } |
513 | 0 | else |
514 | 0 | { |
515 | 0 | rowbytes = TIFFScanlineSize(tif); |
516 | 0 | rowpixels = td->td_imagewidth; |
517 | 0 | } |
518 | 0 | if ((int64_t)rowbytes < ((int64_t)rowpixels + 7) / 8) |
519 | 0 | { |
520 | 0 | TIFFErrorExtR(tif, module, |
521 | 0 | "Inconsistent number of bytes per row : rowbytes=%" PRId64 |
522 | 0 | " rowpixels=%" PRIu32, |
523 | 0 | (int64_t)rowbytes, rowpixels); |
524 | 0 | return (0); |
525 | 0 | } |
526 | 0 | sp->rowbytes = rowbytes; |
527 | 0 | sp->rowpixels = rowpixels; |
528 | | /* |
529 | | * Allocate any additional space required for decoding/encoding. |
530 | | */ |
531 | 0 | needsRefLine = ((sp->groupoptions & GROUP3OPT_2DENCODING) || |
532 | 0 | td->td_compression == COMPRESSION_CCITTFAX4); |
533 | | |
534 | | /* |
535 | | Assure that allocation computations do not overflow. |
536 | | |
537 | | TIFFroundup and TIFFSafeMultiply return zero on integer overflow |
538 | | */ |
539 | 0 | dsp->runs = (uint32_t *)NULL; |
540 | 0 | dsp->nruns = TIFFroundup_32(rowpixels + 1, 32); |
541 | 0 | if (needsRefLine) |
542 | 0 | { |
543 | 0 | dsp->nruns = TIFFSafeMultiply(uint32_t, dsp->nruns, 2); |
544 | 0 | } |
545 | 0 | if ((dsp->nruns == 0) || (TIFFSafeMultiply(uint32_t, dsp->nruns, 2) == 0)) |
546 | 0 | { |
547 | 0 | TIFFErrorExtR(tif, tif->tif_name, |
548 | 0 | "Row pixels integer overflow (rowpixels %" PRIu32 ")", |
549 | 0 | rowpixels); |
550 | 0 | return (0); |
551 | 0 | } |
552 | 0 | dsp->runs = (uint32_t *)_TIFFCheckMalloc( |
553 | 0 | tif, TIFFSafeMultiply(uint32_t, dsp->nruns, 2), sizeof(uint32_t), |
554 | 0 | "for Group 3/4 run arrays"); |
555 | 0 | if (dsp->runs == NULL) |
556 | 0 | return (0); |
557 | 0 | memset(dsp->runs, 0, |
558 | 0 | TIFFSafeMultiply(uint32_t, dsp->nruns, 2) * sizeof(uint32_t)); |
559 | 0 | dsp->curruns = dsp->runs; |
560 | 0 | if (needsRefLine) |
561 | 0 | dsp->refruns = dsp->runs + dsp->nruns; |
562 | 0 | else |
563 | 0 | dsp->refruns = NULL; |
564 | 0 | if (td->td_compression == COMPRESSION_CCITTFAX3 && is2DEncoding(dsp)) |
565 | 0 | { /* NB: default is 1D routine */ |
566 | 0 | tif->tif_decoderow = Fax3Decode2D; |
567 | 0 | tif->tif_decodestrip = Fax3Decode2D; |
568 | 0 | tif->tif_decodetile = Fax3Decode2D; |
569 | 0 | } |
570 | |
|
571 | 0 | if (needsRefLine) |
572 | 0 | { /* 2d encoding */ |
573 | 0 | Fax3CodecState *esp = EncoderState(tif); |
574 | | /* |
575 | | * 2d encoding requires a scanline |
576 | | * buffer for the ``reference line''; the |
577 | | * scanline against which delta encoding |
578 | | * is referenced. The reference line must |
579 | | * be initialized to be ``white'' (done elsewhere). |
580 | | */ |
581 | 0 | esp->refline = (unsigned char *)_TIFFmallocExt(tif, rowbytes); |
582 | 0 | if (esp->refline == NULL) |
583 | 0 | { |
584 | 0 | TIFFErrorExtR(tif, module, "No space for Group 3/4 reference line"); |
585 | 0 | return (0); |
586 | 0 | } |
587 | 0 | } |
588 | 0 | else /* 1d encoding */ |
589 | 0 | EncoderState(tif)->refline = NULL; |
590 | | |
591 | 0 | return (1); |
592 | 0 | } |
593 | | |
594 | | /* |
595 | | * CCITT Group 3 FAX Encoding. |
596 | | */ |
597 | | |
598 | | #define Fax3FlushBits(tif, sp) \ |
599 | 0 | { \ |
600 | 0 | if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \ |
601 | 0 | { \ |
602 | 0 | if (!TIFFFlushData1(tif)) \ |
603 | 0 | return 0; \ |
604 | 0 | } \ |
605 | 0 | *(tif)->tif_rawcp++ = (uint8_t)(sp)->data; \ |
606 | 0 | (tif)->tif_rawcc++; \ |
607 | 0 | (sp)->data = 0, (sp)->bit = 8; \ |
608 | 0 | } |
609 | | #define _FlushBits(tif) \ |
610 | 0 | { \ |
611 | 0 | if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \ |
612 | 0 | { \ |
613 | 0 | if (!TIFFFlushData1(tif)) \ |
614 | 0 | return 0; \ |
615 | 0 | } \ |
616 | 0 | *(tif)->tif_rawcp++ = (uint8_t)data; \ |
617 | 0 | (tif)->tif_rawcc++; \ |
618 | 0 | data = 0, bit = 8; \ |
619 | 0 | } |
620 | | static const int _msbmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, |
621 | | 0x1f, 0x3f, 0x7f, 0xff}; |
622 | | #define _PutBits(tif, bits, length) \ |
623 | 0 | { \ |
624 | 0 | while (length > bit) \ |
625 | 0 | { \ |
626 | 0 | data |= bits >> (length - bit); \ |
627 | 0 | length -= bit; \ |
628 | 0 | _FlushBits(tif); \ |
629 | 0 | } \ |
630 | 0 | assert(length < 9); \ |
631 | 0 | data |= (bits & _msbmask[length]) << (bit - length); \ |
632 | 0 | bit -= length; \ |
633 | 0 | if (bit == 0) \ |
634 | 0 | _FlushBits(tif); \ |
635 | 0 | } |
636 | | |
637 | | /* |
638 | | * Write a variable-length bit-value to |
639 | | * the output stream. Values are |
640 | | * assumed to be at most 16 bits. |
641 | | */ |
642 | | static int Fax3PutBits(TIFF *tif, unsigned int bits, unsigned int length) |
643 | 0 | { |
644 | 0 | Fax3CodecState *sp = EncoderState(tif); |
645 | 0 | unsigned int bit = sp->bit; |
646 | 0 | int data = sp->data; |
647 | |
|
648 | 0 | _PutBits(tif, bits, length); |
649 | |
|
650 | 0 | sp->data = data; |
651 | 0 | sp->bit = bit; |
652 | 0 | return 1; |
653 | 0 | } |
654 | | |
655 | | /* |
656 | | * Write a code to the output stream. |
657 | | */ |
658 | 0 | #define putcode(tif, te) Fax3PutBits(tif, (te)->code, (te)->length) |
659 | | |
660 | | #ifdef FAX3_DEBUG |
661 | | #define DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B") |
662 | | #define DEBUG_PRINT(what, len) \ |
663 | | { \ |
664 | | int t; \ |
665 | | printf("%08" PRIX32 "/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what), \ |
666 | | len); \ |
667 | | for (t = length - 1; t >= 0; t--) \ |
668 | | putchar(code & (1 << t) ? '1' : '0'); \ |
669 | | putchar('\n'); \ |
670 | | } |
671 | | #endif |
672 | | |
673 | | /* |
674 | | * Write the sequence of codes that describes |
675 | | * the specified span of zero's or one's. The |
676 | | * appropriate table that holds the make-up and |
677 | | * terminating codes is supplied. |
678 | | */ |
679 | | static int putspan(TIFF *tif, int32_t span, const tableentry *tab) |
680 | 0 | { |
681 | 0 | Fax3CodecState *sp = EncoderState(tif); |
682 | 0 | unsigned int bit = sp->bit; |
683 | 0 | int data = sp->data; |
684 | 0 | unsigned int code, length; |
685 | |
|
686 | 0 | while (span >= 2624) |
687 | 0 | { |
688 | 0 | const tableentry *te = &tab[63 + (2560 >> 6)]; |
689 | 0 | code = te->code; |
690 | 0 | length = te->length; |
691 | | #ifdef FAX3_DEBUG |
692 | | DEBUG_PRINT("MakeUp", te->runlen); |
693 | | #endif |
694 | 0 | _PutBits(tif, code, length); |
695 | 0 | span -= te->runlen; |
696 | 0 | } |
697 | 0 | if (span >= 64) |
698 | 0 | { |
699 | 0 | const tableentry *te = &tab[63 + (span >> 6)]; |
700 | 0 | assert(te->runlen == 64 * (span >> 6)); |
701 | 0 | code = te->code; |
702 | 0 | length = te->length; |
703 | | #ifdef FAX3_DEBUG |
704 | | DEBUG_PRINT("MakeUp", te->runlen); |
705 | | #endif |
706 | 0 | _PutBits(tif, code, length); |
707 | 0 | span -= te->runlen; |
708 | 0 | } |
709 | 0 | code = tab[span].code; |
710 | 0 | length = tab[span].length; |
711 | | #ifdef FAX3_DEBUG |
712 | | DEBUG_PRINT(" Term", tab[span].runlen); |
713 | | #endif |
714 | 0 | _PutBits(tif, code, length); |
715 | |
|
716 | 0 | sp->data = data; |
717 | 0 | sp->bit = bit; |
718 | |
|
719 | 0 | return 1; |
720 | 0 | } |
721 | | |
722 | | /* |
723 | | * Write an EOL code to the output stream. The zero-fill |
724 | | * logic for byte-aligning encoded scanlines is handled |
725 | | * here. We also handle writing the tag bit for the next |
726 | | * scanline when doing 2d encoding. |
727 | | */ |
728 | | static int Fax3PutEOL(TIFF *tif) |
729 | 0 | { |
730 | 0 | Fax3CodecState *sp = EncoderState(tif); |
731 | 0 | unsigned int bit = sp->bit; |
732 | 0 | int data = sp->data; |
733 | 0 | unsigned int code, length, tparm; |
734 | |
|
735 | 0 | if (sp->b.groupoptions & GROUP3OPT_FILLBITS) |
736 | 0 | { |
737 | | /* |
738 | | * Force bit alignment so EOL will terminate on |
739 | | * a byte boundary. That is, force the bit alignment |
740 | | * to 16-12 = 4 before putting out the EOL code. |
741 | | */ |
742 | 0 | int align = 8 - 4; |
743 | 0 | if (align != sp->bit) |
744 | 0 | { |
745 | 0 | if (align > sp->bit) |
746 | 0 | align = sp->bit + (8 - align); |
747 | 0 | else |
748 | 0 | align = sp->bit - align; |
749 | 0 | tparm = align; |
750 | 0 | _PutBits(tif, 0, tparm); |
751 | 0 | } |
752 | 0 | } |
753 | 0 | code = EOL; |
754 | 0 | length = 12; |
755 | 0 | if (is2DEncoding(sp)) |
756 | 0 | { |
757 | 0 | code = (code << 1) | (sp->tag == G3_1D); |
758 | 0 | length++; |
759 | 0 | } |
760 | 0 | _PutBits(tif, code, length); |
761 | |
|
762 | 0 | sp->data = data; |
763 | 0 | sp->bit = bit; |
764 | |
|
765 | 0 | return 1; |
766 | 0 | } |
767 | | |
768 | | /* |
769 | | * Reset encoding state at the start of a strip. |
770 | | */ |
771 | | static int Fax3PreEncode(TIFF *tif, uint16_t s) |
772 | 0 | { |
773 | 0 | Fax3CodecState *sp = EncoderState(tif); |
774 | |
|
775 | 0 | (void)s; |
776 | 0 | assert(sp != NULL); |
777 | 0 | sp->bit = 8; |
778 | 0 | sp->data = 0; |
779 | 0 | sp->tag = G3_1D; |
780 | | /* |
781 | | * This is necessary for Group 4; otherwise it isn't |
782 | | * needed because the first scanline of each strip ends |
783 | | * up being copied into the refline. |
784 | | */ |
785 | 0 | if (sp->refline) |
786 | 0 | _TIFFmemset(sp->refline, 0x00, sp->b.rowbytes); |
787 | 0 | if (is2DEncoding(sp)) |
788 | 0 | { |
789 | 0 | float res = tif->tif_dir.td_yresolution; |
790 | | /* |
791 | | * The CCITT spec says that when doing 2d encoding, you |
792 | | * should only do it on K consecutive scanlines, where K |
793 | | * depends on the resolution of the image being encoded |
794 | | * (2 for <= 200 lpi, 4 for > 200 lpi). Since the directory |
795 | | * code initializes td_yresolution to 0, this code will |
796 | | * select a K of 2 unless the YResolution tag is set |
797 | | * appropriately. (Note also that we fudge a little here |
798 | | * and use 150 lpi to avoid problems with units conversion.) |
799 | | */ |
800 | 0 | if (tif->tif_dir.td_resolutionunit == RESUNIT_CENTIMETER) |
801 | 0 | res *= 2.54f; /* convert to inches */ |
802 | 0 | sp->maxk = (res > 150 ? 4 : 2); |
803 | 0 | sp->k = sp->maxk - 1; |
804 | 0 | } |
805 | 0 | else |
806 | 0 | sp->k = sp->maxk = 0; |
807 | 0 | sp->line = 0; |
808 | 0 | return (1); |
809 | 0 | } |
810 | | |
811 | | static const unsigned char zeroruns[256] = { |
812 | | 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0f */ |
813 | | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */ |
814 | | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2f */ |
815 | | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3f */ |
816 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */ |
817 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5f */ |
818 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */ |
819 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7f */ |
820 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8f */ |
821 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9f */ |
822 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 - 0xaf */ |
823 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 - 0xbf */ |
824 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 - 0xcf */ |
825 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 - 0xdf */ |
826 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 - 0xef */ |
827 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xf0 - 0xff */ |
828 | | }; |
829 | | static const unsigned char oneruns[256] = { |
830 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 - 0x0f */ |
831 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */ |
832 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 - 0x2f */ |
833 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */ |
834 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */ |
835 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */ |
836 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6f */ |
837 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7f */ |
838 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x80 - 0x8f */ |
839 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x90 - 0x9f */ |
840 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xa0 - 0xaf */ |
841 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xb0 - 0xbf */ |
842 | | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xc0 - 0xcf */ |
843 | | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xd0 - 0xdf */ |
844 | | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */ |
845 | | 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, /* 0xf0 - 0xff */ |
846 | | }; |
847 | | |
848 | | /* |
849 | | * Find a span of ones or zeros using the supplied |
850 | | * table. The ``base'' of the bit string is supplied |
851 | | * along with the start+end bit indices. |
852 | | */ |
853 | | static inline int32_t find0span(unsigned char *bp, int32_t bs, int32_t be) |
854 | 0 | { |
855 | 0 | int32_t bits = be - bs; |
856 | 0 | int32_t n, span; |
857 | |
|
858 | 0 | bp += bs >> 3; |
859 | | /* |
860 | | * Check partial byte on lhs. |
861 | | */ |
862 | 0 | if (bits > 0 && (n = (bs & 7)) != 0) |
863 | 0 | { |
864 | 0 | span = zeroruns[(*bp << n) & 0xff]; |
865 | 0 | if (span > 8 - n) /* table value too generous */ |
866 | 0 | span = 8 - n; |
867 | 0 | if (span > bits) /* constrain span to bit range */ |
868 | 0 | span = bits; |
869 | 0 | if (n + span < 8) /* doesn't extend to edge of byte */ |
870 | 0 | return (span); |
871 | 0 | bits -= span; |
872 | 0 | bp++; |
873 | 0 | } |
874 | 0 | else |
875 | 0 | span = 0; |
876 | 0 | if (bits >= (int32_t)(2 * 8 * sizeof(int64_t))) |
877 | 0 | { |
878 | 0 | int64_t *lp; |
879 | | /* |
880 | | * Align to int64_t boundary and check int64_t words. |
881 | | */ |
882 | 0 | while (!isAligned(bp, int64_t)) |
883 | 0 | { |
884 | 0 | if (*bp != 0x00) |
885 | 0 | return (span + zeroruns[*bp]); |
886 | 0 | span += 8; |
887 | 0 | bits -= 8; |
888 | 0 | bp++; |
889 | 0 | } |
890 | 0 | lp = (int64_t *)bp; |
891 | 0 | while ((bits >= (int32_t)(8 * sizeof(int64_t))) && (0 == *lp)) |
892 | 0 | { |
893 | 0 | span += 8 * sizeof(int64_t); |
894 | 0 | bits -= 8 * sizeof(int64_t); |
895 | 0 | lp++; |
896 | 0 | } |
897 | 0 | bp = (unsigned char *)lp; |
898 | 0 | } |
899 | | /* |
900 | | * Scan full bytes for all 0's. |
901 | | */ |
902 | 0 | while (bits >= 8) |
903 | 0 | { |
904 | 0 | if (*bp != 0x00) /* end of run */ |
905 | 0 | return (span + zeroruns[*bp]); |
906 | 0 | span += 8; |
907 | 0 | bits -= 8; |
908 | 0 | bp++; |
909 | 0 | } |
910 | | /* |
911 | | * Check partial byte on rhs. |
912 | | */ |
913 | 0 | if (bits > 0) |
914 | 0 | { |
915 | 0 | n = zeroruns[*bp]; |
916 | 0 | span += (n > bits ? bits : n); |
917 | 0 | } |
918 | 0 | return (span); |
919 | 0 | } |
920 | | |
921 | | static inline int32_t find1span(unsigned char *bp, int32_t bs, int32_t be) |
922 | 0 | { |
923 | 0 | int32_t bits = be - bs; |
924 | 0 | int32_t n, span; |
925 | |
|
926 | 0 | bp += bs >> 3; |
927 | | /* |
928 | | * Check partial byte on lhs. |
929 | | */ |
930 | 0 | if (bits > 0 && (n = (bs & 7)) != 0) |
931 | 0 | { |
932 | 0 | span = oneruns[(*bp << n) & 0xff]; |
933 | 0 | if (span > 8 - n) /* table value too generous */ |
934 | 0 | span = 8 - n; |
935 | 0 | if (span > bits) /* constrain span to bit range */ |
936 | 0 | span = bits; |
937 | 0 | if (n + span < 8) /* doesn't extend to edge of byte */ |
938 | 0 | return (span); |
939 | 0 | bits -= span; |
940 | 0 | bp++; |
941 | 0 | } |
942 | 0 | else |
943 | 0 | span = 0; |
944 | 0 | if (bits >= (int32_t)(2 * 8 * sizeof(int64_t))) |
945 | 0 | { |
946 | 0 | int64_t *lp; |
947 | | /* |
948 | | * Align to int64_t boundary and check int64_t words. |
949 | | */ |
950 | 0 | while (!isAligned(bp, int64_t)) |
951 | 0 | { |
952 | 0 | if (*bp != 0xff) |
953 | 0 | return (span + oneruns[*bp]); |
954 | 0 | span += 8; |
955 | 0 | bits -= 8; |
956 | 0 | bp++; |
957 | 0 | } |
958 | 0 | lp = (int64_t *)bp; |
959 | 0 | while ((bits >= (int32_t)(8 * sizeof(int64_t))) && |
960 | 0 | (~((uint64_t)0) == (uint64_t)*lp)) |
961 | 0 | { |
962 | 0 | span += 8 * sizeof(int64_t); |
963 | 0 | bits -= 8 * sizeof(int64_t); |
964 | 0 | lp++; |
965 | 0 | } |
966 | 0 | bp = (unsigned char *)lp; |
967 | 0 | } |
968 | | /* |
969 | | * Scan full bytes for all 1's. |
970 | | */ |
971 | 0 | while (bits >= 8) |
972 | 0 | { |
973 | 0 | if (*bp != 0xff) /* end of run */ |
974 | 0 | return (span + oneruns[*bp]); |
975 | 0 | span += 8; |
976 | 0 | bits -= 8; |
977 | 0 | bp++; |
978 | 0 | } |
979 | | /* |
980 | | * Check partial byte on rhs. |
981 | | */ |
982 | 0 | if (bits > 0) |
983 | 0 | { |
984 | 0 | n = oneruns[*bp]; |
985 | 0 | span += (n > bits ? bits : n); |
986 | 0 | } |
987 | 0 | return (span); |
988 | 0 | } |
989 | | |
990 | | /* |
991 | | * Return the offset of the next bit in the range |
992 | | * [bs..be] that is different from the specified |
993 | | * color. The end, be, is returned if no such bit |
994 | | * exists. |
995 | | */ |
996 | | #define finddiff(_cp, _bs, _be, _color) \ |
997 | 0 | (_bs + (_color ? find1span(_cp, _bs, _be) : find0span(_cp, _bs, _be))) |
998 | | /* |
999 | | * Like finddiff, but also check the starting bit |
1000 | | * against the end in case start > end. |
1001 | | */ |
1002 | | #define finddiff2(_cp, _bs, _be, _color) \ |
1003 | 0 | (_bs < _be ? finddiff(_cp, _bs, _be, _color) : _be) |
1004 | | |
1005 | | /* |
1006 | | * 1d-encode a row of pixels. The encoding is |
1007 | | * a sequence of all-white or all-black spans |
1008 | | * of pixels encoded with Huffman codes. |
1009 | | */ |
1010 | | static int Fax3Encode1DRow(TIFF *tif, unsigned char *bp, uint32_t bits) |
1011 | 0 | { |
1012 | 0 | Fax3CodecState *sp = EncoderState(tif); |
1013 | 0 | int32_t span; |
1014 | 0 | uint32_t bs = 0; |
1015 | |
|
1016 | 0 | for (;;) |
1017 | 0 | { |
1018 | 0 | span = find0span(bp, bs, bits); /* white span */ |
1019 | 0 | if (!putspan(tif, span, TIFFFaxWhiteCodes)) |
1020 | 0 | return 0; |
1021 | 0 | bs += span; |
1022 | 0 | if (bs >= bits) |
1023 | 0 | break; |
1024 | 0 | span = find1span(bp, bs, bits); /* black span */ |
1025 | 0 | if (!putspan(tif, span, TIFFFaxBlackCodes)) |
1026 | 0 | return 0; |
1027 | 0 | bs += span; |
1028 | 0 | if (bs >= bits) |
1029 | 0 | break; |
1030 | 0 | } |
1031 | 0 | if (sp->b.mode & (FAXMODE_BYTEALIGN | FAXMODE_WORDALIGN)) |
1032 | 0 | { |
1033 | 0 | if (sp->bit != 8) /* byte-align */ |
1034 | 0 | Fax3FlushBits(tif, sp); |
1035 | 0 | if ((sp->b.mode & FAXMODE_WORDALIGN) && |
1036 | 0 | !isAligned(tif->tif_rawcp, uint16_t)) |
1037 | 0 | Fax3FlushBits(tif, sp); |
1038 | 0 | } |
1039 | 0 | return (1); |
1040 | 0 | } |
1041 | | |
1042 | | static const tableentry horizcode = {3, 0x1, 0}; /* 001 */ |
1043 | | static const tableentry passcode = {4, 0x1, 0}; /* 0001 */ |
1044 | | static const tableentry vcodes[7] = { |
1045 | | {7, 0x03, 0}, /* 0000 011 */ |
1046 | | {6, 0x03, 0}, /* 0000 11 */ |
1047 | | {3, 0x03, 0}, /* 011 */ |
1048 | | {1, 0x1, 0}, /* 1 */ |
1049 | | {3, 0x2, 0}, /* 010 */ |
1050 | | {6, 0x02, 0}, /* 0000 10 */ |
1051 | | {7, 0x02, 0} /* 0000 010 */ |
1052 | | }; |
1053 | | |
1054 | | /* |
1055 | | * 2d-encode a row of pixels. Consult the CCITT |
1056 | | * documentation for the algorithm. |
1057 | | */ |
1058 | | static int Fax3Encode2DRow(TIFF *tif, unsigned char *bp, unsigned char *rp, |
1059 | | uint32_t bits) |
1060 | 0 | { |
1061 | 0 | #define PIXEL(buf, ix) ((((buf)[(ix) >> 3]) >> (7 - ((ix)&7))) & 1) |
1062 | 0 | uint32_t a0 = 0; |
1063 | 0 | uint32_t a1 = (PIXEL(bp, 0) != 0 ? 0 : finddiff(bp, 0, bits, 0)); |
1064 | 0 | uint32_t b1 = (PIXEL(rp, 0) != 0 ? 0 : finddiff(rp, 0, bits, 0)); |
1065 | 0 | uint32_t a2, b2; |
1066 | |
|
1067 | 0 | for (;;) |
1068 | 0 | { |
1069 | 0 | b2 = finddiff2(rp, b1, bits, PIXEL(rp, b1)); |
1070 | 0 | if (b2 >= a1) |
1071 | 0 | { |
1072 | | /* Naive computation triggers |
1073 | | * -fsanitize=undefined,unsigned-integer-overflow */ |
1074 | | /* although it is correct unless the difference between both is < 31 |
1075 | | * bit */ |
1076 | | /* int32_t d = b1 - a1; */ |
1077 | 0 | int32_t d = (b1 >= a1 && b1 - a1 <= 3U) ? (int32_t)(b1 - a1) |
1078 | 0 | : (b1 < a1 && a1 - b1 <= 3U) ? -(int32_t)(a1 - b1) |
1079 | 0 | : 0x7FFFFFFF; |
1080 | 0 | if (!(-3 <= d && d <= 3)) |
1081 | 0 | { /* horizontal mode */ |
1082 | 0 | a2 = finddiff2(bp, a1, bits, PIXEL(bp, a1)); |
1083 | 0 | if (!putcode(tif, &horizcode)) |
1084 | 0 | return 0; |
1085 | 0 | if (a0 + a1 == 0 || PIXEL(bp, a0) == 0) |
1086 | 0 | { |
1087 | 0 | if (!putspan(tif, a1 - a0, TIFFFaxWhiteCodes)) |
1088 | 0 | return 0; |
1089 | 0 | if (!putspan(tif, a2 - a1, TIFFFaxBlackCodes)) |
1090 | 0 | return 0; |
1091 | 0 | } |
1092 | 0 | else |
1093 | 0 | { |
1094 | 0 | if (!putspan(tif, a1 - a0, TIFFFaxBlackCodes)) |
1095 | 0 | return 0; |
1096 | 0 | if (!putspan(tif, a2 - a1, TIFFFaxWhiteCodes)) |
1097 | 0 | return 0; |
1098 | 0 | } |
1099 | 0 | a0 = a2; |
1100 | 0 | } |
1101 | 0 | else |
1102 | 0 | { /* vertical mode */ |
1103 | 0 | if (!putcode(tif, &vcodes[d + 3])) |
1104 | 0 | return 0; |
1105 | 0 | a0 = a1; |
1106 | 0 | } |
1107 | 0 | } |
1108 | 0 | else |
1109 | 0 | { /* pass mode */ |
1110 | 0 | if (!putcode(tif, &passcode)) |
1111 | 0 | return 0; |
1112 | 0 | a0 = b2; |
1113 | 0 | } |
1114 | 0 | if (a0 >= bits) |
1115 | 0 | break; |
1116 | 0 | a1 = finddiff(bp, a0, bits, PIXEL(bp, a0)); |
1117 | 0 | b1 = finddiff(rp, a0, bits, !PIXEL(bp, a0)); |
1118 | 0 | b1 = finddiff(rp, b1, bits, PIXEL(bp, a0)); |
1119 | 0 | } |
1120 | 0 | return (1); |
1121 | 0 | #undef PIXEL |
1122 | 0 | } |
1123 | | |
1124 | | /* |
1125 | | * Encode a buffer of pixels. |
1126 | | */ |
1127 | | static int Fax3Encode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) |
1128 | 0 | { |
1129 | 0 | static const char module[] = "Fax3Encode"; |
1130 | 0 | Fax3CodecState *sp = EncoderState(tif); |
1131 | 0 | (void)s; |
1132 | 0 | if (cc % sp->b.rowbytes) |
1133 | 0 | { |
1134 | 0 | TIFFErrorExtR(tif, module, "Fractional scanlines cannot be written"); |
1135 | 0 | return (0); |
1136 | 0 | } |
1137 | 0 | while (cc > 0) |
1138 | 0 | { |
1139 | 0 | if ((sp->b.mode & FAXMODE_NOEOL) == 0) |
1140 | 0 | { |
1141 | 0 | if (!Fax3PutEOL(tif)) |
1142 | 0 | return 0; |
1143 | 0 | } |
1144 | 0 | if (is2DEncoding(sp)) |
1145 | 0 | { |
1146 | 0 | if (sp->tag == G3_1D) |
1147 | 0 | { |
1148 | 0 | if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels)) |
1149 | 0 | return (0); |
1150 | 0 | sp->tag = G3_2D; |
1151 | 0 | } |
1152 | 0 | else |
1153 | 0 | { |
1154 | 0 | if (!Fax3Encode2DRow(tif, bp, sp->refline, sp->b.rowpixels)) |
1155 | 0 | return (0); |
1156 | 0 | sp->k--; |
1157 | 0 | } |
1158 | 0 | if (sp->k == 0) |
1159 | 0 | { |
1160 | 0 | sp->tag = G3_1D; |
1161 | 0 | sp->k = sp->maxk - 1; |
1162 | 0 | } |
1163 | 0 | else |
1164 | 0 | _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes); |
1165 | 0 | } |
1166 | 0 | else |
1167 | 0 | { |
1168 | 0 | if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels)) |
1169 | 0 | return (0); |
1170 | 0 | } |
1171 | 0 | bp += sp->b.rowbytes; |
1172 | 0 | cc -= sp->b.rowbytes; |
1173 | 0 | } |
1174 | 0 | return (1); |
1175 | 0 | } |
1176 | | |
1177 | | static int Fax3PostEncode(TIFF *tif) |
1178 | 0 | { |
1179 | 0 | Fax3CodecState *sp = EncoderState(tif); |
1180 | |
|
1181 | 0 | if (sp->bit != 8) |
1182 | 0 | Fax3FlushBits(tif, sp); |
1183 | 0 | return (1); |
1184 | 0 | } |
1185 | | |
1186 | | static int _Fax3Close(TIFF *tif) |
1187 | 0 | { |
1188 | 0 | if ((Fax3State(tif)->mode & FAXMODE_NORTC) == 0 && tif->tif_rawcp) |
1189 | 0 | { |
1190 | 0 | Fax3CodecState *sp = EncoderState(tif); |
1191 | 0 | unsigned int code = EOL; |
1192 | 0 | unsigned int length = 12; |
1193 | 0 | int i; |
1194 | |
|
1195 | 0 | if (is2DEncoding(sp)) |
1196 | 0 | { |
1197 | 0 | code = (code << 1) | (sp->tag == G3_1D); |
1198 | 0 | length++; |
1199 | 0 | } |
1200 | 0 | for (i = 0; i < 6; i++) |
1201 | 0 | Fax3PutBits(tif, code, length); |
1202 | 0 | Fax3FlushBits(tif, sp); |
1203 | 0 | } |
1204 | 0 | return 1; |
1205 | 0 | } |
1206 | | |
1207 | 0 | static void Fax3Close(TIFF *tif) { _Fax3Close(tif); } |
1208 | | |
1209 | | static void Fax3Cleanup(TIFF *tif) |
1210 | 0 | { |
1211 | 0 | Fax3CodecState *sp = DecoderState(tif); |
1212 | |
|
1213 | 0 | assert(sp != 0); |
1214 | | |
1215 | 0 | tif->tif_tagmethods.vgetfield = sp->b.vgetparent; |
1216 | 0 | tif->tif_tagmethods.vsetfield = sp->b.vsetparent; |
1217 | 0 | tif->tif_tagmethods.printdir = sp->b.printdir; |
1218 | |
|
1219 | 0 | if (sp->runs) |
1220 | 0 | _TIFFfreeExt(tif, sp->runs); |
1221 | 0 | if (sp->refline) |
1222 | 0 | _TIFFfreeExt(tif, sp->refline); |
1223 | |
|
1224 | 0 | _TIFFfreeExt(tif, tif->tif_data); |
1225 | 0 | tif->tif_data = NULL; |
1226 | |
|
1227 | 0 | _TIFFSetDefaultCompressionState(tif); |
1228 | 0 | } |
1229 | | |
1230 | | #define FIELD_BADFAXLINES (FIELD_CODEC + 0) |
1231 | | #define FIELD_CLEANFAXDATA (FIELD_CODEC + 1) |
1232 | | #define FIELD_BADFAXRUN (FIELD_CODEC + 2) |
1233 | | |
1234 | | #define FIELD_OPTIONS (FIELD_CODEC + 7) |
1235 | | |
1236 | | static const TIFFField faxFields[] = { |
1237 | | {TIFFTAG_FAXMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, |
1238 | | FIELD_PSEUDO, FALSE, FALSE, "FaxMode", NULL}, |
1239 | | {TIFFTAG_FAXFILLFUNC, 0, 0, TIFF_ANY, 0, TIFF_SETGET_OTHER, |
1240 | | TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "FaxFillFunc", NULL}, |
1241 | | {TIFFTAG_BADFAXLINES, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, |
1242 | | TIFF_SETGET_UINT32, FIELD_BADFAXLINES, TRUE, FALSE, "BadFaxLines", NULL}, |
1243 | | {TIFFTAG_CLEANFAXDATA, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, |
1244 | | TIFF_SETGET_UINT16, FIELD_CLEANFAXDATA, TRUE, FALSE, "CleanFaxData", NULL}, |
1245 | | {TIFFTAG_CONSECUTIVEBADFAXLINES, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, |
1246 | | TIFF_SETGET_UINT32, FIELD_BADFAXRUN, TRUE, FALSE, "ConsecutiveBadFaxLines", |
1247 | | NULL}}; |
1248 | | static const TIFFField fax3Fields[] = { |
1249 | | {TIFFTAG_GROUP3OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, |
1250 | | TIFF_SETGET_UINT32, FIELD_OPTIONS, FALSE, FALSE, "Group3Options", NULL}, |
1251 | | }; |
1252 | | static const TIFFField fax4Fields[] = { |
1253 | | {TIFFTAG_GROUP4OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, |
1254 | | TIFF_SETGET_UINT32, FIELD_OPTIONS, FALSE, FALSE, "Group4Options", NULL}, |
1255 | | }; |
1256 | | |
1257 | | static int Fax3VSetField(TIFF *tif, uint32_t tag, va_list ap) |
1258 | 0 | { |
1259 | 0 | Fax3BaseState *sp = Fax3State(tif); |
1260 | 0 | const TIFFField *fip; |
1261 | |
|
1262 | 0 | assert(sp != 0); |
1263 | 0 | assert(sp->vsetparent != 0); |
1264 | | |
1265 | 0 | switch (tag) |
1266 | 0 | { |
1267 | 0 | case TIFFTAG_FAXMODE: |
1268 | 0 | sp->mode = (int)va_arg(ap, int); |
1269 | 0 | return 1; /* NB: pseudo tag */ |
1270 | 0 | case TIFFTAG_FAXFILLFUNC: |
1271 | 0 | DecoderState(tif)->fill = va_arg(ap, TIFFFaxFillFunc); |
1272 | 0 | return 1; /* NB: pseudo tag */ |
1273 | 0 | case TIFFTAG_GROUP3OPTIONS: |
1274 | | /* XXX: avoid reading options if compression mismatches. */ |
1275 | 0 | if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX3) |
1276 | 0 | sp->groupoptions = (uint32_t)va_arg(ap, uint32_t); |
1277 | 0 | break; |
1278 | 0 | case TIFFTAG_GROUP4OPTIONS: |
1279 | | /* XXX: avoid reading options if compression mismatches. */ |
1280 | 0 | if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4) |
1281 | 0 | sp->groupoptions = (uint32_t)va_arg(ap, uint32_t); |
1282 | 0 | break; |
1283 | 0 | case TIFFTAG_BADFAXLINES: |
1284 | 0 | sp->badfaxlines = (uint32_t)va_arg(ap, uint32_t); |
1285 | 0 | break; |
1286 | 0 | case TIFFTAG_CLEANFAXDATA: |
1287 | 0 | sp->cleanfaxdata = (uint16_t)va_arg(ap, uint16_vap); |
1288 | 0 | break; |
1289 | 0 | case TIFFTAG_CONSECUTIVEBADFAXLINES: |
1290 | 0 | sp->badfaxrun = (uint32_t)va_arg(ap, uint32_t); |
1291 | 0 | break; |
1292 | 0 | default: |
1293 | 0 | return (*sp->vsetparent)(tif, tag, ap); |
1294 | 0 | } |
1295 | | |
1296 | 0 | if ((fip = TIFFFieldWithTag(tif, tag)) != NULL) |
1297 | 0 | TIFFSetFieldBit(tif, fip->field_bit); |
1298 | 0 | else |
1299 | 0 | return 0; |
1300 | | |
1301 | 0 | tif->tif_flags |= TIFF_DIRTYDIRECT; |
1302 | 0 | return 1; |
1303 | 0 | } |
1304 | | |
1305 | | static int Fax3VGetField(TIFF *tif, uint32_t tag, va_list ap) |
1306 | 0 | { |
1307 | 0 | Fax3BaseState *sp = Fax3State(tif); |
1308 | |
|
1309 | 0 | assert(sp != 0); |
1310 | | |
1311 | 0 | switch (tag) |
1312 | 0 | { |
1313 | 0 | case TIFFTAG_FAXMODE: |
1314 | 0 | *va_arg(ap, int *) = sp->mode; |
1315 | 0 | break; |
1316 | 0 | case TIFFTAG_FAXFILLFUNC: |
1317 | 0 | *va_arg(ap, TIFFFaxFillFunc *) = DecoderState(tif)->fill; |
1318 | 0 | break; |
1319 | 0 | case TIFFTAG_GROUP3OPTIONS: |
1320 | 0 | case TIFFTAG_GROUP4OPTIONS: |
1321 | 0 | *va_arg(ap, uint32_t *) = sp->groupoptions; |
1322 | 0 | break; |
1323 | 0 | case TIFFTAG_BADFAXLINES: |
1324 | 0 | *va_arg(ap, uint32_t *) = sp->badfaxlines; |
1325 | 0 | break; |
1326 | 0 | case TIFFTAG_CLEANFAXDATA: |
1327 | 0 | *va_arg(ap, uint16_t *) = sp->cleanfaxdata; |
1328 | 0 | break; |
1329 | 0 | case TIFFTAG_CONSECUTIVEBADFAXLINES: |
1330 | 0 | *va_arg(ap, uint32_t *) = sp->badfaxrun; |
1331 | 0 | break; |
1332 | 0 | default: |
1333 | 0 | return (*sp->vgetparent)(tif, tag, ap); |
1334 | 0 | } |
1335 | 0 | return (1); |
1336 | 0 | } |
1337 | | |
1338 | | static void Fax3PrintDir(TIFF *tif, FILE *fd, long flags) |
1339 | 0 | { |
1340 | 0 | Fax3BaseState *sp = Fax3State(tif); |
1341 | |
|
1342 | 0 | assert(sp != 0); |
1343 | | |
1344 | 0 | (void)flags; |
1345 | 0 | if (TIFFFieldSet(tif, FIELD_OPTIONS)) |
1346 | 0 | { |
1347 | 0 | const char *sep = " "; |
1348 | 0 | if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4) |
1349 | 0 | { |
1350 | 0 | fprintf(fd, " Group 4 Options:"); |
1351 | 0 | if (sp->groupoptions & GROUP4OPT_UNCOMPRESSED) |
1352 | 0 | fprintf(fd, "%suncompressed data", sep); |
1353 | 0 | } |
1354 | 0 | else |
1355 | 0 | { |
1356 | |
|
1357 | 0 | fprintf(fd, " Group 3 Options:"); |
1358 | 0 | if (sp->groupoptions & GROUP3OPT_2DENCODING) |
1359 | 0 | { |
1360 | 0 | fprintf(fd, "%s2-d encoding", sep); |
1361 | 0 | sep = "+"; |
1362 | 0 | } |
1363 | 0 | if (sp->groupoptions & GROUP3OPT_FILLBITS) |
1364 | 0 | { |
1365 | 0 | fprintf(fd, "%sEOL padding", sep); |
1366 | 0 | sep = "+"; |
1367 | 0 | } |
1368 | 0 | if (sp->groupoptions & GROUP3OPT_UNCOMPRESSED) |
1369 | 0 | fprintf(fd, "%suncompressed data", sep); |
1370 | 0 | } |
1371 | 0 | fprintf(fd, " (%" PRIu32 " = 0x%" PRIx32 ")\n", sp->groupoptions, |
1372 | 0 | sp->groupoptions); |
1373 | 0 | } |
1374 | 0 | if (TIFFFieldSet(tif, FIELD_CLEANFAXDATA)) |
1375 | 0 | { |
1376 | 0 | fprintf(fd, " Fax Data:"); |
1377 | 0 | switch (sp->cleanfaxdata) |
1378 | 0 | { |
1379 | 0 | case CLEANFAXDATA_CLEAN: |
1380 | 0 | fprintf(fd, " clean"); |
1381 | 0 | break; |
1382 | 0 | case CLEANFAXDATA_REGENERATED: |
1383 | 0 | fprintf(fd, " receiver regenerated"); |
1384 | 0 | break; |
1385 | 0 | case CLEANFAXDATA_UNCLEAN: |
1386 | 0 | fprintf(fd, " uncorrected errors"); |
1387 | 0 | break; |
1388 | 0 | } |
1389 | 0 | fprintf(fd, " (%" PRIu16 " = 0x%" PRIx16 ")\n", sp->cleanfaxdata, |
1390 | 0 | sp->cleanfaxdata); |
1391 | 0 | } |
1392 | 0 | if (TIFFFieldSet(tif, FIELD_BADFAXLINES)) |
1393 | 0 | fprintf(fd, " Bad Fax Lines: %" PRIu32 "\n", sp->badfaxlines); |
1394 | 0 | if (TIFFFieldSet(tif, FIELD_BADFAXRUN)) |
1395 | 0 | fprintf(fd, " Consecutive Bad Fax Lines: %" PRIu32 "\n", |
1396 | 0 | sp->badfaxrun); |
1397 | 0 | if (sp->printdir) |
1398 | 0 | (*sp->printdir)(tif, fd, flags); |
1399 | 0 | } |
1400 | | |
1401 | | static int InitCCITTFax3(TIFF *tif) |
1402 | 0 | { |
1403 | 0 | static const char module[] = "InitCCITTFax3"; |
1404 | 0 | Fax3BaseState *sp; |
1405 | | |
1406 | | /* |
1407 | | * Merge codec-specific tag information. |
1408 | | */ |
1409 | 0 | if (!_TIFFMergeFields(tif, faxFields, TIFFArrayCount(faxFields))) |
1410 | 0 | { |
1411 | 0 | TIFFErrorExtR(tif, "InitCCITTFax3", |
1412 | 0 | "Merging common CCITT Fax codec-specific tags failed"); |
1413 | 0 | return 0; |
1414 | 0 | } |
1415 | | |
1416 | | /* |
1417 | | * Allocate state block so tag methods have storage to record values. |
1418 | | */ |
1419 | 0 | tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(Fax3CodecState)); |
1420 | |
|
1421 | 0 | if (tif->tif_data == NULL) |
1422 | 0 | { |
1423 | 0 | TIFFErrorExtR(tif, module, "No space for state block"); |
1424 | 0 | return (0); |
1425 | 0 | } |
1426 | 0 | _TIFFmemset(tif->tif_data, 0, sizeof(Fax3CodecState)); |
1427 | |
|
1428 | 0 | sp = Fax3State(tif); |
1429 | 0 | sp->rw_mode = tif->tif_mode; |
1430 | | |
1431 | | /* |
1432 | | * Override parent get/set field methods. |
1433 | | */ |
1434 | 0 | sp->vgetparent = tif->tif_tagmethods.vgetfield; |
1435 | 0 | tif->tif_tagmethods.vgetfield = Fax3VGetField; /* hook for codec tags */ |
1436 | 0 | sp->vsetparent = tif->tif_tagmethods.vsetfield; |
1437 | 0 | tif->tif_tagmethods.vsetfield = Fax3VSetField; /* hook for codec tags */ |
1438 | 0 | sp->printdir = tif->tif_tagmethods.printdir; |
1439 | 0 | tif->tif_tagmethods.printdir = Fax3PrintDir; /* hook for codec tags */ |
1440 | 0 | sp->groupoptions = 0; |
1441 | |
|
1442 | 0 | if (sp->rw_mode == O_RDONLY) /* FIXME: improve for in place update */ |
1443 | 0 | tif->tif_flags |= TIFF_NOBITREV; /* decoder does bit reversal */ |
1444 | 0 | DecoderState(tif)->runs = NULL; |
1445 | 0 | TIFFSetField(tif, TIFFTAG_FAXFILLFUNC, _TIFFFax3fillruns); |
1446 | 0 | EncoderState(tif)->refline = NULL; |
1447 | | |
1448 | | /* |
1449 | | * Install codec methods. |
1450 | | */ |
1451 | 0 | tif->tif_fixuptags = Fax3FixupTags; |
1452 | 0 | tif->tif_setupdecode = Fax3SetupState; |
1453 | 0 | tif->tif_predecode = Fax3PreDecode; |
1454 | 0 | tif->tif_decoderow = Fax3Decode1D; |
1455 | 0 | tif->tif_decodestrip = Fax3Decode1D; |
1456 | 0 | tif->tif_decodetile = Fax3Decode1D; |
1457 | 0 | tif->tif_setupencode = Fax3SetupState; |
1458 | 0 | tif->tif_preencode = Fax3PreEncode; |
1459 | 0 | tif->tif_postencode = Fax3PostEncode; |
1460 | 0 | tif->tif_encoderow = Fax3Encode; |
1461 | 0 | tif->tif_encodestrip = Fax3Encode; |
1462 | 0 | tif->tif_encodetile = Fax3Encode; |
1463 | 0 | tif->tif_close = Fax3Close; |
1464 | 0 | tif->tif_cleanup = Fax3Cleanup; |
1465 | |
|
1466 | 0 | return (1); |
1467 | 0 | } |
1468 | | |
1469 | | int TIFFInitCCITTFax3(TIFF *tif, int scheme) |
1470 | 0 | { |
1471 | 0 | (void)scheme; |
1472 | 0 | if (InitCCITTFax3(tif)) |
1473 | 0 | { |
1474 | | /* |
1475 | | * Merge codec-specific tag information. |
1476 | | */ |
1477 | 0 | if (!_TIFFMergeFields(tif, fax3Fields, TIFFArrayCount(fax3Fields))) |
1478 | 0 | { |
1479 | 0 | TIFFErrorExtR(tif, "TIFFInitCCITTFax3", |
1480 | 0 | "Merging CCITT Fax 3 codec-specific tags failed"); |
1481 | 0 | return 0; |
1482 | 0 | } |
1483 | | |
1484 | | /* |
1485 | | * The default format is Class/F-style w/o RTC. |
1486 | | */ |
1487 | 0 | return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_CLASSF); |
1488 | 0 | } |
1489 | 0 | else |
1490 | 0 | return 01; |
1491 | 0 | } |
1492 | | |
1493 | | /* |
1494 | | * CCITT Group 4 (T.6) Facsimile-compatible |
1495 | | * Compression Scheme Support. |
1496 | | */ |
1497 | | |
1498 | | #define SWAP(t, a, b) \ |
1499 | 0 | { \ |
1500 | 0 | t x; \ |
1501 | 0 | x = (a); \ |
1502 | 0 | (a) = (b); \ |
1503 | 0 | (b) = x; \ |
1504 | 0 | } |
1505 | | /* |
1506 | | * Decode the requested amount of G4-encoded data. |
1507 | | */ |
1508 | | static int Fax4Decode(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s) |
1509 | 0 | { |
1510 | 0 | DECLARE_STATE_2D(tif, sp, "Fax4Decode"); |
1511 | 0 | (void)s; |
1512 | 0 | if (occ % sp->b.rowbytes) |
1513 | 0 | { |
1514 | 0 | TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read"); |
1515 | 0 | return (-1); |
1516 | 0 | } |
1517 | 0 | CACHE_STATE(tif, sp); |
1518 | 0 | while (occ > 0) |
1519 | 0 | { |
1520 | 0 | a0 = 0; |
1521 | 0 | RunLength = 0; |
1522 | 0 | pa = thisrun = sp->curruns; |
1523 | 0 | pb = sp->refruns; |
1524 | 0 | b1 = *pb++; |
1525 | | #ifdef FAX3_DEBUG |
1526 | | printf("\nBitAcc=%08" PRIX32 ", BitsAvail = %d\n", BitAcc, BitsAvail); |
1527 | | printf("-------------------- %d\n", tif->tif_row); |
1528 | | fflush(stdout); |
1529 | | #endif |
1530 | 0 | EXPAND2D(EOFG4); |
1531 | 0 | if (EOLcnt) |
1532 | 0 | goto EOFG4; |
1533 | 0 | if (((lastx + 7) >> 3) > (int)occ) /* check for buffer overrun */ |
1534 | 0 | { |
1535 | 0 | TIFFErrorExtR(tif, module, |
1536 | 0 | "Buffer overrun detected : %" TIFF_SSIZE_FORMAT |
1537 | 0 | " bytes available, %d bits needed", |
1538 | 0 | occ, lastx); |
1539 | 0 | return -1; |
1540 | 0 | } |
1541 | 0 | (*sp->fill)(buf, thisrun, pa, lastx); |
1542 | 0 | SETVALUE(0); /* imaginary change for reference */ |
1543 | 0 | SWAP(uint32_t *, sp->curruns, sp->refruns); |
1544 | 0 | buf += sp->b.rowbytes; |
1545 | 0 | occ -= sp->b.rowbytes; |
1546 | 0 | sp->line++; |
1547 | 0 | continue; |
1548 | 0 | EOFG4: |
1549 | 0 | NeedBits16(13, BADG4); |
1550 | 0 | BADG4: |
1551 | | #ifdef FAX3_DEBUG |
1552 | | if (GetBits(13) != 0x1001) |
1553 | | fputs("Bad EOFB\n", stderr); |
1554 | | #endif |
1555 | 0 | ClrBits(13); |
1556 | 0 | if (((lastx + 7) >> 3) > (int)occ) /* check for buffer overrun */ |
1557 | 0 | { |
1558 | 0 | TIFFErrorExtR(tif, module, |
1559 | 0 | "Buffer overrun detected : %" TIFF_SSIZE_FORMAT |
1560 | 0 | " bytes available, %d bits needed", |
1561 | 0 | occ, lastx); |
1562 | 0 | return -1; |
1563 | 0 | } |
1564 | 0 | (*sp->fill)(buf, thisrun, pa, lastx); |
1565 | 0 | UNCACHE_STATE(tif, sp); |
1566 | 0 | return (sp->line ? 1 : -1); /* don't error on badly-terminated strips */ |
1567 | 0 | } |
1568 | 0 | UNCACHE_STATE(tif, sp); |
1569 | 0 | return (1); |
1570 | 0 | } |
1571 | | #undef SWAP |
1572 | | |
1573 | | /* |
1574 | | * Encode the requested amount of data. |
1575 | | */ |
1576 | | static int Fax4Encode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) |
1577 | 0 | { |
1578 | 0 | static const char module[] = "Fax4Encode"; |
1579 | 0 | Fax3CodecState *sp = EncoderState(tif); |
1580 | 0 | (void)s; |
1581 | 0 | if (cc % sp->b.rowbytes) |
1582 | 0 | { |
1583 | 0 | TIFFErrorExtR(tif, module, "Fractional scanlines cannot be written"); |
1584 | 0 | return (0); |
1585 | 0 | } |
1586 | 0 | while (cc > 0) |
1587 | 0 | { |
1588 | 0 | if (!Fax3Encode2DRow(tif, bp, sp->refline, sp->b.rowpixels)) |
1589 | 0 | return (0); |
1590 | 0 | _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes); |
1591 | 0 | bp += sp->b.rowbytes; |
1592 | 0 | cc -= sp->b.rowbytes; |
1593 | 0 | } |
1594 | 0 | return (1); |
1595 | 0 | } |
1596 | | |
1597 | | static int Fax4PostEncode(TIFF *tif) |
1598 | 0 | { |
1599 | 0 | Fax3CodecState *sp = EncoderState(tif); |
1600 | | |
1601 | | /* terminate strip w/ EOFB */ |
1602 | 0 | Fax3PutBits(tif, EOL, 12); |
1603 | 0 | Fax3PutBits(tif, EOL, 12); |
1604 | 0 | if (sp->bit != 8) |
1605 | 0 | Fax3FlushBits(tif, sp); |
1606 | 0 | return (1); |
1607 | 0 | } |
1608 | | |
1609 | | int TIFFInitCCITTFax4(TIFF *tif, int scheme) |
1610 | 0 | { |
1611 | 0 | (void)scheme; |
1612 | 0 | if (InitCCITTFax3(tif)) |
1613 | 0 | { /* reuse G3 support */ |
1614 | | /* |
1615 | | * Merge codec-specific tag information. |
1616 | | */ |
1617 | 0 | if (!_TIFFMergeFields(tif, fax4Fields, TIFFArrayCount(fax4Fields))) |
1618 | 0 | { |
1619 | 0 | TIFFErrorExtR(tif, "TIFFInitCCITTFax4", |
1620 | 0 | "Merging CCITT Fax 4 codec-specific tags failed"); |
1621 | 0 | return 0; |
1622 | 0 | } |
1623 | | |
1624 | 0 | tif->tif_decoderow = Fax4Decode; |
1625 | 0 | tif->tif_decodestrip = Fax4Decode; |
1626 | 0 | tif->tif_decodetile = Fax4Decode; |
1627 | 0 | tif->tif_encoderow = Fax4Encode; |
1628 | 0 | tif->tif_encodestrip = Fax4Encode; |
1629 | 0 | tif->tif_encodetile = Fax4Encode; |
1630 | 0 | tif->tif_postencode = Fax4PostEncode; |
1631 | | /* |
1632 | | * Suppress RTC at the end of each strip. |
1633 | | */ |
1634 | 0 | return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_NORTC); |
1635 | 0 | } |
1636 | 0 | else |
1637 | 0 | return (0); |
1638 | 0 | } |
1639 | | |
1640 | | /* |
1641 | | * CCITT Group 3 1-D Modified Huffman RLE Compression Support. |
1642 | | * (Compression algorithms 2 and 32771) |
1643 | | */ |
1644 | | |
1645 | | /* |
1646 | | * Decode the requested amount of RLE-encoded data. |
1647 | | */ |
1648 | | static int Fax3DecodeRLE(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s) |
1649 | 0 | { |
1650 | 0 | DECLARE_STATE(tif, sp, "Fax3DecodeRLE"); |
1651 | 0 | int mode = sp->b.mode; |
1652 | 0 | (void)s; |
1653 | 0 | if (occ % sp->b.rowbytes) |
1654 | 0 | { |
1655 | 0 | TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read"); |
1656 | 0 | return (-1); |
1657 | 0 | } |
1658 | 0 | CACHE_STATE(tif, sp); |
1659 | 0 | thisrun = sp->curruns; |
1660 | 0 | while (occ > 0) |
1661 | 0 | { |
1662 | 0 | a0 = 0; |
1663 | 0 | RunLength = 0; |
1664 | 0 | pa = thisrun; |
1665 | | #ifdef FAX3_DEBUG |
1666 | | printf("\nBitAcc=%08" PRIX32 ", BitsAvail = %d\n", BitAcc, BitsAvail); |
1667 | | printf("-------------------- %" PRIu32 "\n", tif->tif_row); |
1668 | | fflush(stdout); |
1669 | | #endif |
1670 | 0 | EXPAND1D(EOFRLE); |
1671 | 0 | (*sp->fill)(buf, thisrun, pa, lastx); |
1672 | | /* |
1673 | | * Cleanup at the end of the row. |
1674 | | */ |
1675 | 0 | if (mode & FAXMODE_BYTEALIGN) |
1676 | 0 | { |
1677 | 0 | int n = BitsAvail - (BitsAvail & ~7); |
1678 | 0 | ClrBits(n); |
1679 | 0 | } |
1680 | 0 | else if (mode & FAXMODE_WORDALIGN) |
1681 | 0 | { |
1682 | 0 | int n = BitsAvail - (BitsAvail & ~15); |
1683 | 0 | ClrBits(n); |
1684 | 0 | if (BitsAvail == 0 && !isAligned(cp, uint16_t)) |
1685 | 0 | cp++; |
1686 | 0 | } |
1687 | 0 | buf += sp->b.rowbytes; |
1688 | 0 | occ -= sp->b.rowbytes; |
1689 | 0 | sp->line++; |
1690 | 0 | continue; |
1691 | 0 | EOFRLE: /* premature EOF */ |
1692 | 0 | (*sp->fill)(buf, thisrun, pa, lastx); |
1693 | 0 | UNCACHE_STATE(tif, sp); |
1694 | 0 | return (-1); |
1695 | 0 | } |
1696 | 0 | UNCACHE_STATE(tif, sp); |
1697 | 0 | return (1); |
1698 | 0 | } |
1699 | | |
1700 | | int TIFFInitCCITTRLE(TIFF *tif, int scheme) |
1701 | 0 | { |
1702 | 0 | (void)scheme; |
1703 | 0 | if (InitCCITTFax3(tif)) |
1704 | 0 | { /* reuse G3 support */ |
1705 | 0 | tif->tif_decoderow = Fax3DecodeRLE; |
1706 | 0 | tif->tif_decodestrip = Fax3DecodeRLE; |
1707 | 0 | tif->tif_decodetile = Fax3DecodeRLE; |
1708 | | /* |
1709 | | * Suppress RTC+EOLs when encoding and byte-align data. |
1710 | | */ |
1711 | 0 | return TIFFSetField(tif, TIFFTAG_FAXMODE, |
1712 | 0 | FAXMODE_NORTC | FAXMODE_NOEOL | FAXMODE_BYTEALIGN); |
1713 | 0 | } |
1714 | 0 | else |
1715 | 0 | return (0); |
1716 | 0 | } |
1717 | | |
1718 | | int TIFFInitCCITTRLEW(TIFF *tif, int scheme) |
1719 | 0 | { |
1720 | 0 | (void)scheme; |
1721 | 0 | if (InitCCITTFax3(tif)) |
1722 | 0 | { /* reuse G3 support */ |
1723 | 0 | tif->tif_decoderow = Fax3DecodeRLE; |
1724 | 0 | tif->tif_decodestrip = Fax3DecodeRLE; |
1725 | 0 | tif->tif_decodetile = Fax3DecodeRLE; |
1726 | | /* |
1727 | | * Suppress RTC+EOLs when encoding and word-align data. |
1728 | | */ |
1729 | 0 | return TIFFSetField(tif, TIFFTAG_FAXMODE, |
1730 | 0 | FAXMODE_NORTC | FAXMODE_NOEOL | FAXMODE_WORDALIGN); |
1731 | 0 | } |
1732 | 0 | else |
1733 | 0 | return (0); |
1734 | 0 | } |
1735 | | #endif /* CCITT_SUPPORT */ |