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.
50 int rw_mode
; /* O_RDONLY for decode, else encode */
51 int mode
; /* operating mode */
52 tmsize_t rowbytes
; /* bytes in a decoded scanline */
53 uint32_t rowpixels
; /* pixels in a scanline */
55 uint16_t cleanfaxdata
; /* CleanFaxData tag */
56 uint32_t badfaxrun
; /* BadFaxRun tag */
57 uint32_t badfaxlines
; /* BadFaxLines tag */
58 uint32_t groupoptions
; /* Group 3/4 options tag */
60 TIFFVGetMethod vgetparent
; /* super-class method */
61 TIFFVSetMethod vsetparent
; /* super-class method */
62 TIFFPrintMethod printdir
; /* super-class method */
64 #define Fax3State(tif) ((Fax3BaseState *)(tif)->tif_data)
75 /* Decoder state info */
76 const unsigned char *bitmap
; /* bit reversal table */
77 uint32_t data
; /* current i/o byte/word */
78 int bit
; /* current i/o bit in byte */
79 int EOLcnt
; /* count of EOL codes recognized */
80 TIFFFaxFillFunc fill
; /* fill routine */
81 uint32_t *runs
; /* b&w runs for current/previous row */
82 uint32_t nruns
; /* size of the refruns / curruns arrays */
83 uint32_t *refruns
; /* runs for reference line */
84 uint32_t *curruns
; /* runs for current line */
86 /* Encoder state info */
87 Ttag tag
; /* encoding state */
88 unsigned char *refline
; /* reference line for 2d decoding */
89 int k
; /* #rows left that can be 2d encoded */
90 int maxk
; /* max #rows that can be 2d encoded */
94 #define DecoderState(tif) ((Fax3CodecState *)Fax3State(tif))
95 #define EncoderState(tif) ((Fax3CodecState *)Fax3State(tif))
97 #define is2DEncoding(sp) (sp->b.groupoptions & GROUP3OPT_2DENCODING)
98 #define isAligned(p, t) ((((size_t)(p)) & (sizeof(t) - 1)) == 0)
101 * Group 3 and Group 4 Decoding.
105 * These macros glue the TIFF library state to
106 * the state expected by Frank's decoder.
108 #define DECLARE_STATE(tif, sp, mod) \
109 static const char module[] = mod; \
110 Fax3CodecState *sp = DecoderState(tif); \
111 int a0; /* reference element */ \
112 int lastx = sp->b.rowpixels; /* last element in row */ \
113 uint32_t BitAcc; /* bit accumulator */ \
114 int BitsAvail; /* # valid bits in BitAcc */ \
115 int RunLength; /* length of current run */ \
116 unsigned char *cp; /* next byte of input data */ \
117 unsigned char *ep; /* end of input data */ \
118 uint32_t *pa; /* place to stuff next run */ \
119 uint32_t *thisrun; /* current row's run array */ \
120 int EOLcnt; /* # EOL codes recognized */ \
121 const unsigned char *bitmap = sp->bitmap; /* input data bit reverser */ \
122 const TIFFFaxTabEnt *TabEnt
123 #define DECLARE_STATE_2D(tif, sp, mod) \
124 DECLARE_STATE(tif, sp, mod); \
125 int b1; /* next change on prev line */ \
127 *pb /* next run in reference line */ /* \
128 * Load any state that may be \
129 * changed during decoding. \
131 #define CACHE_STATE(tif, sp) \
135 BitsAvail = sp->bit; \
136 EOLcnt = sp->EOLcnt; \
137 cp = (unsigned char *)tif->tif_rawcp; \
138 ep = cp + tif->tif_rawcc; \
141 * Save state possibly changed during decoding.
143 #define UNCACHE_STATE(tif, sp) \
146 sp->bit = BitsAvail; \
148 sp->EOLcnt = EOLcnt; \
149 tif->tif_rawcc -= (tmsize_t)((uint8_t *)cp - tif->tif_rawcp); \
150 tif->tif_rawcp = (uint8_t *)cp; \
154 * Setup state for decoding a strip.
156 static int Fax3PreDecode(TIFF
*tif
, uint16_t s
)
158 Fax3CodecState
*sp
= DecoderState(tif
);
162 sp
->bit
= 0; /* force initial read */
164 sp
->EOLcnt
= 0; /* force initial scan for EOL */
166 * Decoder assumes lsb-to-msb bit order. Note that we select
167 * this here rather than in Fax3SetupState so that viewers can
168 * hold the image open, fiddle with the FillOrder tag value,
169 * and then re-decode the image. Otherwise they'd need to close
170 * and open the image to get the state reset.
173 TIFFGetBitRevTable(tif
->tif_dir
.td_fillorder
!= FILLORDER_LSB2MSB
);
174 sp
->curruns
= sp
->runs
;
176 { /* init reference line to white */
177 sp
->refruns
= sp
->runs
+ sp
->nruns
;
178 sp
->refruns
[0] = (uint32_t)sp
->b
.rowpixels
;
186 * Routine for handling various errors/conditions.
187 * Note how they are "glued into the decoder" by
188 * overriding the definitions used by the decoder.
191 static void Fax3Unexpected(const char *module
, TIFF
*tif
, uint32_t line
,
194 TIFFErrorExtR(tif
, module
,
195 "Bad code word at line %" PRIu32
" of %s %" PRIu32
197 line
, isTiled(tif
) ? "tile" : "strip",
198 (isTiled(tif
) ? tif
->tif_curtile
: tif
->tif_curstrip
), a0
);
200 #define unexpected(table, a0) Fax3Unexpected(module, tif, sp->line, a0)
202 static void Fax3Extension(const char *module
, TIFF
*tif
, uint32_t line
,
205 TIFFErrorExtR(tif
, module
,
206 "Uncompressed data (not supported) at line %" PRIu32
207 " of %s %" PRIu32
" (x %" PRIu32
")",
208 line
, isTiled(tif
) ? "tile" : "strip",
209 (isTiled(tif
) ? tif
->tif_curtile
: tif
->tif_curstrip
), a0
);
211 #define extension(a0) Fax3Extension(module, tif, sp->line, a0)
213 static void Fax3BadLength(const char *module
, TIFF
*tif
, uint32_t line
,
214 uint32_t a0
, uint32_t lastx
)
216 TIFFWarningExtR(tif
, module
,
217 "%s at line %" PRIu32
" of %s %" PRIu32
" (got %" PRIu32
218 ", expected %" PRIu32
")",
219 a0
< lastx
? "Premature EOL" : "Line length mismatch", line
,
220 isTiled(tif
) ? "tile" : "strip",
221 (isTiled(tif
) ? tif
->tif_curtile
: tif
->tif_curstrip
), a0
,
224 #define badlength(a0, lastx) Fax3BadLength(module, tif, sp->line, a0, lastx)
226 static void Fax3PrematureEOF(const char *module
, TIFF
*tif
, uint32_t line
,
229 TIFFWarningExtR(tif
, module
,
230 "Premature EOF at line %" PRIu32
" of %s %" PRIu32
232 line
, isTiled(tif
) ? "tile" : "strip",
233 (isTiled(tif
) ? tif
->tif_curtile
: tif
->tif_curstrip
), a0
);
235 #define prematureEOF(a0) Fax3PrematureEOF(module, tif, sp->line, a0)
240 * Decode the requested amount of G3 1D-encoded data.
241 * @param buf destination buffer
242 * @param occ available bytes in destination buffer
243 * @param s number of planes (ignored)
244 * @returns 1 for success, -1 in case of error
246 static int Fax3Decode1D(TIFF
*tif
, uint8_t *buf
, tmsize_t occ
, uint16_t s
)
248 DECLARE_STATE(tif
, sp
, "Fax3Decode1D");
250 if (occ
% sp
->b
.rowbytes
)
252 TIFFErrorExtR(tif
, module
, "Fractional scanlines cannot be read");
255 CACHE_STATE(tif
, sp
);
256 thisrun
= sp
->curruns
;
263 printf("\nBitAcc=%08" PRIX32
", BitsAvail = %d\n", BitAcc
, BitsAvail
);
264 printf("-------------------- %" PRIu32
"\n", tif
->tif_row
);
269 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
270 buf
+= sp
->b
.rowbytes
;
271 occ
-= sp
->b
.rowbytes
;
274 EOF1D
: /* premature EOF */
276 EOF1Da
: /* premature EOF */
277 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
278 UNCACHE_STATE(tif
, sp
);
281 UNCACHE_STATE(tif
, sp
);
285 #define SWAP(t, a, b) \
293 * Decode the requested amount of G3 2D-encoded data.
295 static int Fax3Decode2D(TIFF
*tif
, uint8_t *buf
, tmsize_t occ
, uint16_t s
)
297 DECLARE_STATE_2D(tif
, sp
, "Fax3Decode2D");
298 int is1D
; /* current line is 1d/2d-encoded */
300 if (occ
% sp
->b
.rowbytes
)
302 TIFFErrorExtR(tif
, module
, "Fractional scanlines cannot be read");
305 CACHE_STATE(tif
, sp
);
310 pa
= thisrun
= sp
->curruns
;
312 printf("\nBitAcc=%08" PRIX32
", BitsAvail = %d EOLcnt = %d", BitAcc
,
317 is1D
= GetBits(1); /* 1D/2D-encoding tag bit */
320 printf(" %s\n-------------------- %" PRIu32
"\n", is1D
? "1D" : "2D",
330 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
331 if (pa
< thisrun
+ sp
->nruns
)
333 SETVALUE(0); /* imaginary change for reference */
335 SWAP(uint32_t *, sp
->curruns
, sp
->refruns
);
336 buf
+= sp
->b
.rowbytes
;
337 occ
-= sp
->b
.rowbytes
;
340 EOF2D
: /* premature EOF */
342 EOF2Da
: /* premature EOF */
343 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
344 UNCACHE_STATE(tif
, sp
);
347 UNCACHE_STATE(tif
, sp
);
352 #define FILL(n, cp) \
353 for (int32_t ifill = 0; ifill < (n); ++ifill) \
355 (cp)[ifill] = 0xff; \
359 #define ZERO(n, cp) \
360 for (int32_t izero = 0; izero < (n); ++izero) \
367 * Bit-fill a row according to the white/black
368 * runs generated during G3/G4 decoding.
370 void _TIFFFax3fillruns(unsigned char *buf
, uint32_t *runs
, uint32_t *erun
,
373 static const unsigned char _fillmasks
[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
374 0xf8, 0xfc, 0xfe, 0xff};
380 if ((erun
- runs
) & 1)
383 for (; runs
< erun
; runs
+= 2)
386 if (x
+ run
> lastx
|| run
> lastx
)
387 run
= runs
[0] = (uint32_t)(lastx
- x
);
395 { /* align to byte boundary */
396 *cp
++ &= 0xff << (8 - bx
);
399 if ((n
= run
>> 3) != 0)
400 { /* multiple bytes to fill */
401 if ((n
/ sizeof(int64_t)) > 1)
404 * Align to int64_tword boundary and fill.
406 for (; n
&& !isAligned(cp
, int64_t); n
--)
409 nw
= (int32_t)(n
/ sizeof(int64_t));
410 n
-= nw
* sizeof(int64_t);
415 cp
= (unsigned char *)lp
;
421 cp
[0] &= 0xff >> run
;
424 cp
[0] &= ~(_fillmasks
[run
] >> bx
);
428 if (x
+ run
> lastx
|| run
> lastx
)
429 run
= runs
[1] = lastx
- x
;
437 { /* align to byte boundary */
441 if ((n
= run
>> 3) != 0)
442 { /* multiple bytes to fill */
443 if ((n
/ sizeof(int64_t)) > 1)
446 * Align to int64_t boundary and fill.
448 for (; n
&& !isAligned(cp
, int64_t); n
--)
451 nw
= (int32_t)(n
/ sizeof(int64_t));
452 n
-= nw
* sizeof(int64_t);
457 cp
= (unsigned char *)lp
;
462 /* Explicit 0xff masking to make icc -check=conversions happy */
464 cp
[0] = (unsigned char)((cp
[0] | (0xff00 >> run
)) & 0xff);
467 cp
[0] |= _fillmasks
[run
] >> bx
;
476 static int Fax3FixupTags(TIFF
*tif
)
483 * Setup G3/G4-related compression/decompression state
484 * before data is processed. This routine is called once
485 * per image -- it sets up different state based on whether
486 * or not decoding or encoding is being done and whether
487 * 1D- or 2D-encoded data is involved.
489 static int Fax3SetupState(TIFF
*tif
)
491 static const char module
[] = "Fax3SetupState";
492 TIFFDirectory
*td
= &tif
->tif_dir
;
493 Fax3BaseState
*sp
= Fax3State(tif
);
495 Fax3CodecState
*dsp
= (Fax3CodecState
*)Fax3State(tif
);
499 if (td
->td_bitspersample
!= 1)
501 TIFFErrorExtR(tif
, module
,
502 "Bits/sample must be 1 for Group 3/4 encoding/decoding");
506 * Calculate the scanline/tile widths.
510 rowbytes
= TIFFTileRowSize(tif
);
511 rowpixels
= td
->td_tilewidth
;
515 rowbytes
= TIFFScanlineSize(tif
);
516 rowpixels
= td
->td_imagewidth
;
518 if ((int64_t)rowbytes
< ((int64_t)rowpixels
+ 7) / 8)
520 TIFFErrorExtR(tif
, module
,
521 "Inconsistent number of bytes per row : rowbytes=%" PRId64
522 " rowpixels=%" PRIu32
,
523 (int64_t)rowbytes
, rowpixels
);
526 sp
->rowbytes
= rowbytes
;
527 sp
->rowpixels
= rowpixels
;
529 * Allocate any additional space required for decoding/encoding.
531 needsRefLine
= ((sp
->groupoptions
& GROUP3OPT_2DENCODING
) ||
532 td
->td_compression
== COMPRESSION_CCITTFAX4
);
535 Assure that allocation computations do not overflow.
537 TIFFroundup and TIFFSafeMultiply return zero on integer overflow
539 dsp
->runs
= (uint32_t *)NULL
;
540 dsp
->nruns
= TIFFroundup_32(rowpixels
+ 1, 32);
543 dsp
->nruns
= TIFFSafeMultiply(uint32_t, dsp
->nruns
, 2);
545 if ((dsp
->nruns
== 0) || (TIFFSafeMultiply(uint32_t, dsp
->nruns
, 2) == 0))
547 TIFFErrorExtR(tif
, tif
->tif_name
,
548 "Row pixels integer overflow (rowpixels %" PRIu32
")",
552 dsp
->runs
= (uint32_t *)_TIFFCheckMalloc(
553 tif
, TIFFSafeMultiply(uint32_t, dsp
->nruns
, 2), sizeof(uint32_t),
554 "for Group 3/4 run arrays");
555 if (dsp
->runs
== NULL
)
558 TIFFSafeMultiply(uint32_t, dsp
->nruns
, 2) * sizeof(uint32_t));
559 dsp
->curruns
= dsp
->runs
;
561 dsp
->refruns
= dsp
->runs
+ dsp
->nruns
;
564 if (td
->td_compression
== COMPRESSION_CCITTFAX3
&& is2DEncoding(dsp
))
565 { /* NB: default is 1D routine */
566 tif
->tif_decoderow
= Fax3Decode2D
;
567 tif
->tif_decodestrip
= Fax3Decode2D
;
568 tif
->tif_decodetile
= Fax3Decode2D
;
573 Fax3CodecState
*esp
= EncoderState(tif
);
575 * 2d encoding requires a scanline
576 * buffer for the ``reference line''; the
577 * scanline against which delta encoding
578 * is referenced. The reference line must
579 * be initialized to be ``white'' (done elsewhere).
581 esp
->refline
= (unsigned char *)_TIFFmallocExt(tif
, rowbytes
);
582 if (esp
->refline
== NULL
)
584 TIFFErrorExtR(tif
, module
, "No space for Group 3/4 reference line");
588 else /* 1d encoding */
589 EncoderState(tif
)->refline
= NULL
;
595 * CCITT Group 3 FAX Encoding.
598 #define Fax3FlushBits(tif, sp) \
600 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
602 if (!TIFFFlushData1(tif)) \
605 *(tif)->tif_rawcp++ = (uint8_t)(sp)->data; \
606 (tif)->tif_rawcc++; \
607 (sp)->data = 0, (sp)->bit = 8; \
609 #define _FlushBits(tif) \
611 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
613 if (!TIFFFlushData1(tif)) \
616 *(tif)->tif_rawcp++ = (uint8_t)data; \
617 (tif)->tif_rawcc++; \
620 static const int _msbmask
[9] = {0x00, 0x01, 0x03, 0x07, 0x0f,
621 0x1f, 0x3f, 0x7f, 0xff};
622 #define _PutBits(tif, bits, length) \
624 while (length > bit) \
626 data |= bits >> (length - bit); \
630 assert(length < 9); \
631 data |= (bits & _msbmask[length]) << (bit - length); \
638 * Write a variable-length bit-value to
639 * the output stream. Values are
640 * assumed to be at most 16 bits.
642 static int Fax3PutBits(TIFF
*tif
, unsigned int bits
, unsigned int length
)
644 Fax3CodecState
*sp
= EncoderState(tif
);
645 unsigned int bit
= sp
->bit
;
648 _PutBits(tif
, bits
, length
);
656 * Write a code to the output stream.
658 #define putcode(tif, te) Fax3PutBits(tif, (te)->code, (te)->length)
661 #define DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B")
662 #define DEBUG_PRINT(what, len) \
665 printf("%08" PRIX32 "/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what), \
667 for (t = length - 1; t >= 0; t--) \
668 putchar(code & (1 << t) ? '1' : '0'); \
674 * Write the sequence of codes that describes
675 * the specified span of zero's or one's. The
676 * appropriate table that holds the make-up and
677 * terminating codes is supplied.
679 static int putspan(TIFF
*tif
, int32_t span
, const tableentry
*tab
)
681 Fax3CodecState
*sp
= EncoderState(tif
);
682 unsigned int bit
= sp
->bit
;
684 unsigned int code
, length
;
688 const tableentry
*te
= &tab
[63 + (2560 >> 6)];
692 DEBUG_PRINT("MakeUp", te
->runlen
);
694 _PutBits(tif
, code
, length
);
699 const tableentry
*te
= &tab
[63 + (span
>> 6)];
700 assert(te
->runlen
== 64 * (span
>> 6));
704 DEBUG_PRINT("MakeUp", te
->runlen
);
706 _PutBits(tif
, code
, length
);
709 code
= tab
[span
].code
;
710 length
= tab
[span
].length
;
712 DEBUG_PRINT(" Term", tab
[span
].runlen
);
714 _PutBits(tif
, code
, length
);
723 * Write an EOL code to the output stream. The zero-fill
724 * logic for byte-aligning encoded scanlines is handled
725 * here. We also handle writing the tag bit for the next
726 * scanline when doing 2d encoding.
728 static int Fax3PutEOL(TIFF
*tif
)
730 Fax3CodecState
*sp
= EncoderState(tif
);
731 unsigned int bit
= sp
->bit
;
733 unsigned int code
, length
, tparm
;
735 if (sp
->b
.groupoptions
& GROUP3OPT_FILLBITS
)
738 * Force bit alignment so EOL will terminate on
739 * a byte boundary. That is, force the bit alignment
740 * to 16-12 = 4 before putting out the EOL code.
743 if (align
!= sp
->bit
)
746 align
= sp
->bit
+ (8 - align
);
748 align
= sp
->bit
- align
;
750 _PutBits(tif
, 0, tparm
);
755 if (is2DEncoding(sp
))
757 code
= (code
<< 1) | (sp
->tag
== G3_1D
);
760 _PutBits(tif
, code
, length
);
769 * Reset encoding state at the start of a strip.
771 static int Fax3PreEncode(TIFF
*tif
, uint16_t s
)
773 Fax3CodecState
*sp
= EncoderState(tif
);
781 * This is necessary for Group 4; otherwise it isn't
782 * needed because the first scanline of each strip ends
783 * up being copied into the refline.
786 _TIFFmemset(sp
->refline
, 0x00, sp
->b
.rowbytes
);
787 if (is2DEncoding(sp
))
789 float res
= tif
->tif_dir
.td_yresolution
;
791 * The CCITT spec says that when doing 2d encoding, you
792 * should only do it on K consecutive scanlines, where K
793 * depends on the resolution of the image being encoded
794 * (2 for <= 200 lpi, 4 for > 200 lpi). Since the directory
795 * code initializes td_yresolution to 0, this code will
796 * select a K of 2 unless the YResolution tag is set
797 * appropriately. (Note also that we fudge a little here
798 * and use 150 lpi to avoid problems with units conversion.)
800 if (tif
->tif_dir
.td_resolutionunit
== RESUNIT_CENTIMETER
)
801 res
*= 2.54f
; /* convert to inches */
802 sp
->maxk
= (res
> 150 ? 4 : 2);
803 sp
->k
= sp
->maxk
- 1;
806 sp
->k
= sp
->maxk
= 0;
811 static const unsigned char zeroruns
[256] = {
812 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0f */
813 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */
814 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2f */
815 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3f */
816 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */
817 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5f */
818 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */
819 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7f */
820 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8f */
821 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9f */
822 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 - 0xaf */
823 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 - 0xbf */
824 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 - 0xcf */
825 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 - 0xdf */
826 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 - 0xef */
827 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xf0 - 0xff */
829 static const unsigned char oneruns
[256] = {
830 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 - 0x0f */
831 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */
832 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 - 0x2f */
833 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */
834 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */
835 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */
836 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6f */
837 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7f */
838 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x80 - 0x8f */
839 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x90 - 0x9f */
840 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xa0 - 0xaf */
841 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xb0 - 0xbf */
842 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xc0 - 0xcf */
843 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xd0 - 0xdf */
844 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */
845 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, /* 0xf0 - 0xff */
849 * Find a span of ones or zeros using the supplied
850 * table. The ``base'' of the bit string is supplied
851 * along with the start+end bit indices.
853 static inline int32_t find0span(unsigned char *bp
, int32_t bs
, int32_t be
)
855 int32_t bits
= be
- bs
;
860 * Check partial byte on lhs.
862 if (bits
> 0 && (n
= (bs
& 7)) != 0)
864 span
= zeroruns
[(*bp
<< n
) & 0xff];
865 if (span
> 8 - n
) /* table value too generous */
867 if (span
> bits
) /* constrain span to bit range */
869 if (n
+ span
< 8) /* doesn't extend to edge of byte */
876 if (bits
>= (int32_t)(2 * 8 * sizeof(int64_t)))
880 * Align to int64_t boundary and check int64_t words.
882 while (!isAligned(bp
, int64_t))
885 return (span
+ zeroruns
[*bp
]);
891 while ((bits
>= (int32_t)(8 * sizeof(int64_t))) && (0 == *lp
))
893 span
+= 8 * sizeof(int64_t);
894 bits
-= 8 * sizeof(int64_t);
897 bp
= (unsigned char *)lp
;
900 * Scan full bytes for all 0's.
904 if (*bp
!= 0x00) /* end of run */
905 return (span
+ zeroruns
[*bp
]);
911 * Check partial byte on rhs.
916 span
+= (n
> bits
? bits
: n
);
921 static inline int32_t find1span(unsigned char *bp
, int32_t bs
, int32_t be
)
923 int32_t bits
= be
- bs
;
928 * Check partial byte on lhs.
930 if (bits
> 0 && (n
= (bs
& 7)) != 0)
932 span
= oneruns
[(*bp
<< n
) & 0xff];
933 if (span
> 8 - n
) /* table value too generous */
935 if (span
> bits
) /* constrain span to bit range */
937 if (n
+ span
< 8) /* doesn't extend to edge of byte */
944 if (bits
>= (int32_t)(2 * 8 * sizeof(int64_t)))
948 * Align to int64_t boundary and check int64_t words.
950 while (!isAligned(bp
, int64_t))
953 return (span
+ oneruns
[*bp
]);
959 while ((bits
>= (int32_t)(8 * sizeof(int64_t))) &&
960 (~((uint64_t)0) == (uint64_t)*lp
))
962 span
+= 8 * sizeof(int64_t);
963 bits
-= 8 * sizeof(int64_t);
966 bp
= (unsigned char *)lp
;
969 * Scan full bytes for all 1's.
973 if (*bp
!= 0xff) /* end of run */
974 return (span
+ oneruns
[*bp
]);
980 * Check partial byte on rhs.
985 span
+= (n
> bits
? bits
: n
);
991 * Return the offset of the next bit in the range
992 * [bs..be] that is different from the specified
993 * color. The end, be, is returned if no such bit
996 #define finddiff(_cp, _bs, _be, _color) \
997 (_bs + (_color ? find1span(_cp, _bs, _be) : find0span(_cp, _bs, _be)))
999 * Like finddiff, but also check the starting bit
1000 * against the end in case start > end.
1002 #define finddiff2(_cp, _bs, _be, _color) \
1003 (_bs < _be ? finddiff(_cp, _bs, _be, _color) : _be)
1006 * 1d-encode a row of pixels. The encoding is
1007 * a sequence of all-white or all-black spans
1008 * of pixels encoded with Huffman codes.
1010 static int Fax3Encode1DRow(TIFF
*tif
, unsigned char *bp
, uint32_t bits
)
1012 Fax3CodecState
*sp
= EncoderState(tif
);
1018 span
= find0span(bp
, bs
, bits
); /* white span */
1019 if (!putspan(tif
, span
, TIFFFaxWhiteCodes
))
1024 span
= find1span(bp
, bs
, bits
); /* black span */
1025 if (!putspan(tif
, span
, TIFFFaxBlackCodes
))
1031 if (sp
->b
.mode
& (FAXMODE_BYTEALIGN
| FAXMODE_WORDALIGN
))
1033 if (sp
->bit
!= 8) /* byte-align */
1034 Fax3FlushBits(tif
, sp
);
1035 if ((sp
->b
.mode
& FAXMODE_WORDALIGN
) &&
1036 !isAligned(tif
->tif_rawcp
, uint16_t))
1037 Fax3FlushBits(tif
, sp
);
1042 static const tableentry horizcode
= {3, 0x1, 0}; /* 001 */
1043 static const tableentry passcode
= {4, 0x1, 0}; /* 0001 */
1044 static const tableentry vcodes
[7] = {
1045 {7, 0x03, 0}, /* 0000 011 */
1046 {6, 0x03, 0}, /* 0000 11 */
1047 {3, 0x03, 0}, /* 011 */
1048 {1, 0x1, 0}, /* 1 */
1049 {3, 0x2, 0}, /* 010 */
1050 {6, 0x02, 0}, /* 0000 10 */
1051 {7, 0x02, 0} /* 0000 010 */
1055 * 2d-encode a row of pixels. Consult the CCITT
1056 * documentation for the algorithm.
1058 static int Fax3Encode2DRow(TIFF
*tif
, unsigned char *bp
, unsigned char *rp
,
1061 #define PIXEL(buf, ix) ((((buf)[(ix) >> 3]) >> (7 - ((ix)&7))) & 1)
1063 uint32_t a1
= (PIXEL(bp
, 0) != 0 ? 0 : finddiff(bp
, 0, bits
, 0));
1064 uint32_t b1
= (PIXEL(rp
, 0) != 0 ? 0 : finddiff(rp
, 0, bits
, 0));
1069 b2
= finddiff2(rp
, b1
, bits
, PIXEL(rp
, b1
));
1072 /* Naive computation triggers
1073 * -fsanitize=undefined,unsigned-integer-overflow */
1074 /* although it is correct unless the difference between both is < 31
1076 /* int32_t d = b1 - a1; */
1077 int32_t d
= (b1
>= a1
&& b1
- a1
<= 3U) ? (int32_t)(b1
- a1
)
1078 : (b1
< a1
&& a1
- b1
<= 3U) ? -(int32_t)(a1
- b1
)
1080 if (!(-3 <= d
&& d
<= 3))
1081 { /* horizontal mode */
1082 a2
= finddiff2(bp
, a1
, bits
, PIXEL(bp
, a1
));
1083 if (!putcode(tif
, &horizcode
))
1085 if (a0
+ a1
== 0 || PIXEL(bp
, a0
) == 0)
1087 if (!putspan(tif
, a1
- a0
, TIFFFaxWhiteCodes
))
1089 if (!putspan(tif
, a2
- a1
, TIFFFaxBlackCodes
))
1094 if (!putspan(tif
, a1
- a0
, TIFFFaxBlackCodes
))
1096 if (!putspan(tif
, a2
- a1
, TIFFFaxWhiteCodes
))
1102 { /* vertical mode */
1103 if (!putcode(tif
, &vcodes
[d
+ 3]))
1110 if (!putcode(tif
, &passcode
))
1116 a1
= finddiff(bp
, a0
, bits
, PIXEL(bp
, a0
));
1117 b1
= finddiff(rp
, a0
, bits
, !PIXEL(bp
, a0
));
1118 b1
= finddiff(rp
, b1
, bits
, PIXEL(bp
, a0
));
1125 * Encode a buffer of pixels.
1127 static int Fax3Encode(TIFF
*tif
, uint8_t *bp
, tmsize_t cc
, uint16_t s
)
1129 static const char module
[] = "Fax3Encode";
1130 Fax3CodecState
*sp
= EncoderState(tif
);
1132 if (cc
% sp
->b
.rowbytes
)
1134 TIFFErrorExtR(tif
, module
, "Fractional scanlines cannot be written");
1139 if ((sp
->b
.mode
& FAXMODE_NOEOL
) == 0)
1141 if (!Fax3PutEOL(tif
))
1144 if (is2DEncoding(sp
))
1146 if (sp
->tag
== G3_1D
)
1148 if (!Fax3Encode1DRow(tif
, bp
, sp
->b
.rowpixels
))
1154 if (!Fax3Encode2DRow(tif
, bp
, sp
->refline
, sp
->b
.rowpixels
))
1161 sp
->k
= sp
->maxk
- 1;
1164 _TIFFmemcpy(sp
->refline
, bp
, sp
->b
.rowbytes
);
1168 if (!Fax3Encode1DRow(tif
, bp
, sp
->b
.rowpixels
))
1171 bp
+= sp
->b
.rowbytes
;
1172 cc
-= sp
->b
.rowbytes
;
1177 static int Fax3PostEncode(TIFF
*tif
)
1179 Fax3CodecState
*sp
= EncoderState(tif
);
1182 Fax3FlushBits(tif
, sp
);
1186 static int _Fax3Close(TIFF
*tif
)
1188 if ((Fax3State(tif
)->mode
& FAXMODE_NORTC
) == 0 && tif
->tif_rawcp
)
1190 Fax3CodecState
*sp
= EncoderState(tif
);
1191 unsigned int code
= EOL
;
1192 unsigned int length
= 12;
1195 if (is2DEncoding(sp
))
1197 code
= (code
<< 1) | (sp
->tag
== G3_1D
);
1200 for (i
= 0; i
< 6; i
++)
1201 Fax3PutBits(tif
, code
, length
);
1202 Fax3FlushBits(tif
, sp
);
1207 static void Fax3Close(TIFF
*tif
) { _Fax3Close(tif
); }
1209 static void Fax3Cleanup(TIFF
*tif
)
1211 Fax3CodecState
*sp
= DecoderState(tif
);
1215 tif
->tif_tagmethods
.vgetfield
= sp
->b
.vgetparent
;
1216 tif
->tif_tagmethods
.vsetfield
= sp
->b
.vsetparent
;
1217 tif
->tif_tagmethods
.printdir
= sp
->b
.printdir
;
1220 _TIFFfreeExt(tif
, sp
->runs
);
1222 _TIFFfreeExt(tif
, sp
->refline
);
1224 _TIFFfreeExt(tif
, tif
->tif_data
);
1225 tif
->tif_data
= NULL
;
1227 _TIFFSetDefaultCompressionState(tif
);
1230 #define FIELD_BADFAXLINES (FIELD_CODEC + 0)
1231 #define FIELD_CLEANFAXDATA (FIELD_CODEC + 1)
1232 #define FIELD_BADFAXRUN (FIELD_CODEC + 2)
1234 #define FIELD_OPTIONS (FIELD_CODEC + 7)
1236 static const TIFFField faxFields
[] = {
1237 {TIFFTAG_FAXMODE
, 0, 0, TIFF_ANY
, 0, TIFF_SETGET_INT
, TIFF_SETGET_UNDEFINED
,
1238 FIELD_PSEUDO
, FALSE
, FALSE
, "FaxMode", NULL
},
1239 {TIFFTAG_FAXFILLFUNC
, 0, 0, TIFF_ANY
, 0, TIFF_SETGET_OTHER
,
1240 TIFF_SETGET_UNDEFINED
, FIELD_PSEUDO
, FALSE
, FALSE
, "FaxFillFunc", NULL
},
1241 {TIFFTAG_BADFAXLINES
, 1, 1, TIFF_LONG
, 0, TIFF_SETGET_UINT32
,
1242 TIFF_SETGET_UINT32
, FIELD_BADFAXLINES
, TRUE
, FALSE
, "BadFaxLines", NULL
},
1243 {TIFFTAG_CLEANFAXDATA
, 1, 1, TIFF_SHORT
, 0, TIFF_SETGET_UINT16
,
1244 TIFF_SETGET_UINT16
, FIELD_CLEANFAXDATA
, TRUE
, FALSE
, "CleanFaxData", NULL
},
1245 {TIFFTAG_CONSECUTIVEBADFAXLINES
, 1, 1, TIFF_LONG
, 0, TIFF_SETGET_UINT32
,
1246 TIFF_SETGET_UINT32
, FIELD_BADFAXRUN
, TRUE
, FALSE
, "ConsecutiveBadFaxLines",
1248 static const TIFFField fax3Fields
[] = {
1249 {TIFFTAG_GROUP3OPTIONS
, 1, 1, TIFF_LONG
, 0, TIFF_SETGET_UINT32
,
1250 TIFF_SETGET_UINT32
, FIELD_OPTIONS
, FALSE
, FALSE
, "Group3Options", NULL
},
1252 static const TIFFField fax4Fields
[] = {
1253 {TIFFTAG_GROUP4OPTIONS
, 1, 1, TIFF_LONG
, 0, TIFF_SETGET_UINT32
,
1254 TIFF_SETGET_UINT32
, FIELD_OPTIONS
, FALSE
, FALSE
, "Group4Options", NULL
},
1257 static int Fax3VSetField(TIFF
*tif
, uint32_t tag
, va_list ap
)
1259 Fax3BaseState
*sp
= Fax3State(tif
);
1260 const TIFFField
*fip
;
1263 assert(sp
->vsetparent
!= 0);
1267 case TIFFTAG_FAXMODE
:
1268 sp
->mode
= (int)va_arg(ap
, int);
1269 return 1; /* NB: pseudo tag */
1270 case TIFFTAG_FAXFILLFUNC
:
1271 DecoderState(tif
)->fill
= va_arg(ap
, TIFFFaxFillFunc
);
1272 return 1; /* NB: pseudo tag */
1273 case TIFFTAG_GROUP3OPTIONS
:
1274 /* XXX: avoid reading options if compression mismatches. */
1275 if (tif
->tif_dir
.td_compression
== COMPRESSION_CCITTFAX3
)
1276 sp
->groupoptions
= (uint32_t)va_arg(ap
, uint32_t);
1278 case TIFFTAG_GROUP4OPTIONS
:
1279 /* XXX: avoid reading options if compression mismatches. */
1280 if (tif
->tif_dir
.td_compression
== COMPRESSION_CCITTFAX4
)
1281 sp
->groupoptions
= (uint32_t)va_arg(ap
, uint32_t);
1283 case TIFFTAG_BADFAXLINES
:
1284 sp
->badfaxlines
= (uint32_t)va_arg(ap
, uint32_t);
1286 case TIFFTAG_CLEANFAXDATA
:
1287 sp
->cleanfaxdata
= (uint16_t)va_arg(ap
, uint16_vap
);
1289 case TIFFTAG_CONSECUTIVEBADFAXLINES
:
1290 sp
->badfaxrun
= (uint32_t)va_arg(ap
, uint32_t);
1293 return (*sp
->vsetparent
)(tif
, tag
, ap
);
1296 if ((fip
= TIFFFieldWithTag(tif
, tag
)) != NULL
)
1297 TIFFSetFieldBit(tif
, fip
->field_bit
);
1301 tif
->tif_flags
|= TIFF_DIRTYDIRECT
;
1305 static int Fax3VGetField(TIFF
*tif
, uint32_t tag
, va_list ap
)
1307 Fax3BaseState
*sp
= Fax3State(tif
);
1313 case TIFFTAG_FAXMODE
:
1314 *va_arg(ap
, int *) = sp
->mode
;
1316 case TIFFTAG_FAXFILLFUNC
:
1317 *va_arg(ap
, TIFFFaxFillFunc
*) = DecoderState(tif
)->fill
;
1319 case TIFFTAG_GROUP3OPTIONS
:
1320 case TIFFTAG_GROUP4OPTIONS
:
1321 *va_arg(ap
, uint32_t *) = sp
->groupoptions
;
1323 case TIFFTAG_BADFAXLINES
:
1324 *va_arg(ap
, uint32_t *) = sp
->badfaxlines
;
1326 case TIFFTAG_CLEANFAXDATA
:
1327 *va_arg(ap
, uint16_t *) = sp
->cleanfaxdata
;
1329 case TIFFTAG_CONSECUTIVEBADFAXLINES
:
1330 *va_arg(ap
, uint32_t *) = sp
->badfaxrun
;
1333 return (*sp
->vgetparent
)(tif
, tag
, ap
);
1338 static void Fax3PrintDir(TIFF
*tif
, FILE *fd
, long flags
)
1340 Fax3BaseState
*sp
= Fax3State(tif
);
1345 if (TIFFFieldSet(tif
, FIELD_OPTIONS
))
1347 const char *sep
= " ";
1348 if (tif
->tif_dir
.td_compression
== COMPRESSION_CCITTFAX4
)
1350 fprintf(fd
, " Group 4 Options:");
1351 if (sp
->groupoptions
& GROUP4OPT_UNCOMPRESSED
)
1352 fprintf(fd
, "%suncompressed data", sep
);
1357 fprintf(fd
, " Group 3 Options:");
1358 if (sp
->groupoptions
& GROUP3OPT_2DENCODING
)
1360 fprintf(fd
, "%s2-d encoding", sep
);
1363 if (sp
->groupoptions
& GROUP3OPT_FILLBITS
)
1365 fprintf(fd
, "%sEOL padding", sep
);
1368 if (sp
->groupoptions
& GROUP3OPT_UNCOMPRESSED
)
1369 fprintf(fd
, "%suncompressed data", sep
);
1371 fprintf(fd
, " (%" PRIu32
" = 0x%" PRIx32
")\n", sp
->groupoptions
,
1374 if (TIFFFieldSet(tif
, FIELD_CLEANFAXDATA
))
1376 fprintf(fd
, " Fax Data:");
1377 switch (sp
->cleanfaxdata
)
1379 case CLEANFAXDATA_CLEAN
:
1380 fprintf(fd
, " clean");
1382 case CLEANFAXDATA_REGENERATED
:
1383 fprintf(fd
, " receiver regenerated");
1385 case CLEANFAXDATA_UNCLEAN
:
1386 fprintf(fd
, " uncorrected errors");
1389 fprintf(fd
, " (%" PRIu16
" = 0x%" PRIx16
")\n", sp
->cleanfaxdata
,
1392 if (TIFFFieldSet(tif
, FIELD_BADFAXLINES
))
1393 fprintf(fd
, " Bad Fax Lines: %" PRIu32
"\n", sp
->badfaxlines
);
1394 if (TIFFFieldSet(tif
, FIELD_BADFAXRUN
))
1395 fprintf(fd
, " Consecutive Bad Fax Lines: %" PRIu32
"\n",
1398 (*sp
->printdir
)(tif
, fd
, flags
);
1401 static int InitCCITTFax3(TIFF
*tif
)
1403 static const char module
[] = "InitCCITTFax3";
1407 * Merge codec-specific tag information.
1409 if (!_TIFFMergeFields(tif
, faxFields
, TIFFArrayCount(faxFields
)))
1411 TIFFErrorExtR(tif
, "InitCCITTFax3",
1412 "Merging common CCITT Fax codec-specific tags failed");
1417 * Allocate state block so tag methods have storage to record values.
1419 tif
->tif_data
= (uint8_t *)_TIFFmallocExt(tif
, sizeof(Fax3CodecState
));
1421 if (tif
->tif_data
== NULL
)
1423 TIFFErrorExtR(tif
, module
, "No space for state block");
1426 _TIFFmemset(tif
->tif_data
, 0, sizeof(Fax3CodecState
));
1428 sp
= Fax3State(tif
);
1429 sp
->rw_mode
= tif
->tif_mode
;
1432 * Override parent get/set field methods.
1434 sp
->vgetparent
= tif
->tif_tagmethods
.vgetfield
;
1435 tif
->tif_tagmethods
.vgetfield
= Fax3VGetField
; /* hook for codec tags */
1436 sp
->vsetparent
= tif
->tif_tagmethods
.vsetfield
;
1437 tif
->tif_tagmethods
.vsetfield
= Fax3VSetField
; /* hook for codec tags */
1438 sp
->printdir
= tif
->tif_tagmethods
.printdir
;
1439 tif
->tif_tagmethods
.printdir
= Fax3PrintDir
; /* hook for codec tags */
1440 sp
->groupoptions
= 0;
1442 if (sp
->rw_mode
== O_RDONLY
) /* FIXME: improve for in place update */
1443 tif
->tif_flags
|= TIFF_NOBITREV
; /* decoder does bit reversal */
1444 DecoderState(tif
)->runs
= NULL
;
1445 TIFFSetField(tif
, TIFFTAG_FAXFILLFUNC
, _TIFFFax3fillruns
);
1446 EncoderState(tif
)->refline
= NULL
;
1449 * Install codec methods.
1451 tif
->tif_fixuptags
= Fax3FixupTags
;
1452 tif
->tif_setupdecode
= Fax3SetupState
;
1453 tif
->tif_predecode
= Fax3PreDecode
;
1454 tif
->tif_decoderow
= Fax3Decode1D
;
1455 tif
->tif_decodestrip
= Fax3Decode1D
;
1456 tif
->tif_decodetile
= Fax3Decode1D
;
1457 tif
->tif_setupencode
= Fax3SetupState
;
1458 tif
->tif_preencode
= Fax3PreEncode
;
1459 tif
->tif_postencode
= Fax3PostEncode
;
1460 tif
->tif_encoderow
= Fax3Encode
;
1461 tif
->tif_encodestrip
= Fax3Encode
;
1462 tif
->tif_encodetile
= Fax3Encode
;
1463 tif
->tif_close
= Fax3Close
;
1464 tif
->tif_cleanup
= Fax3Cleanup
;
1469 int TIFFInitCCITTFax3(TIFF
*tif
, int scheme
)
1472 if (InitCCITTFax3(tif
))
1475 * Merge codec-specific tag information.
1477 if (!_TIFFMergeFields(tif
, fax3Fields
, TIFFArrayCount(fax3Fields
)))
1479 TIFFErrorExtR(tif
, "TIFFInitCCITTFax3",
1480 "Merging CCITT Fax 3 codec-specific tags failed");
1485 * The default format is Class/F-style w/o RTC.
1487 return TIFFSetField(tif
, TIFFTAG_FAXMODE
, FAXMODE_CLASSF
);
1494 * CCITT Group 4 (T.6) Facsimile-compatible
1495 * Compression Scheme Support.
1498 #define SWAP(t, a, b) \
1506 * Decode the requested amount of G4-encoded data.
1508 static int Fax4Decode(TIFF
*tif
, uint8_t *buf
, tmsize_t occ
, uint16_t s
)
1510 DECLARE_STATE_2D(tif
, sp
, "Fax4Decode");
1512 if (occ
% sp
->b
.rowbytes
)
1514 TIFFErrorExtR(tif
, module
, "Fractional scanlines cannot be read");
1517 CACHE_STATE(tif
, sp
);
1522 pa
= thisrun
= sp
->curruns
;
1526 printf("\nBitAcc=%08" PRIX32
", BitsAvail = %d\n", BitAcc
, BitsAvail
);
1527 printf("-------------------- %d\n", tif
->tif_row
);
1533 if (((lastx
+ 7) >> 3) > (int)occ
) /* check for buffer overrun */
1535 TIFFErrorExtR(tif
, module
,
1536 "Buffer overrun detected : %" TIFF_SSIZE_FORMAT
1537 " bytes available, %d bits needed",
1541 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1542 SETVALUE(0); /* imaginary change for reference */
1543 SWAP(uint32_t *, sp
->curruns
, sp
->refruns
);
1544 buf
+= sp
->b
.rowbytes
;
1545 occ
-= sp
->b
.rowbytes
;
1549 NeedBits16(13, BADG4
);
1552 if (GetBits(13) != 0x1001)
1553 fputs("Bad EOFB\n", stderr
);
1556 if (((lastx
+ 7) >> 3) > (int)occ
) /* check for buffer overrun */
1558 TIFFErrorExtR(tif
, module
,
1559 "Buffer overrun detected : %" TIFF_SSIZE_FORMAT
1560 " bytes available, %d bits needed",
1564 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1565 UNCACHE_STATE(tif
, sp
);
1566 return (sp
->line
? 1 : -1); /* don't error on badly-terminated strips */
1568 UNCACHE_STATE(tif
, sp
);
1574 * Encode the requested amount of data.
1576 static int Fax4Encode(TIFF
*tif
, uint8_t *bp
, tmsize_t cc
, uint16_t s
)
1578 static const char module
[] = "Fax4Encode";
1579 Fax3CodecState
*sp
= EncoderState(tif
);
1581 if (cc
% sp
->b
.rowbytes
)
1583 TIFFErrorExtR(tif
, module
, "Fractional scanlines cannot be written");
1588 if (!Fax3Encode2DRow(tif
, bp
, sp
->refline
, sp
->b
.rowpixels
))
1590 _TIFFmemcpy(sp
->refline
, bp
, sp
->b
.rowbytes
);
1591 bp
+= sp
->b
.rowbytes
;
1592 cc
-= sp
->b
.rowbytes
;
1597 static int Fax4PostEncode(TIFF
*tif
)
1599 Fax3CodecState
*sp
= EncoderState(tif
);
1601 /* terminate strip w/ EOFB */
1602 Fax3PutBits(tif
, EOL
, 12);
1603 Fax3PutBits(tif
, EOL
, 12);
1605 Fax3FlushBits(tif
, sp
);
1609 int TIFFInitCCITTFax4(TIFF
*tif
, int scheme
)
1612 if (InitCCITTFax3(tif
))
1613 { /* reuse G3 support */
1615 * Merge codec-specific tag information.
1617 if (!_TIFFMergeFields(tif
, fax4Fields
, TIFFArrayCount(fax4Fields
)))
1619 TIFFErrorExtR(tif
, "TIFFInitCCITTFax4",
1620 "Merging CCITT Fax 4 codec-specific tags failed");
1624 tif
->tif_decoderow
= Fax4Decode
;
1625 tif
->tif_decodestrip
= Fax4Decode
;
1626 tif
->tif_decodetile
= Fax4Decode
;
1627 tif
->tif_encoderow
= Fax4Encode
;
1628 tif
->tif_encodestrip
= Fax4Encode
;
1629 tif
->tif_encodetile
= Fax4Encode
;
1630 tif
->tif_postencode
= Fax4PostEncode
;
1632 * Suppress RTC at the end of each strip.
1634 return TIFFSetField(tif
, TIFFTAG_FAXMODE
, FAXMODE_NORTC
);
1641 * CCITT Group 3 1-D Modified Huffman RLE Compression Support.
1642 * (Compression algorithms 2 and 32771)
1646 * Decode the requested amount of RLE-encoded data.
1648 static int Fax3DecodeRLE(TIFF
*tif
, uint8_t *buf
, tmsize_t occ
, uint16_t s
)
1650 DECLARE_STATE(tif
, sp
, "Fax3DecodeRLE");
1651 int mode
= sp
->b
.mode
;
1653 if (occ
% sp
->b
.rowbytes
)
1655 TIFFErrorExtR(tif
, module
, "Fractional scanlines cannot be read");
1658 CACHE_STATE(tif
, sp
);
1659 thisrun
= sp
->curruns
;
1666 printf("\nBitAcc=%08" PRIX32
", BitsAvail = %d\n", BitAcc
, BitsAvail
);
1667 printf("-------------------- %" PRIu32
"\n", tif
->tif_row
);
1671 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1673 * Cleanup at the end of the row.
1675 if (mode
& FAXMODE_BYTEALIGN
)
1677 int n
= BitsAvail
- (BitsAvail
& ~7);
1680 else if (mode
& FAXMODE_WORDALIGN
)
1682 int n
= BitsAvail
- (BitsAvail
& ~15);
1684 if (BitsAvail
== 0 && !isAligned(cp
, uint16_t))
1687 buf
+= sp
->b
.rowbytes
;
1688 occ
-= sp
->b
.rowbytes
;
1691 EOFRLE
: /* premature EOF */
1692 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1693 UNCACHE_STATE(tif
, sp
);
1696 UNCACHE_STATE(tif
, sp
);
1700 int TIFFInitCCITTRLE(TIFF
*tif
, int scheme
)
1703 if (InitCCITTFax3(tif
))
1704 { /* reuse G3 support */
1705 tif
->tif_decoderow
= Fax3DecodeRLE
;
1706 tif
->tif_decodestrip
= Fax3DecodeRLE
;
1707 tif
->tif_decodetile
= Fax3DecodeRLE
;
1709 * Suppress RTC+EOLs when encoding and byte-align data.
1711 return TIFFSetField(tif
, TIFFTAG_FAXMODE
,
1712 FAXMODE_NORTC
| FAXMODE_NOEOL
| FAXMODE_BYTEALIGN
);
1718 int TIFFInitCCITTRLEW(TIFF
*tif
, int scheme
)
1721 if (InitCCITTFax3(tif
))
1722 { /* reuse G3 support */
1723 tif
->tif_decoderow
= Fax3DecodeRLE
;
1724 tif
->tif_decodestrip
= Fax3DecodeRLE
;
1725 tif
->tif_decodetile
= Fax3DecodeRLE
;
1727 * Suppress RTC+EOLs when encoding and word-align data.
1729 return TIFFSetField(tif
, TIFFTAG_FAXMODE
,
1730 FAXMODE_NORTC
| FAXMODE_NOEOL
| FAXMODE_WORDALIGN
);
1735 #endif /* CCITT_SUPPORT */