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