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