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