1 /* $Id: tif_fax3.c,v 1.78 2016-09-04 21:32:56 erouault Exp $ */
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
33 * CCITT Group 3 (T.4) and Group 4 (T.6) Compression Support.
35 * This file contains support for decoding and encoding TIFF
36 * compression algorithms 2, 3, 4, and 32771.
38 * Decoder support is derived, with permission, from the code
39 * in Frank Cringle's viewfax program;
40 * 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 tmsize_t 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 */
62 TIFFVGetMethod vgetparent
; /* super-class method */
63 TIFFVSetMethod vsetparent
; /* super-class method */
64 TIFFPrintMethod printdir
; /* super-class method */
66 #define Fax3State(tif) ((Fax3BaseState*) (tif)->tif_data)
68 typedef enum { G3_1D
, G3_2D
} Ttag
;
72 /* Decoder state info */
73 const unsigned char* bitmap
; /* bit reversal table */
74 uint32 data
; /* current i/o byte/word */
75 int bit
; /* current i/o bit in byte */
76 int EOLcnt
; /* count of EOL codes recognized */
77 TIFFFaxFillFunc fill
; /* fill routine */
78 uint32
* runs
; /* b&w runs for current/previous row */
79 uint32
* refruns
; /* runs for reference line */
80 uint32
* curruns
; /* runs for current line */
82 /* Encoder state info */
83 Ttag tag
; /* encoding state */
84 unsigned char* refline
; /* reference line for 2d decoding */
85 int k
; /* #rows left that can be 2d encoded */
86 int maxk
; /* max #rows that can be 2d encoded */
90 #define DecoderState(tif) ((Fax3CodecState*) Fax3State(tif))
91 #define EncoderState(tif) ((Fax3CodecState*) Fax3State(tif))
93 #define is2DEncoding(sp) (sp->b.groupoptions & GROUP3OPT_2DENCODING)
94 #define isAligned(p,t) ((((size_t)(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 -= (tmsize_t)((uint8*) cp - tif->tif_rawcp); \
141 tif->tif_rawcp = (uint8*) cp; \
145 * Setup state for decoding a strip.
148 Fax3PreDecode(TIFF
* tif
, uint16 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
;
175 * Routine for handling various errors/conditions.
176 * Note how they are "glued into the decoder" by
177 * overriding the definitions used by the decoder.
181 Fax3Unexpected(const char* module
, TIFF
* tif
, uint32 line
, uint32 a0
)
183 TIFFErrorExt(tif
->tif_clientdata
, module
, "Bad code word at line %u of %s %u (x %u)",
184 line
, isTiled(tif
) ? "tile" : "strip",
185 (isTiled(tif
) ? tif
->tif_curtile
: tif
->tif_curstrip
),
188 #define unexpected(table, a0) Fax3Unexpected(module, tif, sp->line, a0)
191 Fax3Extension(const char* module
, TIFF
* tif
, uint32 line
, uint32 a0
)
193 TIFFErrorExt(tif
->tif_clientdata
, module
,
194 "Uncompressed data (not supported) at line %u of %s %u (x %u)",
195 line
, isTiled(tif
) ? "tile" : "strip",
196 (isTiled(tif
) ? tif
->tif_curtile
: tif
->tif_curstrip
),
199 #define extension(a0) Fax3Extension(module, tif, sp->line, a0)
202 Fax3BadLength(const char* module
, TIFF
* tif
, uint32 line
, uint32 a0
, uint32 lastx
)
204 TIFFWarningExt(tif
->tif_clientdata
, module
, "%s at line %u of %s %u (got %u, expected %u)",
205 a0
< lastx
? "Premature EOL" : "Line length mismatch",
206 line
, isTiled(tif
) ? "tile" : "strip",
207 (isTiled(tif
) ? tif
->tif_curtile
: tif
->tif_curstrip
),
210 #define badlength(a0,lastx) Fax3BadLength(module, tif, sp->line, a0, lastx)
213 Fax3PrematureEOF(const char* module
, TIFF
* tif
, uint32 line
, uint32 a0
)
215 TIFFWarningExt(tif
->tif_clientdata
, module
, "Premature EOF at line %u of %s %u (x %u)",
216 line
, isTiled(tif
) ? "tile" : "strip",
217 (isTiled(tif
) ? tif
->tif_curtile
: tif
->tif_curstrip
),
220 #define prematureEOF(a0) Fax3PrematureEOF(module, tif, sp->line, a0)
225 * Decode the requested amount of G3 1D-encoded data.
228 Fax3Decode1D(TIFF
* tif
, uint8
* buf
, tmsize_t occ
, uint16 s
)
230 DECLARE_STATE(tif
, sp
, "Fax3Decode1D");
232 if (occ
% sp
->b
.rowbytes
)
234 TIFFErrorExt(tif
->tif_clientdata
, module
, "Fractional scanlines cannot be read");
237 CACHE_STATE(tif
, sp
);
238 thisrun
= sp
->curruns
;
244 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc
, BitsAvail
);
245 printf("-------------------- %d\n", tif
->tif_row
);
250 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
251 buf
+= sp
->b
.rowbytes
;
252 occ
-= sp
->b
.rowbytes
;
255 EOF1D
: /* premature EOF */
257 EOF1Da
: /* premature EOF */
258 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
259 UNCACHE_STATE(tif
, sp
);
262 UNCACHE_STATE(tif
, sp
);
266 #define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }
268 * Decode the requested amount of G3 2D-encoded data.
271 Fax3Decode2D(TIFF
* tif
, uint8
* buf
, tmsize_t occ
, uint16 s
)
273 DECLARE_STATE_2D(tif
, sp
, "Fax3Decode2D");
274 int is1D
; /* current line is 1d/2d-encoded */
276 if (occ
% sp
->b
.rowbytes
)
278 TIFFErrorExt(tif
->tif_clientdata
, module
, "Fractional scanlines cannot be read");
281 CACHE_STATE(tif
, sp
);
285 pa
= thisrun
= sp
->curruns
;
287 printf("\nBitAcc=%08X, BitsAvail = %d EOLcnt = %d",
288 BitAcc
, BitsAvail
, EOLcnt
);
292 is1D
= GetBits(1); /* 1D/2D-encoding tag bit */
295 printf(" %s\n-------------------- %d\n",
296 is1D
? "1D" : "2D", tif
->tif_row
);
305 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
306 SETVALUE(0); /* imaginary change for reference */
307 SWAP(uint32
*, sp
->curruns
, sp
->refruns
);
308 buf
+= sp
->b
.rowbytes
;
309 occ
-= sp
->b
.rowbytes
;
312 EOF2D
: /* premature EOF */
314 EOF2Da
: /* premature EOF */
315 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
316 UNCACHE_STATE(tif
, sp
);
319 UNCACHE_STATE(tif
, sp
);
325 * The ZERO & FILL macros must handle spans < 2*sizeof(long) bytes.
326 * For machines with 64-bit longs this is <16 bytes; otherwise
327 * this is <8 bytes. We optimize the code here to reflect the
328 * machine characteristics.
330 #if SIZEOF_UNSIGNED_LONG == 8
331 # define FILL(n, cp) \
333 case 15:(cp)[14] = 0xff; case 14:(cp)[13] = 0xff; case 13: (cp)[12] = 0xff;\
334 case 12:(cp)[11] = 0xff; case 11:(cp)[10] = 0xff; case 10: (cp)[9] = 0xff;\
335 case 9: (cp)[8] = 0xff; case 8: (cp)[7] = 0xff; case 7: (cp)[6] = 0xff;\
336 case 6: (cp)[5] = 0xff; case 5: (cp)[4] = 0xff; case 4: (cp)[3] = 0xff;\
337 case 3: (cp)[2] = 0xff; case 2: (cp)[1] = 0xff; \
338 case 1: (cp)[0] = 0xff; (cp) += (n); case 0: ; \
340 # define ZERO(n, cp) \
342 case 15:(cp)[14] = 0; case 14:(cp)[13] = 0; case 13: (cp)[12] = 0; \
343 case 12:(cp)[11] = 0; case 11:(cp)[10] = 0; case 10: (cp)[9] = 0; \
344 case 9: (cp)[8] = 0; case 8: (cp)[7] = 0; case 7: (cp)[6] = 0; \
345 case 6: (cp)[5] = 0; case 5: (cp)[4] = 0; case 4: (cp)[3] = 0; \
346 case 3: (cp)[2] = 0; case 2: (cp)[1] = 0; \
347 case 1: (cp)[0] = 0; (cp) += (n); case 0: ; \
350 # define FILL(n, cp) \
352 case 7: (cp)[6] = 0xff; case 6: (cp)[5] = 0xff; case 5: (cp)[4] = 0xff; \
353 case 4: (cp)[3] = 0xff; case 3: (cp)[2] = 0xff; case 2: (cp)[1] = 0xff; \
354 case 1: (cp)[0] = 0xff; (cp) += (n); case 0: ; \
356 # define ZERO(n, cp) \
358 case 7: (cp)[6] = 0; case 6: (cp)[5] = 0; case 5: (cp)[4] = 0; \
359 case 4: (cp)[3] = 0; case 3: (cp)[2] = 0; case 2: (cp)[1] = 0; \
360 case 1: (cp)[0] = 0; (cp) += (n); case 0: ; \
365 * Bit-fill a row according to the white/black
366 * runs generated during G3/G4 decoding.
369 _TIFFFax3fillruns(unsigned char* buf
, uint32
* runs
, uint32
* erun
, uint32 lastx
)
371 static const unsigned char _fillmasks
[] =
372 { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
381 for (; runs
< erun
; runs
+= 2) {
383 if (x
+run
> lastx
|| run
> lastx
)
384 run
= runs
[0] = (uint32
) (lastx
- x
);
389 if (bx
) { /* align to byte boundary */
390 *cp
++ &= 0xff << (8-bx
);
393 if( (n
= run
>> 3) != 0 ) { /* multiple bytes to fill */
394 if ((n
/sizeof (long)) > 1) {
396 * Align to longword boundary and fill.
398 for (; n
&& !isAligned(cp
, long); n
--)
401 nw
= (int32
)(n
/ sizeof (long));
402 n
-= nw
* sizeof (long);
406 cp
= (unsigned char*) lp
;
412 cp
[0] &= 0xff >> run
;
414 cp
[0] &= ~(_fillmasks
[run
]>>bx
);
418 if (x
+run
> lastx
|| run
> lastx
)
419 run
= runs
[1] = lastx
- x
;
424 if (bx
) { /* align to byte boundary */
428 if( (n
= run
>>3) != 0 ) { /* multiple bytes to fill */
429 if ((n
/sizeof (long)) > 1) {
431 * Align to longword boundary and fill.
433 for (; n
&& !isAligned(cp
, long); n
--)
436 nw
= (int32
)(n
/ sizeof (long));
437 n
-= nw
* sizeof (long);
441 cp
= (unsigned char*) lp
;
446 /* Explicit 0xff masking to make icc -check=conversions happy */
448 cp
[0] = (unsigned char)((cp
[0] | (0xff00 >> run
))&0xff);
450 cp
[0] |= _fillmasks
[run
]>>bx
;
460 Fax3FixupTags(TIFF
* tif
)
467 * Setup G3/G4-related compression/decompression state
468 * before data is processed. This routine is called once
469 * per image -- it sets up different state based on whether
470 * or not decoding or encoding is being done and whether
471 * 1D- or 2D-encoded data is involved.
474 Fax3SetupState(TIFF
* tif
)
476 static const char module
[] = "Fax3SetupState";
477 TIFFDirectory
* td
= &tif
->tif_dir
;
478 Fax3BaseState
* sp
= Fax3State(tif
);
480 Fax3CodecState
* dsp
= (Fax3CodecState
*) Fax3State(tif
);
482 uint32 rowpixels
, nruns
;
484 if (td
->td_bitspersample
!= 1) {
485 TIFFErrorExt(tif
->tif_clientdata
, module
,
486 "Bits/sample must be 1 for Group 3/4 encoding/decoding");
490 * Calculate the scanline/tile widths.
493 rowbytes
= TIFFTileRowSize(tif
);
494 rowpixels
= td
->td_tilewidth
;
496 rowbytes
= TIFFScanlineSize(tif
);
497 rowpixels
= td
->td_imagewidth
;
499 sp
->rowbytes
= rowbytes
;
500 sp
->rowpixels
= rowpixels
;
502 * Allocate any additional space required for decoding/encoding.
505 (sp
->groupoptions
& GROUP3OPT_2DENCODING
) ||
506 td
->td_compression
== COMPRESSION_CCITTFAX4
510 Assure that allocation computations do not overflow.
512 TIFFroundup and TIFFSafeMultiply return zero on integer overflow
514 dsp
->runs
=(uint32
*) NULL
;
515 nruns
= TIFFroundup_32(rowpixels
,32);
517 nruns
= TIFFSafeMultiply(uint32
,nruns
,2);
519 if ((nruns
== 0) || (TIFFSafeMultiply(uint32
,nruns
,2) == 0)) {
520 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
521 "Row pixels integer overflow (rowpixels %u)",
525 dsp
->runs
= (uint32
*) _TIFFCheckMalloc(tif
,
526 TIFFSafeMultiply(uint32
,nruns
,2),
528 "for Group 3/4 run arrays");
529 if (dsp
->runs
== NULL
)
531 memset( dsp
->runs
, 0, TIFFSafeMultiply(uint32
,nruns
,2)*sizeof(uint32
));
532 dsp
->curruns
= dsp
->runs
;
534 dsp
->refruns
= dsp
->runs
+ nruns
;
537 if (td
->td_compression
== COMPRESSION_CCITTFAX3
538 && is2DEncoding(dsp
)) { /* NB: default is 1D routine */
539 tif
->tif_decoderow
= Fax3Decode2D
;
540 tif
->tif_decodestrip
= Fax3Decode2D
;
541 tif
->tif_decodetile
= Fax3Decode2D
;
544 if (needsRefLine
) { /* 2d encoding */
545 Fax3CodecState
* esp
= EncoderState(tif
);
547 * 2d encoding requires a scanline
548 * buffer for the ``reference line''; the
549 * scanline against which delta encoding
550 * is referenced. The reference line must
551 * be initialized to be ``white'' (done elsewhere).
553 esp
->refline
= (unsigned char*) _TIFFmalloc(rowbytes
);
554 if (esp
->refline
== NULL
) {
555 TIFFErrorExt(tif
->tif_clientdata
, module
,
556 "No space for Group 3/4 reference line");
559 } else /* 1d encoding */
560 EncoderState(tif
)->refline
= NULL
;
566 * CCITT Group 3 FAX Encoding.
569 #define Fax3FlushBits(tif, sp) { \
570 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
571 (void) TIFFFlushData1(tif); \
572 *(tif)->tif_rawcp++ = (uint8) (sp)->data; \
573 (tif)->tif_rawcc++; \
574 (sp)->data = 0, (sp)->bit = 8; \
576 #define _FlushBits(tif) { \
577 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
578 (void) TIFFFlushData1(tif); \
579 *(tif)->tif_rawcp++ = (uint8) data; \
580 (tif)->tif_rawcc++; \
583 static const int _msbmask
[9] =
584 { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
585 #define _PutBits(tif, bits, length) { \
586 while (length > bit) { \
587 data |= bits >> (length - bit); \
591 assert( length < 9 ); \
592 data |= (bits & _msbmask[length]) << (bit - length); \
599 * Write a variable-length bit-value to
600 * the output stream. Values are
601 * assumed to be at most 16 bits.
604 Fax3PutBits(TIFF
* tif
, unsigned int bits
, unsigned int length
)
606 Fax3CodecState
* sp
= EncoderState(tif
);
607 unsigned int bit
= sp
->bit
;
610 _PutBits(tif
, bits
, length
);
617 * Write a code to the output stream.
619 #define putcode(tif, te) Fax3PutBits(tif, (te)->code, (te)->length)
622 #define DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B")
623 #define DEBUG_PRINT(what,len) { \
625 printf("%08X/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what), len); \
626 for (t = length-1; t >= 0; t--) \
627 putchar(code & (1<<t) ? '1' : '0'); \
633 * Write the sequence of codes that describes
634 * the specified span of zero's or one's. The
635 * appropriate table that holds the make-up and
636 * terminating codes is supplied.
639 putspan(TIFF
* tif
, int32 span
, const tableentry
* tab
)
641 Fax3CodecState
* sp
= EncoderState(tif
);
642 unsigned int bit
= sp
->bit
;
644 unsigned int code
, length
;
646 while (span
>= 2624) {
647 const tableentry
* te
= &tab
[63 + (2560>>6)];
651 DEBUG_PRINT("MakeUp", te
->runlen
);
653 _PutBits(tif
, code
, length
);
657 const tableentry
* te
= &tab
[63 + (span
>>6)];
658 assert(te
->runlen
== 64*(span
>>6));
662 DEBUG_PRINT("MakeUp", te
->runlen
);
664 _PutBits(tif
, code
, length
);
667 code
= tab
[span
].code
;
668 length
= tab
[span
].length
;
670 DEBUG_PRINT(" Term", tab
[span
].runlen
);
672 _PutBits(tif
, code
, length
);
679 * Write an EOL code to the output stream. The zero-fill
680 * logic for byte-aligning encoded scanlines is handled
681 * here. We also handle writing the tag bit for the next
682 * scanline when doing 2d encoding.
685 Fax3PutEOL(TIFF
* tif
)
687 Fax3CodecState
* sp
= EncoderState(tif
);
688 unsigned int bit
= sp
->bit
;
690 unsigned int code
, length
, tparm
;
692 if (sp
->b
.groupoptions
& GROUP3OPT_FILLBITS
) {
694 * Force bit alignment so EOL will terminate on
695 * a byte boundary. That is, force the bit alignment
696 * to 16-12 = 4 before putting out the EOL code.
699 if (align
!= sp
->bit
) {
701 align
= sp
->bit
+ (8 - align
);
703 align
= sp
->bit
- align
;
705 _PutBits(tif
, 0, tparm
);
710 if (is2DEncoding(sp
)) {
711 code
= (code
<<1) | (sp
->tag
== G3_1D
);
714 _PutBits(tif
, code
, length
);
721 * Reset encoding state at the start of a strip.
724 Fax3PreEncode(TIFF
* tif
, uint16 s
)
726 Fax3CodecState
* sp
= EncoderState(tif
);
734 * This is necessary for Group 4; otherwise it isn't
735 * needed because the first scanline of each strip ends
736 * up being copied into the refline.
739 _TIFFmemset(sp
->refline
, 0x00, sp
->b
.rowbytes
);
740 if (is2DEncoding(sp
)) {
741 float res
= tif
->tif_dir
.td_yresolution
;
743 * The CCITT spec says that when doing 2d encoding, you
744 * should only do it on K consecutive scanlines, where K
745 * depends on the resolution of the image being encoded
746 * (2 for <= 200 lpi, 4 for > 200 lpi). Since the directory
747 * code initializes td_yresolution to 0, this code will
748 * select a K of 2 unless the YResolution tag is set
749 * appropriately. (Note also that we fudge a little here
750 * and use 150 lpi to avoid problems with units conversion.)
752 if (tif
->tif_dir
.td_resolutionunit
== RESUNIT_CENTIMETER
)
753 res
*= 2.54f
; /* convert to inches */
754 sp
->maxk
= (res
> 150 ? 4 : 2);
757 sp
->k
= sp
->maxk
= 0;
762 static const unsigned char zeroruns
[256] = {
763 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0f */
764 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */
765 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2f */
766 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3f */
767 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */
768 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5f */
769 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */
770 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7f */
771 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8f */
772 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9f */
773 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 - 0xaf */
774 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 - 0xbf */
775 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 - 0xcf */
776 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 - 0xdf */
777 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 - 0xef */
778 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xf0 - 0xff */
780 static const unsigned char oneruns
[256] = {
781 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 - 0x0f */
782 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */
783 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 - 0x2f */
784 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */
785 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */
786 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */
787 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6f */
788 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7f */
789 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x80 - 0x8f */
790 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x90 - 0x9f */
791 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xa0 - 0xaf */
792 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xb0 - 0xbf */
793 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xc0 - 0xcf */
794 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xd0 - 0xdf */
795 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */
796 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, /* 0xf0 - 0xff */
800 * On certain systems it pays to inline
801 * the routines that find pixel spans.
804 static int32
find0span(unsigned char*, int32
, int32
);
805 static int32
find1span(unsigned char*, int32
, int32
);
806 #pragma inline(find0span,find1span)
810 * Find a span of ones or zeros using the supplied
811 * table. The ``base'' of the bit string is supplied
812 * along with the start+end bit indices.
815 find0span(unsigned char* bp
, int32 bs
, int32 be
)
817 int32 bits
= be
- bs
;
822 * Check partial byte on lhs.
824 if (bits
> 0 && (n
= (bs
& 7)) != 0) {
825 span
= zeroruns
[(*bp
<< n
) & 0xff];
826 if (span
> 8-n
) /* table value too generous */
828 if (span
> bits
) /* constrain span to bit range */
830 if (n
+span
< 8) /* doesn't extend to edge of byte */
836 if (bits
>= (int32
)(2 * 8 * sizeof(long))) {
839 * Align to longword boundary and check longwords.
841 while (!isAligned(bp
, long)) {
843 return (span
+ zeroruns
[*bp
]);
849 while ((bits
>= (int32
)(8 * sizeof(long))) && (0 == *lp
)) {
850 span
+= 8*sizeof (long);
851 bits
-= 8*sizeof (long);
854 bp
= (unsigned char*) lp
;
857 * Scan full bytes for all 0's.
860 if (*bp
!= 0x00) /* end of run */
861 return (span
+ zeroruns
[*bp
]);
867 * Check partial byte on rhs.
871 span
+= (n
> bits
? bits
: n
);
877 find1span(unsigned char* bp
, int32 bs
, int32 be
)
879 int32 bits
= be
- bs
;
884 * Check partial byte on lhs.
886 if (bits
> 0 && (n
= (bs
& 7)) != 0) {
887 span
= oneruns
[(*bp
<< n
) & 0xff];
888 if (span
> 8-n
) /* table value too generous */
890 if (span
> bits
) /* constrain span to bit range */
892 if (n
+span
< 8) /* doesn't extend to edge of byte */
898 if (bits
>= (int32
)(2 * 8 * sizeof(long))) {
901 * Align to longword boundary and check longwords.
903 while (!isAligned(bp
, long)) {
905 return (span
+ oneruns
[*bp
]);
911 while ((bits
>= (int32
)(8 * sizeof(long))) && (~0 == *lp
)) {
912 span
+= 8*sizeof (long);
913 bits
-= 8*sizeof (long);
916 bp
= (unsigned char*) lp
;
919 * Scan full bytes for all 1's.
922 if (*bp
!= 0xff) /* end of run */
923 return (span
+ oneruns
[*bp
]);
929 * Check partial byte on rhs.
933 span
+= (n
> bits
? bits
: n
);
939 * Return the offset of the next bit in the range
940 * [bs..be] that is different from the specified
941 * color. The end, be, is returned if no such bit
944 #define finddiff(_cp, _bs, _be, _color) \
945 (_bs + (_color ? find1span(_cp,_bs,_be) : find0span(_cp,_bs,_be)))
947 * Like finddiff, but also check the starting bit
948 * against the end in case start > end.
950 #define finddiff2(_cp, _bs, _be, _color) \
951 (_bs < _be ? finddiff(_cp,_bs,_be,_color) : _be)
954 * 1d-encode a row of pixels. The encoding is
955 * a sequence of all-white or all-black spans
956 * of pixels encoded with Huffman codes.
959 Fax3Encode1DRow(TIFF
* tif
, unsigned char* bp
, uint32 bits
)
961 Fax3CodecState
* sp
= EncoderState(tif
);
966 span
= find0span(bp
, bs
, bits
); /* white span */
967 putspan(tif
, span
, TIFFFaxWhiteCodes
);
971 span
= find1span(bp
, bs
, bits
); /* black span */
972 putspan(tif
, span
, TIFFFaxBlackCodes
);
977 if (sp
->b
.mode
& (FAXMODE_BYTEALIGN
|FAXMODE_WORDALIGN
)) {
978 if (sp
->bit
!= 8) /* byte-align */
979 Fax3FlushBits(tif
, sp
);
980 if ((sp
->b
.mode
&FAXMODE_WORDALIGN
) &&
981 !isAligned(tif
->tif_rawcp
, uint16
))
982 Fax3FlushBits(tif
, sp
);
987 static const tableentry horizcode
=
988 { 3, 0x1, 0 }; /* 001 */
989 static const tableentry passcode
=
990 { 4, 0x1, 0 }; /* 0001 */
991 static const tableentry vcodes
[7] = {
992 { 7, 0x03, 0 }, /* 0000 011 */
993 { 6, 0x03, 0 }, /* 0000 11 */
994 { 3, 0x03, 0 }, /* 011 */
995 { 1, 0x1, 0 }, /* 1 */
996 { 3, 0x2, 0 }, /* 010 */
997 { 6, 0x02, 0 }, /* 0000 10 */
998 { 7, 0x02, 0 } /* 0000 010 */
1002 * 2d-encode a row of pixels. Consult the CCITT
1003 * documentation for the algorithm.
1006 Fax3Encode2DRow(TIFF
* tif
, unsigned char* bp
, unsigned char* rp
, uint32 bits
)
1008 #define PIXEL(buf,ix) ((((buf)[(ix)>>3]) >> (7-((ix)&7))) & 1)
1010 uint32 a1
= (PIXEL(bp
, 0) != 0 ? 0 : finddiff(bp
, 0, bits
, 0));
1011 uint32 b1
= (PIXEL(rp
, 0) != 0 ? 0 : finddiff(rp
, 0, bits
, 0));
1015 b2
= finddiff2(rp
, b1
, bits
, PIXEL(rp
,b1
));
1018 if (!(-3 <= d
&& d
<= 3)) { /* horizontal mode */
1019 a2
= finddiff2(bp
, a1
, bits
, PIXEL(bp
,a1
));
1020 putcode(tif
, &horizcode
);
1021 if (a0
+a1
== 0 || PIXEL(bp
, a0
) == 0) {
1022 putspan(tif
, a1
-a0
, TIFFFaxWhiteCodes
);
1023 putspan(tif
, a2
-a1
, TIFFFaxBlackCodes
);
1025 putspan(tif
, a1
-a0
, TIFFFaxBlackCodes
);
1026 putspan(tif
, a2
-a1
, TIFFFaxWhiteCodes
);
1029 } else { /* vertical mode */
1030 putcode(tif
, &vcodes
[d
+3]);
1033 } else { /* pass mode */
1034 putcode(tif
, &passcode
);
1039 a1
= finddiff(bp
, a0
, bits
, PIXEL(bp
,a0
));
1040 b1
= finddiff(rp
, a0
, bits
, !PIXEL(bp
,a0
));
1041 b1
= finddiff(rp
, b1
, bits
, PIXEL(bp
,a0
));
1048 * Encode a buffer of pixels.
1051 Fax3Encode(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
1053 static const char module
[] = "Fax3Encode";
1054 Fax3CodecState
* sp
= EncoderState(tif
);
1056 if (cc
% sp
->b
.rowbytes
)
1058 TIFFErrorExt(tif
->tif_clientdata
, module
, "Fractional scanlines cannot be written");
1062 if ((sp
->b
.mode
& FAXMODE_NOEOL
) == 0)
1064 if (is2DEncoding(sp
)) {
1065 if (sp
->tag
== G3_1D
) {
1066 if (!Fax3Encode1DRow(tif
, bp
, sp
->b
.rowpixels
))
1070 if (!Fax3Encode2DRow(tif
, bp
, sp
->refline
,
1079 _TIFFmemcpy(sp
->refline
, bp
, sp
->b
.rowbytes
);
1081 if (!Fax3Encode1DRow(tif
, bp
, sp
->b
.rowpixels
))
1084 bp
+= sp
->b
.rowbytes
;
1085 cc
-= sp
->b
.rowbytes
;
1091 Fax3PostEncode(TIFF
* tif
)
1093 Fax3CodecState
* sp
= EncoderState(tif
);
1096 Fax3FlushBits(tif
, sp
);
1101 Fax3Close(TIFF
* tif
)
1103 if ((Fax3State(tif
)->mode
& FAXMODE_NORTC
) == 0) {
1104 Fax3CodecState
* sp
= EncoderState(tif
);
1105 unsigned int code
= EOL
;
1106 unsigned int length
= 12;
1109 if (is2DEncoding(sp
)) {
1110 code
= (code
<<1) | (sp
->tag
== G3_1D
);
1113 for (i
= 0; i
< 6; i
++)
1114 Fax3PutBits(tif
, code
, length
);
1115 Fax3FlushBits(tif
, sp
);
1120 Fax3Cleanup(TIFF
* tif
)
1122 Fax3CodecState
* sp
= DecoderState(tif
);
1126 tif
->tif_tagmethods
.vgetfield
= sp
->b
.vgetparent
;
1127 tif
->tif_tagmethods
.vsetfield
= sp
->b
.vsetparent
;
1128 tif
->tif_tagmethods
.printdir
= sp
->b
.printdir
;
1131 _TIFFfree(sp
->runs
);
1133 _TIFFfree(sp
->refline
);
1135 _TIFFfree(tif
->tif_data
);
1136 tif
->tif_data
= NULL
;
1138 _TIFFSetDefaultCompressionState(tif
);
1141 #define FIELD_BADFAXLINES (FIELD_CODEC+0)
1142 #define FIELD_CLEANFAXDATA (FIELD_CODEC+1)
1143 #define FIELD_BADFAXRUN (FIELD_CODEC+2)
1145 #define FIELD_OPTIONS (FIELD_CODEC+7)
1147 static const TIFFField faxFields
[] = {
1148 { TIFFTAG_FAXMODE
, 0, 0, TIFF_ANY
, 0, TIFF_SETGET_INT
, TIFF_SETGET_UNDEFINED
, FIELD_PSEUDO
, FALSE
, FALSE
, "FaxMode", NULL
},
1149 { TIFFTAG_FAXFILLFUNC
, 0, 0, TIFF_ANY
, 0, TIFF_SETGET_OTHER
, TIFF_SETGET_UNDEFINED
, FIELD_PSEUDO
, FALSE
, FALSE
, "FaxFillFunc", NULL
},
1150 { TIFFTAG_BADFAXLINES
, 1, 1, TIFF_LONG
, 0, TIFF_SETGET_UINT32
, TIFF_SETGET_UINT32
, FIELD_BADFAXLINES
, TRUE
, FALSE
, "BadFaxLines", NULL
},
1151 { TIFFTAG_CLEANFAXDATA
, 1, 1, TIFF_SHORT
, 0, TIFF_SETGET_UINT16
, TIFF_SETGET_UINT16
, FIELD_CLEANFAXDATA
, TRUE
, FALSE
, "CleanFaxData", NULL
},
1152 { TIFFTAG_CONSECUTIVEBADFAXLINES
, 1, 1, TIFF_LONG
, 0, TIFF_SETGET_UINT32
, TIFF_SETGET_UINT32
, FIELD_BADFAXRUN
, TRUE
, FALSE
, "ConsecutiveBadFaxLines", NULL
}};
1153 static const TIFFField fax3Fields
[] = {
1154 { TIFFTAG_GROUP3OPTIONS
, 1, 1, TIFF_LONG
, 0, TIFF_SETGET_UINT32
, TIFF_SETGET_UINT32
, FIELD_OPTIONS
, FALSE
, FALSE
, "Group3Options", NULL
},
1156 static const TIFFField fax4Fields
[] = {
1157 { TIFFTAG_GROUP4OPTIONS
, 1, 1, TIFF_LONG
, 0, TIFF_SETGET_UINT32
, TIFF_SETGET_UINT32
, FIELD_OPTIONS
, FALSE
, FALSE
, "Group4Options", NULL
},
1161 Fax3VSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
1163 Fax3BaseState
* sp
= Fax3State(tif
);
1164 const TIFFField
* fip
;
1167 assert(sp
->vsetparent
!= 0);
1170 case TIFFTAG_FAXMODE
:
1171 sp
->mode
= (int) va_arg(ap
, int);
1172 return 1; /* NB: pseudo tag */
1173 case TIFFTAG_FAXFILLFUNC
:
1174 DecoderState(tif
)->fill
= va_arg(ap
, TIFFFaxFillFunc
);
1175 return 1; /* NB: pseudo tag */
1176 case TIFFTAG_GROUP3OPTIONS
:
1177 /* XXX: avoid reading options if compression mismatches. */
1178 if (tif
->tif_dir
.td_compression
== COMPRESSION_CCITTFAX3
)
1179 sp
->groupoptions
= (uint32
) va_arg(ap
, uint32
);
1181 case TIFFTAG_GROUP4OPTIONS
:
1182 /* XXX: avoid reading options if compression mismatches. */
1183 if (tif
->tif_dir
.td_compression
== COMPRESSION_CCITTFAX4
)
1184 sp
->groupoptions
= (uint32
) va_arg(ap
, uint32
);
1186 case TIFFTAG_BADFAXLINES
:
1187 sp
->badfaxlines
= (uint32
) va_arg(ap
, uint32
);
1189 case TIFFTAG_CLEANFAXDATA
:
1190 sp
->cleanfaxdata
= (uint16
) va_arg(ap
, uint16_vap
);
1192 case TIFFTAG_CONSECUTIVEBADFAXLINES
:
1193 sp
->badfaxrun
= (uint32
) va_arg(ap
, uint32
);
1196 return (*sp
->vsetparent
)(tif
, tag
, ap
);
1199 if ((fip
= TIFFFieldWithTag(tif
, tag
)) != NULL
)
1200 TIFFSetFieldBit(tif
, fip
->field_bit
);
1204 tif
->tif_flags
|= TIFF_DIRTYDIRECT
;
1209 Fax3VGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
1211 Fax3BaseState
* sp
= Fax3State(tif
);
1216 case TIFFTAG_FAXMODE
:
1217 *va_arg(ap
, int*) = sp
->mode
;
1219 case TIFFTAG_FAXFILLFUNC
:
1220 *va_arg(ap
, TIFFFaxFillFunc
*) = DecoderState(tif
)->fill
;
1222 case TIFFTAG_GROUP3OPTIONS
:
1223 case TIFFTAG_GROUP4OPTIONS
:
1224 *va_arg(ap
, uint32
*) = sp
->groupoptions
;
1226 case TIFFTAG_BADFAXLINES
:
1227 *va_arg(ap
, uint32
*) = sp
->badfaxlines
;
1229 case TIFFTAG_CLEANFAXDATA
:
1230 *va_arg(ap
, uint16
*) = sp
->cleanfaxdata
;
1232 case TIFFTAG_CONSECUTIVEBADFAXLINES
:
1233 *va_arg(ap
, uint32
*) = sp
->badfaxrun
;
1236 return (*sp
->vgetparent
)(tif
, tag
, ap
);
1242 Fax3PrintDir(TIFF
* tif
, FILE* fd
, long flags
)
1244 Fax3BaseState
* sp
= Fax3State(tif
);
1249 if (TIFFFieldSet(tif
,FIELD_OPTIONS
)) {
1250 const char* sep
= " ";
1251 if (tif
->tif_dir
.td_compression
== COMPRESSION_CCITTFAX4
) {
1252 fprintf(fd
, " Group 4 Options:");
1253 if (sp
->groupoptions
& GROUP4OPT_UNCOMPRESSED
)
1254 fprintf(fd
, "%suncompressed data", sep
);
1257 fprintf(fd
, " Group 3 Options:");
1258 if (sp
->groupoptions
& GROUP3OPT_2DENCODING
) {
1259 fprintf(fd
, "%s2-d encoding", sep
);
1262 if (sp
->groupoptions
& GROUP3OPT_FILLBITS
) {
1263 fprintf(fd
, "%sEOL padding", sep
);
1266 if (sp
->groupoptions
& GROUP3OPT_UNCOMPRESSED
)
1267 fprintf(fd
, "%suncompressed data", sep
);
1269 fprintf(fd
, " (%lu = 0x%lx)\n",
1270 (unsigned long) sp
->groupoptions
,
1271 (unsigned long) sp
->groupoptions
);
1273 if (TIFFFieldSet(tif
,FIELD_CLEANFAXDATA
)) {
1274 fprintf(fd
, " Fax Data:");
1275 switch (sp
->cleanfaxdata
) {
1276 case CLEANFAXDATA_CLEAN
:
1277 fprintf(fd
, " clean");
1279 case CLEANFAXDATA_REGENERATED
:
1280 fprintf(fd
, " receiver regenerated");
1282 case CLEANFAXDATA_UNCLEAN
:
1283 fprintf(fd
, " uncorrected errors");
1286 fprintf(fd
, " (%u = 0x%x)\n",
1287 sp
->cleanfaxdata
, sp
->cleanfaxdata
);
1289 if (TIFFFieldSet(tif
,FIELD_BADFAXLINES
))
1290 fprintf(fd
, " Bad Fax Lines: %lu\n",
1291 (unsigned long) sp
->badfaxlines
);
1292 if (TIFFFieldSet(tif
,FIELD_BADFAXRUN
))
1293 fprintf(fd
, " Consecutive Bad Fax Lines: %lu\n",
1294 (unsigned long) sp
->badfaxrun
);
1296 (*sp
->printdir
)(tif
, fd
, flags
);
1300 InitCCITTFax3(TIFF
* tif
)
1302 static const char module
[] = "InitCCITTFax3";
1306 * Merge codec-specific tag information.
1308 if (!_TIFFMergeFields(tif
, faxFields
, TIFFArrayCount(faxFields
))) {
1309 TIFFErrorExt(tif
->tif_clientdata
, "InitCCITTFax3",
1310 "Merging common CCITT Fax codec-specific tags failed");
1315 * Allocate state block so tag methods have storage to record values.
1317 tif
->tif_data
= (uint8
*)
1318 _TIFFmalloc(sizeof (Fax3CodecState
));
1320 if (tif
->tif_data
== NULL
) {
1321 TIFFErrorExt(tif
->tif_clientdata
, module
,
1322 "No space for state block");
1326 sp
= Fax3State(tif
);
1327 sp
->rw_mode
= tif
->tif_mode
;
1330 * Override parent get/set field methods.
1332 sp
->vgetparent
= tif
->tif_tagmethods
.vgetfield
;
1333 tif
->tif_tagmethods
.vgetfield
= Fax3VGetField
; /* hook for codec tags */
1334 sp
->vsetparent
= tif
->tif_tagmethods
.vsetfield
;
1335 tif
->tif_tagmethods
.vsetfield
= Fax3VSetField
; /* hook for codec tags */
1336 sp
->printdir
= tif
->tif_tagmethods
.printdir
;
1337 tif
->tif_tagmethods
.printdir
= Fax3PrintDir
; /* hook for codec tags */
1338 sp
->groupoptions
= 0;
1340 if (sp
->rw_mode
== O_RDONLY
) /* FIXME: improve for in place update */
1341 tif
->tif_flags
|= TIFF_NOBITREV
; /* decoder does bit reversal */
1342 DecoderState(tif
)->runs
= NULL
;
1343 TIFFSetField(tif
, TIFFTAG_FAXFILLFUNC
, _TIFFFax3fillruns
);
1344 EncoderState(tif
)->refline
= NULL
;
1347 * Install codec methods.
1349 tif
->tif_fixuptags
= Fax3FixupTags
;
1350 tif
->tif_setupdecode
= Fax3SetupState
;
1351 tif
->tif_predecode
= Fax3PreDecode
;
1352 tif
->tif_decoderow
= Fax3Decode1D
;
1353 tif
->tif_decodestrip
= Fax3Decode1D
;
1354 tif
->tif_decodetile
= Fax3Decode1D
;
1355 tif
->tif_setupencode
= Fax3SetupState
;
1356 tif
->tif_preencode
= Fax3PreEncode
;
1357 tif
->tif_postencode
= Fax3PostEncode
;
1358 tif
->tif_encoderow
= Fax3Encode
;
1359 tif
->tif_encodestrip
= Fax3Encode
;
1360 tif
->tif_encodetile
= Fax3Encode
;
1361 tif
->tif_close
= Fax3Close
;
1362 tif
->tif_cleanup
= Fax3Cleanup
;
1368 TIFFInitCCITTFax3(TIFF
* tif
, int scheme
)
1371 if (InitCCITTFax3(tif
)) {
1373 * Merge codec-specific tag information.
1375 if (!_TIFFMergeFields(tif
, fax3Fields
,
1376 TIFFArrayCount(fax3Fields
))) {
1377 TIFFErrorExt(tif
->tif_clientdata
, "TIFFInitCCITTFax3",
1378 "Merging CCITT Fax 3 codec-specific tags failed");
1383 * The default format is Class/F-style w/o RTC.
1385 return TIFFSetField(tif
, TIFFTAG_FAXMODE
, FAXMODE_CLASSF
);
1391 * CCITT Group 4 (T.6) Facsimile-compatible
1392 * Compression Scheme Support.
1395 #define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }
1397 * Decode the requested amount of G4-encoded data.
1400 Fax4Decode(TIFF
* tif
, uint8
* buf
, tmsize_t occ
, uint16 s
)
1402 DECLARE_STATE_2D(tif
, sp
, "Fax4Decode");
1404 if (occ
% sp
->b
.rowbytes
)
1406 TIFFErrorExt(tif
->tif_clientdata
, module
, "Fractional scanlines cannot be read");
1409 CACHE_STATE(tif
, sp
);
1413 pa
= thisrun
= sp
->curruns
;
1417 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc
, BitsAvail
);
1418 printf("-------------------- %d\n", tif
->tif_row
);
1424 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1425 SETVALUE(0); /* imaginary change for reference */
1426 SWAP(uint32
*, sp
->curruns
, sp
->refruns
);
1427 buf
+= sp
->b
.rowbytes
;
1428 occ
-= sp
->b
.rowbytes
;
1432 NeedBits16( 13, BADG4
);
1435 if( GetBits(13) != 0x1001 )
1436 fputs( "Bad EOFB\n", stderr
);
1439 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1440 UNCACHE_STATE(tif
, sp
);
1441 return ( sp
->line
? 1 : -1); /* don't error on badly-terminated strips */
1443 UNCACHE_STATE(tif
, sp
);
1449 * Encode the requested amount of data.
1452 Fax4Encode(TIFF
* tif
, uint8
* bp
, tmsize_t cc
, uint16 s
)
1454 static const char module
[] = "Fax4Encode";
1455 Fax3CodecState
*sp
= EncoderState(tif
);
1457 if (cc
% sp
->b
.rowbytes
)
1459 TIFFErrorExt(tif
->tif_clientdata
, module
, "Fractional scanlines cannot be written");
1463 if (!Fax3Encode2DRow(tif
, bp
, sp
->refline
, sp
->b
.rowpixels
))
1465 _TIFFmemcpy(sp
->refline
, bp
, sp
->b
.rowbytes
);
1466 bp
+= sp
->b
.rowbytes
;
1467 cc
-= sp
->b
.rowbytes
;
1473 Fax4PostEncode(TIFF
* tif
)
1475 Fax3CodecState
*sp
= EncoderState(tif
);
1477 /* terminate strip w/ EOFB */
1478 Fax3PutBits(tif
, EOL
, 12);
1479 Fax3PutBits(tif
, EOL
, 12);
1481 Fax3FlushBits(tif
, sp
);
1486 TIFFInitCCITTFax4(TIFF
* tif
, int scheme
)
1489 if (InitCCITTFax3(tif
)) { /* reuse G3 support */
1491 * Merge codec-specific tag information.
1493 if (!_TIFFMergeFields(tif
, fax4Fields
,
1494 TIFFArrayCount(fax4Fields
))) {
1495 TIFFErrorExt(tif
->tif_clientdata
, "TIFFInitCCITTFax4",
1496 "Merging CCITT Fax 4 codec-specific tags failed");
1500 tif
->tif_decoderow
= Fax4Decode
;
1501 tif
->tif_decodestrip
= Fax4Decode
;
1502 tif
->tif_decodetile
= Fax4Decode
;
1503 tif
->tif_encoderow
= Fax4Encode
;
1504 tif
->tif_encodestrip
= Fax4Encode
;
1505 tif
->tif_encodetile
= Fax4Encode
;
1506 tif
->tif_postencode
= Fax4PostEncode
;
1508 * Suppress RTC at the end of each strip.
1510 return TIFFSetField(tif
, TIFFTAG_FAXMODE
, FAXMODE_NORTC
);
1516 * CCITT Group 3 1-D Modified Huffman RLE Compression Support.
1517 * (Compression algorithms 2 and 32771)
1521 * Decode the requested amount of RLE-encoded data.
1524 Fax3DecodeRLE(TIFF
* tif
, uint8
* buf
, tmsize_t occ
, uint16 s
)
1526 DECLARE_STATE(tif
, sp
, "Fax3DecodeRLE");
1527 int mode
= sp
->b
.mode
;
1529 if (occ
% sp
->b
.rowbytes
)
1531 TIFFErrorExt(tif
->tif_clientdata
, module
, "Fractional scanlines cannot be read");
1534 CACHE_STATE(tif
, sp
);
1535 thisrun
= sp
->curruns
;
1541 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc
, BitsAvail
);
1542 printf("-------------------- %d\n", tif
->tif_row
);
1546 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1548 * Cleanup at the end of the row.
1550 if (mode
& FAXMODE_BYTEALIGN
) {
1551 int n
= BitsAvail
- (BitsAvail
&~ 7);
1553 } else if (mode
& FAXMODE_WORDALIGN
) {
1554 int n
= BitsAvail
- (BitsAvail
&~ 15);
1556 if (BitsAvail
== 0 && !isAligned(cp
, uint16
))
1559 buf
+= sp
->b
.rowbytes
;
1560 occ
-= sp
->b
.rowbytes
;
1563 EOFRLE
: /* premature EOF */
1564 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1565 UNCACHE_STATE(tif
, sp
);
1568 UNCACHE_STATE(tif
, sp
);
1573 TIFFInitCCITTRLE(TIFF
* tif
, int scheme
)
1576 if (InitCCITTFax3(tif
)) { /* reuse G3 support */
1577 tif
->tif_decoderow
= Fax3DecodeRLE
;
1578 tif
->tif_decodestrip
= Fax3DecodeRLE
;
1579 tif
->tif_decodetile
= Fax3DecodeRLE
;
1581 * Suppress RTC+EOLs when encoding and byte-align data.
1583 return TIFFSetField(tif
, TIFFTAG_FAXMODE
,
1584 FAXMODE_NORTC
|FAXMODE_NOEOL
|FAXMODE_BYTEALIGN
);
1590 TIFFInitCCITTRLEW(TIFF
* tif
, int scheme
)
1593 if (InitCCITTFax3(tif
)) { /* reuse G3 support */
1594 tif
->tif_decoderow
= Fax3DecodeRLE
;
1595 tif
->tif_decodestrip
= Fax3DecodeRLE
;
1596 tif
->tif_decodetile
= Fax3DecodeRLE
;
1598 * Suppress RTC+EOLs when encoding and word-align data.
1600 return TIFFSetField(tif
, TIFFTAG_FAXMODE
,
1601 FAXMODE_NORTC
|FAXMODE_NOEOL
|FAXMODE_WORDALIGN
);
1605 #endif /* CCITT_SUPPORT */
1607 /* vim: set ts=8 sts=8 sw=8 noet: */