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 * NB: define PURIFY if you're using purify and you want
49 * to avoid some harmless array bounds complaints that
50 * can happen in the _TIFFFax3fillruns routine.
54 * Compression+decompression state blocks are
55 * derived from this ``base state'' block.
58 int mode
; /* operating mode */
59 uint32 rowbytes
; /* bytes in a decoded scanline */
60 uint32 rowpixels
; /* pixels in a scanline */
62 uint16 cleanfaxdata
; /* CleanFaxData tag */
63 uint32 badfaxrun
; /* BadFaxRun tag */
64 uint32 badfaxlines
; /* BadFaxLines tag */
65 uint32 groupoptions
; /* Group 3/4 options tag */
66 uint32 recvparams
; /* encoded Class 2 session params */
67 char* subaddress
; /* subaddress string */
68 uint32 recvtime
; /* time spent receiving (secs) */
69 TIFFVGetMethod vgetparent
; /* super-class method */
70 TIFFVSetMethod vsetparent
; /* super-class method */
72 #define Fax3State(tif) ((Fax3BaseState*) (tif)->tif_data)
76 const u_char
* bitmap
; /* bit reversal table */
77 uint32 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
* runs
; /* b&w runs for current/previous row */
82 uint32
* refruns
; /* runs for reference line */
83 uint32
* curruns
; /* runs for current line */
85 #define DecoderState(tif) ((Fax3DecodeState*) Fax3State(tif))
89 int data
; /* current i/o byte */
90 int bit
; /* current i/o bit in byte */
91 enum { G3_1D
, G3_2D
} tag
; /* encoding state */
92 u_char
* refline
; /* reference line for 2d decoding */
93 int k
; /* #rows left that can be 2d encoded */
94 int maxk
; /* max #rows that can be 2d encoded */
96 #define EncoderState(tif) ((Fax3EncodeState*) Fax3State(tif))
98 #define is2DEncoding(sp) \
99 (sp->b.groupoptions & GROUP3OPT_2DENCODING)
100 #define isAligned(p,t) ((((u_long)(p)) & (sizeof (t)-1)) == 0)
103 * Group 3 and Group 4 Decoding.
107 * These macros glue the TIFF library state to
108 * the state expected by Frank's decoder.
110 #define DECLARE_STATE(tif, sp, mod) \
111 static const char module[] = mod; \
112 Fax3DecodeState* sp = DecoderState(tif); \
113 int a0; /* reference element */ \
114 int lastx = sp->b.rowpixels; /* last element in row */ \
115 uint32 BitAcc; /* bit accumulator */ \
116 int BitsAvail; /* # valid bits in BitAcc */ \
117 int RunLength; /* length of current run */ \
118 u_char* cp; /* next byte of input data */ \
119 u_char* ep; /* end of input data */ \
120 uint32* pa; /* place to stuff next run */ \
121 uint32* thisrun; /* current row's run array */ \
122 int EOLcnt; /* # EOL codes recognized */ \
123 const u_char* bitmap = sp->bitmap; /* input data bit reverser */ \
124 const TIFFFaxTabEnt* TabEnt
125 #define DECLARE_STATE_2D(tif, sp, mod) \
126 DECLARE_STATE(tif, sp, mod); \
127 int b1; /* next change on prev line */ \
128 uint32* pb /* next run in reference line */\
130 * Load any state that may be changed during decoding.
132 #define CACHE_STATE(tif, sp) do { \
134 BitsAvail = sp->bit; \
135 EOLcnt = sp->EOLcnt; \
136 cp = (unsigned char*) tif->tif_rawcp; \
137 ep = cp + tif->tif_rawcc; \
140 * Save state possibly changed during decoding.
142 #define UNCACHE_STATE(tif, sp) do { \
143 sp->bit = BitsAvail; \
145 sp->EOLcnt = EOLcnt; \
146 tif->tif_rawcc -= (tidata_t) cp - tif->tif_rawcp; \
147 tif->tif_rawcp = (tidata_t) cp; \
151 * Setup state for decoding a strip.
154 Fax3PreDecode(TIFF
* tif
, tsample_t s
)
156 Fax3DecodeState
* sp
= DecoderState(tif
);
160 sp
->bit
= 0; /* force initial read */
162 sp
->EOLcnt
= 0; /* force initial scan for EOL */
164 * Decoder assumes lsb-to-msb bit order. Note that we select
165 * this here rather than in Fax3SetupState so that viewers can
166 * hold the image open, fiddle with the FillOrder tag value,
167 * and then re-decode the image. Otherwise they'd need to close
168 * and open the image to get the state reset.
171 TIFFGetBitRevTable(tif
->tif_dir
.td_fillorder
!= FILLORDER_LSB2MSB
);
172 if (sp
->refruns
) { /* init reference line to white */
173 sp
->refruns
[0] = sp
->b
.rowpixels
;
180 * Routine for handling various errors/conditions.
181 * Note how they are "glued into the decoder" by
182 * overriding the definitions used by the decoder.
185 static void LINKAGEMODE
186 Fax3Unexpected(const char* module, TIFF
* tif
, uint32 a0
)
188 TIFFError(module, "%s: Bad code word at scanline %d (x %lu)",
189 tif
->tif_name
, tif
->tif_row
, (u_long
) a0
);
191 #define unexpected(table, a0) Fax3Unexpected(module, tif, a0)
193 static void LINKAGEMODE
194 Fax3Extension(const char* module, TIFF
* tif
, uint32 a0
)
197 "%s: Uncompressed data (not supported) at scanline %d (x %lu)",
198 tif
->tif_name
, tif
->tif_row
, (u_long
) a0
);
200 #define extension(a0) Fax3Extension(module, tif, a0)
202 static void LINKAGEMODE
203 Fax3BadLength(const char* module, TIFF
* tif
, uint32 a0
, uint32 lastx
)
205 TIFFWarning(module, "%s: %s at scanline %d (got %lu, expected %lu)",
207 a0
< lastx
? "Premature EOL" : "Line length mismatch",
208 tif
->tif_row
, (u_long
) a0
, (u_long
) lastx
);
210 #define badlength(a0,lastx) Fax3BadLength(module, tif, a0, lastx)
212 static void LINKAGEMODE
213 Fax3PrematureEOF(const char* module, TIFF
* tif
, uint32 a0
)
215 TIFFWarning(module, "%s: Premature EOF at scanline %d (x %lu)",
216 tif
->tif_name
, tif
->tif_row
, (u_long
) a0
);
218 #define prematureEOF(a0) Fax3PrematureEOF(module, tif, a0)
223 * Decode the requested amount of G3 1D-encoded data.
225 static int LINKAGEMODE
226 Fax3Decode1D(TIFF
* tif
, tidata_t buf
, tsize_t occ
, tsample_t s
)
228 DECLARE_STATE(tif
, sp
, "Fax3Decode1D");
231 CACHE_STATE(tif
, sp
);
232 thisrun
= sp
->curruns
;
233 while ((long)occ
> 0) {
238 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc
, BitsAvail
);
239 printf("-------------------- %d\n", tif
->tif_row
);
244 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
245 buf
+= sp
->b
.rowbytes
;
246 occ
-= sp
->b
.rowbytes
;
250 EOF1D
: /* premature EOF */
252 EOF1Da
: /* premature EOF */
253 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
254 UNCACHE_STATE(tif
, sp
);
257 UNCACHE_STATE(tif
, sp
);
261 #define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }
263 * Decode the requested amount of G3 2D-encoded data.
265 static int LINKAGEMODE
266 Fax3Decode2D(TIFF
* tif
, tidata_t buf
, tsize_t occ
, tsample_t s
)
268 DECLARE_STATE_2D(tif
, sp
, "Fax3Decode2D");
269 int is1D
; /* current line is 1d/2d-encoded */
272 CACHE_STATE(tif
, sp
);
273 while ((long)occ
> 0) {
276 pa
= thisrun
= sp
->curruns
;
278 printf("\nBitAcc=%08X, BitsAvail = %d EOLcnt = %d",
279 BitAcc
, BitsAvail
, EOLcnt
);
283 is1D
= GetBits(1); /* 1D/2D-encoding tag bit */
286 printf(" %s\n-------------------- %d\n",
287 is1D
? "1D" : "2D", tif
->tif_row
);
296 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
297 SETVAL(0); /* imaginary change for reference */
298 SWAP(uint32
*, sp
->curruns
, sp
->refruns
);
299 buf
+= sp
->b
.rowbytes
;
300 occ
-= sp
->b
.rowbytes
;
304 EOF2D
: /* premature EOF */
306 EOF2Da
: /* premature EOF */
307 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
308 UNCACHE_STATE(tif
, sp
);
311 UNCACHE_STATE(tif
, sp
);
317 * The ZERO & FILL macros must handle spans < 2*sizeof(long) bytes.
318 * For machines with 64-bit longs this is <16 bytes; otherwise
319 * this is <8 bytes. We optimize the code here to reflect the
320 * machine characteristics.
322 #if defined(__alpha) || _MIPS_SZLONG == 64
323 #define FILL(n, cp) \
325 case 15:(cp)[14] = 0xff; case 14:(cp)[13] = 0xff; case 13: (cp)[12] = 0xff;\
326 case 12:(cp)[11] = 0xff; case 11:(cp)[10] = 0xff; case 10: (cp)[9] = 0xff;\
327 case 9: (cp)[8] = 0xff; case 8: (cp)[7] = 0xff; case 7: (cp)[6] = 0xff;\
328 case 6: (cp)[5] = 0xff; case 5: (cp)[4] = 0xff; case 4: (cp)[3] = 0xff;\
329 case 3: (cp)[2] = 0xff; case 2: (cp)[1] = 0xff; \
330 case 1: (cp)[0] = 0xff; (cp) += (n); case 0: ; \
332 #define ZERO(n, cp) \
334 case 15:(cp)[14] = 0; case 14:(cp)[13] = 0; case 13: (cp)[12] = 0; \
335 case 12:(cp)[11] = 0; case 11:(cp)[10] = 0; case 10: (cp)[9] = 0; \
336 case 9: (cp)[8] = 0; case 8: (cp)[7] = 0; case 7: (cp)[6] = 0; \
337 case 6: (cp)[5] = 0; case 5: (cp)[4] = 0; case 4: (cp)[3] = 0; \
338 case 3: (cp)[2] = 0; case 2: (cp)[1] = 0; \
339 case 1: (cp)[0] = 0; (cp) += (n); case 0: ; \
342 #define FILL(n, cp) \
344 case 7: (cp)[6] = 0xff; case 6: (cp)[5] = 0xff; case 5: (cp)[4] = 0xff; \
345 case 4: (cp)[3] = 0xff; case 3: (cp)[2] = 0xff; case 2: (cp)[1] = 0xff; \
346 case 1: (cp)[0] = 0xff; (cp) += (n); case 0: ; \
348 #define ZERO(n, cp) \
350 case 7: (cp)[6] = 0; case 6: (cp)[5] = 0; case 5: (cp)[4] = 0; \
351 case 4: (cp)[3] = 0; case 3: (cp)[2] = 0; case 2: (cp)[1] = 0; \
352 case 1: (cp)[0] = 0; (cp) += (n); case 0: ; \
357 * Bit-fill a row according to the white/black
358 * runs generated during G3/G4 decoding.
361 _TIFFFax3fillruns(u_char
* buf
, uint32
* runs
, uint32
* erun
, uint32 lastx
)
363 static const unsigned char _fillmasks
[] =
364 { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
373 for (; runs
< erun
; runs
+= 2) {
376 run
= runs
[0] = lastx
- x
;
381 if (bx
) { /* align to byte boundary */
382 *cp
++ &= 0xff << (8-bx
);
385 if( (n
= run
>> 3) != 0 ) { /* multiple bytes to fill */
386 if ((n
/sizeof (long)) > 1) {
388 * Align to longword boundary and fill.
390 for (; n
&& !isAligned(cp
, long); n
--)
393 nw
= (int32
)(n
/ sizeof (long));
394 n
-= nw
* sizeof (long);
405 cp
[0] &= 0xff >> run
;
407 cp
[0] &= 0xff >> run
;
410 cp
[0] &= ~(_fillmasks
[run
]>>bx
);
415 run
= runs
[1] = lastx
- x
;
420 if (bx
) { /* align to byte boundary */
424 if( (n
= run
>>3) != 0 ) { /* multiple bytes to fill */
425 if ((n
/sizeof (long)) > 1) {
427 * Align to longword boundary and fill.
429 for (; n
&& !isAligned(cp
, long); n
--)
432 nw
= (int32
)(n
/ sizeof (long));
433 n
-= nw
* sizeof (long);
444 cp
[0] |= 0xff00 >> run
;
446 cp
[0] |= 0xff00 >> run
;
449 cp
[0] |= _fillmasks
[run
]>>bx
;
459 * Setup G3/G4-related compression/decompression state
460 * before data is processed. This routine is called once
461 * per image -- it sets up different state based on whether
462 * or not decoding or encoding is being done and whether
463 * 1D- or 2D-encoded data is involved.
466 Fax3SetupState(TIFF
* tif
)
468 TIFFDirectory
* td
= &tif
->tif_dir
;
469 Fax3BaseState
* sp
= Fax3State(tif
);
470 long rowbytes
, rowpixels
;
473 if (td
->td_bitspersample
!= 1) {
474 TIFFError(tif
->tif_name
,
475 "Bits/sample must be 1 for Group 3/4 encoding/decoding");
479 * Calculate the scanline/tile widths.
482 rowbytes
= TIFFTileRowSize(tif
);
483 rowpixels
= td
->td_tilewidth
;
485 rowbytes
= TIFFScanlineSize(tif
);
486 rowpixels
= td
->td_imagewidth
;
488 sp
->rowbytes
= (uint32
) rowbytes
;
489 sp
->rowpixels
= (uint32
) rowpixels
;
491 * Allocate any additional space required for decoding/encoding.
494 (sp
->groupoptions
& GROUP3OPT_2DENCODING
) ||
495 td
->td_compression
== COMPRESSION_CCITTFAX4
497 if (tif
->tif_mode
== O_RDONLY
) { /* 1d/2d decoding */
498 Fax3DecodeState
* dsp
= DecoderState(tif
);
499 uint32 nruns
= needsRefLine
?
500 2*TIFFroundup(rowpixels
,32) : rowpixels
;
503 dsp
->runs
= (uint32
*) _TIFFmalloc(nruns
*sizeof (uint16
));
509 Decoding the file frle_bug.tif causes a crash (such as with tiff2rgba).
511 In particular the array dsp->runs allocated in Fax3SetupState() is overrun
512 by 4-8 bytes. This occurs when Fax3DecodeRLE() processes the first
513 scanline. The EXPAND1D() macro advances "pa" to be thisrun+512 (an
514 alias for dsp->runs), pointing just beyond the end of the array. Then
515 the call to _TIFFFax3fillruns() does an "*erun++ = 0;" which writes beyond
516 the end of the array.
518 In the short term I have modified the dsp->runs allocation to add eight
519 extra bytes to the runs buffer; however, I am only doing this because I
520 don't understand the algorithm well enough to change it without risking
521 more adverse side effects.
523 Frank Warmerdam (warmerda@home.com)
527 dsp
->runs
= (uint32
*) _TIFFmalloc(8+nruns
*sizeof (uint32
));
529 if (dsp
->runs
== NULL
) {
530 TIFFError("Fax3SetupState",
531 "%s: No space for Group 3/4 run arrays",
535 dsp
->curruns
= dsp
->runs
;
537 dsp
->refruns
= dsp
->runs
+ (nruns
>>1);
540 if (is2DEncoding(dsp
)) { /* NB: default is 1D routine */
541 tif
->tif_decoderow
= Fax3Decode2D
;
542 tif
->tif_decodestrip
= Fax3Decode2D
;
543 tif
->tif_decodetile
= Fax3Decode2D
;
545 } else if (needsRefLine
) { /* 2d encoding */
546 Fax3EncodeState
* esp
= EncoderState(tif
);
548 * 2d encoding requires a scanline
549 * buffer for the ``reference line''; the
550 * scanline against which delta encoding
551 * is referenced. The reference line must
552 * be initialized to be ``white'' (done elsewhere).
554 esp
->refline
= (u_char
*) _TIFFmalloc(rowbytes
);
555 if (esp
->refline
== NULL
) {
556 TIFFError("Fax3SetupState",
557 "%s: No space for Group 3/4 reference line",
561 } else /* 1d encoding */
562 EncoderState(tif
)->refline
= NULL
;
567 * CCITT Group 3 FAX Encoding.
570 #define Fax3FlushBits(tif, sp) { \
571 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
572 (void) TIFFFlushData1(tif); \
573 *(tif)->tif_rawcp++ = (sp)->data; \
574 (tif)->tif_rawcc++; \
575 (sp)->data = 0, (sp)->bit = 8; \
577 #define _FlushBits(tif) { \
578 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \
579 (void) TIFFFlushData1(tif); \
580 *(tif)->tif_rawcp++ = data; \
581 (tif)->tif_rawcc++; \
584 static const int _msbmask
[9] =
585 { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
586 #define _PutBits(tif, bits, length) { \
587 while (length > bit) { \
588 data |= bits >> (length - bit); \
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.
603 static void LINKAGEMODE
604 Fax3PutBits(TIFF
* tif
, u_int bits
, u_int length
)
606 Fax3EncodeState
* sp
= EncoderState(tif
);
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.
638 static void LINKAGEMODE
639 putspan(TIFF
* tif
, int32 span
, const tableentry
* tab
)
641 Fax3EncodeState
* sp
= EncoderState(tif
);
646 while (span
>= 2624) {
647 const tableentry
* te
= &tab
[63 + (2560>>6)];
648 code
= te
->code
, length
= te
->length
;
650 DEBUG_PRINT("MakeUp", te
->runlen
);
652 _PutBits(tif
, code
, length
);
656 const tableentry
* te
= &tab
[63 + (span
>>6)];
657 assert(te
->runlen
== 64*(span
>>6));
658 code
= te
->code
, length
= te
->length
;
660 DEBUG_PRINT("MakeUp", te
->runlen
);
662 _PutBits(tif
, code
, length
);
665 code
= tab
[span
].code
, length
= tab
[span
].length
;
667 DEBUG_PRINT(" Term", tab
[span
].runlen
);
669 _PutBits(tif
, code
, length
);
676 * Write an EOL code to the output stream. The zero-fill
677 * logic for byte-aligning encoded scanlines is handled
678 * here. We also handle writing the tag bit for the next
679 * scanline when doing 2d encoding.
681 static void LINKAGEMODE
682 Fax3PutEOL(TIFF
* tif
)
684 Fax3EncodeState
* sp
= EncoderState(tif
);
689 if (sp
->b
.groupoptions
& GROUP3OPT_FILLBITS
) {
691 * Force bit alignment so EOL will terminate on
692 * a byte boundary. That is, force the bit alignment
693 * to 16-12 = 4 before putting out the EOL code.
696 if (align
!= sp
->bit
) {
698 align
= sp
->bit
+ (8 - align
);
700 align
= sp
->bit
- align
;
702 _PutBits(tif
, 0, align
);
705 code
= EOL
, length
= 12;
706 if (is2DEncoding(sp
))
707 #if defined(__VISAGECPP30__)
708 /* VA 3.0 is just plain wierd. */
709 code
= (code
<<1) | (sp
->tag
== Fax3EncodeState::G3_1D
), length
++;
711 code
= (code
<<1) | (sp
->tag
== G3_1D
), length
++;
713 _PutBits(tif
, code
, length
);
720 * Reset encoding state at the start of a strip.
723 Fax3PreEncode(TIFF
* tif
, tsample_t s
)
725 Fax3EncodeState
* sp
= EncoderState(tif
);
731 #if defined(__VISAGECPP30__)
732 /* VA 3.0 is just plain wierd. */
733 sp
->tag
= Fax3EncodeState::G3_1D
;
738 * This is necessary for Group 4; otherwise it isn't
739 * needed because the first scanline of each strip ends
740 * up being copied into the refline.
743 _TIFFmemset(sp
->refline
, 0x00, sp
->b
.rowbytes
);
744 if (is2DEncoding(sp
)) {
745 float res
= tif
->tif_dir
.td_yresolution
;
747 * The CCITT spec says that when doing 2d encoding, you
748 * should only do it on K consecutive scanlines, where K
749 * depends on the resolution of the image being encoded
750 * (2 for <= 200 lpi, 4 for > 200 lpi). Since the directory
751 * code initializes td_yresolution to 0, this code will
752 * select a K of 2 unless the YResolution tag is set
753 * appropriately. (Note also that we fudge a little here
754 * and use 150 lpi to avoid problems with units conversion.)
756 if (tif
->tif_dir
.td_resolutionunit
== RESUNIT_CENTIMETER
)
757 res
*= 2.54f
; /* convert to inches */
758 sp
->maxk
= (res
> 150 ? 4 : 2);
761 sp
->k
= sp
->maxk
= 0;
765 static const u_char zeroruns
[256] = {
766 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0f */
767 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */
768 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2f */
769 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3f */
770 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */
771 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5f */
772 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */
773 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7f */
774 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8f */
775 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9f */
776 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 - 0xaf */
777 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 - 0xbf */
778 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 - 0xcf */
779 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 - 0xdf */
780 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 - 0xef */
781 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xf0 - 0xff */
783 static const u_char oneruns
[256] = {
784 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 - 0x0f */
785 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */
786 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 - 0x2f */
787 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */
788 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */
789 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */
790 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6f */
791 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7f */
792 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x80 - 0x8f */
793 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x90 - 0x9f */
794 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xa0 - 0xaf */
795 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xb0 - 0xbf */
796 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xc0 - 0xcf */
797 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xd0 - 0xdf */
798 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */
799 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, /* 0xf0 - 0xff */
803 * On certain systems it pays to inline
804 * the routines that find pixel spans.
807 static int32
find0span(u_char
*, int32
, int32
);
808 static int32
find1span(u_char
*, int32
, int32
);
809 #pragma inline(find0span,find1span)
813 * Find a span of ones or zeros using the supplied
814 * table. The ``base'' of the bit string is supplied
815 * along with the start+end bit indices.
817 INLINE
static int32 LINKAGEMODE
818 find0span(u_char
* bp
, int32 bs
, int32 be
)
820 int32 bits
= be
- bs
;
825 * Check partial byte on lhs.
827 if (bits
> 0 && (n
= (bs
& 7))) {
828 span
= zeroruns
[(*bp
<< n
) & 0xff];
829 if (span
> 8-n
) /* table value too generous */
831 if (span
> bits
) /* constrain span to bit range */
833 if (n
+span
< 8) /* doesn't extend to edge of byte */
839 if (bits
>= 2*8*sizeof (long)) {
842 * Align to longword boundary and check longwords.
844 while (!isAligned(bp
, long)) {
846 return (span
+ zeroruns
[*bp
]);
847 span
+= 8, bits
-= 8;
851 while (bits
>= 8*sizeof (long) && *lp
== 0) {
852 span
+= 8*sizeof (long), bits
-= 8*sizeof (long);
858 * Scan full bytes for all 0's.
861 if (*bp
!= 0x00) /* end of run */
862 return (span
+ zeroruns
[*bp
]);
863 span
+= 8, bits
-= 8;
867 * Check partial byte on rhs.
871 span
+= (n
> bits
? bits
: n
);
876 INLINE
static int32 LINKAGEMODE
877 find1span(u_char
* bp
, int32 bs
, int32 be
)
879 int32 bits
= be
- bs
;
884 * Check partial byte on lhs.
886 if (bits
> 0 && (n
= (bs
& 7))) {
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
>= 2*8*sizeof (long)) {
901 * Align to longword boundary and check longwords.
903 while (!isAligned(bp
, long)) {
905 return (span
+ oneruns
[*bp
]);
906 span
+= 8, bits
-= 8;
910 while (bits
>= 8*sizeof (long) && *lp
== ~0) {
911 span
+= 8*sizeof (long), bits
-= 8*sizeof (long);
917 * Scan full bytes for all 1's.
920 if (*bp
!= 0xff) /* end of run */
921 return (span
+ oneruns
[*bp
]);
922 span
+= 8, bits
-= 8;
926 * Check partial byte on rhs.
930 span
+= (n
> bits
? bits
: n
);
936 * Return the offset of the next bit in the range
937 * [bs..be] that is different from the specified
938 * color. The end, be, is returned if no such bit
941 #define finddiff(_cp, _bs, _be, _color) \
942 (_bs + (_color ? find1span(_cp,_bs,_be) : find0span(_cp,_bs,_be)))
944 * Like finddiff, but also check the starting bit
945 * against the end in case start > end.
947 #define finddiff2(_cp, _bs, _be, _color) \
948 (_bs < _be ? finddiff(_cp,_bs,_be,_color) : _be)
951 * 1d-encode a row of pixels. The encoding is
952 * a sequence of all-white or all-black spans
953 * of pixels encoded with Huffman codes.
955 static int LINKAGEMODE
956 Fax3Encode1DRow(TIFF
* tif
, u_char
* bp
, uint32 bits
)
958 Fax3EncodeState
* sp
= EncoderState(tif
);
962 span
= find0span(bp
, bs
, bits
); /* white span */
963 putspan(tif
, span
, TIFFFaxWhiteCodes
);
967 span
= find1span(bp
, bs
, bits
); /* black span */
968 putspan(tif
, span
, TIFFFaxBlackCodes
);
973 if (sp
->b
.mode
& (FAXMODE_BYTEALIGN
|FAXMODE_WORDALIGN
)) {
974 if (sp
->bit
!= 8) /* byte-align */
975 Fax3FlushBits(tif
, sp
);
976 if ((sp
->b
.mode
&FAXMODE_WORDALIGN
) &&
977 !isAligned(tif
->tif_rawcp
, uint16
))
978 Fax3FlushBits(tif
, sp
);
983 static const tableentry horizcode
=
984 { 3, 0x1 }; /* 001 */
985 static const tableentry passcode
=
986 { 4, 0x1 }; /* 0001 */
987 static const tableentry vcodes
[7] = {
988 { 7, 0x03 }, /* 0000 011 */
989 { 6, 0x03 }, /* 0000 11 */
990 { 3, 0x03 }, /* 011 */
992 { 3, 0x2 }, /* 010 */
993 { 6, 0x02 }, /* 0000 10 */
994 { 7, 0x02 } /* 0000 010 */
998 * 2d-encode a row of pixels. Consult the CCITT
999 * documentation for the algorithm.
1001 static int LINKAGEMODE
1002 Fax3Encode2DRow(TIFF
* tif
, u_char
* bp
, u_char
* rp
, uint32 bits
)
1004 #define PIXEL(buf,ix) ((((buf)[(ix)>>3]) >> (7-((ix)&7))) & 1)
1006 int32 a1
= (PIXEL(bp
, 0) != 0 ? 0 : finddiff(bp
, 0, bits
, 0));
1007 int32 b1
= (PIXEL(rp
, 0) != 0 ? 0 : finddiff(rp
, 0, bits
, 0));
1011 b2
= finddiff2(rp
, b1
, bits
, PIXEL(rp
,b1
));
1014 if (!(-3 <= d
&& d
<= 3)) { /* horizontal mode */
1015 a2
= finddiff2(bp
, a1
, bits
, PIXEL(bp
,a1
));
1016 putcode(tif
, &horizcode
);
1017 if (a0
+a1
== 0 || PIXEL(bp
, a0
) == 0) {
1018 putspan(tif
, a1
-a0
, TIFFFaxWhiteCodes
);
1019 putspan(tif
, a2
-a1
, TIFFFaxBlackCodes
);
1021 putspan(tif
, a1
-a0
, TIFFFaxBlackCodes
);
1022 putspan(tif
, a2
-a1
, TIFFFaxWhiteCodes
);
1025 } else { /* vertical mode */
1026 putcode(tif
, &vcodes
[d
+3]);
1029 } else { /* pass mode */
1030 putcode(tif
, &passcode
);
1035 a1
= finddiff(bp
, a0
, bits
, PIXEL(bp
,a0
));
1036 b1
= finddiff(rp
, a0
, bits
, !PIXEL(bp
,a0
));
1037 b1
= finddiff(rp
, b1
, bits
, PIXEL(bp
,a0
));
1044 * Encode a buffer of pixels.
1046 static int LINKAGEMODE
1047 Fax3Encode(TIFF
* tif
, tidata_t bp
, tsize_t cc
, tsample_t s
)
1049 Fax3EncodeState
* sp
= EncoderState(tif
);
1052 while ((long)cc
> 0) {
1053 if ((sp
->b
.mode
& FAXMODE_NOEOL
) == 0)
1055 if (is2DEncoding(sp
)) {
1056 #if defined(__VISAGECPP30__)
1057 /* VA 3.0 is just plain wierd. */
1058 if (sp
->tag
== Fax3EncodeState::G3_1D
) {
1060 if (sp
->tag
== G3_1D
) {
1062 if (!Fax3Encode1DRow(tif
, bp
, sp
->b
.rowpixels
))
1064 #if defined(__VISAGECPP30__)
1065 /* VA 3.0 is just plain wierd. */
1066 sp
->tag
= Fax3EncodeState::G3_2D
;
1071 if (!Fax3Encode2DRow(tif
, bp
, sp
->refline
, sp
->b
.rowpixels
))
1076 #if defined(__VISAGECPP30__)
1077 /* VA 3.0 is just plain wierd. */
1078 sp
->tag
= Fax3EncodeState::G3_1D
;
1084 _TIFFmemcpy(sp
->refline
, bp
, sp
->b
.rowbytes
);
1086 if (!Fax3Encode1DRow(tif
, bp
, sp
->b
.rowpixels
))
1089 bp
+= sp
->b
.rowbytes
;
1090 cc
-= sp
->b
.rowbytes
;
1098 Fax3PostEncode(TIFF
* tif
)
1100 Fax3EncodeState
* sp
= EncoderState(tif
);
1103 Fax3FlushBits(tif
, sp
);
1108 Fax3Close(TIFF
* tif
)
1110 if ((Fax3State(tif
)->mode
& FAXMODE_NORTC
) == 0) {
1111 Fax3EncodeState
* sp
= EncoderState(tif
);
1116 if (is2DEncoding(sp
))
1117 #if defined(__VISAGECPP30__)
1118 /* VA 3.0 is just plain wierd. */
1119 code
= (code
<<1) | (sp
->tag
== Fax3EncodeState::G3_1D
), length
++;
1121 code
= (code
<<1) | (sp
->tag
== G3_1D
), length
++;
1123 for (i
= 0; i
< 6; i
++)
1124 Fax3PutBits(tif
, code
, length
);
1125 Fax3FlushBits(tif
, sp
);
1130 Fax3Cleanup(TIFF
* tif
)
1132 if (tif
->tif_data
) {
1133 if (tif
->tif_mode
== O_RDONLY
) {
1134 Fax3DecodeState
* sp
= DecoderState(tif
);
1136 _TIFFfree(sp
->runs
);
1138 Fax3EncodeState
* sp
= EncoderState(tif
);
1140 _TIFFfree(sp
->refline
);
1142 if (Fax3State(tif
)->subaddress
)
1143 _TIFFfree(Fax3State(tif
)->subaddress
);
1144 _TIFFfree(tif
->tif_data
);
1145 tif
->tif_data
= NULL
;
1149 #define FIELD_BADFAXLINES (FIELD_CODEC+0)
1150 #define FIELD_CLEANFAXDATA (FIELD_CODEC+1)
1151 #define FIELD_BADFAXRUN (FIELD_CODEC+2)
1152 #define FIELD_RECVPARAMS (FIELD_CODEC+3)
1153 #define FIELD_SUBADDRESS (FIELD_CODEC+4)
1154 #define FIELD_RECVTIME (FIELD_CODEC+5)
1156 #define FIELD_OPTIONS (FIELD_CODEC+6)
1158 static const TIFFFieldInfo faxFieldInfo
[] = {
1159 { TIFFTAG_FAXMODE
, 0, 0, TIFF_ANY
, FIELD_PSEUDO
,
1160 FALSE
, FALSE
, "FaxMode" },
1161 { TIFFTAG_FAXFILLFUNC
, 0, 0, TIFF_ANY
, FIELD_PSEUDO
,
1162 FALSE
, FALSE
, "FaxFillFunc" },
1163 { TIFFTAG_BADFAXLINES
, 1, 1, TIFF_LONG
, FIELD_BADFAXLINES
,
1164 TRUE
, FALSE
, "BadFaxLines" },
1165 { TIFFTAG_BADFAXLINES
, 1, 1, TIFF_SHORT
, FIELD_BADFAXLINES
,
1166 TRUE
, FALSE
, "BadFaxLines" },
1167 { TIFFTAG_CLEANFAXDATA
, 1, 1, TIFF_SHORT
, FIELD_CLEANFAXDATA
,
1168 TRUE
, FALSE
, "CleanFaxData" },
1169 { TIFFTAG_CONSECUTIVEBADFAXLINES
,1,1, TIFF_LONG
, FIELD_BADFAXRUN
,
1170 TRUE
, FALSE
, "ConsecutiveBadFaxLines" },
1171 { TIFFTAG_CONSECUTIVEBADFAXLINES
,1,1, TIFF_SHORT
, FIELD_BADFAXRUN
,
1172 TRUE
, FALSE
, "ConsecutiveBadFaxLines" },
1173 { TIFFTAG_FAXRECVPARAMS
, 1, 1, TIFF_LONG
, FIELD_RECVPARAMS
,
1174 TRUE
, FALSE
, "FaxRecvParams" },
1175 { TIFFTAG_FAXSUBADDRESS
, -1,-1, TIFF_ASCII
, FIELD_SUBADDRESS
,
1176 TRUE
, FALSE
, "FaxSubAddress" },
1177 { TIFFTAG_FAXRECVTIME
, 1, 1, TIFF_LONG
, FIELD_RECVTIME
,
1178 TRUE
, FALSE
, "FaxRecvTime" },
1180 static const TIFFFieldInfo fax3FieldInfo
[] = {
1181 { TIFFTAG_GROUP3OPTIONS
, 1, 1, TIFF_LONG
, FIELD_OPTIONS
,
1182 FALSE
, FALSE
, "Group3Options" },
1184 static const TIFFFieldInfo fax4FieldInfo
[] = {
1185 { TIFFTAG_GROUP4OPTIONS
, 1, 1, TIFF_LONG
, FIELD_OPTIONS
,
1186 FALSE
, FALSE
, "Group4Options" },
1188 #define N(a) (sizeof (a) / sizeof (a[0]))
1191 Fax3VSetField(TIFF
* tif
, ttag_t tag
, va_list ap
)
1193 Fax3BaseState
* sp
= Fax3State(tif
);
1196 case TIFFTAG_FAXMODE
:
1197 sp
->mode
= va_arg(ap
, int);
1198 return (1); /* NB: pseudo tag */
1199 case TIFFTAG_FAXFILLFUNC
:
1200 if (tif
->tif_mode
== O_RDONLY
)
1201 DecoderState(tif
)->fill
= va_arg(ap
, TIFFFaxFillFunc
);
1202 return (1); /* NB: pseudo tag */
1203 case TIFFTAG_GROUP3OPTIONS
:
1204 case TIFFTAG_GROUP4OPTIONS
:
1205 sp
->groupoptions
= va_arg(ap
, uint32
);
1207 case TIFFTAG_BADFAXLINES
:
1208 sp
->badfaxlines
= va_arg(ap
, uint32
);
1210 case TIFFTAG_CLEANFAXDATA
:
1211 sp
->cleanfaxdata
= (uint16
) va_arg(ap
, int);
1213 case TIFFTAG_CONSECUTIVEBADFAXLINES
:
1214 sp
->badfaxrun
= va_arg(ap
, uint32
);
1216 case TIFFTAG_FAXRECVPARAMS
:
1217 sp
->recvparams
= va_arg(ap
, uint32
);
1219 case TIFFTAG_FAXSUBADDRESS
:
1220 _TIFFsetString(&sp
->subaddress
, va_arg(ap
, char*));
1222 case TIFFTAG_FAXRECVTIME
:
1223 sp
->recvtime
= va_arg(ap
, uint32
);
1226 return (*sp
->vsetparent
)(tif
, tag
, ap
);
1228 TIFFSetFieldBit(tif
, _TIFFFieldWithTag(tif
, tag
)->field_bit
);
1229 tif
->tif_flags
|= TIFF_DIRTYDIRECT
;
1234 Fax3VGetField(TIFF
* tif
, ttag_t tag
, va_list ap
)
1236 Fax3BaseState
* sp
= Fax3State(tif
);
1239 case TIFFTAG_FAXMODE
:
1240 *va_arg(ap
, int*) = sp
->mode
;
1242 case TIFFTAG_FAXFILLFUNC
:
1243 if (tif
->tif_mode
== O_RDONLY
)
1244 *va_arg(ap
, TIFFFaxFillFunc
*) = DecoderState(tif
)->fill
;
1246 case TIFFTAG_GROUP3OPTIONS
:
1247 case TIFFTAG_GROUP4OPTIONS
:
1248 *va_arg(ap
, uint32
*) = sp
->groupoptions
;
1250 case TIFFTAG_BADFAXLINES
:
1251 *va_arg(ap
, uint32
*) = sp
->badfaxlines
;
1253 case TIFFTAG_CLEANFAXDATA
:
1254 *va_arg(ap
, uint16
*) = sp
->cleanfaxdata
;
1256 case TIFFTAG_CONSECUTIVEBADFAXLINES
:
1257 *va_arg(ap
, uint32
*) = sp
->badfaxrun
;
1259 case TIFFTAG_FAXRECVPARAMS
:
1260 *va_arg(ap
, uint32
*) = sp
->recvparams
;
1262 case TIFFTAG_FAXSUBADDRESS
:
1263 *va_arg(ap
, char**) = sp
->subaddress
;
1265 case TIFFTAG_FAXRECVTIME
:
1266 *va_arg(ap
, uint32
*) = sp
->recvtime
;
1269 return (*sp
->vgetparent
)(tif
, tag
, ap
);
1274 static void LINKAGEMODE
1275 Fax3PrintDir(TIFF
* tif
, FILE* fd
, long flags
)
1277 Fax3BaseState
* sp
= Fax3State(tif
);
1280 if (TIFFFieldSet(tif
,FIELD_OPTIONS
)) {
1281 const char* sep
= " ";
1282 if (tif
->tif_dir
.td_compression
== COMPRESSION_CCITTFAX4
) {
1283 fprintf(fd
, " Group 4 Options:");
1284 if (sp
->groupoptions
& GROUP4OPT_UNCOMPRESSED
)
1285 fprintf(fd
, "%suncompressed data", sep
);
1288 fprintf(fd
, " Group 3 Options:");
1289 if (sp
->groupoptions
& GROUP3OPT_2DENCODING
)
1290 fprintf(fd
, "%s2-d encoding", sep
), sep
= "+";
1291 if (sp
->groupoptions
& GROUP3OPT_FILLBITS
)
1292 fprintf(fd
, "%sEOL padding", sep
), sep
= "+";
1293 if (sp
->groupoptions
& GROUP3OPT_UNCOMPRESSED
)
1294 fprintf(fd
, "%suncompressed data", sep
);
1296 fprintf(fd
, " (%lu = 0x%lx)\n",
1297 (u_long
) sp
->groupoptions
, (u_long
) sp
->groupoptions
);
1299 if (TIFFFieldSet(tif
,FIELD_CLEANFAXDATA
)) {
1300 fprintf(fd
, " Fax Data:");
1301 switch (sp
->cleanfaxdata
) {
1302 case CLEANFAXDATA_CLEAN
:
1303 fprintf(fd
, " clean");
1305 case CLEANFAXDATA_REGENERATED
:
1306 fprintf(fd
, " receiver regenerated");
1308 case CLEANFAXDATA_UNCLEAN
:
1309 fprintf(fd
, " uncorrected errors");
1312 fprintf(fd
, " (%u = 0x%x)\n",
1313 sp
->cleanfaxdata
, sp
->cleanfaxdata
);
1315 if (TIFFFieldSet(tif
,FIELD_BADFAXLINES
))
1316 fprintf(fd
, " Bad Fax Lines: %lu\n", (u_long
) sp
->badfaxlines
);
1317 if (TIFFFieldSet(tif
,FIELD_BADFAXRUN
))
1318 fprintf(fd
, " Consecutive Bad Fax Lines: %lu\n",
1319 (u_long
) sp
->badfaxrun
);
1320 if (TIFFFieldSet(tif
,FIELD_RECVPARAMS
))
1321 fprintf(fd
, " Fax Receive Parameters: %08lx\n",
1322 (u_long
) sp
->recvparams
);
1323 if (TIFFFieldSet(tif
,FIELD_SUBADDRESS
))
1324 fprintf(fd
, " Fax SubAddress: %s\n", sp
->subaddress
);
1325 if (TIFFFieldSet(tif
,FIELD_RECVTIME
))
1326 fprintf(fd
, " Fax Receive Time: %lu secs\n",
1327 (u_long
) sp
->recvtime
);
1330 static int LINKAGEMODE
1331 InitCCITTFax3(TIFF
* tif
)
1336 * Allocate state block so tag methods have storage to record values.
1338 if (tif
->tif_mode
== O_RDONLY
)
1339 #if defined(__VISAGECPP__)
1340 tif
->tif_data
= (tidata_t
)_TIFFmalloc(sizeof (Fax3DecodeState
));
1342 tif
->tif_data
= (tidata_t
)_TIFFmalloc(sizeof (Fax3EncodeState
));
1344 tif
->tif_data
= _TIFFmalloc(sizeof (Fax3DecodeState
));
1346 tif
->tif_data
= _TIFFmalloc(sizeof (Fax3EncodeState
));
1348 if (tif
->tif_data
== NULL
) {
1349 TIFFError("TIFFInitCCITTFax3",
1350 "%s: No space for state block", tif
->tif_name
);
1353 sp
= Fax3State(tif
);
1356 * Merge codec-specific tag information and
1357 * override parent get/set field methods.
1359 _TIFFMergeFieldInfo(tif
, faxFieldInfo
, N(faxFieldInfo
));
1360 sp
->vgetparent
= tif
->tif_vgetfield
;
1361 tif
->tif_vgetfield
= Fax3VGetField
; /* hook for codec tags */
1362 sp
->vsetparent
= tif
->tif_vsetfield
;
1363 tif
->tif_vsetfield
= Fax3VSetField
; /* hook for codec tags */
1364 tif
->tif_printdir
= Fax3PrintDir
; /* hook for codec tags */
1365 sp
->groupoptions
= 0;
1367 sp
->subaddress
= NULL
;
1369 if (tif
->tif_mode
== O_RDONLY
) {
1370 tif
->tif_flags
|= TIFF_NOBITREV
;/* decoder does bit reversal */
1371 DecoderState(tif
)->runs
= NULL
;
1372 TIFFSetField(tif
, TIFFTAG_FAXFILLFUNC
, _TIFFFax3fillruns
);
1374 EncoderState(tif
)->refline
= NULL
;
1377 * Install codec methods.
1379 tif
->tif_setupdecode
= Fax3SetupState
;
1380 tif
->tif_predecode
= Fax3PreDecode
;
1381 tif
->tif_decoderow
= Fax3Decode1D
;
1382 tif
->tif_decodestrip
= Fax3Decode1D
;
1383 tif
->tif_decodetile
= Fax3Decode1D
;
1384 tif
->tif_setupencode
= Fax3SetupState
;
1385 tif
->tif_preencode
= Fax3PreEncode
;
1386 tif
->tif_postencode
= Fax3PostEncode
;
1387 tif
->tif_encoderow
= Fax3Encode
;
1388 tif
->tif_encodestrip
= Fax3Encode
;
1389 tif
->tif_encodetile
= Fax3Encode
;
1390 tif
->tif_close
= Fax3Close
;
1391 tif
->tif_cleanup
= Fax3Cleanup
;
1397 TIFFInitCCITTFax3(TIFF
* tif
, int scheme
)
1399 if (InitCCITTFax3(tif
)) {
1400 _TIFFMergeFieldInfo(tif
, fax3FieldInfo
, N(fax3FieldInfo
));
1403 * The default format is Class/F-style w/o RTC.
1405 return TIFFSetField(tif
, TIFFTAG_FAXMODE
, FAXMODE_CLASSF
);
1411 * CCITT Group 4 (T.6) Facsimile-compatible
1412 * Compression Scheme Support.
1415 #define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }
1417 * Decode the requested amount of G4-encoded data.
1419 static int LINKAGEMODE
1420 Fax4Decode(TIFF
* tif
, tidata_t buf
, tsize_t occ
, tsample_t s
)
1422 DECLARE_STATE_2D(tif
, sp
, "Fax4Decode");
1425 CACHE_STATE(tif
, sp
);
1426 while ((long)occ
> 0) {
1429 pa
= thisrun
= sp
->curruns
;
1433 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc
, BitsAvail
);
1434 printf("-------------------- %d\n", tif
->tif_row
);
1438 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1439 SETVAL(0); /* imaginary change for reference */
1440 SWAP(uint32
*, sp
->curruns
, sp
->refruns
);
1441 buf
+= sp
->b
.rowbytes
;
1442 occ
-= sp
->b
.rowbytes
;
1447 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1448 UNCACHE_STATE(tif
, sp
);
1451 UNCACHE_STATE(tif
, sp
);
1457 * Encode the requested amount of data.
1459 static int LINKAGEMODE
1460 Fax4Encode(TIFF
* tif
, tidata_t bp
, tsize_t cc
, tsample_t s
)
1462 Fax3EncodeState
*sp
= EncoderState(tif
);
1465 while ((long)cc
> 0) {
1466 if (!Fax3Encode2DRow(tif
, bp
, sp
->refline
, sp
->b
.rowpixels
))
1468 _TIFFmemcpy(sp
->refline
, bp
, sp
->b
.rowbytes
);
1469 bp
+= sp
->b
.rowbytes
;
1470 cc
-= sp
->b
.rowbytes
;
1478 Fax4PostEncode(TIFF
* tif
)
1480 Fax3EncodeState
*sp
= EncoderState(tif
);
1482 /* terminate strip w/ EOFB */
1483 Fax3PutBits(tif
, EOL
, 12);
1484 Fax3PutBits(tif
, EOL
, 12);
1486 Fax3FlushBits(tif
, sp
);
1491 TIFFInitCCITTFax4(TIFF
* tif
, int scheme
)
1493 if (InitCCITTFax3(tif
)) { /* reuse G3 support */
1494 _TIFFMergeFieldInfo(tif
, fax4FieldInfo
, N(fax4FieldInfo
));
1496 tif
->tif_decoderow
= Fax4Decode
;
1497 tif
->tif_decodestrip
= Fax4Decode
;
1498 tif
->tif_decodetile
= Fax4Decode
;
1499 tif
->tif_encoderow
= Fax4Encode
;
1500 tif
->tif_encodestrip
= Fax4Encode
;
1501 tif
->tif_encodetile
= Fax4Encode
;
1502 tif
->tif_postencode
= Fax4PostEncode
;
1504 * Suppress RTC at the end of each strip.
1506 return TIFFSetField(tif
, TIFFTAG_FAXMODE
, FAXMODE_NORTC
);
1512 * CCITT Group 3 1-D Modified Huffman RLE Compression Support.
1513 * (Compression algorithms 2 and 32771)
1517 * Decode the requested amount of RLE-encoded data.
1519 static int LINKAGEMODE
1520 Fax3DecodeRLE(TIFF
* tif
, tidata_t buf
, tsize_t occ
, tsample_t s
)
1522 DECLARE_STATE(tif
, sp
, "Fax3DecodeRLE");
1523 int mode
= sp
->b
.mode
;
1526 CACHE_STATE(tif
, sp
);
1527 thisrun
= sp
->curruns
;
1528 while ((long)occ
> 0) {
1533 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc
, BitsAvail
);
1534 printf("-------------------- %d\n", tif
->tif_row
);
1538 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1540 * Cleanup at the end of the row.
1542 if (mode
& FAXMODE_BYTEALIGN
) {
1543 int n
= BitsAvail
- (BitsAvail
&~ 7);
1545 } else if (mode
& FAXMODE_WORDALIGN
) {
1546 int n
= BitsAvail
- (BitsAvail
&~ 15);
1548 if (BitsAvail
== 0 && !isAligned(cp
, uint16
))
1551 buf
+= sp
->b
.rowbytes
;
1552 occ
-= sp
->b
.rowbytes
;
1556 EOFRLE
: /* premature EOF */
1557 (*sp
->fill
)(buf
, thisrun
, pa
, lastx
);
1558 UNCACHE_STATE(tif
, sp
);
1561 UNCACHE_STATE(tif
, sp
);
1566 TIFFInitCCITTRLE(TIFF
* tif
, int scheme
)
1568 if (InitCCITTFax3(tif
)) { /* reuse G3 support */
1569 tif
->tif_decoderow
= Fax3DecodeRLE
;
1570 tif
->tif_decodestrip
= Fax3DecodeRLE
;
1571 tif
->tif_decodetile
= Fax3DecodeRLE
;
1573 * Suppress RTC+EOLs when encoding and byte-align data.
1575 return TIFFSetField(tif
, TIFFTAG_FAXMODE
,
1576 FAXMODE_NORTC
|FAXMODE_NOEOL
|FAXMODE_BYTEALIGN
);
1582 TIFFInitCCITTRLEW(TIFF
* tif
, int scheme
)
1584 if (InitCCITTFax3(tif
)) { /* reuse G3 support */
1585 tif
->tif_decoderow
= Fax3DecodeRLE
;
1586 tif
->tif_decodestrip
= Fax3DecodeRLE
;
1587 tif
->tif_decodetile
= Fax3DecodeRLE
;
1589 * Suppress RTC+EOLs when encoding and word-align data.
1591 return TIFFSetField(tif
, TIFFTAG_FAXMODE
,
1592 FAXMODE_NORTC
|FAXMODE_NOEOL
|FAXMODE_WORDALIGN
);
1596 #endif /* CCITT_SUPPORT */