1 /* $Id: tif_fax3.h,v 1.13 2016-12-14 18:36:27 faxguy Exp $ */
4 * Copyright (c) 1990-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
32 * CCITT Group 3 (T.4) and Group 4 (T.6) Decompression Support.
34 * Decoder support is derived, with permission, from the code
35 * in Frank Cringle's viewfax program;
36 * Copyright (C) 1990, 1995 Frank D. Cringle.
41 * To override the default routine used to image decoded
42 * spans one can use the pseudo tag TIFFTAG_FAXFILLFUNC.
43 * The routine must have the type signature given below;
46 * fillruns(unsigned char* buf, uint32* runs, uint32* erun, uint32 lastx)
48 * where buf is place to set the bits, runs is the array of b&w run
49 * lengths (white then black), erun is the last run in the array, and
50 * lastx is the width of the row in pixels. Fill routines can assume
51 * the run array has room for at least lastx runs and can overwrite
52 * data in the run array as needed (e.g. to append zero runs to bring
53 * the count up to a nice multiple).
55 typedef void (*TIFFFaxFillFunc
)(unsigned char*, uint32
*, uint32
*, uint32
);
58 * The default run filler; made external for other decoders.
60 #if defined(__cplusplus)
63 extern void _TIFFFax3fillruns(unsigned char*, uint32
*, uint32
*, uint32
);
64 #if defined(__cplusplus)
69 /* finite state machine codes */
84 /* WARNING: do not change the layout of this structure as the HylaFAX software */
85 /* really depends on it. See http://bugzilla.maptools.org/show_bug.cgi?id=2636 */
86 typedef struct { /* state table entry */
87 unsigned char State
; /* see above */
88 unsigned char Width
; /* width of code in bits */
89 uint32 Param
; /* unsigned 32-bit run length in bits (holds on 16 bit actually, but cannot be changed. See above warning) */
92 extern const TIFFFaxTabEnt TIFFFaxMainTable
[];
93 extern const TIFFFaxTabEnt TIFFFaxWhiteTable
[];
94 extern const TIFFFaxTabEnt TIFFFaxBlackTable
[];
97 * The following macros define the majority of the G3/G4 decoder
98 * algorithm using the state tables defined elsewhere. To build
99 * a decoder you need some setup code and some glue code. Note
100 * that you may also need/want to change the way the NeedBits*
101 * macros get input data if, for example, you know the data to be
102 * decoded is properly aligned and oriented (doing so before running
103 * the decoder can be a big performance win).
105 * Consult the decoder in the TIFF library for an idea of what you
106 * need to define and setup to make use of these definitions.
108 * NB: to enable a debugging version of these macros define FAX3_DEBUG
109 * before including this file. Trace output goes to stdout.
113 #define EndOfData() (cp >= ep)
116 * Need <=8 or <=16 bits of input data. Unlike viewfax we
117 * cannot use/assume a word-aligned, properly bit swizzled
118 * input data set because data may come from an arbitrarily
119 * aligned, read-only source such as a memory-mapped file.
120 * Note also that the viewfax decoder does not check for
121 * running off the end of the input data buffer. This is
122 * possible for G3-encoded data because it prescans the input
123 * data to count EOL markers, but can cause problems for G4
124 * data. In any event, we don't prescan and must watch for
125 * running out of data since we can't permit the library to
126 * scan past the end of the input data buffer.
128 * Finally, note that we must handle remaindered data at the end
129 * of a strip specially. The coder asks for a fixed number of
130 * bits when scanning for the next code. This may be more bits
131 * than are actually present in the data stream. If we appear
132 * to run out of data but still have some number of valid bits
133 * remaining then we makeup the requested amount with zeros and
134 * return successfully. If the returned data is incorrect then
135 * we should be called again and get a premature EOF error;
136 * otherwise we should get the right answer.
139 #define NeedBits8(n,eoflab) do { \
140 if (BitsAvail < (n)) { \
142 if (BitsAvail == 0) /* no valid bits */ \
144 BitsAvail = (n); /* pad with zeros */ \
146 BitAcc |= ((uint32) bitmap[*cp++])<<BitsAvail; \
153 #define NeedBits16(n,eoflab) do { \
154 if (BitsAvail < (n)) { \
156 if (BitsAvail == 0) /* no valid bits */ \
158 BitsAvail = (n); /* pad with zeros */ \
160 BitAcc |= ((uint32) bitmap[*cp++])<<BitsAvail; \
161 if ((BitsAvail += 8) < (n)) { \
163 /* NB: we know BitsAvail is non-zero here */ \
164 BitsAvail = (n); /* pad with zeros */ \
166 BitAcc |= ((uint32) bitmap[*cp++])<<BitsAvail; \
174 #define GetBits(n) (BitAcc & ((1<<(n))-1))
175 #define ClrBits(n) do { \
181 static const char* StateNames
[] = {
196 #define DEBUG_SHOW putchar(BitAcc & (1 << t) ? '1' : '0')
197 #define LOOKUP8(wid,tab,eoflab) do { \
199 NeedBits8(wid,eoflab); \
200 TabEnt = tab + GetBits(wid); \
201 printf("%08lX/%d: %s%5d\t", (long) BitAcc, BitsAvail, \
202 StateNames[TabEnt->State], TabEnt->Param); \
203 for (t = 0; t < TabEnt->Width; t++) \
207 ClrBits(TabEnt->Width); \
209 #define LOOKUP16(wid,tab,eoflab) do { \
211 NeedBits16(wid,eoflab); \
212 TabEnt = tab + GetBits(wid); \
213 printf("%08lX/%d: %s%5d\t", (long) BitAcc, BitsAvail, \
214 StateNames[TabEnt->State], TabEnt->Param); \
215 for (t = 0; t < TabEnt->Width; t++) \
219 ClrBits(TabEnt->Width); \
222 #define SETVALUE(x) do { \
223 *pa++ = RunLength + (x); \
224 printf("SETVALUE: %d\t%d\n", RunLength + (x), a0); \
229 #define LOOKUP8(wid,tab,eoflab) do { \
230 NeedBits8(wid,eoflab); \
231 TabEnt = tab + GetBits(wid); \
232 ClrBits(TabEnt->Width); \
234 #define LOOKUP16(wid,tab,eoflab) do { \
235 NeedBits16(wid,eoflab); \
236 TabEnt = tab + GetBits(wid); \
237 ClrBits(TabEnt->Width); \
241 * Append a run to the run length array for the
242 * current row and reset decoding state.
244 #define SETVALUE(x) do { \
245 *pa++ = RunLength + (x); \
252 * Synchronize input decoding at the start of each
253 * row by scanning for an EOL (if appropriate) and
254 * skipping any trash data that might be present
255 * after a decoding error. Note that the decoding
256 * done elsewhere that recognizes an EOL only consumes
257 * 11 consecutive zero bits. This means that if EOLcnt
258 * is non-zero then we still need to scan for the final flag
259 * bit that is part of the EOL code.
261 #define SYNC_EOL(eoflab) do { \
264 NeedBits16(11,eoflab); \
265 if (GetBits(11) == 0) \
271 NeedBits8(8,eoflab); \
276 while (GetBits(1) == 0) \
278 ClrBits(1); /* EOL bit */ \
279 EOLcnt = 0; /* reset EOL counter/flag */ \
283 * Cleanup the array of runs after decoding a row.
284 * We adjust final runs to insure the user buffer is not
285 * overwritten and/or undecoded area is white filled.
287 #define CLEANUP_RUNS() do { \
291 badlength(a0, lastx); \
292 while (a0 > lastx && pa > thisrun) \
297 if ((pa-thisrun)&1) \
299 SETVALUE(lastx - a0); \
300 } else if (a0 > lastx) { \
308 * Decode a line of 1D-encoded data.
310 * The line expanders are written as macros so that they can be reused
311 * but still have direct access to the local variables of the "calling"
314 * Note that unlike the original version we have to explicitly test for
315 * a0 >= lastx after each black/white run is decoded. This is because
316 * the original code depended on the input data being zero-padded to
317 * insure the decoder recognized an EOL before running out of data.
319 #define EXPAND1D(eoflab) do { \
322 LOOKUP16(12, TIFFFaxWhiteTable, eof1d); \
323 switch (TabEnt->State) { \
328 SETVALUE(TabEnt->Param); \
332 a0 += TabEnt->Param; \
333 RunLength += TabEnt->Param; \
336 unexpected("WhiteTable", a0); \
344 LOOKUP16(13, TIFFFaxBlackTable, eof1d); \
345 switch (TabEnt->State) { \
350 SETVALUE(TabEnt->Param); \
354 a0 += TabEnt->Param; \
355 RunLength += TabEnt->Param; \
358 unexpected("BlackTable", a0); \
365 if( *(pa-1) == 0 && *(pa-2) == 0 ) \
377 * Update the value of b1 using the array
378 * of runs for the reference line.
380 #define CHECK_b1 do { \
381 if (pa != thisrun) while (b1 <= a0 && b1 < lastx) { \
382 b1 += pb[0] + pb[1]; \
388 * Expand a row of 2D-encoded data.
390 #define EXPAND2D(eoflab) do { \
391 while (a0 < lastx) { \
392 LOOKUP8(7, TIFFFaxMainTable, eof2d); \
393 switch (TabEnt->State) { \
397 RunLength += b1 - a0; \
402 if ((pa-thisrun)&1) { \
403 for (;;) { /* black first */ \
404 LOOKUP16(13, TIFFFaxBlackTable, eof2d); \
405 switch (TabEnt->State) { \
407 SETVALUE(TabEnt->Param); \
411 a0 += TabEnt->Param; \
412 RunLength += TabEnt->Param; \
419 for (;;) { /* then white */ \
420 LOOKUP16(12, TIFFFaxWhiteTable, eof2d); \
421 switch (TabEnt->State) { \
423 SETVALUE(TabEnt->Param); \
427 a0 += TabEnt->Param; \
428 RunLength += TabEnt->Param; \
436 for (;;) { /* white first */ \
437 LOOKUP16(12, TIFFFaxWhiteTable, eof2d); \
438 switch (TabEnt->State) { \
440 SETVALUE(TabEnt->Param); \
444 a0 += TabEnt->Param; \
445 RunLength += TabEnt->Param; \
452 for (;;) { /* then black */ \
453 LOOKUP16(13, TIFFFaxBlackTable, eof2d); \
454 switch (TabEnt->State) { \
456 SETVALUE(TabEnt->Param); \
460 a0 += TabEnt->Param; \
461 RunLength += TabEnt->Param; \
478 SETVALUE(b1 - a0 + TabEnt->Param); \
483 if (b1 <= (int) (a0 + TabEnt->Param)) { \
484 if (b1 < (int) (a0 + TabEnt->Param) || pa != thisrun) { \
485 unexpected("VL", a0); \
489 SETVALUE(b1 - a0 - TabEnt->Param); \
493 *pa++ = lastx - a0; \
497 *pa++ = lastx - a0; \
498 NeedBits8(4,eof2d); \
500 unexpected("EOL", a0); \
506 unexpected("MainTable", a0); \
509 unexpected("BlackTable", a0); \
512 unexpected("WhiteTable", a0); \
521 if (RunLength + a0 < lastx) { \
522 /* expect a final V0 */ \
523 NeedBits8(1,eof2d); \