e576bb2002-10-11Martin Nilsson /* || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information. */
1b10db2002-10-08Martin Nilsson 
ce6f781997-10-27Mirar (Pontus Hagland) /* **! module Image */
9257a01997-10-27Mirar (Pontus Hagland) 
a79b2e1997-11-02Mirar (Pontus Hagland) #include "global.h"
f0ac4d2000-09-11Henrik Grubbström (Grubba) #include "config.h" #ifdef WITH_GIF #include "../Image/image_machine.h"
9257a01997-10-27Mirar (Pontus Hagland) #include "gif_lzw.h"
6dc2772000-07-28Fredrik Hübinette (Hubbe) 
a79b2e1997-11-02Mirar (Pontus Hagland) #define DEFAULT_OUTBYTES 16384 #define STDLZWCODES 8192
01b9212016-01-12Per Hedbor static inline void lzw_output(struct gif_lzw *lzw,lzwcode_t codeno)
a79b2e1997-11-02Mirar (Pontus Hagland) { if (lzw->outpos+4>=lzw->outlen) { unsigned char *new; new=realloc(lzw->out,lzw->outlen*=2); if (!new) { lzw->outpos=0; lzw->broken=1; return; } lzw->out=new; }
44519e1998-04-29Mirar (Pontus Hagland)  if (!lzw->reversebits)
a79b2e1997-11-02Mirar (Pontus Hagland)  {
44519e1998-04-29Mirar (Pontus Hagland)  int bits,bitp; unsigned char c; bitp=lzw->outbit; c=(unsigned char)lzw->lastout; bits=lzw->codebits; if (bits>12) bits=12; while (bits)
a79b2e1997-11-02Mirar (Pontus Hagland)  {
44519e1998-04-29Mirar (Pontus Hagland)  c|=(codeno<<bitp); if (bits+bitp>=8) { bits-=8-bitp; codeno>>=8-bitp; bitp=0; lzw->out[lzw->outpos++]=c; c=0; } else { lzw->outbit=bitp+bits; lzw->lastout=c; return; }
a79b2e1997-11-02Mirar (Pontus Hagland)  }
44519e1998-04-29Mirar (Pontus Hagland)  lzw->lastout=0; lzw->outbit=0; } else { #ifdef GIF_DEBUG
32cf1a2013-01-25Martin Nilsson  fprintf(stderr,"codeno=%x lastout=%lx outbit=%ld codebits=%ld\n",
44519e1998-04-29Mirar (Pontus Hagland)  codeno,lzw->lastout,lzw->outbit,lzw->codebits); #endif lzw->lastout=(lzw->lastout<<lzw->codebits)|codeno; lzw->outbit+=lzw->codebits; #ifdef GIF_DEBUG
32cf1a2013-01-25Martin Nilsson  fprintf(stderr,"-> codeno=%x lastout=%lx outbit=%ld codebits=%ld\n",
44519e1998-04-29Mirar (Pontus Hagland)  codeno,lzw->lastout,lzw->outbit,lzw->codebits); #endif while (lzw->outbit>8)
a79b2e1997-11-02Mirar (Pontus Hagland)  {
44519e1998-04-29Mirar (Pontus Hagland)  lzw->out[lzw->outpos++] = (unsigned char)(lzw->lastout>>(lzw->outbit-8)); lzw->outbit-=8; #ifdef GIF_DEBUG
32cf1a2013-01-25Martin Nilsson  fprintf(stderr, "(shiftout) codeno=%x lastout=%lx outbit=%ld codebits=%ld\n",
44519e1998-04-29Mirar (Pontus Hagland)  codeno,lzw->lastout,lzw->outbit,lzw->codebits); #endif
a79b2e1997-11-02Mirar (Pontus Hagland)  } } }
01b9212016-01-12Per Hedbor static inline void lzw_add(struct gif_lzw *lzw,int c)
a79b2e1997-11-02Mirar (Pontus Hagland) { lzwcode_t lno,lno2; struct gif_lzwc *l; if (lzw->current==LZWCNULL) /* no current, load */ { lzw->current=c;
a630be1999-05-30Mirar (Pontus Hagland) #ifdef GIF_LZW_LZ lzw->skipone=0; #endif
a79b2e1997-11-02Mirar (Pontus Hagland)  return; }
a630be1999-05-30Mirar (Pontus Hagland) #ifdef GIF_LZW_LZ
79c62a2003-08-12Martin Nilsson  if (!lzw->skipone) {
a630be1999-05-30Mirar (Pontus Hagland) #endif
79c62a2003-08-12Martin Nilsson  /* check if we have this sequence */ lno=lzw->code[lzw->current].firstchild; while (lno!=LZWCNULL) { if (lzw->code[lno].c==c && lno!=lzw->codes-1 ) { lzw->current=lno; return; } lno=lzw->code[lno].next; }
9281142001-04-27Henrik Grubbström (Grubba) #ifdef GIF_LZW_LZ
a79b2e1997-11-02Mirar (Pontus Hagland)  }
a630be1999-05-30Mirar (Pontus Hagland) #endif
a79b2e1997-11-02Mirar (Pontus Hagland)  if (lzw->codes==4096) /* needs more than 12 bits */ { int i; lzw_output(lzw,lzw->current); for (i=0; i<(1L<<lzw->bits); i++) lzw->code[i].firstchild=LZWCNULL; lzw->codes=(1L<<lzw->bits)+2;
13670c2015-05-25Martin Nilsson 
a79b2e1997-11-02Mirar (Pontus Hagland)  /* output clearcode, 1000... (bits) */
f258372000-09-08Henrik Grubbström (Grubba)  lzw_output(lzw, (lzwcode_t)(1L<<lzw->bits));
a79b2e1997-11-02Mirar (Pontus Hagland)  lzw->codebits=lzw->bits+1; lzw->current=c;
a630be1999-05-30Mirar (Pontus Hagland) #ifdef GIF_LZW_LZ lzw->skipone=0; #endif
a79b2e1997-11-02Mirar (Pontus Hagland)  return; } /* output current code no, make new & reset */ lzw_output(lzw,lzw->current); lno=lzw->code[lzw->current].firstchild;
cc7cf42015-10-14Martin Nilsson  lno2 = (lzwcode_t)lzw->codes;
a79b2e1997-11-02Mirar (Pontus Hagland)  l=lzw->code+lno2; l->next=lno; l->firstchild=LZWCNULL; l->c=c; lzw->code[lzw->current].firstchild=lno2; lzw->codes++;
13670c2015-05-25Martin Nilsson  if (lzw->codes+lzw->earlychange>(unsigned long)(1L<<lzw->codebits))
44519e1998-04-29Mirar (Pontus Hagland)  lzw->codebits++;
a79b2e1997-11-02Mirar (Pontus Hagland)  lzw->current=c;
a630be1999-05-30Mirar (Pontus Hagland) #ifdef GIF_LZW_LZ lzw->skipone=!lzw->skipone; #endif
a79b2e1997-11-02Mirar (Pontus Hagland) } void image_gif_lzw_init(struct gif_lzw *lzw,int bits) {
c881141997-11-05Mirar (Pontus Hagland)  unsigned long i;
a79b2e1997-11-02Mirar (Pontus Hagland)  lzw->broken=0; lzw->codes=(1L<<bits)+2; lzw->bits=bits; lzw->codebits=bits+1;
dc8d022014-04-27Martin Nilsson  lzw->code=malloc(sizeof(struct gif_lzwc)*4096);
a79b2e1997-11-02Mirar (Pontus Hagland)  if (!lzw->code) { lzw->broken=1; return; } for (i=0; i<lzw->codes; i++) { lzw->code[i].c=(unsigned char)i; lzw->code[i].firstchild=LZWCNULL; lzw->code[i].next=LZWCNULL; } lzw->out=malloc(DEFAULT_OUTBYTES); if (!lzw->out) { lzw->broken=1; return; } lzw->outlen=DEFAULT_OUTBYTES; lzw->outpos=0; lzw->current=LZWCNULL; lzw->outbit=0; lzw->lastout=0;
44519e1998-04-29Mirar (Pontus Hagland)  lzw->earlychange=0; lzw->reversebits=0;
f258372000-09-08Henrik Grubbström (Grubba)  lzw_output(lzw, (lzwcode_t)(1L<<bits));
a79b2e1997-11-02Mirar (Pontus Hagland) } void image_gif_lzw_finish(struct gif_lzw *lzw)
9257a01997-10-27Mirar (Pontus Hagland) {
a79b2e1997-11-02Mirar (Pontus Hagland)  if (lzw->current!=LZWCNULL) lzw_output(lzw,lzw->current);
f258372000-09-08Henrik Grubbström (Grubba)  lzw_output( lzw, (lzwcode_t)(1L<<lzw->bits)+1 ); /* GIF end code */
a79b2e1997-11-02Mirar (Pontus Hagland)  if (lzw->outbit)
44519e1998-04-29Mirar (Pontus Hagland)  { if (lzw->reversebits)
cc7cf42015-10-14Martin Nilsson  lzw->out[lzw->outpos++] = (lzwcode_t)(lzw->lastout<< (8-lzw->outbit));
44519e1998-04-29Mirar (Pontus Hagland)  else
cc7cf42015-10-14Martin Nilsson  lzw->out[lzw->outpos++] = (lzwcode_t)lzw->lastout;
44519e1998-04-29Mirar (Pontus Hagland)  }
9257a01997-10-27Mirar (Pontus Hagland) }
a79b2e1997-11-02Mirar (Pontus Hagland) void image_gif_lzw_free(struct gif_lzw *lzw)
9257a01997-10-27Mirar (Pontus Hagland) {
a79b2e1997-11-02Mirar (Pontus Hagland)  if (lzw->out) free(lzw->out); if (lzw->code) free(lzw->code);
9257a01997-10-27Mirar (Pontus Hagland) }
3bd87a2000-08-03Henrik Grubbström (Grubba) void image_gif_lzw_add(struct gif_lzw *lzw, unsigned char *data, size_t len)
9257a01997-10-27Mirar (Pontus Hagland) {
a79b2e1997-11-02Mirar (Pontus Hagland)  while (len--) lzw_add(lzw,*(data++));
9257a01997-10-27Mirar (Pontus Hagland) }
f0ac4d2000-09-11Henrik Grubbström (Grubba)  #endif /* WITH_GIF */