65 #define MAXCODE(n) ((1L<<(n))-1) 73 #define CODE_CLEAR 256 75 #define CODE_FIRST 258 76 #define CODE_MAX MAXCODE(BITS_MAX) 81 #define CSIZE (MAXCODE(BITS_MAX)+1024L) 83 #define CSIZE (MAXCODE(BITS_MAX)+1L) 95 unsigned short maxcode;
96 unsigned short free_ent;
103 #define lzw_nbits base.nbits 104 #define lzw_maxcode base.maxcode 105 #define lzw_free_ent base.free_ent 106 #define lzw_nextdata base.nextdata 107 #define lzw_nextbits base.nextbits 121 typedef struct code_ent {
122 struct code_ent *next;
125 unsigned char firstchar;
149 #define CHECK_GAP 10000 157 #define LZWState(tif) ((LZWBaseState*) (tif)->tif_data) 158 #define DecoderState(tif) ((LZWCodecState*) LZWState(tif)) 159 #define EncoderState(tif) ((LZWCodecState*) LZWState(tif)) 165 static void cl_hash(LZWCodecState*);
176 #define NextCode(_tif, _sp, _bp, _code, _get) { \ 177 if ((_sp)->dec_bitsleft < nbits) { \ 178 TIFFWarningExt(_tif->tif_clientdata, _tif->tif_name, \ 179 "LZWDecode: Strip %d not terminated with EOI code", \ 180 _tif->tif_curstrip); \ 183 _get(_sp,_bp,_code); \ 184 (_sp)->dec_bitsleft -= nbits; \ 188 #define NextCode(tif, sp, bp, code, get) get(sp, bp, code) 192 LZWSetupDecode(
TIFF* tif)
195 static const char module[] =
" LZWSetupDecode";
224 if (sp->dec_codetab ==
NULL) {
226 if (sp->dec_codetab ==
NULL) {
236 sp->dec_codetab[
code].firstchar =
code;
237 sp->dec_codetab[
code].length = 1;
259 if (!sp->dec_decode) {
261 "Old-style LZW codes, convert file");
278 sp->dec_decode = LZWDecodeCompat;
282 if (!sp->dec_decode) {
284 "Old-style LZW codes not supported");
285 sp->dec_decode = LZWDecode;
291 sp->dec_decode = LZWDecode;
294 sp->lzw_nextbits = 0;
295 sp->lzw_nextdata = 0;
302 sp->dec_free_entp = sp->dec_codetab +
CODE_FIRST;
311 sp->dec_oldcodep = &sp->dec_codetab[-1];
312 sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask-1];
319 #define GetNextCode(sp, bp, code) { \ 320 nextdata = (nextdata<<8) | *(bp)++; \ 322 if (nextbits < nbits) { \ 323 nextdata = (nextdata<<8) | *(bp)++; \ 326 code = (hcode_t)((nextdata >> (nextbits-nbits)) & nbitsmask); \ 334 "LZWDecode: Bogus encoding, loop in the code table; scanline %d",
342 char *op = (
char*) op0;
343 long occ = (
long) occ0;
348 long nbits, nextbits, nextdata, nbitsmask;
349 code_t *codep, *free_entp, *maxcodep, *oldcodep;
356 if (sp->dec_restart) {
359 codep = sp->dec_codep;
360 residue = codep->length - sp->dec_restart;
368 sp->dec_restart += occ;
371 }
while (--residue > occ && codep);
375 *--tp = codep->value;
377 }
while (--occ && codep);
384 op += residue, occ -= residue;
392 }
while (--residue && codep);
397 nbits = sp->lzw_nbits;
398 nextdata = sp->lzw_nextdata;
399 nextbits = sp->lzw_nextbits;
400 nbitsmask = sp->dec_nbitsmask;
401 oldcodep = sp->dec_oldcodep;
402 free_entp = sp->dec_free_entp;
403 maxcodep = sp->dec_maxcodep;
413 maxcodep = sp->dec_codetab + nbitsmask-1;
417 *op++ = (char)code, occ--;
418 oldcodep = sp->dec_codetab +
code;
421 codep = sp->dec_codetab +
code;
426 if (free_entp < &sp->dec_codetab[0] ||
427 free_entp >= &sp->dec_codetab[
CSIZE]) {
429 "LZWDecode: Corrupted LZW table at scanline %d",
434 free_entp->next = oldcodep;
435 if (free_entp->next < &sp->dec_codetab[0] ||
436 free_entp->next >= &sp->dec_codetab[
CSIZE]) {
438 "LZWDecode: Corrupted LZW table at scanline %d",
442 free_entp->firstchar = free_entp->next->firstchar;
443 free_entp->length = free_entp->next->length+1;
444 free_entp->value = (codep < free_entp) ?
445 codep->firstchar : free_entp->firstchar;
446 if (++free_entp > maxcodep) {
450 maxcodep = sp->dec_codetab + nbitsmask-1;
458 if(codep->length == 0) {
460 "LZWDecode: Wrong length of decoded string: " 461 "data probably corrupted at scanline %d",
465 if (codep->length > occ) {
472 sp->dec_codep = codep;
475 }
while (codep && codep->length > occ);
477 sp->dec_restart = occ;
480 *--tp = codep->value;
482 }
while (--occ && codep);
496 }
while (codep && tp > op);
503 *op++ = (char)code, occ--;
507 sp->lzw_nbits = (
unsigned short) nbits;
508 sp->lzw_nextdata = nextdata;
509 sp->lzw_nextbits = nextbits;
510 sp->dec_nbitsmask = nbitsmask;
511 sp->dec_oldcodep = oldcodep;
512 sp->dec_free_entp = free_entp;
513 sp->dec_maxcodep = maxcodep;
517 "LZWDecode: Not enough data at scanline %d (short %d bytes)",
528 #define GetNextCodeCompat(sp, bp, code) { \ 529 nextdata |= (unsigned long) *(bp)++ << nextbits; \ 531 if (nextbits < nbits) { \ 532 nextdata |= (unsigned long) *(bp)++ << nextbits;\ 535 code = (hcode_t)(nextdata & nbitsmask); \ 536 nextdata >>= nbits; \ 544 char *op = (
char*) op0;
545 long occ = (
long) occ0;
549 long nextbits, nextdata, nbitsmask;
550 code_t *codep, *free_entp, *maxcodep, *oldcodep;
557 if (sp->dec_restart) {
560 codep = sp->dec_codep;
561 residue = codep->length - sp->dec_restart;
569 sp->dec_restart += occ;
572 }
while (--residue > occ);
575 *--tp = codep->value;
583 op += residue, occ -= residue;
586 *--tp = codep->value;
593 nbits = sp->lzw_nbits;
594 nextdata = sp->lzw_nextdata;
595 nextbits = sp->lzw_nextbits;
596 nbitsmask = sp->dec_nbitsmask;
597 oldcodep = sp->dec_oldcodep;
598 free_entp = sp->dec_free_entp;
599 maxcodep = sp->dec_maxcodep;
609 maxcodep = sp->dec_codetab + nbitsmask;
614 oldcodep = sp->dec_codetab +
code;
617 codep = sp->dec_codetab +
code;
622 if (free_entp < &sp->dec_codetab[0] ||
623 free_entp >= &sp->dec_codetab[
CSIZE]) {
625 "LZWDecodeCompat: Corrupted LZW table at scanline %d",
630 free_entp->next = oldcodep;
631 if (free_entp->next < &sp->dec_codetab[0] ||
632 free_entp->next >= &sp->dec_codetab[
CSIZE]) {
634 "LZWDecodeCompat: Corrupted LZW table at scanline %d",
638 free_entp->firstchar = free_entp->next->firstchar;
639 free_entp->length = free_entp->next->length+1;
640 free_entp->value = (codep < free_entp) ?
641 codep->firstchar : free_entp->firstchar;
642 if (++free_entp > maxcodep) {
646 maxcodep = sp->dec_codetab + nbitsmask;
654 if(codep->length == 0) {
656 "LZWDecodeCompat: Wrong length of decoded " 657 "string: data probably corrupted at scanline %d",
661 if (codep->length > occ) {
668 sp->dec_codep = codep;
671 }
while (codep->length > occ);
672 sp->dec_restart = occ;
675 *--tp = codep->value;
680 op += codep->length, occ -= codep->length;
683 *--tp = codep->value;
684 }
while( (codep = codep->next) !=
NULL);
690 sp->lzw_nbits =
nbits;
691 sp->lzw_nextdata = nextdata;
692 sp->lzw_nextbits = nextbits;
693 sp->dec_nbitsmask = nbitsmask;
694 sp->dec_oldcodep = oldcodep;
695 sp->dec_free_entp = free_entp;
696 sp->dec_maxcodep = maxcodep;
700 "LZWDecodeCompat: Not enough data at scanline %d (short %d bytes)",
713 LZWSetupEncode(
TIFF* tif)
716 static const char module[] =
"LZWSetupEncode";
720 if (sp->enc_hashtab ==
NULL) {
740 sp->lzw_nextbits = 0;
741 sp->lzw_nextdata = 0;
745 sp->enc_outcount = 0;
752 sp->enc_oldcode = (
hcode_t) -1;
756 #define CALCRATIO(sp, rat) { \ 757 if (incount > 0x007fffff) { \ 758 rat = outcount >> 8; \ 759 rat = (rat == 0 ? 0x7fffffff : incount/rat); \ 761 rat = (incount<<8) / outcount; \ 763 #define PutNextCode(op, c) { \ 764 nextdata = (nextdata << nbits) | c; \ 766 *op++ = (unsigned char)(nextdata >> (nextbits-8)); \ 768 if (nextbits >= 8) { \ 769 *op++ = (unsigned char)(nextdata >> (nextbits-8)); \ 798 long incount, outcount, checkpoint;
799 long nextdata, nextbits;
800 int free_ent, maxcode,
nbits;
809 incount = sp->enc_incount;
810 outcount = sp->enc_outcount;
811 checkpoint = sp->enc_checkpoint;
812 nextdata = sp->lzw_nextdata;
813 nextbits = sp->lzw_nextbits;
814 free_ent = sp->lzw_free_ent;
815 maxcode = sp->lzw_maxcode;
816 nbits = sp->lzw_nbits;
818 limit = sp->enc_rawlimit;
819 ent = sp->enc_oldcode;
821 if (ent == (
hcode_t) -1 && cc > 0) {
828 ent = *bp++; cc--; incount++;
831 c = *bp++; cc--; incount++;
841 hp = &sp->enc_hashtab[
h];
842 if (hp->hash == fcode) {
860 hp = &sp->enc_hashtab[
h];
861 if (hp->hash == fcode) {
865 }
while (hp->hash >= 0);
883 hp->code = free_ent++;
900 if (free_ent > maxcode) {
904 }
else if (incount >= checkpoint) {
914 if (rat <= sp->enc_ratio) {
934 sp->enc_incount = incount;
935 sp->enc_outcount = outcount;
936 sp->enc_checkpoint = checkpoint;
937 sp->enc_oldcode = ent;
938 sp->lzw_nextdata = nextdata;
939 sp->lzw_nextbits = nextbits;
940 sp->lzw_free_ent = free_ent;
941 sp->lzw_maxcode = maxcode;
942 sp->lzw_nbits =
nbits;
952 LZWPostEncode(
TIFF* tif)
956 long nextbits = sp->lzw_nextbits;
957 long nextdata = sp->lzw_nextdata;
958 long outcount = sp->enc_outcount;
959 int nbits = sp->lzw_nbits;
961 if (op > sp->enc_rawlimit) {
966 if (sp->enc_oldcode != (
hcode_t) -1) {
968 sp->enc_oldcode = (
hcode_t) -1;
972 *op++ = (
unsigned char)(nextdata << (8-nextbits));
981 cl_hash(LZWCodecState* sp)
983 register hash_t *hp = &sp->enc_hashtab[
HSIZE-1];
984 register long i =
HSIZE-8;
998 for (i += 8; i > 0; i--, hp--)
1003 LZWCleanup(
TIFF* tif)
1058 "No space for LZW state block");
GLenum GLuint GLenum GLsizei length
TIFFCodeMethod tif_decoderow
TIFFPreMethod tif_predecode
void TIFFWarningExt(thandle_t fd, const char *module, const char *fmt,...)
#define NextCode(_tif, _sp, _bp, _code, _get)
#define DecoderState(tif)
TIFFCodeMethod tif_encodestrip
GLfloat GLfloat GLfloat GLfloat h
TIFFCodeMethod tif_encoderow
int TIFFPredictorCleanup(TIFF *tif)
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
int(* decodeFunc)(TIFF *, tidata_t, tsize_t, tsample_t)
#define CALCRATIO(sp, rat)
TIFFCodeMethod tif_decodestrip
#define GetNextCodeCompat(sp, bp, code)
TIFFCodeMethod tif_encodetile
#define PutNextCode(op, c)
TIFFPreMethod tif_preencode
int TIFFInitLZW(TIFF *tif, int scheme)
TIFFBoolMethod tif_postencode
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
register bit_buf_type register int int nbits
void TIFFErrorExt(thandle_t fd, const char *module, const char *fmt,...)
TIFFCodeMethod tif_decodetile
GLsizei const GLfloat * value
#define EncoderState(tif)
TIFFVoidMethod tif_cleanup
void _TIFFmemset(tdata_t p, int v, tsize_t c)
void _TIFFSetDefaultCompressionState(TIFF *tif)
if(!abbox) return FT_THROW(Invalid_Argument)
#define GetNextCode(sp, bp, code)
int TIFFPredictorInit(TIFF *tif)
void * _TIFFmalloc(tsize_t s)
void _TIFFfree(tdata_t p)
TIFFBoolMethod tif_setupencode
int TIFFFlushData1(TIFF *tif)
TIFFBoolMethod tif_setupdecode