2 * Copyright (c) 1990-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
30 * CCITT Group 3 (T.4) and Group 4 (T.6) Compression Support.
32 * This file contains support for decoding and encoding TIFF
33 * compression algorithms 2, 3, 4, and 32771.
35 * Decoder support is derived, with permission, from the code
36 * in Frank Cringle's viewfax program;
37 * Copyright (C) 1990, 1995 Frank D. Cringle.
45 * Compression+decompression state blocks are
46 * derived from this ``base state'' block.
49 int rw_mode
; /* O_RDONLY for decode, else encode */
50 int mode
; /* operating mode */
51 tmsize_t rowbytes
; /* bytes in a decoded scanline */
52 uint32 rowpixels
; /* pixels in a scanline */
54 uint16 cleanfaxdata
; /* CleanFaxData tag */
55 uint32 badfaxrun
; /* BadFaxRun tag */
56 uint32 badfaxlines
; /* BadFaxLines tag */
57 uint32 groupoptions
; /* Group 3/4 options tag */
59 TIFFVGetMethod vgetparent
; /* super-class method */
60 TIFFVSetMethod vsetparent
; /* super-class method */
61 TIFFPrintMethod printdir
; /* super-class method */
63 #define Fax3State(tif) ((Fax3BaseState*) (tif)->tif_data)
65 typedef enum { G3_1D
, G3_2D
} Ttag
;
69 /* Decoder state info */
70 const unsigned char* bitmap
; /* bit reversal table */
71 uint32 data
; /* current i/o byte/word */
72 int bit
; /* current i/o bit in byte */
73 int EOLcnt
; /* count of EOL codes recognized */
74 TIFFFaxFillFunc fill
; /* fill routine */
75 uint32
* runs
; /* b&w runs for current/previous row */
76 uint32
* refruns
; /* runs for reference line */
77 uint32
* curruns
; /* runs for current line */
79 /* Encoder state info */
80 Ttag tag
; /* encoding state */
81 unsigned char* refline
; /* reference line for 2d decoding */
82 int k
; /* #rows left that can be 2d encoded */
83 int maxk
; /* max #rows that can be 2d encoded */
87 #define DecoderState(tif) ((Fax3CodecState*) Fax3State(tif))
88 #define EncoderState(tif) ((Fax3CodecState*) Fax3State(tif))
90 #define is2DEncoding(sp) (sp->b.groupoptions & GROUP3OPT_2DENCODING)
91 #define isAligned(p,t) ((((size_t)(p)) & (sizeof (t)-1)) == 0)
94 * Group 3 and Group 4 Decoding.
98 * These macros glue the TIFF library state to
99 * the state expected by Frank's decoder.
101 #define DECLARE_STATE(tif, sp, mod) \
102 static const char module[] = mod; \
103 Fax3CodecState* sp = DecoderState(tif); \
104 int a0; /* reference element */ \
105 int lastx = sp->b.rowpixels; /* last element in row */ \
106 uint32 BitAcc; /* bit accumulator */ \
107 int BitsAvail; /* # valid bits in BitAcc */ \
108 int RunLength; /* length of current run */ \
109 unsigned char* cp; /* next byte of input data */ \
110 unsigned char* ep; /* end of input data */ \
111 uint32* pa; /* place to stuff next run */ \
112 uint32* thisrun; /* current row's run array */ \
113 int EOLcnt; /* # EOL codes recognized */ \
114 const unsigned char* bitmap = sp->bitmap; /* input data bit reverser */ \
115 const TIFFFaxTabEnt* TabEnt
116 #define DECLARE_STATE_2D(tif, sp, mod) \
117 DECLARE_STATE(tif, sp, mod); \
118 int b1; /* next change on prev line */ \
119 uint32* pb /* next run in reference line */\
121 * Load any state that may be changed during decoding.
123 #define CACHE_STATE(tif, sp) do { \
125 BitsAvail = sp->bit; \
126 EOLcnt = sp->EOLcnt; \
127 cp = (unsigned char*) tif->tif_rawcp; \
128 ep = cp + tif->tif_rawcc; \
131 * Save state possibly changed during decoding.
133 #define UNCACHE_STATE(tif, sp) do { \
134 sp->bit = BitsAvail; \
136 sp->EOLcnt = EOLcnt; \
137 tif->tif_rawcc -= (tmsize_t)((uint8*) cp - tif->tif_rawcp); \
138 tif->tif_rawcp = (uint8*) cp; \
142 * Setup state for decoding a strip.
145 Fax3PreDecode(TIFF
* tif
, uint16 s
)
147 Fax3CodecState
* sp
= DecoderState(tif
);
151 sp
->bit
= 0; /* force initial read */
153 sp
->EOLcnt
= 0; /* force initial scan for EOL */
155 * Decoder assumes lsb-to-msb bit order. Note that we select
156 * this here rather than in Fax3SetupState so that viewers can
157 * hold the image open, fiddle with the FillOrder tag value,
158 * and then re-decode the image. Otherwise they'd need to close
159 * and open the image to get the state reset.
162 TIFFGetBitRevTable(tif
->tif_dir
.td_fillorder
!= FILLORDER_LSB2MSB
);
163 if (sp
->refruns
) { /* init reference line to white */
164 sp
->refruns
[0] = (uint32
) sp
->b
.rowpixels
;
172 * Routine for handling various errors/conditions.
173 * Note how they are "glued into the decoder" by
174 * overriding the definitions used by the decoder.
178 Fax3Unexpected(const char* module
, TIFF
* tif
, uint32 line
, uint32 a0
)
180 TIFFErrorExt(tif
->tif_clientdata
, module
, "Bad code word at line %u of %s %u (x %u)",
181 line
, isTiled(tif
) ? "tile" : "strip",
182 (isTiled(tif
) ? tif
->tif_curtile
: tif
->tif_curstrip
),
185 #define unexpected(table, a0) Fax3Unexpected(module, tif, sp->line, a0)
188 Fax3Extension(const char* module
, TIFF
* tif
, uint32 line
, uint32 a0
)
190 TIFFErrorExt(tif
->tif_clientdata
, module
,
191 "Uncompressed data (not supported) at line %u of %s %u (x %u)",
192 line
, isTiled(tif
) ? "tile" : "strip",
193 (isTiled(tif
) ? tif
->tif_curtile
: tif
->tif_curstrip
),
196 #define extension(a0) Fax3Extension(module, tif, sp->line, a0)
199 Fax3BadLength(const char* module
, TIFF
* tif
, uint32 line
, uint32 a0
, uint32 lastx
)
201 TIFFWarningExt(tif
->tif_clientdata
, module
, "%s at line %u of %s %u (got %u, expected %u)",
202 a0
< lastx
? "Premature EOL" : "Line length mismatch",
203 line
, isTiled(tif
) ? "tile" : "strip",
204 (isTiled(tif
) ? tif
->tif_curtile
: tif
->tif_curstrip
),
207 #define badlength(a0,lastx) Fax3BadLength(module, tif, sp->line, a0, lastx)
210 Fax3PrematureEOF(const char* module
, TIFF
* tif
, uint32 line
, uint32 a0
)
212 TIFFWarningExt(tif
->tif_clientdata
, module
, "Premature EOF at line %u of %s %u (x %u)",
213 line
, isTiled(tif
) ? "tile" : "strip",
214 (isTiled(tif
) ? tif
->tif_curtile
: tif
->tif_curstrip
),
217 #define prematureEOF(a0) Fax3PrematureEOF(module, tif, sp->line, a0)
222 * Decode the requested amount of G3 1D-encoded data.
225 Fax3Decode1D(TIFF
* tif
, uint8
* buf
, tmsize_t occ
, uint16 s
)
227 DECLARE_STATE(tif
, sp
, "Fax3Decode1D");
229 if (occ
% sp
->b
.rowbytes
)
231 TIFFErrorExt(tif
->tif_clientdata
, module
, "Fractional scanlines cannot be read");
234 CACHE_STATE(tif
, sp
);
235 thisrun
= sp
->curruns
;
241 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc
, BitsAvail
);
242 printf("-------------------- %d\n", tif
->tif_row
);
247 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
248 buf
+= sp
->b
.rowbytes
;
249 occ
-= sp
->b
.rowbytes
;
252 EOF1D
: /* premature EOF */
254 EOF1Da
: /* premature EOF */
255 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
256 UNCACHE_STATE(tif
, sp
);
259 UNCACHE_STATE(tif
, sp
);
263 #define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }
265 * Decode the requested amount of G3 2D-encoded data.
268 Fax3Decode2D(TIFF
* tif
, uint8
* buf
, tmsize_t occ
, uint16 s
)
270 DECLARE_STATE_2D(tif
, sp
, "Fax3Decode2D");
271 int is1D
; /* current line is 1d/2d-encoded */
273 if (occ
% sp
->b
.rowbytes
)
275 TIFFErrorExt(tif
->tif_clientdata
, module
, "Fractional scanlines cannot be read");
278 CACHE_STATE(tif
, sp
);
282 pa
= thisrun
= sp
->curruns
;
284 printf("\nBitAcc=%08X, BitsAvail = %d EOLcnt = %d",
285 BitAcc
, BitsAvail
, EOLcnt
);
289 is1D
= GetBits(1); /* 1D/2D-encoding tag bit */
292 printf(" %s\n-------------------- %d\n",
293 is1D
? "1D" : "2D", tif
->tif_row
);
302 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
303 SETVALUE(0); /* imaginary change for reference */
304 SWAP(uint32
*, sp
->curruns
, sp
->refruns
);
305 buf
+= sp
->b
.rowbytes
;
306 occ
-= sp
->b
.rowbytes
;
309 EOF2D
: /* premature EOF */
311 EOF2Da
: /* premature EOF */
312 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
313 UNCACHE_STATE(tif
, sp
);
316 UNCACHE_STATE(tif
, sp
);
322 * The ZERO & FILL macros must handle spans < 2*sizeof(long) bytes.
323 * For machines with 64-bit longs this is <16 bytes; otherwise
324 * this is <8 bytes. We optimize the code here to reflect the
325 * machine characteristics.
327 #if SIZEOF_UNSIGNED_LONG == 8
328 # define FILL(n, cp) \
330 case 15:(cp)[14] = 0xff; /*-fallthrough*/ \
331 case 14:(cp)[13] = 0xff; /*-fallthrough*/ \
332 case 13:(cp)[12] = 0xff; /*-fallthrough*/ \
333 case 12:(cp)[11] = 0xff; /*-fallthrough*/ \
334 case 11:(cp)[10] = 0xff; /*-fallthrough*/ \
335 case 10: (cp)[9] = 0xff; /*-fallthrough*/ \
336 case 9: (cp)[8] = 0xff; /*-fallthrough*/ \
337 case 8: (cp)[7] = 0xff; /*-fallthrough*/ \
338 case 7: (cp)[6] = 0xff; /*-fallthrough*/ \
339 case 6: (cp)[5] = 0xff; /*-fallthrough*/ \
340 case 5: (cp)[4] = 0xff; /*-fallthrough*/ \
341 case 4: (cp)[3] = 0xff; /*-fallthrough*/ \
342 case 3: (cp)[2] = 0xff; /*-fallthrough*/ \
343 case 2: (cp)[1] = 0xff; /*-fallthrough*/ \
344 case 1: (cp)[0] = 0xff; (cp) += (n); /*-fallthrough*/ \
347 # define ZERO(n, cp) \
349 case 15:(cp)[14] = 0; /*-fallthrough*/ \
350 case 14:(cp)[13] = 0; /*-fallthrough*/ \
351 case 13:(cp)[12] = 0; /*-fallthrough*/ \
352 case 12:(cp)[11] = 0; /*-fallthrough*/ \
353 case 11:(cp)[10] = 0; /*-fallthrough*/ \
354 case 10: (cp)[9] = 0; /*-fallthrough*/ \
355 case 9: (cp)[8] = 0; /*-fallthrough*/ \
356 case 8: (cp)[7] = 0; /*-fallthrough*/ \
357 case 7: (cp)[6] = 0; /*-fallthrough*/ \
358 case 6: (cp)[5] = 0; /*-fallthrough*/ \
359 case 5: (cp)[4] = 0; /*-fallthrough*/ \
360 case 4: (cp)[3] = 0; /*-fallthrough*/ \
361 case 3: (cp)[2] = 0; /*-fallthrough*/ \
362 case 2: (cp)[1] = 0; /*-fallthrough*/ \
363 case 1: (cp)[0] = 0; (cp) += (n); /*-fallthrough*/ \
367 # define FILL(n, cp) \
369 case 7: (cp)[6] = 0xff; /*-fallthrough*/ \
370 case 6: (cp)[5] = 0xff; /*-fallthrough*/ \
371 case 5: (cp)[4] = 0xff; /*-fallthrough*/ \
372 case 4: (cp)[3] = 0xff; /*-fallthrough*/ \
373 case 3: (cp)[2] = 0xff; /*-fallthrough*/ \
374 case 2: (cp)[1] = 0xff; /*-fallthrough*/ \
375 case 1: (cp)[0] = 0xff; (cp) += (n); /*-fallthrough*/ \
378 # define ZERO(n, cp) \
380 case 7: (cp)[6] = 0; /*-fallthrough*/ \
381 case 6: (cp)[5] = 0; /*-fallthrough*/ \
382 case 5: (cp)[4] = 0; /*-fallthrough*/ \
383 case 4: (cp)[3] = 0; /*-fallthrough*/ \
384 case 3: (cp)[2] = 0; /*-fallthrough*/ \
385 case 2: (cp)[1] = 0; /*-fallthrough*/ \
386 case 1: (cp)[0] = 0; (cp) += (n); /*-fallthrough*/ \
392 * Bit-fill a row according to the white/black
393 * runs generated during G3/G4 decoding.
396 _TIFFFax3fillruns(unsigned char* buf
, uint32
* runs
, uint32
* erun
, uint32 lastx
)
398 static const unsigned char _fillmasks
[] =
399 { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
408 for (; runs
< erun
; runs
+= 2) {
410 if (x
+run
> lastx
|| run
> lastx
)
411 run
= runs
[0] = (uint32
) (lastx
- x
);
416 if (bx
) { /* align to byte boundary */
417 *cp
++ &= 0xff << (8-bx
);
420 if( (n
= run
>> 3) != 0 ) { /* multiple bytes to fill */
421 if ((n
/sizeof (long)) > 1) {
423 * Align to longword boundary and fill.
425 for (; n
&& !isAligned(cp
, long); n
--)
428 nw
= (int32
)(n
/ sizeof (long));
429 n
-= nw
* sizeof (long);
433 cp
= (unsigned char*) lp
;
439 cp
[0] &= 0xff >> run
;
441 cp
[0] &= ~(_fillmasks
[run
]>>bx
);
445 if (x
+run
> lastx
|| run
> lastx
)
446 run
= runs
[1] = lastx
- x
;
451 if (bx
) { /* align to byte boundary */
455 if( (n
= run
>>3) != 0 ) { /* multiple bytes to fill */
456 if ((n
/sizeof (long)) > 1) {
458 * Align to longword boundary and fill.
460 for (; n
&& !isAligned(cp
, long); n
--)
463 nw
= (int32
)(n
/ sizeof (long));
464 n
-= nw
* sizeof (long);
468 cp
= (unsigned char*) lp
;
473 /* Explicit 0xff masking to make icc -check=conversions happy */
475 cp
[0] = (unsigned char)((cp
[0] | (0xff00 >> run
))&0xff);
477 cp
[0] |= _fillmasks
[run
]>>bx
;
487 Fax3FixupTags(TIFF
* tif
)
494 * Setup G3/G4-related compression/decompression state
495 * before data is processed. This routine is called once
496 * per image -- it sets up different state based on whether
497 * or not decoding or encoding is being done and whether
498 * 1D- or 2D-encoded data is involved.
501 Fax3SetupState(TIFF
* tif
)
503 static const char module
[] = "Fax3SetupState";
504 TIFFDirectory
* td
= &tif
->tif_dir
;
505 Fax3BaseState
* sp
= Fax3State(tif
);
507 Fax3CodecState
* dsp
= (Fax3CodecState
*) Fax3State(tif
);
509 uint32 rowpixels
, nruns
;
511 if (td
->td_bitspersample
!= 1) {
512 TIFFErrorExt(tif
->tif_clientdata
, module
,
513 "Bits/sample must be 1 for Group 3/4 encoding/decoding");
517 * Calculate the scanline/tile widths.
520 rowbytes
= TIFFTileRowSize(tif
);
521 rowpixels
= td
->td_tilewidth
;
523 rowbytes
= TIFFScanlineSize(tif
);
524 rowpixels
= td
->td_imagewidth
;
526 sp
->rowbytes
= rowbytes
;
527 sp
->rowpixels
= rowpixels
;
529 * Allocate any additional space required for decoding/encoding.
532 (sp
->groupoptions
& GROUP3OPT_2DENCODING
) ||
533 td
->td_compression
== COMPRESSION_CCITTFAX4
537 Assure that allocation computations do not overflow.
539 TIFFroundup and TIFFSafeMultiply return zero on integer overflow
541 dsp
->runs
=(uint32
*) NULL
;
542 nruns
= TIFFroundup_32(rowpixels
,32);
544 nruns
= TIFFSafeMultiply(uint32
,nruns
,2);
546 if ((nruns
== 0) || (TIFFSafeMultiply(uint32
,nruns
,2) == 0)) {
547 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
548 "Row pixels integer overflow (rowpixels %u)",
552 dsp
->runs
= (uint32
*) _TIFFCheckMalloc(tif
,
553 TIFFSafeMultiply(uint32
,nruns
,2),
555 "for Group 3/4 run arrays");
556 if (dsp
->runs
== NULL
)
558 memset( dsp
->runs
, 0, TIFFSafeMultiply(uint32
,nruns
,2)*sizeof(uint32
));
559 dsp
->curruns
= dsp
->runs
;
561 dsp
->refruns
= dsp
->runs
+ nruns
;
564 if (td
->td_compression
== COMPRESSION_CCITTFAX3
565 && is2DEncoding(dsp
)) { /* NB: default is 1D routine */
566 tif
->tif_decoderow
= Fax3Decode2D
;
567 tif
->tif_decodestrip
= Fax3Decode2D
;
568 tif
->tif_decodetile
= Fax3Decode2D
;
571 if (needsRefLine
) { /* 2d encoding */
572 Fax3CodecState
* esp
= EncoderState(tif
);
574 * 2d encoding requires a scanline
575 * buffer for the ``reference line''; the
576 * scanline against which delta encoding
577 * is referenced. The reference line must
578 * be initialized to be ``white'' (done elsewhere).
580 esp
->refline
= (unsigned char*) _TIFFmalloc(rowbytes
);
581 if (esp
->refline
== NULL
) {
582 TIFFErrorExt(tif
->tif_clientdata
, module
,
583 "No space for Group 3/4 reference line");
586 } else /* 1d encoding */
587 EncoderState(tif
)->refline
= NULL
;
593 * CCITT Group 3 FAX Encoding.
596 #define Fax3FlushBits(tif, sp) { \
597 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
598 (void) TIFFFlushData1(tif); \
599 *(tif)->tif_rawcp++ = (uint8) (sp)->data; \
600 (tif)->tif_rawcc++; \
601 (sp)->data = 0, (sp)->bit = 8; \
603 #define _FlushBits(tif) { \
604 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
605 (void) TIFFFlushData1(tif); \
606 *(tif)->tif_rawcp++ = (uint8) data; \
607 (tif)->tif_rawcc++; \
610 static const int _msbmask
[9] =
611 { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
612 #define _PutBits(tif, bits, length) { \
613 while (length > bit) { \
614 data |= bits >> (length - bit); \
618 assert( length < 9 ); \
619 data |= (bits & _msbmask[length]) << (bit - length); \
626 * Write a variable-length bit-value to
627 * the output stream. Values are
628 * assumed to be at most 16 bits.
631 Fax3PutBits(TIFF
* tif
, unsigned int bits
, unsigned int length
)
633 Fax3CodecState
* sp
= EncoderState(tif
);
634 unsigned int bit
= sp
->bit
;
637 _PutBits(tif
, bits
, length
);
644 * Write a code to the output stream.
646 #define putcode(tif, te) Fax3PutBits(tif, (te)->code, (te)->length)
649 #define DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B")
650 #define DEBUG_PRINT(what,len) { \
652 printf("%08X/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what), len); \
653 for (t = length-1; t >= 0; t--) \
654 putchar(code & (1<<t) ? '1' : '0'); \
660 * Write the sequence of codes that describes
661 * the specified span of zero's or one's. The
662 * appropriate table that holds the make-up and
663 * terminating codes is supplied.
666 putspan(TIFF
* tif
, int32 span
, const tableentry
* tab
)
668 Fax3CodecState
* sp
= EncoderState(tif
);
669 unsigned int bit
= sp
->bit
;
671 unsigned int code
, length
;
673 while (span
>= 2624) {
674 const tableentry
* te
= &tab
[63 + (2560>>6)];
678 DEBUG_PRINT("MakeUp", te
->runlen
);
680 _PutBits(tif
, code
, length
);
684 const tableentry
* te
= &tab
[63 + (span
>>6)];
685 assert(te
->runlen
== 64*(span
>>6));
689 DEBUG_PRINT("MakeUp", te
->runlen
);
691 _PutBits(tif
, code
, length
);
694 code
= tab
[span
].code
;
695 length
= tab
[span
].length
;
697 DEBUG_PRINT(" Term", tab
[span
].runlen
);
699 _PutBits(tif
, code
, length
);
706 * Write an EOL code to the output stream. The zero-fill
707 * logic for byte-aligning encoded scanlines is handled
708 * here. We also handle writing the tag bit for the next
709 * scanline when doing 2d encoding.
712 Fax3PutEOL(TIFF
* tif
)
714 Fax3CodecState
* sp
= EncoderState(tif
);
715 unsigned int bit
= sp
->bit
;
717 unsigned int code
, length
, tparm
;
719 if (sp
->b
.groupoptions
& GROUP3OPT_FILLBITS
) {
721 * Force bit alignment so EOL will terminate on
722 * a byte boundary. That is, force the bit alignment
723 * to 16-12 = 4 before putting out the EOL code.
726 if (align
!= sp
->bit
) {
728 align
= sp
->bit
+ (8 - align
);
730 align
= sp
->bit
- align
;
732 _PutBits(tif
, 0, tparm
);
737 if (is2DEncoding(sp
)) {
738 code
= (code
<<1) | (sp
->tag
== G3_1D
);
741 _PutBits(tif
, code
, length
);
748 * Reset encoding state at the start of a strip.
751 Fax3PreEncode(TIFF
* tif
, uint16 s
)
753 Fax3CodecState
* sp
= EncoderState(tif
);
761 * This is necessary for Group 4; otherwise it isn't
762 * needed because the first scanline of each strip ends
763 * up being copied into the refline.
766 _TIFFmemset(sp
->refline
, 0x00, sp
->b
.rowbytes
);
767 if (is2DEncoding(sp
)) {
768 float res
= tif
->tif_dir
.td_yresolution
;
770 * The CCITT spec says that when doing 2d encoding, you
771 * should only do it on K consecutive scanlines, where K
772 * depends on the resolution of the image being encoded
773 * (2 for <= 200 lpi, 4 for > 200 lpi). Since the directory
774 * code initializes td_yresolution to 0, this code will
775 * select a K of 2 unless the YResolution tag is set
776 * appropriately. (Note also that we fudge a little here
777 * and use 150 lpi to avoid problems with units conversion.)
779 if (tif
->tif_dir
.td_resolutionunit
== RESUNIT_CENTIMETER
)
780 res
*= 2.54f
; /* convert to inches */
781 sp
->maxk
= (res
> 150 ? 4 : 2);
784 sp
->k
= sp
->maxk
= 0;
789 static const unsigned char zeroruns
[256] = {
790 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0f */
791 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */
792 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2f */
793 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3f */
794 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */
795 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5f */
796 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */
797 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7f */
798 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8f */
799 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9f */
800 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 - 0xaf */
801 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 - 0xbf */
802 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 - 0xcf */
803 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 - 0xdf */
804 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 - 0xef */
805 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xf0 - 0xff */
807 static const unsigned char oneruns
[256] = {
808 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 - 0x0f */
809 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */
810 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 - 0x2f */
811 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */
812 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */
813 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */
814 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6f */
815 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7f */
816 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x80 - 0x8f */
817 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x90 - 0x9f */
818 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xa0 - 0xaf */
819 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xb0 - 0xbf */
820 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xc0 - 0xcf */
821 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xd0 - 0xdf */
822 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */
823 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, /* 0xf0 - 0xff */
827 * On certain systems it pays to inline
828 * the routines that find pixel spans.
831 static int32
find0span(unsigned char*, int32
, int32
);
832 static int32
find1span(unsigned char*, int32
, int32
);
833 #pragma inline(find0span,find1span)
837 * Find a span of ones or zeros using the supplied
838 * table. The ``base'' of the bit string is supplied
839 * along with the start+end bit indices.
842 find0span(unsigned char* bp
, int32 bs
, int32 be
)
844 int32 bits
= be
- bs
;
849 * Check partial byte on lhs.
851 if (bits
> 0 && (n
= (bs
& 7)) != 0) {
852 span
= zeroruns
[(*bp
<< n
) & 0xff];
853 if (span
> 8-n
) /* table value too generous */
855 if (span
> bits
) /* constrain span to bit range */
857 if (n
+span
< 8) /* doesn't extend to edge of byte */
863 if (bits
>= (int32
)(2 * 8 * sizeof(long))) {
866 * Align to longword boundary and check longwords.
868 while (!isAligned(bp
, long)) {
870 return (span
+ zeroruns
[*bp
]);
876 while ((bits
>= (int32
)(8 * sizeof(long))) && (0 == *lp
)) {
877 span
+= 8*sizeof (long);
878 bits
-= 8*sizeof (long);
881 bp
= (unsigned char*) lp
;
884 * Scan full bytes for all 0's.
887 if (*bp
!= 0x00) /* end of run */
888 return (span
+ zeroruns
[*bp
]);
894 * Check partial byte on rhs.
898 span
+= (n
> bits
? bits
: n
);
904 find1span(unsigned char* bp
, int32 bs
, int32 be
)
906 int32 bits
= be
- bs
;
911 * Check partial byte on lhs.
913 if (bits
> 0 && (n
= (bs
& 7)) != 0) {
914 span
= oneruns
[(*bp
<< n
) & 0xff];
915 if (span
> 8-n
) /* table value too generous */
917 if (span
> bits
) /* constrain span to bit range */
919 if (n
+span
< 8) /* doesn't extend to edge of byte */
925 if (bits
>= (int32
)(2 * 8 * sizeof(long))) {
928 * Align to longword boundary and check longwords.
930 while (!isAligned(bp
, long)) {
932 return (span
+ oneruns
[*bp
]);
938 while ((bits
>= (int32
)(8 * sizeof(long))) && (~0 == *lp
)) {
939 span
+= 8*sizeof (long);
940 bits
-= 8*sizeof (long);
943 bp
= (unsigned char*) lp
;
946 * Scan full bytes for all 1's.
949 if (*bp
!= 0xff) /* end of run */
950 return (span
+ oneruns
[*bp
]);
956 * Check partial byte on rhs.
960 span
+= (n
> bits
? bits
: n
);
966 * Return the offset of the next bit in the range
967 * [bs..be] that is different from the specified
968 * color. The end, be, is returned if no such bit
971 #define finddiff(_cp, _bs, _be, _color) \
972 (_bs + (_color ? find1span(_cp,_bs,_be) : find0span(_cp,_bs,_be)))
974 * Like finddiff, but also check the starting bit
975 * against the end in case start > end.
977 #define finddiff2(_cp, _bs, _be, _color) \
978 (_bs < _be ? finddiff(_cp,_bs,_be,_color) : _be)
981 * 1d-encode a row of pixels. The encoding is
982 * a sequence of all-white or all-black spans
983 * of pixels encoded with Huffman codes.
986 Fax3Encode1DRow(TIFF
* tif
, unsigned char* bp
, uint32 bits
)
988 Fax3CodecState
* sp
= EncoderState(tif
);
993 span
= find0span(bp
, bs
, bits
); /* white span */
994 putspan(tif
, span
, TIFFFaxWhiteCodes
);
998 span
= find1span(bp
, bs
, bits
); /* black span */
999 putspan(tif
, span
, TIFFFaxBlackCodes
);
1004 if (sp
->b
.mode
& (FAXMODE_BYTEALIGN
|FAXMODE_WORDALIGN
)) {
1005 if (sp
->bit
!= 8) /* byte-align */
1006 Fax3FlushBits(tif
, sp
);
1007 if ((sp
->b
.mode
&FAXMODE_WORDALIGN
) &&
1008 !isAligned(tif
->tif_rawcp
, uint16
))
1009 Fax3FlushBits(tif
, sp
);
1014 static const tableentry horizcode
=
1015 { 3, 0x1, 0 }; /* 001 */
1016 static const tableentry passcode
=
1017 { 4, 0x1, 0 }; /* 0001 */
1018 static const tableentry vcodes
[7] = {
1019 { 7, 0x03, 0 }, /* 0000 011 */
1020 { 6, 0x03, 0 }, /* 0000 11 */
1021 { 3, 0x03, 0 }, /* 011 */
1022 { 1, 0x1, 0 }, /* 1 */
1023 { 3, 0x2, 0 }, /* 010 */
1024 { 6, 0x02, 0 }, /* 0000 10 */
1025 { 7, 0x02, 0 } /* 0000 010 */
1029 * 2d-encode a row of pixels. Consult the CCITT
1030 * documentation for the algorithm.
1033 Fax3Encode2DRow(TIFF
* tif
, unsigned char* bp
, unsigned char* rp
, uint32 bits
)
1035 #define PIXEL(buf,ix) ((((buf)[(ix)>>3]) >> (7-((ix)&7))) & 1)
1037 uint32 a1
= (PIXEL(bp
, 0) != 0 ? 0 : finddiff(bp
, 0, bits
, 0));
1038 uint32 b1
= (PIXEL(rp
, 0) != 0 ? 0 : finddiff(rp
, 0, bits
, 0));
1042 b2
= finddiff2(rp
, b1
, bits
, PIXEL(rp
,b1
));
1044 /* Naive computation triggers -fsanitize=undefined,unsigned-integer-overflow */
1045 /* although it is correct unless the difference between both is < 31 bit */
1046 /* int32 d = b1 - a1; */
1047 int32 d
= (b1
>= a1
&& b1
- a1
<= 3U) ? (int32
)(b1
- a1
):
1048 (b1
< a1
&& a1
- b1
<= 3U) ? -(int32
)(a1
- b1
) : 0x7FFFFFFF;
1049 if (!(-3 <= d
&& d
<= 3)) { /* horizontal mode */
1050 a2
= finddiff2(bp
, a1
, bits
, PIXEL(bp
,a1
));
1051 putcode(tif
, &horizcode
);
1052 if (a0
+a1
== 0 || PIXEL(bp
, a0
) == 0) {
1053 putspan(tif
, a1
-a0
, TIFFFaxWhiteCodes
);
1054 putspan(tif
, a2
-a1
, TIFFFaxBlackCodes
);
1056 putspan(tif
, a1
-a0
, TIFFFaxBlackCodes
);
1057 putspan(tif
, a2
-a1
, TIFFFaxWhiteCodes
);
1060 } else { /* vertical mode */
1061 putcode(tif
, &vcodes
[d
+3]);
1064 } else { /* pass mode */
1065 putcode(tif
, &passcode
);
1070 a1
= finddiff(bp
, a0
, bits
, PIXEL(bp
,a0
));
1071 b1
= finddiff(rp
, a0
, bits
, !PIXEL(bp
,a0
));
1072 b1
= finddiff(rp
, b1
, bits
, PIXEL(bp
,a0
));
1079 * Encode a buffer of pixels.
1082 Fax3Encode(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
1084 static const char module
[] = "Fax3Encode";
1085 Fax3CodecState
* sp
= EncoderState(tif
);
1087 if (cc
% sp
->b
.rowbytes
)
1089 TIFFErrorExt(tif
->tif_clientdata
, module
, "Fractional scanlines cannot be written");
1093 if ((sp
->b
.mode
& FAXMODE_NOEOL
) == 0)
1095 if (is2DEncoding(sp
)) {
1096 if (sp
->tag
== G3_1D
) {
1097 if (!Fax3Encode1DRow(tif
, bp
, sp
->b
.rowpixels
))
1101 if (!Fax3Encode2DRow(tif
, bp
, sp
->refline
,
1110 _TIFFmemcpy(sp
->refline
, bp
, sp
->b
.rowbytes
);
1112 if (!Fax3Encode1DRow(tif
, bp
, sp
->b
.rowpixels
))
1115 bp
+= sp
->b
.rowbytes
;
1116 cc
-= sp
->b
.rowbytes
;
1122 Fax3PostEncode(TIFF
* tif
)
1124 Fax3CodecState
* sp
= EncoderState(tif
);
1127 Fax3FlushBits(tif
, sp
);
1132 Fax3Close(TIFF
* tif
)
1134 if ((Fax3State(tif
)->mode
& FAXMODE_NORTC
) == 0 && tif
->tif_rawcp
) {
1135 Fax3CodecState
* sp
= EncoderState(tif
);
1136 unsigned int code
= EOL
;
1137 unsigned int length
= 12;
1140 if (is2DEncoding(sp
)) {
1141 code
= (code
<<1) | (sp
->tag
== G3_1D
);
1144 for (i
= 0; i
< 6; i
++)
1145 Fax3PutBits(tif
, code
, length
);
1146 Fax3FlushBits(tif
, sp
);
1151 Fax3Cleanup(TIFF
* tif
)
1153 Fax3CodecState
* sp
= DecoderState(tif
);
1157 tif
->tif_tagmethods
.vgetfield
= sp
->b
.vgetparent
;
1158 tif
->tif_tagmethods
.vsetfield
= sp
->b
.vsetparent
;
1159 tif
->tif_tagmethods
.printdir
= sp
->b
.printdir
;
1162 _TIFFfree(sp
->runs
);
1164 _TIFFfree(sp
->refline
);
1166 _TIFFfree(tif
->tif_data
);
1167 tif
->tif_data
= NULL
;
1169 _TIFFSetDefaultCompressionState(tif
);
1172 #define FIELD_BADFAXLINES (FIELD_CODEC+0)
1173 #define FIELD_CLEANFAXDATA (FIELD_CODEC+1)
1174 #define FIELD_BADFAXRUN (FIELD_CODEC+2)
1176 #define FIELD_OPTIONS (FIELD_CODEC+7)
1178 static const TIFFField faxFields
[] = {
1179 { TIFFTAG_FAXMODE
, 0, 0, TIFF_ANY
, 0, TIFF_SETGET_INT
, TIFF_SETGET_UNDEFINED
, FIELD_PSEUDO
, FALSE
, FALSE
, "FaxMode", NULL
},
1180 { TIFFTAG_FAXFILLFUNC
, 0, 0, TIFF_ANY
, 0, TIFF_SETGET_OTHER
, TIFF_SETGET_UNDEFINED
, FIELD_PSEUDO
, FALSE
, FALSE
, "FaxFillFunc", NULL
},
1181 { TIFFTAG_BADFAXLINES
, 1, 1, TIFF_LONG
, 0, TIFF_SETGET_UINT32
, TIFF_SETGET_UINT32
, FIELD_BADFAXLINES
, TRUE
, FALSE
, "BadFaxLines", NULL
},
1182 { TIFFTAG_CLEANFAXDATA
, 1, 1, TIFF_SHORT
, 0, TIFF_SETGET_UINT16
, TIFF_SETGET_UINT16
, FIELD_CLEANFAXDATA
, TRUE
, FALSE
, "CleanFaxData", NULL
},
1183 { TIFFTAG_CONSECUTIVEBADFAXLINES
, 1, 1, TIFF_LONG
, 0, TIFF_SETGET_UINT32
, TIFF_SETGET_UINT32
, FIELD_BADFAXRUN
, TRUE
, FALSE
, "ConsecutiveBadFaxLines", NULL
}};
1184 static const TIFFField fax3Fields
[] = {
1185 { TIFFTAG_GROUP3OPTIONS
, 1, 1, TIFF_LONG
, 0, TIFF_SETGET_UINT32
, TIFF_SETGET_UINT32
, FIELD_OPTIONS
, FALSE
, FALSE
, "Group3Options", NULL
},
1187 static const TIFFField fax4Fields
[] = {
1188 { TIFFTAG_GROUP4OPTIONS
, 1, 1, TIFF_LONG
, 0, TIFF_SETGET_UINT32
, TIFF_SETGET_UINT32
, FIELD_OPTIONS
, FALSE
, FALSE
, "Group4Options", NULL
},
1192 Fax3VSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
1194 Fax3BaseState
* sp
= Fax3State(tif
);
1195 const TIFFField
* fip
;
1198 assert(sp
->vsetparent
!= 0);
1201 case TIFFTAG_FAXMODE
:
1202 sp
->mode
= (int) va_arg(ap
, int);
1203 return 1; /* NB: pseudo tag */
1204 case TIFFTAG_FAXFILLFUNC
:
1205 DecoderState(tif
)->fill
= va_arg(ap
, TIFFFaxFillFunc
);
1206 return 1; /* NB: pseudo tag */
1207 case TIFFTAG_GROUP3OPTIONS
:
1208 /* XXX: avoid reading options if compression mismatches. */
1209 if (tif
->tif_dir
.td_compression
== COMPRESSION_CCITTFAX3
)
1210 sp
->groupoptions
= (uint32
) va_arg(ap
, uint32
);
1212 case TIFFTAG_GROUP4OPTIONS
:
1213 /* XXX: avoid reading options if compression mismatches. */
1214 if (tif
->tif_dir
.td_compression
== COMPRESSION_CCITTFAX4
)
1215 sp
->groupoptions
= (uint32
) va_arg(ap
, uint32
);
1217 case TIFFTAG_BADFAXLINES
:
1218 sp
->badfaxlines
= (uint32
) va_arg(ap
, uint32
);
1220 case TIFFTAG_CLEANFAXDATA
:
1221 sp
->cleanfaxdata
= (uint16
) va_arg(ap
, uint16_vap
);
1223 case TIFFTAG_CONSECUTIVEBADFAXLINES
:
1224 sp
->badfaxrun
= (uint32
) va_arg(ap
, uint32
);
1227 return (*sp
->vsetparent
)(tif
, tag
, ap
);
1230 if ((fip
= TIFFFieldWithTag(tif
, tag
)) != NULL
)
1231 TIFFSetFieldBit(tif
, fip
->field_bit
);
1235 tif
->tif_flags
|= TIFF_DIRTYDIRECT
;
1240 Fax3VGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
1242 Fax3BaseState
* sp
= Fax3State(tif
);
1247 case TIFFTAG_FAXMODE
:
1248 *va_arg(ap
, int*) = sp
->mode
;
1250 case TIFFTAG_FAXFILLFUNC
:
1251 *va_arg(ap
, TIFFFaxFillFunc
*) = DecoderState(tif
)->fill
;
1253 case TIFFTAG_GROUP3OPTIONS
:
1254 case TIFFTAG_GROUP4OPTIONS
:
1255 *va_arg(ap
, uint32
*) = sp
->groupoptions
;
1257 case TIFFTAG_BADFAXLINES
:
1258 *va_arg(ap
, uint32
*) = sp
->badfaxlines
;
1260 case TIFFTAG_CLEANFAXDATA
:
1261 *va_arg(ap
, uint16
*) = sp
->cleanfaxdata
;
1263 case TIFFTAG_CONSECUTIVEBADFAXLINES
:
1264 *va_arg(ap
, uint32
*) = sp
->badfaxrun
;
1267 return (*sp
->vgetparent
)(tif
, tag
, ap
);
1273 Fax3PrintDir(TIFF
* tif
, FILE* fd
, long flags
)
1275 Fax3BaseState
* sp
= Fax3State(tif
);
1280 if (TIFFFieldSet(tif
,FIELD_OPTIONS
)) {
1281 const char* sep
= " ";
1282 if (tif
->tif_dir
.td_compression
== COMPRESSION_CCITTFAX4
) {
1283 fprintf(fd
, " Group 4 Options:");
1284 if (sp
->groupoptions
& GROUP4OPT_UNCOMPRESSED
)
1285 fprintf(fd
, "%suncompressed data", sep
);
1288 fprintf(fd
, " Group 3 Options:");
1289 if (sp
->groupoptions
& GROUP3OPT_2DENCODING
) {
1290 fprintf(fd
, "%s2-d encoding", sep
);
1293 if (sp
->groupoptions
& GROUP3OPT_FILLBITS
) {
1294 fprintf(fd
, "%sEOL padding", sep
);
1297 if (sp
->groupoptions
& GROUP3OPT_UNCOMPRESSED
)
1298 fprintf(fd
, "%suncompressed data", sep
);
1300 fprintf(fd
, " (%lu = 0x%lx)\n",
1301 (unsigned long) sp
->groupoptions
,
1302 (unsigned long) sp
->groupoptions
);
1304 if (TIFFFieldSet(tif
,FIELD_CLEANFAXDATA
)) {
1305 fprintf(fd
, " Fax Data:");
1306 switch (sp
->cleanfaxdata
) {
1307 case CLEANFAXDATA_CLEAN
:
1308 fprintf(fd
, " clean");
1310 case CLEANFAXDATA_REGENERATED
:
1311 fprintf(fd
, " receiver regenerated");
1313 case CLEANFAXDATA_UNCLEAN
:
1314 fprintf(fd
, " uncorrected errors");
1317 fprintf(fd
, " (%u = 0x%x)\n",
1318 sp
->cleanfaxdata
, sp
->cleanfaxdata
);
1320 if (TIFFFieldSet(tif
,FIELD_BADFAXLINES
))
1321 fprintf(fd
, " Bad Fax Lines: %lu\n",
1322 (unsigned long) sp
->badfaxlines
);
1323 if (TIFFFieldSet(tif
,FIELD_BADFAXRUN
))
1324 fprintf(fd
, " Consecutive Bad Fax Lines: %lu\n",
1325 (unsigned long) sp
->badfaxrun
);
1327 (*sp
->printdir
)(tif
, fd
, flags
);
1331 InitCCITTFax3(TIFF
* tif
)
1333 static const char module
[] = "InitCCITTFax3";
1337 * Merge codec-specific tag information.
1339 if (!_TIFFMergeFields(tif
, faxFields
, TIFFArrayCount(faxFields
))) {
1340 TIFFErrorExt(tif
->tif_clientdata
, "InitCCITTFax3",
1341 "Merging common CCITT Fax codec-specific tags failed");
1346 * Allocate state block so tag methods have storage to record values.
1348 tif
->tif_data
= (uint8
*)
1349 _TIFFmalloc(sizeof (Fax3CodecState
));
1351 if (tif
->tif_data
== NULL
) {
1352 TIFFErrorExt(tif
->tif_clientdata
, module
,
1353 "No space for state block");
1356 _TIFFmemset(tif
->tif_data
, 0, sizeof (Fax3CodecState
));
1358 sp
= Fax3State(tif
);
1359 sp
->rw_mode
= tif
->tif_mode
;
1362 * Override parent get/set field methods.
1364 sp
->vgetparent
= tif
->tif_tagmethods
.vgetfield
;
1365 tif
->tif_tagmethods
.vgetfield
= Fax3VGetField
; /* hook for codec tags */
1366 sp
->vsetparent
= tif
->tif_tagmethods
.vsetfield
;
1367 tif
->tif_tagmethods
.vsetfield
= Fax3VSetField
; /* hook for codec tags */
1368 sp
->printdir
= tif
->tif_tagmethods
.printdir
;
1369 tif
->tif_tagmethods
.printdir
= Fax3PrintDir
; /* hook for codec tags */
1370 sp
->groupoptions
= 0;
1372 if (sp
->rw_mode
== O_RDONLY
) /* FIXME: improve for in place update */
1373 tif
->tif_flags
|= TIFF_NOBITREV
; /* decoder does bit reversal */
1374 DecoderState(tif
)->runs
= NULL
;
1375 TIFFSetField(tif
, TIFFTAG_FAXFILLFUNC
, _TIFFFax3fillruns
);
1376 EncoderState(tif
)->refline
= NULL
;
1379 * Install codec methods.
1381 tif
->tif_fixuptags
= Fax3FixupTags
;
1382 tif
->tif_setupdecode
= Fax3SetupState
;
1383 tif
->tif_predecode
= Fax3PreDecode
;
1384 tif
->tif_decoderow
= Fax3Decode1D
;
1385 tif
->tif_decodestrip
= Fax3Decode1D
;
1386 tif
->tif_decodetile
= Fax3Decode1D
;
1387 tif
->tif_setupencode
= Fax3SetupState
;
1388 tif
->tif_preencode
= Fax3PreEncode
;
1389 tif
->tif_postencode
= Fax3PostEncode
;
1390 tif
->tif_encoderow
= Fax3Encode
;
1391 tif
->tif_encodestrip
= Fax3Encode
;
1392 tif
->tif_encodetile
= Fax3Encode
;
1393 tif
->tif_close
= Fax3Close
;
1394 tif
->tif_cleanup
= Fax3Cleanup
;
1400 TIFFInitCCITTFax3(TIFF
* tif
, int scheme
)
1403 if (InitCCITTFax3(tif
)) {
1405 * Merge codec-specific tag information.
1407 if (!_TIFFMergeFields(tif
, fax3Fields
,
1408 TIFFArrayCount(fax3Fields
))) {
1409 TIFFErrorExt(tif
->tif_clientdata
, "TIFFInitCCITTFax3",
1410 "Merging CCITT Fax 3 codec-specific tags failed");
1415 * The default format is Class/F-style w/o RTC.
1417 return TIFFSetField(tif
, TIFFTAG_FAXMODE
, FAXMODE_CLASSF
);
1423 * CCITT Group 4 (T.6) Facsimile-compatible
1424 * Compression Scheme Support.
1427 #define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }
1429 * Decode the requested amount of G4-encoded data.
1432 Fax4Decode(TIFF
* tif
, uint8
* buf
, tmsize_t occ
, uint16 s
)
1434 DECLARE_STATE_2D(tif
, sp
, "Fax4Decode");
1436 if (occ
% sp
->b
.rowbytes
)
1438 TIFFErrorExt(tif
->tif_clientdata
, module
, "Fractional scanlines cannot be read");
1441 CACHE_STATE(tif
, sp
);
1445 pa
= thisrun
= sp
->curruns
;
1449 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc
, BitsAvail
);
1450 printf("-------------------- %d\n", tif
->tif_row
);
1456 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1457 SETVALUE(0); /* imaginary change for reference */
1458 SWAP(uint32
*, sp
->curruns
, sp
->refruns
);
1459 buf
+= sp
->b
.rowbytes
;
1460 occ
-= sp
->b
.rowbytes
;
1464 NeedBits16( 13, BADG4
);
1467 if( GetBits(13) != 0x1001 )
1468 fputs( "Bad EOFB\n", stderr
);
1471 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1472 UNCACHE_STATE(tif
, sp
);
1473 return ( sp
->line
? 1 : -1); /* don't error on badly-terminated strips */
1475 UNCACHE_STATE(tif
, sp
);
1481 * Encode the requested amount of data.
1484 Fax4Encode(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
1486 static const char module
[] = "Fax4Encode";
1487 Fax3CodecState
*sp
= EncoderState(tif
);
1489 if (cc
% sp
->b
.rowbytes
)
1491 TIFFErrorExt(tif
->tif_clientdata
, module
, "Fractional scanlines cannot be written");
1495 if (!Fax3Encode2DRow(tif
, bp
, sp
->refline
, sp
->b
.rowpixels
))
1497 _TIFFmemcpy(sp
->refline
, bp
, sp
->b
.rowbytes
);
1498 bp
+= sp
->b
.rowbytes
;
1499 cc
-= sp
->b
.rowbytes
;
1505 Fax4PostEncode(TIFF
* tif
)
1507 Fax3CodecState
*sp
= EncoderState(tif
);
1509 /* terminate strip w/ EOFB */
1510 Fax3PutBits(tif
, EOL
, 12);
1511 Fax3PutBits(tif
, EOL
, 12);
1513 Fax3FlushBits(tif
, sp
);
1518 TIFFInitCCITTFax4(TIFF
* tif
, int scheme
)
1521 if (InitCCITTFax3(tif
)) { /* reuse G3 support */
1523 * Merge codec-specific tag information.
1525 if (!_TIFFMergeFields(tif
, fax4Fields
,
1526 TIFFArrayCount(fax4Fields
))) {
1527 TIFFErrorExt(tif
->tif_clientdata
, "TIFFInitCCITTFax4",
1528 "Merging CCITT Fax 4 codec-specific tags failed");
1532 tif
->tif_decoderow
= Fax4Decode
;
1533 tif
->tif_decodestrip
= Fax4Decode
;
1534 tif
->tif_decodetile
= Fax4Decode
;
1535 tif
->tif_encoderow
= Fax4Encode
;
1536 tif
->tif_encodestrip
= Fax4Encode
;
1537 tif
->tif_encodetile
= Fax4Encode
;
1538 tif
->tif_postencode
= Fax4PostEncode
;
1540 * Suppress RTC at the end of each strip.
1542 return TIFFSetField(tif
, TIFFTAG_FAXMODE
, FAXMODE_NORTC
);
1548 * CCITT Group 3 1-D Modified Huffman RLE Compression Support.
1549 * (Compression algorithms 2 and 32771)
1553 * Decode the requested amount of RLE-encoded data.
1556 Fax3DecodeRLE(TIFF
* tif
, uint8
* buf
, tmsize_t occ
, uint16 s
)
1558 DECLARE_STATE(tif
, sp
, "Fax3DecodeRLE");
1559 int mode
= sp
->b
.mode
;
1561 if (occ
% sp
->b
.rowbytes
)
1563 TIFFErrorExt(tif
->tif_clientdata
, module
, "Fractional scanlines cannot be read");
1566 CACHE_STATE(tif
, sp
);
1567 thisrun
= sp
->curruns
;
1573 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc
, BitsAvail
);
1574 printf("-------------------- %d\n", tif
->tif_row
);
1578 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1580 * Cleanup at the end of the row.
1582 if (mode
& FAXMODE_BYTEALIGN
) {
1583 int n
= BitsAvail
- (BitsAvail
&~ 7);
1585 } else if (mode
& FAXMODE_WORDALIGN
) {
1586 int n
= BitsAvail
- (BitsAvail
&~ 15);
1588 if (BitsAvail
== 0 && !isAligned(cp
, uint16
))
1591 buf
+= sp
->b
.rowbytes
;
1592 occ
-= sp
->b
.rowbytes
;
1595 EOFRLE
: /* premature EOF */
1596 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1597 UNCACHE_STATE(tif
, sp
);
1600 UNCACHE_STATE(tif
, sp
);
1605 TIFFInitCCITTRLE(TIFF
* tif
, int scheme
)
1608 if (InitCCITTFax3(tif
)) { /* reuse G3 support */
1609 tif
->tif_decoderow
= Fax3DecodeRLE
;
1610 tif
->tif_decodestrip
= Fax3DecodeRLE
;
1611 tif
->tif_decodetile
= Fax3DecodeRLE
;
1613 * Suppress RTC+EOLs when encoding and byte-align data.
1615 return TIFFSetField(tif
, TIFFTAG_FAXMODE
,
1616 FAXMODE_NORTC
|FAXMODE_NOEOL
|FAXMODE_BYTEALIGN
);
1622 TIFFInitCCITTRLEW(TIFF
* tif
, int scheme
)
1625 if (InitCCITTFax3(tif
)) { /* reuse G3 support */
1626 tif
->tif_decoderow
= Fax3DecodeRLE
;
1627 tif
->tif_decodestrip
= Fax3DecodeRLE
;
1628 tif
->tif_decodetile
= Fax3DecodeRLE
;
1630 * Suppress RTC+EOLs when encoding and word-align data.
1632 return TIFFSetField(tif
, TIFFTAG_FAXMODE
,
1633 FAXMODE_NORTC
|FAXMODE_NOEOL
|FAXMODE_WORDALIGN
);
1637 #endif /* CCITT_SUPPORT */
1639 /* vim: set ts=8 sts=8 sw=8 noet: */