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