diff options
Diffstat (limited to 'nx-X11/lib/lbxutil/image')
-rw-r--r-- | nx-X11/lib/lbxutil/image/Imakefile | 45 | ||||
-rw-r--r-- | nx-X11/lib/lbxutil/image/dfaxg42d.c | 420 | ||||
-rw-r--r-- | nx-X11/lib/lbxutil/image/dpackbits.c | 100 | ||||
-rw-r--r-- | nx-X11/lib/lbxutil/image/efaxg42d.c | 328 | ||||
-rw-r--r-- | nx-X11/lib/lbxutil/image/epackbits.c | 202 | ||||
-rw-r--r-- | nx-X11/lib/lbxutil/image/lbxbwcodes.h | 269 | ||||
-rw-r--r-- | nx-X11/lib/lbxutil/image/lbxfax.h | 60 | ||||
-rw-r--r-- | nx-X11/lib/lbxutil/image/misc.c | 213 | ||||
-rw-r--r-- | nx-X11/lib/lbxutil/image/mkg3states.c | 761 |
9 files changed, 2398 insertions, 0 deletions
diff --git a/nx-X11/lib/lbxutil/image/Imakefile b/nx-X11/lib/lbxutil/image/Imakefile new file mode 100644 index 000000000..fa7421615 --- /dev/null +++ b/nx-X11/lib/lbxutil/image/Imakefile @@ -0,0 +1,45 @@ +XCOMM $Xorg: Imakefile,v 1.3 2000/08/17 19:46:40 cpqbld Exp $ + + + + +XCOMM $XFree86: xc/lib/lbxutil/image/Imakefile,v 1.3 2001/01/17 19:43:35 dawes Exp $ + +#define IncSubdir X11 +#include <Library.tmpl> + +SRCS = dfaxg42d.c dpackbits.c efaxg42d.c epackbits.c misc.c + +OBJS = dfaxg42d.o dpackbits.o efaxg42d.o epackbits.o misc.o + +INCLUDES = -I$(XINCLUDESRC) -I$(EXTINCSRC) -I$(SERVERSRC)/include -I../../../include/extensions + +SubdirLibraryRule($(OBJS)) +NormalLibraryObjectRule() +NormalLintTarget($(SRCS)) + +MKG3STATES = ./mkg3states + +#if CrossCompiling +HOST_CFLAGS=$(TOP_INCLUDES) $(INCLUDES) $(BOOTSTRAPCFLAGS) \ + $(CROSSCOMPILEDEFINES) $(CROSSCOMPILEBOOTSTRAPDEFINES) +#else +HOST_CFLAGS=$(CFLAGS) +#endif + +g3states.h : mkg3states.c + RemoveFiles($@ HostProgramTargetName(mkg3states)) + -HostLinkRule(HostProgramTargetName(mkg3states), $(HOST_CFLAGS),mkg3states.c,$(LDLIBS)) + RunProgram(MKG3STATES,> g3_states_h) + $(MV) g3_states_h $@ + RemoveFiles(HostProgramTargetName(mkg3states) g3_states_h) + +includes:: g3states.h + +depend:: g3states.h + +clean:: + RemoveFiles(g3_states_h g3states.h HostProgramTargetName(mkg3states)) + +DependTarget() + diff --git a/nx-X11/lib/lbxutil/image/dfaxg42d.c b/nx-X11/lib/lbxutil/image/dfaxg42d.c new file mode 100644 index 000000000..609992d62 --- /dev/null +++ b/nx-X11/lib/lbxutil/image/dfaxg42d.c @@ -0,0 +1,420 @@ +/* $Xorg: dfaxg42d.c,v 1.3 2000/08/17 19:46:40 cpqbld Exp $ */ +/* + * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler + * Copyright (c) 1991, 1992 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ +/* $XFree86: xc/lib/lbxutil/image/dfaxg42d.c,v 1.5 2001/01/17 19:43:35 dawes Exp $ */ + +#include <X11/Xos.h> +#include <X11/Xfuncproto.h> +#include <X11/Xfuncs.h> +#include <stdlib.h> +#include "g3states.h" +#include "lbxfax.h" +#include <X11/extensions/lbximage.h> + +/* + * ------------------------------------------------------------------------- + * FAX G42D decoding for 1 bit images + * ------------------------------------------------------------------------- + */ + +static short sp_data, sp_bit; + + +/* + * Fetch a byte from the input stream + */ + +static unsigned char +fetchByte (unsigned char **inbuf) + +{ + unsigned char byte = **inbuf; + (*inbuf)++; + return (byte); +} + + +/* + * Decode a run of white. + */ + +static int +decode_white_run (unsigned char **inbuf) + +{ + short state = sp_bit; + short action; + int runlen = 0; + + for (;;) + { + if (sp_bit == 0) + { + nextbyte: + sp_data = fetchByte (inbuf); + } + + action = TIFFFax1DAction[state][sp_data]; + state = TIFFFax1DNextState[state][sp_data]; + if (action == ACT_INCOMP) + goto nextbyte; + if (action == ACT_INVALID) + return (G3CODE_INVALID); + if (action == ACT_EOL) + return (G3CODE_EOL); + sp_bit = state; + action = RUNLENGTH(action - ACT_WRUNT); + runlen += action; + if (action < 64) + return (runlen); + } +} + + +/* + * Decode a run of black. + */ + +static int +decode_black_run (unsigned char **inbuf) + +{ + short state = sp_bit + 8; + short action; + int runlen = 0; + + for (;;) + { + if (sp_bit == 0) + { + nextbyte: + sp_data = fetchByte (inbuf); + } + + action = TIFFFax1DAction[state][sp_data]; + state = TIFFFax1DNextState[state][sp_data]; + if (action == ACT_INCOMP) + goto nextbyte; + if (action == ACT_INVALID) + return (G3CODE_INVALID); + if (action == ACT_EOL) + return (G3CODE_EOL); + sp_bit = state; + action = RUNLENGTH(action - ACT_BRUNT); + runlen += action; + if (action < 64) + return (runlen); + state += 8; + } +} + + +/* + * Return the next uncompressed mode code word. + */ + +static int +decode_uncomp_code (unsigned char **inbuf) + +{ + short code; + + do { + if (sp_bit == 0 || sp_bit > 7) + sp_data = fetchByte (inbuf); + + code = TIFFFaxUncompAction[sp_bit][sp_data]; + sp_bit = TIFFFaxUncompNextState[sp_bit][sp_data]; + } while (code == ACT_INCOMP); + + return (code); +} + + +/* + * Fill a span with ones. + */ + +static void +fillspan (char *cp, + int x, int count) + +{ + static unsigned char masks[] = + { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; + + if (count <= 0) + return; + + cp += x>>3; + + if (x &= 7) + { + /* align to byte boundary */ + + if (count < 8 - x) { + *cp++ |= masks[count] >> x; + return; + } + + *cp++ |= 0xff >> x; + count -= 8 - x; + } + + while (count >= 8) + { + *cp++ = (char)0xff; + count -= 8; + } + + *cp |= masks[count]; +} + + +/* + * Return the next bit in the input stream. This is + * used to extract 2D tag values and the color tag + * at the end of a terminating uncompressed data code. + */ + +static int +nextbit (unsigned char **inbuf) + +{ + static unsigned char bitMask[8] = + { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; + int bit; + + if (sp_bit == 0) + sp_data = fetchByte (inbuf); + + bit = sp_data & bitMask[sp_bit]; + + if (++(sp_bit) > 7) + sp_bit = 0; + + return (bit); +} + + +static int +DecodeFaxG42D (unsigned char **inbuf, + unsigned char *refline, + int pixels_per_line, + unsigned char *outbuf) + +{ + int a0 = -1; + int b1, b2; + int run1, run2; /* for horizontal mode */ + short mode; + short color = 1; + + do { + if (sp_bit == 0 || sp_bit > 7) + sp_data = fetchByte (inbuf); + + mode = TIFFFax2DMode[sp_bit][sp_data]; + sp_bit = TIFFFax2DNextState[sp_bit][sp_data]; + + switch (mode) + { + case MODE_NULL: + break; + + case MODE_PASS: + b2 = LbxImageFindDiff (refline, a0, pixels_per_line, !color); + b1 = LbxImageFindDiff (refline, b2, pixels_per_line, color); + b2 = LbxImageFindDiff (refline, b1, pixels_per_line, !color); + + if (color) + { + if (a0 < 0) + a0 = 0; + fillspan ((char *) outbuf, a0, b2 - a0); + } + + a0 = b2; + break; + + case MODE_HORIZ: + if (color == 1) + { + run1 = decode_white_run (inbuf); + run2 = decode_black_run (inbuf); + } + else + { + run1 = decode_black_run (inbuf); + run2 = decode_white_run (inbuf); + } + + /* + * Do the appropriate fill. Note that we exit this logic with + * the same color that we enter with since we do 2 fills. This + * explains the somewhat obscure logic below. + */ + + if (a0 < 0) + a0 = 0; + if (a0 + run1 > pixels_per_line) + run1 = pixels_per_line - a0; + if (color) + fillspan ((char *) outbuf, a0, run1); + a0 += run1; + if (a0 + run2 > pixels_per_line) + run2 = pixels_per_line - a0; + if (!color) + fillspan ((char *) outbuf, a0, run2); + a0 += run2; + break; + + case MODE_VERT_V0: + case MODE_VERT_VR1: + case MODE_VERT_VR2: + case MODE_VERT_VR3: + case MODE_VERT_VL1: + case MODE_VERT_VL2: + case MODE_VERT_VL3: + b2 = LbxImageFindDiff (refline, a0, pixels_per_line, !color); + b1 = LbxImageFindDiff (refline, b2, pixels_per_line, color); + b1 += mode - MODE_VERT_V0; + + if (color) + { + if (a0 < 0) + a0 = 0; + fillspan ((char *) outbuf, a0, b1 - a0); + } + + color = !color; + a0 = b1; + break; + + case MODE_UNCOMP: + /* + * Uncompressed mode: select from the special set of code words. + */ + + if (a0 < 0) + a0 = 0; + do + { + mode = decode_uncomp_code (inbuf); + switch (mode) + { + case UNCOMP_RUN1: + case UNCOMP_RUN2: + case UNCOMP_RUN3: + case UNCOMP_RUN4: + case UNCOMP_RUN5: + run1 = mode - UNCOMP_RUN0; + fillspan ((char *) outbuf, a0+run1-1, 1); + a0 += run1; + break; + + case UNCOMP_RUN6: + a0 += 5; + break; + + case UNCOMP_TRUN0: + case UNCOMP_TRUN1: + case UNCOMP_TRUN2: + case UNCOMP_TRUN3: + case UNCOMP_TRUN4: + + run1 = mode - UNCOMP_TRUN0; + a0 += run1; + color = nextbit (inbuf) ? 0 : 1; + break; + + case UNCOMP_INVALID: + goto bad; + + case UNCOMP_EOF: + return (0); + } + } while (mode < UNCOMP_EXIT); + break; + + case MODE_ERROR_1: + /* fall thru... */ + case MODE_ERROR: + goto bad; + + default: + return (0); + } + + } while (a0 < pixels_per_line); + +bad: + return (a0 >= pixels_per_line); +} + + +int +LbxImageDecodeFaxG42D (unsigned char *inbuf, + unsigned char *outbuf, + int image_bytes, + int pixels_per_line, + int padded_bytes_per_scanline, + int reverse_bits) + +{ + int bytes_per_scanline = ROUNDUP8 (pixels_per_line); + unsigned char *refline, *refptr; + unsigned char *outbuf_start = outbuf; + int bytes_left = image_bytes; + int i; + + refline = (unsigned char *) malloc (bytes_per_scanline + 1); + refptr = refline + 1; + + for (i = 0; i < bytes_per_scanline + 1; i++) + refline[i] = 0xff; + + bzero (outbuf, image_bytes); + + sp_bit = 0; + sp_data = 0; + + while (bytes_left > 0) + { + if (!DecodeFaxG42D (&inbuf, refptr, pixels_per_line, outbuf)) + return (0); + + memcpy (refptr, outbuf, bytes_per_scanline); + + outbuf += padded_bytes_per_scanline; + bytes_left -= padded_bytes_per_scanline; + } + + free ((char *) refline); + + if (reverse_bits) + LbxReverseBits (outbuf_start, image_bytes); + + return (outbuf - outbuf_start); +} diff --git a/nx-X11/lib/lbxutil/image/dpackbits.c b/nx-X11/lib/lbxutil/image/dpackbits.c new file mode 100644 index 000000000..f0f2058d0 --- /dev/null +++ b/nx-X11/lib/lbxutil/image/dpackbits.c @@ -0,0 +1,100 @@ +/* $Xorg: dpackbits.c,v 1.4 2000/08/17 19:46:40 cpqbld Exp $ */ +/* + * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler + * Copyright (c) 1991, 1992 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ +/* $XFree86: xc/lib/lbxutil/image/dpackbits.c,v 1.3 2000/05/18 23:46:16 dawes Exp $ */ + +#include <X11/Xos.h> +#include <X11/Xfuncproto.h> +#include <X11/extensions/lbximage.h> + +/* + * ------------------------------------------------------------------------- + * PackBits decoding for 8 bit color images + * ------------------------------------------------------------------------- + */ + +static void +DecodePackBits (char **inbuf, + int numPixels, + char *outbuf) + +{ + register char *inptr = *inbuf; + register int n, b; + + while (numPixels > 0) + { + n = (int) *inptr++; + + /* Watch out for compilers that don't sign extend chars... */ + + if (n >= 128) + n -= 256; + + if (n < 0) + { + /* replicate next byte -n+1 times */ + + if (n == -128) /* nop */ + continue; + + n = -n + 1; + numPixels -= n; + for (b = *inptr++; n-- > 0;) + *outbuf++ = b; + } + else + { + /* copy next n+1 bytes literally */ + + memcpy (outbuf, inptr, ++n); + outbuf += n; numPixels -= n; + inptr += n; + } + } + + *inbuf = inptr; +} + + +int +LbxImageDecodePackBits (char *inbuf, + char *outbuf, + int num_scan_lines, + int scan_line_size) + +{ + char *outbuf_start = outbuf; + int padded_scan_line_size = (scan_line_size % 4) ? + scan_line_size + (4 - scan_line_size % 4) : scan_line_size; + + while (num_scan_lines > 0) + { + DecodePackBits (&inbuf, scan_line_size, outbuf); + outbuf += padded_scan_line_size; + num_scan_lines--; + } + + return (outbuf - outbuf_start); +} diff --git a/nx-X11/lib/lbxutil/image/efaxg42d.c b/nx-X11/lib/lbxutil/image/efaxg42d.c new file mode 100644 index 000000000..a89b5e0d2 --- /dev/null +++ b/nx-X11/lib/lbxutil/image/efaxg42d.c @@ -0,0 +1,328 @@ +/* $Xorg: efaxg42d.c,v 1.3 2000/08/17 19:46:41 cpqbld Exp $ */ +/* + * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler + * Copyright (c) 1991, 1992 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ +/* $XFree86: xc/lib/lbxutil/image/efaxg42d.c,v 1.4 2001/01/17 19:43:35 dawes Exp $ */ + +#include <X11/Xos.h> +#include <X11/Xfuncproto.h> +#include <stdlib.h> +#include "lbxfax.h" +#include <X11/extensions/lbximage.h> +#include "lbxbwcodes.h" + +/* + * ------------------------------------------------------------------------- + * FAX G42D encoding for 1 bit images + * ------------------------------------------------------------------------- + */ + +static short sp_data, sp_bit; + +static tableentry horizcode = + { 3, 0x1 }; /* 001 */ + +static tableentry passcode = + { 4, 0x1 }; /* 0001 */ + +static tableentry vcodes[7] = { + { 7, 0x03 }, /* 0000 011 */ + { 6, 0x03 }, /* 0000 11 */ + { 3, 0x03 }, /* 011 */ + { 1, 0x1 }, /* 1 */ + { 3, 0x2 }, /* 010 */ + { 6, 0x02 }, /* 0000 10 */ + { 7, 0x02 } /* 0000 010 */ +}; + +typedef struct { + unsigned char *bufStart; + unsigned char *bufPtr; + int bufSize; + int bytesLeft; +} Buffer; + + + +/* + * Flush bits to output buffer. + */ + +static int +flushbits (Buffer *outbuf) + +{ + if (outbuf->bytesLeft > 0) + { + *(outbuf->bufPtr++) = sp_data; + outbuf->bytesLeft--; + + sp_data = 0; + sp_bit = 8; + + return (1); + } + else + return (0); +} + + +/* + * Write a variable-length bit-value to the output stream. Values are + * assumed to be at most 16 bits. + */ + +static int +putbits (unsigned int bits, + unsigned int length, + Buffer *outbuf) + +{ + static int mask[9] = + {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}; + + while (length > sp_bit) + { + sp_data |= bits >> (length - sp_bit); + length -= sp_bit; + if (!flushbits (outbuf)) + return (0); + } + + sp_data |= (bits & mask[length]) << (sp_bit - length); + sp_bit -= length; + + if (sp_bit == 0) + { + if (!flushbits (outbuf)) + return (0); + } + + return (1); +} + + +/* + * Write a code to the output stream. + */ + +static int +putcode (tableentry *te, + Buffer *outbuf) + +{ + return (putbits (te->code, te->length, outbuf)); +} + + +/* + * Write the sequence of codes that describes the specified span of + * zero's or one's. The appropriate table that holds the make-up and + * terminating codes is supplied. + */ + +static int +putspan (int span, + tableentry *tab, + Buffer *outbuf) + +{ + while (span >= 2624) + { + tableentry *te = &tab[63 + (2560 >> 6)]; + if (!putcode (te, outbuf)) + return (0); + span -= te->runlen; + } + + if (span >= 64) + { + tableentry *te = &tab[63 + (span >> 6)]; + if (!putcode (te, outbuf)) + return (0); + span -= te->runlen; + } + + if (!putcode (&tab[span], outbuf)) + return (0); + + return (1); +} + + + +#define PIXEL(buf,ix) ((((buf)[(ix)>>3]) >> (7-((ix)&7))) & 1) + +static int +EncodeFaxG42D (unsigned char *inbuf, + unsigned char *refline, + int bits, + Buffer *outbuf) + +{ + short white = 1; + int a0 = 0; + int a1 = (PIXEL (inbuf, 0) != white ? + 0 : LbxImageFindDiff (inbuf, 0, bits, white)); + int b1 = (PIXEL (refline, 0) != white ? + 0 : LbxImageFindDiff (refline, 0, bits, white)); + int a2, b2; + + for (;;) + { + b2 = LbxImageFindDiff (refline, b1, bits, PIXEL (refline, b1)); + if (b2 >= a1) + { + int d = b1 - a1; + if (!(-3 <= d && d <= 3)) + { + /* horizontal mode */ + + a2 = LbxImageFindDiff (inbuf, a1, bits, PIXEL (inbuf, a1)); + if (!putcode (&horizcode, outbuf)) + return (0); + + if (a0 + a1 == 0 || PIXEL (inbuf, a0) == white) + { + if (!putspan(a1 - a0, TIFFFaxWhiteCodes, outbuf)) + return (0); + if (!putspan(a2 - a1, TIFFFaxBlackCodes, outbuf)) + return (0); + } + else + { + if (!putspan (a1 - a0, TIFFFaxBlackCodes, outbuf)) + return (0); + if (!putspan (a2 - a1, TIFFFaxWhiteCodes, outbuf)) + return (0); + } + + a0 = a2; + } + else + { + /* vertical mode */ + + if (!putcode (&vcodes[d+3], outbuf)) + return (0); + a0 = a1; + } + } + else + { + /* pass mode */ + + if (!putcode (&passcode, outbuf)) + return (0); + a0 = b2; + } + + if (a0 >= bits) + break; + + a1 = LbxImageFindDiff (inbuf, a0, bits, PIXEL (inbuf, a0)); + b1 = LbxImageFindDiff (refline, a0, bits, !PIXEL (inbuf, a0)); + b1 = LbxImageFindDiff (refline, b1, bits, PIXEL (inbuf, a0)); + } + + return (1); +} + + +int +LbxImageEncodeFaxG42D (unsigned char *inbuf, + unsigned char *outbuf, + int outbufSize, + int image_bytes, + int pixels_per_line, + int padded_bytes_per_scanline, + int reverse_bits, + int *bytesCompressed) + +{ + int bytes_per_scanline = ROUNDUP8 (pixels_per_line); + unsigned char *refline, *refptr; + unsigned char *save_inbuf = inbuf; + int bytes_left = image_bytes; + Buffer OutBuf; + int status, i; + + OutBuf.bufStart = OutBuf.bufPtr = outbuf; + OutBuf.bufSize = OutBuf.bytesLeft = outbufSize; + + if (!(refline = (unsigned char *) malloc (bytes_per_scanline + 1))) + return (LBX_IMAGE_COMPRESS_BAD_MALLOC); + + refptr = refline + 1; + + for (i = 0; i < bytes_per_scanline + 1; i++) + refline[i] = 0xff; + + if (reverse_bits) + LbxReverseBits (inbuf, image_bytes); + + sp_bit = 8; + sp_data = 0; + + while (bytes_left > 0) + { + if (!(status = EncodeFaxG42D (inbuf, refptr, + pixels_per_line, &OutBuf))) + { + goto bad; + } + + memcpy (refptr, inbuf, bytes_per_scanline); + + inbuf += padded_bytes_per_scanline; + bytes_left -= padded_bytes_per_scanline; + } + + status = putbits (EOL, 12, &OutBuf); + if (status) + status = putbits (EOL, 12, &OutBuf); + if (status && sp_bit != 8) + { + status = flushbits (&OutBuf); + } + + bad: + + free ((char *) refline); + + /* put the bits back the way they were */ + if (reverse_bits) + LbxReverseBits (save_inbuf, image_bytes); + + if (status) + { + *bytesCompressed = OutBuf.bufPtr - OutBuf.bufStart; + + if (OutBuf.bytesLeft > 0) + return (LBX_IMAGE_COMPRESS_SUCCESS); + else + return (LBX_IMAGE_COMPRESS_NOT_WORTH_IT); + } + else + return (LBX_IMAGE_COMPRESS_NOT_WORTH_IT); +} diff --git a/nx-X11/lib/lbxutil/image/epackbits.c b/nx-X11/lib/lbxutil/image/epackbits.c new file mode 100644 index 000000000..816fee57a --- /dev/null +++ b/nx-X11/lib/lbxutil/image/epackbits.c @@ -0,0 +1,202 @@ +/* $Xorg: epackbits.c,v 1.4 2000/08/17 19:46:41 cpqbld Exp $ */ +/* + * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler + * Copyright (c) 1991, 1992 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include <X11/Xfuncproto.h> +#include <X11/extensions/lbximage.h> + +/* + * ------------------------------------------------------------------------- + * PackBits encoding for 8 bit color images + * ------------------------------------------------------------------------- + */ + +#define PutByte(byte,bufptr,bytesLeft) \ +{ \ + if (*bytesLeft < 1) {\ + *outbuf = outptr; \ + return (0); \ + } \ + *bufptr++ = byte; \ + (*bytesLeft)--; \ +} + + +static int +EncodePackBits (char *inbuf, + int numPixels, + char **outbuf, + int *bytesLeft) + +{ + register int pixelsLeft = numPixels; + register char *outptr = *outbuf; + register char *lastliteral; + register int n, b; + enum { BASE, LITERAL, RUN, LITERAL_RUN } state; + + state = BASE; + lastliteral = 0; + + while (pixelsLeft > 0) + { + /* + * Find the longest string of identical bytes. + */ + + b = *inbuf++, pixelsLeft--, n = 1; + for (; pixelsLeft > 0 && b == *inbuf; pixelsLeft--, inbuf++) + n++; + + again: + + switch (state) + { + case BASE: /* initial state, set run/literal */ + if (n > 1) + { + state = RUN; + + if (n > 128) + { + PutByte (-127, outptr, bytesLeft); + PutByte (b, outptr, bytesLeft); + n -= 128; + goto again; + } + + PutByte (-(n-1), outptr, bytesLeft); + PutByte (b, outptr, bytesLeft); + } + else + { + lastliteral = outptr; + PutByte (0, outptr, bytesLeft); + PutByte (b, outptr, bytesLeft); + state = LITERAL; + } + + break; + + case LITERAL: /* last object was literal string */ + if (n > 1) + { + state = LITERAL_RUN; + + if (n > 128) + { + PutByte (-127, outptr, bytesLeft); + PutByte (b, outptr, bytesLeft); + n -= 128; + goto again; + } + + PutByte (-(n-1), outptr, bytesLeft); /* encode run */ + PutByte (b, outptr, bytesLeft); + } + else + { /* extend literal */ + if (++(*lastliteral) == 127) + state = BASE; + PutByte (b, outptr, bytesLeft); + } + + break; + + case RUN: /* last object was run */ + if (n > 1) + { + if (n > 128) + { + PutByte (-127, outptr, bytesLeft); + PutByte (b, outptr, bytesLeft); + n -= 128; + goto again; + } + PutByte (-(n-1), outptr, bytesLeft); + PutByte (b, outptr, bytesLeft); + } + else + { + lastliteral = outptr; + PutByte (0, outptr, bytesLeft); + PutByte (b, outptr, bytesLeft); + state = LITERAL; + } + + break; + + case LITERAL_RUN: /* literal followed by a run */ + /* + * Check to see if previous run should + * be converted to a literal, in which + * case we convert literal-run-literal + * to a single literal. + */ + + if (n == 1 && outptr[-2] == (char)-1 && *lastliteral < 126) + { + state = (((*lastliteral) += 2) == 127 ? BASE : LITERAL); + outptr[-2] = outptr[-1]; /* replicate */ + } + else + state = RUN; + goto again; + } + } + + *outbuf = outptr; + + return (1); +} + + +int +LbxImageEncodePackBits (char *inbuf, + char *outbuf, + int outbufSize, + int format, + int depth, + int num_scan_lines, + int scan_line_size, + int *bytesCompressed) + +{ + char *outbuf_start = outbuf; + int padded_scan_line_size = (scan_line_size % 4) ? + scan_line_size + (4 - scan_line_size % 4) : scan_line_size; + int bytesLeft = outbufSize; + + while (num_scan_lines > 0) + { + if (!EncodePackBits (inbuf, scan_line_size, &outbuf, &bytesLeft)) + return (LBX_IMAGE_COMPRESS_NOT_WORTH_IT); + + inbuf += padded_scan_line_size; + num_scan_lines--; + } + + *bytesCompressed = outbuf - outbuf_start; + return (LBX_IMAGE_COMPRESS_SUCCESS); +} diff --git a/nx-X11/lib/lbxutil/image/lbxbwcodes.h b/nx-X11/lib/lbxutil/image/lbxbwcodes.h new file mode 100644 index 000000000..e26e2864c --- /dev/null +++ b/nx-X11/lib/lbxutil/image/lbxbwcodes.h @@ -0,0 +1,269 @@ +/* $Xorg: lbxbwcodes.h,v 1.3 2000/08/17 19:46:41 cpqbld Exp $ */ + +/* + * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler + * Copyright (c) 1991, 1992 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ +/* $XFree86$ */ + +/* + * Note that these tables are ordered such that the + * index into the table is known to be either the + * run length, or (run length / 64) + a fixed offset. + * + * NB: The G3CODE_INVALID entries are only used + * during state generation (see mkg3states.c). + */ + +#ifdef __DARWIN__ +#ifndef TIFFaxWhiteCodes +#define TIFFFaxWhiteCodes Darwin_X_TIFFFaxWhiteCodes +#endif +#endif +tableentry TIFFFaxWhiteCodes[] = { + { 8, 0x35, 0 }, /* 0011 0101 */ + { 6, 0x7, 1 }, /* 0001 11 */ + { 4, 0x7, 2 }, /* 0111 */ + { 4, 0x8, 3 }, /* 1000 */ + { 4, 0xB, 4 }, /* 1011 */ + { 4, 0xC, 5 }, /* 1100 */ + { 4, 0xE, 6 }, /* 1110 */ + { 4, 0xF, 7 }, /* 1111 */ + { 5, 0x13, 8 }, /* 1001 1 */ + { 5, 0x14, 9 }, /* 1010 0 */ + { 5, 0x7, 10 }, /* 0011 1 */ + { 5, 0x8, 11 }, /* 0100 0 */ + { 6, 0x8, 12 }, /* 0010 00 */ + { 6, 0x3, 13 }, /* 0000 11 */ + { 6, 0x34, 14 }, /* 1101 00 */ + { 6, 0x35, 15 }, /* 1101 01 */ + { 6, 0x2A, 16 }, /* 1010 10 */ + { 6, 0x2B, 17 }, /* 1010 11 */ + { 7, 0x27, 18 }, /* 0100 111 */ + { 7, 0xC, 19 }, /* 0001 100 */ + { 7, 0x8, 20 }, /* 0001 000 */ + { 7, 0x17, 21 }, /* 0010 111 */ + { 7, 0x3, 22 }, /* 0000 011 */ + { 7, 0x4, 23 }, /* 0000 100 */ + { 7, 0x28, 24 }, /* 0101 000 */ + { 7, 0x2B, 25 }, /* 0101 011 */ + { 7, 0x13, 26 }, /* 0010 011 */ + { 7, 0x24, 27 }, /* 0100 100 */ + { 7, 0x18, 28 }, /* 0011 000 */ + { 8, 0x2, 29 }, /* 0000 0010 */ + { 8, 0x3, 30 }, /* 0000 0011 */ + { 8, 0x1A, 31 }, /* 0001 1010 */ + { 8, 0x1B, 32 }, /* 0001 1011 */ + { 8, 0x12, 33 }, /* 0001 0010 */ + { 8, 0x13, 34 }, /* 0001 0011 */ + { 8, 0x14, 35 }, /* 0001 0100 */ + { 8, 0x15, 36 }, /* 0001 0101 */ + { 8, 0x16, 37 }, /* 0001 0110 */ + { 8, 0x17, 38 }, /* 0001 0111 */ + { 8, 0x28, 39 }, /* 0010 1000 */ + { 8, 0x29, 40 }, /* 0010 1001 */ + { 8, 0x2A, 41 }, /* 0010 1010 */ + { 8, 0x2B, 42 }, /* 0010 1011 */ + { 8, 0x2C, 43 }, /* 0010 1100 */ + { 8, 0x2D, 44 }, /* 0010 1101 */ + { 8, 0x4, 45 }, /* 0000 0100 */ + { 8, 0x5, 46 }, /* 0000 0101 */ + { 8, 0xA, 47 }, /* 0000 1010 */ + { 8, 0xB, 48 }, /* 0000 1011 */ + { 8, 0x52, 49 }, /* 0101 0010 */ + { 8, 0x53, 50 }, /* 0101 0011 */ + { 8, 0x54, 51 }, /* 0101 0100 */ + { 8, 0x55, 52 }, /* 0101 0101 */ + { 8, 0x24, 53 }, /* 0010 0100 */ + { 8, 0x25, 54 }, /* 0010 0101 */ + { 8, 0x58, 55 }, /* 0101 1000 */ + { 8, 0x59, 56 }, /* 0101 1001 */ + { 8, 0x5A, 57 }, /* 0101 1010 */ + { 8, 0x5B, 58 }, /* 0101 1011 */ + { 8, 0x4A, 59 }, /* 0100 1010 */ + { 8, 0x4B, 60 }, /* 0100 1011 */ + { 8, 0x32, 61 }, /* 0011 0010 */ + { 8, 0x33, 62 }, /* 0011 0011 */ + { 8, 0x34, 63 }, /* 0011 0100 */ + { 5, 0x1B, 64 }, /* 1101 1 */ + { 5, 0x12, 128 }, /* 1001 0 */ + { 6, 0x17, 192 }, /* 0101 11 */ + { 7, 0x37, 256 }, /* 0110 111 */ + { 8, 0x36, 320 }, /* 0011 0110 */ + { 8, 0x37, 384 }, /* 0011 0111 */ + { 8, 0x64, 448 }, /* 0110 0100 */ + { 8, 0x65, 512 }, /* 0110 0101 */ + { 8, 0x68, 576 }, /* 0110 1000 */ + { 8, 0x67, 640 }, /* 0110 0111 */ + { 9, 0xCC, 704 }, /* 0110 0110 0 */ + { 9, 0xCD, 768 }, /* 0110 0110 1 */ + { 9, 0xD2, 832 }, /* 0110 1001 0 */ + { 9, 0xD3, 896 }, /* 0110 1001 1 */ + { 9, 0xD4, 960 }, /* 0110 1010 0 */ + { 9, 0xD5, 1024 }, /* 0110 1010 1 */ + { 9, 0xD6, 1088 }, /* 0110 1011 0 */ + { 9, 0xD7, 1152 }, /* 0110 1011 1 */ + { 9, 0xD8, 1216 }, /* 0110 1100 0 */ + { 9, 0xD9, 1280 }, /* 0110 1100 1 */ + { 9, 0xDA, 1344 }, /* 0110 1101 0 */ + { 9, 0xDB, 1408 }, /* 0110 1101 1 */ + { 9, 0x98, 1472 }, /* 0100 1100 0 */ + { 9, 0x99, 1536 }, /* 0100 1100 1 */ + { 9, 0x9A, 1600 }, /* 0100 1101 0 */ + { 6, 0x18, 1664 }, /* 0110 00 */ + { 9, 0x9B, 1728 }, /* 0100 1101 1 */ + { 11, 0x8, 1792 }, /* 0000 0001 000 */ + { 11, 0xC, 1856 }, /* 0000 0001 100 */ + { 11, 0xD, 1920 }, /* 0000 0001 101 */ + { 12, 0x12, 1984 }, /* 0000 0001 0010 */ + { 12, 0x13, 2048 }, /* 0000 0001 0011 */ + { 12, 0x14, 2112 }, /* 0000 0001 0100 */ + { 12, 0x15, 2176 }, /* 0000 0001 0101 */ + { 12, 0x16, 2240 }, /* 0000 0001 0110 */ + { 12, 0x17, 2304 }, /* 0000 0001 0111 */ + { 12, 0x1C, 2368 }, /* 0000 0001 1100 */ + { 12, 0x1D, 2432 }, /* 0000 0001 1101 */ + { 12, 0x1E, 2496 }, /* 0000 0001 1110 */ + { 12, 0x1F, 2560 }, /* 0000 0001 1111 */ + { 12, 0x1, G3CODE_EOL }, /* 0000 0000 0001 */ + { 9, 0x1, G3CODE_INVALID }, /* 0000 0000 1 */ + { 10, 0x1, G3CODE_INVALID }, /* 0000 0000 01 */ + { 11, 0x1, G3CODE_INVALID }, /* 0000 0000 001 */ + { 12, 0x0, G3CODE_INVALID }, /* 0000 0000 0000 */ +}; + +#ifdef __DARWIN__ +#ifndef TIFFaxBlackCodes +#define TIFFFaxBlackCodes Darwin_X_TIFFFaxBlackCodes +#endif +#endif +tableentry TIFFFaxBlackCodes[] = { + { 10, 0x37, 0 }, /* 0000 1101 11 */ + { 3, 0x2, 1 }, /* 010 */ + { 2, 0x3, 2 }, /* 11 */ + { 2, 0x2, 3 }, /* 10 */ + { 3, 0x3, 4 }, /* 011 */ + { 4, 0x3, 5 }, /* 0011 */ + { 4, 0x2, 6 }, /* 0010 */ + { 5, 0x3, 7 }, /* 0001 1 */ + { 6, 0x5, 8 }, /* 0001 01 */ + { 6, 0x4, 9 }, /* 0001 00 */ + { 7, 0x4, 10 }, /* 0000 100 */ + { 7, 0x5, 11 }, /* 0000 101 */ + { 7, 0x7, 12 }, /* 0000 111 */ + { 8, 0x4, 13 }, /* 0000 0100 */ + { 8, 0x7, 14 }, /* 0000 0111 */ + { 9, 0x18, 15 }, /* 0000 1100 0 */ + { 10, 0x17, 16 }, /* 0000 0101 11 */ + { 10, 0x18, 17 }, /* 0000 0110 00 */ + { 10, 0x8, 18 }, /* 0000 0010 00 */ + { 11, 0x67, 19 }, /* 0000 1100 111 */ + { 11, 0x68, 20 }, /* 0000 1101 000 */ + { 11, 0x6C, 21 }, /* 0000 1101 100 */ + { 11, 0x37, 22 }, /* 0000 0110 111 */ + { 11, 0x28, 23 }, /* 0000 0101 000 */ + { 11, 0x17, 24 }, /* 0000 0010 111 */ + { 11, 0x18, 25 }, /* 0000 0011 000 */ + { 12, 0xCA, 26 }, /* 0000 1100 1010 */ + { 12, 0xCB, 27 }, /* 0000 1100 1011 */ + { 12, 0xCC, 28 }, /* 0000 1100 1100 */ + { 12, 0xCD, 29 }, /* 0000 1100 1101 */ + { 12, 0x68, 30 }, /* 0000 0110 1000 */ + { 12, 0x69, 31 }, /* 0000 0110 1001 */ + { 12, 0x6A, 32 }, /* 0000 0110 1010 */ + { 12, 0x6B, 33 }, /* 0000 0110 1011 */ + { 12, 0xD2, 34 }, /* 0000 1101 0010 */ + { 12, 0xD3, 35 }, /* 0000 1101 0011 */ + { 12, 0xD4, 36 }, /* 0000 1101 0100 */ + { 12, 0xD5, 37 }, /* 0000 1101 0101 */ + { 12, 0xD6, 38 }, /* 0000 1101 0110 */ + { 12, 0xD7, 39 }, /* 0000 1101 0111 */ + { 12, 0x6C, 40 }, /* 0000 0110 1100 */ + { 12, 0x6D, 41 }, /* 0000 0110 1101 */ + { 12, 0xDA, 42 }, /* 0000 1101 1010 */ + { 12, 0xDB, 43 }, /* 0000 1101 1011 */ + { 12, 0x54, 44 }, /* 0000 0101 0100 */ + { 12, 0x55, 45 }, /* 0000 0101 0101 */ + { 12, 0x56, 46 }, /* 0000 0101 0110 */ + { 12, 0x57, 47 }, /* 0000 0101 0111 */ + { 12, 0x64, 48 }, /* 0000 0110 0100 */ + { 12, 0x65, 49 }, /* 0000 0110 0101 */ + { 12, 0x52, 50 }, /* 0000 0101 0010 */ + { 12, 0x53, 51 }, /* 0000 0101 0011 */ + { 12, 0x24, 52 }, /* 0000 0010 0100 */ + { 12, 0x37, 53 }, /* 0000 0011 0111 */ + { 12, 0x38, 54 }, /* 0000 0011 1000 */ + { 12, 0x27, 55 }, /* 0000 0010 0111 */ + { 12, 0x28, 56 }, /* 0000 0010 1000 */ + { 12, 0x58, 57 }, /* 0000 0101 1000 */ + { 12, 0x59, 58 }, /* 0000 0101 1001 */ + { 12, 0x2B, 59 }, /* 0000 0010 1011 */ + { 12, 0x2C, 60 }, /* 0000 0010 1100 */ + { 12, 0x5A, 61 }, /* 0000 0101 1010 */ + { 12, 0x66, 62 }, /* 0000 0110 0110 */ + { 12, 0x67, 63 }, /* 0000 0110 0111 */ + { 10, 0xF, 64 }, /* 0000 0011 11 */ + { 12, 0xC8, 128 }, /* 0000 1100 1000 */ + { 12, 0xC9, 192 }, /* 0000 1100 1001 */ + { 12, 0x5B, 256 }, /* 0000 0101 1011 */ + { 12, 0x33, 320 }, /* 0000 0011 0011 */ + { 12, 0x34, 384 }, /* 0000 0011 0100 */ + { 12, 0x35, 448 }, /* 0000 0011 0101 */ + { 13, 0x6C, 512 }, /* 0000 0011 0110 0 */ + { 13, 0x6D, 576 }, /* 0000 0011 0110 1 */ + { 13, 0x4A, 640 }, /* 0000 0010 0101 0 */ + { 13, 0x4B, 704 }, /* 0000 0010 0101 1 */ + { 13, 0x4C, 768 }, /* 0000 0010 0110 0 */ + { 13, 0x4D, 832 }, /* 0000 0010 0110 1 */ + { 13, 0x72, 896 }, /* 0000 0011 1001 0 */ + { 13, 0x73, 960 }, /* 0000 0011 1001 1 */ + { 13, 0x74, 1024 }, /* 0000 0011 1010 0 */ + { 13, 0x75, 1088 }, /* 0000 0011 1010 1 */ + { 13, 0x76, 1152 }, /* 0000 0011 1011 0 */ + { 13, 0x77, 1216 }, /* 0000 0011 1011 1 */ + { 13, 0x52, 1280 }, /* 0000 0010 1001 0 */ + { 13, 0x53, 1344 }, /* 0000 0010 1001 1 */ + { 13, 0x54, 1408 }, /* 0000 0010 1010 0 */ + { 13, 0x55, 1472 }, /* 0000 0010 1010 1 */ + { 13, 0x5A, 1536 }, /* 0000 0010 1101 0 */ + { 13, 0x5B, 1600 }, /* 0000 0010 1101 1 */ + { 13, 0x64, 1664 }, /* 0000 0011 0010 0 */ + { 13, 0x65, 1728 }, /* 0000 0011 0010 1 */ + { 11, 0x8, 1792 }, /* 0000 0001 000 */ + { 11, 0xC, 1856 }, /* 0000 0001 100 */ + { 11, 0xD, 1920 }, /* 0000 0001 101 */ + { 12, 0x12, 1984 }, /* 0000 0001 0010 */ + { 12, 0x13, 2048 }, /* 0000 0001 0011 */ + { 12, 0x14, 2112 }, /* 0000 0001 0100 */ + { 12, 0x15, 2176 }, /* 0000 0001 0101 */ + { 12, 0x16, 2240 }, /* 0000 0001 0110 */ + { 12, 0x17, 2304 }, /* 0000 0001 0111 */ + { 12, 0x1C, 2368 }, /* 0000 0001 1100 */ + { 12, 0x1D, 2432 }, /* 0000 0001 1101 */ + { 12, 0x1E, 2496 }, /* 0000 0001 1110 */ + { 12, 0x1F, 2560 }, /* 0000 0001 1111 */ + { 12, 0x1, G3CODE_EOL }, /* 0000 0000 0001 */ + { 9, 0x1, G3CODE_INVALID }, /* 0000 0000 1 */ + { 10, 0x1, G3CODE_INVALID }, /* 0000 0000 01 */ + { 11, 0x1, G3CODE_INVALID }, /* 0000 0000 001 */ + { 12, 0x0, G3CODE_INVALID }, /* 0000 0000 0000 */ +}; diff --git a/nx-X11/lib/lbxutil/image/lbxfax.h b/nx-X11/lib/lbxutil/image/lbxfax.h new file mode 100644 index 000000000..beac228b7 --- /dev/null +++ b/nx-X11/lib/lbxutil/image/lbxfax.h @@ -0,0 +1,60 @@ +/* $Xorg: lbxfax.h,v 1.3 2000/08/17 19:46:41 cpqbld Exp $ */ +/* + * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler + * Copyright (c) 1991, 1992 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ +/* $XFree86$ */ + +/* + * This header file contains various constants and tables needed + * for FAX G42D compression. + */ + +typedef struct tableentry { + unsigned short length; /* bit length of g3 code */ + unsigned short code; /* g3 code */ + short runlen; /* run length in bits */ +} tableentry; + +#define EOL 0x001 /* EOL code value - 0000 0000 0000 1 */ + +/* status values returned instead of a run length */ + +#define G3CODE_INVALID -1 +#define G3CODE_INCOMP -2 +#define G3CODE_EOL -3 +#define G3CODE_EOF -4 + +#define ROUNDUP8(x) ((x + 7) >> 3) + +#ifdef __DARWIN__ +#ifndef TIFFaxWhiteCodes +#define TIFFFaxWhiteCodes Darwin_X_TIFFFaxWhiteCodes +#define TIFFFaxBlackCodes Darwin_X_TIFFFaxBlackCodes +#endif +#endif +extern tableentry TIFFFaxWhiteCodes[]; +extern tableentry TIFFFaxBlackCodes[]; + +/* misc.c */ +extern int LbxImageFindDiff ( unsigned char *cp, int bs, int be, int color ); +extern void LbxReverseBits ( unsigned char *cp, int n ); diff --git a/nx-X11/lib/lbxutil/image/misc.c b/nx-X11/lib/lbxutil/image/misc.c new file mode 100644 index 000000000..fd228624e --- /dev/null +++ b/nx-X11/lib/lbxutil/image/misc.c @@ -0,0 +1,213 @@ +/* $Xorg: misc.c,v 1.3 2000/08/17 19:46:41 cpqbld Exp $ */ +/* + * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler + * Copyright (c) 1991, 1992 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +/* + * This module contains utilities for image compression used + * by the LBX server and proxy. + */ + +#include "lbxfax.h" + +static unsigned char zeroruns[256] = { + 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0f */ + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2f */ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 - 0xaf */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 - 0xbf */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 - 0xcf */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 - 0xdf */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 - 0xef */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xf0 - 0xff */ +}; + +static unsigned char oneruns[256] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 - 0x0f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 - 0x2f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x80 - 0x8f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x90 - 0x9f */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xa0 - 0xaf */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xb0 - 0xbf */ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xc0 - 0xcf */ + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xd0 - 0xdf */ + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */ + 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, /* 0xf0 - 0xff */ +}; + + +static unsigned char BitRevTable[256] = { + 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, + 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, + 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, + 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, + 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, + 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, + 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, + 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, + 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, + 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, + 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, + 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, + 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, + 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, + 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, + 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, + 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, + 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, + 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, + 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, + 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, + 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, + 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, + 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, + 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, + 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, + 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, + 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, + 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, + 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, + 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, + 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff +}; + + +/* + * Find a span of ones or zeros using the supplied table. The byte-aligned + * start of the bit string is supplied along with the start+end bit indices. + * The table gives the number of consecutive ones or zeros starting from the + * msb and is indexed by byte value. + */ + +static int +findspan (unsigned char **bpp, + int bs, int be, + unsigned char *tab) + +{ + unsigned char *bp = *bpp; + int bits = be - bs; + int n, span; + + /* + * Check partial byte on lhs. + */ + + if (bits > 0 && (n = (bs & 7))) + { + span = tab[(*bp << n) & 0xff]; + + if (span > 8 - n) /* table value too generous */ + span = 8 - n; + if (span > bits) /* constrain span to bit range */ + span = bits; + if (n+span < 8) /* doesn't extend to edge of byte */ + goto done; + + bits -= span; + bp++; + } + else + span = 0; + + /* + * Scan full bytes for all 1's or all 0's. + */ + + while (bits >= 8) + { + n = tab[*bp]; + span += n; + bits -= n; + if (n < 8) /* end of run */ + goto done; + bp++; + } + + /* + * Check partial byte on rhs. + */ + + if (bits > 0) + { + n = tab[*bp]; + span += (n > bits ? bits : n); + } + +done: + + *bpp = bp; + return (span); +} + +/* + * Return the offset of the next bit in the range [bs..be] that is different + * from the specified color. The end, be, is returned if no such bit exists. + */ + +int +LbxImageFindDiff (unsigned char *cp, + int bs, int be, int color) + +{ + cp += bs >> 3; /* adjust byte offset */ + return (bs + findspan (&cp, bs, be, color ? oneruns : zeroruns)); +} + + + + +void +LbxReverseBits (unsigned char *cp, + int n) + +{ + for (; n > 8; n -= 8) + { + cp[0] = BitRevTable[cp[0]]; + cp[1] = BitRevTable[cp[1]]; + cp[2] = BitRevTable[cp[2]]; + cp[3] = BitRevTable[cp[3]]; + cp[4] = BitRevTable[cp[4]]; + cp[5] = BitRevTable[cp[5]]; + cp[6] = BitRevTable[cp[6]]; + cp[7] = BitRevTable[cp[7]]; + cp += 8; + } + + while (n-- > 0) + *cp = BitRevTable[*cp], cp++; +} diff --git a/nx-X11/lib/lbxutil/image/mkg3states.c b/nx-X11/lib/lbxutil/image/mkg3states.c new file mode 100644 index 000000000..0e8fe3613 --- /dev/null +++ b/nx-X11/lib/lbxutil/image/mkg3states.c @@ -0,0 +1,761 @@ +/* $Xorg: mkg3states.c,v 1.3 2000/08/17 19:46:41 cpqbld Exp $ */ +/* + * Copyright (c) 1991, 1992 Sam Leffler + * Copyright (c) 1991, 1992 Silicon Graphics, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that (i) the above copyright notices and this permission notice appear in + * all copies of the software and related documentation, and (ii) the names of + * Sam Leffler and Silicon Graphics may not be used in any advertising or + * publicity relating to the software without the specific, prior written + * permission of Sam Leffler and Silicon Graphics. + * + * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, + * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * + * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR + * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, + * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF + * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ +/* $XFree86: xc/lib/lbxutil/image/mkg3states.c,v 1.3 1999/02/01 11:55:49 dawes Exp $ */ + +/* + * Program to construct Group 3 & Group 4 decoding tables. + * + * This code is derived from code by Michael P. Marking. In + * particular, the algorithms to generate the null_mode and + * horiz_mode state tables are his. See the comments below + * for more information. + * + * BEGIN (from the original source) + LEGAL + * Copyright 1989, 1990 Michael P. Marking, Post Office Box 8039, + * Scottsdale, Arizona 85252-8039. All rights reserved. + * + * License is granted by the copyright holder to distribute and use this + * code without payment of royalties or the necessity of notification as + * long as this notice (all the text under "LEGAL") is included. + * + * This program is offered without any warranty of any kind. It includes + * no warranty of merchantability or fitness for any purpose. Testing and + * suitability for any use are the sole responsibility of the user. + * + INFORMATION + * Although there is no support offered with this program, the author will + * endeavor to correct errors. Updates will also be made available from + * time to time. + * + * Contact: Michael P. Marking, Post Office Box 8039, Scottsdale, Arizona + * 85252-8039 USA. Replies are not guaranteed to be swift. Beginning + * July 1990, e-mail may be sent to uunet!ipel!marking. + * + * Also beginning in July 1990, this code will be archived at the + * ipel!phoenix BBS in file g3g4.zoo. The 24-hour telephone number + * for 300/1200/2400 is (602)274-0462. When logging in, specify user + * "public", system "bbs", and password "public". + * + * This code is also available from the C Users Group in volume 317. + * + * END (from the original source) + */ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#ifndef TRUE +#define TRUE 1 +#define FALSE 0 +#endif + +#define WHITE 0 +#define BLACK 1 + +/* + * G3 2D and G4 decoding modes. Note that + * the vertical modes are ordered so that + * (mode - MODE_VERT_V0) gives the vertical + * adjustment for the b1 parameter. + */ +#define MODE_NULL 0 +#define MODE_PASS 1 +#define MODE_HORIZ 2 +#define MODE_VERT_VL3 3 +#define MODE_VERT_VL2 4 +#define MODE_VERT_VL1 5 +#define MODE_VERT_V0 6 +#define MODE_VERT_VR1 7 +#define MODE_VERT_VR2 8 +#define MODE_VERT_VR3 9 +#define MODE_UNCOMP 10 +#define MODE_ERROR 11 +#define MODE_ERROR_1 12 + +unsigned long append_0 ( unsigned long prefix ); +unsigned long append_1 ( unsigned long prefix ); + +unsigned long append_0 (unsigned long prefix) +{ + return (prefix + (1L<<16)); +} + +unsigned long append_1 (unsigned long prefix) +{ + static unsigned short prefix_bit[16] = { + 0x8000, 0x4000, 0x2000, 0x1000, + 0x0800, 0x0400, 0x0200, 0x0100, + 0x0080, 0x0040, 0x0020, 0x0010, + 0x0008, 0x0004, 0x0002, 0x0001 + }; + unsigned char len = (prefix >> 16) & 0xf; + return (append_0(prefix) + prefix_bit[len]); +} + +#define G3CODES +#include "lbxfax.h" +#include "lbxbwcodes.h" + +short search_table ( unsigned long prefix, tableentry *tab, int n ); +short white_run_length ( unsigned long prefix ); +short black_run_length ( unsigned long prefix ); +short horiz_mode_code_black ( short runlen ); +short horiz_mode_code_white ( short runlen ); +void write_define ( FILE *fd, char *name, int value, char *comment ); +void write_preamble ( FILE *fd ); +void extern_table ( FILE *fd, char *name ); +void write_tables ( FILE *fd ); +short find_null_mode_prefix ( long prefix ); +short find_horiz_mode_prefix ( long prefix, int color ); +short find_uncomp_mode_prefix ( long prefix ); +short null_mode_type ( long prefix ); +short uncomp_mode_type ( long prefix ); +void build_null_mode_tables ( void ); +void build_horiz_mode_tables ( void ); +void build_uncomp_mode_tables ( void ); + +short search_table (unsigned long prefix, tableentry *tab, int n) +{ + unsigned short len = (prefix >> 16) & 0xf; + unsigned short code = (prefix & 0xffff) >> (16 - len); + + while (n-- > 0) { + if (tab->length == len && tab->code == code) + return ((short) tab->runlen); + tab++; + } + return (G3CODE_INCOMP); +} + +#define NCODES(a) (sizeof (a) / sizeof (a[0])) +short white_run_length (unsigned long prefix) +{ + return (search_table(prefix, TIFFFaxWhiteCodes, NCODES(TIFFFaxWhiteCodes))); +} + +short black_run_length (unsigned long prefix) +{ + return (search_table(prefix, TIFFFaxBlackCodes, NCODES(TIFFFaxBlackCodes))); +} +#undef NCODES + +#define MAX_NULLPREFIX 200 /* max # of null-mode prefixes */ +typedef unsigned char NullModeTable[MAX_NULLPREFIX][256]; +#define MAX_HORIZPREFIX 250 /* max # of incomplete 1-D prefixes */ +typedef unsigned char HorizModeTable[MAX_HORIZPREFIX][256]; + + /* the bit string corresponding to this row of the decoding table */ +long null_mode_prefix[MAX_NULLPREFIX]; +NullModeTable null_mode; /* MODE_*, indexed by bit and byte */ +NullModeTable null_mode_next_state; /* next row of decoding tables to use */ + /* number of prefixes or rows in the G4 decoding tables */ +short null_mode_prefix_count = 0; + +void write_null_mode_table ( FILE *fd, NullModeTable table, char *name ); +void write_horiz_mode_table ( FILE *fd, HorizModeTable table, char *name ); + +/* + * 2D uncompressed mode codes. Note + * that two groups of codes are arranged + * so that the decoder can caluclate the + * length of the run by subtracting the + * code from a known base value. + */ +#define UNCOMP_INCOMP 0 +/* runs of [0]*1 */ +#define UNCOMP_RUN0 1 +#define UNCOMP_RUN1 2 +#define UNCOMP_RUN2 3 +#define UNCOMP_RUN3 4 +#define UNCOMP_RUN4 5 +#define UNCOMP_RUN5 6 +#define UNCOMP_RUN6 7 +/* runs of [0]* w/ terminating color */ +#define UNCOMP_TRUN0 8 +#define UNCOMP_TRUN1 9 +#define UNCOMP_TRUN2 10 +#define UNCOMP_TRUN3 11 +#define UNCOMP_TRUN4 12 +/* special code for unexpected EOF */ +#define UNCOMP_EOF 13 +/* invalid code encountered */ +#define UNCOMP_INVALID 14 + +long uncomp_mode_prefix[MAX_NULLPREFIX]; +NullModeTable uncomp_mode; +NullModeTable uncomp_mode_next_state; +short uncomp_mode_prefix_count = 0; + +/* + * Decoding action values for horiz_mode. + */ +#define ACT_INCOMP 0 /* incompletely decoded code */ +#define ACT_INVALID 1 /* invalide code */ +#define ACT_WRUNT 2 /* terminating white run code */ +#define ACT_WRUN 65 /* non-terminating white run code */ +#define ACT_BRUNT 106 /* terminating black run code */ +#define ACT_BRUN 169 /* non-terminating black run code */ +#define ACT_EOL 210 /* end-of-line code */ +HorizModeTable horiz_mode; + +short horiz_mode_code_black (short runlen) +{ + return (runlen < 64 ? runlen + ACT_BRUNT : (runlen / 64) + ACT_BRUN); +} + +short horiz_mode_code_white (short runlen) +{ + return (runlen < 64 ? runlen + ACT_WRUNT : (runlen / 64) + ACT_WRUN); +} + +/* + * If the corresponding horiz_mode entry is ACT_INCOMP + * this entry is a row number for decoding the next byte; + * otherwise, it is the bit number with which to continue + * decoding the next codeword. + */ +HorizModeTable horiz_mode_next_state; + /* prefixes corresponding to the rows of the decoding table */ +long horiz_mode_prefix[MAX_HORIZPREFIX]; + /* color of next run, BLACK or WHITE */ +char horiz_mode_color[MAX_HORIZPREFIX]; +short horiz_mode_prefix_count = 0; + +static unsigned char bit_mask[8] = + { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; + + +int verbose = FALSE; +char *storage_class = ""; + +int +main (int argc, char *argv[]) +{ + while (argc > 1 && argv[1][0] == '-') { + if (strcmp(argv[1], "-v") == 0) { + verbose = TRUE; + argc--, argv++; + } else if (strcmp(argv[1], "-c") == 0) { + storage_class = "const "; + argc--, argv++; + } + } + build_null_mode_tables(); /* null mode decoding tables */ + if (verbose) { + fprintf(stderr, "%d null mode prefixes defined\n", + (int) null_mode_prefix_count); + fprintf(stderr, "building uncompressed mode scripts...\n"); + } + build_uncomp_mode_tables(); /* uncompressed mode decoding tables */ + if (verbose) { + fprintf(stderr, "%d uncompressed mode prefixes defined\n", + (int) uncomp_mode_prefix_count); + fprintf(stderr, "building 1D scripts...\n"); + } + build_horiz_mode_tables(); /* 1D decoding tables */ + if (verbose) + fprintf(stderr, "%d incomplete prefixes defined\n", + (int) horiz_mode_prefix_count); + write_tables(stdout); + exit(0); +} + +void write_null_mode_table (fd, table, name) +FILE *fd; +NullModeTable table; +char *name; +{ + int i, j; + char* outersep; + char* sep; + + fprintf(fd, "%sunsigned char\t%s[%d][256] = {", storage_class, + name, (int) null_mode_prefix_count); + outersep = ""; + for (i = 0; i < null_mode_prefix_count; i++) { + fprintf(fd, "%s\n/* prefix %d */ {\n", outersep, i); + sep = " "; + for (j = 0; j < 256; j++) { + fprintf(fd, "%s%2d", sep, (int) table[i][j]); + if (((j+1) % 16) == 0) { + fprintf(fd, ", /* %3d-%3d */\n", j-15, j); + sep = " "; + } else + sep = ","; + } + fprintf(fd, "}"); + outersep = ","; + } + fprintf(fd, "\n};\n"); +} + +void +write_horiz_mode_table (FILE *fd, HorizModeTable table, char *name) +{ + int i, j; + char* outersep; + char* sep; + + fprintf(fd, "%s unsigned char\t%s[%d][256] = {", storage_class, + name, (int) horiz_mode_prefix_count); + outersep = ""; + for (i = 0; i < horiz_mode_prefix_count; i++) { + fprintf(fd, "%s\n/* prefix %d */ {\n", outersep, i); + sep = " "; + for (j = 0; j < 256; j++) { + fprintf(fd, "%s%3d", sep, (int) table[i][j]); + if (((j+1) % 14) == 0) { + fprintf(fd, ", /* %3d-%3d */\n", j-13, j); + sep = " "; + } else + sep = ","; + } + fprintf(fd, "\n}"); + outersep = ","; + } + fprintf(fd, "\n};\n"); +} + +void +write_define(FILE *fd, char *name, int value, char *comment) +{ + fprintf(fd, "#define\t%s\t%d", name, value); + if (comment) + fprintf(fd, "\t/* %s */", comment); + fprintf(fd, "\n"); +} + +void +write_preamble(FILE *fd) +{ + fprintf(fd, "%s\n", +"/* DO NOT EDIT THIS FILE, IT WAS AUTOMATICALLY CREATED BY mkg3state */"); + write_define(fd, "ACT_INCOMP", ACT_INCOMP, "incompletely decoded code"); + write_define(fd, "ACT_INVALID", ACT_INVALID, "invalide code"); + write_define(fd, "ACT_WRUNT", ACT_WRUNT, "terminating white run code"); + write_define(fd, "ACT_WRUN", ACT_WRUN, "non-terminating white run code"); + write_define(fd, "ACT_BRUNT", ACT_BRUNT, "terminating black run code"); + write_define(fd, "ACT_BRUN", ACT_BRUN, "non-terminating black run code"); + write_define(fd, "ACT_EOL", ACT_EOL, "end-of-line code"); + fprintf(fd, "\n"); + fprintf(fd, "/* modes that the decoder can be in */\n"); + write_define(fd, "MODE_NULL", MODE_NULL, NULL); + write_define(fd, "MODE_PASS", MODE_PASS, NULL); + write_define(fd, "MODE_HORIZ", MODE_HORIZ, NULL); + write_define(fd, "MODE_VERT_V0", MODE_VERT_V0, NULL); + write_define(fd, "MODE_VERT_VR1", MODE_VERT_VR1, NULL); + write_define(fd, "MODE_VERT_VR2", MODE_VERT_VR2, NULL); + write_define(fd, "MODE_VERT_VR3", MODE_VERT_VR3, NULL); + write_define(fd, "MODE_VERT_VL1", MODE_VERT_VL1, NULL); + write_define(fd, "MODE_VERT_VL2", MODE_VERT_VL2, NULL); + write_define(fd, "MODE_VERT_VL3", MODE_VERT_VL3, NULL); + write_define(fd, "MODE_UNCOMP", MODE_UNCOMP, NULL); + write_define(fd, "MODE_ERROR", MODE_ERROR, NULL); + write_define(fd, "MODE_ERROR_1", MODE_ERROR_1, NULL); + fprintf(fd, "\n"); + fprintf(fd, "#define\tRUNLENGTH(ix) (TIFFFaxWhiteCodes[ix].runlen)\n"); + fprintf(fd, "\n"); + write_define(fd, "UNCOMP_INCOMP", UNCOMP_INCOMP, NULL); + fprintf(fd, "/* runs of [0]*1 */\n"); + write_define(fd, "UNCOMP_RUN0", UNCOMP_RUN0, NULL); + write_define(fd, "UNCOMP_RUN1", UNCOMP_RUN1, NULL); + write_define(fd, "UNCOMP_RUN2", UNCOMP_RUN2, NULL); + write_define(fd, "UNCOMP_RUN3", UNCOMP_RUN3, NULL); + write_define(fd, "UNCOMP_RUN4", UNCOMP_RUN4, NULL); + write_define(fd, "UNCOMP_RUN5", UNCOMP_RUN5, NULL); + write_define(fd, "UNCOMP_RUN6", UNCOMP_RUN6, NULL); + fprintf(fd, "/* runs of [0]* w/ terminating color */\n"); + write_define(fd, "UNCOMP_TRUN0", UNCOMP_TRUN0, NULL); + write_define(fd, "UNCOMP_TRUN1", UNCOMP_TRUN1, NULL); + write_define(fd, "UNCOMP_TRUN2", UNCOMP_TRUN2, NULL); + write_define(fd, "UNCOMP_TRUN3", UNCOMP_TRUN3, NULL); + write_define(fd, "UNCOMP_TRUN4", UNCOMP_TRUN4, NULL); + fprintf(fd, "/* special code for unexpected EOF */\n"); + write_define(fd, "UNCOMP_EOF", UNCOMP_EOF, NULL); + fprintf(fd, "/* invalid code encountered */\n"); + write_define(fd, "UNCOMP_INVALID", UNCOMP_INVALID, NULL); + fprintf(fd, "/* codes >= terminate uncompress mode */\n"); + fprintf(fd, "#define\tUNCOMP_EXIT UNCOMP_TRUN0\n"); + fprintf(fd, "\n"); +} + +void +extern_table(FILE *fd, char *name) +{ + fprintf(fd, "extern\t%sunsigned char %s[][256];\n", storage_class, name); +} + +void +write_tables(FILE *fd) +{ + write_preamble(fd); + write_null_mode_table(fd, null_mode, "TIFFFax2DMode"); + write_null_mode_table(fd, null_mode_next_state, "TIFFFax2DNextState"); + write_null_mode_table(fd, uncomp_mode, "TIFFFaxUncompAction"); + write_null_mode_table(fd, uncomp_mode_next_state, "TIFFFaxUncompNextState"); + write_horiz_mode_table(fd, horiz_mode, "TIFFFax1DAction"); + write_horiz_mode_table(fd, horiz_mode_next_state, "TIFFFax1DNextState"); +} + +short +find_null_mode_prefix (long prefix) +{ + short j1; + + if (prefix == 0L) + return (0); + for (j1 = 8; j1 < null_mode_prefix_count; j1++) + if (prefix == null_mode_prefix[j1]) + return (j1); + if (null_mode_prefix_count == MAX_NULLPREFIX) { + fprintf(stderr, "ERROR: null mode prefix table overflow\n"); + exit(1); + } + if (verbose) + fprintf(stderr, "adding null mode prefix[%d] 0x%lx\n", + (int) null_mode_prefix_count, prefix); + null_mode_prefix[null_mode_prefix_count++] = prefix; + return (null_mode_prefix_count-1); +} + +short +find_horiz_mode_prefix (long prefix, int color) +{ + short j1; + + for (j1 = 0; j1 < horiz_mode_prefix_count; j1++) + if (prefix == horiz_mode_prefix[j1] && horiz_mode_color[j1] == color) + return (j1); + /* + * It wasn't found, so add it to the tables, but first, is there room? + */ + if (horiz_mode_prefix_count == MAX_HORIZPREFIX) { + fprintf(stderr, "ERROR: 1D prefix table overflow\n"); + exit(1); + } + /* OK, there's room... */ + if (verbose) + fprintf(stderr, "\nhoriz mode prefix %d, color %c = 0x%lx ", + (int) horiz_mode_prefix_count, "WB"[color], prefix); + horiz_mode_prefix[horiz_mode_prefix_count] = prefix; + horiz_mode_color[horiz_mode_prefix_count] = color; + horiz_mode_prefix_count++; + return (horiz_mode_prefix_count - 1); +} + +short +find_uncomp_mode_prefix (long prefix) +{ + short j1; + + if (prefix == 0L) + return (0); + for (j1 = 8; j1 < uncomp_mode_prefix_count; j1++) + if (prefix == uncomp_mode_prefix[j1]) + return (j1); + if (uncomp_mode_prefix_count == MAX_NULLPREFIX) { + fprintf(stderr, "ERROR: uncomp mode prefix table overflow\n"); + exit(1); + } + if (verbose) + fprintf(stderr, "adding uncomp mode prefix[%d] 0x%lx\n", + (int) uncomp_mode_prefix_count, prefix); + uncomp_mode_prefix[uncomp_mode_prefix_count++] = prefix; + return (uncomp_mode_prefix_count-1); +} + +short +null_mode_type (long prefix) +{ + switch (prefix) { + case 0x18000L: return (MODE_VERT_V0); /* 1 */ + case 0x36000L: return (MODE_VERT_VR1); /* 011 */ + case 0x34000L: return (MODE_VERT_VL1); /* 010 */ + case 0x32000L: return (MODE_HORIZ); /* 001 */ + case 0x41000L: return (MODE_PASS); /* 0001 */ + case 0x60C00L: return (MODE_VERT_VR2); /* 0000 11 */ + case 0x60800L: return (MODE_VERT_VL2); /* 0000 10 */ + case 0x70600L: return (MODE_VERT_VR3); /* 0000 011 */ + case 0x70400L: return (MODE_VERT_VL3); /* 0000 010 */ + case 0x80200L: return (MODE_ERROR); /* 0000 0010 */ + case 0x90300L: return (MODE_ERROR); /* 0000 0011 0 */ + case 0xA0380L: return (MODE_ERROR); /* 0000 0011 10 */ + case 0xA03C0L: return (MODE_UNCOMP); /* 0000 0011 11 */ + /* + * Under the assumption that there are no + * errors in the file, then this bit string + * can only be the beginning of an EOL code. + */ + case 0x70000L: return (MODE_ERROR_1); /* 0000 000 */ + } + return (-1); +} + +short +uncomp_mode_type (long prefix) +{ + short code; + short len; + switch (prefix) { + case 0x18000L: return (UNCOMP_RUN1); /* 1 */ + case 0x24000L: return (UNCOMP_RUN2); /* 01 */ + case 0x32000L: return (UNCOMP_RUN3); /* 001 */ + case 0x41000L: return (UNCOMP_RUN4); /* 0001 */ + case 0x50800L: return (UNCOMP_RUN5); /* 0000 1 */ + case 0x60400L: return (UNCOMP_RUN6); /* 0000 01 */ + case 0x70200L: return (UNCOMP_TRUN0); /* 0000 001 */ + case 0x80100L: return (UNCOMP_TRUN1); /* 0000 0001 */ + case 0x90080L: return (UNCOMP_TRUN2); /* 0000 0000 1 */ + case 0xA0040L: return (UNCOMP_TRUN3); /* 0000 0000 01 */ + case 0xB0020L: return (UNCOMP_TRUN4); /* 0000 0000 001 */ + } + code = prefix & 0xffffL; + len = (prefix >> 16) & 0xf; + return ((code || len > 10) ? UNCOMP_INVALID : -1); +} + +#define BASESTATE(b) ((unsigned char) ((b) & 0x7)) + +void +build_null_mode_tables(void) +{ + short prefix; + + /* + * Note: the first eight entries correspond to + * a null prefix and starting bit numbers 0-7. + */ + null_mode_prefix_count = 8; + for (prefix = 0; prefix < null_mode_prefix_count; prefix++) { + short byte; + for (byte = 0; byte < 256; byte++) { + short firstbit; + short bit; + long curprefix; + char found_code = FALSE; + + if (prefix < 8) { + curprefix = 0L; + firstbit = prefix; + } else { + curprefix = null_mode_prefix[prefix]; + firstbit = 0; + } + for (bit = firstbit; bit < 8 && !found_code; bit++) { + short mode; + + if (bit_mask[bit] & byte) + curprefix = append_1(curprefix); + else + curprefix = append_0(curprefix); + switch (mode = null_mode_type(curprefix)) { + case MODE_PASS: + case MODE_HORIZ: + case MODE_VERT_V0: + case MODE_VERT_VR1: + case MODE_VERT_VR2: + case MODE_VERT_VR3: + case MODE_VERT_VL1: + case MODE_VERT_VL2: + case MODE_VERT_VL3: + case MODE_UNCOMP: + case MODE_ERROR: + case MODE_ERROR_1: + /* + * NOTE: if the bit number is 8, then the table + * entry will be zero, which indicates a new byte + * is to be fetched during the decoding process + */ + found_code = TRUE; + null_mode[prefix][byte] = (unsigned char) mode; + null_mode_next_state[prefix][byte] = BASESTATE(bit+1); + break; + } + } + if (!found_code) { + null_mode_next_state[prefix][byte] = (unsigned char) + find_null_mode_prefix(curprefix); + /* + * This indicates to the decoder that + * no valid code has yet been identified. + */ + null_mode[prefix][byte] = MODE_NULL; + } + } + } +} + +void +build_horiz_mode_tables(void) +{ + unsigned short byte; + short prefix; + + /* + * The first 8 are for white, + * the second 8 are for black, + * beginning with bits 0-7. + */ + horiz_mode_prefix_count = 16; + for (prefix = 0; prefix < horiz_mode_prefix_count; prefix++) + for (byte = 0; byte < 256; byte++) { + short bits_digested = 0; + short bit; + short firstbit; + char color; + unsigned long curprefix; + + if (prefix < 8) { + color = WHITE; + curprefix = 0L; + firstbit = prefix; + } else if (prefix < 16) { + color = BLACK; + curprefix = 0L; + firstbit = prefix - 8; + } else { + color = horiz_mode_color[prefix]; + curprefix = horiz_mode_prefix[prefix]; + firstbit = 0; + } + for (bit = firstbit; bit < 8 && !bits_digested; bit++) { + if (bit_mask[bit] & byte) + curprefix = append_1(curprefix); + else + curprefix = append_0(curprefix); + /* + * The following conversion allows for arbitrary strings of + * zeroes to precede the end-of-line code 0000 0000 0001. + * It assumes no errors in the data, and is based on + * the assumption that the code replaced (12 consecutive + * zeroes) can only be "legally" encountered before the + * end-of-line code. This assumption is valid only for + * a Group 3 image; the combination will never occur + * in horizontal mode in a proper Group 4 image. + */ + if (curprefix == 0xC0000L) + curprefix = 0xB0000L; + if (color == WHITE) { + short runlength = white_run_length(curprefix); + + if (runlength == G3CODE_INVALID) { + horiz_mode[prefix][byte] = (unsigned char) ACT_INVALID; + horiz_mode_next_state[prefix][byte] = (unsigned char) bit; + bits_digested = bit + 1; + } else if (runlength == G3CODE_EOL) { /* Group 3 only */ + horiz_mode[prefix][byte] = (unsigned char) ACT_EOL; + horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1); + bits_digested = bit + 1; + } else if (runlength != G3CODE_INCOMP) { + horiz_mode[prefix][byte] = (unsigned char) + horiz_mode_code_white(runlength); + horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1); + bits_digested = bit + 1; + } + } else { /* color == BLACK */ + short runlength = black_run_length(curprefix); + + if (runlength == G3CODE_INVALID) { + horiz_mode[prefix][byte] = (unsigned char) ACT_INVALID; + horiz_mode_next_state[prefix][byte] = (unsigned char) (bit+8); + bits_digested = bit + 1; + } else if (runlength == G3CODE_EOL) { /* Group 3 only */ + horiz_mode[prefix][byte] = (unsigned char) ACT_EOL; + horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1); + bits_digested = bit + 1; + } else if (runlength != G3CODE_INCOMP) { + horiz_mode[prefix][byte] = (unsigned char) + horiz_mode_code_black(runlength); + horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1); + bits_digested = bit + 1; + } + } + } + if (!bits_digested) { /* no codewords after examining byte */ + horiz_mode[prefix][byte] = (unsigned char) ACT_INCOMP; + horiz_mode_next_state[prefix][byte] = (unsigned char) + find_horiz_mode_prefix(curprefix, color); + } + } +} + +void +build_uncomp_mode_tables(void) +{ + short prefix; + + /* + * Note: the first eight entries correspond to + * a null prefix and starting bit numbers 0-7. + */ + uncomp_mode_prefix_count = 8; + for (prefix = 0; prefix < uncomp_mode_prefix_count; prefix++) { + short byte; + for (byte = 0; byte < 256; byte++) { + short firstbit; + short bit; + long curprefix; + char found_code = FALSE; + + if (prefix < 8) { + curprefix = 0L; + firstbit = prefix; + } else { + curprefix = uncomp_mode_prefix[prefix]; + firstbit = 0; + } + for (bit = firstbit; bit < 8 && !found_code; bit++) { + short mode; + + if (bit_mask[bit] & byte) + curprefix = append_1(curprefix); + else + curprefix = append_0(curprefix); + mode = uncomp_mode_type(curprefix); + if (mode != -1) { + /* + * NOTE: if the bit number is 8, then the table + * entry will be zero, which indicates a new byte + * is to be fetched during the decoding process + */ + found_code = TRUE; + uncomp_mode[prefix][byte] = (unsigned char) mode; + uncomp_mode_next_state[prefix][byte] = BASESTATE(bit+1); + break; + } + } + if (!found_code) { + uncomp_mode_next_state[prefix][byte] = (unsigned char) + find_uncomp_mode_prefix(curprefix); + /* + * This indicates to the decoder that + * no valid code has yet been identified. + */ + uncomp_mode[prefix][byte] = UNCOMP_INCOMP; + } + } + } +} |