1 /* PDFlib GmbH cvsid:
2  * $Id: tif_fax3.c,v 1.27.2.3 2010/10/08 12:17:32 stm Exp $ */
3 
4 /*
5  * Copyright (c) 1990-1997 Sam Leffler
6  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and
9  * its documentation for any purpose is hereby granted without fee, provided
10  * that (i) the above copyright notices and this permission notice appear in
11  * all copies of the software and related documentation, and (ii) the names of
12  * Sam Leffler and Silicon Graphics may not be used in any advertising or
13  * publicity relating to the software without the specific, prior written
14  * permission of Sam Leffler and Silicon Graphics.
15  *
16  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
21  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
22  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
23  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
24  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25  * OF THIS SOFTWARE.
26  */
27 
28 #include "tiffiop.h"
29 #ifdef CCITT_SUPPORT
30 /*
31  * TIFF Library.
32  *
33  * CCITT Group 3 (T.4) and Group 4 (T.6) Compression Support.
34  *
35  * This file contains support for decoding and encoding TIFF
36  * compression algorithms 2, 3, 4, and 32771.
37  *
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.
41  */
42 #include "tif_fax3.h"
43 #define	G3CODES
44 #include "t4.h"
45 #include <stdio.h>
46 
47 /*
48  * Compression+decompression state blocks are
49  * derived from this ``base state'' block.
50  */
51 typedef struct {
52         int     rw_mode;                /* O_RDONLY for decode, else encode */
53 	int	mode;			/* operating mode */
54 	uint32	rowbytes;		/* bytes in a decoded scanline */
55 	uint32	rowpixels;		/* pixels in a scanline */
56 
57 	uint16	cleanfaxdata;		/* CleanFaxData tag */
58 	uint32	badfaxrun;		/* BadFaxRun tag */
59 	uint32	badfaxlines;		/* BadFaxLines tag */
60 	uint32	groupoptions;		/* Group 3/4 options tag */
61 	uint32	recvparams;		/* encoded Class 2 session params */
62 	char*	subaddress;		/* subaddress string */
63 	uint32	recvtime;		/* time spent receiving (secs) */
64 	char*	faxdcs;			/* Table 2/T.30 encoded session params*/
65 	TIFFVGetMethod vgetparent;	/* super-class method */
66 	TIFFVSetMethod vsetparent;	/* super-class method */
67 } Fax3BaseState;
68 #define	Fax3State(tif)		((Fax3BaseState*) (tif)->tif_data)
69 
70 typedef enum { G3_1D, G3_2D } Ttag;
71 typedef struct {
72 	Fax3BaseState b;
73 
74 	/* Decoder state info */
75 	const unsigned char* bitmap;	/* bit reversal table */
76 	uint32	data;			/* current i/o byte/word */
77 	int	bit;			/* current i/o bit in byte */
78 	int	EOLcnt;			/* count of EOL codes recognized */
79 	TIFFFaxFillFunc fill;		/* fill routine */
80 	uint32*	runs;			/* b&w runs for current/previous row */
81 	uint32*	refruns;		/* runs for reference line */
82 	uint32*	curruns;		/* runs for current line */
83 
84 	/* Encoder state info */
85 	Ttag    tag;			/* encoding state */
86 	unsigned char*	refline;	/* reference line for 2d decoding */
87 	int	k;			/* #rows left that can be 2d encoded */
88 	int	maxk;			/* max #rows that can be 2d encoded */
89 } Fax3CodecState;
90 #define	DecoderState(tif)	((Fax3CodecState*) Fax3State(tif))
91 #define	EncoderState(tif)	((Fax3CodecState*) Fax3State(tif))
92 
93 #define	is2DEncoding(sp) \
94 	(sp->b.groupoptions & GROUP3OPT_2DENCODING)
95 #define	isAligned(p,t)	((((unsigned long)(p)) & (sizeof (t)-1)) == 0)
96 
97 /*
98  * Group 3 and Group 4 Decoding.
99  */
100 
101 /*
102  * These macros glue the TIFF library state to
103  * the state expected by Frank's decoder.
104  */
105 #define	DECLARE_STATE(tif, sp, mod)					\
106     static const char module[] = mod;					\
107     Fax3CodecState* sp = DecoderState(tif);				\
108     int a0;				/* reference element */		\
109     int lastx = sp->b.rowpixels;	/* last element in row */	\
110     uint32 BitAcc;			/* bit accumulator */		\
111     int BitsAvail;			/* # valid bits in BitAcc */	\
112     int RunLength;			/* length of current run */	\
113     unsigned char* cp;			/* next byte of input data */	\
114     unsigned char* ep;			/* end of input data */		\
115     uint32* pa;				/* place to stuff next run */	\
116     uint32* thisrun;			/* current row's run array */	\
117     int EOLcnt;				/* # EOL codes recognized */	\
118     const unsigned char* bitmap = sp->bitmap;	/* input data bit reverser */\
119     const TIFFFaxTabEnt* TabEnt
120 #define	DECLARE_STATE_2D(tif, sp, mod)					\
121     DECLARE_STATE(tif, sp, mod);					\
122     int b1;				/* next change on prev line */	\
123     uint32* pb				/* next run in reference line */\
124 /*
125  * Load any state that may be changed during decoding.
126  */
127 #define	CACHE_STATE(tif, sp) do {					\
128     BitAcc = sp->data;							\
129     BitsAvail = sp->bit;						\
130     EOLcnt = sp->EOLcnt;						\
131     cp = (unsigned char*) tif->tif_rawcp;				\
132     ep = cp + tif->tif_rawcc;						\
133 } while (0)
134 /*
135  * Save state possibly changed during decoding.
136  */
137 #define	UNCACHE_STATE(tif, sp) do {					\
138     sp->bit = BitsAvail;						\
139     sp->data = BitAcc;							\
140     sp->EOLcnt = EOLcnt;						\
141     tif->tif_rawcc -= (tidata_t) cp - tif->tif_rawcp;			\
142     tif->tif_rawcp = (tidata_t) cp;					\
143 } while (0)
144 
145 /*
146  * Setup state for decoding a strip.
147  */
148 static int
Fax3PreDecode(TIFF * tif,tsample_t s)149 Fax3PreDecode(TIFF* tif, tsample_t s)
150 {
151 	Fax3CodecState* sp = DecoderState(tif);
152 
153 	(void) s;
154 	assert(sp != NULL);
155 	sp->bit = 0;			/* force initial read */
156 	sp->data = 0;
157 	sp->EOLcnt = 0;			/* force initial scan for EOL */
158 	/*
159 	 * Decoder assumes lsb-to-msb bit order.  Note that we select
160 	 * this here rather than in Fax3SetupState so that viewers can
161 	 * hold the image open, fiddle with the FillOrder tag value,
162 	 * and then re-decode the image.  Otherwise they'd need to close
163 	 * and open the image to get the state reset.
164 	 */
165 	sp->bitmap =
166 	    TIFFGetBitRevTable(tif->tif_dir.td_fillorder != FILLORDER_LSB2MSB);
167 	if (sp->refruns) {		/* init reference line to white */
168 		sp->refruns[0] = (uint32) sp->b.rowpixels;
169 		sp->refruns[1] = 0;
170 	}
171 	return (1);
172 }
173 
174 /*
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.
178  */
179 
180 static void
Fax3Unexpected(const char * module,TIFF * tif,uint32 a0)181 Fax3Unexpected(const char* module, TIFF* tif, uint32 a0)
182 {
183 	_TIFFError(tif, module, "%s: Bad code word at scanline %d (x %lu)",
184 	    tif->tif_name, tif->tif_row, (unsigned long) a0);
185 }
186 #define	unexpected(table, a0)	Fax3Unexpected(module, tif, a0)
187 
188 static void
Fax3Extension(const char * module,TIFF * tif,uint32 a0)189 Fax3Extension(const char* module, TIFF* tif, uint32 a0)
190 {
191 	_TIFFError(tif, module,
192 	    "%s: Uncompressed data (not supported) at scanline %d (x %lu)",
193 	    tif->tif_name, tif->tif_row, (unsigned long) a0);
194 }
195 #define	extension(a0)	Fax3Extension(module, tif, a0)
196 
197 static void
Fax3BadLength(const char * module,TIFF * tif,uint32 a0,uint32 lastx)198 Fax3BadLength(const char* module, TIFF* tif, uint32 a0, uint32 lastx)
199 {
200 	_TIFFWarning(tif,module,"%s: %s at scanline %d (got %lu, expected %lu)",
201 	    tif->tif_name,
202 	    a0 < lastx ? "Premature EOL" : "Line length mismatch",
203 	    tif->tif_row, (unsigned long) a0, (unsigned long) lastx);
204 }
205 #define	badlength(a0,lastx)	Fax3BadLength(module, tif, a0, lastx)
206 
207 static void
Fax3PrematureEOF(const char * module,TIFF * tif,uint32 a0)208 Fax3PrematureEOF(const char* module, TIFF* tif, uint32 a0)
209 {
210 	_TIFFWarning(tif, module, "%s: Premature EOF at scanline %d (x %lu)",
211 	    tif->tif_name, tif->tif_row, (unsigned long) a0);
212 }
213 #define	prematureEOF(a0)	Fax3PrematureEOF(module, tif, a0)
214 
215 #define	Nop
216 
217 /*
218  * Decode the requested amount of G3 1D-encoded data.
219  */
220 static int
Fax3Decode1D(TIFF * tif,tidata_t buf,tsize_t occ,tsample_t s)221 Fax3Decode1D(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
222 {
223 	DECLARE_STATE(tif, sp, "Fax3Decode1D");
224 
225 	(void) s;
226 	CACHE_STATE(tif, sp);
227 	thisrun = sp->curruns;
228 	while ((long)occ > 0) {
229 		a0 = 0;
230 		RunLength = 0;
231 		pa = thisrun;
232 #ifdef FAX3_DEBUG
233 		printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail);
234 		printf("-------------------- %d\n", tif->tif_row);
235 		fflush(stdout);
236 #endif
237 		SYNC_EOL(EOF1D);
238 		EXPAND1D(EOF1Da);
239 		(*sp->fill)(buf, thisrun, pa, lastx);
240 		buf += sp->b.rowbytes;
241 		occ -= sp->b.rowbytes;
242 		continue;
243 	EOF1D:				/* premature EOF */
244 		CLEANUP_RUNS();
245 	EOF1Da:				/* premature EOF */
246 		(*sp->fill)(buf, thisrun, pa, lastx);
247 		UNCACHE_STATE(tif, sp);
248 		return (-1);
249 	}
250 	UNCACHE_STATE(tif, sp);
251 	return (1);
252 }
253 
254 #define	SWAP(t,a,b)	{ t x; x = (a); (a) = (b); (b) = x; }
255 /*
256  * Decode the requested amount of G3 2D-encoded data.
257  */
258 static int
Fax3Decode2D(TIFF * tif,tidata_t buf,tsize_t occ,tsample_t s)259 Fax3Decode2D(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
260 {
261 	DECLARE_STATE_2D(tif, sp, "Fax3Decode2D");
262 	int is1D;			/* current line is 1d/2d-encoded */
263 
264 	(void) s;
265 	CACHE_STATE(tif, sp);
266 	while ((long)occ > 0) {
267 		a0 = 0;
268 		RunLength = 0;
269 		pa = thisrun = sp->curruns;
270 #ifdef FAX3_DEBUG
271 		printf("\nBitAcc=%08X, BitsAvail = %d EOLcnt = %d",
272 		    BitAcc, BitsAvail, EOLcnt);
273 #endif
274 		SYNC_EOL(EOF2D);
275 		NeedBits8(1, EOF2D);
276 		is1D = GetBits(1);	/* 1D/2D-encoding tag bit */
277 		ClrBits(1);
278 #ifdef FAX3_DEBUG
279 		printf(" %s\n-------------------- %d\n",
280 		    is1D ? "1D" : "2D", tif->tif_row);
281 		fflush(stdout);
282 #endif
283 		pb = sp->refruns;
284 		b1 = *pb++;
285 		if (is1D)
286 			EXPAND1D(EOF2Da);
287 		else
288 			EXPAND2D(EOF2Da);
289 		(*sp->fill)(buf, thisrun, pa, lastx);
290 		SETVAL(0);		/* imaginary change for reference */
291 		SWAP(uint32*, sp->curruns, sp->refruns);
292 		buf += sp->b.rowbytes;
293 		occ -= sp->b.rowbytes;
294 		continue;
295 	EOF2D:				/* premature EOF */
296 		CLEANUP_RUNS();
297 	EOF2Da:				/* premature EOF */
298 		(*sp->fill)(buf, thisrun, pa, lastx);
299 		UNCACHE_STATE(tif, sp);
300 		return (-1);
301 	}
302 	UNCACHE_STATE(tif, sp);
303 	return (1);
304 }
305 #undef SWAP
306 
307 /*
308  * The ZERO & FILL macros must handle spans < 2*sizeof(long) bytes.
309  * For machines with 64-bit longs this is <16 bytes; otherwise
310  * this is <8 bytes.  We optimize the code here to reflect the
311  * machine characteristics.
312  */
313 #if defined(__alpha) || _MIPS_SZLONG == 64 || \
314         defined(__sparcv9) || defined(__LP64__) || defined(__arch64__) \
315 	|| (defined(__x86_64__) && defined(__SunOS)) \
316 	|| defined(PDC_PF_AIX64)
317 /* #if SIZEOF_LONG == 8 */
318 # define FILL(n, cp)							    \
319     switch (n) {							    \
320     case 15:(cp)[14] = 0xff; case 14:(cp)[13] = 0xff; case 13: (cp)[12] = 0xff;\
321     case 12:(cp)[11] = 0xff; case 11:(cp)[10] = 0xff; case 10: (cp)[9] = 0xff;\
322     case  9: (cp)[8] = 0xff; case  8: (cp)[7] = 0xff; case  7: (cp)[6] = 0xff;\
323     case  6: (cp)[5] = 0xff; case  5: (cp)[4] = 0xff; case  4: (cp)[3] = 0xff;\
324     case  3: (cp)[2] = 0xff; case  2: (cp)[1] = 0xff;			      \
325     case  1: (cp)[0] = 0xff; (cp) += (n); case 0:  ;			      \
326     }
327 # define ZERO(n, cp)							\
328     switch (n) {							\
329     case 15:(cp)[14] = 0; case 14:(cp)[13] = 0; case 13: (cp)[12] = 0;	\
330     case 12:(cp)[11] = 0; case 11:(cp)[10] = 0; case 10: (cp)[9] = 0;	\
331     case  9: (cp)[8] = 0; case  8: (cp)[7] = 0; case  7: (cp)[6] = 0;	\
332     case  6: (cp)[5] = 0; case  5: (cp)[4] = 0; case  4: (cp)[3] = 0;	\
333     case  3: (cp)[2] = 0; case  2: (cp)[1] = 0;				\
334     case  1: (cp)[0] = 0; (cp) += (n); case 0:  ;			\
335     }
336 #else
337 # define FILL(n, cp)							    \
338     switch (n) {							    \
339     case 7: (cp)[6] = 0xff; case 6: (cp)[5] = 0xff; case 5: (cp)[4] = 0xff; \
340     case 4: (cp)[3] = 0xff; case 3: (cp)[2] = 0xff; case 2: (cp)[1] = 0xff; \
341     case 1: (cp)[0] = 0xff; (cp) += (n); case 0:  ;			    \
342     }
343 # define ZERO(n, cp)							\
344     switch (n) {							\
345     case 7: (cp)[6] = 0; case 6: (cp)[5] = 0; case 5: (cp)[4] = 0;	\
346     case 4: (cp)[3] = 0; case 3: (cp)[2] = 0; case 2: (cp)[1] = 0;	\
347     case 1: (cp)[0] = 0; (cp) += (n); case 0:  ;			\
348     }
349 #endif
350 
351 /*
352  * Bit-fill a row according to the white/black
353  * runs generated during G3/G4 decoding.
354  */
355 void
_TIFFFax3fillruns(unsigned char * buf,uint32 * runs,uint32 * erun,uint32 lastx)356 _TIFFFax3fillruns(unsigned char* buf, uint32* runs, uint32* erun, uint32 lastx)
357 {
358 	static const unsigned char _fillmasks[] =
359 	    { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
360 	unsigned char* cp;
361 	uint32 x, bx, run;
362 	int32 n, nw;
363 	long* lp;
364 
365 	if ((erun-runs)&1)
366 	    *erun++ = 0;
367 	x = 0;
368 	for (; runs < erun; runs += 2) {
369 	    run = runs[0];
370 	    if (x+run > lastx || run > lastx )
371 		run = runs[0] = (uint32) (lastx - x);
372 	    if (run) {
373 		cp = buf + (x>>3);
374 		bx = x&7;
375 		if (run > 8-bx) {
376 		    if (bx) {			/* align to byte boundary */
377 			*cp++ &= 0xff << (8-bx);
378 			run -= 8-bx;
379 		    }
380 		    if( (n = run >> 3) != 0 ) {	/* multiple bytes to fill */
381 			if ((n/sizeof (long)) > 1) {
382 			    /*
383 			     * Align to longword boundary and fill.
384 			     */
385 			    for (; n && !isAligned(cp, long); n--)
386 				    *cp++ = 0x00;
387 			    lp = (long*) cp;
388 			    nw = (int32)(n / sizeof (long));
389 			    n -= nw * sizeof (long);
390 			    do {
391 				    *lp++ = 0L;
392 			    } while (--nw);
393 			    cp = (unsigned char*) lp;
394 			}
395 			ZERO(n, cp);
396 			run &= 7;
397 		    }
398 		    if (run)
399 			cp[0] &= 0xff >> run;
400 		} else
401 		    cp[0] &= ~(_fillmasks[run]>>bx);
402 		x += runs[0];
403 	    }
404 	    run = runs[1];
405 	    if (x+run > lastx || run > lastx )
406 		run = runs[1] = lastx - x;
407 	    if (run) {
408 		cp = buf + (x>>3);
409 		bx = x&7;
410 		if (run > 8-bx) {
411 		    if (bx) {			/* align to byte boundary */
412 			*cp++ |= 0xff >> bx;
413 			run -= 8-bx;
414 		    }
415 		    if( (n = run>>3) != 0 ) {	/* multiple bytes to fill */
416 			if ((n/sizeof (long)) > 1) {
417 			    /*
418 			     * Align to longword boundary and fill.
419 			     */
420 			    for (; n && !isAligned(cp, long); n--)
421 				*cp++ = 0xff;
422 			    lp = (long*) cp;
423 			    nw = (int32)(n / sizeof (long));
424 			    n -= nw * sizeof (long);
425 			    do {
426 				*lp++ = -1L;
427 			    } while (--nw);
428 			    cp = (unsigned char*) lp;
429 			}
430 			FILL(n, cp);
431 			run &= 7;
432 		    }
433 		    if (run)
434 			cp[0] |= 0xff00 >> run;
435 		} else
436 		    cp[0] |= _fillmasks[run]>>bx;
437 		x += runs[1];
438 	    }
439 	}
440 	assert(x == lastx);
441 }
442 #undef	ZERO
443 #undef	FILL
444 
445 /*
446  * Setup G3/G4-related compression/decompression state
447  * before data is processed.  This routine is called once
448  * per image -- it sets up different state based on whether
449  * or not decoding or encoding is being done and whether
450  * 1D- or 2D-encoded data is involved.
451  */
452 static int
Fax3SetupState(TIFF * tif)453 Fax3SetupState(TIFF* tif)
454 {
455 	TIFFDirectory* td = &tif->tif_dir;
456 	Fax3BaseState* sp = Fax3State(tif);
457 	long rowbytes, rowpixels;
458 	int needsRefLine;
459 	Fax3CodecState* dsp = (Fax3CodecState*) Fax3State(tif);
460 	uint32 nruns;
461 
462 	if (td->td_bitspersample != 1) {
463 		_TIFFError(tif, tif->tif_name,
464 		    "Bits/sample must be 1 for Group 3/4 encoding/decoding");
465 		return (0);
466 	}
467 	/*
468 	 * Calculate the scanline/tile widths.
469 	 */
470 	if (isTiled(tif)) {
471 		rowbytes = TIFFTileRowSize(tif);
472 		rowpixels = td->td_tilewidth;
473 	} else {
474 		rowbytes = TIFFScanlineSize(tif);
475 		rowpixels = td->td_imagewidth;
476 	}
477 	sp->rowbytes = (uint32) rowbytes;
478 	sp->rowpixels = (uint32) rowpixels;
479 	/*
480 	 * Allocate any additional space required for decoding/encoding.
481 	 */
482 	needsRefLine = (
483 	    (sp->groupoptions & GROUP3OPT_2DENCODING) ||
484 	    td->td_compression == COMPRESSION_CCITTFAX4
485 	);
486 
487 	nruns = needsRefLine ? 2*TIFFroundup(rowpixels,32) : rowpixels;
488         nruns += 3;
489         dsp->runs = (uint32*) _TIFFCheckMalloc(tif, 2*nruns, sizeof (uint32),
490 					  "for Group 3/4 run arrays");
491 	if (dsp->runs == NULL)
492 		return (0);
493 	dsp->curruns = dsp->runs;
494 	if (needsRefLine)
495 	        dsp->refruns = dsp->runs + nruns;
496 	else
497 		dsp->refruns = NULL;
498 	if (td->td_compression == COMPRESSION_CCITTFAX3
499 	    && is2DEncoding(dsp)) {	/* NB: default is 1D routine */
500 		tif->tif_decoderow = Fax3Decode2D;
501 		tif->tif_decodestrip = Fax3Decode2D;
502 		tif->tif_decodetile = Fax3Decode2D;
503 	}
504 
505 	if (needsRefLine) {		/* 2d encoding */
506 		Fax3CodecState* esp = EncoderState(tif);
507 		/*
508 		 * 2d encoding requires a scanline
509 		 * buffer for the ``reference line''; the
510 		 * scanline against which delta encoding
511 		 * is referenced.  The reference line must
512 		 * be initialized to be ``white'' (done elsewhere).
513 		 */
514 		esp->refline = (unsigned char*) _TIFFmalloc(rowbytes);
515 		if (esp->refline == NULL) {
516 			_TIFFError(tif, "Fax3SetupState",
517 			    "%s: No space for Group 3/4 reference line",
518 			    tif->tif_name);
519 			return (0);
520 		}
521 	} else					/* 1d encoding */
522 		EncoderState(tif)->refline = NULL;
523 
524 	return (1);
525 }
526 
527 /*
528  * CCITT Group 3 FAX Encoding.
529  */
530 
531 #ifdef PDFLIB_TIFFWRITE_SUPPORT
532 #define	Fax3FlushBits(tif, sp) {				\
533 	if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize)		\
534 		(void) TIFFFlushData1(tif);			\
535 	*(tif)->tif_rawcp++ = (tidataval_t) (sp)->data;		\
536 	(tif)->tif_rawcc++;					\
537 	(sp)->data = 0, (sp)->bit = 8;				\
538 }
539 #define	_FlushBits(tif) {					\
540 	if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize)		\
541 		(void) TIFFFlushData1(tif);			\
542 	*(tif)->tif_rawcp++ = (tidataval_t) data;		\
543 	(tif)->tif_rawcc++;					\
544 	data = 0, bit = 8;					\
545 }
546 static const int _msbmask[9] =
547     { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
548 #define	_PutBits(tif, bits, length) {				\
549 	while (length > bit) {					\
550 		data |= bits >> (length - bit);			\
551 		length -= bit;					\
552 		_FlushBits(tif);				\
553 	}							\
554 	data |= (bits & _msbmask[length]) << (bit - length);	\
555 	bit -= length;						\
556 	if (bit == 0)						\
557 		_FlushBits(tif);				\
558 }
559 
560 /*
561  * Write a variable-length bit-value to
562  * the output stream.  Values are
563  * assumed to be at most 16 bits.
564  */
565 static void
Fax3PutBits(TIFF * tif,unsigned int bits,unsigned int length)566 Fax3PutBits(TIFF* tif, unsigned int bits, unsigned int length)
567 {
568 	Fax3CodecState* sp = EncoderState(tif);
569 	unsigned int bit = sp->bit;
570 	int data = sp->data;
571 
572 	_PutBits(tif, bits, length);
573 
574 	sp->data = data;
575 	sp->bit = bit;
576 }
577 
578 /*
579  * Write a code to the output stream.
580  */
581 #define putcode(tif, te)	Fax3PutBits(tif, (te)->code, (te)->length)
582 
583 #ifdef FAX3_DEBUG
584 #define	DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B")
585 #define	DEBUG_PRINT(what,len) {						\
586     int t;								\
587     printf("%08X/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what), len);	\
588     for (t = length-1; t >= 0; t--)					\
589 	putchar(code & (1<<t) ? '1' : '0');				\
590     putchar('\n');							\
591 }
592 #endif
593 
594 /*
595  * Write the sequence of codes that describes
596  * the specified span of zero's or one's.  The
597  * appropriate table that holds the make-up and
598  * terminating codes is supplied.
599  */
600 static void
putspan(TIFF * tif,int32 span,const tableentry * tab)601 putspan(TIFF* tif, int32 span, const tableentry* tab)
602 {
603 	Fax3CodecState* sp = EncoderState(tif);
604 	unsigned int bit = sp->bit;
605 	int data = sp->data;
606 	unsigned int code, length;
607 
608 	while (span >= 2624) {
609 		const tableentry* te = &tab[63 + (2560>>6)];
610 		code = te->code, length = te->length;
611 #ifdef FAX3_DEBUG
612 		DEBUG_PRINT("MakeUp", te->runlen);
613 #endif
614 		_PutBits(tif, code, length);
615 		span -= te->runlen;
616 	}
617 	if (span >= 64) {
618 		const tableentry* te = &tab[63 + (span>>6)];
619 		assert(te->runlen == 64*(span>>6));
620 		code = te->code, length = te->length;
621 #ifdef FAX3_DEBUG
622 		DEBUG_PRINT("MakeUp", te->runlen);
623 #endif
624 		_PutBits(tif, code, length);
625 		span -= te->runlen;
626 	}
627 	code = tab[span].code, length = tab[span].length;
628 #ifdef FAX3_DEBUG
629 	DEBUG_PRINT("  Term", tab[span].runlen);
630 #endif
631 	_PutBits(tif, code, length);
632 
633 	sp->data = data;
634 	sp->bit = bit;
635 }
636 
637 /*
638  * Write an EOL code to the output stream.  The zero-fill
639  * logic for byte-aligning encoded scanlines is handled
640  * here.  We also handle writing the tag bit for the next
641  * scanline when doing 2d encoding.
642  */
643 static void
Fax3PutEOL(TIFF * tif)644 Fax3PutEOL(TIFF* tif)
645 {
646 	Fax3CodecState* sp = EncoderState(tif);
647 	unsigned int bit = sp->bit;
648 	int data = sp->data;
649 	unsigned int code, length, tparm;
650 
651 	if (sp->b.groupoptions & GROUP3OPT_FILLBITS) {
652 		/*
653 		 * Force bit alignment so EOL will terminate on
654 		 * a byte boundary.  That is, force the bit alignment
655 		 * to 16-12 = 4 before putting out the EOL code.
656 		 */
657 		int align = 8 - 4;
658 		if (align != sp->bit) {
659 			if (align > sp->bit)
660 				align = sp->bit + (8 - align);
661 			else
662 				align = sp->bit - align;
663 			code = 0;
664 			tparm=align;
665 			_PutBits(tif, 0, tparm);
666 		}
667 	}
668 	code = EOL, length = 12;
669 	if (is2DEncoding(sp))
670 		code = (code<<1) | (sp->tag == G3_1D), length++;
671 	_PutBits(tif, code, length);
672 
673 	sp->data = data;
674 	sp->bit = bit;
675 }
676 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
677 
678 /*
679  * Reset encoding state at the start of a strip.
680  */
681 #ifdef PDFLIB_TIFFWRITE_SUPPORT
682 static int
Fax3PreEncode(TIFF * tif,tsample_t s)683 Fax3PreEncode(TIFF* tif, tsample_t s)
684 {
685 	Fax3CodecState* sp = EncoderState(tif);
686 
687 	(void) s;
688 	assert(sp != NULL);
689 	sp->bit = 8;
690 	sp->data = 0;
691 	sp->tag = G3_1D;
692 	/*
693 	 * This is necessary for Group 4; otherwise it isn't
694 	 * needed because the first scanline of each strip ends
695 	 * up being copied into the refline.
696 	 */
697 	if (sp->refline)
698 		_TIFFmemset(sp->refline, 0x00, sp->b.rowbytes);
699 	if (is2DEncoding(sp)) {
700 		float res = tif->tif_dir.td_yresolution;
701 		/*
702 		 * The CCITT spec says that when doing 2d encoding, you
703 		 * should only do it on K consecutive scanlines, where K
704 		 * depends on the resolution of the image being encoded
705 		 * (2 for <= 200 lpi, 4 for > 200 lpi).  Since the directory
706 		 * code initializes td_yresolution to 0, this code will
707 		 * select a K of 2 unless the YResolution tag is set
708 		 * appropriately.  (Note also that we fudge a little here
709 		 * and use 150 lpi to avoid problems with units conversion.)
710 		 */
711 		if (tif->tif_dir.td_resolutionunit == RESUNIT_CENTIMETER)
712 			res *= 2.54f;		/* convert to inches */
713 		sp->maxk = (res > 150 ? 4 : 2);
714 		sp->k = sp->maxk-1;
715 	} else
716 		sp->k = sp->maxk = 0;
717 	return (1);
718 }
719 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
720 
721 static const unsigned char zeroruns[256] = {
722     8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,	/* 0x00 - 0x0f */
723     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,	/* 0x10 - 0x1f */
724     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,	/* 0x20 - 0x2f */
725     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,	/* 0x30 - 0x3f */
726     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x40 - 0x4f */
727     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x50 - 0x5f */
728     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x60 - 0x6f */
729     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x70 - 0x7f */
730     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x80 - 0x8f */
731     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x90 - 0x9f */
732     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xa0 - 0xaf */
733     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xb0 - 0xbf */
734     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xc0 - 0xcf */
735     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xd0 - 0xdf */
736     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xe0 - 0xef */
737     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xf0 - 0xff */
738 };
739 static const unsigned char oneruns[256] = {
740     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x00 - 0x0f */
741     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x10 - 0x1f */
742     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x20 - 0x2f */
743     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x30 - 0x3f */
744     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x40 - 0x4f */
745     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x50 - 0x5f */
746     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x60 - 0x6f */
747     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x70 - 0x7f */
748     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x80 - 0x8f */
749     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x90 - 0x9f */
750     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0xa0 - 0xaf */
751     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0xb0 - 0xbf */
752     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,	/* 0xc0 - 0xcf */
753     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,	/* 0xd0 - 0xdf */
754     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,	/* 0xe0 - 0xef */
755     4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8,	/* 0xf0 - 0xff */
756 };
757 
758 /*
759  * On certain systems it pays to inline
760  * the routines that find pixel spans.
761  */
762 #ifdef VAXC
763 static	int32 find0span(unsigned char*, int32, int32);
764 static	int32 find1span(unsigned char*, int32, int32);
765 #pragma inline(find0span,find1span)
766 #endif
767 
768 /*
769  * Find a span of ones or zeros using the supplied
770  * table.  The ``base'' of the bit string is supplied
771  * along with the start+end bit indices.
772  */
773 #ifdef PDFLIB_TIFFWRITE_SUPPORT
774 inline static int32
find0span(unsigned char * bp,int32 bs,int32 be)775 find0span(unsigned char* bp, int32 bs, int32 be)
776 {
777 	int32 bits = be - bs;
778 	int32 n, span;
779 
780 	bp += bs>>3;
781 	/*
782 	 * Check partial byte on lhs.
783 	 */
784 	if (bits > 0 && (n = (bs & 7))) {
785 		span = zeroruns[(*bp << n) & 0xff];
786 		if (span > 8-n)		/* table value too generous */
787 			span = 8-n;
788 		if (span > bits)	/* constrain span to bit range */
789 			span = bits;
790 		if (n+span < 8)		/* doesn't extend to edge of byte */
791 			return (span);
792 		bits -= span;
793 		bp++;
794 	} else
795 		span = 0;
796 	if (bits >= (int)(2*8*sizeof (long))) {
797 		long* lp;
798 		/*
799 		 * Align to longword boundary and check longwords.
800 		 */
801 		while (!isAligned(bp, long)) {
802 			if (*bp != 0x00)
803 				return (span + zeroruns[*bp]);
804 			span += 8, bits -= 8;
805 			bp++;
806 		}
807 		lp = (long*) bp;
808 		while (bits >= (int)(8*sizeof (long)) && *lp == 0) {
809 			span += 8*sizeof (long), bits -= 8*sizeof (long);
810 			lp++;
811 		}
812 		bp = (unsigned char*) lp;
813 	}
814 	/*
815 	 * Scan full bytes for all 0's.
816 	 */
817 	while (bits >= 8) {
818 		if (*bp != 0x00)	/* end of run */
819 			return (span + zeroruns[*bp]);
820 		span += 8, bits -= 8;
821 		bp++;
822 	}
823 	/*
824 	 * Check partial byte on rhs.
825 	 */
826 	if (bits > 0) {
827 		n = zeroruns[*bp];
828 		span += (n > bits ? bits : n);
829 	}
830 	return (span);
831 }
832 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
833 
834 #ifdef PDFLIB_TIFFWRITE_SUPPORT
835 inline static int32
find1span(unsigned char * bp,int32 bs,int32 be)836 find1span(unsigned char* bp, int32 bs, int32 be)
837 {
838 	int32 bits = be - bs;
839 	int32 n, span;
840 
841 	bp += bs>>3;
842 	/*
843 	 * Check partial byte on lhs.
844 	 */
845 	if (bits > 0 && (n = (bs & 7))) {
846 		span = oneruns[(*bp << n) & 0xff];
847 		if (span > 8-n)		/* table value too generous */
848 			span = 8-n;
849 		if (span > bits)	/* constrain span to bit range */
850 			span = bits;
851 		if (n+span < 8)		/* doesn't extend to edge of byte */
852 			return (span);
853 		bits -= span;
854 		bp++;
855 	} else
856 		span = 0;
857 	if (bits >= (int)(2*8*sizeof (long))) {
858 		long* lp;
859 		/*
860 		 * Align to longword boundary and check longwords.
861 		 */
862 		while (!isAligned(bp, long)) {
863 			if (*bp != 0xff)
864 				return (span + oneruns[*bp]);
865 			span += 8, bits -= 8;
866 			bp++;
867 		}
868 		lp = (long*) bp;
869 		while (bits >= (int)(8*sizeof (long)) && *lp == ~0) {
870 			span += 8*sizeof (long), bits -= 8*sizeof (long);
871 			lp++;
872 		}
873 		bp = (unsigned char*) lp;
874 	}
875 	/*
876 	 * Scan full bytes for all 1's.
877 	 */
878 	while (bits >= 8) {
879 		if (*bp != 0xff)	/* end of run */
880 			return (span + oneruns[*bp]);
881 		span += 8, bits -= 8;
882 		bp++;
883 	}
884 	/*
885 	 * Check partial byte on rhs.
886 	 */
887 	if (bits > 0) {
888 		n = oneruns[*bp];
889 		span += (n > bits ? bits : n);
890 	}
891 	return (span);
892 }
893 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
894 
895 /*
896  * Return the offset of the next bit in the range
897  * [bs..be] that is different from the specified
898  * color.  The end, be, is returned if no such bit
899  * exists.
900  */
901 #define	finddiff(_cp, _bs, _be, _color)	\
902 	(_bs + (_color ? find1span(_cp,_bs,_be) : find0span(_cp,_bs,_be)))
903 /*
904  * Like finddiff, but also check the starting bit
905  * against the end in case start > end.
906  */
907 #define	finddiff2(_cp, _bs, _be, _color) \
908 	(_bs < _be ? finddiff(_cp,_bs,_be,_color) : _be)
909 
910 /*
911  * 1d-encode a row of pixels.  The encoding is
912  * a sequence of all-white or all-black spans
913  * of pixels encoded with Huffman codes.
914  */
915 #ifdef PDFLIB_TIFFWRITE_SUPPORT
916 static int
Fax3Encode1DRow(TIFF * tif,unsigned char * bp,uint32 bits)917 Fax3Encode1DRow(TIFF* tif, unsigned char* bp, uint32 bits)
918 {
919 	Fax3CodecState* sp = EncoderState(tif);
920 	int32 span;
921         uint32 bs = 0;
922 
923 	for (;;) {
924 		span = find0span(bp, bs, bits);		/* white span */
925 		putspan(tif, span, TIFFFaxWhiteCodes);
926 		bs += span;
927 		if (bs >= bits)
928 			break;
929 		span = find1span(bp, bs, bits);		/* black span */
930 		putspan(tif, span, TIFFFaxBlackCodes);
931 		bs += span;
932 		if (bs >= bits)
933 			break;
934 	}
935 	if (sp->b.mode & (FAXMODE_BYTEALIGN|FAXMODE_WORDALIGN)) {
936 		if (sp->bit != 8)			/* byte-align */
937 			Fax3FlushBits(tif, sp);
938 		if ((sp->b.mode&FAXMODE_WORDALIGN) &&
939 		    !isAligned(tif->tif_rawcp, uint16))
940 			Fax3FlushBits(tif, sp);
941 	}
942 	return (1);
943 }
944 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
945 
946 static const tableentry horizcode =
947     { 3, 0x1 };		/* 001 */
948 static const tableentry passcode =
949     { 4, 0x1 };		/* 0001 */
950 static const tableentry vcodes[7] = {
951     { 7, 0x03 },	/* 0000 011 */
952     { 6, 0x03 },	/* 0000 11 */
953     { 3, 0x03 },	/* 011 */
954     { 1, 0x1 },		/* 1 */
955     { 3, 0x2 },		/* 010 */
956     { 6, 0x02 },	/* 0000 10 */
957     { 7, 0x02 }		/* 0000 010 */
958 };
959 
960 /*
961  * 2d-encode a row of pixels.  Consult the CCITT
962  * documentation for the algorithm.
963  */
964 #ifdef PDFLIB_TIFFWRITE_SUPPORT
965 static int
Fax3Encode2DRow(TIFF * tif,unsigned char * bp,unsigned char * rp,uint32 bits)966 Fax3Encode2DRow(TIFF* tif, unsigned char* bp, unsigned char* rp, uint32 bits)
967 {
968 #define	PIXEL(buf,ix)	((((buf)[(ix)>>3]) >> (7-((ix)&7))) & 1)
969         uint32 a0 = 0;
970 	uint32 a1 = (PIXEL(bp, 0) != 0 ? 0 : finddiff(bp, 0, bits, 0));
971 	uint32 b1 = (PIXEL(rp, 0) != 0 ? 0 : finddiff(rp, 0, bits, 0));
972 	uint32 a2, b2;
973 
974 	for (;;) {
975 		b2 = finddiff2(rp, b1, bits, PIXEL(rp,b1));
976 		if (b2 >= a1) {
977 			int32 d = b1 - a1;
978 			if (!(-3 <= d && d <= 3)) {	/* horizontal mode */
979 				a2 = finddiff2(bp, a1, bits, PIXEL(bp,a1));
980 				putcode(tif, &horizcode);
981 				if (a0+a1 == 0 || PIXEL(bp, a0) == 0) {
982 					putspan(tif, a1-a0, TIFFFaxWhiteCodes);
983 					putspan(tif, a2-a1, TIFFFaxBlackCodes);
984 				} else {
985 					putspan(tif, a1-a0, TIFFFaxBlackCodes);
986 					putspan(tif, a2-a1, TIFFFaxWhiteCodes);
987 				}
988 				a0 = a2;
989 			} else {			/* vertical mode */
990 				putcode(tif, &vcodes[d+3]);
991 				a0 = a1;
992 			}
993 		} else {				/* pass mode */
994 			putcode(tif, &passcode);
995 			a0 = b2;
996 		}
997 		if (a0 >= bits)
998 			break;
999 		a1 = finddiff(bp, a0, bits, PIXEL(bp,a0));
1000 		b1 = finddiff(rp, a0, bits, !PIXEL(bp,a0));
1001 		b1 = finddiff(rp, b1, bits, PIXEL(bp,a0));
1002 	}
1003 	return (1);
1004 #undef PIXEL
1005 }
1006 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
1007 
1008 /*
1009  * Encode a buffer of pixels.
1010  */
1011 #ifdef PDFLIB_TIFFWRITE_SUPPORT
1012 static int
Fax3Encode(TIFF * tif,tidata_t bp,tsize_t cc,tsample_t s)1013 Fax3Encode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
1014 {
1015 	Fax3CodecState* sp = EncoderState(tif);
1016 
1017 	(void) s;
1018 	while ((long)cc > 0) {
1019 		if ((sp->b.mode & FAXMODE_NOEOL) == 0)
1020 			Fax3PutEOL(tif);
1021 		if (is2DEncoding(sp)) {
1022 			if (sp->tag == G3_1D) {
1023 				if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels))
1024 					return (0);
1025 				sp->tag = G3_2D;
1026 			} else {
1027 				if (!Fax3Encode2DRow(tif, bp, sp->refline,
1028                                                      sp->b.rowpixels))
1029 					return (0);
1030 				sp->k--;
1031 			}
1032 			if (sp->k == 0) {
1033 				sp->tag = G3_1D;
1034 				sp->k = sp->maxk-1;
1035 			} else
1036 				_TIFFmemcpy(sp->refline, bp, sp->b.rowbytes);
1037 		} else {
1038 			if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels))
1039 				return (0);
1040 		}
1041 		bp += sp->b.rowbytes;
1042 		cc -= sp->b.rowbytes;
1043 	}
1044 	return (1);
1045 }
1046 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
1047 
1048 #ifdef PDFLIB_TIFFWRITE_SUPPORT
1049 static int
Fax3PostEncode(TIFF * tif)1050 Fax3PostEncode(TIFF* tif)
1051 {
1052 	Fax3CodecState* sp = EncoderState(tif);
1053 
1054 	if (sp->bit != 8)
1055 		Fax3FlushBits(tif, sp);
1056 	return (1);
1057 }
1058 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
1059 
1060 #ifdef PDFLIB_TIFFWRITE_SUPPORT
1061 static void
Fax3Close(TIFF * tif)1062 Fax3Close(TIFF* tif)
1063 {
1064 	if ((Fax3State(tif)->mode & FAXMODE_NORTC) == 0) {
1065 		Fax3CodecState* sp = EncoderState(tif);
1066 		unsigned int code = EOL;
1067 		unsigned int length = 12;
1068 		int i;
1069 
1070 		if (is2DEncoding(sp))
1071 			code = (code<<1) | (sp->tag == G3_1D), length++;
1072 		for (i = 0; i < 6; i++)
1073 			Fax3PutBits(tif, code, length);
1074 		Fax3FlushBits(tif, sp);
1075 	}
1076 }
1077 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
1078 
1079 static void
Fax3Cleanup(TIFF * tif)1080 Fax3Cleanup(TIFF* tif)
1081 {
1082 	if (tif->tif_data) {
1083 		Fax3CodecState* sp = DecoderState(tif);
1084 
1085 		if (sp->runs)
1086 			_TIFFfree(sp->runs);
1087 		if (sp->refline)
1088 			_TIFFfree(sp->refline);
1089 
1090 		if (Fax3State(tif)->subaddress)
1091 			_TIFFfree(Fax3State(tif)->subaddress);
1092 		_TIFFfree(tif->tif_data);
1093 		tif->tif_data = NULL;
1094 	}
1095 }
1096 
1097 #define	FIELD_BADFAXLINES	(FIELD_CODEC+0)
1098 #define	FIELD_CLEANFAXDATA	(FIELD_CODEC+1)
1099 #define	FIELD_BADFAXRUN		(FIELD_CODEC+2)
1100 #define	FIELD_RECVPARAMS	(FIELD_CODEC+3)
1101 #define	FIELD_SUBADDRESS	(FIELD_CODEC+4)
1102 #define	FIELD_RECVTIME		(FIELD_CODEC+5)
1103 #define	FIELD_FAXDCS		(FIELD_CODEC+6)
1104 
1105 #define	FIELD_OPTIONS		(FIELD_CODEC+7)
1106 
1107 static const TIFFFieldInfo faxFieldInfo[] = {
1108     { TIFFTAG_FAXMODE,		 0, 0,	TIFF_ANY,	FIELD_PSEUDO,
1109       FALSE,	FALSE,	"FaxMode" },
1110     { TIFFTAG_FAXFILLFUNC,	 0, 0,	TIFF_ANY,	FIELD_PSEUDO,
1111       FALSE,	FALSE,	"FaxFillFunc" },
1112     { TIFFTAG_BADFAXLINES,	 1, 1,	TIFF_LONG,	FIELD_BADFAXLINES,
1113       TRUE,	FALSE,	"BadFaxLines" },
1114     { TIFFTAG_BADFAXLINES,	 1, 1,	TIFF_SHORT,	FIELD_BADFAXLINES,
1115       TRUE,	FALSE,	"BadFaxLines" },
1116     { TIFFTAG_CLEANFAXDATA,	 1, 1,	TIFF_SHORT,	FIELD_CLEANFAXDATA,
1117       TRUE,	FALSE,	"CleanFaxData" },
1118     { TIFFTAG_CONSECUTIVEBADFAXLINES,1,1, TIFF_LONG,	FIELD_BADFAXRUN,
1119       TRUE,	FALSE,	"ConsecutiveBadFaxLines" },
1120     { TIFFTAG_CONSECUTIVEBADFAXLINES,1,1, TIFF_SHORT,	FIELD_BADFAXRUN,
1121       TRUE,	FALSE,	"ConsecutiveBadFaxLines" },
1122     { TIFFTAG_FAXRECVPARAMS,	 1, 1, TIFF_LONG,	FIELD_RECVPARAMS,
1123       TRUE,	FALSE,	"FaxRecvParams" },
1124     { TIFFTAG_FAXSUBADDRESS,	-1,-1, TIFF_ASCII,	FIELD_SUBADDRESS,
1125       TRUE,	FALSE,	"FaxSubAddress" },
1126     { TIFFTAG_FAXRECVTIME,	 1, 1, TIFF_LONG,	FIELD_RECVTIME,
1127       TRUE,	FALSE,	"FaxRecvTime" },
1128     { TIFFTAG_FAXDCS,		-1,-1, TIFF_ASCII,	FIELD_FAXDCS,
1129       TRUE,	FALSE,	"FaxDcs" },
1130 };
1131 static const TIFFFieldInfo fax3FieldInfo[] = {
1132     { TIFFTAG_GROUP3OPTIONS,	 1, 1,	TIFF_LONG,	FIELD_OPTIONS,
1133       FALSE,	FALSE,	"Group3Options" },
1134 };
1135 static const TIFFFieldInfo fax4FieldInfo[] = {
1136     { TIFFTAG_GROUP4OPTIONS,	 1, 1,	TIFF_LONG,	FIELD_OPTIONS,
1137       FALSE,	FALSE,	"Group4Options" },
1138 };
1139 #define	N(a)	(sizeof (a) / sizeof (a[0]))
1140 
1141 static int
Fax3VSetField(TIFF * tif,ttag_t tag,va_list ap)1142 Fax3VSetField(TIFF* tif, ttag_t tag, va_list ap)
1143 {
1144 	Fax3BaseState* sp = Fax3State(tif);
1145 
1146 	switch (tag) {
1147 	case TIFFTAG_FAXMODE:
1148 		sp->mode = va_arg(ap, int);
1149 		return (1);			/* NB: pseudo tag */
1150 	case TIFFTAG_FAXFILLFUNC:
1151 		DecoderState(tif)->fill = va_arg(ap, TIFFFaxFillFunc);
1152 		return (1);			/* NB: pseudo tag */
1153 	case TIFFTAG_GROUP3OPTIONS:
1154 		/* XXX: avoid reading options if compression mismatches. */
1155 	        if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX3)
1156 			sp->groupoptions = va_arg(ap, uint32);
1157 		break;
1158 	case TIFFTAG_GROUP4OPTIONS:
1159 		/* XXX: avoid reading options if compression mismatches. */
1160 		if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4)
1161 			sp->groupoptions = va_arg(ap, uint32);
1162 		break;
1163 	case TIFFTAG_BADFAXLINES:
1164 		sp->badfaxlines = va_arg(ap, uint32);
1165 		break;
1166 	case TIFFTAG_CLEANFAXDATA:
1167 		sp->cleanfaxdata = (uint16) va_arg(ap, int);
1168 		break;
1169 	case TIFFTAG_CONSECUTIVEBADFAXLINES:
1170 		sp->badfaxrun = va_arg(ap, uint32);
1171 		break;
1172 	case TIFFTAG_FAXRECVPARAMS:
1173 		sp->recvparams = va_arg(ap, uint32);
1174 		break;
1175 	case TIFFTAG_FAXSUBADDRESS:
1176 		_TIFFsetString(tif, &sp->subaddress, va_arg(ap, char*));
1177 		break;
1178 	case TIFFTAG_FAXRECVTIME:
1179 		sp->recvtime = va_arg(ap, uint32);
1180 		break;
1181 	case TIFFTAG_FAXDCS:
1182 		_TIFFsetString(tif, &sp->faxdcs, va_arg(ap, char*));
1183 		break;
1184 	default:
1185 		return (*sp->vsetparent)(tif, tag, ap);
1186 	}
1187 	TIFFSetFieldBit(tif, TIFFFieldWithTag(tif, tag)->field_bit);
1188 	tif->tif_flags |= TIFF_DIRTYDIRECT;
1189 	return (1);
1190 }
1191 
1192 static int
Fax3VGetField(TIFF * tif,ttag_t tag,va_list ap)1193 Fax3VGetField(TIFF* tif, ttag_t tag, va_list ap)
1194 {
1195 	Fax3BaseState* sp = Fax3State(tif);
1196 
1197 	switch (tag) {
1198 	case TIFFTAG_FAXMODE:
1199 		*va_arg(ap, int*) = sp->mode;
1200 		break;
1201 	case TIFFTAG_FAXFILLFUNC:
1202 		*va_arg(ap, TIFFFaxFillFunc*) = DecoderState(tif)->fill;
1203 		break;
1204 	case TIFFTAG_GROUP3OPTIONS:
1205 	case TIFFTAG_GROUP4OPTIONS:
1206 		*va_arg(ap, uint32*) = sp->groupoptions;
1207 		break;
1208 	case TIFFTAG_BADFAXLINES:
1209 		*va_arg(ap, uint32*) = sp->badfaxlines;
1210 		break;
1211 	case TIFFTAG_CLEANFAXDATA:
1212 		*va_arg(ap, uint16*) = sp->cleanfaxdata;
1213 		break;
1214 	case TIFFTAG_CONSECUTIVEBADFAXLINES:
1215 		*va_arg(ap, uint32*) = sp->badfaxrun;
1216 		break;
1217 	case TIFFTAG_FAXRECVPARAMS:
1218 		*va_arg(ap, uint32*) = sp->recvparams;
1219 		break;
1220 	case TIFFTAG_FAXSUBADDRESS:
1221 		*va_arg(ap, char**) = sp->subaddress;
1222 		break;
1223 	case TIFFTAG_FAXRECVTIME:
1224 		*va_arg(ap, uint32*) = sp->recvtime;
1225 		break;
1226 	case TIFFTAG_FAXDCS:
1227 		*va_arg(ap, char**) = sp->faxdcs;
1228 		break;
1229 	default:
1230 		return (*sp->vgetparent)(tif, tag, ap);
1231 	}
1232 	return (1);
1233 }
1234 
1235 #ifdef PDFLIB_TIFFWRITE_SUPPORT
1236 static void
Fax3PrintDir(TIFF * tif,FILE * fd,long flags)1237 Fax3PrintDir(TIFF* tif, FILE* fd, long flags)
1238 {
1239 	Fax3BaseState* sp = Fax3State(tif);
1240 
1241 	(void) flags;
1242 	if (TIFFFieldSet(tif,FIELD_OPTIONS)) {
1243 		const char* sep = " ";
1244 		if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4) {
1245 			fprintf(fd, "  Group 4 Options:");
1246 			if (sp->groupoptions & GROUP4OPT_UNCOMPRESSED)
1247 				fprintf(fd, "%suncompressed data", sep);
1248 		} else {
1249 
1250 			fprintf(fd, "  Group 3 Options:");
1251 			if (sp->groupoptions & GROUP3OPT_2DENCODING)
1252 				fprintf(fd, "%s2-d encoding", sep), sep = "+";
1253 			if (sp->groupoptions & GROUP3OPT_FILLBITS)
1254 				fprintf(fd, "%sEOL padding", sep), sep = "+";
1255 			if (sp->groupoptions & GROUP3OPT_UNCOMPRESSED)
1256 				fprintf(fd, "%suncompressed data", sep);
1257 		}
1258 		fprintf(fd, " (%lu = 0x%lx)\n",
1259                         (unsigned long) sp->groupoptions,
1260                         (unsigned long) sp->groupoptions);
1261 	}
1262 	if (TIFFFieldSet(tif,FIELD_CLEANFAXDATA)) {
1263 		fprintf(fd, "  Fax Data:");
1264 		switch (sp->cleanfaxdata) {
1265 		case CLEANFAXDATA_CLEAN:
1266 			fprintf(fd, " clean");
1267 			break;
1268 		case CLEANFAXDATA_REGENERATED:
1269 			fprintf(fd, " receiver regenerated");
1270 			break;
1271 		case CLEANFAXDATA_UNCLEAN:
1272 			fprintf(fd, " uncorrected errors");
1273 			break;
1274 		}
1275 		fprintf(fd, " (%u = 0x%x)\n",
1276 		    sp->cleanfaxdata, sp->cleanfaxdata);
1277 	}
1278 	if (TIFFFieldSet(tif,FIELD_BADFAXLINES))
1279 		fprintf(fd, "  Bad Fax Lines: %lu\n",
1280                         (unsigned long) sp->badfaxlines);
1281 	if (TIFFFieldSet(tif,FIELD_BADFAXRUN))
1282 		fprintf(fd, "  Consecutive Bad Fax Lines: %lu\n",
1283 		    (unsigned long) sp->badfaxrun);
1284 	if (TIFFFieldSet(tif,FIELD_RECVPARAMS))
1285 		fprintf(fd, "  Fax Receive Parameters: %08lx\n",
1286 		   (unsigned long) sp->recvparams);
1287 	if (TIFFFieldSet(tif,FIELD_SUBADDRESS))
1288 		fprintf(fd, "  Fax SubAddress: %s\n", sp->subaddress);
1289 	if (TIFFFieldSet(tif,FIELD_RECVTIME))
1290 		fprintf(fd, "  Fax Receive Time: %lu secs\n",
1291 		    (unsigned long) sp->recvtime);
1292 	if (TIFFFieldSet(tif,FIELD_FAXDCS))
1293 		fprintf(fd, "  Fax DCS: %s\n", sp->faxdcs);
1294 }
1295 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
1296 
1297 static int
InitCCITTFax3(TIFF * tif)1298 InitCCITTFax3(TIFF* tif)
1299 {
1300 	Fax3BaseState* sp;
1301 
1302 	/*
1303 	 * Allocate state block so tag methods have storage to record values.
1304 	 */
1305 	tif->tif_data = (tidata_t)
1306 		_TIFFmalloc(sizeof (Fax3CodecState));
1307 
1308 	if (tif->tif_data == NULL) {
1309 		_TIFFError(tif, "TIFFInitCCITTFax3",
1310 		    "%s: No space for state block", tif->tif_name);
1311 		return (0);
1312 	}
1313 
1314 	sp = Fax3State(tif);
1315         sp->rw_mode = tif->tif_mode;
1316 
1317 	/*
1318 	 * Merge codec-specific tag information and
1319 	 * override parent get/set field methods.
1320 	 */
1321 	TIFFMergeFieldInfo(tif, faxFieldInfo, N(faxFieldInfo));
1322 	sp->vgetparent = tif->tif_tagmethods.vgetfield;
1323 	tif->tif_tagmethods.vgetfield = Fax3VGetField; /* hook for codec tags */
1324 	sp->vsetparent = tif->tif_tagmethods.vsetfield;
1325 	tif->tif_tagmethods.vsetfield = Fax3VSetField; /* hook for codec tags */
1326 #ifdef PDFLIB_TIFFWRITE_SUPPORT
1327 	tif->tif_tagmethods.printdir = Fax3PrintDir;   /* hook for codec tags */
1328 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
1329 	sp->groupoptions = 0;
1330 	sp->recvparams = 0;
1331 	sp->subaddress = NULL;
1332 	sp->faxdcs = NULL;
1333 
1334 	if (sp->rw_mode == O_RDONLY) /* FIXME: improve for in place update */
1335 		tif->tif_flags |= TIFF_NOBITREV; /* decoder does bit reversal */
1336 	DecoderState(tif)->runs = NULL;
1337 	TIFFSetField(tif, TIFFTAG_FAXFILLFUNC, _TIFFFax3fillruns);
1338 	EncoderState(tif)->refline = NULL;
1339 
1340 	/*
1341 	 * Install codec methods.
1342 	 */
1343 	tif->tif_setupdecode = Fax3SetupState;
1344 	tif->tif_predecode = Fax3PreDecode;
1345 	tif->tif_decoderow = Fax3Decode1D;
1346 	tif->tif_decodestrip = Fax3Decode1D;
1347 	tif->tif_decodetile = Fax3Decode1D;
1348 	tif->tif_setupencode = Fax3SetupState;
1349 #ifdef PDFLIB_TIFFWRITE_SUPPORT
1350 	tif->tif_preencode = Fax3PreEncode;
1351 	tif->tif_postencode = Fax3PostEncode;
1352 	tif->tif_encoderow = Fax3Encode;
1353 	tif->tif_encodestrip = Fax3Encode;
1354 	tif->tif_encodetile = Fax3Encode;
1355 	tif->tif_close = Fax3Close;
1356 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
1357 	tif->tif_cleanup = Fax3Cleanup;
1358 
1359 	return (1);
1360 }
1361 
1362 int
TIFFInitCCITTFax3(TIFF * tif,int scheme)1363 TIFFInitCCITTFax3(TIFF* tif, int scheme)
1364 {
1365 	(void) scheme;
1366 
1367 	if (InitCCITTFax3(tif)) {
1368 		TIFFMergeFieldInfo(tif, fax3FieldInfo, N(fax3FieldInfo));
1369 
1370 		/*
1371 		 * The default format is Class/F-style w/o RTC.
1372 		 */
1373 		return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_CLASSF);
1374 	} else
1375 		return (0);
1376 }
1377 
1378 /*
1379  * CCITT Group 4 (T.6) Facsimile-compatible
1380  * Compression Scheme Support.
1381  */
1382 
1383 #define	SWAP(t,a,b)	{ t x; x = (a); (a) = (b); (b) = x; }
1384 /*
1385  * Decode the requested amount of G4-encoded data.
1386  */
1387 static int
Fax4Decode(TIFF * tif,tidata_t buf,tsize_t occ,tsample_t s)1388 Fax4Decode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
1389 {
1390 	DECLARE_STATE_2D(tif, sp, "Fax4Decode");
1391 
1392 	(void) s;
1393 	CACHE_STATE(tif, sp);
1394 	while ((long)occ > 0) {
1395 		a0 = 0;
1396 		RunLength = 0;
1397 		pa = thisrun = sp->curruns;
1398 		pb = sp->refruns;
1399 		b1 = *pb++;
1400 #ifdef FAX3_DEBUG
1401 		printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail);
1402 		printf("-------------------- %d\n", tif->tif_row);
1403 		fflush(stdout);
1404 #endif
1405 		EXPAND2D(EOFG4);
1406                 if (EOLcnt)
1407                     goto EOFG4;
1408 		(*sp->fill)(buf, thisrun, pa, lastx);
1409 		SETVAL(0);		/* imaginary change for reference */
1410 		SWAP(uint32*, sp->curruns, sp->refruns);
1411 		buf += sp->b.rowbytes;
1412 		occ -= sp->b.rowbytes;
1413 		continue;
1414 	EOFG4:
1415                 NeedBits16( 13, BADG4 );
1416         BADG4:
1417 #ifdef FAX3_DEBUG
1418                 if( GetBits(13) != 0x1001 )
1419                     fputs( "Bad RTC\n", stderr );
1420 #endif
1421                 ClrBits( 13 );
1422 		(*sp->fill)(buf, thisrun, pa, lastx);
1423 		UNCACHE_STATE(tif, sp);
1424 		return (-1);
1425 	}
1426 	UNCACHE_STATE(tif, sp);
1427 	return (1);
1428 }
1429 #undef	SWAP
1430 
1431 /*
1432  * Encode the requested amount of data.
1433  */
1434 #ifdef PDFLIB_TIFFWRITE_SUPPORT
1435 static int
Fax4Encode(TIFF * tif,tidata_t bp,tsize_t cc,tsample_t s)1436 Fax4Encode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
1437 {
1438 	Fax3CodecState *sp = EncoderState(tif);
1439 
1440 	(void) s;
1441 	while ((long)cc > 0) {
1442 		if (!Fax3Encode2DRow(tif, bp, sp->refline, sp->b.rowpixels))
1443 			return (0);
1444 		_TIFFmemcpy(sp->refline, bp, sp->b.rowbytes);
1445 		bp += sp->b.rowbytes;
1446 		cc -= sp->b.rowbytes;
1447 	}
1448 	return (1);
1449 }
1450 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
1451 
1452 #ifdef PDFLIB_TIFFWRITE_SUPPORT
1453 static int
Fax4PostEncode(TIFF * tif)1454 Fax4PostEncode(TIFF* tif)
1455 {
1456 	Fax3CodecState *sp = EncoderState(tif);
1457 
1458 	/* terminate strip w/ EOFB */
1459 	Fax3PutBits(tif, EOL, 12);
1460 	Fax3PutBits(tif, EOL, 12);
1461 	if (sp->bit != 8)
1462 		Fax3FlushBits(tif, sp);
1463 	return (1);
1464 }
1465 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
1466 
1467 int
TIFFInitCCITTFax4(TIFF * tif,int scheme)1468 TIFFInitCCITTFax4(TIFF* tif, int scheme)
1469 {
1470 	(void) scheme;
1471 
1472 	if (InitCCITTFax3(tif)) {		/* reuse G3 support */
1473 		TIFFMergeFieldInfo(tif, fax4FieldInfo, N(fax4FieldInfo));
1474 
1475 		tif->tif_decoderow = Fax4Decode;
1476 		tif->tif_decodestrip = Fax4Decode;
1477 		tif->tif_decodetile = Fax4Decode;
1478 #ifdef PDFLIB_TIFFWRITE_SUPPORT
1479 		tif->tif_encoderow = Fax4Encode;
1480 		tif->tif_encodestrip = Fax4Encode;
1481 		tif->tif_encodetile = Fax4Encode;
1482 		tif->tif_postencode = Fax4PostEncode;
1483 #endif /* PDFLIB_TIFFWRITE_SUPPORT */
1484 		/*
1485 		 * Suppress RTC at the end of each strip.
1486 		 */
1487 		return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_NORTC);
1488 	} else
1489 		return (0);
1490 }
1491 
1492 /*
1493  * CCITT Group 3 1-D Modified Huffman RLE Compression Support.
1494  * (Compression algorithms 2 and 32771)
1495  */
1496 
1497 /*
1498  * Decode the requested amount of RLE-encoded data.
1499  */
1500 static int
Fax3DecodeRLE(TIFF * tif,tidata_t buf,tsize_t occ,tsample_t s)1501 Fax3DecodeRLE(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
1502 {
1503 	DECLARE_STATE(tif, sp, "Fax3DecodeRLE");
1504 	int mode = sp->b.mode;
1505 
1506 	(void) s;
1507 	CACHE_STATE(tif, sp);
1508 	thisrun = sp->curruns;
1509 	while ((long)occ > 0) {
1510 		a0 = 0;
1511 		RunLength = 0;
1512 		pa = thisrun;
1513 #ifdef FAX3_DEBUG
1514 		printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail);
1515 		printf("-------------------- %d\n", tif->tif_row);
1516 		fflush(stdout);
1517 #endif
1518 		EXPAND1D(EOFRLE);
1519 		(*sp->fill)(buf, thisrun, pa, lastx);
1520 		/*
1521 		 * Cleanup at the end of the row.
1522 		 */
1523 		if (mode & FAXMODE_BYTEALIGN) {
1524 			int n = BitsAvail - (BitsAvail &~ 7);
1525 			ClrBits(n);
1526 		} else if (mode & FAXMODE_WORDALIGN) {
1527 			int n = BitsAvail - (BitsAvail &~ 15);
1528 			ClrBits(n);
1529 			if (BitsAvail == 0 && !isAligned(cp, uint16))
1530 			    cp++;
1531 		}
1532 		buf += sp->b.rowbytes;
1533 		occ -= sp->b.rowbytes;
1534 		continue;
1535 	EOFRLE:				/* premature EOF */
1536 		(*sp->fill)(buf, thisrun, pa, lastx);
1537 		UNCACHE_STATE(tif, sp);
1538 		return (-1);
1539 	}
1540 	UNCACHE_STATE(tif, sp);
1541 	return (1);
1542 }
1543 
1544 int
TIFFInitCCITTRLE(TIFF * tif,int scheme)1545 TIFFInitCCITTRLE(TIFF* tif, int scheme)
1546 {
1547 	(void) scheme;
1548 
1549 	if (InitCCITTFax3(tif)) {		/* reuse G3 support */
1550 		tif->tif_decoderow = Fax3DecodeRLE;
1551 		tif->tif_decodestrip = Fax3DecodeRLE;
1552 		tif->tif_decodetile = Fax3DecodeRLE;
1553 		/*
1554 		 * Suppress RTC+EOLs when encoding and byte-align data.
1555 		 */
1556 		return TIFFSetField(tif, TIFFTAG_FAXMODE,
1557 		    FAXMODE_NORTC|FAXMODE_NOEOL|FAXMODE_BYTEALIGN);
1558 	} else
1559 		return (0);
1560 }
1561 
1562 int
TIFFInitCCITTRLEW(TIFF * tif,int scheme)1563 TIFFInitCCITTRLEW(TIFF* tif, int scheme)
1564 {
1565 	(void) scheme;
1566 
1567 	if (InitCCITTFax3(tif)) {		/* reuse G3 support */
1568 		tif->tif_decoderow = Fax3DecodeRLE;
1569 		tif->tif_decodestrip = Fax3DecodeRLE;
1570 		tif->tif_decodetile = Fax3DecodeRLE;
1571 		/*
1572 		 * Suppress RTC+EOLs when encoding and word-align data.
1573 		 */
1574 		return TIFFSetField(tif, TIFFTAG_FAXMODE,
1575 		    FAXMODE_NORTC|FAXMODE_NOEOL|FAXMODE_WORDALIGN);
1576 	} else
1577 		return (0);
1578 }
1579 #endif /* CCITT_SUPPORT */
1580 
1581 /* vim: set ts=8 sts=8 sw=8 noet: */
1582