63#define MAXCODE(n) ((1L<<(n))-1)
74#define CODE_MAX MAXCODE(BITS_MAX)
79#define CSIZE (MAXCODE(BITS_MAX)+1024L)
81#define CSIZE (MAXCODE(BITS_MAX)+1L)
93 unsigned short maxcode;
94 unsigned short free_ent;
95 unsigned long nextdata;
101#define lzw_nbits base.nbits
102#define lzw_maxcode base.maxcode
103#define lzw_free_ent base.free_ent
104#define lzw_nextdata base.nextdata
105#define lzw_nextbits base.nextbits
119typedef struct code_ent {
120 struct code_ent *
next;
123 unsigned char firstchar;
138 decodeFunc dec_decode;
140 code_t* dec_oldcodep;
141 code_t* dec_free_entp;
142 code_t* dec_maxcodep;
148#define CHECK_GAP 10000
156#define LZWState(tif) ((LZWBaseState*) (tif)->tif_data)
157#define DecoderState(tif) ((LZWCodecState*) LZWState(tif))
158#define EncoderState(tif) ((LZWCodecState*) LZWState(tif))
164static void cl_hash(LZWCodecState*);
175#define NextCode(_tif, _sp, _bp, _code, _get) { \
176 if ((_sp)->dec_bitsleft < (uint64)nbits) { \
177 TIFFWarningExt(_tif->tif_clientdata, module, \
178 "LZWDecode: Strip %d not terminated with EOI code", \
179 _tif->tif_curstrip); \
182 _get(_sp,_bp,_code); \
183 (_sp)->dec_bitsleft -= nbits; \
187#define NextCode(tif, sp, bp, code, get) get(sp, bp, code)
191LZWFixupTags(
TIFF* tif)
198LZWSetupDecode(
TIFF* tif)
200 static const char module[] =
"LZWSetupDecode";
201 LZWCodecState*
sp = DecoderState(tif);
217 DecoderState(tif)->dec_codetab =
NULL;
218 DecoderState(tif)->dec_decode =
NULL;
225 sp = DecoderState(tif);
230 if (
sp->dec_codetab ==
NULL) {
231 sp->dec_codetab = (code_t*)
_TIFFmalloc(CSIZE*
sizeof (code_t));
232 if (
sp->dec_codetab ==
NULL) {
234 "No space for LZW code table");
244 sp->dec_codetab[
code].length = 1;
253 (CODE_FIRST - CODE_CLEAR) *
sizeof (code_t));
264 static const char module[] =
"LZWPreDecode";
265 LZWCodecState *
sp = DecoderState(tif);
269 if(
sp->dec_codetab ==
NULL )
272 if(
sp->dec_codetab ==
NULL )
282 if (!
sp->dec_decode) {
284 "Old-style LZW codes, convert file");
301 sp->dec_decode = LZWDecodeCompat;
303 sp->lzw_maxcode = MAXCODE(BITS_MIN);
305 if (!
sp->dec_decode) {
307 "Old-style LZW codes not supported");
308 sp->dec_decode = LZWDecode;
313 sp->lzw_maxcode = MAXCODE(BITS_MIN)-1;
314 sp->dec_decode = LZWDecode;
316 sp->lzw_nbits = BITS_MIN;
317 sp->lzw_nextbits = 0;
318 sp->lzw_nextdata = 0;
321 sp->dec_nbitsmask = MAXCODE(BITS_MIN);
323 sp->dec_bitsleft = 0;
324 sp->old_tif_rawcc = 0;
326 sp->dec_free_entp =
sp->dec_codetab + CODE_FIRST;
335 sp->dec_oldcodep = &
sp->dec_codetab[-1];
336 sp->dec_maxcodep = &
sp->dec_codetab[
sp->dec_nbitsmask-1];
343#define GetNextCode(sp, bp, code) { \
344 nextdata = (nextdata<<8) | *(bp)++; \
346 if (nextbits < nbits) { \
347 nextdata = (nextdata<<8) | *(bp)++; \
350 code = (hcode_t)((nextdata >> (nextbits-nbits)) & nbitsmask); \
358 "Bogus encoding, loop in the code table; scanline %d",
365 static const char module[] =
"LZWDecode";
366 LZWCodecState *
sp = DecoderState(tif);
367 char *
op = (
char*) op0;
368 long occ = (
long) occ0;
373 long nbits, nextbits, nbitsmask;
374 unsigned long nextdata;
375 code_t *
codep, *free_entp, *maxcodep, *oldcodep;
389 if (
sp->dec_restart) {
393 residue =
codep->length -
sp->dec_restart;
401 sp->dec_restart += occ;
404 }
while (--residue > occ &&
codep);
410 }
while (--occ &&
codep);
426 }
while (--residue &&
codep);
434 nbits =
sp->lzw_nbits;
435 nextdata =
sp->lzw_nextdata;
436 nextbits =
sp->lzw_nextbits;
437 nbitsmask =
sp->dec_nbitsmask;
438 oldcodep =
sp->dec_oldcodep;
439 free_entp =
sp->dec_free_entp;
440 maxcodep =
sp->dec_maxcodep;
443 NextCode(tif,
sp, bp,
code, GetNextCode);
444 if (
code == CODE_EOI)
446 if (
code == CODE_CLEAR) {
448 free_entp =
sp->dec_codetab + CODE_FIRST;
450 (CSIZE - CODE_FIRST) *
sizeof (code_t));
452 nbitsmask = MAXCODE(BITS_MIN);
453 maxcodep =
sp->dec_codetab + nbitsmask-1;
454 NextCode(tif,
sp, bp,
code, GetNextCode);
455 }
while (
code == CODE_CLEAR);
456 if (
code == CODE_EOI)
458 if (
code > CODE_CLEAR) {
460 "LZWDecode: Corrupted LZW table at scanline %d",
466 oldcodep =
sp->dec_codetab +
code;
474 if (free_entp < &sp->dec_codetab[0] ||
475 free_entp >= &
sp->dec_codetab[CSIZE]) {
477 "Corrupted LZW table at scanline %d",
482 free_entp->next = oldcodep;
483 if (free_entp->next < &
sp->dec_codetab[0] ||
484 free_entp->next >= &
sp->dec_codetab[CSIZE]) {
486 "Corrupted LZW table at scanline %d",
490 free_entp->firstchar = free_entp->next->firstchar;
491 free_entp->length = free_entp->next->length+1;
492 free_entp->value = (
codep < free_entp) ?
493 codep->firstchar : free_entp->firstchar;
494 if (++free_entp > maxcodep) {
495 if (++nbits > BITS_MAX)
497 nbitsmask = MAXCODE(nbits);
498 maxcodep =
sp->dec_codetab + nbitsmask-1;
506 if(
codep->length == 0) {
508 "Wrong length of decoded string: "
509 "data probably corrupted at scanline %d",
513 if (
codep->length > occ) {
525 sp->dec_restart = (
long)occ;
530 }
while (--occ &&
codep);
563 sp->lzw_nbits = (
unsigned short) nbits;
564 sp->lzw_nextdata = nextdata;
565 sp->lzw_nextbits = nextbits;
566 sp->dec_nbitsmask = nbitsmask;
567 sp->dec_oldcodep = oldcodep;
568 sp->dec_free_entp = free_entp;
569 sp->dec_maxcodep = maxcodep;
572#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
574 "Not enough data at scanline %d (short %I64d bytes)",
578 "Not enough data at scanline %d (short %llu bytes)",
579 tif->
tif_row, (
unsigned long long) occ);
590#define GetNextCodeCompat(sp, bp, code) { \
591 nextdata |= (unsigned long) *(bp)++ << nextbits; \
593 if (nextbits < nbits) { \
594 nextdata |= (unsigned long) *(bp)++ << nextbits;\
597 code = (hcode_t)(nextdata & nbitsmask); \
598 nextdata >>= nbits; \
605 static const char module[] =
"LZWDecodeCompat";
606 LZWCodecState *
sp = DecoderState(tif);
607 char *
op = (
char*) op0;
608 long occ = (
long) occ0;
613 long nextbits, nextdata, nbitsmask;
614 code_t *
codep, *free_entp, *maxcodep, *oldcodep;
628 if (
sp->dec_restart) {
632 residue =
codep->length -
sp->dec_restart;
640 sp->dec_restart += occ;
643 }
while (--residue > occ);
668 nbits =
sp->lzw_nbits;
669 nextdata =
sp->lzw_nextdata;
670 nextbits =
sp->lzw_nextbits;
671 nbitsmask =
sp->dec_nbitsmask;
672 oldcodep =
sp->dec_oldcodep;
673 free_entp =
sp->dec_free_entp;
674 maxcodep =
sp->dec_maxcodep;
677 NextCode(tif,
sp, bp,
code, GetNextCodeCompat);
678 if (
code == CODE_EOI)
680 if (
code == CODE_CLEAR) {
682 free_entp =
sp->dec_codetab + CODE_FIRST;
684 (CSIZE - CODE_FIRST) *
sizeof (code_t));
686 nbitsmask = MAXCODE(BITS_MIN);
687 maxcodep =
sp->dec_codetab + nbitsmask;
688 NextCode(tif,
sp, bp,
code, GetNextCodeCompat);
689 }
while (
code == CODE_CLEAR);
690 if (
code == CODE_EOI)
692 if (
code > CODE_CLEAR) {
694 "LZWDecode: Corrupted LZW table at scanline %d",
700 oldcodep =
sp->dec_codetab +
code;
708 if (free_entp < &sp->dec_codetab[0] ||
709 free_entp >= &
sp->dec_codetab[CSIZE]) {
711 "Corrupted LZW table at scanline %d", tif->
tif_row);
715 free_entp->next = oldcodep;
716 if (free_entp->next < &
sp->dec_codetab[0] ||
717 free_entp->next >= &
sp->dec_codetab[CSIZE]) {
719 "Corrupted LZW table at scanline %d", tif->
tif_row);
722 free_entp->firstchar = free_entp->next->firstchar;
723 free_entp->length = free_entp->next->length+1;
724 free_entp->value = (
codep < free_entp) ?
725 codep->firstchar : free_entp->firstchar;
726 if (++free_entp > maxcodep) {
727 if (++nbits > BITS_MAX)
729 nbitsmask = MAXCODE(nbits);
730 maxcodep =
sp->dec_codetab + nbitsmask;
738 if(
codep->length == 0) {
740 "Wrong length of decoded "
741 "string: data probably corrupted at scanline %d",
745 if (
codep->length > occ) {
755 }
while (
codep->length > occ);
756 sp->dec_restart = occ;
787 sp->lzw_nbits = (
unsigned short)nbits;
788 sp->lzw_nextdata = nextdata;
789 sp->lzw_nextbits = nextbits;
790 sp->dec_nbitsmask = nbitsmask;
791 sp->dec_oldcodep = oldcodep;
792 sp->dec_free_entp = free_entp;
793 sp->dec_maxcodep = maxcodep;
796#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
798 "Not enough data at scanline %d (short %I64d bytes)",
802 "Not enough data at scanline %d (short %llu bytes)",
803 tif->
tif_row, (
unsigned long long) occ);
816LZWSetupEncode(
TIFF* tif)
818 static const char module[] =
"LZWSetupEncode";
819 LZWCodecState*
sp = EncoderState(tif);
822 sp->enc_hashtab = (hash_t*)
_TIFFmalloc(HSIZE*
sizeof (hash_t));
823 if (
sp->enc_hashtab ==
NULL) {
825 "No space for LZW hash table");
837 LZWCodecState *
sp = EncoderState(tif);
842 if(
sp->enc_hashtab ==
NULL )
847 sp->lzw_nbits = BITS_MIN;
848 sp->lzw_maxcode = MAXCODE(BITS_MIN);
849 sp->lzw_free_ent = CODE_FIRST;
850 sp->lzw_nextbits = 0;
851 sp->lzw_nextdata = 0;
852 sp->enc_checkpoint = CHECK_GAP;
855 sp->enc_outcount = 0;
862 sp->enc_oldcode = (hcode_t) -1;
866#define CALCRATIO(sp, rat) { \
867 if (incount > 0x007fffff) { \
868 rat = outcount >> 8; \
869 rat = (rat == 0 ? 0x7fffffff : incount/rat); \
871 rat = (incount<<8) / outcount; \
875#define PutNextCode(op, c) { \
876 nextdata = (nextdata << nbits) | c; \
878 *op++ = (unsigned char)((nextdata >> (nextbits-8))&0xff); \
880 if (nextbits >= 8) { \
881 *op++ = (unsigned char)((nextdata >> (nextbits-8))&0xff); \
904 register LZWCodecState *
sp = EncoderState(tif);
910 long incount, outcount, checkpoint;
911 unsigned long nextdata;
913 int free_ent, maxcode, nbits;
926 incount =
sp->enc_incount;
927 outcount =
sp->enc_outcount;
928 checkpoint =
sp->enc_checkpoint;
929 nextdata =
sp->lzw_nextdata;
930 nextbits =
sp->lzw_nextbits;
931 free_ent =
sp->lzw_free_ent;
932 maxcode =
sp->lzw_maxcode;
933 nbits =
sp->lzw_nbits;
936 ent = (hcode_t)
sp->enc_oldcode;
938 if (ent == (hcode_t) -1 &&
cc > 0) {
944 PutNextCode(
op, CODE_CLEAR);
945 ent = *bp++;
cc--; incount++;
948 c = *bp++;
cc--; incount++;
949 fcode = ((
long)
c << BITS_MAX) + ent;
950 h = (
c << HSHIFT) ^ ent;
958 hp = &
sp->enc_hashtab[
h];
959 if (hp->hash == fcode) {
977 hp = &
sp->enc_hashtab[
h];
978 if (hp->hash == fcode) {
982 }
while (hp->hash >= 0);
999 PutNextCode(
op, ent);
1001 hp->code = (hcode_t)(free_ent++);
1003 if (free_ent == CODE_MAX-1) {
1009 free_ent = CODE_FIRST;
1010 PutNextCode(
op, CODE_CLEAR);
1012 maxcode = MAXCODE(BITS_MIN);
1018 if (free_ent > maxcode) {
1020 assert(nbits <= BITS_MAX);
1021 maxcode = (
int) MAXCODE(nbits);
1022 }
else if (incount >= checkpoint) {
1030 checkpoint = incount+CHECK_GAP;
1032 if (rat <= sp->enc_ratio) {
1037 free_ent = CODE_FIRST;
1038 PutNextCode(
op, CODE_CLEAR);
1040 maxcode = MAXCODE(BITS_MIN);
1042 sp->enc_ratio = rat;
1052 sp->enc_incount = incount;
1053 sp->enc_outcount = outcount;
1054 sp->enc_checkpoint = checkpoint;
1055 sp->enc_oldcode = ent;
1056 sp->lzw_nextdata = nextdata;
1057 sp->lzw_nextbits = nextbits;
1058 sp->lzw_free_ent = (
unsigned short)free_ent;
1059 sp->lzw_maxcode = (
unsigned short)maxcode;
1060 sp->lzw_nbits = (
unsigned short)nbits;
1070LZWPostEncode(
TIFF* tif)
1072 register LZWCodecState *
sp = EncoderState(tif);
1074 long nextbits =
sp->lzw_nextbits;
1075 unsigned long nextdata =
sp->lzw_nextdata;
1076 long outcount =
sp->enc_outcount;
1077 int nbits =
sp->lzw_nbits;
1079 if (
op >
sp->enc_rawlimit) {
1085 if (
sp->enc_oldcode != (hcode_t) -1) {
1086 int free_ent =
sp->lzw_free_ent;
1088 PutNextCode(
op,
sp->enc_oldcode);
1089 sp->enc_oldcode = (hcode_t) -1;
1092 if (free_ent == CODE_MAX-1) {
1095 PutNextCode(
op, CODE_CLEAR);
1102 if (free_ent >
sp->lzw_maxcode) {
1104 assert(nbits <= BITS_MAX);
1108 PutNextCode(
op, CODE_EOI);
1111 *
op++ = (
unsigned char)((nextdata << (8-nextbits))&0xff);
1120cl_hash(LZWCodecState*
sp)
1122 register hash_t *hp = &
sp->enc_hashtab[HSIZE-1];
1123 register long i = HSIZE-8;
1137 for (
i += 8;
i > 0;
i--, hp--)
1142LZWCleanup(
TIFF* tif)
1148 if (DecoderState(tif)->dec_codetab)
1149 _TIFFfree(DecoderState(tif)->dec_codetab);
1151 if (EncoderState(tif)->enc_hashtab)
1152 _TIFFfree(EncoderState(tif)->enc_hashtab);
1163 static const char module[] =
"TIFFInitLZW";
1171 DecoderState(tif)->dec_codetab =
NULL;
1172 DecoderState(tif)->dec_decode =
NULL;
1173 EncoderState(tif)->enc_hashtab =
NULL;
1174 LZWState(tif)->rw_mode = tif->
tif_mode;
1199 "No space for LZW state block");
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
unsigned short(__cdecl typeof(TIFFCurrentDirectory))(struct tiff *)
_In_ uint64_t _In_ uint64_t _In_ uint64_t _In_opt_ traverse_ptr * tp
GLuint GLsizei GLsizei * length
GLfloat GLfloat GLfloat GLfloat h
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
static unsigned char * codep
static unsigned __int64 next
TIFFCodeMethod tif_encodestrip
TIFFCodeMethod tif_encodetile
TIFFPreMethod tif_preencode
TIFFBoolMethod tif_fixuptags
TIFFPreMethod tif_predecode
TIFFCodeMethod tif_decodestrip
TIFFCodeMethod tif_decoderow
TIFFBoolMethod tif_setupencode
TIFFBoolMethod tif_postencode
TIFFCodeMethod tif_encoderow
TIFFVoidMethod tif_cleanup
TIFFBoolMethod tif_setupdecode
TIFFCodeMethod tif_decodetile
void _TIFFSetDefaultCompressionState(TIFF *tif)
void TIFFErrorExt(thandle_t fd, const char *module, const char *fmt,...)
int TIFFPredictorCleanup(TIFF *tif)
int TIFFPredictorInit(TIFF *tif)
void _TIFFmemset(void *p, int v, tmsize_t c)
void * _TIFFmalloc(tmsize_t s)
void TIFFWarningExt(thandle_t fd, const char *module, const char *fmt,...)
int TIFFFlushData1(TIFF *tif)
ActualNumberDriverObjects * sizeof(PDRIVER_OBJECT)) PDRIVER_OBJECT *DriverObjectList