Blame libtiff/tif_fax3.h

Packit 7838c8
/* $Id: tif_fax3.h,v 1.13 2016-12-14 18:36:27 faxguy 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
#ifndef _FAX3_
Packit 7838c8
#define	_FAX3_
Packit 7838c8
/*
Packit 7838c8
 * TIFF Library.
Packit 7838c8
 *
Packit 7838c8
 * CCITT Group 3 (T.4) and Group 4 (T.6) Decompression Support.
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 "tiff.h"
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * To override the default routine used to image decoded
Packit 7838c8
 * spans one can use the pseudo tag TIFFTAG_FAXFILLFUNC.
Packit 7838c8
 * The routine must have the type signature given below;
Packit 7838c8
 * for example:
Packit 7838c8
 *
Packit 7838c8
 * fillruns(unsigned char* buf, uint32* runs, uint32* erun, uint32 lastx)
Packit 7838c8
 *
Packit 7838c8
 * where buf is place to set the bits, runs is the array of b&w run
Packit 7838c8
 * lengths (white then black), erun is the last run in the array, and
Packit 7838c8
 * lastx is the width of the row in pixels.  Fill routines can assume
Packit 7838c8
 * the run array has room for at least lastx runs and can overwrite
Packit 7838c8
 * data in the run array as needed (e.g. to append zero runs to bring
Packit 7838c8
 * the count up to a nice multiple).
Packit 7838c8
 */
Packit 7838c8
typedef void (*TIFFFaxFillFunc)(unsigned char*, uint32*, uint32*, uint32);
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * The default run filler; made external for other decoders.
Packit 7838c8
 */
Packit 7838c8
#if defined(__cplusplus)
Packit 7838c8
extern "C" {
Packit 7838c8
#endif
Packit 7838c8
extern void _TIFFFax3fillruns(unsigned char*, uint32*, uint32*, uint32);
Packit 7838c8
#if defined(__cplusplus)
Packit 7838c8
}
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
Packit 7838c8
/* finite state machine codes */
Packit 7838c8
#define S_Null     0
Packit 7838c8
#define S_Pass     1
Packit 7838c8
#define S_Horiz    2
Packit 7838c8
#define S_V0       3
Packit 7838c8
#define S_VR       4
Packit 7838c8
#define S_VL       5
Packit 7838c8
#define S_Ext      6
Packit 7838c8
#define S_TermW    7
Packit 7838c8
#define S_TermB    8
Packit 7838c8
#define S_MakeUpW  9
Packit 7838c8
#define S_MakeUpB  10
Packit 7838c8
#define S_MakeUp   11
Packit 7838c8
#define S_EOL      12
Packit 7838c8
Packit 7838c8
/* WARNING: do not change the layout of this structure as the HylaFAX software */
Packit 7838c8
/* really depends on it. See http://bugzilla.maptools.org/show_bug.cgi?id=2636 */
Packit 7838c8
typedef struct {                /* state table entry */
Packit 7838c8
	unsigned char State;    /* see above */
Packit 7838c8
	unsigned char Width;    /* width of code in bits */
Packit 7838c8
	uint32 Param;           /* unsigned 32-bit run length in bits (holds on 16 bit actually, but cannot be changed. See above warning) */
Packit 7838c8
} TIFFFaxTabEnt;
Packit 7838c8
Packit 7838c8
extern const TIFFFaxTabEnt TIFFFaxMainTable[];
Packit 7838c8
extern const TIFFFaxTabEnt TIFFFaxWhiteTable[];
Packit 7838c8
extern const TIFFFaxTabEnt TIFFFaxBlackTable[];
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * The following macros define the majority of the G3/G4 decoder
Packit 7838c8
 * algorithm using the state tables defined elsewhere.  To build
Packit 7838c8
 * a decoder you need some setup code and some glue code. Note
Packit 7838c8
 * that you may also need/want to change the way the NeedBits*
Packit 7838c8
 * macros get input data if, for example, you know the data to be
Packit 7838c8
 * decoded is properly aligned and oriented (doing so before running
Packit 7838c8
 * the decoder can be a big performance win).
Packit 7838c8
 *
Packit 7838c8
 * Consult the decoder in the TIFF library for an idea of what you
Packit 7838c8
 * need to define and setup to make use of these definitions.
Packit 7838c8
 *
Packit 7838c8
 * NB: to enable a debugging version of these macros define FAX3_DEBUG
Packit 7838c8
 *     before including this file.  Trace output goes to stdout.
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
#ifndef EndOfData
Packit 7838c8
#define EndOfData()	(cp >= ep)
Packit 7838c8
#endif
Packit 7838c8
/*
Packit 7838c8
 * Need <=8 or <=16 bits of input data.  Unlike viewfax we
Packit 7838c8
 * cannot use/assume a word-aligned, properly bit swizzled
Packit 7838c8
 * input data set because data may come from an arbitrarily
Packit 7838c8
 * aligned, read-only source such as a memory-mapped file.
Packit 7838c8
 * Note also that the viewfax decoder does not check for
Packit 7838c8
 * running off the end of the input data buffer.  This is
Packit 7838c8
 * possible for G3-encoded data because it prescans the input
Packit 7838c8
 * data to count EOL markers, but can cause problems for G4
Packit 7838c8
 * data.  In any event, we don't prescan and must watch for
Packit 7838c8
 * running out of data since we can't permit the library to
Packit 7838c8
 * scan past the end of the input data buffer.
Packit 7838c8
 *
Packit 7838c8
 * Finally, note that we must handle remaindered data at the end
Packit 7838c8
 * of a strip specially.  The coder asks for a fixed number of
Packit 7838c8
 * bits when scanning for the next code.  This may be more bits
Packit 7838c8
 * than are actually present in the data stream.  If we appear
Packit 7838c8
 * to run out of data but still have some number of valid bits
Packit 7838c8
 * remaining then we makeup the requested amount with zeros and
Packit 7838c8
 * return successfully.  If the returned data is incorrect then
Packit 7838c8
 * we should be called again and get a premature EOF error;
Packit 7838c8
 * otherwise we should get the right answer.
Packit 7838c8
 */
Packit 7838c8
#ifndef NeedBits8
Packit 7838c8
#define NeedBits8(n,eoflab) do {					\
Packit 7838c8
    if (BitsAvail < (n)) {						\
Packit 7838c8
	if (EndOfData()) {						\
Packit 7838c8
	    if (BitsAvail == 0)			/* no valid bits */	\
Packit 7838c8
		goto eoflab;						\
Packit 7838c8
	    BitsAvail = (n);			/* pad with zeros */	\
Packit 7838c8
	} else {							\
Packit 7838c8
	    BitAcc |= ((uint32) bitmap[*cp++])<
Packit 7838c8
	    BitsAvail += 8;						\
Packit 7838c8
	}								\
Packit 7838c8
    }									\
Packit 7838c8
} while (0)
Packit 7838c8
#endif
Packit 7838c8
#ifndef NeedBits16
Packit 7838c8
#define NeedBits16(n,eoflab) do {					\
Packit 7838c8
    if (BitsAvail < (n)) {						\
Packit 7838c8
	if (EndOfData()) {						\
Packit 7838c8
	    if (BitsAvail == 0)			/* no valid bits */	\
Packit 7838c8
		goto eoflab;						\
Packit 7838c8
	    BitsAvail = (n);			/* pad with zeros */	\
Packit 7838c8
	} else {							\
Packit 7838c8
	    BitAcc |= ((uint32) bitmap[*cp++])<
Packit 7838c8
	    if ((BitsAvail += 8) < (n)) {				\
Packit 7838c8
		if (EndOfData()) {					\
Packit 7838c8
		    /* NB: we know BitsAvail is non-zero here */	\
Packit 7838c8
		    BitsAvail = (n);		/* pad with zeros */	\
Packit 7838c8
		} else {						\
Packit 7838c8
		    BitAcc |= ((uint32) bitmap[*cp++])<
Packit 7838c8
		    BitsAvail += 8;					\
Packit 7838c8
		}							\
Packit 7838c8
	    }								\
Packit 7838c8
	}								\
Packit 7838c8
    }									\
Packit 7838c8
} while (0)
Packit 7838c8
#endif
Packit 7838c8
#define GetBits(n)	(BitAcc & ((1<<(n))-1))
Packit 7838c8
#define ClrBits(n) do {							\
Packit 7838c8
    BitsAvail -= (n);							\
Packit 7838c8
    BitAcc >>= (n);							\
Packit 7838c8
} while (0)
Packit 7838c8
Packit 7838c8
#ifdef FAX3_DEBUG
Packit 7838c8
static const char* StateNames[] = {
Packit 7838c8
    "Null   ",
Packit 7838c8
    "Pass   ",
Packit 7838c8
    "Horiz  ",
Packit 7838c8
    "V0     ",
Packit 7838c8
    "VR     ",
Packit 7838c8
    "VL     ",
Packit 7838c8
    "Ext    ",
Packit 7838c8
    "TermW  ",
Packit 7838c8
    "TermB  ",
Packit 7838c8
    "MakeUpW",
Packit 7838c8
    "MakeUpB",
Packit 7838c8
    "MakeUp ",
Packit 7838c8
    "EOL    ",
Packit 7838c8
};
Packit 7838c8
#define DEBUG_SHOW putchar(BitAcc & (1 << t) ? '1' : '0')
Packit 7838c8
#define LOOKUP8(wid,tab,eoflab) do {					\
Packit 7838c8
    int t;								\
Packit 7838c8
    NeedBits8(wid,eoflab);						\
Packit 7838c8
    TabEnt = tab + GetBits(wid);					\
Packit 7838c8
    printf("%08lX/%d: %s%5d\t", (long) BitAcc, BitsAvail,		\
Packit 7838c8
	   StateNames[TabEnt->State], TabEnt->Param);			\
Packit 7838c8
    for (t = 0; t < TabEnt->Width; t++)					\
Packit 7838c8
	DEBUG_SHOW;							\
Packit 7838c8
    putchar('\n');							\
Packit 7838c8
    fflush(stdout);							\
Packit 7838c8
    ClrBits(TabEnt->Width);						\
Packit 7838c8
} while (0)
Packit 7838c8
#define LOOKUP16(wid,tab,eoflab) do {					\
Packit 7838c8
    int t;								\
Packit 7838c8
    NeedBits16(wid,eoflab);						\
Packit 7838c8
    TabEnt = tab + GetBits(wid);					\
Packit 7838c8
    printf("%08lX/%d: %s%5d\t", (long) BitAcc, BitsAvail,		\
Packit 7838c8
	   StateNames[TabEnt->State], TabEnt->Param);			\
Packit 7838c8
    for (t = 0; t < TabEnt->Width; t++)					\
Packit 7838c8
	DEBUG_SHOW;							\
Packit 7838c8
    putchar('\n');							\
Packit 7838c8
    fflush(stdout);							\
Packit 7838c8
    ClrBits(TabEnt->Width);						\
Packit 7838c8
} while (0)
Packit 7838c8
Packit 7838c8
#define SETVALUE(x) do {							\
Packit 7838c8
    *pa++ = RunLength + (x);						\
Packit 7838c8
    printf("SETVALUE: %d\t%d\n", RunLength + (x), a0);			\
Packit 7838c8
    a0 += x;								\
Packit 7838c8
    RunLength = 0;							\
Packit 7838c8
} while (0)
Packit 7838c8
#else
Packit 7838c8
#define LOOKUP8(wid,tab,eoflab) do {					\
Packit 7838c8
    NeedBits8(wid,eoflab);						\
Packit 7838c8
    TabEnt = tab + GetBits(wid);					\
Packit 7838c8
    ClrBits(TabEnt->Width);						\
Packit 7838c8
} while (0)
Packit 7838c8
#define LOOKUP16(wid,tab,eoflab) do {					\
Packit 7838c8
    NeedBits16(wid,eoflab);						\
Packit 7838c8
    TabEnt = tab + GetBits(wid);					\
Packit 7838c8
    ClrBits(TabEnt->Width);						\
Packit 7838c8
} while (0)
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Append a run to the run length array for the
Packit 7838c8
 * current row and reset decoding state.
Packit 7838c8
 */
Packit 7838c8
#define SETVALUE(x) do {							\
Packit 7838c8
    *pa++ = RunLength + (x);						\
Packit 7838c8
    a0 += (x);								\
Packit 7838c8
    RunLength = 0;							\
Packit 7838c8
} while (0)
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Synchronize input decoding at the start of each
Packit 7838c8
 * row by scanning for an EOL (if appropriate) and
Packit 7838c8
 * skipping any trash data that might be present
Packit 7838c8
 * after a decoding error.  Note that the decoding
Packit 7838c8
 * done elsewhere that recognizes an EOL only consumes
Packit 7838c8
 * 11 consecutive zero bits.  This means that if EOLcnt
Packit 7838c8
 * is non-zero then we still need to scan for the final flag
Packit 7838c8
 * bit that is part of the EOL code.
Packit 7838c8
 */
Packit 7838c8
#define	SYNC_EOL(eoflab) do {						\
Packit 7838c8
    if (EOLcnt == 0) {							\
Packit 7838c8
	for (;;) {							\
Packit 7838c8
	    NeedBits16(11,eoflab);					\
Packit 7838c8
	    if (GetBits(11) == 0)					\
Packit 7838c8
		break;							\
Packit 7838c8
	    ClrBits(1);							\
Packit 7838c8
	}								\
Packit 7838c8
    }									\
Packit 7838c8
    for (;;) {								\
Packit 7838c8
	NeedBits8(8,eoflab);						\
Packit 7838c8
	if (GetBits(8))							\
Packit 7838c8
	    break;							\
Packit 7838c8
	ClrBits(8);							\
Packit 7838c8
    }									\
Packit 7838c8
    while (GetBits(1) == 0)						\
Packit 7838c8
	ClrBits(1);							\
Packit 7838c8
    ClrBits(1);				/* EOL bit */			\
Packit 7838c8
    EOLcnt = 0;				/* reset EOL counter/flag */	\
Packit 7838c8
} while (0)
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Cleanup the array of runs after decoding a row.
Packit 7838c8
 * We adjust final runs to insure the user buffer is not
Packit 7838c8
 * overwritten and/or undecoded area is white filled.
Packit 7838c8
 */
Packit 7838c8
#define	CLEANUP_RUNS() do {						\
Packit 7838c8
    if (RunLength)							\
Packit 7838c8
	SETVALUE(0);							\
Packit 7838c8
    if (a0 != lastx) {							\
Packit 7838c8
	badlength(a0, lastx);						\
Packit 7838c8
	while (a0 > lastx && pa > thisrun)				\
Packit 7838c8
	    a0 -= *--pa;						\
Packit 7838c8
	if (a0 < lastx) {						\
Packit 7838c8
	    if (a0 < 0)							\
Packit 7838c8
		a0 = 0;							\
Packit 7838c8
	    if ((pa-thisrun)&1)						\
Packit 7838c8
		SETVALUE(0);						\
Packit 7838c8
	    SETVALUE(lastx - a0);						\
Packit 7838c8
	} else if (a0 > lastx) {					\
Packit 7838c8
	    SETVALUE(lastx);						\
Packit 7838c8
	    SETVALUE(0);							\
Packit 7838c8
	}								\
Packit 7838c8
    }									\
Packit 7838c8
} while (0)
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Decode a line of 1D-encoded data.
Packit 7838c8
 *
Packit 7838c8
 * The line expanders are written as macros so that they can be reused
Packit 7838c8
 * but still have direct access to the local variables of the "calling"
Packit 7838c8
 * function.
Packit 7838c8
 *
Packit 7838c8
 * Note that unlike the original version we have to explicitly test for
Packit 7838c8
 * a0 >= lastx after each black/white run is decoded.  This is because
Packit 7838c8
 * the original code depended on the input data being zero-padded to
Packit 7838c8
 * insure the decoder recognized an EOL before running out of data.
Packit 7838c8
 */
Packit 7838c8
#define EXPAND1D(eoflab) do {						\
Packit 7838c8
    for (;;) {								\
Packit 7838c8
	for (;;) {							\
Packit 7838c8
	    LOOKUP16(12, TIFFFaxWhiteTable, eof1d);			\
Packit 7838c8
	    switch (TabEnt->State) {					\
Packit 7838c8
	    case S_EOL:							\
Packit 7838c8
		EOLcnt = 1;						\
Packit 7838c8
		goto done1d;						\
Packit 7838c8
	    case S_TermW:						\
Packit 7838c8
		SETVALUE(TabEnt->Param);					\
Packit 7838c8
		goto doneWhite1d;					\
Packit 7838c8
	    case S_MakeUpW:						\
Packit 7838c8
	    case S_MakeUp:						\
Packit 7838c8
		a0 += TabEnt->Param;					\
Packit 7838c8
		RunLength += TabEnt->Param;				\
Packit 7838c8
		break;							\
Packit 7838c8
	    default:							\
Packit 7838c8
		unexpected("WhiteTable", a0);				\
Packit 7838c8
		goto done1d;						\
Packit 7838c8
	    }								\
Packit 7838c8
	}								\
Packit 7838c8
    doneWhite1d:							\
Packit 7838c8
	if (a0 >= lastx)						\
Packit 7838c8
	    goto done1d;						\
Packit 7838c8
	for (;;) {							\
Packit 7838c8
	    LOOKUP16(13, TIFFFaxBlackTable, eof1d);			\
Packit 7838c8
	    switch (TabEnt->State) {					\
Packit 7838c8
	    case S_EOL:							\
Packit 7838c8
		EOLcnt = 1;						\
Packit 7838c8
		goto done1d;						\
Packit 7838c8
	    case S_TermB:						\
Packit 7838c8
		SETVALUE(TabEnt->Param);					\
Packit 7838c8
		goto doneBlack1d;					\
Packit 7838c8
	    case S_MakeUpB:						\
Packit 7838c8
	    case S_MakeUp:						\
Packit 7838c8
		a0 += TabEnt->Param;					\
Packit 7838c8
		RunLength += TabEnt->Param;				\
Packit 7838c8
		break;							\
Packit 7838c8
	    default:							\
Packit 7838c8
		unexpected("BlackTable", a0);				\
Packit 7838c8
		goto done1d;						\
Packit 7838c8
	    }								\
Packit 7838c8
	}								\
Packit 7838c8
    doneBlack1d:							\
Packit 7838c8
	if (a0 >= lastx)						\
Packit 7838c8
	    goto done1d;						\
Packit 7838c8
        if( *(pa-1) == 0 && *(pa-2) == 0 )				\
Packit 7838c8
            pa -= 2;                                                    \
Packit 7838c8
    }									\
Packit 7838c8
eof1d:									\
Packit 7838c8
    prematureEOF(a0);							\
Packit 7838c8
    CLEANUP_RUNS();							\
Packit 7838c8
    goto eoflab;							\
Packit 7838c8
done1d:									\
Packit 7838c8
    CLEANUP_RUNS();							\
Packit 7838c8
} while (0)
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Update the value of b1 using the array
Packit 7838c8
 * of runs for the reference line.
Packit 7838c8
 */
Packit 7838c8
#define CHECK_b1 do {							\
Packit 7838c8
    if (pa != thisrun) while (b1 <= a0 && b1 < lastx) {			\
Packit 7838c8
	b1 += pb[0] + pb[1];						\
Packit 7838c8
	pb += 2;							\
Packit 7838c8
    }									\
Packit 7838c8
} while (0)
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Expand a row of 2D-encoded data.
Packit 7838c8
 */
Packit 7838c8
#define EXPAND2D(eoflab) do {						\
Packit 7838c8
    while (a0 < lastx) {						\
Packit 7838c8
	LOOKUP8(7, TIFFFaxMainTable, eof2d);				\
Packit 7838c8
	switch (TabEnt->State) {					\
Packit 7838c8
	case S_Pass:							\
Packit 7838c8
	    CHECK_b1;							\
Packit 7838c8
	    b1 += *pb++;						\
Packit 7838c8
	    RunLength += b1 - a0;					\
Packit 7838c8
	    a0 = b1;							\
Packit 7838c8
	    b1 += *pb++;						\
Packit 7838c8
	    break;							\
Packit 7838c8
	case S_Horiz:							\
Packit 7838c8
	    if ((pa-thisrun)&1) {					\
Packit 7838c8
		for (;;) {	/* black first */			\
Packit 7838c8
		    LOOKUP16(13, TIFFFaxBlackTable, eof2d);		\
Packit 7838c8
		    switch (TabEnt->State) {				\
Packit 7838c8
		    case S_TermB:					\
Packit 7838c8
			SETVALUE(TabEnt->Param);				\
Packit 7838c8
			goto doneWhite2da;				\
Packit 7838c8
		    case S_MakeUpB:					\
Packit 7838c8
		    case S_MakeUp:					\
Packit 7838c8
			a0 += TabEnt->Param;				\
Packit 7838c8
			RunLength += TabEnt->Param;			\
Packit 7838c8
			break;						\
Packit 7838c8
		    default:						\
Packit 7838c8
			goto badBlack2d;				\
Packit 7838c8
		    }							\
Packit 7838c8
		}							\
Packit 7838c8
	    doneWhite2da:;						\
Packit 7838c8
		for (;;) {	/* then white */			\
Packit 7838c8
		    LOOKUP16(12, TIFFFaxWhiteTable, eof2d);		\
Packit 7838c8
		    switch (TabEnt->State) {				\
Packit 7838c8
		    case S_TermW:					\
Packit 7838c8
			SETVALUE(TabEnt->Param);				\
Packit 7838c8
			goto doneBlack2da;				\
Packit 7838c8
		    case S_MakeUpW:					\
Packit 7838c8
		    case S_MakeUp:					\
Packit 7838c8
			a0 += TabEnt->Param;				\
Packit 7838c8
			RunLength += TabEnt->Param;			\
Packit 7838c8
			break;						\
Packit 7838c8
		    default:						\
Packit 7838c8
			goto badWhite2d;				\
Packit 7838c8
		    }							\
Packit 7838c8
		}							\
Packit 7838c8
	    doneBlack2da:;						\
Packit 7838c8
	    } else {							\
Packit 7838c8
		for (;;) {	/* white first */			\
Packit 7838c8
		    LOOKUP16(12, TIFFFaxWhiteTable, eof2d);		\
Packit 7838c8
		    switch (TabEnt->State) {				\
Packit 7838c8
		    case S_TermW:					\
Packit 7838c8
			SETVALUE(TabEnt->Param);				\
Packit 7838c8
			goto doneWhite2db;				\
Packit 7838c8
		    case S_MakeUpW:					\
Packit 7838c8
		    case S_MakeUp:					\
Packit 7838c8
			a0 += TabEnt->Param;				\
Packit 7838c8
			RunLength += TabEnt->Param;			\
Packit 7838c8
			break;						\
Packit 7838c8
		    default:						\
Packit 7838c8
			goto badWhite2d;				\
Packit 7838c8
		    }							\
Packit 7838c8
		}							\
Packit 7838c8
	    doneWhite2db:;						\
Packit 7838c8
		for (;;) {	/* then black */			\
Packit 7838c8
		    LOOKUP16(13, TIFFFaxBlackTable, eof2d);		\
Packit 7838c8
		    switch (TabEnt->State) {				\
Packit 7838c8
		    case S_TermB:					\
Packit 7838c8
			SETVALUE(TabEnt->Param);				\
Packit 7838c8
			goto doneBlack2db;				\
Packit 7838c8
		    case S_MakeUpB:					\
Packit 7838c8
		    case S_MakeUp:					\
Packit 7838c8
			a0 += TabEnt->Param;				\
Packit 7838c8
			RunLength += TabEnt->Param;			\
Packit 7838c8
			break;						\
Packit 7838c8
		    default:						\
Packit 7838c8
			goto badBlack2d;				\
Packit 7838c8
		    }							\
Packit 7838c8
		}							\
Packit 7838c8
	    doneBlack2db:;						\
Packit 7838c8
	    }								\
Packit 7838c8
	    CHECK_b1;							\
Packit 7838c8
	    break;							\
Packit 7838c8
	case S_V0:							\
Packit 7838c8
	    CHECK_b1;							\
Packit 7838c8
	    SETVALUE(b1 - a0);						\
Packit 7838c8
	    b1 += *pb++;						\
Packit 7838c8
	    break;							\
Packit 7838c8
	case S_VR:							\
Packit 7838c8
	    CHECK_b1;							\
Packit 7838c8
	    SETVALUE(b1 - a0 + TabEnt->Param);				\
Packit 7838c8
	    b1 += *pb++;						\
Packit 7838c8
	    break;							\
Packit 7838c8
	case S_VL:							\
Packit 7838c8
	    CHECK_b1;							\
Packit 7838c8
	    if (b1 <= (int) (a0 + TabEnt->Param)) {			\
Packit 7838c8
		if (b1 < (int) (a0 + TabEnt->Param) || pa != thisrun) {	\
Packit 7838c8
		    unexpected("VL", a0);				\
Packit 7838c8
		    goto eol2d;						\
Packit 7838c8
		}							\
Packit 7838c8
	    }								\
Packit 7838c8
	    SETVALUE(b1 - a0 - TabEnt->Param);				\
Packit 7838c8
	    b1 -= *--pb;						\
Packit 7838c8
	    break;							\
Packit 7838c8
	case S_Ext:							\
Packit 7838c8
	    *pa++ = lastx - a0;						\
Packit 7838c8
	    extension(a0);						\
Packit 7838c8
	    goto eol2d;							\
Packit 7838c8
	case S_EOL:							\
Packit 7838c8
	    *pa++ = lastx - a0;						\
Packit 7838c8
	    NeedBits8(4,eof2d);						\
Packit 7838c8
	    if (GetBits(4))						\
Packit 7838c8
		unexpected("EOL", a0);					\
Packit 7838c8
            ClrBits(4);                                                 \
Packit 7838c8
	    EOLcnt = 1;							\
Packit 7838c8
	    goto eol2d;							\
Packit 7838c8
	default:							\
Packit 7838c8
	badMain2d:							\
Packit 7838c8
	    unexpected("MainTable", a0);				\
Packit 7838c8
	    goto eol2d;							\
Packit 7838c8
	badBlack2d:							\
Packit 7838c8
	    unexpected("BlackTable", a0);				\
Packit 7838c8
	    goto eol2d;							\
Packit 7838c8
	badWhite2d:							\
Packit 7838c8
	    unexpected("WhiteTable", a0);				\
Packit 7838c8
	    goto eol2d;							\
Packit 7838c8
	eof2d:								\
Packit 7838c8
	    prematureEOF(a0);						\
Packit 7838c8
	    CLEANUP_RUNS();						\
Packit 7838c8
	    goto eoflab;						\
Packit 7838c8
	}								\
Packit 7838c8
    }									\
Packit 7838c8
    if (RunLength) {							\
Packit 7838c8
	if (RunLength + a0 < lastx) {					\
Packit 7838c8
	    /* expect a final V0 */					\
Packit 7838c8
	    NeedBits8(1,eof2d);						\
Packit 7838c8
	    if (!GetBits(1))						\
Packit 7838c8
		goto badMain2d;						\
Packit 7838c8
	    ClrBits(1);							\
Packit 7838c8
	}								\
Packit 7838c8
	SETVALUE(0);							\
Packit 7838c8
    }									\
Packit 7838c8
eol2d:									\
Packit 7838c8
    CLEANUP_RUNS();							\
Packit 7838c8
} while (0)
Packit 7838c8
#endif /* _FAX3_ */
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
 */