Blame libtiff/tif_fax3.c

Packit 7838c8
/* $Id: tif_fax3.c,v 1.81 2017-06-18 10:31:50 erouault Exp $ */
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Copyright (c) 1990-1997 Sam Leffler
Packit 7838c8
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
Packit 7838c8
 *
Packit 7838c8
 * Permission to use, copy, modify, distribute, and sell this software and 
Packit 7838c8
 * its documentation for any purpose is hereby granted without fee, provided
Packit 7838c8
 * that (i) the above copyright notices and this permission notice appear in
Packit 7838c8
 * all copies of the software and related documentation, and (ii) the names of
Packit 7838c8
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
Packit 7838c8
 * publicity relating to the software without the specific, prior written
Packit 7838c8
 * permission of Sam Leffler and Silicon Graphics.
Packit 7838c8
 * 
Packit 7838c8
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
Packit 7838c8
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
Packit 7838c8
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
Packit 7838c8
 * 
Packit 7838c8
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
Packit 7838c8
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
Packit 7838c8
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
Packit 7838c8
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
Packit 7838c8
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
Packit 7838c8
 * OF THIS SOFTWARE.
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
#include "tiffiop.h"
Packit 7838c8
#ifdef CCITT_SUPPORT
Packit 7838c8
/*
Packit 7838c8
 * TIFF Library.
Packit 7838c8
 *
Packit 7838c8
 * CCITT Group 3 (T.4) and Group 4 (T.6) Compression Support.
Packit 7838c8
 *
Packit 7838c8
 * This file contains support for decoding and encoding TIFF
Packit 7838c8
 * compression algorithms 2, 3, 4, and 32771.
Packit 7838c8
 *
Packit 7838c8
 * Decoder support is derived, with permission, from the code
Packit 7838c8
 * in Frank Cringle's viewfax program;
Packit 7838c8
 *      Copyright (C) 1990, 1995  Frank D. Cringle.
Packit 7838c8
 */
Packit 7838c8
#include "tif_fax3.h"
Packit 7838c8
#define	G3CODES
Packit 7838c8
#include "t4.h"
Packit 7838c8
#include <stdio.h>
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Compression+decompression state blocks are
Packit 7838c8
 * derived from this ``base state'' block.
Packit 7838c8
 */
Packit 7838c8
typedef struct {
Packit 7838c8
	int      rw_mode;                /* O_RDONLY for decode, else encode */
Packit 7838c8
	int      mode;                   /* operating mode */
Packit 7838c8
	tmsize_t rowbytes;               /* bytes in a decoded scanline */
Packit 7838c8
	uint32   rowpixels;              /* pixels in a scanline */
Packit 7838c8
Packit 7838c8
	uint16   cleanfaxdata;           /* CleanFaxData tag */
Packit 7838c8
	uint32   badfaxrun;              /* BadFaxRun tag */
Packit 7838c8
	uint32   badfaxlines;            /* BadFaxLines tag */
Packit 7838c8
	uint32   groupoptions;           /* Group 3/4 options tag */
Packit 7838c8
Packit 7838c8
	TIFFVGetMethod  vgetparent;      /* super-class method */
Packit 7838c8
	TIFFVSetMethod  vsetparent;      /* super-class method */
Packit 7838c8
	TIFFPrintMethod printdir;        /* super-class method */
Packit 7838c8
} Fax3BaseState;
Packit 7838c8
#define	Fax3State(tif)		((Fax3BaseState*) (tif)->tif_data)
Packit 7838c8
Packit 7838c8
typedef enum { G3_1D, G3_2D } Ttag;
Packit 7838c8
typedef struct {
Packit 7838c8
	Fax3BaseState b;
Packit 7838c8
Packit 7838c8
	/* Decoder state info */
Packit 7838c8
	const unsigned char* bitmap;	/* bit reversal table */
Packit 7838c8
	uint32	data;			/* current i/o byte/word */
Packit 7838c8
	int	bit;			/* current i/o bit in byte */
Packit 7838c8
	int	EOLcnt;			/* count of EOL codes recognized */
Packit 7838c8
	TIFFFaxFillFunc fill;		/* fill routine */
Packit 7838c8
	uint32*	runs;			/* b&w runs for current/previous row */
Packit 7838c8
	uint32*	refruns;		/* runs for reference line */
Packit 7838c8
	uint32*	curruns;		/* runs for current line */
Packit 7838c8
Packit 7838c8
	/* Encoder state info */
Packit 7838c8
	Ttag    tag;			/* encoding state */
Packit 7838c8
	unsigned char*	refline;	/* reference line for 2d decoding */
Packit 7838c8
	int	k;			/* #rows left that can be 2d encoded */
Packit 7838c8
	int	maxk;			/* max #rows that can be 2d encoded */
Packit 7838c8
Packit 7838c8
	int line;
Packit 7838c8
} Fax3CodecState;
Packit 7838c8
#define DecoderState(tif) ((Fax3CodecState*) Fax3State(tif))
Packit 7838c8
#define EncoderState(tif) ((Fax3CodecState*) Fax3State(tif))
Packit 7838c8
Packit 7838c8
#define is2DEncoding(sp) (sp->b.groupoptions & GROUP3OPT_2DENCODING)
Packit 7838c8
#define isAligned(p,t) ((((size_t)(p)) & (sizeof (t)-1)) == 0)
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Group 3 and Group 4 Decoding.
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * These macros glue the TIFF library state to
Packit 7838c8
 * the state expected by Frank's decoder.
Packit 7838c8
 */
Packit 7838c8
#define	DECLARE_STATE(tif, sp, mod)					\
Packit 7838c8
    static const char module[] = mod;					\
Packit 7838c8
    Fax3CodecState* sp = DecoderState(tif);				\
Packit 7838c8
    int a0;				/* reference element */		\
Packit 7838c8
    int lastx = sp->b.rowpixels;	/* last element in row */	\
Packit 7838c8
    uint32 BitAcc;			/* bit accumulator */		\
Packit 7838c8
    int BitsAvail;			/* # valid bits in BitAcc */	\
Packit 7838c8
    int RunLength;			/* length of current run */	\
Packit 7838c8
    unsigned char* cp;			/* next byte of input data */	\
Packit 7838c8
    unsigned char* ep;			/* end of input data */		\
Packit 7838c8
    uint32* pa;				/* place to stuff next run */	\
Packit 7838c8
    uint32* thisrun;			/* current row's run array */	\
Packit 7838c8
    int EOLcnt;				/* # EOL codes recognized */	\
Packit 7838c8
    const unsigned char* bitmap = sp->bitmap;	/* input data bit reverser */	\
Packit 7838c8
    const TIFFFaxTabEnt* TabEnt
Packit 7838c8
#define	DECLARE_STATE_2D(tif, sp, mod)					\
Packit 7838c8
    DECLARE_STATE(tif, sp, mod);					\
Packit 7838c8
    int b1;				/* next change on prev line */	\
Packit 7838c8
    uint32* pb				/* next run in reference line */\
Packit 7838c8
/*
Packit 7838c8
 * Load any state that may be changed during decoding.
Packit 7838c8
 */
Packit 7838c8
#define	CACHE_STATE(tif, sp) do {					\
Packit 7838c8
    BitAcc = sp->data;							\
Packit 7838c8
    BitsAvail = sp->bit;						\
Packit 7838c8
    EOLcnt = sp->EOLcnt;						\
Packit 7838c8
    cp = (unsigned char*) tif->tif_rawcp;				\
Packit 7838c8
    ep = cp + tif->tif_rawcc;						\
Packit 7838c8
} while (0)
Packit 7838c8
/*
Packit 7838c8
 * Save state possibly changed during decoding.
Packit 7838c8
 */
Packit 7838c8
#define	UNCACHE_STATE(tif, sp) do {					\
Packit 7838c8
    sp->bit = BitsAvail;						\
Packit 7838c8
    sp->data = BitAcc;							\
Packit 7838c8
    sp->EOLcnt = EOLcnt;						\
Packit 7838c8
    tif->tif_rawcc -= (tmsize_t)((uint8*) cp - tif->tif_rawcp);		\
Packit 7838c8
    tif->tif_rawcp = (uint8*) cp;					\
Packit 7838c8
} while (0)
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Setup state for decoding a strip.
Packit 7838c8
 */
Packit 7838c8
static int
Packit 7838c8
Fax3PreDecode(TIFF* tif, uint16 s)
Packit 7838c8
{
Packit 7838c8
	Fax3CodecState* sp = DecoderState(tif);
Packit 7838c8
Packit 7838c8
	(void) s;
Packit 7838c8
	assert(sp != NULL);
Packit 7838c8
	sp->bit = 0;			/* force initial read */
Packit 7838c8
	sp->data = 0;
Packit 7838c8
	sp->EOLcnt = 0;			/* force initial scan for EOL */
Packit 7838c8
	/*
Packit 7838c8
	 * Decoder assumes lsb-to-msb bit order.  Note that we select
Packit 7838c8
	 * this here rather than in Fax3SetupState so that viewers can
Packit 7838c8
	 * hold the image open, fiddle with the FillOrder tag value,
Packit 7838c8
	 * and then re-decode the image.  Otherwise they'd need to close
Packit 7838c8
	 * and open the image to get the state reset.
Packit 7838c8
	 */
Packit 7838c8
	sp->bitmap =
Packit 7838c8
	    TIFFGetBitRevTable(tif->tif_dir.td_fillorder != FILLORDER_LSB2MSB);
Packit 7838c8
	if (sp->refruns) {		/* init reference line to white */
Packit 7838c8
		sp->refruns[0] = (uint32) sp->b.rowpixels;
Packit 7838c8
		sp->refruns[1] = 0;
Packit 7838c8
	}
Packit 7838c8
	sp->line = 0;
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Routine for handling various errors/conditions.
Packit 7838c8
 * Note how they are "glued into the decoder" by
Packit 7838c8
 * overriding the definitions used by the decoder.
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
static void
Packit 7838c8
Fax3Unexpected(const char* module, TIFF* tif, uint32 line, uint32 a0)
Packit 7838c8
{
Packit 7838c8
	TIFFErrorExt(tif->tif_clientdata, module, "Bad code word at line %u of %s %u (x %u)",
Packit 7838c8
	    line, isTiled(tif) ? "tile" : "strip",
Packit 7838c8
	    (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
Packit 7838c8
	    a0);
Packit 7838c8
}
Packit 7838c8
#define	unexpected(table, a0)	Fax3Unexpected(module, tif, sp->line, a0)
Packit 7838c8
Packit 7838c8
static void
Packit 7838c8
Fax3Extension(const char* module, TIFF* tif, uint32 line, uint32 a0)
Packit 7838c8
{
Packit 7838c8
	TIFFErrorExt(tif->tif_clientdata, module,
Packit 7838c8
	    "Uncompressed data (not supported) at line %u of %s %u (x %u)",
Packit 7838c8
	    line, isTiled(tif) ? "tile" : "strip",
Packit 7838c8
	    (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
Packit 7838c8
	    a0);
Packit 7838c8
}
Packit 7838c8
#define	extension(a0)	Fax3Extension(module, tif, sp->line, a0)
Packit 7838c8
Packit 7838c8
static void
Packit 7838c8
Fax3BadLength(const char* module, TIFF* tif, uint32 line, uint32 a0, uint32 lastx)
Packit 7838c8
{
Packit 7838c8
	TIFFWarningExt(tif->tif_clientdata, module, "%s at line %u of %s %u (got %u, expected %u)",
Packit 7838c8
	    a0 < lastx ? "Premature EOL" : "Line length mismatch",
Packit 7838c8
	    line, isTiled(tif) ? "tile" : "strip",
Packit 7838c8
	    (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
Packit 7838c8
	    a0, lastx);
Packit 7838c8
}
Packit 7838c8
#define	badlength(a0,lastx)	Fax3BadLength(module, tif, sp->line, a0, lastx)
Packit 7838c8
Packit 7838c8
static void
Packit 7838c8
Fax3PrematureEOF(const char* module, TIFF* tif, uint32 line, uint32 a0)
Packit 7838c8
{
Packit 7838c8
	TIFFWarningExt(tif->tif_clientdata, module, "Premature EOF at line %u of %s %u (x %u)",
Packit 7838c8
	    line, isTiled(tif) ? "tile" : "strip",
Packit 7838c8
	    (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip),
Packit 7838c8
	    a0);
Packit 7838c8
}
Packit 7838c8
#define	prematureEOF(a0)	Fax3PrematureEOF(module, tif, sp->line, a0)
Packit 7838c8
Packit 7838c8
#define	Nop
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Decode the requested amount of G3 1D-encoded data.
Packit 7838c8
 */
Packit 7838c8
static int
Packit 7838c8
Fax3Decode1D(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
Packit 7838c8
{
Packit 7838c8
	DECLARE_STATE(tif, sp, "Fax3Decode1D");
Packit 7838c8
	(void) s;
Packit 7838c8
	if (occ % sp->b.rowbytes)
Packit 7838c8
	{
Packit 7838c8
		TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
Packit 7838c8
		return (-1);
Packit 7838c8
	}
Packit 7838c8
	CACHE_STATE(tif, sp);
Packit 7838c8
	thisrun = sp->curruns;
Packit 7838c8
	while (occ > 0) {
Packit 7838c8
		a0 = 0;
Packit 7838c8
		RunLength = 0;
Packit 7838c8
		pa = thisrun;
Packit 7838c8
#ifdef FAX3_DEBUG
Packit 7838c8
		printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail);
Packit 7838c8
		printf("-------------------- %d\n", tif->tif_row);
Packit 7838c8
		fflush(stdout);
Packit 7838c8
#endif
Packit 7838c8
		SYNC_EOL(EOF1D);
Packit 7838c8
		EXPAND1D(EOF1Da);
Packit 7838c8
		(*sp->fill)(buf, thisrun, pa, lastx);
Packit 7838c8
		buf += sp->b.rowbytes;
Packit 7838c8
		occ -= sp->b.rowbytes;
Packit 7838c8
		sp->line++;
Packit 7838c8
		continue;
Packit 7838c8
	EOF1D:				/* premature EOF */
Packit 7838c8
		CLEANUP_RUNS();
Packit 7838c8
	EOF1Da:				/* premature EOF */
Packit 7838c8
		(*sp->fill)(buf, thisrun, pa, lastx);
Packit 7838c8
		UNCACHE_STATE(tif, sp);
Packit 7838c8
		return (-1);
Packit 7838c8
	}
Packit 7838c8
	UNCACHE_STATE(tif, sp);
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
#define	SWAP(t,a,b)	{ t x; x = (a); (a) = (b); (b) = x; }
Packit 7838c8
/*
Packit 7838c8
 * Decode the requested amount of G3 2D-encoded data.
Packit 7838c8
 */
Packit 7838c8
static int
Packit 7838c8
Fax3Decode2D(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
Packit 7838c8
{
Packit 7838c8
	DECLARE_STATE_2D(tif, sp, "Fax3Decode2D");
Packit 7838c8
	int is1D;			/* current line is 1d/2d-encoded */
Packit 7838c8
	(void) s;
Packit 7838c8
	if (occ % sp->b.rowbytes)
Packit 7838c8
	{
Packit 7838c8
		TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
Packit 7838c8
		return (-1);
Packit 7838c8
	}
Packit 7838c8
	CACHE_STATE(tif, sp);
Packit 7838c8
	while (occ > 0) {
Packit 7838c8
		a0 = 0;
Packit 7838c8
		RunLength = 0;
Packit 7838c8
		pa = thisrun = sp->curruns;
Packit 7838c8
#ifdef FAX3_DEBUG
Packit 7838c8
		printf("\nBitAcc=%08X, BitsAvail = %d EOLcnt = %d",
Packit 7838c8
		    BitAcc, BitsAvail, EOLcnt);
Packit 7838c8
#endif
Packit 7838c8
		SYNC_EOL(EOF2D);
Packit 7838c8
		NeedBits8(1, EOF2D);
Packit 7838c8
		is1D = GetBits(1);	/* 1D/2D-encoding tag bit */
Packit 7838c8
		ClrBits(1);
Packit 7838c8
#ifdef FAX3_DEBUG
Packit 7838c8
		printf(" %s\n-------------------- %d\n",
Packit 7838c8
		    is1D ? "1D" : "2D", tif->tif_row);
Packit 7838c8
		fflush(stdout);
Packit 7838c8
#endif
Packit 7838c8
		pb = sp->refruns;
Packit 7838c8
		b1 = *pb++;
Packit 7838c8
		if (is1D)
Packit 7838c8
			EXPAND1D(EOF2Da);
Packit 7838c8
		else
Packit 7838c8
			EXPAND2D(EOF2Da);
Packit 7838c8
		(*sp->fill)(buf, thisrun, pa, lastx);
Packit 7838c8
		SETVALUE(0);		/* imaginary change for reference */
Packit 7838c8
		SWAP(uint32*, sp->curruns, sp->refruns);
Packit 7838c8
		buf += sp->b.rowbytes;
Packit 7838c8
		occ -= sp->b.rowbytes;
Packit 7838c8
		sp->line++;
Packit 7838c8
		continue;
Packit 7838c8
	EOF2D:				/* premature EOF */
Packit 7838c8
		CLEANUP_RUNS();
Packit 7838c8
	EOF2Da:				/* premature EOF */
Packit 7838c8
		(*sp->fill)(buf, thisrun, pa, lastx);
Packit 7838c8
		UNCACHE_STATE(tif, sp);
Packit 7838c8
		return (-1);
Packit 7838c8
	}
Packit 7838c8
	UNCACHE_STATE(tif, sp);
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
#undef SWAP
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * The ZERO & FILL macros must handle spans < 2*sizeof(long) bytes.
Packit 7838c8
 * For machines with 64-bit longs this is <16 bytes; otherwise
Packit 7838c8
 * this is <8 bytes.  We optimize the code here to reflect the
Packit 7838c8
 * machine characteristics.
Packit 7838c8
 */
Packit 7838c8
#if SIZEOF_UNSIGNED_LONG == 8
Packit 7838c8
# define FILL(n, cp)							    \
Packit 7838c8
    switch (n) {							    \
Packit 7838c8
    case 15:(cp)[14] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case 14:(cp)[13] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case 13:(cp)[12] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case 12:(cp)[11] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case 11:(cp)[10] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case 10: (cp)[9] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case  9: (cp)[8] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case  8: (cp)[7] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case  7: (cp)[6] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case  6: (cp)[5] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case  5: (cp)[4] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case  4: (cp)[3] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case  3: (cp)[2] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case  2: (cp)[1] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case  1: (cp)[0] = 0xff; (cp) += (n); /*-fallthrough*/ \
Packit 7838c8
    case 0:  ;			      \
Packit 7838c8
    }
Packit 7838c8
# define ZERO(n, cp)							\
Packit 7838c8
    switch (n) {							\
Packit 7838c8
    case 15:(cp)[14] = 0; /*-fallthrough*/ \
Packit 7838c8
    case 14:(cp)[13] = 0; /*-fallthrough*/ \
Packit 7838c8
    case 13:(cp)[12] = 0; /*-fallthrough*/ \
Packit 7838c8
    case 12:(cp)[11] = 0; /*-fallthrough*/ \
Packit 7838c8
    case 11:(cp)[10] = 0; /*-fallthrough*/ \
Packit 7838c8
    case 10: (cp)[9] = 0; /*-fallthrough*/ \
Packit 7838c8
    case  9: (cp)[8] = 0; /*-fallthrough*/ \
Packit 7838c8
    case  8: (cp)[7] = 0; /*-fallthrough*/ \
Packit 7838c8
    case  7: (cp)[6] = 0; /*-fallthrough*/ \
Packit 7838c8
    case  6: (cp)[5] = 0; /*-fallthrough*/ \
Packit 7838c8
    case  5: (cp)[4] = 0; /*-fallthrough*/ \
Packit 7838c8
    case  4: (cp)[3] = 0; /*-fallthrough*/ \
Packit 7838c8
    case  3: (cp)[2] = 0; /*-fallthrough*/ \
Packit 7838c8
    case  2: (cp)[1] = 0; /*-fallthrough*/ \
Packit 7838c8
    case  1: (cp)[0] = 0; (cp) += (n); /*-fallthrough*/ \
Packit 7838c8
    case 0:  ;			\
Packit 7838c8
    }
Packit 7838c8
#else
Packit 7838c8
# define FILL(n, cp)							    \
Packit 7838c8
    switch (n) {							    \
Packit 7838c8
    case 7: (cp)[6] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case 6: (cp)[5] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case 5: (cp)[4] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case 4: (cp)[3] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case 3: (cp)[2] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case 2: (cp)[1] = 0xff; /*-fallthrough*/ \
Packit 7838c8
    case 1: (cp)[0] = 0xff; (cp) += (n);  /*-fallthrough*/ \
Packit 7838c8
    case 0:  ;			    \
Packit 7838c8
    }
Packit 7838c8
# define ZERO(n, cp)							\
Packit 7838c8
    switch (n) {							\
Packit 7838c8
    case 7: (cp)[6] = 0; /*-fallthrough*/ \
Packit 7838c8
    case 6: (cp)[5] = 0; /*-fallthrough*/ \
Packit 7838c8
    case 5: (cp)[4] = 0; /*-fallthrough*/ \
Packit 7838c8
    case 4: (cp)[3] = 0; /*-fallthrough*/ \
Packit 7838c8
    case 3: (cp)[2] = 0; /*-fallthrough*/ \
Packit 7838c8
    case 2: (cp)[1] = 0; /*-fallthrough*/ \
Packit 7838c8
    case 1: (cp)[0] = 0; (cp) += (n); /*-fallthrough*/ \
Packit 7838c8
    case 0:  ;			\
Packit 7838c8
    }
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Bit-fill a row according to the white/black
Packit 7838c8
 * runs generated during G3/G4 decoding.
Packit 7838c8
 */
Packit 7838c8
void
Packit 7838c8
_TIFFFax3fillruns(unsigned char* buf, uint32* runs, uint32* erun, uint32 lastx)
Packit 7838c8
{
Packit 7838c8
	static const unsigned char _fillmasks[] =
Packit 7838c8
	    { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
Packit 7838c8
	unsigned char* cp;
Packit 7838c8
	uint32 x, bx, run;
Packit 7838c8
	int32 n, nw;
Packit 7838c8
	long* lp;
Packit 7838c8
Packit 7838c8
	if ((erun-runs)&1)
Packit 7838c8
	    *erun++ = 0;
Packit 7838c8
	x = 0;
Packit 7838c8
	for (; runs < erun; runs += 2) {
Packit 7838c8
	    run = runs[0];
Packit 7838c8
	    if (x+run > lastx || run > lastx )
Packit 7838c8
		run = runs[0] = (uint32) (lastx - x);
Packit 7838c8
	    if (run) {
Packit 7838c8
		cp = buf + (x>>3);
Packit 7838c8
		bx = x&7;
Packit 7838c8
		if (run > 8-bx) {
Packit 7838c8
		    if (bx) {			/* align to byte boundary */
Packit 7838c8
			*cp++ &= 0xff << (8-bx);
Packit 7838c8
			run -= 8-bx;
Packit 7838c8
		    }
Packit 7838c8
		    if( (n = run >> 3) != 0 ) {	/* multiple bytes to fill */
Packit 7838c8
			if ((n/sizeof (long)) > 1) {
Packit 7838c8
			    /*
Packit 7838c8
			     * Align to longword boundary and fill.
Packit 7838c8
			     */
Packit 7838c8
			    for (; n && !isAligned(cp, long); n--)
Packit 7838c8
				    *cp++ = 0x00;
Packit 7838c8
			    lp = (long*) cp;
Packit 7838c8
			    nw = (int32)(n / sizeof (long));
Packit 7838c8
			    n -= nw * sizeof (long);
Packit 7838c8
			    do {
Packit 7838c8
				    *lp++ = 0L;
Packit 7838c8
			    } while (--nw);
Packit 7838c8
			    cp = (unsigned char*) lp;
Packit 7838c8
			}
Packit 7838c8
			ZERO(n, cp);
Packit 7838c8
			run &= 7;
Packit 7838c8
		    }
Packit 7838c8
		    if (run)
Packit 7838c8
			cp[0] &= 0xff >> run;
Packit 7838c8
		} else
Packit 7838c8
		    cp[0] &= ~(_fillmasks[run]>>bx);
Packit 7838c8
		x += runs[0];
Packit 7838c8
	    }
Packit 7838c8
	    run = runs[1];
Packit 7838c8
	    if (x+run > lastx || run > lastx )
Packit 7838c8
		run = runs[1] = lastx - x;
Packit 7838c8
	    if (run) {
Packit 7838c8
		cp = buf + (x>>3);
Packit 7838c8
		bx = x&7;
Packit 7838c8
		if (run > 8-bx) {
Packit 7838c8
		    if (bx) {			/* align to byte boundary */
Packit 7838c8
			*cp++ |= 0xff >> bx;
Packit 7838c8
			run -= 8-bx;
Packit 7838c8
		    }
Packit 7838c8
		    if( (n = run>>3) != 0 ) {	/* multiple bytes to fill */
Packit 7838c8
			if ((n/sizeof (long)) > 1) {
Packit 7838c8
			    /*
Packit 7838c8
			     * Align to longword boundary and fill.
Packit 7838c8
			     */
Packit 7838c8
			    for (; n && !isAligned(cp, long); n--)
Packit 7838c8
				*cp++ = 0xff;
Packit 7838c8
			    lp = (long*) cp;
Packit 7838c8
			    nw = (int32)(n / sizeof (long));
Packit 7838c8
			    n -= nw * sizeof (long);
Packit 7838c8
			    do {
Packit 7838c8
				*lp++ = -1L;
Packit 7838c8
			    } while (--nw);
Packit 7838c8
			    cp = (unsigned char*) lp;
Packit 7838c8
			}
Packit 7838c8
			FILL(n, cp);
Packit 7838c8
			run &= 7;
Packit 7838c8
		    }
Packit 7838c8
                    /* Explicit 0xff masking to make icc -check=conversions happy */
Packit 7838c8
		    if (run)
Packit 7838c8
			cp[0] = (unsigned char)((cp[0] | (0xff00 >> run))&0xff);
Packit 7838c8
		} else
Packit 7838c8
		    cp[0] |= _fillmasks[run]>>bx;
Packit 7838c8
		x += runs[1];
Packit 7838c8
	    }
Packit 7838c8
	}
Packit 7838c8
	assert(x == lastx);
Packit 7838c8
}
Packit 7838c8
#undef	ZERO
Packit 7838c8
#undef	FILL
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
Fax3FixupTags(TIFF* tif)
Packit 7838c8
{
Packit 7838c8
	(void) tif;
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Setup G3/G4-related compression/decompression state
Packit 7838c8
 * before data is processed.  This routine is called once
Packit 7838c8
 * per image -- it sets up different state based on whether
Packit 7838c8
 * or not decoding or encoding is being done and whether
Packit 7838c8
 * 1D- or 2D-encoded data is involved.
Packit 7838c8
 */
Packit 7838c8
static int
Packit 7838c8
Fax3SetupState(TIFF* tif)
Packit 7838c8
{
Packit 7838c8
	static const char module[] = "Fax3SetupState";
Packit 7838c8
	TIFFDirectory* td = &tif->tif_dir;
Packit 7838c8
	Fax3BaseState* sp = Fax3State(tif);
Packit 7838c8
	int needsRefLine;
Packit 7838c8
	Fax3CodecState* dsp = (Fax3CodecState*) Fax3State(tif);
Packit 7838c8
	tmsize_t rowbytes;
Packit 7838c8
	uint32 rowpixels, nruns;
Packit 7838c8
Packit 7838c8
	if (td->td_bitspersample != 1) {
Packit 7838c8
		TIFFErrorExt(tif->tif_clientdata, module,
Packit 7838c8
		    "Bits/sample must be 1 for Group 3/4 encoding/decoding");
Packit 7838c8
		return (0);
Packit 7838c8
	}
Packit 7838c8
	/*
Packit 7838c8
	 * Calculate the scanline/tile widths.
Packit 7838c8
	 */
Packit 7838c8
	if (isTiled(tif)) {
Packit 7838c8
		rowbytes = TIFFTileRowSize(tif);
Packit 7838c8
		rowpixels = td->td_tilewidth;
Packit 7838c8
	} else {
Packit 7838c8
		rowbytes = TIFFScanlineSize(tif);
Packit 7838c8
		rowpixels = td->td_imagewidth;
Packit 7838c8
	}
Packit 7838c8
	sp->rowbytes = rowbytes;
Packit 7838c8
	sp->rowpixels = rowpixels;
Packit 7838c8
	/*
Packit 7838c8
	 * Allocate any additional space required for decoding/encoding.
Packit 7838c8
	 */
Packit 7838c8
	needsRefLine = (
Packit 7838c8
	    (sp->groupoptions & GROUP3OPT_2DENCODING) ||
Packit 7838c8
	    td->td_compression == COMPRESSION_CCITTFAX4
Packit 7838c8
	);
Packit 7838c8
Packit 7838c8
	/*
Packit 7838c8
	  Assure that allocation computations do not overflow.
Packit 7838c8
	  
Packit 7838c8
	  TIFFroundup and TIFFSafeMultiply return zero on integer overflow
Packit 7838c8
	*/
Packit 7838c8
	dsp->runs=(uint32*) NULL;
Packit 7838c8
	nruns = TIFFroundup_32(rowpixels,32);
Packit 7838c8
	if (needsRefLine) {
Packit 7838c8
		nruns = TIFFSafeMultiply(uint32,nruns,2);
Packit 7838c8
	}
Packit 7838c8
	if ((nruns == 0) || (TIFFSafeMultiply(uint32,nruns,2) == 0)) {
Packit 7838c8
		TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
Packit 7838c8
			     "Row pixels integer overflow (rowpixels %u)",
Packit 7838c8
			     rowpixels);
Packit 7838c8
		return (0);
Packit 7838c8
	}
Packit 7838c8
	dsp->runs = (uint32*) _TIFFCheckMalloc(tif,
Packit 7838c8
					       TIFFSafeMultiply(uint32,nruns,2),
Packit 7838c8
					       sizeof (uint32),
Packit 7838c8
					       "for Group 3/4 run arrays");
Packit 7838c8
	if (dsp->runs == NULL)
Packit 7838c8
		return (0);
Packit 7838c8
	memset( dsp->runs, 0, TIFFSafeMultiply(uint32,nruns,2)*sizeof(uint32));
Packit 7838c8
	dsp->curruns = dsp->runs;
Packit 7838c8
	if (needsRefLine)
Packit 7838c8
		dsp->refruns = dsp->runs + nruns;
Packit 7838c8
	else
Packit 7838c8
		dsp->refruns = NULL;
Packit 7838c8
	if (td->td_compression == COMPRESSION_CCITTFAX3
Packit 7838c8
	    && is2DEncoding(dsp)) {	/* NB: default is 1D routine */
Packit 7838c8
		tif->tif_decoderow = Fax3Decode2D;
Packit 7838c8
		tif->tif_decodestrip = Fax3Decode2D;
Packit 7838c8
		tif->tif_decodetile = Fax3Decode2D;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	if (needsRefLine) {		/* 2d encoding */
Packit 7838c8
		Fax3CodecState* esp = EncoderState(tif);
Packit 7838c8
		/*
Packit 7838c8
		 * 2d encoding requires a scanline
Packit 7838c8
		 * buffer for the ``reference line''; the
Packit 7838c8
		 * scanline against which delta encoding
Packit 7838c8
		 * is referenced.  The reference line must
Packit 7838c8
		 * be initialized to be ``white'' (done elsewhere).
Packit 7838c8
		 */
Packit 7838c8
		esp->refline = (unsigned char*) _TIFFmalloc(rowbytes);
Packit 7838c8
		if (esp->refline == NULL) {
Packit 7838c8
			TIFFErrorExt(tif->tif_clientdata, module,
Packit 7838c8
			    "No space for Group 3/4 reference line");
Packit 7838c8
			return (0);
Packit 7838c8
		}
Packit 7838c8
	} else					/* 1d encoding */
Packit 7838c8
		EncoderState(tif)->refline = NULL;
Packit 7838c8
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * CCITT Group 3 FAX Encoding.
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
#define	Fax3FlushBits(tif, sp) {				\
Packit 7838c8
	if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize)		\
Packit 7838c8
		(void) TIFFFlushData1(tif);			\
Packit 7838c8
	*(tif)->tif_rawcp++ = (uint8) (sp)->data;		\
Packit 7838c8
	(tif)->tif_rawcc++;					\
Packit 7838c8
	(sp)->data = 0, (sp)->bit = 8;				\
Packit 7838c8
}
Packit 7838c8
#define	_FlushBits(tif) {					\
Packit 7838c8
	if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize)		\
Packit 7838c8
		(void) TIFFFlushData1(tif);			\
Packit 7838c8
	*(tif)->tif_rawcp++ = (uint8) data;		\
Packit 7838c8
	(tif)->tif_rawcc++;					\
Packit 7838c8
	data = 0, bit = 8;					\
Packit 7838c8
}
Packit 7838c8
static const int _msbmask[9] =
Packit 7838c8
    { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
Packit 7838c8
#define	_PutBits(tif, bits, length) {				\
Packit 7838c8
	while (length > bit) {					\
Packit 7838c8
		data |= bits >> (length - bit);			\
Packit 7838c8
		length -= bit;					\
Packit 7838c8
		_FlushBits(tif);				\
Packit 7838c8
	}							\
Packit 7838c8
        assert( length < 9 );                                   \
Packit 7838c8
	data |= (bits & _msbmask[length]) << (bit - length);	\
Packit 7838c8
	bit -= length;						\
Packit 7838c8
	if (bit == 0)						\
Packit 7838c8
		_FlushBits(tif);				\
Packit 7838c8
}
Packit 7838c8
	
Packit 7838c8
/*
Packit 7838c8
 * Write a variable-length bit-value to
Packit 7838c8
 * the output stream.  Values are
Packit 7838c8
 * assumed to be at most 16 bits.
Packit 7838c8
 */
Packit 7838c8
static void
Packit 7838c8
Fax3PutBits(TIFF* tif, unsigned int bits, unsigned int length)
Packit 7838c8
{
Packit 7838c8
	Fax3CodecState* sp = EncoderState(tif);
Packit 7838c8
	unsigned int bit = sp->bit;
Packit 7838c8
	int data = sp->data;
Packit 7838c8
Packit 7838c8
	_PutBits(tif, bits, length);
Packit 7838c8
Packit 7838c8
	sp->data = data;
Packit 7838c8
	sp->bit = bit;
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Write a code to the output stream.
Packit 7838c8
 */
Packit 7838c8
#define putcode(tif, te)	Fax3PutBits(tif, (te)->code, (te)->length)
Packit 7838c8
Packit 7838c8
#ifdef FAX3_DEBUG
Packit 7838c8
#define	DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B")
Packit 7838c8
#define	DEBUG_PRINT(what,len) {						\
Packit 7838c8
    int t;								\
Packit 7838c8
    printf("%08X/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what), len);	\
Packit 7838c8
    for (t = length-1; t >= 0; t--)					\
Packit 7838c8
	putchar(code & (1<
Packit 7838c8
    putchar('\n');							\
Packit 7838c8
}
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Write the sequence of codes that describes
Packit 7838c8
 * the specified span of zero's or one's.  The
Packit 7838c8
 * appropriate table that holds the make-up and
Packit 7838c8
 * terminating codes is supplied.
Packit 7838c8
 */
Packit 7838c8
static void
Packit 7838c8
putspan(TIFF* tif, int32 span, const tableentry* tab)
Packit 7838c8
{
Packit 7838c8
	Fax3CodecState* sp = EncoderState(tif);
Packit 7838c8
	unsigned int bit = sp->bit;
Packit 7838c8
	int data = sp->data;
Packit 7838c8
	unsigned int code, length;
Packit 7838c8
Packit 7838c8
	while (span >= 2624) {
Packit 7838c8
		const tableentry* te = &tab[63 + (2560>>6)];
Packit 7838c8
		code = te->code;
Packit 7838c8
		length = te->length;
Packit 7838c8
#ifdef FAX3_DEBUG
Packit 7838c8
		DEBUG_PRINT("MakeUp", te->runlen);
Packit 7838c8
#endif
Packit 7838c8
		_PutBits(tif, code, length);
Packit 7838c8
		span -= te->runlen;
Packit 7838c8
	}
Packit 7838c8
	if (span >= 64) {
Packit 7838c8
		const tableentry* te = &tab[63 + (span>>6)];
Packit 7838c8
		assert(te->runlen == 64*(span>>6));
Packit 7838c8
		code = te->code;
Packit 7838c8
		length = te->length;
Packit 7838c8
#ifdef FAX3_DEBUG
Packit 7838c8
		DEBUG_PRINT("MakeUp", te->runlen);
Packit 7838c8
#endif
Packit 7838c8
		_PutBits(tif, code, length);
Packit 7838c8
		span -= te->runlen;
Packit 7838c8
	}
Packit 7838c8
	code = tab[span].code;
Packit 7838c8
	length = tab[span].length;
Packit 7838c8
#ifdef FAX3_DEBUG
Packit 7838c8
	DEBUG_PRINT("  Term", tab[span].runlen);
Packit 7838c8
#endif
Packit 7838c8
	_PutBits(tif, code, length);
Packit 7838c8
Packit 7838c8
	sp->data = data;
Packit 7838c8
	sp->bit = bit;
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Write an EOL code to the output stream.  The zero-fill
Packit 7838c8
 * logic for byte-aligning encoded scanlines is handled
Packit 7838c8
 * here.  We also handle writing the tag bit for the next
Packit 7838c8
 * scanline when doing 2d encoding.
Packit 7838c8
 */
Packit 7838c8
static void
Packit 7838c8
Fax3PutEOL(TIFF* tif)
Packit 7838c8
{
Packit 7838c8
	Fax3CodecState* sp = EncoderState(tif);
Packit 7838c8
	unsigned int bit = sp->bit;
Packit 7838c8
	int data = sp->data;
Packit 7838c8
	unsigned int code, length, tparm;
Packit 7838c8
Packit 7838c8
	if (sp->b.groupoptions & GROUP3OPT_FILLBITS) {
Packit 7838c8
		/*
Packit 7838c8
		 * Force bit alignment so EOL will terminate on
Packit 7838c8
		 * a byte boundary.  That is, force the bit alignment
Packit 7838c8
		 * to 16-12 = 4 before putting out the EOL code.
Packit 7838c8
		 */
Packit 7838c8
		int align = 8 - 4;
Packit 7838c8
		if (align != sp->bit) {
Packit 7838c8
			if (align > sp->bit)
Packit 7838c8
				align = sp->bit + (8 - align);
Packit 7838c8
			else
Packit 7838c8
				align = sp->bit - align;
Packit 7838c8
			tparm=align; 
Packit 7838c8
			_PutBits(tif, 0, tparm);
Packit 7838c8
		}
Packit 7838c8
	}
Packit 7838c8
	code = EOL;
Packit 7838c8
	length = 12;
Packit 7838c8
	if (is2DEncoding(sp)) {
Packit 7838c8
		code = (code<<1) | (sp->tag == G3_1D);
Packit 7838c8
		length++;
Packit 7838c8
	}
Packit 7838c8
	_PutBits(tif, code, length);
Packit 7838c8
Packit 7838c8
	sp->data = data;
Packit 7838c8
	sp->bit = bit;
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Reset encoding state at the start of a strip.
Packit 7838c8
 */
Packit 7838c8
static int
Packit 7838c8
Fax3PreEncode(TIFF* tif, uint16 s)
Packit 7838c8
{
Packit 7838c8
	Fax3CodecState* sp = EncoderState(tif);
Packit 7838c8
Packit 7838c8
	(void) s;
Packit 7838c8
	assert(sp != NULL);
Packit 7838c8
	sp->bit = 8;
Packit 7838c8
	sp->data = 0;
Packit 7838c8
	sp->tag = G3_1D;
Packit 7838c8
	/*
Packit 7838c8
	 * This is necessary for Group 4; otherwise it isn't
Packit 7838c8
	 * needed because the first scanline of each strip ends
Packit 7838c8
	 * up being copied into the refline.
Packit 7838c8
	 */
Packit 7838c8
	if (sp->refline)
Packit 7838c8
		_TIFFmemset(sp->refline, 0x00, sp->b.rowbytes);
Packit 7838c8
	if (is2DEncoding(sp)) {
Packit 7838c8
		float res = tif->tif_dir.td_yresolution;
Packit 7838c8
		/*
Packit 7838c8
		 * The CCITT spec says that when doing 2d encoding, you
Packit 7838c8
		 * should only do it on K consecutive scanlines, where K
Packit 7838c8
		 * depends on the resolution of the image being encoded
Packit 7838c8
		 * (2 for <= 200 lpi, 4 for > 200 lpi).  Since the directory
Packit 7838c8
		 * code initializes td_yresolution to 0, this code will
Packit 7838c8
		 * select a K of 2 unless the YResolution tag is set
Packit 7838c8
		 * appropriately.  (Note also that we fudge a little here
Packit 7838c8
		 * and use 150 lpi to avoid problems with units conversion.)
Packit 7838c8
		 */
Packit 7838c8
		if (tif->tif_dir.td_resolutionunit == RESUNIT_CENTIMETER)
Packit 7838c8
			res *= 2.54f;		/* convert to inches */
Packit 7838c8
		sp->maxk = (res > 150 ? 4 : 2);
Packit 7838c8
		sp->k = sp->maxk-1;
Packit 7838c8
	} else
Packit 7838c8
		sp->k = sp->maxk = 0;
Packit 7838c8
	sp->line = 0;
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static const unsigned char zeroruns[256] = {
Packit 7838c8
    8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,	/* 0x00 - 0x0f */
Packit 7838c8
    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,	/* 0x10 - 0x1f */
Packit 7838c8
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,	/* 0x20 - 0x2f */
Packit 7838c8
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,	/* 0x30 - 0x3f */
Packit 7838c8
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x40 - 0x4f */
Packit 7838c8
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x50 - 0x5f */
Packit 7838c8
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x60 - 0x6f */
Packit 7838c8
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x70 - 0x7f */
Packit 7838c8
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x80 - 0x8f */
Packit 7838c8
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x90 - 0x9f */
Packit 7838c8
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xa0 - 0xaf */
Packit 7838c8
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xb0 - 0xbf */
Packit 7838c8
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xc0 - 0xcf */
Packit 7838c8
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xd0 - 0xdf */
Packit 7838c8
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xe0 - 0xef */
Packit 7838c8
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0xf0 - 0xff */
Packit 7838c8
};
Packit 7838c8
static const unsigned char oneruns[256] = {
Packit 7838c8
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x00 - 0x0f */
Packit 7838c8
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x10 - 0x1f */
Packit 7838c8
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x20 - 0x2f */
Packit 7838c8
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x30 - 0x3f */
Packit 7838c8
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x40 - 0x4f */
Packit 7838c8
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x50 - 0x5f */
Packit 7838c8
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x60 - 0x6f */
Packit 7838c8
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,	/* 0x70 - 0x7f */
Packit 7838c8
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x80 - 0x8f */
Packit 7838c8
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0x90 - 0x9f */
Packit 7838c8
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0xa0 - 0xaf */
Packit 7838c8
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,	/* 0xb0 - 0xbf */
Packit 7838c8
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,	/* 0xc0 - 0xcf */
Packit 7838c8
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,	/* 0xd0 - 0xdf */
Packit 7838c8
    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,	/* 0xe0 - 0xef */
Packit 7838c8
    4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8,	/* 0xf0 - 0xff */
Packit 7838c8
};
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * On certain systems it pays to inline
Packit 7838c8
 * the routines that find pixel spans.
Packit 7838c8
 */
Packit 7838c8
#ifdef VAXC
Packit 7838c8
static	int32 find0span(unsigned char*, int32, int32);
Packit 7838c8
static	int32 find1span(unsigned char*, int32, int32);
Packit 7838c8
#pragma inline(find0span,find1span)
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Find a span of ones or zeros using the supplied
Packit 7838c8
 * table.  The ``base'' of the bit string is supplied
Packit 7838c8
 * along with the start+end bit indices.
Packit 7838c8
 */
Packit 7838c8
inline static int32
Packit 7838c8
find0span(unsigned char* bp, int32 bs, int32 be)
Packit 7838c8
{
Packit 7838c8
	int32 bits = be - bs;
Packit 7838c8
	int32 n, span;
Packit 7838c8
Packit 7838c8
	bp += bs>>3;
Packit 7838c8
	/*
Packit 7838c8
	 * Check partial byte on lhs.
Packit 7838c8
	 */
Packit 7838c8
	if (bits > 0 && (n = (bs & 7)) != 0) {
Packit 7838c8
		span = zeroruns[(*bp << n) & 0xff];
Packit 7838c8
		if (span > 8-n)		/* table value too generous */
Packit 7838c8
			span = 8-n;
Packit 7838c8
		if (span > bits)	/* constrain span to bit range */
Packit 7838c8
			span = bits;
Packit 7838c8
		if (n+span < 8)		/* doesn't extend to edge of byte */
Packit 7838c8
			return (span);
Packit 7838c8
		bits -= span;
Packit 7838c8
		bp++;
Packit 7838c8
	} else
Packit 7838c8
		span = 0;
Packit 7838c8
	if (bits >= (int32)(2 * 8 * sizeof(long))) {
Packit 7838c8
		long* lp;
Packit 7838c8
		/*
Packit 7838c8
		 * Align to longword boundary and check longwords.
Packit 7838c8
		 */
Packit 7838c8
		while (!isAligned(bp, long)) {
Packit 7838c8
			if (*bp != 0x00)
Packit 7838c8
				return (span + zeroruns[*bp]);
Packit 7838c8
			span += 8;
Packit 7838c8
			bits -= 8;
Packit 7838c8
			bp++;
Packit 7838c8
		}
Packit 7838c8
		lp = (long*) bp;
Packit 7838c8
		while ((bits >= (int32)(8 * sizeof(long))) && (0 == *lp)) {
Packit 7838c8
			span += 8*sizeof (long);
Packit 7838c8
			bits -= 8*sizeof (long);
Packit 7838c8
			lp++;
Packit 7838c8
		}
Packit 7838c8
		bp = (unsigned char*) lp;
Packit 7838c8
	}
Packit 7838c8
	/*
Packit 7838c8
	 * Scan full bytes for all 0's.
Packit 7838c8
	 */
Packit 7838c8
	while (bits >= 8) {
Packit 7838c8
		if (*bp != 0x00)	/* end of run */
Packit 7838c8
			return (span + zeroruns[*bp]);
Packit 7838c8
		span += 8;
Packit 7838c8
		bits -= 8;
Packit 7838c8
		bp++;
Packit 7838c8
	}
Packit 7838c8
	/*
Packit 7838c8
	 * Check partial byte on rhs.
Packit 7838c8
	 */
Packit 7838c8
	if (bits > 0) {
Packit 7838c8
		n = zeroruns[*bp];
Packit 7838c8
		span += (n > bits ? bits : n);
Packit 7838c8
	}
Packit 7838c8
	return (span);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
inline static int32
Packit 7838c8
find1span(unsigned char* bp, int32 bs, int32 be)
Packit 7838c8
{
Packit 7838c8
	int32 bits = be - bs;
Packit 7838c8
	int32 n, span;
Packit 7838c8
Packit 7838c8
	bp += bs>>3;
Packit 7838c8
	/*
Packit 7838c8
	 * Check partial byte on lhs.
Packit 7838c8
	 */
Packit 7838c8
	if (bits > 0 && (n = (bs & 7)) != 0) {
Packit 7838c8
		span = oneruns[(*bp << n) & 0xff];
Packit 7838c8
		if (span > 8-n)		/* table value too generous */
Packit 7838c8
			span = 8-n;
Packit 7838c8
		if (span > bits)	/* constrain span to bit range */
Packit 7838c8
			span = bits;
Packit 7838c8
		if (n+span < 8)		/* doesn't extend to edge of byte */
Packit 7838c8
			return (span);
Packit 7838c8
		bits -= span;
Packit 7838c8
		bp++;
Packit 7838c8
	} else
Packit 7838c8
		span = 0;
Packit 7838c8
	if (bits >= (int32)(2 * 8 * sizeof(long))) {
Packit 7838c8
		long* lp;
Packit 7838c8
		/*
Packit 7838c8
		 * Align to longword boundary and check longwords.
Packit 7838c8
		 */
Packit 7838c8
		while (!isAligned(bp, long)) {
Packit 7838c8
			if (*bp != 0xff)
Packit 7838c8
				return (span + oneruns[*bp]);
Packit 7838c8
			span += 8;
Packit 7838c8
			bits -= 8;
Packit 7838c8
			bp++;
Packit 7838c8
		}
Packit 7838c8
		lp = (long*) bp;
Packit 7838c8
		while ((bits >= (int32)(8 * sizeof(long))) && (~0 == *lp)) {
Packit 7838c8
			span += 8*sizeof (long);
Packit 7838c8
			bits -= 8*sizeof (long);
Packit 7838c8
			lp++;
Packit 7838c8
		}
Packit 7838c8
		bp = (unsigned char*) lp;
Packit 7838c8
	}
Packit 7838c8
	/*
Packit 7838c8
	 * Scan full bytes for all 1's.
Packit 7838c8
	 */
Packit 7838c8
	while (bits >= 8) {
Packit 7838c8
		if (*bp != 0xff)	/* end of run */
Packit 7838c8
			return (span + oneruns[*bp]);
Packit 7838c8
		span += 8;
Packit 7838c8
		bits -= 8;
Packit 7838c8
		bp++;
Packit 7838c8
	}
Packit 7838c8
	/*
Packit 7838c8
	 * Check partial byte on rhs.
Packit 7838c8
	 */
Packit 7838c8
	if (bits > 0) {
Packit 7838c8
		n = oneruns[*bp];
Packit 7838c8
		span += (n > bits ? bits : n);
Packit 7838c8
	}
Packit 7838c8
	return (span);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Return the offset of the next bit in the range
Packit 7838c8
 * [bs..be] that is different from the specified
Packit 7838c8
 * color.  The end, be, is returned if no such bit
Packit 7838c8
 * exists.
Packit 7838c8
 */
Packit 7838c8
#define	finddiff(_cp, _bs, _be, _color)	\
Packit 7838c8
	(_bs + (_color ? find1span(_cp,_bs,_be) : find0span(_cp,_bs,_be)))
Packit 7838c8
/*
Packit 7838c8
 * Like finddiff, but also check the starting bit
Packit 7838c8
 * against the end in case start > end.
Packit 7838c8
 */
Packit 7838c8
#define	finddiff2(_cp, _bs, _be, _color) \
Packit 7838c8
	(_bs < _be ? finddiff(_cp,_bs,_be,_color) : _be)
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * 1d-encode a row of pixels.  The encoding is
Packit 7838c8
 * a sequence of all-white or all-black spans
Packit 7838c8
 * of pixels encoded with Huffman codes.
Packit 7838c8
 */
Packit 7838c8
static int
Packit 7838c8
Fax3Encode1DRow(TIFF* tif, unsigned char* bp, uint32 bits)
Packit 7838c8
{
Packit 7838c8
	Fax3CodecState* sp = EncoderState(tif);
Packit 7838c8
	int32 span;
Packit 7838c8
        uint32 bs = 0;
Packit 7838c8
Packit 7838c8
	for (;;) {
Packit 7838c8
		span = find0span(bp, bs, bits);		/* white span */
Packit 7838c8
		putspan(tif, span, TIFFFaxWhiteCodes);
Packit 7838c8
		bs += span;
Packit 7838c8
		if (bs >= bits)
Packit 7838c8
			break;
Packit 7838c8
		span = find1span(bp, bs, bits);		/* black span */
Packit 7838c8
		putspan(tif, span, TIFFFaxBlackCodes);
Packit 7838c8
		bs += span;
Packit 7838c8
		if (bs >= bits)
Packit 7838c8
			break;
Packit 7838c8
	}
Packit 7838c8
	if (sp->b.mode & (FAXMODE_BYTEALIGN|FAXMODE_WORDALIGN)) {
Packit 7838c8
		if (sp->bit != 8)			/* byte-align */
Packit 7838c8
			Fax3FlushBits(tif, sp);
Packit 7838c8
		if ((sp->b.mode&FAXMODE_WORDALIGN) &&
Packit 7838c8
		    !isAligned(tif->tif_rawcp, uint16))
Packit 7838c8
			Fax3FlushBits(tif, sp);
Packit 7838c8
	}
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static const tableentry horizcode =
Packit 7838c8
    { 3, 0x1, 0 };	/* 001 */
Packit 7838c8
static const tableentry passcode =
Packit 7838c8
    { 4, 0x1, 0 };	/* 0001 */
Packit 7838c8
static const tableentry vcodes[7] = {
Packit 7838c8
    { 7, 0x03, 0 },	/* 0000 011 */
Packit 7838c8
    { 6, 0x03, 0 },	/* 0000 11 */
Packit 7838c8
    { 3, 0x03, 0 },	/* 011 */
Packit 7838c8
    { 1, 0x1, 0 },	/* 1 */
Packit 7838c8
    { 3, 0x2, 0 },	/* 010 */
Packit 7838c8
    { 6, 0x02, 0 },	/* 0000 10 */
Packit 7838c8
    { 7, 0x02, 0 }	/* 0000 010 */
Packit 7838c8
};
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * 2d-encode a row of pixels.  Consult the CCITT
Packit 7838c8
 * documentation for the algorithm.
Packit 7838c8
 */
Packit 7838c8
static int
Packit 7838c8
Fax3Encode2DRow(TIFF* tif, unsigned char* bp, unsigned char* rp, uint32 bits)
Packit 7838c8
{
Packit 7838c8
#define	PIXEL(buf,ix)	((((buf)[(ix)>>3]) >> (7-((ix)&7))) & 1)
Packit 7838c8
        uint32 a0 = 0;
Packit 7838c8
	uint32 a1 = (PIXEL(bp, 0) != 0 ? 0 : finddiff(bp, 0, bits, 0));
Packit 7838c8
	uint32 b1 = (PIXEL(rp, 0) != 0 ? 0 : finddiff(rp, 0, bits, 0));
Packit 7838c8
	uint32 a2, b2;
Packit 7838c8
Packit 7838c8
	for (;;) {
Packit 7838c8
		b2 = finddiff2(rp, b1, bits, PIXEL(rp,b1));
Packit 7838c8
		if (b2 >= a1) {
Packit 7838c8
			/* Naive computation triggers -fsanitize=undefined,unsigned-integer-overflow */
Packit 7838c8
			/* although it is correct unless the difference between both is < 31 bit */
Packit 7838c8
			/* int32 d = b1 - a1; */
Packit 7838c8
			int32 d = (b1 >= a1 && b1 - a1 <= 3U) ? (int32)(b1 - a1):
Packit 7838c8
			          (b1 < a1 && a1 - b1 <= 3U) ? -(int32)(a1 - b1) : 0x7FFFFFFF;
Packit 7838c8
			if (!(-3 <= d && d <= 3)) {	/* horizontal mode */
Packit 7838c8
				a2 = finddiff2(bp, a1, bits, PIXEL(bp,a1));
Packit 7838c8
				putcode(tif, &horizcode);
Packit 7838c8
				if (a0+a1 == 0 || PIXEL(bp, a0) == 0) {
Packit 7838c8
					putspan(tif, a1-a0, TIFFFaxWhiteCodes);
Packit 7838c8
					putspan(tif, a2-a1, TIFFFaxBlackCodes);
Packit 7838c8
				} else {
Packit 7838c8
					putspan(tif, a1-a0, TIFFFaxBlackCodes);
Packit 7838c8
					putspan(tif, a2-a1, TIFFFaxWhiteCodes);
Packit 7838c8
				}
Packit 7838c8
				a0 = a2;
Packit 7838c8
			} else {			/* vertical mode */
Packit 7838c8
				putcode(tif, &vcodes[d+3]);
Packit 7838c8
				a0 = a1;
Packit 7838c8
			}
Packit 7838c8
		} else {				/* pass mode */
Packit 7838c8
			putcode(tif, &passcode);
Packit 7838c8
			a0 = b2;
Packit 7838c8
		}
Packit 7838c8
		if (a0 >= bits)
Packit 7838c8
			break;
Packit 7838c8
		a1 = finddiff(bp, a0, bits, PIXEL(bp,a0));
Packit 7838c8
		b1 = finddiff(rp, a0, bits, !PIXEL(bp,a0));
Packit 7838c8
		b1 = finddiff(rp, b1, bits, PIXEL(bp,a0));
Packit 7838c8
	}
Packit 7838c8
	return (1);
Packit 7838c8
#undef PIXEL
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Encode a buffer of pixels.
Packit 7838c8
 */
Packit 7838c8
static int
Packit 7838c8
Fax3Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
Packit 7838c8
{
Packit 7838c8
	static const char module[] = "Fax3Encode";
Packit 7838c8
	Fax3CodecState* sp = EncoderState(tif);
Packit 7838c8
	(void) s;
Packit 7838c8
	if (cc % sp->b.rowbytes)
Packit 7838c8
	{
Packit 7838c8
		TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be written");
Packit 7838c8
		return (0);
Packit 7838c8
	}
Packit 7838c8
	while (cc > 0) {
Packit 7838c8
		if ((sp->b.mode & FAXMODE_NOEOL) == 0)
Packit 7838c8
			Fax3PutEOL(tif);
Packit 7838c8
		if (is2DEncoding(sp)) {
Packit 7838c8
			if (sp->tag == G3_1D) {
Packit 7838c8
				if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels))
Packit 7838c8
					return (0);
Packit 7838c8
				sp->tag = G3_2D;
Packit 7838c8
			} else {
Packit 7838c8
				if (!Fax3Encode2DRow(tif, bp, sp->refline,
Packit 7838c8
				    sp->b.rowpixels))
Packit 7838c8
					return (0);
Packit 7838c8
				sp->k--;
Packit 7838c8
			}
Packit 7838c8
			if (sp->k == 0) {
Packit 7838c8
				sp->tag = G3_1D;
Packit 7838c8
				sp->k = sp->maxk-1;
Packit 7838c8
			} else
Packit 7838c8
				_TIFFmemcpy(sp->refline, bp, sp->b.rowbytes);
Packit 7838c8
		} else {
Packit 7838c8
			if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels))
Packit 7838c8
				return (0);
Packit 7838c8
		}
Packit 7838c8
		bp += sp->b.rowbytes;
Packit 7838c8
		cc -= sp->b.rowbytes;
Packit 7838c8
	}
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
Fax3PostEncode(TIFF* tif)
Packit 7838c8
{
Packit 7838c8
	Fax3CodecState* sp = EncoderState(tif);
Packit 7838c8
Packit 7838c8
	if (sp->bit != 8)
Packit 7838c8
		Fax3FlushBits(tif, sp);
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static void
Packit 7838c8
Fax3Close(TIFF* tif)
Packit 7838c8
{
Packit 7838c8
	if ((Fax3State(tif)->mode & FAXMODE_NORTC) == 0 && tif->tif_rawcp) {
Packit 7838c8
		Fax3CodecState* sp = EncoderState(tif);
Packit 7838c8
		unsigned int code = EOL;
Packit 7838c8
		unsigned int length = 12;
Packit 7838c8
		int i;
Packit 7838c8
Packit 7838c8
		if (is2DEncoding(sp)) {
Packit 7838c8
			code = (code<<1) | (sp->tag == G3_1D);
Packit 7838c8
			length++;
Packit 7838c8
		}
Packit 7838c8
		for (i = 0; i < 6; i++)
Packit 7838c8
			Fax3PutBits(tif, code, length);
Packit 7838c8
		Fax3FlushBits(tif, sp);
Packit 7838c8
	}
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static void
Packit 7838c8
Fax3Cleanup(TIFF* tif)
Packit 7838c8
{
Packit 7838c8
	Fax3CodecState* sp = DecoderState(tif);
Packit 7838c8
	
Packit 7838c8
	assert(sp != 0);
Packit 7838c8
Packit 7838c8
	tif->tif_tagmethods.vgetfield = sp->b.vgetparent;
Packit 7838c8
	tif->tif_tagmethods.vsetfield = sp->b.vsetparent;
Packit 7838c8
	tif->tif_tagmethods.printdir = sp->b.printdir;
Packit 7838c8
Packit 7838c8
	if (sp->runs)
Packit 7838c8
		_TIFFfree(sp->runs);
Packit 7838c8
	if (sp->refline)
Packit 7838c8
		_TIFFfree(sp->refline);
Packit 7838c8
Packit 7838c8
	_TIFFfree(tif->tif_data);
Packit 7838c8
	tif->tif_data = NULL;
Packit 7838c8
Packit 7838c8
	_TIFFSetDefaultCompressionState(tif);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
#define	FIELD_BADFAXLINES	(FIELD_CODEC+0)
Packit 7838c8
#define	FIELD_CLEANFAXDATA	(FIELD_CODEC+1)
Packit 7838c8
#define	FIELD_BADFAXRUN		(FIELD_CODEC+2)
Packit 7838c8
Packit 7838c8
#define	FIELD_OPTIONS		(FIELD_CODEC+7)
Packit 7838c8
Packit 7838c8
static const TIFFField faxFields[] = {
Packit 7838c8
    { TIFFTAG_FAXMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "FaxMode", NULL },
Packit 7838c8
    { TIFFTAG_FAXFILLFUNC, 0, 0, TIFF_ANY, 0, TIFF_SETGET_OTHER, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "FaxFillFunc", NULL },
Packit 7838c8
    { TIFFTAG_BADFAXLINES, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_BADFAXLINES, TRUE, FALSE, "BadFaxLines", NULL },
Packit 7838c8
    { TIFFTAG_CLEANFAXDATA, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UINT16, FIELD_CLEANFAXDATA, TRUE, FALSE, "CleanFaxData", NULL },
Packit 7838c8
    { TIFFTAG_CONSECUTIVEBADFAXLINES, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_BADFAXRUN, TRUE, FALSE, "ConsecutiveBadFaxLines", NULL }};
Packit 7838c8
static const TIFFField fax3Fields[] = {
Packit 7838c8
    { TIFFTAG_GROUP3OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_OPTIONS, FALSE, FALSE, "Group3Options", NULL },
Packit 7838c8
};
Packit 7838c8
static const TIFFField fax4Fields[] = {
Packit 7838c8
    { TIFFTAG_GROUP4OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_OPTIONS, FALSE, FALSE, "Group4Options", NULL },
Packit 7838c8
};
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
Fax3VSetField(TIFF* tif, uint32 tag, va_list ap)
Packit 7838c8
{
Packit 7838c8
	Fax3BaseState* sp = Fax3State(tif);
Packit 7838c8
	const TIFFField* fip;
Packit 7838c8
Packit 7838c8
	assert(sp != 0);
Packit 7838c8
	assert(sp->vsetparent != 0);
Packit 7838c8
Packit 7838c8
	switch (tag) {
Packit 7838c8
	case TIFFTAG_FAXMODE:
Packit 7838c8
		sp->mode = (int) va_arg(ap, int);
Packit 7838c8
		return 1;			/* NB: pseudo tag */
Packit 7838c8
	case TIFFTAG_FAXFILLFUNC:
Packit 7838c8
		DecoderState(tif)->fill = va_arg(ap, TIFFFaxFillFunc);
Packit 7838c8
		return 1;			/* NB: pseudo tag */
Packit 7838c8
	case TIFFTAG_GROUP3OPTIONS:
Packit 7838c8
		/* XXX: avoid reading options if compression mismatches. */
Packit 7838c8
		if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX3)
Packit 7838c8
			sp->groupoptions = (uint32) va_arg(ap, uint32);
Packit 7838c8
		break;
Packit 7838c8
	case TIFFTAG_GROUP4OPTIONS:
Packit 7838c8
		/* XXX: avoid reading options if compression mismatches. */
Packit 7838c8
		if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4)
Packit 7838c8
			sp->groupoptions = (uint32) va_arg(ap, uint32);
Packit 7838c8
		break;
Packit 7838c8
	case TIFFTAG_BADFAXLINES:
Packit 7838c8
		sp->badfaxlines = (uint32) va_arg(ap, uint32);
Packit 7838c8
		break;
Packit 7838c8
	case TIFFTAG_CLEANFAXDATA:
Packit 7838c8
		sp->cleanfaxdata = (uint16) va_arg(ap, uint16_vap);
Packit 7838c8
		break;
Packit 7838c8
	case TIFFTAG_CONSECUTIVEBADFAXLINES:
Packit 7838c8
		sp->badfaxrun = (uint32) va_arg(ap, uint32);
Packit 7838c8
		break;
Packit 7838c8
	default:
Packit 7838c8
		return (*sp->vsetparent)(tif, tag, ap);
Packit 7838c8
	}
Packit 7838c8
	
Packit 7838c8
	if ((fip = TIFFFieldWithTag(tif, tag)) != NULL)
Packit 7838c8
		TIFFSetFieldBit(tif, fip->field_bit);
Packit 7838c8
	else
Packit 7838c8
		return 0;
Packit 7838c8
Packit 7838c8
	tif->tif_flags |= TIFF_DIRTYDIRECT;
Packit 7838c8
	return 1;
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
Fax3VGetField(TIFF* tif, uint32 tag, va_list ap)
Packit 7838c8
{
Packit 7838c8
	Fax3BaseState* sp = Fax3State(tif);
Packit 7838c8
Packit 7838c8
	assert(sp != 0);
Packit 7838c8
Packit 7838c8
	switch (tag) {
Packit 7838c8
	case TIFFTAG_FAXMODE:
Packit 7838c8
		*va_arg(ap, int*) = sp->mode;
Packit 7838c8
		break;
Packit 7838c8
	case TIFFTAG_FAXFILLFUNC:
Packit 7838c8
		*va_arg(ap, TIFFFaxFillFunc*) = DecoderState(tif)->fill;
Packit 7838c8
		break;
Packit 7838c8
	case TIFFTAG_GROUP3OPTIONS:
Packit 7838c8
	case TIFFTAG_GROUP4OPTIONS:
Packit 7838c8
		*va_arg(ap, uint32*) = sp->groupoptions;
Packit 7838c8
		break;
Packit 7838c8
	case TIFFTAG_BADFAXLINES:
Packit 7838c8
		*va_arg(ap, uint32*) = sp->badfaxlines;
Packit 7838c8
		break;
Packit 7838c8
	case TIFFTAG_CLEANFAXDATA:
Packit 7838c8
		*va_arg(ap, uint16*) = sp->cleanfaxdata;
Packit 7838c8
		break;
Packit 7838c8
	case TIFFTAG_CONSECUTIVEBADFAXLINES:
Packit 7838c8
		*va_arg(ap, uint32*) = sp->badfaxrun;
Packit 7838c8
		break;
Packit 7838c8
	default:
Packit 7838c8
		return (*sp->vgetparent)(tif, tag, ap);
Packit 7838c8
	}
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static void
Packit 7838c8
Fax3PrintDir(TIFF* tif, FILE* fd, long flags)
Packit 7838c8
{
Packit 7838c8
	Fax3BaseState* sp = Fax3State(tif);
Packit 7838c8
Packit 7838c8
	assert(sp != 0);
Packit 7838c8
Packit 7838c8
	(void) flags;
Packit 7838c8
	if (TIFFFieldSet(tif,FIELD_OPTIONS)) {
Packit 7838c8
		const char* sep = " ";
Packit 7838c8
		if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4) {
Packit 7838c8
			fprintf(fd, "  Group 4 Options:");
Packit 7838c8
			if (sp->groupoptions & GROUP4OPT_UNCOMPRESSED)
Packit 7838c8
				fprintf(fd, "%suncompressed data", sep);
Packit 7838c8
		} else {
Packit 7838c8
Packit 7838c8
			fprintf(fd, "  Group 3 Options:");
Packit 7838c8
			if (sp->groupoptions & GROUP3OPT_2DENCODING) {
Packit 7838c8
				fprintf(fd, "%s2-d encoding", sep);
Packit 7838c8
				sep = "+";
Packit 7838c8
			}
Packit 7838c8
			if (sp->groupoptions & GROUP3OPT_FILLBITS) {
Packit 7838c8
				fprintf(fd, "%sEOL padding", sep);
Packit 7838c8
				sep = "+";
Packit 7838c8
			}
Packit 7838c8
			if (sp->groupoptions & GROUP3OPT_UNCOMPRESSED)
Packit 7838c8
				fprintf(fd, "%suncompressed data", sep);
Packit 7838c8
		}
Packit 7838c8
		fprintf(fd, " (%lu = 0x%lx)\n",
Packit 7838c8
                        (unsigned long) sp->groupoptions,
Packit 7838c8
                        (unsigned long) sp->groupoptions);
Packit 7838c8
	}
Packit 7838c8
	if (TIFFFieldSet(tif,FIELD_CLEANFAXDATA)) {
Packit 7838c8
		fprintf(fd, "  Fax Data:");
Packit 7838c8
		switch (sp->cleanfaxdata) {
Packit 7838c8
		case CLEANFAXDATA_CLEAN:
Packit 7838c8
			fprintf(fd, " clean");
Packit 7838c8
			break;
Packit 7838c8
		case CLEANFAXDATA_REGENERATED:
Packit 7838c8
			fprintf(fd, " receiver regenerated");
Packit 7838c8
			break;
Packit 7838c8
		case CLEANFAXDATA_UNCLEAN:
Packit 7838c8
			fprintf(fd, " uncorrected errors");
Packit 7838c8
			break;
Packit 7838c8
		}
Packit 7838c8
		fprintf(fd, " (%u = 0x%x)\n",
Packit 7838c8
		    sp->cleanfaxdata, sp->cleanfaxdata);
Packit 7838c8
	}
Packit 7838c8
	if (TIFFFieldSet(tif,FIELD_BADFAXLINES))
Packit 7838c8
		fprintf(fd, "  Bad Fax Lines: %lu\n",
Packit 7838c8
                        (unsigned long) sp->badfaxlines);
Packit 7838c8
	if (TIFFFieldSet(tif,FIELD_BADFAXRUN))
Packit 7838c8
		fprintf(fd, "  Consecutive Bad Fax Lines: %lu\n",
Packit 7838c8
		    (unsigned long) sp->badfaxrun);
Packit 7838c8
	if (sp->printdir)
Packit 7838c8
		(*sp->printdir)(tif, fd, flags);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
InitCCITTFax3(TIFF* tif)
Packit 7838c8
{
Packit 7838c8
	static const char module[] = "InitCCITTFax3";
Packit 7838c8
	Fax3BaseState* sp;
Packit 7838c8
Packit 7838c8
	/*
Packit 7838c8
	 * Merge codec-specific tag information.
Packit 7838c8
	 */
Packit 7838c8
	if (!_TIFFMergeFields(tif, faxFields, TIFFArrayCount(faxFields))) {
Packit 7838c8
		TIFFErrorExt(tif->tif_clientdata, "InitCCITTFax3",
Packit 7838c8
			"Merging common CCITT Fax codec-specific tags failed");
Packit 7838c8
		return 0;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	/*
Packit 7838c8
	 * Allocate state block so tag methods have storage to record values.
Packit 7838c8
	 */
Packit 7838c8
	tif->tif_data = (uint8*)
Packit 7838c8
		_TIFFmalloc(sizeof (Fax3CodecState));
Packit 7838c8
Packit 7838c8
	if (tif->tif_data == NULL) {
Packit 7838c8
		TIFFErrorExt(tif->tif_clientdata, module,
Packit 7838c8
		    "No space for state block");
Packit 7838c8
		return (0);
Packit 7838c8
	}
Packit 7838c8
	_TIFFmemset(tif->tif_data, 0, sizeof (Fax3CodecState));
Packit 7838c8
Packit 7838c8
	sp = Fax3State(tif);
Packit 7838c8
        sp->rw_mode = tif->tif_mode;
Packit 7838c8
Packit 7838c8
	/*
Packit 7838c8
	 * Override parent get/set field methods.
Packit 7838c8
	 */
Packit 7838c8
	sp->vgetparent = tif->tif_tagmethods.vgetfield;
Packit 7838c8
	tif->tif_tagmethods.vgetfield = Fax3VGetField; /* hook for codec tags */
Packit 7838c8
	sp->vsetparent = tif->tif_tagmethods.vsetfield;
Packit 7838c8
	tif->tif_tagmethods.vsetfield = Fax3VSetField; /* hook for codec tags */
Packit 7838c8
	sp->printdir = tif->tif_tagmethods.printdir;
Packit 7838c8
	tif->tif_tagmethods.printdir = Fax3PrintDir;   /* hook for codec tags */
Packit 7838c8
	sp->groupoptions = 0;	
Packit 7838c8
Packit 7838c8
	if (sp->rw_mode == O_RDONLY) /* FIXME: improve for in place update */
Packit 7838c8
		tif->tif_flags |= TIFF_NOBITREV; /* decoder does bit reversal */
Packit 7838c8
	DecoderState(tif)->runs = NULL;
Packit 7838c8
	TIFFSetField(tif, TIFFTAG_FAXFILLFUNC, _TIFFFax3fillruns);
Packit 7838c8
	EncoderState(tif)->refline = NULL;
Packit 7838c8
Packit 7838c8
	/*
Packit 7838c8
	 * Install codec methods.
Packit 7838c8
	 */
Packit 7838c8
	tif->tif_fixuptags = Fax3FixupTags;
Packit 7838c8
	tif->tif_setupdecode = Fax3SetupState;
Packit 7838c8
	tif->tif_predecode = Fax3PreDecode;
Packit 7838c8
	tif->tif_decoderow = Fax3Decode1D;
Packit 7838c8
	tif->tif_decodestrip = Fax3Decode1D;
Packit 7838c8
	tif->tif_decodetile = Fax3Decode1D;
Packit 7838c8
	tif->tif_setupencode = Fax3SetupState;
Packit 7838c8
	tif->tif_preencode = Fax3PreEncode;
Packit 7838c8
	tif->tif_postencode = Fax3PostEncode;
Packit 7838c8
	tif->tif_encoderow = Fax3Encode;
Packit 7838c8
	tif->tif_encodestrip = Fax3Encode;
Packit 7838c8
	tif->tif_encodetile = Fax3Encode;
Packit 7838c8
	tif->tif_close = Fax3Close;
Packit 7838c8
	tif->tif_cleanup = Fax3Cleanup;
Packit 7838c8
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
int
Packit 7838c8
TIFFInitCCITTFax3(TIFF* tif, int scheme)
Packit 7838c8
{
Packit 7838c8
	(void) scheme;
Packit 7838c8
	if (InitCCITTFax3(tif)) {
Packit 7838c8
		/*
Packit 7838c8
		 * Merge codec-specific tag information.
Packit 7838c8
		 */
Packit 7838c8
		if (!_TIFFMergeFields(tif, fax3Fields,
Packit 7838c8
				      TIFFArrayCount(fax3Fields))) {
Packit 7838c8
			TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax3",
Packit 7838c8
			"Merging CCITT Fax 3 codec-specific tags failed");
Packit 7838c8
			return 0;
Packit 7838c8
		}
Packit 7838c8
Packit 7838c8
		/*
Packit 7838c8
		 * The default format is Class/F-style w/o RTC.
Packit 7838c8
		 */
Packit 7838c8
		return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_CLASSF);
Packit 7838c8
	} else
Packit 7838c8
		return 01;
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * CCITT Group 4 (T.6) Facsimile-compatible
Packit 7838c8
 * Compression Scheme Support.
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
#define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }
Packit 7838c8
/*
Packit 7838c8
 * Decode the requested amount of G4-encoded data.
Packit 7838c8
 */
Packit 7838c8
static int
Packit 7838c8
Fax4Decode(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
Packit 7838c8
{
Packit 7838c8
	DECLARE_STATE_2D(tif, sp, "Fax4Decode");
Packit 7838c8
	(void) s;
Packit 7838c8
	if (occ % sp->b.rowbytes)
Packit 7838c8
	{
Packit 7838c8
		TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
Packit 7838c8
		return (-1);
Packit 7838c8
	}
Packit 7838c8
	CACHE_STATE(tif, sp);
Packit 7838c8
	while (occ > 0) {
Packit 7838c8
		a0 = 0;
Packit 7838c8
		RunLength = 0;
Packit 7838c8
		pa = thisrun = sp->curruns;
Packit 7838c8
		pb = sp->refruns;
Packit 7838c8
		b1 = *pb++;
Packit 7838c8
#ifdef FAX3_DEBUG
Packit 7838c8
		printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail);
Packit 7838c8
		printf("-------------------- %d\n", tif->tif_row);
Packit 7838c8
		fflush(stdout);
Packit 7838c8
#endif
Packit 7838c8
		EXPAND2D(EOFG4);
Packit 7838c8
                if (EOLcnt)
Packit 7838c8
                    goto EOFG4;
Packit 7838c8
		(*sp->fill)(buf, thisrun, pa, lastx);
Packit 7838c8
		SETVALUE(0);		/* imaginary change for reference */
Packit 7838c8
		SWAP(uint32*, sp->curruns, sp->refruns);
Packit 7838c8
		buf += sp->b.rowbytes;
Packit 7838c8
		occ -= sp->b.rowbytes;
Packit 7838c8
		sp->line++;
Packit 7838c8
		continue;
Packit 7838c8
	EOFG4:
Packit 7838c8
                NeedBits16( 13, BADG4 );
Packit 7838c8
        BADG4:
Packit 7838c8
#ifdef FAX3_DEBUG
Packit 7838c8
                if( GetBits(13) != 0x1001 )
Packit 7838c8
                    fputs( "Bad EOFB\n", stderr );
Packit 7838c8
#endif                
Packit 7838c8
                ClrBits( 13 );
Packit 7838c8
		(*sp->fill)(buf, thisrun, pa, lastx);
Packit 7838c8
		UNCACHE_STATE(tif, sp);
Packit 7838c8
		return ( sp->line ? 1 : -1);	/* don't error on badly-terminated strips */
Packit 7838c8
	}
Packit 7838c8
	UNCACHE_STATE(tif, sp);
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
#undef	SWAP
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Encode the requested amount of data.
Packit 7838c8
 */
Packit 7838c8
static int
Packit 7838c8
Fax4Encode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
Packit 7838c8
{
Packit 7838c8
	static const char module[] = "Fax4Encode";
Packit 7838c8
	Fax3CodecState *sp = EncoderState(tif);
Packit 7838c8
	(void) s;
Packit 7838c8
	if (cc % sp->b.rowbytes)
Packit 7838c8
	{
Packit 7838c8
		TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be written");
Packit 7838c8
		return (0);
Packit 7838c8
	}
Packit 7838c8
	while (cc > 0) {
Packit 7838c8
		if (!Fax3Encode2DRow(tif, bp, sp->refline, sp->b.rowpixels))
Packit 7838c8
			return (0);
Packit 7838c8
		_TIFFmemcpy(sp->refline, bp, sp->b.rowbytes);
Packit 7838c8
		bp += sp->b.rowbytes;
Packit 7838c8
		cc -= sp->b.rowbytes;
Packit 7838c8
	}
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
Fax4PostEncode(TIFF* tif)
Packit 7838c8
{
Packit 7838c8
	Fax3CodecState *sp = EncoderState(tif);
Packit 7838c8
Packit 7838c8
	/* terminate strip w/ EOFB */
Packit 7838c8
	Fax3PutBits(tif, EOL, 12);
Packit 7838c8
	Fax3PutBits(tif, EOL, 12);
Packit 7838c8
	if (sp->bit != 8)
Packit 7838c8
		Fax3FlushBits(tif, sp);
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
int
Packit 7838c8
TIFFInitCCITTFax4(TIFF* tif, int scheme)
Packit 7838c8
{
Packit 7838c8
	(void) scheme;
Packit 7838c8
	if (InitCCITTFax3(tif)) {		/* reuse G3 support */
Packit 7838c8
		/*
Packit 7838c8
		 * Merge codec-specific tag information.
Packit 7838c8
		 */
Packit 7838c8
		if (!_TIFFMergeFields(tif, fax4Fields,
Packit 7838c8
				      TIFFArrayCount(fax4Fields))) {
Packit 7838c8
			TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax4",
Packit 7838c8
			"Merging CCITT Fax 4 codec-specific tags failed");
Packit 7838c8
			return 0;
Packit 7838c8
		}
Packit 7838c8
Packit 7838c8
		tif->tif_decoderow = Fax4Decode;
Packit 7838c8
		tif->tif_decodestrip = Fax4Decode;
Packit 7838c8
		tif->tif_decodetile = Fax4Decode;
Packit 7838c8
		tif->tif_encoderow = Fax4Encode;
Packit 7838c8
		tif->tif_encodestrip = Fax4Encode;
Packit 7838c8
		tif->tif_encodetile = Fax4Encode;
Packit 7838c8
		tif->tif_postencode = Fax4PostEncode;
Packit 7838c8
		/*
Packit 7838c8
		 * Suppress RTC at the end of each strip.
Packit 7838c8
		 */
Packit 7838c8
		return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_NORTC);
Packit 7838c8
	} else
Packit 7838c8
		return (0);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * CCITT Group 3 1-D Modified Huffman RLE Compression Support.
Packit 7838c8
 * (Compression algorithms 2 and 32771)
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Decode the requested amount of RLE-encoded data.
Packit 7838c8
 */
Packit 7838c8
static int
Packit 7838c8
Fax3DecodeRLE(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
Packit 7838c8
{
Packit 7838c8
	DECLARE_STATE(tif, sp, "Fax3DecodeRLE");
Packit 7838c8
	int mode = sp->b.mode;
Packit 7838c8
	(void) s;
Packit 7838c8
	if (occ % sp->b.rowbytes)
Packit 7838c8
	{
Packit 7838c8
		TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
Packit 7838c8
		return (-1);
Packit 7838c8
	}
Packit 7838c8
	CACHE_STATE(tif, sp);
Packit 7838c8
	thisrun = sp->curruns;
Packit 7838c8
	while (occ > 0) {
Packit 7838c8
		a0 = 0;
Packit 7838c8
		RunLength = 0;
Packit 7838c8
		pa = thisrun;
Packit 7838c8
#ifdef FAX3_DEBUG
Packit 7838c8
		printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail);
Packit 7838c8
		printf("-------------------- %d\n", tif->tif_row);
Packit 7838c8
		fflush(stdout);
Packit 7838c8
#endif
Packit 7838c8
		EXPAND1D(EOFRLE);
Packit 7838c8
		(*sp->fill)(buf, thisrun, pa, lastx);
Packit 7838c8
		/*
Packit 7838c8
		 * Cleanup at the end of the row.
Packit 7838c8
		 */
Packit 7838c8
		if (mode & FAXMODE_BYTEALIGN) {
Packit 7838c8
			int n = BitsAvail - (BitsAvail &~ 7);
Packit 7838c8
			ClrBits(n);
Packit 7838c8
		} else if (mode & FAXMODE_WORDALIGN) {
Packit 7838c8
			int n = BitsAvail - (BitsAvail &~ 15);
Packit 7838c8
			ClrBits(n);
Packit 7838c8
			if (BitsAvail == 0 && !isAligned(cp, uint16))
Packit 7838c8
			    cp++;
Packit 7838c8
		}
Packit 7838c8
		buf += sp->b.rowbytes;
Packit 7838c8
		occ -= sp->b.rowbytes;
Packit 7838c8
		sp->line++;
Packit 7838c8
		continue;
Packit 7838c8
	EOFRLE:				/* premature EOF */
Packit 7838c8
		(*sp->fill)(buf, thisrun, pa, lastx);
Packit 7838c8
		UNCACHE_STATE(tif, sp);
Packit 7838c8
		return (-1);
Packit 7838c8
	}
Packit 7838c8
	UNCACHE_STATE(tif, sp);
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
int
Packit 7838c8
TIFFInitCCITTRLE(TIFF* tif, int scheme)
Packit 7838c8
{
Packit 7838c8
	(void) scheme;
Packit 7838c8
	if (InitCCITTFax3(tif)) {		/* reuse G3 support */
Packit 7838c8
		tif->tif_decoderow = Fax3DecodeRLE;
Packit 7838c8
		tif->tif_decodestrip = Fax3DecodeRLE;
Packit 7838c8
		tif->tif_decodetile = Fax3DecodeRLE;
Packit 7838c8
		/*
Packit 7838c8
		 * Suppress RTC+EOLs when encoding and byte-align data.
Packit 7838c8
		 */
Packit 7838c8
		return TIFFSetField(tif, TIFFTAG_FAXMODE,
Packit 7838c8
		    FAXMODE_NORTC|FAXMODE_NOEOL|FAXMODE_BYTEALIGN);
Packit 7838c8
	} else
Packit 7838c8
		return (0);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
int
Packit 7838c8
TIFFInitCCITTRLEW(TIFF* tif, int scheme)
Packit 7838c8
{
Packit 7838c8
	(void) scheme;
Packit 7838c8
	if (InitCCITTFax3(tif)) {		/* reuse G3 support */
Packit 7838c8
		tif->tif_decoderow = Fax3DecodeRLE;
Packit 7838c8
		tif->tif_decodestrip = Fax3DecodeRLE;
Packit 7838c8
		tif->tif_decodetile = Fax3DecodeRLE;  
Packit 7838c8
		/*
Packit 7838c8
		 * Suppress RTC+EOLs when encoding and word-align data.
Packit 7838c8
		 */
Packit 7838c8
		return TIFFSetField(tif, TIFFTAG_FAXMODE,
Packit 7838c8
		    FAXMODE_NORTC|FAXMODE_NOEOL|FAXMODE_WORDALIGN);
Packit 7838c8
	} else
Packit 7838c8
		return (0);
Packit 7838c8
}
Packit 7838c8
#endif /* CCITT_SUPPORT */
Packit 7838c8
Packit 7838c8
/* vim: set ts=8 sts=8 sw=8 noet: */
Packit 7838c8
/*
Packit 7838c8
 * Local Variables:
Packit 7838c8
 * mode: c
Packit 7838c8
 * c-basic-offset: 8
Packit 7838c8
 * fill-column: 78
Packit 7838c8
 * End:
Packit 7838c8
 */