static bool TIFFInitLZW(TIFF tif, COMPRESSION scheme) { #if DEBUG if(scheme!=COMPRESSION.LZW) throw new Exception("scheme!=COMPRESSION.LZW"); #endif // Allocate state block so tag methods have storage to record values. LZWCodecState sp=null; try { tif.tif_data=sp=new LZWCodecState(); } catch { TIFFErrorExt(tif.tif_clientdata, "TIFFInitLZW", "No space for LZW state block"); return false; } sp.dec_codetab=null; sp.dec_decode=null; sp.enc_hashtab=null; sp.rw_mode=tif.tif_mode; // Install codec methods. tif.tif_setupdecode=LZWSetupDecode; tif.tif_predecode=LZWPreDecode; tif.tif_decoderow=LZWDecode; tif.tif_decodestrip=LZWDecode; tif.tif_decodetile=LZWDecode; tif.tif_setupencode=LZWSetupEncode; tif.tif_preencode=LZWPreEncode; tif.tif_postencode=LZWPostEncode; tif.tif_encoderow=LZWEncode; tif.tif_encodestrip=LZWEncode; tif.tif_encodetile=LZWEncode; tif.tif_cleanup=LZWCleanup; // Setup predictor setup. TIFFPredictorInit(tif); return true; }
// LZW Decoder. static bool LZWSetupDecode(TIFF tif) { LZWCodecState sp=tif.tif_data as LZWCodecState; string module=" LZWSetupDecode"; if(sp==null) { // Allocate state block so tag methods have storage to record // values. try { tif.tif_data=sp=new LZWCodecState(); } catch { TIFFErrorExt(tif.tif_clientdata, "LZWPreDecode", "No space for LZW state block"); return false; } sp.dec_codetab=null; sp.dec_decode=null; // Setup predictor setup. TIFFPredictorInit(tif); sp=tif.tif_data as LZWCodecState; } #if DEBUG if(sp==null) throw new Exception("sp==null"); #endif if(sp.dec_codetab==null) { try { sp.dec_codetab=new code_t[CSIZE]; for(int i=0; i<CSIZE; i++) { sp.dec_codetab[i]=new code_t(); sp.dec_codetab[i].next=-1; } } catch { TIFFErrorExt(tif.tif_clientdata, module, "No space for LZW code table"); return false; } // Pre-load the table. byte code=255; do { sp.dec_codetab[code].value=code; sp.dec_codetab[code].firstchar=code; sp.dec_codetab[code].length=1; sp.dec_codetab[code].next=-1; } while((code--)!=0); // Zero-out the unused entries for(int i=CODE_CLEAR; i<CODE_FIRST; i++) { sp.dec_codetab[i].next=0; sp.dec_codetab[i].length=0; sp.dec_codetab[i].value=0; sp.dec_codetab[i].firstchar=0; } } return true; }
// Reset encoding hash table. static void cl_hash(LZWCodecState sp) { for(int i=0; i<HSIZE; i++) sp.enc_hashtab[i].hash=-1; }