Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygentif_fax3.c
Go to the documentation of this file.
00001 /* $Id: tif_fax3.c,v 1.43.2.10 2010-06-09 17:16:58 bfriesen Exp $ */ 00002 00003 /* 00004 * Copyright (c) 1990-1997 Sam Leffler 00005 * Copyright (c) 1991-1997 Silicon Graphics, Inc. 00006 * 00007 * Permission to use, copy, modify, distribute, and sell this software and 00008 * its documentation for any purpose is hereby granted without fee, provided 00009 * that (i) the above copyright notices and this permission notice appear in 00010 * all copies of the software and related documentation, and (ii) the names of 00011 * Sam Leffler and Silicon Graphics may not be used in any advertising or 00012 * publicity relating to the software without the specific, prior written 00013 * permission of Sam Leffler and Silicon Graphics. 00014 * 00015 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 00016 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 00017 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 00018 * 00019 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR 00020 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, 00021 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 00022 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 00023 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 00024 * OF THIS SOFTWARE. 00025 */ 00026 00027 #include "tiffiop.h" 00028 #ifdef CCITT_SUPPORT 00029 /* 00030 * TIFF Library. 00031 * 00032 * CCITT Group 3 (T.4) and Group 4 (T.6) Compression Support. 00033 * 00034 * This file contains support for decoding and encoding TIFF 00035 * compression algorithms 2, 3, 4, and 32771. 00036 * 00037 * Decoder support is derived, with permission, from the code 00038 * in Frank Cringle's viewfax program; 00039 * Copyright (C) 1990, 1995 Frank D. Cringle. 00040 */ 00041 #include "tif_fax3.h" 00042 #define G3CODES 00043 #include "t4.h" 00044 #include <stdio.h> 00045 00046 /* 00047 * Compression+decompression state blocks are 00048 * derived from this ``base state'' block. 00049 */ 00050 typedef struct { 00051 int rw_mode; /* O_RDONLY for decode, else encode */ 00052 int mode; /* operating mode */ 00053 uint32 rowbytes; /* bytes in a decoded scanline */ 00054 uint32 rowpixels; /* pixels in a scanline */ 00055 00056 uint16 cleanfaxdata; /* CleanFaxData tag */ 00057 uint32 badfaxrun; /* BadFaxRun tag */ 00058 uint32 badfaxlines; /* BadFaxLines tag */ 00059 uint32 groupoptions; /* Group 3/4 options tag */ 00060 uint32 recvparams; /* encoded Class 2 session params */ 00061 char* subaddress; /* subaddress string */ 00062 uint32 recvtime; /* time spent receiving (secs) */ 00063 char* faxdcs; /* Table 2/T.30 encoded session params */ 00064 TIFFVGetMethod vgetparent; /* super-class method */ 00065 TIFFVSetMethod vsetparent; /* super-class method */ 00066 TIFFPrintMethod printdir; /* super-class method */ 00067 } Fax3BaseState; 00068 #define Fax3State(tif) ((Fax3BaseState*) (tif)->tif_data) 00069 00070 typedef enum { G3_1D, G3_2D } Ttag; 00071 typedef struct { 00072 Fax3BaseState b; 00073 00074 /* Decoder state info */ 00075 const unsigned char* bitmap; /* bit reversal table */ 00076 uint32 data; /* current i/o byte/word */ 00077 int bit; /* current i/o bit in byte */ 00078 int EOLcnt; /* count of EOL codes recognized */ 00079 TIFFFaxFillFunc fill; /* fill routine */ 00080 uint32* runs; /* b&w runs for current/previous row */ 00081 uint32* refruns; /* runs for reference line */ 00082 uint32* curruns; /* runs for current line */ 00083 00084 /* Encoder state info */ 00085 Ttag tag; /* encoding state */ 00086 unsigned char* refline; /* reference line for 2d decoding */ 00087 int k; /* #rows left that can be 2d encoded */ 00088 int maxk; /* max #rows that can be 2d encoded */ 00089 00090 int line; 00091 } Fax3CodecState; 00092 #define DecoderState(tif) ((Fax3CodecState*) Fax3State(tif)) 00093 #define EncoderState(tif) ((Fax3CodecState*) Fax3State(tif)) 00094 00095 #define is2DEncoding(sp) \ 00096 (sp->b.groupoptions & GROUP3OPT_2DENCODING) 00097 #define isAligned(p,t) ((((unsigned long)(p)) & (sizeof (t)-1)) == 0) 00098 00099 /* 00100 * Group 3 and Group 4 Decoding. 00101 */ 00102 00103 /* 00104 * These macros glue the TIFF library state to 00105 * the state expected by Frank's decoder. 00106 */ 00107 #define DECLARE_STATE(tif, sp, mod) \ 00108 static const char module[] = mod; \ 00109 Fax3CodecState* sp = DecoderState(tif); \ 00110 int a0; /* reference element */ \ 00111 int lastx = sp->b.rowpixels; /* last element in row */ \ 00112 uint32 BitAcc; /* bit accumulator */ \ 00113 int BitsAvail; /* # valid bits in BitAcc */ \ 00114 int RunLength; /* length of current run */ \ 00115 unsigned char* cp; /* next byte of input data */ \ 00116 unsigned char* ep; /* end of input data */ \ 00117 uint32* pa; /* place to stuff next run */ \ 00118 uint32* thisrun; /* current row's run array */ \ 00119 int EOLcnt; /* # EOL codes recognized */ \ 00120 const unsigned char* bitmap = sp->bitmap; /* input data bit reverser */ \ 00121 const TIFFFaxTabEnt* TabEnt 00122 #define DECLARE_STATE_2D(tif, sp, mod) \ 00123 DECLARE_STATE(tif, sp, mod); \ 00124 int b1; /* next change on prev line */ \ 00125 uint32* pb /* next run in reference line */\ 00126 /* 00127 * Load any state that may be changed during decoding. 00128 */ 00129 #define CACHE_STATE(tif, sp) do { \ 00130 BitAcc = sp->data; \ 00131 BitsAvail = sp->bit; \ 00132 EOLcnt = sp->EOLcnt; \ 00133 cp = (unsigned char*) tif->tif_rawcp; \ 00134 ep = cp + tif->tif_rawcc; \ 00135 } while (0) 00136 /* 00137 * Save state possibly changed during decoding. 00138 */ 00139 #define UNCACHE_STATE(tif, sp) do { \ 00140 sp->bit = BitsAvail; \ 00141 sp->data = BitAcc; \ 00142 sp->EOLcnt = EOLcnt; \ 00143 tif->tif_rawcc -= (tidata_t) cp - tif->tif_rawcp; \ 00144 tif->tif_rawcp = (tidata_t) cp; \ 00145 } while (0) 00146 00147 /* 00148 * Setup state for decoding a strip. 00149 */ 00150 static int 00151 Fax3PreDecode(TIFF* tif, tsample_t s) 00152 { 00153 Fax3CodecState* sp = DecoderState(tif); 00154 00155 (void) s; 00156 assert(sp != NULL); 00157 sp->bit = 0; /* force initial read */ 00158 sp->data = 0; 00159 sp->EOLcnt = 0; /* force initial scan for EOL */ 00160 /* 00161 * Decoder assumes lsb-to-msb bit order. Note that we select 00162 * this here rather than in Fax3SetupState so that viewers can 00163 * hold the image open, fiddle with the FillOrder tag value, 00164 * and then re-decode the image. Otherwise they'd need to close 00165 * and open the image to get the state reset. 00166 */ 00167 sp->bitmap = 00168 TIFFGetBitRevTable(tif->tif_dir.td_fillorder != FILLORDER_LSB2MSB); 00169 if (sp->refruns) { /* init reference line to white */ 00170 sp->refruns[0] = (uint32) sp->b.rowpixels; 00171 sp->refruns[1] = 0; 00172 } 00173 sp->line = 0; 00174 return (1); 00175 } 00176 00177 /* 00178 * Routine for handling various errors/conditions. 00179 * Note how they are "glued into the decoder" by 00180 * overriding the definitions used by the decoder. 00181 */ 00182 00183 static void 00184 Fax3Unexpected(const char* module, TIFF* tif, uint32 line, uint32 a0) 00185 { 00186 TIFFErrorExt(tif->tif_clientdata, module, "%s: Bad code word at line %u of %s %u (x %u)", 00187 tif->tif_name, line, isTiled(tif) ? "tile" : "strip", 00188 (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), 00189 a0); 00190 } 00191 #define unexpected(table, a0) Fax3Unexpected(module, tif, sp->line, a0) 00192 00193 static void 00194 Fax3Extension(const char* module, TIFF* tif, uint32 line, uint32 a0) 00195 { 00196 TIFFErrorExt(tif->tif_clientdata, module, 00197 "%s: Uncompressed data (not supported) at line %u of %s %u (x %u)", 00198 tif->tif_name, line, isTiled(tif) ? "tile" : "strip", 00199 (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), 00200 a0); 00201 } 00202 #define extension(a0) Fax3Extension(module, tif, sp->line, a0) 00203 00204 static void 00205 Fax3BadLength(const char* module, TIFF* tif, uint32 line, uint32 a0, uint32 lastx) 00206 { 00207 TIFFWarningExt(tif->tif_clientdata, module, "%s: %s at line %u of %s %u (got %u, expected %u)", 00208 tif->tif_name, 00209 a0 < lastx ? "Premature EOL" : "Line length mismatch", 00210 line, isTiled(tif) ? "tile" : "strip", 00211 (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), 00212 a0, lastx); 00213 } 00214 #define badlength(a0,lastx) Fax3BadLength(module, tif, sp->line, a0, lastx) 00215 00216 static void 00217 Fax3PrematureEOF(const char* module, TIFF* tif, uint32 line, uint32 a0) 00218 { 00219 TIFFWarningExt(tif->tif_clientdata, module, "%s: Premature EOF at line %u of %s %u (x %u)", 00220 tif->tif_name, 00221 line, isTiled(tif) ? "tile" : "strip", 00222 (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), 00223 a0); 00224 } 00225 #define prematureEOF(a0) Fax3PrematureEOF(module, tif, sp->line, a0) 00226 00227 #define Nop 00228 00229 /* 00230 * Decode the requested amount of G3 1D-encoded data. 00231 */ 00232 static int 00233 Fax3Decode1D(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s) 00234 { 00235 DECLARE_STATE(tif, sp, "Fax3Decode1D"); 00236 00237 (void) s; 00238 CACHE_STATE(tif, sp); 00239 thisrun = sp->curruns; 00240 while ((long)occ > 0) { 00241 a0 = 0; 00242 RunLength = 0; 00243 pa = thisrun; 00244 #ifdef FAX3_DEBUG 00245 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail); 00246 printf("-------------------- %d\n", tif->tif_row); 00247 fflush(stdout); 00248 #endif 00249 SYNC_EOL(EOF1D); 00250 EXPAND1D(EOF1Da); 00251 (*sp->fill)(buf, thisrun, pa, lastx); 00252 buf += sp->b.rowbytes; 00253 occ -= sp->b.rowbytes; 00254 sp->line++; 00255 continue; 00256 EOF1D: /* premature EOF */ 00257 CLEANUP_RUNS(); 00258 EOF1Da: /* premature EOF */ 00259 (*sp->fill)(buf, thisrun, pa, lastx); 00260 UNCACHE_STATE(tif, sp); 00261 return (-1); 00262 } 00263 UNCACHE_STATE(tif, sp); 00264 return (1); 00265 } 00266 00267 #define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; } 00268 /* 00269 * Decode the requested amount of G3 2D-encoded data. 00270 */ 00271 static int 00272 Fax3Decode2D(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s) 00273 { 00274 DECLARE_STATE_2D(tif, sp, "Fax3Decode2D"); 00275 int is1D; /* current line is 1d/2d-encoded */ 00276 00277 (void) s; 00278 CACHE_STATE(tif, sp); 00279 while ((long)occ > 0) { 00280 a0 = 0; 00281 RunLength = 0; 00282 pa = thisrun = sp->curruns; 00283 #ifdef FAX3_DEBUG 00284 printf("\nBitAcc=%08X, BitsAvail = %d EOLcnt = %d", 00285 BitAcc, BitsAvail, EOLcnt); 00286 #endif 00287 SYNC_EOL(EOF2D); 00288 NeedBits8(1, EOF2D); 00289 is1D = GetBits(1); /* 1D/2D-encoding tag bit */ 00290 ClrBits(1); 00291 #ifdef FAX3_DEBUG 00292 printf(" %s\n-------------------- %d\n", 00293 is1D ? "1D" : "2D", tif->tif_row); 00294 fflush(stdout); 00295 #endif 00296 pb = sp->refruns; 00297 b1 = *pb++; 00298 if (is1D) 00299 EXPAND1D(EOF2Da); 00300 else 00301 EXPAND2D(EOF2Da); 00302 (*sp->fill)(buf, thisrun, pa, lastx); 00303 SETVALUE(0); /* imaginary change for reference */ 00304 SWAP(uint32*, sp->curruns, sp->refruns); 00305 buf += sp->b.rowbytes; 00306 occ -= sp->b.rowbytes; 00307 sp->line++; 00308 continue; 00309 EOF2D: /* premature EOF */ 00310 CLEANUP_RUNS(); 00311 EOF2Da: /* premature EOF */ 00312 (*sp->fill)(buf, thisrun, pa, lastx); 00313 UNCACHE_STATE(tif, sp); 00314 return (-1); 00315 } 00316 UNCACHE_STATE(tif, sp); 00317 return (1); 00318 } 00319 #undef SWAP 00320 00321 /* 00322 * The ZERO & FILL macros must handle spans < 2*sizeof(long) bytes. 00323 * For machines with 64-bit longs this is <16 bytes; otherwise 00324 * this is <8 bytes. We optimize the code here to reflect the 00325 * machine characteristics. 00326 */ 00327 #if SIZEOF_LONG == 8 00328 # define FILL(n, cp) \ 00329 switch (n) { \ 00330 case 15:(cp)[14] = 0xff; case 14:(cp)[13] = 0xff; case 13: (cp)[12] = 0xff;\ 00331 case 12:(cp)[11] = 0xff; case 11:(cp)[10] = 0xff; case 10: (cp)[9] = 0xff;\ 00332 case 9: (cp)[8] = 0xff; case 8: (cp)[7] = 0xff; case 7: (cp)[6] = 0xff;\ 00333 case 6: (cp)[5] = 0xff; case 5: (cp)[4] = 0xff; case 4: (cp)[3] = 0xff;\ 00334 case 3: (cp)[2] = 0xff; case 2: (cp)[1] = 0xff; \ 00335 case 1: (cp)[0] = 0xff; (cp) += (n); case 0: ; \ 00336 } 00337 # define ZERO(n, cp) \ 00338 switch (n) { \ 00339 case 15:(cp)[14] = 0; case 14:(cp)[13] = 0; case 13: (cp)[12] = 0; \ 00340 case 12:(cp)[11] = 0; case 11:(cp)[10] = 0; case 10: (cp)[9] = 0; \ 00341 case 9: (cp)[8] = 0; case 8: (cp)[7] = 0; case 7: (cp)[6] = 0; \ 00342 case 6: (cp)[5] = 0; case 5: (cp)[4] = 0; case 4: (cp)[3] = 0; \ 00343 case 3: (cp)[2] = 0; case 2: (cp)[1] = 0; \ 00344 case 1: (cp)[0] = 0; (cp) += (n); case 0: ; \ 00345 } 00346 #else 00347 # define FILL(n, cp) \ 00348 switch (n) { \ 00349 case 7: (cp)[6] = 0xff; case 6: (cp)[5] = 0xff; case 5: (cp)[4] = 0xff; \ 00350 case 4: (cp)[3] = 0xff; case 3: (cp)[2] = 0xff; case 2: (cp)[1] = 0xff; \ 00351 case 1: (cp)[0] = 0xff; (cp) += (n); case 0: ; \ 00352 } 00353 # define ZERO(n, cp) \ 00354 switch (n) { \ 00355 case 7: (cp)[6] = 0; case 6: (cp)[5] = 0; case 5: (cp)[4] = 0; \ 00356 case 4: (cp)[3] = 0; case 3: (cp)[2] = 0; case 2: (cp)[1] = 0; \ 00357 case 1: (cp)[0] = 0; (cp) += (n); case 0: ; \ 00358 } 00359 #endif 00360 00361 /* 00362 * Bit-fill a row according to the white/black 00363 * runs generated during G3/G4 decoding. 00364 */ 00365 void 00366 _TIFFFax3fillruns(unsigned char* buf, uint32* runs, uint32* erun, uint32 lastx) 00367 { 00368 static const unsigned char _fillmasks[] = 00369 { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; 00370 unsigned char* cp; 00371 uint32 x, bx, run; 00372 int32 n, nw; 00373 long* lp; 00374 00375 if ((erun-runs)&1) 00376 *erun++ = 0; 00377 x = 0; 00378 for (; runs < erun; runs += 2) { 00379 run = runs[0]; 00380 if (x+run > lastx || run > lastx ) 00381 run = runs[0] = (uint32) (lastx - x); 00382 if (run) { 00383 cp = buf + (x>>3); 00384 bx = x&7; 00385 if (run > 8-bx) { 00386 if (bx) { /* align to byte boundary */ 00387 *cp++ &= 0xff << (8-bx); 00388 run -= 8-bx; 00389 } 00390 if( (n = run >> 3) != 0 ) { /* multiple bytes to fill */ 00391 if ((n/sizeof (long)) > 1) { 00392 /* 00393 * Align to longword boundary and fill. 00394 */ 00395 for (; n && !isAligned(cp, long); n--) 00396 *cp++ = 0x00; 00397 lp = (long*) cp; 00398 nw = (int32)(n / sizeof (long)); 00399 n -= nw * sizeof (long); 00400 do { 00401 *lp++ = 0L; 00402 } while (--nw); 00403 cp = (unsigned char*) lp; 00404 } 00405 ZERO(n, cp); 00406 run &= 7; 00407 } 00408 if (run) 00409 cp[0] &= 0xff >> run; 00410 } else 00411 cp[0] &= ~(_fillmasks[run]>>bx); 00412 x += runs[0]; 00413 } 00414 run = runs[1]; 00415 if (x+run > lastx || run > lastx ) 00416 run = runs[1] = lastx - x; 00417 if (run) { 00418 cp = buf + (x>>3); 00419 bx = x&7; 00420 if (run > 8-bx) { 00421 if (bx) { /* align to byte boundary */ 00422 *cp++ |= 0xff >> bx; 00423 run -= 8-bx; 00424 } 00425 if( (n = run>>3) != 0 ) { /* multiple bytes to fill */ 00426 if ((n/sizeof (long)) > 1) { 00427 /* 00428 * Align to longword boundary and fill. 00429 */ 00430 for (; n && !isAligned(cp, long); n--) 00431 *cp++ = 0xff; 00432 lp = (long*) cp; 00433 nw = (int32)(n / sizeof (long)); 00434 n -= nw * sizeof (long); 00435 do { 00436 *lp++ = -1L; 00437 } while (--nw); 00438 cp = (unsigned char*) lp; 00439 } 00440 FILL(n, cp); 00441 run &= 7; 00442 } 00443 if (run) 00444 cp[0] |= 0xff00 >> run; 00445 } else 00446 cp[0] |= _fillmasks[run]>>bx; 00447 x += runs[1]; 00448 } 00449 } 00450 assert(x == lastx); 00451 } 00452 #undef ZERO 00453 #undef FILL 00454 00455 /* 00456 * Setup G3/G4-related compression/decompression state 00457 * before data is processed. This routine is called once 00458 * per image -- it sets up different state based on whether 00459 * or not decoding or encoding is being done and whether 00460 * 1D- or 2D-encoded data is involved. 00461 */ 00462 static int 00463 Fax3SetupState(TIFF* tif) 00464 { 00465 TIFFDirectory* td = &tif->tif_dir; 00466 Fax3BaseState* sp = Fax3State(tif); 00467 int needsRefLine; 00468 Fax3CodecState* dsp = (Fax3CodecState*) Fax3State(tif); 00469 uint32 rowbytes, rowpixels, nruns; 00470 00471 if (td->td_bitspersample != 1) { 00472 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 00473 "Bits/sample must be 1 for Group 3/4 encoding/decoding"); 00474 return (0); 00475 } 00476 /* 00477 * Calculate the scanline/tile widths. 00478 */ 00479 if (isTiled(tif)) { 00480 rowbytes = TIFFTileRowSize(tif); 00481 rowpixels = td->td_tilewidth; 00482 } else { 00483 rowbytes = TIFFScanlineSize(tif); 00484 rowpixels = td->td_imagewidth; 00485 } 00486 sp->rowbytes = (uint32) rowbytes; 00487 sp->rowpixels = (uint32) rowpixels; 00488 /* 00489 * Allocate any additional space required for decoding/encoding. 00490 */ 00491 needsRefLine = ( 00492 (sp->groupoptions & GROUP3OPT_2DENCODING) || 00493 td->td_compression == COMPRESSION_CCITTFAX4 00494 ); 00495 00496 /* 00497 Assure that allocation computations do not overflow. 00498 00499 TIFFroundup and TIFFSafeMultiply return zero on integer overflow 00500 */ 00501 dsp->runs=(uint32*) NULL; 00502 nruns = TIFFroundup(rowpixels,32); 00503 if (needsRefLine) { 00504 nruns = TIFFSafeMultiply(uint32,nruns,2); 00505 } 00506 if ((nruns == 0) || (TIFFSafeMultiply(uint32,nruns,2) == 0)) { 00507 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, 00508 "Row pixels integer overflow (rowpixels %u)", 00509 rowpixels); 00510 return (0); 00511 } 00512 dsp->runs = (uint32*) _TIFFCheckMalloc(tif, 00513 TIFFSafeMultiply(uint32,nruns,2), 00514 sizeof (uint32), 00515 "for Group 3/4 run arrays"); 00516 if (dsp->runs == NULL) 00517 return (0); 00518 dsp->curruns = dsp->runs; 00519 if (needsRefLine) 00520 dsp->refruns = dsp->runs + nruns; 00521 else 00522 dsp->refruns = NULL; 00523 if (td->td_compression == COMPRESSION_CCITTFAX3 00524 && is2DEncoding(dsp)) { /* NB: default is 1D routine */ 00525 tif->tif_decoderow = Fax3Decode2D; 00526 tif->tif_decodestrip = Fax3Decode2D; 00527 tif->tif_decodetile = Fax3Decode2D; 00528 } 00529 00530 if (needsRefLine) { /* 2d encoding */ 00531 Fax3CodecState* esp = EncoderState(tif); 00532 /* 00533 * 2d encoding requires a scanline 00534 * buffer for the ``reference line''; the 00535 * scanline against which delta encoding 00536 * is referenced. The reference line must 00537 * be initialized to be ``white'' (done elsewhere). 00538 */ 00539 esp->refline = (unsigned char*) _TIFFmalloc(rowbytes); 00540 if (esp->refline == NULL) { 00541 TIFFErrorExt(tif->tif_clientdata, "Fax3SetupState", 00542 "%s: No space for Group 3/4 reference line", 00543 tif->tif_name); 00544 return (0); 00545 } 00546 } else /* 1d encoding */ 00547 EncoderState(tif)->refline = NULL; 00548 00549 return (1); 00550 } 00551 00552 /* 00553 * CCITT Group 3 FAX Encoding. 00554 */ 00555 00556 #define Fax3FlushBits(tif, sp) { \ 00557 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \ 00558 (void) TIFFFlushData1(tif); \ 00559 *(tif)->tif_rawcp++ = (tidataval_t) (sp)->data; \ 00560 (tif)->tif_rawcc++; \ 00561 (sp)->data = 0, (sp)->bit = 8; \ 00562 } 00563 #define _FlushBits(tif) { \ 00564 if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize) \ 00565 (void) TIFFFlushData1(tif); \ 00566 *(tif)->tif_rawcp++ = (tidataval_t) data; \ 00567 (tif)->tif_rawcc++; \ 00568 data = 0, bit = 8; \ 00569 } 00570 static const int _msbmask[9] = 00571 { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff }; 00572 #define _PutBits(tif, bits, length) { \ 00573 while (length > bit) { \ 00574 data |= bits >> (length - bit); \ 00575 length -= bit; \ 00576 _FlushBits(tif); \ 00577 } \ 00578 data |= (bits & _msbmask[length]) << (bit - length); \ 00579 bit -= length; \ 00580 if (bit == 0) \ 00581 _FlushBits(tif); \ 00582 } 00583 00584 /* 00585 * Write a variable-length bit-value to 00586 * the output stream. Values are 00587 * assumed to be at most 16 bits. 00588 */ 00589 static void 00590 Fax3PutBits(TIFF* tif, unsigned int bits, unsigned int length) 00591 { 00592 Fax3CodecState* sp = EncoderState(tif); 00593 unsigned int bit = sp->bit; 00594 int data = sp->data; 00595 00596 _PutBits(tif, bits, length); 00597 00598 sp->data = data; 00599 sp->bit = bit; 00600 } 00601 00602 /* 00603 * Write a code to the output stream. 00604 */ 00605 #define putcode(tif, te) Fax3PutBits(tif, (te)->code, (te)->length) 00606 00607 #ifdef FAX3_DEBUG 00608 #define DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B") 00609 #define DEBUG_PRINT(what,len) { \ 00610 int t; \ 00611 printf("%08X/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what), len); \ 00612 for (t = length-1; t >= 0; t--) \ 00613 putchar(code & (1<<t) ? '1' : '0'); \ 00614 putchar('\n'); \ 00615 } 00616 #endif 00617 00618 /* 00619 * Write the sequence of codes that describes 00620 * the specified span of zero's or one's. The 00621 * appropriate table that holds the make-up and 00622 * terminating codes is supplied. 00623 */ 00624 static void 00625 putspan(TIFF* tif, int32 span, const tableentry* tab) 00626 { 00627 Fax3CodecState* sp = EncoderState(tif); 00628 unsigned int bit = sp->bit; 00629 int data = sp->data; 00630 unsigned int code, length; 00631 00632 while (span >= 2624) { 00633 const tableentry* te = &tab[63 + (2560>>6)]; 00634 code = te->code, length = te->length; 00635 #ifdef FAX3_DEBUG 00636 DEBUG_PRINT("MakeUp", te->runlen); 00637 #endif 00638 _PutBits(tif, code, length); 00639 span -= te->runlen; 00640 } 00641 if (span >= 64) { 00642 const tableentry* te = &tab[63 + (span>>6)]; 00643 assert(te->runlen == 64*(span>>6)); 00644 code = te->code, length = te->length; 00645 #ifdef FAX3_DEBUG 00646 DEBUG_PRINT("MakeUp", te->runlen); 00647 #endif 00648 _PutBits(tif, code, length); 00649 span -= te->runlen; 00650 } 00651 code = tab[span].code, length = tab[span].length; 00652 #ifdef FAX3_DEBUG 00653 DEBUG_PRINT(" Term", tab[span].runlen); 00654 #endif 00655 _PutBits(tif, code, length); 00656 00657 sp->data = data; 00658 sp->bit = bit; 00659 } 00660 00661 /* 00662 * Write an EOL code to the output stream. The zero-fill 00663 * logic for byte-aligning encoded scanlines is handled 00664 * here. We also handle writing the tag bit for the next 00665 * scanline when doing 2d encoding. 00666 */ 00667 static void 00668 Fax3PutEOL(TIFF* tif) 00669 { 00670 Fax3CodecState* sp = EncoderState(tif); 00671 unsigned int bit = sp->bit; 00672 int data = sp->data; 00673 unsigned int code, length, tparm; 00674 00675 if (sp->b.groupoptions & GROUP3OPT_FILLBITS) { 00676 /* 00677 * Force bit alignment so EOL will terminate on 00678 * a byte boundary. That is, force the bit alignment 00679 * to 16-12 = 4 before putting out the EOL code. 00680 */ 00681 int align = 8 - 4; 00682 if (align != sp->bit) { 00683 if (align > sp->bit) 00684 align = sp->bit + (8 - align); 00685 else 00686 align = sp->bit - align; 00687 code = 0; 00688 tparm=align; 00689 _PutBits(tif, 0, tparm); 00690 } 00691 } 00692 code = EOL, length = 12; 00693 if (is2DEncoding(sp)) 00694 code = (code<<1) | (sp->tag == G3_1D), length++; 00695 _PutBits(tif, code, length); 00696 00697 sp->data = data; 00698 sp->bit = bit; 00699 } 00700 00701 /* 00702 * Reset encoding state at the start of a strip. 00703 */ 00704 static int 00705 Fax3PreEncode(TIFF* tif, tsample_t s) 00706 { 00707 Fax3CodecState* sp = EncoderState(tif); 00708 00709 (void) s; 00710 assert(sp != NULL); 00711 sp->bit = 8; 00712 sp->data = 0; 00713 sp->tag = G3_1D; 00714 /* 00715 * This is necessary for Group 4; otherwise it isn't 00716 * needed because the first scanline of each strip ends 00717 * up being copied into the refline. 00718 */ 00719 if (sp->refline) 00720 _TIFFmemset(sp->refline, 0x00, sp->b.rowbytes); 00721 if (is2DEncoding(sp)) { 00722 float res = tif->tif_dir.td_yresolution; 00723 /* 00724 * The CCITT spec says that when doing 2d encoding, you 00725 * should only do it on K consecutive scanlines, where K 00726 * depends on the resolution of the image being encoded 00727 * (2 for <= 200 lpi, 4 for > 200 lpi). Since the directory 00728 * code initializes td_yresolution to 0, this code will 00729 * select a K of 2 unless the YResolution tag is set 00730 * appropriately. (Note also that we fudge a little here 00731 * and use 150 lpi to avoid problems with units conversion.) 00732 */ 00733 if (tif->tif_dir.td_resolutionunit == RESUNIT_CENTIMETER) 00734 res *= 2.54f; /* convert to inches */ 00735 sp->maxk = (res > 150 ? 4 : 2); 00736 sp->k = sp->maxk-1; 00737 } else 00738 sp->k = sp->maxk = 0; 00739 sp->line = 0; 00740 return (1); 00741 } 00742 00743 static const unsigned char zeroruns[256] = { 00744 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0f */ 00745 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */ 00746 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2f */ 00747 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3f */ 00748 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */ 00749 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5f */ 00750 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */ 00751 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7f */ 00752 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8f */ 00753 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9f */ 00754 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 - 0xaf */ 00755 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 - 0xbf */ 00756 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 - 0xcf */ 00757 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 - 0xdf */ 00758 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 - 0xef */ 00759 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xf0 - 0xff */ 00760 }; 00761 static const unsigned char oneruns[256] = { 00762 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 - 0x0f */ 00763 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */ 00764 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 - 0x2f */ 00765 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */ 00766 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */ 00767 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */ 00768 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6f */ 00769 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7f */ 00770 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x80 - 0x8f */ 00771 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x90 - 0x9f */ 00772 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xa0 - 0xaf */ 00773 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xb0 - 0xbf */ 00774 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xc0 - 0xcf */ 00775 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xd0 - 0xdf */ 00776 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */ 00777 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, /* 0xf0 - 0xff */ 00778 }; 00779 00780 /* 00781 * On certain systems it pays to inline 00782 * the routines that find pixel spans. 00783 */ 00784 #ifdef VAXC 00785 static int32 find0span(unsigned char*, int32, int32); 00786 static int32 find1span(unsigned char*, int32, int32); 00787 #pragma inline(find0span,find1span) 00788 #endif 00789 00790 /* 00791 * Find a span of ones or zeros using the supplied 00792 * table. The ``base'' of the bit string is supplied 00793 * along with the start+end bit indices. 00794 */ 00795 static int32 00796 find0span(unsigned char* bp, int32 bs, int32 be) 00797 { 00798 int32 bits = be - bs; 00799 int32 n, span; 00800 00801 bp += bs>>3; 00802 /* 00803 * Check partial byte on lhs. 00804 */ 00805 if (bits > 0 && (n = (bs & 7))) { 00806 span = zeroruns[(*bp << n) & 0xff]; 00807 if (span > 8-n) /* table value too generous */ 00808 span = 8-n; 00809 if (span > bits) /* constrain span to bit range */ 00810 span = bits; 00811 if (n+span < 8) /* doesn't extend to edge of byte */ 00812 return (span); 00813 bits -= span; 00814 bp++; 00815 } else 00816 span = 0; 00817 if (bits >= (int32)(2 * 8 * sizeof(long))) { 00818 long* lp; 00819 /* 00820 * Align to longword boundary and check longwords. 00821 */ 00822 while (!isAligned(bp, long)) { 00823 if (*bp != 0x00) 00824 return (span + zeroruns[*bp]); 00825 span += 8, bits -= 8; 00826 bp++; 00827 } 00828 lp = (long*) bp; 00829 while ((bits >= (int32)(8 * sizeof(long))) && (0 == *lp)) { 00830 span += 8*sizeof (long), bits -= 8*sizeof (long); 00831 lp++; 00832 } 00833 bp = (unsigned char*) lp; 00834 } 00835 /* 00836 * Scan full bytes for all 0's. 00837 */ 00838 while (bits >= 8) { 00839 if (*bp != 0x00) /* end of run */ 00840 return (span + zeroruns[*bp]); 00841 span += 8, bits -= 8; 00842 bp++; 00843 } 00844 /* 00845 * Check partial byte on rhs. 00846 */ 00847 if (bits > 0) { 00848 n = zeroruns[*bp]; 00849 span += (n > bits ? bits : n); 00850 } 00851 return (span); 00852 } 00853 00854 static int32 00855 find1span(unsigned char* bp, int32 bs, int32 be) 00856 { 00857 int32 bits = be - bs; 00858 int32 n, span; 00859 00860 bp += bs>>3; 00861 /* 00862 * Check partial byte on lhs. 00863 */ 00864 if (bits > 0 && (n = (bs & 7))) { 00865 span = oneruns[(*bp << n) & 0xff]; 00866 if (span > 8-n) /* table value too generous */ 00867 span = 8-n; 00868 if (span > bits) /* constrain span to bit range */ 00869 span = bits; 00870 if (n+span < 8) /* doesn't extend to edge of byte */ 00871 return (span); 00872 bits -= span; 00873 bp++; 00874 } else 00875 span = 0; 00876 if (bits >= (int32)(2 * 8 * sizeof(long))) { 00877 long* lp; 00878 /* 00879 * Align to longword boundary and check longwords. 00880 */ 00881 while (!isAligned(bp, long)) { 00882 if (*bp != 0xff) 00883 return (span + oneruns[*bp]); 00884 span += 8, bits -= 8; 00885 bp++; 00886 } 00887 lp = (long*) bp; 00888 while ((bits >= (int32)(8 * sizeof(long))) && (~0 == *lp)) { 00889 span += 8*sizeof (long), bits -= 8*sizeof (long); 00890 lp++; 00891 } 00892 bp = (unsigned char*) lp; 00893 } 00894 /* 00895 * Scan full bytes for all 1's. 00896 */ 00897 while (bits >= 8) { 00898 if (*bp != 0xff) /* end of run */ 00899 return (span + oneruns[*bp]); 00900 span += 8, bits -= 8; 00901 bp++; 00902 } 00903 /* 00904 * Check partial byte on rhs. 00905 */ 00906 if (bits > 0) { 00907 n = oneruns[*bp]; 00908 span += (n > bits ? bits : n); 00909 } 00910 return (span); 00911 } 00912 00913 /* 00914 * Return the offset of the next bit in the range 00915 * [bs..be] that is different from the specified 00916 * color. The end, be, is returned if no such bit 00917 * exists. 00918 */ 00919 #define finddiff(_cp, _bs, _be, _color) \ 00920 (_bs + (_color ? find1span(_cp,_bs,_be) : find0span(_cp,_bs,_be))) 00921 /* 00922 * Like finddiff, but also check the starting bit 00923 * against the end in case start > end. 00924 */ 00925 #define finddiff2(_cp, _bs, _be, _color) \ 00926 (_bs < _be ? finddiff(_cp,_bs,_be,_color) : _be) 00927 00928 /* 00929 * 1d-encode a row of pixels. The encoding is 00930 * a sequence of all-white or all-black spans 00931 * of pixels encoded with Huffman codes. 00932 */ 00933 static int 00934 Fax3Encode1DRow(TIFF* tif, unsigned char* bp, uint32 bits) 00935 { 00936 Fax3CodecState* sp = EncoderState(tif); 00937 int32 span; 00938 uint32 bs = 0; 00939 00940 for (;;) { 00941 span = find0span(bp, bs, bits); /* white span */ 00942 putspan(tif, span, TIFFFaxWhiteCodes); 00943 bs += span; 00944 if (bs >= bits) 00945 break; 00946 span = find1span(bp, bs, bits); /* black span */ 00947 putspan(tif, span, TIFFFaxBlackCodes); 00948 bs += span; 00949 if (bs >= bits) 00950 break; 00951 } 00952 if (sp->b.mode & (FAXMODE_BYTEALIGN|FAXMODE_WORDALIGN)) { 00953 if (sp->bit != 8) /* byte-align */ 00954 Fax3FlushBits(tif, sp); 00955 if ((sp->b.mode&FAXMODE_WORDALIGN) && 00956 !isAligned(tif->tif_rawcp, uint16)) 00957 Fax3FlushBits(tif, sp); 00958 } 00959 return (1); 00960 } 00961 00962 static const tableentry horizcode = 00963 { 3, 0x1, 0 }; /* 001 */ 00964 static const tableentry passcode = 00965 { 4, 0x1, 0 }; /* 0001 */ 00966 static const tableentry vcodes[7] = { 00967 { 7, 0x03, 0 }, /* 0000 011 */ 00968 { 6, 0x03, 0 }, /* 0000 11 */ 00969 { 3, 0x03, 0 }, /* 011 */ 00970 { 1, 0x1, 0 }, /* 1 */ 00971 { 3, 0x2, 0 }, /* 010 */ 00972 { 6, 0x02, 0 }, /* 0000 10 */ 00973 { 7, 0x02, 0 } /* 0000 010 */ 00974 }; 00975 00976 /* 00977 * 2d-encode a row of pixels. Consult the CCITT 00978 * documentation for the algorithm. 00979 */ 00980 static int 00981 Fax3Encode2DRow(TIFF* tif, unsigned char* bp, unsigned char* rp, uint32 bits) 00982 { 00983 #define PIXEL(buf,ix) ((((buf)[(ix)>>3]) >> (7-((ix)&7))) & 1) 00984 uint32 a0 = 0; 00985 uint32 a1 = (PIXEL(bp, 0) != 0 ? 0 : finddiff(bp, 0, bits, 0)); 00986 uint32 b1 = (PIXEL(rp, 0) != 0 ? 0 : finddiff(rp, 0, bits, 0)); 00987 uint32 a2, b2; 00988 00989 for (;;) { 00990 b2 = finddiff2(rp, b1, bits, PIXEL(rp,b1)); 00991 if (b2 >= a1) { 00992 int32 d = b1 - a1; 00993 if (!(-3 <= d && d <= 3)) { /* horizontal mode */ 00994 a2 = finddiff2(bp, a1, bits, PIXEL(bp,a1)); 00995 putcode(tif, &horizcode); 00996 if (a0+a1 == 0 || PIXEL(bp, a0) == 0) { 00997 putspan(tif, a1-a0, TIFFFaxWhiteCodes); 00998 putspan(tif, a2-a1, TIFFFaxBlackCodes); 00999 } else { 01000 putspan(tif, a1-a0, TIFFFaxBlackCodes); 01001 putspan(tif, a2-a1, TIFFFaxWhiteCodes); 01002 } 01003 a0 = a2; 01004 } else { /* vertical mode */ 01005 putcode(tif, &vcodes[d+3]); 01006 a0 = a1; 01007 } 01008 } else { /* pass mode */ 01009 putcode(tif, &passcode); 01010 a0 = b2; 01011 } 01012 if (a0 >= bits) 01013 break; 01014 a1 = finddiff(bp, a0, bits, PIXEL(bp,a0)); 01015 b1 = finddiff(rp, a0, bits, !PIXEL(bp,a0)); 01016 b1 = finddiff(rp, b1, bits, PIXEL(bp,a0)); 01017 } 01018 return (1); 01019 #undef PIXEL 01020 } 01021 01022 /* 01023 * Encode a buffer of pixels. 01024 */ 01025 static int 01026 Fax3Encode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s) 01027 { 01028 Fax3CodecState* sp = EncoderState(tif); 01029 01030 (void) s; 01031 while ((long)cc > 0) { 01032 if ((sp->b.mode & FAXMODE_NOEOL) == 0) 01033 Fax3PutEOL(tif); 01034 if (is2DEncoding(sp)) { 01035 if (sp->tag == G3_1D) { 01036 if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels)) 01037 return (0); 01038 sp->tag = G3_2D; 01039 } else { 01040 if (!Fax3Encode2DRow(tif, bp, sp->refline, 01041 sp->b.rowpixels)) 01042 return (0); 01043 sp->k--; 01044 } 01045 if (sp->k == 0) { 01046 sp->tag = G3_1D; 01047 sp->k = sp->maxk-1; 01048 } else 01049 _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes); 01050 } else { 01051 if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels)) 01052 return (0); 01053 } 01054 bp += sp->b.rowbytes; 01055 cc -= sp->b.rowbytes; 01056 } 01057 return (1); 01058 } 01059 01060 static int 01061 Fax3PostEncode(TIFF* tif) 01062 { 01063 Fax3CodecState* sp = EncoderState(tif); 01064 01065 if (sp->bit != 8) 01066 Fax3FlushBits(tif, sp); 01067 return (1); 01068 } 01069 01070 static void 01071 Fax3Close(TIFF* tif) 01072 { 01073 if ((Fax3State(tif)->mode & FAXMODE_NORTC) == 0) { 01074 Fax3CodecState* sp = EncoderState(tif); 01075 unsigned int code = EOL; 01076 unsigned int length = 12; 01077 int i; 01078 01079 if (is2DEncoding(sp)) 01080 code = (code<<1) | (sp->tag == G3_1D), length++; 01081 for (i = 0; i < 6; i++) 01082 Fax3PutBits(tif, code, length); 01083 Fax3FlushBits(tif, sp); 01084 } 01085 } 01086 01087 static void 01088 Fax3Cleanup(TIFF* tif) 01089 { 01090 Fax3CodecState* sp = DecoderState(tif); 01091 01092 assert(sp != 0); 01093 01094 tif->tif_tagmethods.vgetfield = sp->b.vgetparent; 01095 tif->tif_tagmethods.vsetfield = sp->b.vsetparent; 01096 tif->tif_tagmethods.printdir = sp->b.printdir; 01097 01098 if (sp->runs) 01099 _TIFFfree(sp->runs); 01100 if (sp->refline) 01101 _TIFFfree(sp->refline); 01102 01103 if (Fax3State(tif)->subaddress) 01104 _TIFFfree(Fax3State(tif)->subaddress); 01105 if (Fax3State(tif)->faxdcs) 01106 _TIFFfree(Fax3State(tif)->faxdcs); 01107 01108 _TIFFfree(tif->tif_data); 01109 tif->tif_data = NULL; 01110 01111 _TIFFSetDefaultCompressionState(tif); 01112 } 01113 01114 #define FIELD_BADFAXLINES (FIELD_CODEC+0) 01115 #define FIELD_CLEANFAXDATA (FIELD_CODEC+1) 01116 #define FIELD_BADFAXRUN (FIELD_CODEC+2) 01117 #define FIELD_RECVPARAMS (FIELD_CODEC+3) 01118 #define FIELD_SUBADDRESS (FIELD_CODEC+4) 01119 #define FIELD_RECVTIME (FIELD_CODEC+5) 01120 #define FIELD_FAXDCS (FIELD_CODEC+6) 01121 01122 #define FIELD_OPTIONS (FIELD_CODEC+7) 01123 01124 static const TIFFFieldInfo faxFieldInfo[] = { 01125 { TIFFTAG_FAXMODE, 0, 0, TIFF_ANY, FIELD_PSEUDO, 01126 FALSE, FALSE, "FaxMode" }, 01127 { TIFFTAG_FAXFILLFUNC, 0, 0, TIFF_ANY, FIELD_PSEUDO, 01128 FALSE, FALSE, "FaxFillFunc" }, 01129 { TIFFTAG_BADFAXLINES, 1, 1, TIFF_LONG, FIELD_BADFAXLINES, 01130 TRUE, FALSE, "BadFaxLines" }, 01131 { TIFFTAG_BADFAXLINES, 1, 1, TIFF_SHORT, FIELD_BADFAXLINES, 01132 TRUE, FALSE, "BadFaxLines" }, 01133 { TIFFTAG_CLEANFAXDATA, 1, 1, TIFF_SHORT, FIELD_CLEANFAXDATA, 01134 TRUE, FALSE, "CleanFaxData" }, 01135 { TIFFTAG_CONSECUTIVEBADFAXLINES,1,1, TIFF_LONG, FIELD_BADFAXRUN, 01136 TRUE, FALSE, "ConsecutiveBadFaxLines" }, 01137 { TIFFTAG_CONSECUTIVEBADFAXLINES,1,1, TIFF_SHORT, FIELD_BADFAXRUN, 01138 TRUE, FALSE, "ConsecutiveBadFaxLines" }, 01139 { TIFFTAG_FAXRECVPARAMS, 1, 1, TIFF_LONG, FIELD_RECVPARAMS, 01140 TRUE, FALSE, "FaxRecvParams" }, 01141 { TIFFTAG_FAXSUBADDRESS, -1,-1, TIFF_ASCII, FIELD_SUBADDRESS, 01142 TRUE, FALSE, "FaxSubAddress" }, 01143 { TIFFTAG_FAXRECVTIME, 1, 1, TIFF_LONG, FIELD_RECVTIME, 01144 TRUE, FALSE, "FaxRecvTime" }, 01145 { TIFFTAG_FAXDCS, -1,-1, TIFF_ASCII, FIELD_FAXDCS, 01146 TRUE, FALSE, "FaxDcs" }, 01147 }; 01148 static const TIFFFieldInfo fax3FieldInfo[] = { 01149 { TIFFTAG_GROUP3OPTIONS, 1, 1, TIFF_LONG, FIELD_OPTIONS, 01150 FALSE, FALSE, "Group3Options" }, 01151 }; 01152 static const TIFFFieldInfo fax4FieldInfo[] = { 01153 { TIFFTAG_GROUP4OPTIONS, 1, 1, TIFF_LONG, FIELD_OPTIONS, 01154 FALSE, FALSE, "Group4Options" }, 01155 }; 01156 #define N(a) (sizeof (a) / sizeof (a[0])) 01157 01158 static int 01159 Fax3VSetField(TIFF* tif, ttag_t tag, va_list ap) 01160 { 01161 Fax3BaseState* sp = Fax3State(tif); 01162 const TIFFFieldInfo* fip; 01163 01164 assert(sp != 0); 01165 assert(sp->vsetparent != 0); 01166 01167 switch (tag) { 01168 case TIFFTAG_FAXMODE: 01169 sp->mode = va_arg(ap, int); 01170 return 1; /* NB: pseudo tag */ 01171 case TIFFTAG_FAXFILLFUNC: 01172 DecoderState(tif)->fill = va_arg(ap, TIFFFaxFillFunc); 01173 return 1; /* NB: pseudo tag */ 01174 case TIFFTAG_GROUP3OPTIONS: 01175 /* XXX: avoid reading options if compression mismatches. */ 01176 if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX3) 01177 sp->groupoptions = va_arg(ap, uint32); 01178 break; 01179 case TIFFTAG_GROUP4OPTIONS: 01180 /* XXX: avoid reading options if compression mismatches. */ 01181 if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4) 01182 sp->groupoptions = va_arg(ap, uint32); 01183 break; 01184 case TIFFTAG_BADFAXLINES: 01185 sp->badfaxlines = va_arg(ap, uint32); 01186 break; 01187 case TIFFTAG_CLEANFAXDATA: 01188 sp->cleanfaxdata = (uint16) va_arg(ap, int); 01189 break; 01190 case TIFFTAG_CONSECUTIVEBADFAXLINES: 01191 sp->badfaxrun = va_arg(ap, uint32); 01192 break; 01193 case TIFFTAG_FAXRECVPARAMS: 01194 sp->recvparams = va_arg(ap, uint32); 01195 break; 01196 case TIFFTAG_FAXSUBADDRESS: 01197 _TIFFsetString(&sp->subaddress, va_arg(ap, char*)); 01198 break; 01199 case TIFFTAG_FAXRECVTIME: 01200 sp->recvtime = va_arg(ap, uint32); 01201 break; 01202 case TIFFTAG_FAXDCS: 01203 _TIFFsetString(&sp->faxdcs, va_arg(ap, char*)); 01204 break; 01205 default: 01206 return (*sp->vsetparent)(tif, tag, ap); 01207 } 01208 01209 if ((fip = _TIFFFieldWithTag(tif, tag))) 01210 TIFFSetFieldBit(tif, fip->field_bit); 01211 else 01212 return 0; 01213 01214 tif->tif_flags |= TIFF_DIRTYDIRECT; 01215 return 1; 01216 } 01217 01218 static int 01219 Fax3VGetField(TIFF* tif, ttag_t tag, va_list ap) 01220 { 01221 Fax3BaseState* sp = Fax3State(tif); 01222 01223 assert(sp != 0); 01224 01225 switch (tag) { 01226 case TIFFTAG_FAXMODE: 01227 *va_arg(ap, int*) = sp->mode; 01228 break; 01229 case TIFFTAG_FAXFILLFUNC: 01230 *va_arg(ap, TIFFFaxFillFunc*) = DecoderState(tif)->fill; 01231 break; 01232 case TIFFTAG_GROUP3OPTIONS: 01233 case TIFFTAG_GROUP4OPTIONS: 01234 *va_arg(ap, uint32*) = sp->groupoptions; 01235 break; 01236 case TIFFTAG_BADFAXLINES: 01237 *va_arg(ap, uint32*) = sp->badfaxlines; 01238 break; 01239 case TIFFTAG_CLEANFAXDATA: 01240 *va_arg(ap, uint16*) = sp->cleanfaxdata; 01241 break; 01242 case TIFFTAG_CONSECUTIVEBADFAXLINES: 01243 *va_arg(ap, uint32*) = sp->badfaxrun; 01244 break; 01245 case TIFFTAG_FAXRECVPARAMS: 01246 *va_arg(ap, uint32*) = sp->recvparams; 01247 break; 01248 case TIFFTAG_FAXSUBADDRESS: 01249 *va_arg(ap, char**) = sp->subaddress; 01250 break; 01251 case TIFFTAG_FAXRECVTIME: 01252 *va_arg(ap, uint32*) = sp->recvtime; 01253 break; 01254 case TIFFTAG_FAXDCS: 01255 *va_arg(ap, char**) = sp->faxdcs; 01256 break; 01257 default: 01258 return (*sp->vgetparent)(tif, tag, ap); 01259 } 01260 return (1); 01261 } 01262 01263 static void 01264 Fax3PrintDir(TIFF* tif, FILE* fd, long flags) 01265 { 01266 Fax3BaseState* sp = Fax3State(tif); 01267 01268 assert(sp != 0); 01269 01270 (void) flags; 01271 if (TIFFFieldSet(tif,FIELD_OPTIONS)) { 01272 const char* sep = " "; 01273 if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4) { 01274 fprintf(fd, " Group 4 Options:"); 01275 if (sp->groupoptions & GROUP4OPT_UNCOMPRESSED) 01276 fprintf(fd, "%suncompressed data", sep); 01277 } else { 01278 01279 fprintf(fd, " Group 3 Options:"); 01280 if (sp->groupoptions & GROUP3OPT_2DENCODING) 01281 fprintf(fd, "%s2-d encoding", sep), sep = "+"; 01282 if (sp->groupoptions & GROUP3OPT_FILLBITS) 01283 fprintf(fd, "%sEOL padding", sep), sep = "+"; 01284 if (sp->groupoptions & GROUP3OPT_UNCOMPRESSED) 01285 fprintf(fd, "%suncompressed data", sep); 01286 } 01287 fprintf(fd, " (%lu = 0x%lx)\n", 01288 (unsigned long) sp->groupoptions, 01289 (unsigned long) sp->groupoptions); 01290 } 01291 if (TIFFFieldSet(tif,FIELD_CLEANFAXDATA)) { 01292 fprintf(fd, " Fax Data:"); 01293 switch (sp->cleanfaxdata) { 01294 case CLEANFAXDATA_CLEAN: 01295 fprintf(fd, " clean"); 01296 break; 01297 case CLEANFAXDATA_REGENERATED: 01298 fprintf(fd, " receiver regenerated"); 01299 break; 01300 case CLEANFAXDATA_UNCLEAN: 01301 fprintf(fd, " uncorrected errors"); 01302 break; 01303 } 01304 fprintf(fd, " (%u = 0x%x)\n", 01305 sp->cleanfaxdata, sp->cleanfaxdata); 01306 } 01307 if (TIFFFieldSet(tif,FIELD_BADFAXLINES)) 01308 fprintf(fd, " Bad Fax Lines: %lu\n", 01309 (unsigned long) sp->badfaxlines); 01310 if (TIFFFieldSet(tif,FIELD_BADFAXRUN)) 01311 fprintf(fd, " Consecutive Bad Fax Lines: %lu\n", 01312 (unsigned long) sp->badfaxrun); 01313 if (TIFFFieldSet(tif,FIELD_RECVPARAMS)) 01314 fprintf(fd, " Fax Receive Parameters: %08lx\n", 01315 (unsigned long) sp->recvparams); 01316 if (TIFFFieldSet(tif,FIELD_SUBADDRESS)) 01317 fprintf(fd, " Fax SubAddress: %s\n", sp->subaddress); 01318 if (TIFFFieldSet(tif,FIELD_RECVTIME)) 01319 fprintf(fd, " Fax Receive Time: %lu secs\n", 01320 (unsigned long) sp->recvtime); 01321 if (TIFFFieldSet(tif,FIELD_FAXDCS)) 01322 fprintf(fd, " Fax DCS: %s\n", sp->faxdcs); 01323 } 01324 01325 static int 01326 InitCCITTFax3(TIFF* tif) 01327 { 01328 Fax3BaseState* sp; 01329 01330 /* 01331 * Merge codec-specific tag information. 01332 */ 01333 if (!_TIFFMergeFieldInfo(tif, faxFieldInfo, N(faxFieldInfo))) { 01334 TIFFErrorExt(tif->tif_clientdata, "InitCCITTFax3", 01335 "Merging common CCITT Fax codec-specific tags failed"); 01336 return 0; 01337 } 01338 01339 /* 01340 * Allocate state block so tag methods have storage to record values. 01341 */ 01342 tif->tif_data = (tidata_t) 01343 _TIFFmalloc(sizeof (Fax3CodecState)); 01344 01345 if (tif->tif_data == NULL) { 01346 TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax3", 01347 "%s: No space for state block", tif->tif_name); 01348 return (0); 01349 } 01350 01351 sp = Fax3State(tif); 01352 sp->rw_mode = tif->tif_mode; 01353 01354 /* 01355 * Override parent get/set field methods. 01356 */ 01357 sp->vgetparent = tif->tif_tagmethods.vgetfield; 01358 tif->tif_tagmethods.vgetfield = Fax3VGetField; /* hook for codec tags */ 01359 sp->vsetparent = tif->tif_tagmethods.vsetfield; 01360 tif->tif_tagmethods.vsetfield = Fax3VSetField; /* hook for codec tags */ 01361 sp->printdir = tif->tif_tagmethods.printdir; 01362 tif->tif_tagmethods.printdir = Fax3PrintDir; /* hook for codec tags */ 01363 sp->groupoptions = 0; 01364 sp->recvparams = 0; 01365 sp->subaddress = NULL; 01366 sp->faxdcs = NULL; 01367 01368 if (sp->rw_mode == O_RDONLY) /* FIXME: improve for in place update */ 01369 tif->tif_flags |= TIFF_NOBITREV; /* decoder does bit reversal */ 01370 DecoderState(tif)->runs = NULL; 01371 TIFFSetField(tif, TIFFTAG_FAXFILLFUNC, _TIFFFax3fillruns); 01372 EncoderState(tif)->refline = NULL; 01373 01374 /* 01375 * Install codec methods. 01376 */ 01377 tif->tif_setupdecode = Fax3SetupState; 01378 tif->tif_predecode = Fax3PreDecode; 01379 tif->tif_decoderow = Fax3Decode1D; 01380 tif->tif_decodestrip = Fax3Decode1D; 01381 tif->tif_decodetile = Fax3Decode1D; 01382 tif->tif_setupencode = Fax3SetupState; 01383 tif->tif_preencode = Fax3PreEncode; 01384 tif->tif_postencode = Fax3PostEncode; 01385 tif->tif_encoderow = Fax3Encode; 01386 tif->tif_encodestrip = Fax3Encode; 01387 tif->tif_encodetile = Fax3Encode; 01388 tif->tif_close = Fax3Close; 01389 tif->tif_cleanup = Fax3Cleanup; 01390 01391 return (1); 01392 } 01393 01394 int 01395 TIFFInitCCITTFax3(TIFF* tif, int scheme) 01396 { 01397 (void) scheme; 01398 if (InitCCITTFax3(tif)) { 01399 /* 01400 * Merge codec-specific tag information. 01401 */ 01402 if (!_TIFFMergeFieldInfo(tif, fax3FieldInfo, N(fax3FieldInfo))) { 01403 TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax3", 01404 "Merging CCITT Fax 3 codec-specific tags failed"); 01405 return 0; 01406 } 01407 01408 /* 01409 * The default format is Class/F-style w/o RTC. 01410 */ 01411 return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_CLASSF); 01412 } else 01413 return 01; 01414 } 01415 01416 /* 01417 * CCITT Group 4 (T.6) Facsimile-compatible 01418 * Compression Scheme Support. 01419 */ 01420 01421 #define SWAP(t,a,b) { t x; x = (a); (a) = (b); (b) = x; } 01422 /* 01423 * Decode the requested amount of G4-encoded data. 01424 */ 01425 static int 01426 Fax4Decode(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s) 01427 { 01428 DECLARE_STATE_2D(tif, sp, "Fax4Decode"); 01429 01430 (void) s; 01431 CACHE_STATE(tif, sp); 01432 while ((long)occ > 0) { 01433 a0 = 0; 01434 RunLength = 0; 01435 pa = thisrun = sp->curruns; 01436 pb = sp->refruns; 01437 b1 = *pb++; 01438 #ifdef FAX3_DEBUG 01439 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail); 01440 printf("-------------------- %d\n", tif->tif_row); 01441 fflush(stdout); 01442 #endif 01443 EXPAND2D(EOFG4); 01444 if (EOLcnt) 01445 goto EOFG4; 01446 (*sp->fill)(buf, thisrun, pa, lastx); 01447 SETVALUE(0); /* imaginary change for reference */ 01448 SWAP(uint32*, sp->curruns, sp->refruns); 01449 buf += sp->b.rowbytes; 01450 occ -= sp->b.rowbytes; 01451 sp->line++; 01452 continue; 01453 EOFG4: 01454 NeedBits16( 13, BADG4 ); 01455 BADG4: 01456 #ifdef FAX3_DEBUG 01457 if( GetBits(13) != 0x1001 ) 01458 fputs( "Bad EOFB\n", stderr ); 01459 #endif 01460 ClrBits( 13 ); 01461 (*sp->fill)(buf, thisrun, pa, lastx); 01462 UNCACHE_STATE(tif, sp); 01463 return ( sp->line ? 1 : -1); /* don't error on badly-terminated strips */ 01464 } 01465 UNCACHE_STATE(tif, sp); 01466 return (1); 01467 } 01468 #undef SWAP 01469 01470 /* 01471 * Encode the requested amount of data. 01472 */ 01473 static int 01474 Fax4Encode(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s) 01475 { 01476 Fax3CodecState *sp = EncoderState(tif); 01477 01478 (void) s; 01479 while ((long)cc > 0) { 01480 if (!Fax3Encode2DRow(tif, bp, sp->refline, sp->b.rowpixels)) 01481 return (0); 01482 _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes); 01483 bp += sp->b.rowbytes; 01484 cc -= sp->b.rowbytes; 01485 } 01486 return (1); 01487 } 01488 01489 static int 01490 Fax4PostEncode(TIFF* tif) 01491 { 01492 Fax3CodecState *sp = EncoderState(tif); 01493 01494 /* terminate strip w/ EOFB */ 01495 Fax3PutBits(tif, EOL, 12); 01496 Fax3PutBits(tif, EOL, 12); 01497 if (sp->bit != 8) 01498 Fax3FlushBits(tif, sp); 01499 return (1); 01500 } 01501 01502 int 01503 TIFFInitCCITTFax4(TIFF* tif, int scheme) 01504 { 01505 (void) scheme; 01506 if (InitCCITTFax3(tif)) { /* reuse G3 support */ 01507 /* 01508 * Merge codec-specific tag information. 01509 */ 01510 if (!_TIFFMergeFieldInfo(tif, fax4FieldInfo, N(fax4FieldInfo))) { 01511 TIFFErrorExt(tif->tif_clientdata, "TIFFInitCCITTFax4", 01512 "Merging CCITT Fax 4 codec-specific tags failed"); 01513 return 0; 01514 } 01515 01516 tif->tif_decoderow = Fax4Decode; 01517 tif->tif_decodestrip = Fax4Decode; 01518 tif->tif_decodetile = Fax4Decode; 01519 tif->tif_encoderow = Fax4Encode; 01520 tif->tif_encodestrip = Fax4Encode; 01521 tif->tif_encodetile = Fax4Encode; 01522 tif->tif_postencode = Fax4PostEncode; 01523 /* 01524 * Suppress RTC at the end of each strip. 01525 */ 01526 return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_NORTC); 01527 } else 01528 return (0); 01529 } 01530 01531 /* 01532 * CCITT Group 3 1-D Modified Huffman RLE Compression Support. 01533 * (Compression algorithms 2 and 32771) 01534 */ 01535 01536 /* 01537 * Decode the requested amount of RLE-encoded data. 01538 */ 01539 static int 01540 Fax3DecodeRLE(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s) 01541 { 01542 DECLARE_STATE(tif, sp, "Fax3DecodeRLE"); 01543 int mode = sp->b.mode; 01544 01545 (void) s; 01546 CACHE_STATE(tif, sp); 01547 thisrun = sp->curruns; 01548 while ((long)occ > 0) { 01549 a0 = 0; 01550 RunLength = 0; 01551 pa = thisrun; 01552 #ifdef FAX3_DEBUG 01553 printf("\nBitAcc=%08X, BitsAvail = %d\n", BitAcc, BitsAvail); 01554 printf("-------------------- %d\n", tif->tif_row); 01555 fflush(stdout); 01556 #endif 01557 EXPAND1D(EOFRLE); 01558 (*sp->fill)(buf, thisrun, pa, lastx); 01559 /* 01560 * Cleanup at the end of the row. 01561 */ 01562 if (mode & FAXMODE_BYTEALIGN) { 01563 int n = BitsAvail - (BitsAvail &~ 7); 01564 ClrBits(n); 01565 } else if (mode & FAXMODE_WORDALIGN) { 01566 int n = BitsAvail - (BitsAvail &~ 15); 01567 ClrBits(n); 01568 if (BitsAvail == 0 && !isAligned(cp, uint16)) 01569 cp++; 01570 } 01571 buf += sp->b.rowbytes; 01572 occ -= sp->b.rowbytes; 01573 sp->line++; 01574 continue; 01575 EOFRLE: /* premature EOF */ 01576 (*sp->fill)(buf, thisrun, pa, lastx); 01577 UNCACHE_STATE(tif, sp); 01578 return (-1); 01579 } 01580 UNCACHE_STATE(tif, sp); 01581 return (1); 01582 } 01583 01584 int 01585 TIFFInitCCITTRLE(TIFF* tif, int scheme) 01586 { 01587 (void) scheme; 01588 if (InitCCITTFax3(tif)) { /* reuse G3 support */ 01589 tif->tif_decoderow = Fax3DecodeRLE; 01590 tif->tif_decodestrip = Fax3DecodeRLE; 01591 tif->tif_decodetile = Fax3DecodeRLE; 01592 /* 01593 * Suppress RTC+EOLs when encoding and byte-align data. 01594 */ 01595 return TIFFSetField(tif, TIFFTAG_FAXMODE, 01596 FAXMODE_NORTC|FAXMODE_NOEOL|FAXMODE_BYTEALIGN); 01597 } else 01598 return (0); 01599 } 01600 01601 int 01602 TIFFInitCCITTRLEW(TIFF* tif, int scheme) 01603 { 01604 (void) scheme; 01605 if (InitCCITTFax3(tif)) { /* reuse G3 support */ 01606 tif->tif_decoderow = Fax3DecodeRLE; 01607 tif->tif_decodestrip = Fax3DecodeRLE; 01608 tif->tif_decodetile = Fax3DecodeRLE; 01609 /* 01610 * Suppress RTC+EOLs when encoding and word-align data. 01611 */ 01612 return TIFFSetField(tif, TIFFTAG_FAXMODE, 01613 FAXMODE_NORTC|FAXMODE_NOEOL|FAXMODE_WORDALIGN); 01614 } else 01615 return (0); 01616 } 01617 #endif /* CCITT_SUPPORT */ 01618 01619 /* vim: set ts=8 sts=8 sw=8 noet: */ 01620 /* 01621 * Local Variables: 01622 * mode: c 01623 * c-basic-offset: 8 01624 * fill-column: 78 01625 * End: 01626 */ Generated on Sat May 26 2012 04:18:23 for ReactOS by
1.7.6.1
|