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.
48 * Compression+decompression state blocks are
49 * derived from this ``base state'' block.
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 */
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 */
67 #define Fax3State(tif) ((Fax3BaseState*) (tif)->tif_data)
69 typedef enum { G3_1D
, G3_2D
} Ttag
;
73 /* Decoder state info */
74 const u_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 u_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) ((((u_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 u_char* cp; /* next byte of input data */ \
113 u_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 u_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 a0
)
182 TIFFError(module, "%s: Bad code word at scanline %d (x %lu)",
183 tif
->tif_name
, tif
->tif_row
, (u_long
) a0
);
185 #define unexpected(table, a0) Fax3Unexpected(module, tif, a0)
188 Fax3Extension(const char* module, TIFF
* tif
, uint32 a0
)
191 "%s: Uncompressed data (not supported) at scanline %d (x %lu)",
192 tif
->tif_name
, tif
->tif_row
, (u_long
) a0
);
194 #define extension(a0) Fax3Extension(module, tif, a0)
197 Fax3BadLength(const char* module, TIFF
* tif
, uint32 a0
, uint32 lastx
)
199 TIFFWarning(module, "%s: %s at scanline %d (got %lu, expected %lu)",
201 a0
< lastx
? "Premature EOL" : "Line length mismatch",
202 tif
->tif_row
, (u_long
) a0
, (u_long
) lastx
);
204 #define badlength(a0,lastx) Fax3BadLength(module, tif, a0, lastx)
207 Fax3PrematureEOF(const char* module, TIFF
* tif
, uint32 a0
)
209 TIFFWarning(module, "%s: Premature EOF at scanline %d (x %lu)",
210 tif
->tif_name
, tif
->tif_row
, (u_long
) a0
);
212 #define prematureEOF(a0) Fax3PrematureEOF(module, tif, a0)
217 * Decode the requested amount of G3 1D-encoded data.
220 Fax3Decode1D(TIFF
* tif
, tidata_t buf
, tsize_t occ
, tsample_t s
)
222 DECLARE_STATE(tif
, sp
, "Fax3Decode1D");
225 CACHE_STATE(tif
, sp
);
226 thisrun
= sp
->curruns
;
227 while ((long)occ
> 0) {
232 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc
, BitsAvail
);
233 printf("-------------------- %d\n", tif
->tif_row
);
238 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
239 buf
+= sp
->b
.rowbytes
;
240 occ
-= sp
->b
.rowbytes
;
242 EOF1D
: /* premature EOF */
244 EOF1Da
: /* premature EOF */
245 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
246 UNCACHE_STATE(tif
, sp
);
249 UNCACHE_STATE(tif
, sp
);
253 #define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }
255 * Decode the requested amount of G3 2D-encoded data.
258 Fax3Decode2D(TIFF
* tif
, tidata_t buf
, tsize_t occ
, tsample_t s
)
260 DECLARE_STATE_2D(tif
, sp
, "Fax3Decode2D");
261 int is1D
; /* current line is 1d/2d-encoded */
264 CACHE_STATE(tif
, sp
);
265 while ((long)occ
> 0) {
268 pa
= thisrun
= sp
->curruns
;
270 printf("\nBitAcc=%08X, BitsAvail = %d EOLcnt = %d",
271 BitAcc
, BitsAvail
, EOLcnt
);
275 is1D
= GetBits(1); /* 1D/2D-encoding tag bit */
278 printf(" %s\n-------------------- %d\n",
279 is1D
? "1D" : "2D", tif
->tif_row
);
288 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
289 SETVAL(0); /* imaginary change for reference */
290 SWAP(uint32
*, sp
->curruns
, sp
->refruns
);
291 buf
+= sp
->b
.rowbytes
;
292 occ
-= sp
->b
.rowbytes
;
294 EOF2D
: /* premature EOF */
296 EOF2Da
: /* premature EOF */
297 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
298 UNCACHE_STATE(tif
, sp
);
301 UNCACHE_STATE(tif
, sp
);
307 * The ZERO & FILL macros must handle spans < 2*sizeof(long) bytes.
308 * For machines with 64-bit longs this is <16 bytes; otherwise
309 * this is <8 bytes. We optimize the code here to reflect the
310 * machine characteristics.
312 #if defined(__alpha) || _MIPS_SZLONG == 64 || defined(__LP64__) || defined(__arch64__)
313 #define FILL(n, cp) \
315 case 15:(cp)[14] = 0xff; case 14:(cp)[13] = 0xff; case 13: (cp)[12] = 0xff;\
316 case 12:(cp)[11] = 0xff; case 11:(cp)[10] = 0xff; case 10: (cp)[9] = 0xff;\
317 case 9: (cp)[8] = 0xff; case 8: (cp)[7] = 0xff; case 7: (cp)[6] = 0xff;\
318 case 6: (cp)[5] = 0xff; case 5: (cp)[4] = 0xff; case 4: (cp)[3] = 0xff;\
319 case 3: (cp)[2] = 0xff; case 2: (cp)[1] = 0xff; \
320 case 1: (cp)[0] = 0xff; (cp) += (n); case 0: ; \
322 #define ZERO(n, cp) \
324 case 15:(cp)[14] = 0; case 14:(cp)[13] = 0; case 13: (cp)[12] = 0; \
325 case 12:(cp)[11] = 0; case 11:(cp)[10] = 0; case 10: (cp)[9] = 0; \
326 case 9: (cp)[8] = 0; case 8: (cp)[7] = 0; case 7: (cp)[6] = 0; \
327 case 6: (cp)[5] = 0; case 5: (cp)[4] = 0; case 4: (cp)[3] = 0; \
328 case 3: (cp)[2] = 0; case 2: (cp)[1] = 0; \
329 case 1: (cp)[0] = 0; (cp) += (n); case 0: ; \
332 #define FILL(n, cp) \
334 case 7: (cp)[6] = 0xff; case 6: (cp)[5] = 0xff; case 5: (cp)[4] = 0xff; \
335 case 4: (cp)[3] = 0xff; case 3: (cp)[2] = 0xff; case 2: (cp)[1] = 0xff; \
336 case 1: (cp)[0] = 0xff; (cp) += (n); case 0: ; \
338 #define ZERO(n, cp) \
340 case 7: (cp)[6] = 0; case 6: (cp)[5] = 0; case 5: (cp)[4] = 0; \
341 case 4: (cp)[3] = 0; case 3: (cp)[2] = 0; case 2: (cp)[1] = 0; \
342 case 1: (cp)[0] = 0; (cp) += (n); case 0: ; \
347 * Bit-fill a row according to the white/black
348 * runs generated during G3/G4 decoding.
351 _TIFFFax3fillruns(u_char
* buf
, uint32
* runs
, uint32
* erun
, uint32 lastx
)
353 static const unsigned char _fillmasks
[] =
354 { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
363 for (; runs
< erun
; runs
+= 2) {
365 if (x
+run
> lastx
|| run
> lastx
)
366 run
= runs
[0] = (uint32
) (lastx
- x
);
371 if (bx
) { /* align to byte boundary */
372 *cp
++ &= 0xff << (8-bx
);
375 if( (n
= run
>> 3) != 0 ) { /* multiple bytes to fill */
376 if ((n
/sizeof (long)) > 1) {
378 * Align to longword boundary and fill.
380 for (; n
&& !isAligned(cp
, long); n
--)
383 nw
= (int32
)(n
/ sizeof (long));
384 n
-= nw
* sizeof (long);
394 cp
[0] &= 0xff >> run
;
396 cp
[0] &= ~(_fillmasks
[run
]>>bx
);
400 if (x
+run
> lastx
|| run
> lastx
)
401 run
= runs
[1] = lastx
- x
;
406 if (bx
) { /* align to byte boundary */
410 if( (n
= run
>>3) != 0 ) { /* multiple bytes to fill */
411 if ((n
/sizeof (long)) > 1) {
413 * Align to longword boundary and fill.
415 for (; n
&& !isAligned(cp
, long); n
--)
418 nw
= (int32
)(n
/ sizeof (long));
419 n
-= nw
* sizeof (long);
429 cp
[0] |= 0xff00 >> run
;
431 cp
[0] |= _fillmasks
[run
]>>bx
;
441 CheckMalloc(TIFF
* tif
, size_t nmemb
, size_t elem_size
, const char* what
)
444 tsize_t bytes
= nmemb
* elem_size
;
446 if (elem_size
&& bytes
/ elem_size
== nmemb
)
447 cp
= (char*) _TIFFmalloc(bytes
);
450 TIFFError(tif
->tif_name
, "No space %s", what
);
456 * Setup G3/G4-related compression/decompression state
457 * before data is processed. This routine is called once
458 * per image -- it sets up different state based on whether
459 * or not decoding or encoding is being done and whether
460 * 1D- or 2D-encoded data is involved.
463 Fax3SetupState(TIFF
* tif
)
465 TIFFDirectory
* td
= &tif
->tif_dir
;
466 Fax3BaseState
* sp
= Fax3State(tif
);
467 long rowbytes
, rowpixels
;
469 Fax3CodecState
* dsp
= DecoderState(tif
);
472 if (td
->td_bitspersample
!= 1) {
473 TIFFError(tif
->tif_name
,
474 "Bits/sample must be 1 for Group 3/4 encoding/decoding");
478 * Calculate the scanline/tile widths.
481 rowbytes
= TIFFTileRowSize(tif
);
482 rowpixels
= td
->td_tilewidth
;
484 rowbytes
= TIFFScanlineSize(tif
);
485 rowpixels
= td
->td_imagewidth
;
487 sp
->rowbytes
= (uint32
) rowbytes
;
488 sp
->rowpixels
= (uint32
) rowpixels
;
490 * Allocate any additional space required for decoding/encoding.
493 (sp
->groupoptions
& GROUP3OPT_2DENCODING
) ||
494 td
->td_compression
== COMPRESSION_CCITTFAX4
497 nruns
= needsRefLine
? 2*TIFFroundup(rowpixels
,32) : rowpixels
;
499 dsp
->runs
= (uint32
*) CheckMalloc(tif
, 2*nruns
+3, sizeof (uint32
),
500 "for Group 3/4 run arrays");
501 if (dsp
->runs
== NULL
)
503 dsp
->curruns
= dsp
->runs
;
505 dsp
->refruns
= dsp
->runs
+ (nruns
>>1);
508 if (is2DEncoding(dsp
)) { /* NB: default is 1D routine */
509 tif
->tif_decoderow
= Fax3Decode2D
;
510 tif
->tif_decodestrip
= Fax3Decode2D
;
511 tif
->tif_decodetile
= Fax3Decode2D
;
514 if (needsRefLine
) { /* 2d encoding */
515 Fax3CodecState
* esp
= EncoderState(tif
);
517 * 2d encoding requires a scanline
518 * buffer for the ``reference line''; the
519 * scanline against which delta encoding
520 * is referenced. The reference line must
521 * be initialized to be ``white'' (done elsewhere).
523 esp
->refline
= (u_char
*) _TIFFmalloc(rowbytes
);
524 if (esp
->refline
== NULL
) {
525 TIFFError("Fax3SetupState",
526 "%s: No space for Group 3/4 reference line",
530 } else /* 1d encoding */
531 EncoderState(tif
)->refline
= NULL
;
536 * CCITT Group 3 FAX Encoding.
539 #define Fax3FlushBits(tif, sp) { \
540 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
541 (void) TIFFFlushData1(tif); \
542 *(tif)->tif_rawcp++ = (tidataval_t) (sp)->data; \
543 (tif)->tif_rawcc++; \
544 (sp)->data = 0, (sp)->bit = 8; \
546 #define _FlushBits(tif) { \
547 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
548 (void) TIFFFlushData1(tif); \
549 *(tif)->tif_rawcp++ = (tidataval_t) data; \
550 (tif)->tif_rawcc++; \
553 static const int _msbmask
[9] =
554 { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
555 #define _PutBits(tif, bits, length) { \
556 while (length > bit) { \
557 data |= bits >> (length - bit); \
561 data |= (bits & _msbmask[length]) << (bit - length); \
568 * Write a variable-length bit-value to
569 * the output stream. Values are
570 * assumed to be at most 16 bits.
573 Fax3PutBits(TIFF
* tif
, u_int bits
, u_int length
)
575 Fax3CodecState
* sp
= EncoderState(tif
);
579 _PutBits(tif
, bits
, length
);
586 * Write a code to the output stream.
588 #define putcode(tif, te) Fax3PutBits(tif, (te)->code, (te)->length)
591 #define DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B")
592 #define DEBUG_PRINT(what,len) { \
594 printf("%08X/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what), len); \
595 for (t = length-1; t >= 0; t--) \
596 putchar(code & (1<<t) ? '1' : '0'); \
602 * Write the sequence of codes that describes
603 * the specified span of zero's or one's. The
604 * appropriate table that holds the make-up and
605 * terminating codes is supplied.
608 putspan(TIFF
* tif
, int32 span
, const tableentry
* tab
)
610 Fax3CodecState
* sp
= EncoderState(tif
);
615 while (span
>= 2624) {
616 const tableentry
* te
= &tab
[63 + (2560>>6)];
617 code
= te
->code
, length
= te
->length
;
619 DEBUG_PRINT("MakeUp", te
->runlen
);
621 _PutBits(tif
, code
, length
);
625 const tableentry
* te
= &tab
[63 + (span
>>6)];
626 assert(te
->runlen
== 64*(span
>>6));
627 code
= te
->code
, length
= te
->length
;
629 DEBUG_PRINT("MakeUp", te
->runlen
);
631 _PutBits(tif
, code
, length
);
634 code
= tab
[span
].code
, length
= tab
[span
].length
;
636 DEBUG_PRINT(" Term", tab
[span
].runlen
);
638 _PutBits(tif
, code
, length
);
645 * Write an EOL code to the output stream. The zero-fill
646 * logic for byte-aligning encoded scanlines is handled
647 * here. We also handle writing the tag bit for the next
648 * scanline when doing 2d encoding.
651 Fax3PutEOL(TIFF
* tif
)
653 Fax3CodecState
* sp
= EncoderState(tif
);
656 u_int code
, length
, tparm
;
658 if (sp
->b
.groupoptions
& GROUP3OPT_FILLBITS
) {
660 * Force bit alignment so EOL will terminate on
661 * a byte boundary. That is, force the bit alignment
662 * to 16-12 = 4 before putting out the EOL code.
665 if (align
!= sp
->bit
) {
667 align
= sp
->bit
+ (8 - align
);
669 align
= sp
->bit
- align
;
672 _PutBits(tif
, 0, tparm
);
675 code
= EOL
, length
= 12;
676 if (is2DEncoding(sp
))
677 code
= (code
<<1) | (sp
->tag
== G3_1D
), length
++;
678 _PutBits(tif
, code
, length
);
685 * Reset encoding state at the start of a strip.
688 Fax3PreEncode(TIFF
* tif
, tsample_t s
)
690 Fax3CodecState
* sp
= EncoderState(tif
);
698 * This is necessary for Group 4; otherwise it isn't
699 * needed because the first scanline of each strip ends
700 * up being copied into the refline.
703 _TIFFmemset(sp
->refline
, 0x00, sp
->b
.rowbytes
);
704 if (is2DEncoding(sp
)) {
705 float res
= tif
->tif_dir
.td_yresolution
;
707 * The CCITT spec says that when doing 2d encoding, you
708 * should only do it on K consecutive scanlines, where K
709 * depends on the resolution of the image being encoded
710 * (2 for <= 200 lpi, 4 for > 200 lpi). Since the directory
711 * code initializes td_yresolution to 0, this code will
712 * select a K of 2 unless the YResolution tag is set
713 * appropriately. (Note also that we fudge a little here
714 * and use 150 lpi to avoid problems with units conversion.)
716 if (tif
->tif_dir
.td_resolutionunit
== RESUNIT_CENTIMETER
)
717 res
*= 2.54f
; /* convert to inches */
718 sp
->maxk
= (res
> 150 ? 4 : 2);
721 sp
->k
= sp
->maxk
= 0;
725 static const u_char zeroruns
[256] = {
726 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0f */
727 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */
728 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2f */
729 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3f */
730 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */
731 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5f */
732 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */
733 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7f */
734 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8f */
735 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9f */
736 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 - 0xaf */
737 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 - 0xbf */
738 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 - 0xcf */
739 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 - 0xdf */
740 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 - 0xef */
741 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xf0 - 0xff */
743 static const u_char oneruns
[256] = {
744 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 - 0x0f */
745 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */
746 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 - 0x2f */
747 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */
748 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */
749 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */
750 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6f */
751 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7f */
752 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x80 - 0x8f */
753 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x90 - 0x9f */
754 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xa0 - 0xaf */
755 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xb0 - 0xbf */
756 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xc0 - 0xcf */
757 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xd0 - 0xdf */
758 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */
759 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, /* 0xf0 - 0xff */
763 * On certain systems it pays to inline
764 * the routines that find pixel spans.
767 static int32
find0span(u_char
*, int32
, int32
);
768 static int32
find1span(u_char
*, int32
, int32
);
769 #pragma inline(find0span,find1span)
773 * Find a span of ones or zeros using the supplied
774 * table. The ``base'' of the bit string is supplied
775 * along with the start+end bit indices.
778 find0span(u_char
* bp
, int32 bs
, int32 be
)
780 int32 bits
= be
- bs
;
785 * Check partial byte on lhs.
787 if (bits
> 0 && (n
= (bs
& 7))) {
788 span
= zeroruns
[(*bp
<< n
) & 0xff];
789 if (span
> 8-n
) /* table value too generous */
791 if (span
> bits
) /* constrain span to bit range */
793 if (n
+span
< 8) /* doesn't extend to edge of byte */
799 if (bits
>= 2*8*sizeof (long)) {
802 * Align to longword boundary and check longwords.
804 while (!isAligned(bp
, long)) {
806 return (span
+ zeroruns
[*bp
]);
807 span
+= 8, bits
-= 8;
811 while (bits
>= 8*sizeof (long) && *lp
== 0) {
812 span
+= 8*sizeof (long), bits
-= 8*sizeof (long);
818 * Scan full bytes for all 0's.
821 if (*bp
!= 0x00) /* end of run */
822 return (span
+ zeroruns
[*bp
]);
823 span
+= 8, bits
-= 8;
827 * Check partial byte on rhs.
831 span
+= (n
> bits
? bits
: n
);
837 find1span(u_char
* bp
, int32 bs
, int32 be
)
839 int32 bits
= be
- bs
;
844 * Check partial byte on lhs.
846 if (bits
> 0 && (n
= (bs
& 7))) {
847 span
= oneruns
[(*bp
<< n
) & 0xff];
848 if (span
> 8-n
) /* table value too generous */
850 if (span
> bits
) /* constrain span to bit range */
852 if (n
+span
< 8) /* doesn't extend to edge of byte */
858 if (bits
>= 2*8*sizeof (long)) {
861 * Align to longword boundary and check longwords.
863 while (!isAligned(bp
, long)) {
865 return (span
+ oneruns
[*bp
]);
866 span
+= 8, bits
-= 8;
870 while (bits
>= 8*sizeof (long) && *lp
== ~0) {
871 span
+= 8*sizeof (long), bits
-= 8*sizeof (long);
877 * Scan full bytes for all 1's.
880 if (*bp
!= 0xff) /* end of run */
881 return (span
+ oneruns
[*bp
]);
882 span
+= 8, bits
-= 8;
886 * Check partial byte on rhs.
890 span
+= (n
> bits
? bits
: n
);
896 * Return the offset of the next bit in the range
897 * [bs..be] that is different from the specified
898 * color. The end, be, is returned if no such bit
901 #define finddiff(_cp, _bs, _be, _color) \
902 (_bs + (_color ? find1span(_cp,_bs,_be) : find0span(_cp,_bs,_be)))
904 * Like finddiff, but also check the starting bit
905 * against the end in case start > end.
907 #define finddiff2(_cp, _bs, _be, _color) \
908 (_bs < _be ? finddiff(_cp,_bs,_be,_color) : _be)
911 * 1d-encode a row of pixels. The encoding is
912 * a sequence of all-white or all-black spans
913 * of pixels encoded with Huffman codes.
916 Fax3Encode1DRow(TIFF
* tif
, u_char
* bp
, uint32 bits
)
918 Fax3CodecState
* sp
= EncoderState(tif
);
923 span
= find0span(bp
, bs
, bits
); /* white span */
924 putspan(tif
, span
, TIFFFaxWhiteCodes
);
928 span
= find1span(bp
, bs
, bits
); /* black span */
929 putspan(tif
, span
, TIFFFaxBlackCodes
);
934 if (sp
->b
.mode
& (FAXMODE_BYTEALIGN
|FAXMODE_WORDALIGN
)) {
935 if (sp
->bit
!= 8) /* byte-align */
936 Fax3FlushBits(tif
, sp
);
937 if ((sp
->b
.mode
&FAXMODE_WORDALIGN
) &&
938 !isAligned(tif
->tif_rawcp
, uint16
))
939 Fax3FlushBits(tif
, sp
);
944 static const tableentry horizcode
=
945 { 3, 0x1 }; /* 001 */
946 static const tableentry passcode
=
947 { 4, 0x1 }; /* 0001 */
948 static const tableentry vcodes
[7] = {
949 { 7, 0x03 }, /* 0000 011 */
950 { 6, 0x03 }, /* 0000 11 */
951 { 3, 0x03 }, /* 011 */
953 { 3, 0x2 }, /* 010 */
954 { 6, 0x02 }, /* 0000 10 */
955 { 7, 0x02 } /* 0000 010 */
959 * 2d-encode a row of pixels. Consult the CCITT
960 * documentation for the algorithm.
963 Fax3Encode2DRow(TIFF
* tif
, u_char
* bp
, u_char
* rp
, uint32 bits
)
965 #define PIXEL(buf,ix) ((((buf)[(ix)>>3]) >> (7-((ix)&7))) & 1)
967 uint32 a1
= (PIXEL(bp
, 0) != 0 ? 0 : finddiff(bp
, 0, bits
, 0));
968 uint32 b1
= (PIXEL(rp
, 0) != 0 ? 0 : finddiff(rp
, 0, bits
, 0));
972 b2
= finddiff2(rp
, b1
, bits
, PIXEL(rp
,b1
));
975 if (!(-3 <= d
&& d
<= 3)) { /* horizontal mode */
976 a2
= finddiff2(bp
, a1
, bits
, PIXEL(bp
,a1
));
977 putcode(tif
, &horizcode
);
978 if (a0
+a1
== 0 || PIXEL(bp
, a0
) == 0) {
979 putspan(tif
, a1
-a0
, TIFFFaxWhiteCodes
);
980 putspan(tif
, a2
-a1
, TIFFFaxBlackCodes
);
982 putspan(tif
, a1
-a0
, TIFFFaxBlackCodes
);
983 putspan(tif
, a2
-a1
, TIFFFaxWhiteCodes
);
986 } else { /* vertical mode */
987 putcode(tif
, &vcodes
[d
+3]);
990 } else { /* pass mode */
991 putcode(tif
, &passcode
);
996 a1
= finddiff(bp
, a0
, bits
, PIXEL(bp
,a0
));
997 b1
= finddiff(rp
, a0
, bits
, !PIXEL(bp
,a0
));
998 b1
= finddiff(rp
, b1
, bits
, PIXEL(bp
,a0
));
1005 * Encode a buffer of pixels.
1008 Fax3Encode(TIFF
* tif
, tidata_t bp
, tsize_t cc
, tsample_t s
)
1010 Fax3CodecState
* sp
= EncoderState(tif
);
1013 while ((long)cc
> 0) {
1014 if ((sp
->b
.mode
& FAXMODE_NOEOL
) == 0)
1016 if (is2DEncoding(sp
)) {
1017 if (sp
->tag
== G3_1D
) {
1018 if (!Fax3Encode1DRow(tif
, bp
, sp
->b
.rowpixels
))
1022 if (!Fax3Encode2DRow(tif
, bp
, sp
->refline
, sp
->b
.rowpixels
))
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
);
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 if (tif
->tif_data
) {
1072 Fax3CodecState
* sp
= DecoderState(tif
);
1075 _TIFFfree(sp
->runs
);
1077 _TIFFfree(sp
->refline
);
1079 if (Fax3State(tif
)->subaddress
)
1080 _TIFFfree(Fax3State(tif
)->subaddress
);
1081 _TIFFfree(tif
->tif_data
);
1082 tif
->tif_data
= NULL
;
1086 #define FIELD_BADFAXLINES (FIELD_CODEC+0)
1087 #define FIELD_CLEANFAXDATA (FIELD_CODEC+1)
1088 #define FIELD_BADFAXRUN (FIELD_CODEC+2)
1089 #define FIELD_RECVPARAMS (FIELD_CODEC+3)
1090 #define FIELD_SUBADDRESS (FIELD_CODEC+4)
1091 #define FIELD_RECVTIME (FIELD_CODEC+5)
1093 #define FIELD_OPTIONS (FIELD_CODEC+6)
1095 static const TIFFFieldInfo faxFieldInfo
[] = {
1096 { TIFFTAG_FAXMODE
, 0, 0, TIFF_ANY
, FIELD_PSEUDO
,
1097 FALSE
, FALSE
, "FaxMode" },
1098 { TIFFTAG_FAXFILLFUNC
, 0, 0, TIFF_ANY
, FIELD_PSEUDO
,
1099 FALSE
, FALSE
, "FaxFillFunc" },
1100 { TIFFTAG_BADFAXLINES
, 1, 1, TIFF_LONG
, FIELD_BADFAXLINES
,
1101 TRUE
, FALSE
, "BadFaxLines" },
1102 { TIFFTAG_BADFAXLINES
, 1, 1, TIFF_SHORT
, FIELD_BADFAXLINES
,
1103 TRUE
, FALSE
, "BadFaxLines" },
1104 { TIFFTAG_CLEANFAXDATA
, 1, 1, TIFF_SHORT
, FIELD_CLEANFAXDATA
,
1105 TRUE
, FALSE
, "CleanFaxData" },
1106 { TIFFTAG_CONSECUTIVEBADFAXLINES
,1,1, TIFF_LONG
, FIELD_BADFAXRUN
,
1107 TRUE
, FALSE
, "ConsecutiveBadFaxLines" },
1108 { TIFFTAG_CONSECUTIVEBADFAXLINES
,1,1, TIFF_SHORT
, FIELD_BADFAXRUN
,
1109 TRUE
, FALSE
, "ConsecutiveBadFaxLines" },
1110 { TIFFTAG_FAXRECVPARAMS
, 1, 1, TIFF_LONG
, FIELD_RECVPARAMS
,
1111 TRUE
, FALSE
, "FaxRecvParams" },
1112 { TIFFTAG_FAXSUBADDRESS
, -1,-1, TIFF_ASCII
, FIELD_SUBADDRESS
,
1113 TRUE
, FALSE
, "FaxSubAddress" },
1114 { TIFFTAG_FAXRECVTIME
, 1, 1, TIFF_LONG
, FIELD_RECVTIME
,
1115 TRUE
, FALSE
, "FaxRecvTime" },
1117 static const TIFFFieldInfo fax3FieldInfo
[] = {
1118 { TIFFTAG_GROUP3OPTIONS
, 1, 1, TIFF_LONG
, FIELD_OPTIONS
,
1119 FALSE
, FALSE
, "Group3Options" },
1121 static const TIFFFieldInfo fax4FieldInfo
[] = {
1122 { TIFFTAG_GROUP4OPTIONS
, 1, 1, TIFF_LONG
, FIELD_OPTIONS
,
1123 FALSE
, FALSE
, "Group4Options" },
1125 #define N(a) (sizeof (a) / sizeof (a[0]))
1128 Fax3VSetField(TIFF
* tif
, ttag_t tag
, va_list ap
)
1130 Fax3BaseState
* sp
= Fax3State(tif
);
1133 case TIFFTAG_FAXMODE
:
1134 sp
->mode
= va_arg(ap
, int);
1135 return (1); /* NB: pseudo tag */
1136 case TIFFTAG_FAXFILLFUNC
:
1137 DecoderState(tif
)->fill
= va_arg(ap
, TIFFFaxFillFunc
);
1138 return (1); /* NB: pseudo tag */
1139 case TIFFTAG_GROUP3OPTIONS
:
1140 case TIFFTAG_GROUP4OPTIONS
:
1141 sp
->groupoptions
= va_arg(ap
, uint32
);
1143 case TIFFTAG_BADFAXLINES
:
1144 sp
->badfaxlines
= va_arg(ap
, uint32
);
1146 case TIFFTAG_CLEANFAXDATA
:
1147 sp
->cleanfaxdata
= (uint16
) va_arg(ap
, int);
1149 case TIFFTAG_CONSECUTIVEBADFAXLINES
:
1150 sp
->badfaxrun
= va_arg(ap
, uint32
);
1152 case TIFFTAG_FAXRECVPARAMS
:
1153 sp
->recvparams
= va_arg(ap
, uint32
);
1155 case TIFFTAG_FAXSUBADDRESS
:
1156 _TIFFsetString(&sp
->subaddress
, va_arg(ap
, char*));
1158 case TIFFTAG_FAXRECVTIME
:
1159 sp
->recvtime
= va_arg(ap
, uint32
);
1162 return (*sp
->vsetparent
)(tif
, tag
, ap
);
1164 TIFFSetFieldBit(tif
, _TIFFFieldWithTag(tif
, tag
)->field_bit
);
1165 tif
->tif_flags
|= TIFF_DIRTYDIRECT
;
1170 Fax3VGetField(TIFF
* tif
, ttag_t tag
, va_list ap
)
1172 Fax3BaseState
* sp
= Fax3State(tif
);
1175 case TIFFTAG_FAXMODE
:
1176 *va_arg(ap
, int*) = sp
->mode
;
1178 case TIFFTAG_FAXFILLFUNC
:
1179 *va_arg(ap
, TIFFFaxFillFunc
*) = DecoderState(tif
)->fill
;
1181 case TIFFTAG_GROUP3OPTIONS
:
1182 case TIFFTAG_GROUP4OPTIONS
:
1183 *va_arg(ap
, uint32
*) = sp
->groupoptions
;
1185 case TIFFTAG_BADFAXLINES
:
1186 *va_arg(ap
, uint32
*) = sp
->badfaxlines
;
1188 case TIFFTAG_CLEANFAXDATA
:
1189 *va_arg(ap
, uint16
*) = sp
->cleanfaxdata
;
1191 case TIFFTAG_CONSECUTIVEBADFAXLINES
:
1192 *va_arg(ap
, uint32
*) = sp
->badfaxrun
;
1194 case TIFFTAG_FAXRECVPARAMS
:
1195 *va_arg(ap
, uint32
*) = sp
->recvparams
;
1197 case TIFFTAG_FAXSUBADDRESS
:
1198 *va_arg(ap
, char**) = sp
->subaddress
;
1200 case TIFFTAG_FAXRECVTIME
:
1201 *va_arg(ap
, uint32
*) = sp
->recvtime
;
1204 return (*sp
->vgetparent
)(tif
, tag
, ap
);
1210 Fax3PrintDir(TIFF
* tif
, FILE* fd
, long flags
)
1212 Fax3BaseState
* sp
= Fax3State(tif
);
1215 if (TIFFFieldSet(tif
,FIELD_OPTIONS
)) {
1216 const char* sep
= " ";
1217 if (tif
->tif_dir
.td_compression
== COMPRESSION_CCITTFAX4
) {
1218 fprintf(fd
, " Group 4 Options:");
1219 if (sp
->groupoptions
& GROUP4OPT_UNCOMPRESSED
)
1220 fprintf(fd
, "%suncompressed data", sep
);
1223 fprintf(fd
, " Group 3 Options:");
1224 if (sp
->groupoptions
& GROUP3OPT_2DENCODING
)
1225 fprintf(fd
, "%s2-d encoding", sep
), sep
= "+";
1226 if (sp
->groupoptions
& GROUP3OPT_FILLBITS
)
1227 fprintf(fd
, "%sEOL padding", sep
), sep
= "+";
1228 if (sp
->groupoptions
& GROUP3OPT_UNCOMPRESSED
)
1229 fprintf(fd
, "%suncompressed data", sep
);
1231 fprintf(fd
, " (%lu = 0x%lx)\n",
1232 (u_long
) sp
->groupoptions
, (u_long
) sp
->groupoptions
);
1234 if (TIFFFieldSet(tif
,FIELD_CLEANFAXDATA
)) {
1235 fprintf(fd
, " Fax Data:");
1236 switch (sp
->cleanfaxdata
) {
1237 case CLEANFAXDATA_CLEAN
:
1238 fprintf(fd
, " clean");
1240 case CLEANFAXDATA_REGENERATED
:
1241 fprintf(fd
, " receiver regenerated");
1243 case CLEANFAXDATA_UNCLEAN
:
1244 fprintf(fd
, " uncorrected errors");
1247 fprintf(fd
, " (%u = 0x%x)\n",
1248 sp
->cleanfaxdata
, sp
->cleanfaxdata
);
1250 if (TIFFFieldSet(tif
,FIELD_BADFAXLINES
))
1251 fprintf(fd
, " Bad Fax Lines: %lu\n", (u_long
) sp
->badfaxlines
);
1252 if (TIFFFieldSet(tif
,FIELD_BADFAXRUN
))
1253 fprintf(fd
, " Consecutive Bad Fax Lines: %lu\n",
1254 (u_long
) sp
->badfaxrun
);
1255 if (TIFFFieldSet(tif
,FIELD_RECVPARAMS
))
1256 fprintf(fd
, " Fax Receive Parameters: %08lx\n",
1257 (u_long
) sp
->recvparams
);
1258 if (TIFFFieldSet(tif
,FIELD_SUBADDRESS
))
1259 fprintf(fd
, " Fax SubAddress: %s\n", sp
->subaddress
);
1260 if (TIFFFieldSet(tif
,FIELD_RECVTIME
))
1261 fprintf(fd
, " Fax Receive Time: %lu secs\n",
1262 (u_long
) sp
->recvtime
);
1266 InitCCITTFax3(TIFF
* tif
)
1271 * Allocate state block so tag methods have storage to record values.
1273 tif
->tif_data
= (tidata_t
)
1274 _TIFFmalloc(sizeof (Fax3CodecState
));
1276 if (tif
->tif_data
== NULL
) {
1277 TIFFError("TIFFInitCCITTFax3",
1278 "%s: No space for state block", tif
->tif_name
);
1282 sp
= Fax3State(tif
);
1283 sp
->rw_mode
= tif
->tif_mode
;
1286 * Merge codec-specific tag information and
1287 * override parent get/set field methods.
1289 _TIFFMergeFieldInfo(tif
, faxFieldInfo
, N(faxFieldInfo
));
1290 sp
->vgetparent
= tif
->tif_tagmethods
.vgetfield
;
1291 tif
->tif_tagmethods
.vgetfield
= Fax3VGetField
; /* hook for codec tags */
1292 sp
->vsetparent
= tif
->tif_tagmethods
.vsetfield
;
1293 tif
->tif_tagmethods
.vsetfield
= Fax3VSetField
; /* hook for codec tags */
1294 tif
->tif_tagmethods
.printdir
= Fax3PrintDir
; /* hook for codec tags */
1295 sp
->groupoptions
= 0;
1297 sp
->subaddress
= NULL
;
1299 tif
->tif_flags
|= TIFF_NOBITREV
; /* decoder does bit reversal */
1300 DecoderState(tif
)->runs
= NULL
;
1301 TIFFSetField(tif
, TIFFTAG_FAXFILLFUNC
, _TIFFFax3fillruns
);
1302 EncoderState(tif
)->refline
= NULL
;
1305 * Install codec methods.
1307 tif
->tif_setupdecode
= Fax3SetupState
;
1308 tif
->tif_predecode
= Fax3PreDecode
;
1309 tif
->tif_decoderow
= Fax3Decode1D
;
1310 tif
->tif_decodestrip
= Fax3Decode1D
;
1311 tif
->tif_decodetile
= Fax3Decode1D
;
1312 tif
->tif_setupencode
= Fax3SetupState
;
1313 tif
->tif_preencode
= Fax3PreEncode
;
1314 tif
->tif_postencode
= Fax3PostEncode
;
1315 tif
->tif_encoderow
= Fax3Encode
;
1316 tif
->tif_encodestrip
= Fax3Encode
;
1317 tif
->tif_encodetile
= Fax3Encode
;
1318 tif
->tif_close
= Fax3Close
;
1319 tif
->tif_cleanup
= Fax3Cleanup
;
1325 TIFFInitCCITTFax3(TIFF
* tif
, int scheme
)
1327 if (InitCCITTFax3(tif
)) {
1328 _TIFFMergeFieldInfo(tif
, fax3FieldInfo
, N(fax3FieldInfo
));
1331 * The default format is Class/F-style w/o RTC.
1333 return TIFFSetField(tif
, TIFFTAG_FAXMODE
, FAXMODE_CLASSF
);
1339 * CCITT Group 4 (T.6) Facsimile-compatible
1340 * Compression Scheme Support.
1343 #define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }
1345 * Decode the requested amount of G4-encoded data.
1348 Fax4Decode(TIFF
* tif
, tidata_t buf
, tsize_t occ
, tsample_t s
)
1350 DECLARE_STATE_2D(tif
, sp
, "Fax4Decode");
1353 CACHE_STATE(tif
, sp
);
1354 while ((long)occ
> 0) {
1357 pa
= thisrun
= sp
->curruns
;
1361 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc
, BitsAvail
);
1362 printf("-------------------- %d\n", tif
->tif_row
);
1368 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1369 SETVAL(0); /* imaginary change for reference */
1370 SWAP(uint32
*, sp
->curruns
, sp
->refruns
);
1371 buf
+= sp
->b
.rowbytes
;
1372 occ
-= sp
->b
.rowbytes
;
1375 NeedBits16( 13, BADG4
);
1378 if( GetBits(13) != 0x1001 )
1379 fputs( "Bad RTC\n", stderr
);
1382 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1383 UNCACHE_STATE(tif
, sp
);
1386 UNCACHE_STATE(tif
, sp
);
1392 * Encode the requested amount of data.
1395 Fax4Encode(TIFF
* tif
, tidata_t bp
, tsize_t cc
, tsample_t s
)
1397 Fax3CodecState
*sp
= EncoderState(tif
);
1400 while ((long)cc
> 0) {
1401 if (!Fax3Encode2DRow(tif
, bp
, sp
->refline
, sp
->b
.rowpixels
))
1403 _TIFFmemcpy(sp
->refline
, bp
, sp
->b
.rowbytes
);
1404 bp
+= sp
->b
.rowbytes
;
1405 cc
-= sp
->b
.rowbytes
;
1411 Fax4PostEncode(TIFF
* tif
)
1413 Fax3CodecState
*sp
= EncoderState(tif
);
1415 /* terminate strip w/ EOFB */
1416 Fax3PutBits(tif
, EOL
, 12);
1417 Fax3PutBits(tif
, EOL
, 12);
1419 Fax3FlushBits(tif
, sp
);
1424 TIFFInitCCITTFax4(TIFF
* tif
, int scheme
)
1426 if (InitCCITTFax3(tif
)) { /* reuse G3 support */
1427 _TIFFMergeFieldInfo(tif
, fax4FieldInfo
, N(fax4FieldInfo
));
1429 tif
->tif_decoderow
= Fax4Decode
;
1430 tif
->tif_decodestrip
= Fax4Decode
;
1431 tif
->tif_decodetile
= Fax4Decode
;
1432 tif
->tif_encoderow
= Fax4Encode
;
1433 tif
->tif_encodestrip
= Fax4Encode
;
1434 tif
->tif_encodetile
= Fax4Encode
;
1435 tif
->tif_postencode
= Fax4PostEncode
;
1437 * Suppress RTC at the end of each strip.
1439 return TIFFSetField(tif
, TIFFTAG_FAXMODE
, FAXMODE_NORTC
);
1445 * CCITT Group 3 1-D Modified Huffman RLE Compression Support.
1446 * (Compression algorithms 2 and 32771)
1450 * Decode the requested amount of RLE-encoded data.
1453 Fax3DecodeRLE(TIFF
* tif
, tidata_t buf
, tsize_t occ
, tsample_t s
)
1455 DECLARE_STATE(tif
, sp
, "Fax3DecodeRLE");
1456 int mode
= sp
->b
.mode
;
1459 CACHE_STATE(tif
, sp
);
1460 thisrun
= sp
->curruns
;
1461 while ((long)occ
> 0) {
1466 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc
, BitsAvail
);
1467 printf("-------------------- %d\n", tif
->tif_row
);
1471 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1473 * Cleanup at the end of the row.
1475 if (mode
& FAXMODE_BYTEALIGN
) {
1476 int n
= BitsAvail
- (BitsAvail
&~ 7);
1478 } else if (mode
& FAXMODE_WORDALIGN
) {
1479 int n
= BitsAvail
- (BitsAvail
&~ 15);
1481 if (BitsAvail
== 0 && !isAligned(cp
, uint16
))
1484 buf
+= sp
->b
.rowbytes
;
1485 occ
-= sp
->b
.rowbytes
;
1487 EOFRLE
: /* premature EOF */
1488 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1489 UNCACHE_STATE(tif
, sp
);
1492 UNCACHE_STATE(tif
, sp
);
1497 TIFFInitCCITTRLE(TIFF
* tif
, int scheme
)
1499 if (InitCCITTFax3(tif
)) { /* reuse G3 support */
1500 tif
->tif_decoderow
= Fax3DecodeRLE
;
1501 tif
->tif_decodestrip
= Fax3DecodeRLE
;
1502 tif
->tif_decodetile
= Fax3DecodeRLE
;
1504 * Suppress RTC+EOLs when encoding and byte-align data.
1506 return TIFFSetField(tif
, TIFFTAG_FAXMODE
,
1507 FAXMODE_NORTC
|FAXMODE_NOEOL
|FAXMODE_BYTEALIGN
);
1513 TIFFInitCCITTRLEW(TIFF
* tif
, int scheme
)
1515 if (InitCCITTFax3(tif
)) { /* reuse G3 support */
1516 tif
->tif_decoderow
= Fax3DecodeRLE
;
1517 tif
->tif_decodestrip
= Fax3DecodeRLE
;
1518 tif
->tif_decodetile
= Fax3DecodeRLE
;
1520 * Suppress RTC+EOLs when encoding and word-align data.
1522 return TIFFSetField(tif
, TIFFTAG_FAXMODE
,
1523 FAXMODE_NORTC
|FAXMODE_NOEOL
|FAXMODE_WORDALIGN
);
1527 #endif /* CCITT_SUPPORT */