?? tif_fax3.c
字號:
/* $Id: tif_fax3.c,v 1.43.2.1 2007/03/28 21:48:16 fwarmerdam Exp $ *//* * Copyright (c) 1990-1997 Sam Leffler * Copyright (c) 1991-1997 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 "tiffiop.h"#ifdef CCITT_SUPPORT/* * TIFF Library. * * CCITT Group 3 (T.4) and Group 4 (T.6) Compression Support. * * This file contains support for decoding and encoding TIFF * compression algorithms 2, 3, 4, and 32771. * * Decoder support is derived, with permission, from the code * in Frank Cringle's viewfax program; * Copyright (C) 1990, 1995 Frank D. Cringle. */#include "tif_fax3.h"#define G3CODES#include "t4.h"#include <stdio.h>/* * Compression+decompression state blocks are * derived from this ``base state'' block. */typedef struct { int rw_mode; /* O_RDONLY for decode, else encode */ int mode; /* operating mode */ uint32 rowbytes; /* bytes in a decoded scanline */ uint32 rowpixels; /* pixels in a scanline */ uint16 cleanfaxdata; /* CleanFaxData tag */ uint32 badfaxrun; /* BadFaxRun tag */ uint32 badfaxlines; /* BadFaxLines tag */ uint32 groupoptions; /* Group 3/4 options tag */ uint32 recvparams; /* encoded Class 2 session params */ char* subaddress; /* subaddress string */ uint32 recvtime; /* time spent receiving (secs) */ char* faxdcs; /* Table 2/T.30 encoded session params */ TIFFVGetMethod vgetparent; /* super-class method */ TIFFVSetMethod vsetparent; /* super-class method */ TIFFPrintMethod printdir; /* super-class method */} Fax3BaseState;#define Fax3State(tif) ((Fax3BaseState*) (tif)->tif_data)typedef enum { G3_1D, G3_2D } Ttag;typedef struct { Fax3BaseState b; /* Decoder state info */ const unsigned char* bitmap; /* bit reversal table */ uint32 data; /* current i/o byte/word */ int bit; /* current i/o bit in byte */ int EOLcnt; /* count of EOL codes recognized */ TIFFFaxFillFunc fill; /* fill routine */ uint32* runs; /* b&w runs for current/previous row */ uint32* refruns; /* runs for reference line */ uint32* curruns; /* runs for current line */ /* Encoder state info */ Ttag tag; /* encoding state */ unsigned char* refline; /* reference line for 2d decoding */ int k; /* #rows left that can be 2d encoded */ int maxk; /* max #rows that can be 2d encoded */ int line;} Fax3CodecState;#define DecoderState(tif) ((Fax3CodecState*) Fax3State(tif))#define EncoderState(tif) ((Fax3CodecState*) Fax3State(tif))#define is2DEncoding(sp) \ (sp->b.groupoptions & GROUP3OPT_2DENCODING)#define isAligned(p,t) ((((unsigned long)(p)) & (sizeof (t)-1)) == 0)/* * Group 3 and Group 4 Decoding. *//* * These macros glue the TIFF library state to * the state expected by Frank's decoder. */#define DECLARE_STATE(tif, sp, mod) \ static const char module[] = mod; \ Fax3CodecState* sp = DecoderState(tif); \ int a0; /* reference element */ \ int lastx = sp->b.rowpixels; /* last element in row */ \ uint32 BitAcc; /* bit accumulator */ \ int BitsAvail; /* # valid bits in BitAcc */ \ int RunLength; /* length of current run */ \ unsigned char* cp; /* next byte of input data */ \ unsigned char* ep; /* end of input data */ \ uint32* pa; /* place to stuff next run */ \ uint32* thisrun; /* current row's run array */ \ int EOLcnt; /* # EOL codes recognized */ \ const unsigned char* bitmap = sp->bitmap; /* input data bit reverser */ \ const TIFFFaxTabEnt* TabEnt#define DECLARE_STATE_2D(tif, sp, mod) \ DECLARE_STATE(tif, sp, mod); \ int b1; /* next change on prev line */ \ uint32* pb /* next run in reference line */\/* * Load any state that may be changed during decoding. */#define CACHE_STATE(tif, sp) do { \ BitAcc = sp->data; \ BitsAvail = sp->bit; \ EOLcnt = sp->EOLcnt; \ cp = (unsigned char*) tif->tif_rawcp; \ ep = cp + tif->tif_rawcc; \} while (0)/* * Save state possibly changed during decoding. */#define UNCACHE_STATE(tif, sp) do { \ sp->bit = BitsAvail; \ sp->data = BitAcc; \ sp->EOLcnt = EOLcnt; \ tif->tif_rawcc -= (tidata_t) cp - tif->tif_rawcp; \ tif->tif_rawcp = (tidata_t) cp; \} while (0)/* * Setup state for decoding a strip. */static intFax3PreDecode(TIFF* tif, tsample_t s){ Fax3CodecState* sp = DecoderState(tif); (void) s; assert(sp != NULL); sp->bit = 0; /* force initial read */ sp->data = 0; sp->EOLcnt = 0; /* force initial scan for EOL */ /* * Decoder assumes lsb-to-msb bit order. Note that we select * this here rather than in Fax3SetupState so that viewers can * hold the image open, fiddle with the FillOrder tag value, * and then re-decode the image. Otherwise they'd need to close * and open the image to get the state reset. */ sp->bitmap = TIFFGetBitRevTable(tif->tif_dir.td_fillorder != FILLORDER_LSB2MSB); if (sp->refruns) { /* init reference line to white */ sp->refruns[0] = (uint32) sp->b.rowpixels; sp->refruns[1] = 0; } sp->line = 0; return (1);}/* * Routine for handling various errors/conditions. * Note how they are "glued into the decoder" by * overriding the definitions used by the decoder. */static voidFax3Unexpected(const char* module, TIFF* tif, uint32 line, uint32 a0){ TIFFErrorExt(tif->tif_clientdata, module, "%s: Bad code word at line %lu of %s %lu (x %lu)", tif->tif_name, (unsigned long) line, isTiled(tif) ? "tile" : "strip", (unsigned long) (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), (unsigned long) a0);}#define unexpected(table, a0) Fax3Unexpected(module, tif, sp->line, a0)static voidFax3Extension(const char* module, TIFF* tif, uint32 line, uint32 a0){ TIFFErrorExt(tif->tif_clientdata, module, "%s: Uncompressed data (not supported) at line %lu of %s %lu (x %lu)", tif->tif_name, (unsigned long) line, isTiled(tif) ? "tile" : "strip", (unsigned long) (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), (unsigned long) a0);}#define extension(a0) Fax3Extension(module, tif, sp->line, a0)static voidFax3BadLength(const char* module, TIFF* tif, uint32 line, uint32 a0, uint32 lastx){ TIFFWarningExt(tif->tif_clientdata, module, "%s: %s at line %lu of %s %lu (got %lu, expected %lu)", tif->tif_name, a0 < lastx ? "Premature EOL" : "Line length mismatch", (unsigned long) line, isTiled(tif) ? "tile" : "strip", (unsigned long) (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), (unsigned long) a0, lastx);}#define badlength(a0,lastx) Fax3BadLength(module, tif, sp->line, a0, lastx)static voidFax3PrematureEOF(const char* module, TIFF* tif, uint32 line, uint32 a0){ TIFFWarningExt(tif->tif_clientdata, module, "%s: Premature EOF at line %lu of %s %lu (x %lu)", tif->tif_name, (unsigned long) line, isTiled(tif) ? "tile" : "strip", (unsigned long) (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), (unsigned long) a0);}#define prematureEOF(a0) Fax3PrematureEOF(module, tif, sp->line, a0)#define Nop/* * Decode the requested amount of G3 1D-encoded data. */static intFax3Decode1D(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s){ DECLARE_STATE(tif, sp, "Fax3Decode1D"); (void) s; CACHE_STATE(tif, sp); thisrun = sp->curruns; while ((long)occ > 0) { a0 = 0; RunLength = 0; pa = thisrun;#ifdef FAX3_DEBUG printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail); printf("-------------------- %d\n", tif->tif_row); fflush(stdout);#endif SYNC_EOL(EOF1D); EXPAND1D(EOF1Da); (*sp->fill)(buf, thisrun, pa, lastx); buf += sp->b.rowbytes; occ -= sp->b.rowbytes; sp->line++; continue; EOF1D: /* premature EOF */ CLEANUP_RUNS(); EOF1Da: /* premature EOF */ UNCACHE_STATE(tif, sp); return (-1); } UNCACHE_STATE(tif, sp); return (1);}#define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; }/* * Decode the requested amount of G3 2D-encoded data. */static intFax3Decode2D(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s){ DECLARE_STATE_2D(tif, sp, "Fax3Decode2D"); int is1D; /* current line is 1d/2d-encoded */ (void) s; CACHE_STATE(tif, sp); while ((long)occ > 0) { a0 = 0; RunLength = 0; pa = thisrun = sp->curruns;#ifdef FAX3_DEBUG printf("\nBitAcc=%08X, BitsAvail = %d EOLcnt = %d", BitAcc, BitsAvail, EOLcnt);#endif SYNC_EOL(EOF2D); NeedBits8(1, EOF2D); is1D = GetBits(1); /* 1D/2D-encoding tag bit */ ClrBits(1);#ifdef FAX3_DEBUG printf(" %s\n-------------------- %d\n", is1D ? "1D" : "2D", tif->tif_row); fflush(stdout);#endif pb = sp->refruns; b1 = *pb++; if (is1D) EXPAND1D(EOF2Da); else EXPAND2D(EOF2Da); (*sp->fill)(buf, thisrun, pa, lastx); SETVALUE(0); /* imaginary change for reference */ SWAP(uint32*, sp->curruns, sp->refruns); buf += sp->b.rowbytes; occ -= sp->b.rowbytes; sp->line++; continue; EOF2D: /* premature EOF */ CLEANUP_RUNS(); EOF2Da: /* premature EOF */ (*sp->fill)(buf, thisrun, pa, lastx); UNCACHE_STATE(tif, sp); return (-1); } UNCACHE_STATE(tif, sp); return (1);}#undef SWAP/* * The ZERO & FILL macros must handle spans < 2*sizeof(long) bytes. * For machines with 64-bit longs this is <16 bytes; otherwise * this is <8 bytes. We optimize the code here to reflect the * machine characteristics. */#if SIZEOF_LONG == 8# define FILL(n, cp) \ switch (n) { \ case 15:(cp)[14] = 0xff; case 14:(cp)[13] = 0xff; case 13: (cp)[12] = 0xff;\ case 12:(cp)[11] = 0xff; case 11:(cp)[10] = 0xff; case 10: (cp)[9] = 0xff;\ case 9: (cp)[8] = 0xff; case 8: (cp)[7] = 0xff; case 7: (cp)[6] = 0xff;\ case 6: (cp)[5] = 0xff; case 5: (cp)[4] = 0xff; case 4: (cp)[3] = 0xff;\ case 3: (cp)[2] = 0xff; case 2: (cp)[1] = 0xff; \ case 1: (cp)[0] = 0xff; (cp) += (n); case 0: ; \ }# define ZERO(n, cp) \ switch (n) { \ case 15:(cp)[14] = 0; case 14:(cp)[13] = 0; case 13: (cp)[12] = 0; \ case 12:(cp)[11] = 0; case 11:(cp)[10] = 0; case 10: (cp)[9] = 0; \ case 9: (cp)[8] = 0; case 8: (cp)[7] = 0; case 7: (cp)[6] = 0; \ case 6: (cp)[5] = 0; case 5: (cp)[4] = 0; case 4: (cp)[3] = 0; \ case 3: (cp)[2] = 0; case 2: (cp)[1] = 0; \ case 1: (cp)[0] = 0; (cp) += (n); case 0: ; \ }#else# define FILL(n, cp) \ switch (n) { \ case 7: (cp)[6] = 0xff; case 6: (cp)[5] = 0xff; case 5: (cp)[4] = 0xff; \ case 4: (cp)[3] = 0xff; case 3: (cp)[2] = 0xff; case 2: (cp)[1] = 0xff; \ case 1: (cp)[0] = 0xff; (cp) += (n); case 0: ; \ }# define ZERO(n, cp) \ switch (n) { \ case 7: (cp)[6] = 0; case 6: (cp)[5] = 0; case 5: (cp)[4] = 0; \ case 4: (cp)[3] = 0; case 3: (cp)[2] = 0; case 2: (cp)[1] = 0; \ case 1: (cp)[0] = 0; (cp) += (n); case 0: ; \ }#endif/* * Bit-fill a row according to the white/black * runs generated during G3/G4 decoding. */void_TIFFFax3fillruns(unsigned char* buf, uint32* runs, uint32* erun, uint32 lastx){ static const unsigned char _fillmasks[] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; unsigned char* cp; uint32 x, bx, run; int32 n, nw; long* lp; if ((erun-runs)&1) *erun++ = 0; x = 0; for (; runs < erun; runs += 2) { run = runs[0]; if (x+run > lastx || run > lastx ) run = runs[0] = (uint32) (lastx - x); if (run) { cp = buf + (x>>3); bx = x&7; if (run > 8-bx) { if (bx) { /* align to byte boundary */ *cp++ &= 0xff << (8-bx); run -= 8-bx; } if( (n = run >> 3) != 0 ) { /* multiple bytes to fill */ if ((n/sizeof (long)) > 1) { /* * Align to longword boundary and fill. */ for (; n && !isAligned(cp, long); n--) *cp++ = 0x00; lp = (long*) cp; nw = (int32)(n / sizeof (long)); n -= nw * sizeof (long); do { *lp++ = 0L; } while (--nw); cp = (unsigned char*) lp; } ZERO(n, cp); run &= 7; } if (run) cp[0] &= 0xff >> run; } else cp[0] &= ~(_fillmasks[run]>>bx); x += runs[0]; } run = runs[1]; if (x+run > lastx || run > lastx ) run = runs[1] = lastx - x; if (run) { cp = buf + (x>>3); bx = x&7; if (run > 8-bx) { if (bx) { /* align to byte boundary */ *cp++ |= 0xff >> bx; run -= 8-bx; } if( (n = run>>3) != 0 ) { /* multiple bytes to fill */ if ((n/sizeof (long)) > 1) { /* * Align to longword boundary and fill. */ for (; n && !isAligned(cp, long); n--) *cp++ = 0xff; lp = (long*) cp; nw = (int32)(n / sizeof (long)); n -= nw * sizeof (long); do { *lp++ = -1L; } while (--nw); cp = (unsigned char*) lp; } FILL(n, cp); run &= 7; } if (run) cp[0] |= 0xff00 >> run; } else cp[0] |= _fillmasks[run]>>bx; x += runs[1]; } } assert(x == lastx);}#undef ZERO#undef FILL/* * Setup G3/G4-related compression/decompression state * before data is processed. This routine is called once * per image -- it sets up different state based on whether * or not decoding or encoding is being done and whether * 1D- or 2D-encoded data is involved. */static intFax3SetupState(TIFF* tif){ TIFFDirectory* td = &tif->tif_dir; Fax3BaseState* sp = Fax3State(tif); int needsRefLine; Fax3CodecState* dsp = (Fax3CodecState*) Fax3State(tif); uint32 rowbytes, rowpixels, nruns; if (td->td_bitspersample != 1) { TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Bits/sample must be 1 for Group 3/4 encoding/decoding"); return (0); } /* * Calculate the scanline/tile widths. */ if (isTiled(tif)) { rowbytes = TIFFTileRowSize(tif); rowpixels = td->td_tilewidth; } else { rowbytes = TIFFScanlineSize(tif); rowpixels = td->td_imagewidth; } sp->rowbytes = (uint32) rowbytes; sp->rowpixels = (uint32) rowpixels; /* * Allocate any additional space required for decoding/encoding. */ needsRefLine = ( (sp->groupoptions & GROUP3OPT_2DENCODING) || td->td_compression == COMPRESSION_CCITTFAX4 ); nruns = needsRefLine ? 2*TIFFroundup(rowpixels,32) : rowpixels; nruns += 3; dsp->runs = (uint32*) _TIFFCheckMalloc(tif, 2*nruns, sizeof (uint32), "for Group 3/4 run arrays"); if (dsp->runs == NULL) return (0); dsp->curruns = dsp->runs; if (needsRefLine) dsp->refruns = dsp->runs + nruns; else dsp->refruns = NULL; if (td->td_compression == COMPRESSION_CCITTFAX3 && is2DEncoding(dsp)) { /* NB: default is 1D routine */ tif->tif_decoderow = Fax3Decode2D; tif->tif_decodestrip = Fax3Decode2D; tif->tif_decodetile = Fax3Decode2D; } if (needsRefLine) { /* 2d encoding */ Fax3CodecState* esp = EncoderState(tif); /* * 2d encoding requires a scanline * buffer for the ``reference line''; the * scanline against which delta encoding * is referenced. The reference line must * be initialized to be ``white'' (done elsewhere). */ esp->refline = (unsigned char*) _TIFFmalloc(rowbytes); if (esp->refline == NULL) { TIFFErrorExt(tif->tif_clientdata, "Fax3SetupState", "%s: No space for Group 3/4 reference line", tif->tif_name);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -