4 * Copyright (c) 1990-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
32 * CCITT Group 3 (T.4) and Group 4 (T.6) Compression Support.
34 * This file contains support for decoding and encoding TIFF
35 * compression algorithms 2, 3, 4, and 32771.
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.
47 * Compression+decompression state blocks are
48 * derived from this ``base state'' block.
51 int rw_mode
; /* O_RDONLY for decode, else encode */
52 int mode
; /* operating mode */
53 uint32 rowbytes
; /* bytes in a decoded scanline */
54 uint32 rowpixels
; /* pixels in a scanline */
56 uint16 cleanfaxdata
; /* CleanFaxData tag */
57 uint32 badfaxrun
; /* BadFaxRun tag */
58 uint32 badfaxlines
; /* BadFaxLines tag */
59 uint32 groupoptions
; /* Group 3/4 options tag */
60 uint32 recvparams
; /* encoded Class 2 session params */
61 char* subaddress
; /* subaddress string */
62 uint32 recvtime
; /* time spent receiving (secs) */
63 char* faxdcs
; /* Table 2/T.30 encoded session params */
64 TIFFVGetMethod vgetparent
; /* super-class method */
65 TIFFVSetMethod vsetparent
; /* super-class method */
67 #define Fax3State(tif) ((Fax3BaseState*) (tif)->tif_data)
69 typedef enum { G3_1D
, G3_2D
} Ttag
;
73 /* Decoder state info */
74 const unsigned char* bitmap
; /* bit reversal table */
75 uint32 data
; /* current i/o byte/word */
76 int bit
; /* current i/o bit in byte */
77 int EOLcnt
; /* count of EOL codes recognized */
78 TIFFFaxFillFunc fill
; /* fill routine */
79 uint32
* runs
; /* b&w runs for current/previous row */
80 uint32
* refruns
; /* runs for reference line */
81 uint32
* curruns
; /* runs for current line */
83 /* Encoder state info */
84 Ttag tag
; /* encoding state */
85 unsigned char* refline
; /* reference line for 2d decoding */
86 int k
; /* #rows left that can be 2d encoded */
87 int maxk
; /* max #rows that can be 2d encoded */
89 #define DecoderState(tif) ((Fax3CodecState*) Fax3State(tif))
90 #define EncoderState(tif) ((Fax3CodecState*) Fax3State(tif))
92 #define is2DEncoding(sp) \
93 (sp->b.groupoptions & GROUP3OPT_2DENCODING)
94 #define isAligned(p,t) ((((unsigned long)(p)) & (sizeof (t)-1)) == 0)
97 * Group 3 and Group 4 Decoding.
101 * These macros glue the TIFF library state to
102 * the state expected by Frank's decoder.
104 #define DECLARE_STATE(tif, sp, mod) \
105 static const char module[] = mod; \
106 Fax3CodecState* sp = DecoderState(tif); \
107 int a0; /* reference element */ \
108 int lastx = sp->b.rowpixels; /* last element in row */ \
109 uint32 BitAcc; /* bit accumulator */ \
110 int BitsAvail; /* # valid bits in BitAcc */ \
111 int RunLength; /* length of current run */ \
112 unsigned char* cp; /* next byte of input data */ \
113 unsigned char* ep; /* end of input data */ \
114 uint32* pa; /* place to stuff next run */ \
115 uint32* thisrun; /* current row's run array */ \
116 int EOLcnt; /* # EOL codes recognized */ \
117 const unsigned char* bitmap = sp->bitmap; /* input data bit reverser */ \
118 const TIFFFaxTabEnt* TabEnt
119 #define DECLARE_STATE_2D(tif, sp, mod) \
120 DECLARE_STATE(tif, sp, mod); \
121 int b1; /* next change on prev line */ \
122 uint32* pb /* next run in reference line */\
124 * Load any state that may be changed during decoding.
126 #define CACHE_STATE(tif, sp) do { \
128 BitsAvail = sp->bit; \
129 EOLcnt = sp->EOLcnt; \
130 cp = (unsigned char*) tif->tif_rawcp; \
131 ep = cp + tif->tif_rawcc; \
134 * Save state possibly changed during decoding.
136 #define UNCACHE_STATE(tif, sp) do { \
137 sp->bit = BitsAvail; \
139 sp->EOLcnt = EOLcnt; \
140 tif->tif_rawcc -= (tidata_t) cp - tif->tif_rawcp; \
141 tif->tif_rawcp = (tidata_t) cp; \
145 * Setup state for decoding a strip.
148 Fax3PreDecode(TIFF
* tif
, tsample_t s
)
150 Fax3CodecState
* sp
= DecoderState(tif
);
154 sp
->bit
= 0; /* force initial read */
156 sp
->EOLcnt
= 0; /* force initial scan for EOL */
158 * Decoder assumes lsb-to-msb bit order. Note that we select
159 * this here rather than in Fax3SetupState so that viewers can
160 * hold the image open, fiddle with the FillOrder tag value,
161 * and then re-decode the image. Otherwise they'd need to close
162 * and open the image to get the state reset.
165 TIFFGetBitRevTable(tif
->tif_dir
.td_fillorder
!= FILLORDER_LSB2MSB
);
166 if (sp
->refruns
) { /* init reference line to white */
167 sp
->refruns
[0] = (uint32
) sp
->b
.rowpixels
;
174 * Routine for handling various errors/conditions.
175 * Note how they are "glued into the decoder" by
176 * overriding the definitions used by the decoder.
180 Fax3Unexpected(const char* module, TIFF
* tif
, uint32 line
, uint32 a0
)
182 TIFFErrorExt(tif
->tif_clientdata
, module, "%s: Bad code word at line %lu of %s %lu (x %lu)",
183 tif
->tif_name
, (unsigned long) line
, isTiled(tif
) ? "tile" : "strip",
184 (unsigned long) (isTiled(tif
) ? tif
->tif_curtile
: tif
->tif_curstrip
),
187 #define unexpected(table, a0) Fax3Unexpected(module, tif, line, a0)
190 Fax3Extension(const char* module, TIFF
* tif
, uint32 line
, uint32 a0
)
192 TIFFErrorExt(tif
->tif_clientdata
, module,
193 "%s: Uncompressed data (not supported) at line %lu of %s %lu (x %lu)",
194 tif
->tif_name
, (unsigned long) line
, isTiled(tif
) ? "tile" : "strip",
195 (unsigned long) (isTiled(tif
) ? tif
->tif_curtile
: tif
->tif_curstrip
),
198 #define extension(a0) Fax3Extension(module, tif, line, a0)
201 Fax3BadLength(const char* module, TIFF
* tif
, uint32 line
, uint32 a0
, uint32 lastx
)
203 TIFFWarningExt(tif
->tif_clientdata
, module, "%s: %s at line %lu of %s %lu (got %lu, expected %lu)",
205 a0
< lastx
? "Premature EOL" : "Line length mismatch",
206 (unsigned long) line
, isTiled(tif
) ? "tile" : "strip",
207 (unsigned long) (isTiled(tif
) ? tif
->tif_curtile
: tif
->tif_curstrip
),
208 (unsigned long) a0
, lastx
);
210 #define badlength(a0,lastx) Fax3BadLength(module, tif, line, a0, lastx)
213 Fax3PrematureEOF(const char* module, TIFF
* tif
, uint32 line
, uint32 a0
)
215 TIFFWarningExt(tif
->tif_clientdata
, module, "%s: Premature EOF at line %lu of %s %lu (x %lu)",
217 (unsigned long) line
, isTiled(tif
) ? "tile" : "strip",
218 (unsigned long) (isTiled(tif
) ? tif
->tif_curtile
: tif
->tif_curstrip
),
221 #define prematureEOF(a0) Fax3PrematureEOF(module, tif, line, a0)
226 * Decode the requested amount of G3 1D-encoded data.
229 Fax3Decode1D(TIFF
* tif
, tidata_t buf
, tsize_t occ
, tsample_t s
)
231 DECLARE_STATE(tif
, sp
, "Fax3Decode1D");
235 CACHE_STATE(tif
, sp
);
236 thisrun
= sp
->curruns
;
237 while ((long)occ
> 0) {
242 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc
, BitsAvail
);
243 printf("-------------------- %d\n", tif
->tif_row
);
248 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
249 buf
+= sp
->b
.rowbytes
;
250 occ
-= sp
->b
.rowbytes
;
253 EOF1D
: /* premature EOF */
255 EOF1Da
: /* premature EOF */
256 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
257 UNCACHE_STATE(tif
, sp
);
260 UNCACHE_STATE(tif
, sp
);
264 #define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }
266 * Decode the requested amount of G3 2D-encoded data.
269 Fax3Decode2D(TIFF
* tif
, tidata_t buf
, tsize_t occ
, tsample_t s
)
271 DECLARE_STATE_2D(tif
, sp
, "Fax3Decode2D");
273 int is1D
; /* current line is 1d/2d-encoded */
276 CACHE_STATE(tif
, sp
);
277 while ((long)occ
> 0) {
280 pa
= thisrun
= sp
->curruns
;
282 printf("\nBitAcc=%08X, BitsAvail = %d EOLcnt = %d",
283 BitAcc
, BitsAvail
, EOLcnt
);
287 is1D
= GetBits(1); /* 1D/2D-encoding tag bit */
290 printf(" %s\n-------------------- %d\n",
291 is1D
? "1D" : "2D", tif
->tif_row
);
300 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
301 SETVALUE(0); /* imaginary change for reference */
302 SWAP(uint32
*, sp
->curruns
, sp
->refruns
);
303 buf
+= sp
->b
.rowbytes
;
304 occ
-= sp
->b
.rowbytes
;
307 EOF2D
: /* premature EOF */
309 EOF2Da
: /* premature EOF */
310 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
311 UNCACHE_STATE(tif
, sp
);
314 UNCACHE_STATE(tif
, sp
);
320 * The ZERO & FILL macros must handle spans < 2*sizeof(long) bytes.
321 * For machines with 64-bit longs this is <16 bytes; otherwise
322 * this is <8 bytes. We optimize the code here to reflect the
323 * machine characteristics.
326 # define FILL(n, cp) \
328 case 15:(cp)[14] = 0xff; case 14:(cp)[13] = 0xff; case 13: (cp)[12] = 0xff;\
329 case 12:(cp)[11] = 0xff; case 11:(cp)[10] = 0xff; case 10: (cp)[9] = 0xff;\
330 case 9: (cp)[8] = 0xff; case 8: (cp)[7] = 0xff; case 7: (cp)[6] = 0xff;\
331 case 6: (cp)[5] = 0xff; case 5: (cp)[4] = 0xff; case 4: (cp)[3] = 0xff;\
332 case 3: (cp)[2] = 0xff; case 2: (cp)[1] = 0xff; \
333 case 1: (cp)[0] = 0xff; (cp) += (n); case 0: ; \
335 # define ZERO(n, cp) \
337 case 15:(cp)[14] = 0; case 14:(cp)[13] = 0; case 13: (cp)[12] = 0; \
338 case 12:(cp)[11] = 0; case 11:(cp)[10] = 0; case 10: (cp)[9] = 0; \
339 case 9: (cp)[8] = 0; case 8: (cp)[7] = 0; case 7: (cp)[6] = 0; \
340 case 6: (cp)[5] = 0; case 5: (cp)[4] = 0; case 4: (cp)[3] = 0; \
341 case 3: (cp)[2] = 0; case 2: (cp)[1] = 0; \
342 case 1: (cp)[0] = 0; (cp) += (n); case 0: ; \
345 # define FILL(n, cp) \
347 case 7: (cp)[6] = 0xff; case 6: (cp)[5] = 0xff; case 5: (cp)[4] = 0xff; \
348 case 4: (cp)[3] = 0xff; case 3: (cp)[2] = 0xff; case 2: (cp)[1] = 0xff; \
349 case 1: (cp)[0] = 0xff; (cp) += (n); case 0: ; \
351 # define ZERO(n, cp) \
353 case 7: (cp)[6] = 0; case 6: (cp)[5] = 0; case 5: (cp)[4] = 0; \
354 case 4: (cp)[3] = 0; case 3: (cp)[2] = 0; case 2: (cp)[1] = 0; \
355 case 1: (cp)[0] = 0; (cp) += (n); case 0: ; \
360 * Bit-fill a row according to the white/black
361 * runs generated during G3/G4 decoding.
364 _TIFFFax3fillruns(unsigned char* buf
, uint32
* runs
, uint32
* erun
, uint32 lastx
)
366 static const unsigned char _fillmasks
[] =
367 { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
376 for (; runs
< erun
; runs
+= 2) {
378 if (x
+run
> lastx
|| run
> lastx
)
379 run
= runs
[0] = (uint32
) (lastx
- x
);
384 if (bx
) { /* align to byte boundary */
385 *cp
++ &= 0xff << (8-bx
);
388 if( (n
= run
>> 3) != 0 ) { /* multiple bytes to fill */
389 if ((n
/sizeof (long)) > 1) {
391 * Align to longword boundary and fill.
393 for (; n
&& !isAligned(cp
, long); n
--)
396 nw
= (int32
)(n
/ sizeof (long));
397 n
-= nw
* sizeof (long);
401 cp
= (unsigned char*) lp
;
407 cp
[0] &= 0xff >> run
;
409 cp
[0] &= ~(_fillmasks
[run
]>>bx
);
413 if (x
+run
> lastx
|| run
> lastx
)
414 run
= runs
[1] = lastx
- x
;
419 if (bx
) { /* align to byte boundary */
423 if( (n
= run
>>3) != 0 ) { /* multiple bytes to fill */
424 if ((n
/sizeof (long)) > 1) {
426 * Align to longword boundary and fill.
428 for (; n
&& !isAligned(cp
, long); n
--)
431 nw
= (int32
)(n
/ sizeof (long));
432 n
-= nw
* sizeof (long);
436 cp
= (unsigned char*) lp
;
442 cp
[0] |= 0xff00 >> run
;
444 cp
[0] |= _fillmasks
[run
]>>bx
;
454 * Setup G3/G4-related compression/decompression state
455 * before data is processed. This routine is called once
456 * per image -- it sets up different state based on whether
457 * or not decoding or encoding is being done and whether
458 * 1D- or 2D-encoded data is involved.
461 Fax3SetupState(TIFF
* tif
)
463 TIFFDirectory
* td
= &tif
->tif_dir
;
464 Fax3BaseState
* sp
= Fax3State(tif
);
466 Fax3CodecState
* dsp
= (Fax3CodecState
*) Fax3State(tif
);
467 uint32 rowbytes
, rowpixels
, nruns
;
469 if (td
->td_bitspersample
!= 1) {
470 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
471 "Bits/sample must be 1 for Group 3/4 encoding/decoding");
475 * Calculate the scanline/tile widths.
478 rowbytes
= TIFFTileRowSize(tif
);
479 rowpixels
= td
->td_tilewidth
;
481 rowbytes
= TIFFScanlineSize(tif
);
482 rowpixels
= td
->td_imagewidth
;
484 sp
->rowbytes
= (uint32
) rowbytes
;
485 sp
->rowpixels
= (uint32
) rowpixels
;
487 * Allocate any additional space required for decoding/encoding.
490 (sp
->groupoptions
& GROUP3OPT_2DENCODING
) ||
491 td
->td_compression
== COMPRESSION_CCITTFAX4
494 nruns
= needsRefLine
? 2*TIFFroundup(rowpixels
,32) : rowpixels
;
496 dsp
->runs
= (uint32
*) _TIFFCheckMalloc(tif
, 2*nruns
+3, sizeof (uint32
),
497 "for Group 3/4 run arrays");
498 if (dsp
->runs
== NULL
)
500 dsp
->curruns
= dsp
->runs
;
502 dsp
->refruns
= dsp
->runs
+ (nruns
>>1);
505 if (td
->td_compression
== COMPRESSION_CCITTFAX3
506 && is2DEncoding(dsp
)) { /* NB: default is 1D routine */
507 tif
->tif_decoderow
= Fax3Decode2D
;
508 tif
->tif_decodestrip
= Fax3Decode2D
;
509 tif
->tif_decodetile
= Fax3Decode2D
;
512 if (needsRefLine
) { /* 2d encoding */
513 Fax3CodecState
* esp
= EncoderState(tif
);
515 * 2d encoding requires a scanline
516 * buffer for the ``reference line''; the
517 * scanline against which delta encoding
518 * is referenced. The reference line must
519 * be initialized to be ``white'' (done elsewhere).
521 esp
->refline
= (unsigned char*) _TIFFmalloc(rowbytes
);
522 if (esp
->refline
== NULL
) {
523 TIFFErrorExt(tif
->tif_clientdata
, "Fax3SetupState",
524 "%s: No space for Group 3/4 reference line",
528 } else /* 1d encoding */
529 EncoderState(tif
)->refline
= NULL
;
535 * CCITT Group 3 FAX Encoding.
538 #define Fax3FlushBits(tif, sp) { \
539 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
540 (void) TIFFFlushData1(tif); \
541 *(tif)->tif_rawcp++ = (tidataval_t) (sp)->data; \
542 (tif)->tif_rawcc++; \
543 (sp)->data = 0, (sp)->bit = 8; \
545 #define _FlushBits(tif) { \
546 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
547 (void) TIFFFlushData1(tif); \
548 *(tif)->tif_rawcp++ = (tidataval_t) data; \
549 (tif)->tif_rawcc++; \
552 static const int _msbmask
[9] =
553 { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
554 #define _PutBits(tif, bits, length) { \
555 while (length > bit) { \
556 data |= bits >> (length - bit); \
560 data |= (bits & _msbmask[length]) << (bit - length); \
567 * Write a variable-length bit-value to
568 * the output stream. Values are
569 * assumed to be at most 16 bits.
572 Fax3PutBits(TIFF
* tif
, unsigned int bits
, unsigned int length
)
574 Fax3CodecState
* sp
= EncoderState(tif
);
575 unsigned int bit
= sp
->bit
;
578 _PutBits(tif
, bits
, length
);
585 * Write a code to the output stream.
587 #define putcode(tif, te) Fax3PutBits(tif, (te)->code, (te)->length)
590 #define DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B")
591 #define DEBUG_PRINT(what,len) { \
593 printf("%08X/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what), len); \
594 for (t = length-1; t >= 0; t--) \
595 putchar(code & (1<<t) ? '1' : '0'); \
601 * Write the sequence of codes that describes
602 * the specified span of zero's or one's. The
603 * appropriate table that holds the make-up and
604 * terminating codes is supplied.
607 putspan(TIFF
* tif
, int32 span
, const tableentry
* tab
)
609 Fax3CodecState
* sp
= EncoderState(tif
);
610 unsigned int bit
= sp
->bit
;
612 unsigned int code
, length
;
614 while (span
>= 2624) {
615 const tableentry
* te
= &tab
[63 + (2560>>6)];
616 code
= te
->code
, length
= te
->length
;
618 DEBUG_PRINT("MakeUp", te
->runlen
);
620 _PutBits(tif
, code
, length
);
624 const tableentry
* te
= &tab
[63 + (span
>>6)];
625 assert(te
->runlen
== 64*(span
>>6));
626 code
= te
->code
, length
= te
->length
;
628 DEBUG_PRINT("MakeUp", te
->runlen
);
630 _PutBits(tif
, code
, length
);
633 code
= tab
[span
].code
, length
= tab
[span
].length
;
635 DEBUG_PRINT(" Term", tab
[span
].runlen
);
637 _PutBits(tif
, code
, length
);
644 * Write an EOL code to the output stream. The zero-fill
645 * logic for byte-aligning encoded scanlines is handled
646 * here. We also handle writing the tag bit for the next
647 * scanline when doing 2d encoding.
650 Fax3PutEOL(TIFF
* tif
)
652 Fax3CodecState
* sp
= EncoderState(tif
);
653 unsigned int bit
= sp
->bit
;
655 unsigned int code
, length
, tparm
;
657 if (sp
->b
.groupoptions
& GROUP3OPT_FILLBITS
) {
659 * Force bit alignment so EOL will terminate on
660 * a byte boundary. That is, force the bit alignment
661 * to 16-12 = 4 before putting out the EOL code.
664 if (align
!= sp
->bit
) {
666 align
= sp
->bit
+ (8 - align
);
668 align
= sp
->bit
- align
;
671 _PutBits(tif
, 0, tparm
);
674 code
= EOL
, length
= 12;
675 if (is2DEncoding(sp
))
676 code
= (code
<<1) | (sp
->tag
== G3_1D
), length
++;
677 _PutBits(tif
, code
, length
);
684 * Reset encoding state at the start of a strip.
687 Fax3PreEncode(TIFF
* tif
, tsample_t s
)
689 Fax3CodecState
* sp
= EncoderState(tif
);
697 * This is necessary for Group 4; otherwise it isn't
698 * needed because the first scanline of each strip ends
699 * up being copied into the refline.
702 _TIFFmemset(sp
->refline
, 0x00, sp
->b
.rowbytes
);
703 if (is2DEncoding(sp
)) {
704 float res
= tif
->tif_dir
.td_yresolution
;
706 * The CCITT spec says that when doing 2d encoding, you
707 * should only do it on K consecutive scanlines, where K
708 * depends on the resolution of the image being encoded
709 * (2 for <= 200 lpi, 4 for > 200 lpi). Since the directory
710 * code initializes td_yresolution to 0, this code will
711 * select a K of 2 unless the YResolution tag is set
712 * appropriately. (Note also that we fudge a little here
713 * and use 150 lpi to avoid problems with units conversion.)
715 if (tif
->tif_dir
.td_resolutionunit
== RESUNIT_CENTIMETER
)
716 res
*= 2.54f
; /* convert to inches */
717 sp
->maxk
= (res
> 150 ? 4 : 2);
720 sp
->k
= sp
->maxk
= 0;
724 static const unsigned char zeroruns
[256] = {
725 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0f */
726 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */
727 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2f */
728 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3f */
729 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */
730 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5f */
731 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */
732 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7f */
733 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8f */
734 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9f */
735 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 - 0xaf */
736 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 - 0xbf */
737 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 - 0xcf */
738 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 - 0xdf */
739 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 - 0xef */
740 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xf0 - 0xff */
742 static const unsigned char oneruns
[256] = {
743 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 - 0x0f */
744 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */
745 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 - 0x2f */
746 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */
747 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */
748 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */
749 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6f */
750 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7f */
751 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x80 - 0x8f */
752 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x90 - 0x9f */
753 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xa0 - 0xaf */
754 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xb0 - 0xbf */
755 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xc0 - 0xcf */
756 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xd0 - 0xdf */
757 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */
758 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, /* 0xf0 - 0xff */
762 * On certain systems it pays to inline
763 * the routines that find pixel spans.
766 static int32
find0span(unsigned char*, int32
, int32
);
767 static int32
find1span(unsigned char*, int32
, int32
);
768 #pragma inline(find0span,find1span)
772 * Find a span of ones or zeros using the supplied
773 * table. The ``base'' of the bit string is supplied
774 * along with the start+end bit indices.
777 find0span(unsigned char* bp
, int32 bs
, int32 be
)
779 int32 bits
= be
- bs
;
784 * Check partial byte on lhs.
786 if (bits
> 0 && (n
= (bs
& 7))) {
787 span
= zeroruns
[(*bp
<< n
) & 0xff];
788 if (span
> 8-n
) /* table value too generous */
790 if (span
> bits
) /* constrain span to bit range */
792 if (n
+span
< 8) /* doesn't extend to edge of byte */
798 if (bits
>= (int32
)(2 * 8 * sizeof(long))) {
801 * Align to longword boundary and check longwords.
803 while (!isAligned(bp
, long)) {
805 return (span
+ zeroruns
[*bp
]);
806 span
+= 8, bits
-= 8;
810 while ((bits
>= (int32
)(8 * sizeof(long))) && (0 == *lp
)) {
811 span
+= 8*sizeof (long), bits
-= 8*sizeof (long);
814 bp
= (unsigned char*) lp
;
817 * Scan full bytes for all 0's.
820 if (*bp
!= 0x00) /* end of run */
821 return (span
+ zeroruns
[*bp
]);
822 span
+= 8, bits
-= 8;
826 * Check partial byte on rhs.
830 span
+= (n
> bits
? bits
: n
);
836 find1span(unsigned char* bp
, int32 bs
, int32 be
)
838 int32 bits
= be
- bs
;
843 * Check partial byte on lhs.
845 if (bits
> 0 && (n
= (bs
& 7))) {
846 span
= oneruns
[(*bp
<< n
) & 0xff];
847 if (span
> 8-n
) /* table value too generous */
849 if (span
> bits
) /* constrain span to bit range */
851 if (n
+span
< 8) /* doesn't extend to edge of byte */
857 if (bits
>= (int32
)(2 * 8 * sizeof(long))) {
860 * Align to longword boundary and check longwords.
862 while (!isAligned(bp
, long)) {
864 return (span
+ oneruns
[*bp
]);
865 span
+= 8, bits
-= 8;
869 while ((bits
>= (int32
)(8 * sizeof(long))) && (~0 == *lp
)) {
870 span
+= 8*sizeof (long), bits
-= 8*sizeof (long);
873 bp
= (unsigned char*) lp
;
876 * Scan full bytes for all 1's.
879 if (*bp
!= 0xff) /* end of run */
880 return (span
+ oneruns
[*bp
]);
881 span
+= 8, bits
-= 8;
885 * Check partial byte on rhs.
889 span
+= (n
> bits
? bits
: n
);
895 * Return the offset of the next bit in the range
896 * [bs..be] that is different from the specified
897 * color. The end, be, is returned if no such bit
900 #define finddiff(_cp, _bs, _be, _color) \
901 (_bs + (_color ? find1span(_cp,_bs,_be) : find0span(_cp,_bs,_be)))
903 * Like finddiff, but also check the starting bit
904 * against the end in case start > end.
906 #define finddiff2(_cp, _bs, _be, _color) \
907 (_bs < _be ? finddiff(_cp,_bs,_be,_color) : _be)
910 * 1d-encode a row of pixels. The encoding is
911 * a sequence of all-white or all-black spans
912 * of pixels encoded with Huffman codes.
915 Fax3Encode1DRow(TIFF
* tif
, unsigned char* bp
, uint32 bits
)
917 Fax3CodecState
* sp
= EncoderState(tif
);
922 span
= find0span(bp
, bs
, bits
); /* white span */
923 putspan(tif
, span
, TIFFFaxWhiteCodes
);
927 span
= find1span(bp
, bs
, bits
); /* black span */
928 putspan(tif
, span
, TIFFFaxBlackCodes
);
933 if (sp
->b
.mode
& (FAXMODE_BYTEALIGN
|FAXMODE_WORDALIGN
)) {
934 if (sp
->bit
!= 8) /* byte-align */
935 Fax3FlushBits(tif
, sp
);
936 if ((sp
->b
.mode
&FAXMODE_WORDALIGN
) &&
937 !isAligned(tif
->tif_rawcp
, uint16
))
938 Fax3FlushBits(tif
, sp
);
943 static const tableentry horizcode
=
944 { 3, 0x1, 0 }; /* 001 */
945 static const tableentry passcode
=
946 { 4, 0x1, 0 }; /* 0001 */
947 static const tableentry vcodes
[7] = {
948 { 7, 0x03, 0 }, /* 0000 011 */
949 { 6, 0x03, 0 }, /* 0000 11 */
950 { 3, 0x03, 0 }, /* 011 */
951 { 1, 0x1, 0 }, /* 1 */
952 { 3, 0x2, 0 }, /* 010 */
953 { 6, 0x02, 0 }, /* 0000 10 */
954 { 7, 0x02, 0 } /* 0000 010 */
958 * 2d-encode a row of pixels. Consult the CCITT
959 * documentation for the algorithm.
962 Fax3Encode2DRow(TIFF
* tif
, unsigned char* bp
, unsigned char* rp
, uint32 bits
)
964 #define PIXEL(buf,ix) ((((buf)[(ix)>>3]) >> (7-((ix)&7))) & 1)
966 uint32 a1
= (PIXEL(bp
, 0) != 0 ? 0 : finddiff(bp
, 0, bits
, 0));
967 uint32 b1
= (PIXEL(rp
, 0) != 0 ? 0 : finddiff(rp
, 0, bits
, 0));
971 b2
= finddiff2(rp
, b1
, bits
, PIXEL(rp
,b1
));
974 if (!(-3 <= d
&& d
<= 3)) { /* horizontal mode */
975 a2
= finddiff2(bp
, a1
, bits
, PIXEL(bp
,a1
));
976 putcode(tif
, &horizcode
);
977 if (a0
+a1
== 0 || PIXEL(bp
, a0
) == 0) {
978 putspan(tif
, a1
-a0
, TIFFFaxWhiteCodes
);
979 putspan(tif
, a2
-a1
, TIFFFaxBlackCodes
);
981 putspan(tif
, a1
-a0
, TIFFFaxBlackCodes
);
982 putspan(tif
, a2
-a1
, TIFFFaxWhiteCodes
);
985 } else { /* vertical mode */
986 putcode(tif
, &vcodes
[d
+3]);
989 } else { /* pass mode */
990 putcode(tif
, &passcode
);
995 a1
= finddiff(bp
, a0
, bits
, PIXEL(bp
,a0
));
996 b1
= finddiff(rp
, a0
, bits
, !PIXEL(bp
,a0
));
997 b1
= finddiff(rp
, b1
, bits
, PIXEL(bp
,a0
));
1004 * Encode a buffer of pixels.
1007 Fax3Encode(TIFF
* tif
, tidata_t bp
, tsize_t cc
, tsample_t s
)
1009 Fax3CodecState
* sp
= EncoderState(tif
);
1012 while ((long)cc
> 0) {
1013 if ((sp
->b
.mode
& FAXMODE_NOEOL
) == 0)
1015 if (is2DEncoding(sp
)) {
1016 if (sp
->tag
== G3_1D
) {
1017 if (!Fax3Encode1DRow(tif
, bp
, sp
->b
.rowpixels
))
1021 if (!Fax3Encode2DRow(tif
, bp
, sp
->refline
,
1030 _TIFFmemcpy(sp
->refline
, bp
, sp
->b
.rowbytes
);
1032 if (!Fax3Encode1DRow(tif
, bp
, sp
->b
.rowpixels
))
1035 bp
+= sp
->b
.rowbytes
;
1036 cc
-= sp
->b
.rowbytes
;
1042 Fax3PostEncode(TIFF
* tif
)
1044 Fax3CodecState
* sp
= EncoderState(tif
);
1047 Fax3FlushBits(tif
, sp
);
1052 Fax3Close(TIFF
* tif
)
1054 if ((Fax3State(tif
)->mode
& FAXMODE_NORTC
) == 0) {
1055 Fax3CodecState
* sp
= EncoderState(tif
);
1056 unsigned int code
= EOL
;
1057 unsigned int length
= 12;
1060 if (is2DEncoding(sp
))
1061 code
= (code
<<1) | (sp
->tag
== G3_1D
), length
++;
1062 for (i
= 0; i
< 6; i
++)
1063 Fax3PutBits(tif
, code
, length
);
1064 Fax3FlushBits(tif
, sp
);
1069 Fax3Cleanup(TIFF
* tif
)
1071 Fax3CodecState
* sp
= DecoderState(tif
);
1075 tif
->tif_tagmethods
.vgetfield
= sp
->b
.vgetparent
;
1076 tif
->tif_tagmethods
.vsetfield
= sp
->b
.vsetparent
;
1079 _TIFFfree(sp
->runs
);
1081 _TIFFfree(sp
->refline
);
1083 if (Fax3State(tif
)->subaddress
)
1084 _TIFFfree(Fax3State(tif
)->subaddress
);
1085 _TIFFfree(tif
->tif_data
);
1086 tif
->tif_data
= NULL
;
1088 _TIFFSetDefaultCompressionState(tif
);
1091 #define FIELD_BADFAXLINES (FIELD_CODEC+0)
1092 #define FIELD_CLEANFAXDATA (FIELD_CODEC+1)
1093 #define FIELD_BADFAXRUN (FIELD_CODEC+2)
1094 #define FIELD_RECVPARAMS (FIELD_CODEC+3)
1095 #define FIELD_SUBADDRESS (FIELD_CODEC+4)
1096 #define FIELD_RECVTIME (FIELD_CODEC+5)
1097 #define FIELD_FAXDCS (FIELD_CODEC+6)
1099 #define FIELD_OPTIONS (FIELD_CODEC+7)
1101 static const TIFFFieldInfo faxFieldInfo
[] = {
1102 { TIFFTAG_FAXMODE
, 0, 0, TIFF_ANY
, FIELD_PSEUDO
,
1103 FALSE
, FALSE
, "FaxMode" },
1104 { TIFFTAG_FAXFILLFUNC
, 0, 0, TIFF_ANY
, FIELD_PSEUDO
,
1105 FALSE
, FALSE
, "FaxFillFunc" },
1106 { TIFFTAG_BADFAXLINES
, 1, 1, TIFF_LONG
, FIELD_BADFAXLINES
,
1107 TRUE
, FALSE
, "BadFaxLines" },
1108 { TIFFTAG_BADFAXLINES
, 1, 1, TIFF_SHORT
, FIELD_BADFAXLINES
,
1109 TRUE
, FALSE
, "BadFaxLines" },
1110 { TIFFTAG_CLEANFAXDATA
, 1, 1, TIFF_SHORT
, FIELD_CLEANFAXDATA
,
1111 TRUE
, FALSE
, "CleanFaxData" },
1112 { TIFFTAG_CONSECUTIVEBADFAXLINES
,1,1, TIFF_LONG
, FIELD_BADFAXRUN
,
1113 TRUE
, FALSE
, "ConsecutiveBadFaxLines" },
1114 { TIFFTAG_CONSECUTIVEBADFAXLINES
,1,1, TIFF_SHORT
, FIELD_BADFAXRUN
,
1115 TRUE
, FALSE
, "ConsecutiveBadFaxLines" },
1116 { TIFFTAG_FAXRECVPARAMS
, 1, 1, TIFF_LONG
, FIELD_RECVPARAMS
,
1117 TRUE
, FALSE
, "FaxRecvParams" },
1118 { TIFFTAG_FAXSUBADDRESS
, -1,-1, TIFF_ASCII
, FIELD_SUBADDRESS
,
1119 TRUE
, FALSE
, "FaxSubAddress" },
1120 { TIFFTAG_FAXRECVTIME
, 1, 1, TIFF_LONG
, FIELD_RECVTIME
,
1121 TRUE
, FALSE
, "FaxRecvTime" },
1122 { TIFFTAG_FAXDCS
, -1,-1, TIFF_ASCII
, FIELD_FAXDCS
,
1123 TRUE
, FALSE
, "FaxDcs" },
1125 static const TIFFFieldInfo fax3FieldInfo
[] = {
1126 { TIFFTAG_GROUP3OPTIONS
, 1, 1, TIFF_LONG
, FIELD_OPTIONS
,
1127 FALSE
, FALSE
, "Group3Options" },
1129 static const TIFFFieldInfo fax4FieldInfo
[] = {
1130 { TIFFTAG_GROUP4OPTIONS
, 1, 1, TIFF_LONG
, FIELD_OPTIONS
,
1131 FALSE
, FALSE
, "Group4Options" },
1133 #define N(a) (sizeof (a) / sizeof (a[0]))
1136 Fax3VSetField(TIFF
* tif
, ttag_t tag
, va_list ap
)
1138 Fax3BaseState
* sp
= Fax3State(tif
);
1141 assert(sp
->vsetparent
!= 0);
1144 case TIFFTAG_FAXMODE
:
1145 sp
->mode
= va_arg(ap
, int);
1146 return (1); /* NB: pseudo tag */
1147 case TIFFTAG_FAXFILLFUNC
:
1148 DecoderState(tif
)->fill
= va_arg(ap
, TIFFFaxFillFunc
);
1149 return (1); /* NB: pseudo tag */
1150 case TIFFTAG_GROUP3OPTIONS
:
1151 /* XXX: avoid reading options if compression mismatches. */
1152 if (tif
->tif_dir
.td_compression
== COMPRESSION_CCITTFAX3
)
1153 sp
->groupoptions
= va_arg(ap
, uint32
);
1155 case TIFFTAG_GROUP4OPTIONS
:
1156 /* XXX: avoid reading options if compression mismatches. */
1157 if (tif
->tif_dir
.td_compression
== COMPRESSION_CCITTFAX4
)
1158 sp
->groupoptions
= va_arg(ap
, uint32
);
1160 case TIFFTAG_BADFAXLINES
:
1161 sp
->badfaxlines
= va_arg(ap
, uint32
);
1163 case TIFFTAG_CLEANFAXDATA
:
1164 sp
->cleanfaxdata
= (uint16
) va_arg(ap
, int);
1166 case TIFFTAG_CONSECUTIVEBADFAXLINES
:
1167 sp
->badfaxrun
= va_arg(ap
, uint32
);
1169 case TIFFTAG_FAXRECVPARAMS
:
1170 sp
->recvparams
= va_arg(ap
, uint32
);
1172 case TIFFTAG_FAXSUBADDRESS
:
1173 _TIFFsetString(&sp
->subaddress
, va_arg(ap
, char*));
1175 case TIFFTAG_FAXRECVTIME
:
1176 sp
->recvtime
= va_arg(ap
, uint32
);
1178 case TIFFTAG_FAXDCS
:
1179 _TIFFsetString(&sp
->faxdcs
, va_arg(ap
, char*));
1182 return (*sp
->vsetparent
)(tif
, tag
, ap
);
1184 TIFFSetFieldBit(tif
, _TIFFFieldWithTag(tif
, tag
)->field_bit
);
1185 tif
->tif_flags
|= TIFF_DIRTYDIRECT
;
1190 Fax3VGetField(TIFF
* tif
, ttag_t tag
, va_list ap
)
1192 Fax3BaseState
* sp
= Fax3State(tif
);
1195 case TIFFTAG_FAXMODE
:
1196 *va_arg(ap
, int*) = sp
->mode
;
1198 case TIFFTAG_FAXFILLFUNC
:
1199 *va_arg(ap
, TIFFFaxFillFunc
*) = DecoderState(tif
)->fill
;
1201 case TIFFTAG_GROUP3OPTIONS
:
1202 case TIFFTAG_GROUP4OPTIONS
:
1203 *va_arg(ap
, uint32
*) = sp
->groupoptions
;
1205 case TIFFTAG_BADFAXLINES
:
1206 *va_arg(ap
, uint32
*) = sp
->badfaxlines
;
1208 case TIFFTAG_CLEANFAXDATA
:
1209 *va_arg(ap
, uint16
*) = sp
->cleanfaxdata
;
1211 case TIFFTAG_CONSECUTIVEBADFAXLINES
:
1212 *va_arg(ap
, uint32
*) = sp
->badfaxrun
;
1214 case TIFFTAG_FAXRECVPARAMS
:
1215 *va_arg(ap
, uint32
*) = sp
->recvparams
;
1217 case TIFFTAG_FAXSUBADDRESS
:
1218 *va_arg(ap
, char**) = sp
->subaddress
;
1220 case TIFFTAG_FAXRECVTIME
:
1221 *va_arg(ap
, uint32
*) = sp
->recvtime
;
1223 case TIFFTAG_FAXDCS
:
1224 *va_arg(ap
, char**) = sp
->faxdcs
;
1227 return (*sp
->vgetparent
)(tif
, tag
, ap
);
1233 Fax3PrintDir(TIFF
* tif
, FILE* fd
, long flags
)
1235 Fax3BaseState
* sp
= Fax3State(tif
);
1238 if (TIFFFieldSet(tif
,FIELD_OPTIONS
)) {
1239 const char* sep
= " ";
1240 if (tif
->tif_dir
.td_compression
== COMPRESSION_CCITTFAX4
) {
1241 fprintf(fd
, " Group 4 Options:");
1242 if (sp
->groupoptions
& GROUP4OPT_UNCOMPRESSED
)
1243 fprintf(fd
, "%suncompressed data", sep
);
1246 fprintf(fd
, " Group 3 Options:");
1247 if (sp
->groupoptions
& GROUP3OPT_2DENCODING
)
1248 fprintf(fd
, "%s2-d encoding", sep
), sep
= "+";
1249 if (sp
->groupoptions
& GROUP3OPT_FILLBITS
)
1250 fprintf(fd
, "%sEOL padding", sep
), sep
= "+";
1251 if (sp
->groupoptions
& GROUP3OPT_UNCOMPRESSED
)
1252 fprintf(fd
, "%suncompressed data", sep
);
1254 fprintf(fd
, " (%lu = 0x%lx)\n",
1255 (unsigned long) sp
->groupoptions
,
1256 (unsigned long) sp
->groupoptions
);
1258 if (TIFFFieldSet(tif
,FIELD_CLEANFAXDATA
)) {
1259 fprintf(fd
, " Fax Data:");
1260 switch (sp
->cleanfaxdata
) {
1261 case CLEANFAXDATA_CLEAN
:
1262 fprintf(fd
, " clean");
1264 case CLEANFAXDATA_REGENERATED
:
1265 fprintf(fd
, " receiver regenerated");
1267 case CLEANFAXDATA_UNCLEAN
:
1268 fprintf(fd
, " uncorrected errors");
1271 fprintf(fd
, " (%u = 0x%x)\n",
1272 sp
->cleanfaxdata
, sp
->cleanfaxdata
);
1274 if (TIFFFieldSet(tif
,FIELD_BADFAXLINES
))
1275 fprintf(fd
, " Bad Fax Lines: %lu\n",
1276 (unsigned long) sp
->badfaxlines
);
1277 if (TIFFFieldSet(tif
,FIELD_BADFAXRUN
))
1278 fprintf(fd
, " Consecutive Bad Fax Lines: %lu\n",
1279 (unsigned long) sp
->badfaxrun
);
1280 if (TIFFFieldSet(tif
,FIELD_RECVPARAMS
))
1281 fprintf(fd
, " Fax Receive Parameters: %08lx\n",
1282 (unsigned long) sp
->recvparams
);
1283 if (TIFFFieldSet(tif
,FIELD_SUBADDRESS
))
1284 fprintf(fd
, " Fax SubAddress: %s\n", sp
->subaddress
);
1285 if (TIFFFieldSet(tif
,FIELD_RECVTIME
))
1286 fprintf(fd
, " Fax Receive Time: %lu secs\n",
1287 (unsigned long) sp
->recvtime
);
1288 if (TIFFFieldSet(tif
,FIELD_FAXDCS
))
1289 fprintf(fd
, " Fax DCS: %s\n", sp
->faxdcs
);
1293 InitCCITTFax3(TIFF
* tif
)
1298 * Allocate state block so tag methods have storage to record values.
1300 tif
->tif_data
= (tidata_t
)
1301 _TIFFmalloc(sizeof (Fax3CodecState
));
1303 if (tif
->tif_data
== NULL
) {
1304 TIFFErrorExt(tif
->tif_clientdata
, "TIFFInitCCITTFax3",
1305 "%s: No space for state block", tif
->tif_name
);
1309 sp
= Fax3State(tif
);
1310 sp
->rw_mode
= tif
->tif_mode
;
1313 * Merge codec-specific tag information and
1314 * override parent get/set field methods.
1316 _TIFFMergeFieldInfo(tif
, faxFieldInfo
, N(faxFieldInfo
));
1317 sp
->vgetparent
= tif
->tif_tagmethods
.vgetfield
;
1318 tif
->tif_tagmethods
.vgetfield
= Fax3VGetField
; /* hook for codec tags */
1319 sp
->vsetparent
= tif
->tif_tagmethods
.vsetfield
;
1320 tif
->tif_tagmethods
.vsetfield
= Fax3VSetField
; /* hook for codec tags */
1321 tif
->tif_tagmethods
.printdir
= Fax3PrintDir
; /* hook for codec tags */
1322 sp
->groupoptions
= 0;
1324 sp
->subaddress
= NULL
;
1327 if (sp
->rw_mode
== O_RDONLY
) /* FIXME: improve for in place update */
1328 tif
->tif_flags
|= TIFF_NOBITREV
; /* decoder does bit reversal */
1329 DecoderState(tif
)->runs
= NULL
;
1330 TIFFSetField(tif
, TIFFTAG_FAXFILLFUNC
, _TIFFFax3fillruns
);
1331 EncoderState(tif
)->refline
= NULL
;
1334 * Install codec methods.
1336 tif
->tif_setupdecode
= Fax3SetupState
;
1337 tif
->tif_predecode
= Fax3PreDecode
;
1338 tif
->tif_decoderow
= Fax3Decode1D
;
1339 tif
->tif_decodestrip
= Fax3Decode1D
;
1340 tif
->tif_decodetile
= Fax3Decode1D
;
1341 tif
->tif_setupencode
= Fax3SetupState
;
1342 tif
->tif_preencode
= Fax3PreEncode
;
1343 tif
->tif_postencode
= Fax3PostEncode
;
1344 tif
->tif_encoderow
= Fax3Encode
;
1345 tif
->tif_encodestrip
= Fax3Encode
;
1346 tif
->tif_encodetile
= Fax3Encode
;
1347 tif
->tif_close
= Fax3Close
;
1348 tif
->tif_cleanup
= Fax3Cleanup
;
1354 TIFFInitCCITTFax3(TIFF
* tif
, int scheme
)
1357 if (InitCCITTFax3(tif
)) {
1358 _TIFFMergeFieldInfo(tif
, fax3FieldInfo
, N(fax3FieldInfo
));
1361 * The default format is Class/F-style w/o RTC.
1363 return TIFFSetField(tif
, TIFFTAG_FAXMODE
, FAXMODE_CLASSF
);
1369 * CCITT Group 4 (T.6) Facsimile-compatible
1370 * Compression Scheme Support.
1373 #define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }
1375 * Decode the requested amount of G4-encoded data.
1378 Fax4Decode(TIFF
* tif
, tidata_t buf
, tsize_t occ
, tsample_t s
)
1380 DECLARE_STATE_2D(tif
, sp
, "Fax4Decode");
1384 CACHE_STATE(tif
, sp
);
1385 while ((long)occ
> 0) {
1388 pa
= thisrun
= sp
->curruns
;
1392 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc
, BitsAvail
);
1393 printf("-------------------- %d\n", tif
->tif_row
);
1399 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1400 SETVALUE(0); /* imaginary change for reference */
1401 SWAP(uint32
*, sp
->curruns
, sp
->refruns
);
1402 buf
+= sp
->b
.rowbytes
;
1403 occ
-= sp
->b
.rowbytes
;
1407 NeedBits16( 13, BADG4
);
1410 if( GetBits(13) != 0x1001 )
1411 fputs( "Bad RTC\n", stderr
);
1414 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1415 UNCACHE_STATE(tif
, sp
);
1418 UNCACHE_STATE(tif
, sp
);
1424 * Encode the requested amount of data.
1427 Fax4Encode(TIFF
* tif
, tidata_t bp
, tsize_t cc
, tsample_t s
)
1429 Fax3CodecState
*sp
= EncoderState(tif
);
1432 while ((long)cc
> 0) {
1433 if (!Fax3Encode2DRow(tif
, bp
, sp
->refline
, sp
->b
.rowpixels
))
1435 _TIFFmemcpy(sp
->refline
, bp
, sp
->b
.rowbytes
);
1436 bp
+= sp
->b
.rowbytes
;
1437 cc
-= sp
->b
.rowbytes
;
1443 Fax4PostEncode(TIFF
* tif
)
1445 Fax3CodecState
*sp
= EncoderState(tif
);
1447 /* terminate strip w/ EOFB */
1448 Fax3PutBits(tif
, EOL
, 12);
1449 Fax3PutBits(tif
, EOL
, 12);
1451 Fax3FlushBits(tif
, sp
);
1456 TIFFInitCCITTFax4(TIFF
* tif
, int scheme
)
1459 if (InitCCITTFax3(tif
)) { /* reuse G3 support */
1460 _TIFFMergeFieldInfo(tif
, fax4FieldInfo
, N(fax4FieldInfo
));
1462 tif
->tif_decoderow
= Fax4Decode
;
1463 tif
->tif_decodestrip
= Fax4Decode
;
1464 tif
->tif_decodetile
= Fax4Decode
;
1465 tif
->tif_encoderow
= Fax4Encode
;
1466 tif
->tif_encodestrip
= Fax4Encode
;
1467 tif
->tif_encodetile
= Fax4Encode
;
1468 tif
->tif_postencode
= Fax4PostEncode
;
1470 * Suppress RTC at the end of each strip.
1472 return TIFFSetField(tif
, TIFFTAG_FAXMODE
, FAXMODE_NORTC
);
1478 * CCITT Group 3 1-D Modified Huffman RLE Compression Support.
1479 * (Compression algorithms 2 and 32771)
1483 * Decode the requested amount of RLE-encoded data.
1486 Fax3DecodeRLE(TIFF
* tif
, tidata_t buf
, tsize_t occ
, tsample_t s
)
1488 DECLARE_STATE(tif
, sp
, "Fax3DecodeRLE");
1489 int mode
= sp
->b
.mode
;
1493 CACHE_STATE(tif
, sp
);
1494 thisrun
= sp
->curruns
;
1495 while ((long)occ
> 0) {
1500 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc
, BitsAvail
);
1501 printf("-------------------- %d\n", tif
->tif_row
);
1505 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1507 * Cleanup at the end of the row.
1509 if (mode
& FAXMODE_BYTEALIGN
) {
1510 int n
= BitsAvail
- (BitsAvail
&~ 7);
1512 } else if (mode
& FAXMODE_WORDALIGN
) {
1513 int n
= BitsAvail
- (BitsAvail
&~ 15);
1515 if (BitsAvail
== 0 && !isAligned(cp
, uint16
))
1518 buf
+= sp
->b
.rowbytes
;
1519 occ
-= sp
->b
.rowbytes
;
1522 EOFRLE
: /* premature EOF */
1523 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1524 UNCACHE_STATE(tif
, sp
);
1527 UNCACHE_STATE(tif
, sp
);
1532 TIFFInitCCITTRLE(TIFF
* tif
, int scheme
)
1535 if (InitCCITTFax3(tif
)) { /* reuse G3 support */
1536 tif
->tif_decoderow
= Fax3DecodeRLE
;
1537 tif
->tif_decodestrip
= Fax3DecodeRLE
;
1538 tif
->tif_decodetile
= Fax3DecodeRLE
;
1540 * Suppress RTC+EOLs when encoding and byte-align data.
1542 return TIFFSetField(tif
, TIFFTAG_FAXMODE
,
1543 FAXMODE_NORTC
|FAXMODE_NOEOL
|FAXMODE_BYTEALIGN
);
1549 TIFFInitCCITTRLEW(TIFF
* tif
, int scheme
)
1552 if (InitCCITTFax3(tif
)) { /* reuse G3 support */
1553 tif
->tif_decoderow
= Fax3DecodeRLE
;
1554 tif
->tif_decodestrip
= Fax3DecodeRLE
;
1555 tif
->tif_decodetile
= Fax3DecodeRLE
;
1557 * Suppress RTC+EOLs when encoding and word-align data.
1559 return TIFFSetField(tif
, TIFFTAG_FAXMODE
,
1560 FAXMODE_NORTC
|FAXMODE_NOEOL
|FAXMODE_WORDALIGN
);
1564 #endif /* CCITT_SUPPORT */
1566 /* vim: set ts=8 sts=8 sw=8 noet: */