Attachment "tk-gif-threadsafety.patch" to
ticket [1080290f]
added by
jenglish
2004-12-08 00:43:58.
Date: Tue Dec 07 09:42:45 AM PST 2004
Files: generic/tkImgGIF.c
--- generic/tkImgGIF.c.gif-threadsafety.old 2004-12-07 07:38:55.000000000 -0800
+++ generic/tkImgGIF.c 2004-12-07 09:29:13.648321913 -0800
@@ -141,14 +141,24 @@
#define LM_to_uint(a,b) (((b)<<8)|(a))
#define ReadOK(file,buffer,len) (Fread(buffer, len, 1, file) != 0)
+typedef struct {
+ unsigned char buf[280];
+ int bytes;
+ int done;
+ unsigned char *c;
+ unsigned int window;
+ int bitsInWindow;
+} GIFDecodeBuf;
+
/*
* Prototypes for local procedures defined in this file:
*/
static int DoExtension _ANSI_ARGS_((Tcl_Channel chan, int label,
int *transparent));
+static void InitDecodeBuf _ANSI_ARGS_((GIFDecodeBuf *));
static int GetCode _ANSI_ARGS_((Tcl_Channel chan, int code_size,
- int flag));
+ GIFDecodeBuf *));
static int GetDataBlock _ANSI_ARGS_((Tcl_Channel chan,
unsigned char *buf));
static int ReadColorMap _ANSI_ARGS_((Tcl_Channel chan, int number,
@@ -712,7 +722,7 @@
int label;
int *transparent;
{
- static unsigned char buf[256];
+ unsigned char buf[256];
int count;
switch (label) {
@@ -724,12 +734,12 @@
case 0xfe: /* Comment Extension */
do {
- count = GetDataBlock(chan, (unsigned char*) buf);
+ count = GetDataBlock(chan, buf);
} while (count > 0);
return count;
case 0xf9: /* Graphic Control Extension */
- count = GetDataBlock(chan, (unsigned char*) buf);
+ count = GetDataBlock(chan, buf);
if (count < 0) {
return 1;
}
@@ -738,13 +748,13 @@
}
do {
- count = GetDataBlock(chan, (unsigned char*) buf);
+ count = GetDataBlock(chan, buf);
} while (count > 0);
return count;
}
do {
- count = GetDataBlock(chan, (unsigned char*) buf);
+ count = GetDataBlock(chan, buf);
} while (count > 0);
return count;
}
@@ -810,6 +820,7 @@
int transparent;
{
unsigned char initialCodeSize;
+ GIFDecodeBuf dcbuf;
int v;
int xpos = 0, ypos = 0, pass = 0, i;
register char *pixelPtr;
@@ -862,7 +873,7 @@
}
top = stack;
- GetCode(chan, 0, 1);
+ InitDecodeBuf(&dcbuf);
/*
* Read until we finish the image
@@ -874,7 +885,7 @@
/*
* Bummer -- our stack is empty. Now we have to work!
*/
- code = GetCode(chan, codeSize, 0);
+ code = GetCode(chan, codeSize, &dcbuf);
if (code < 0) {
return TCL_OK;
}
@@ -1018,6 +1029,23 @@
/*
*----------------------------------------------------------------------
*
+ * InitDecodeBuf --
+ *
+ * Initialize the decoder.
+ */
+
+static void InitDecodeBuf(GIFDecodeBuf *d)
+{
+ d->c = NULL;
+ d->bytes = 0;
+ d->window = 0;
+ d->bitsInWindow = 0;
+ d->done = 0;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
* GetCode --
*
* Extract the next compression code from the file. In GIF's, the
@@ -1045,69 +1073,51 @@
*/
static int
-GetCode(chan, code_size, flag)
+GetCode(chan, code_size, d)
Tcl_Channel chan;
int code_size;
- int flag;
+ GIFDecodeBuf *d;
{
- static unsigned char buf[280];
- static int bytes = 0, done;
- static unsigned char *c;
-
- static unsigned int window;
- static int bitsInWindow = 0;
int ret;
-
- if (flag) {
- /*
- * Initialize the decoder.
- */
- bitsInWindow = 0;
- bytes = 0;
- window = 0;
- done = 0;
- c = NULL;
- return 0;
- }
- while (bitsInWindow < code_size) {
+ while (d->bitsInWindow < code_size) {
/*
* Not enough bits in our window to cover the request.
*/
- if (done) {
+ if (d->done) {
return -1;
}
- if (bytes == 0) {
+ if (d->bytes == 0) {
/*
* Not enough bytes in our buffer to add to the window.
*/
- bytes = GetDataBlock(chan, buf);
- c = buf;
- if (bytes <= 0) {
- done = 1;
+ d->bytes = GetDataBlock(chan, d->buf);
+ d->c = d->buf;
+ if (d->bytes <= 0) {
+ d->done = 1;
break;
}
}
/*
* Tack another byte onto the window, see if that's enough.
*/
- window += (*c) << bitsInWindow;
- c++;
- bitsInWindow += 8;
- bytes--;
+ d->window += (*d->c) << d->bitsInWindow;
+ d->c++;
+ d->bitsInWindow += 8;
+ d->bytes--;
}
/*
* The next code will always be the last code_size bits of the window.
*/
- ret = window & ((1 << code_size) - 1);
+ ret = d->window & ((1 << code_size) - 1);
/*
* Shift data in the window to put the next code at the end.
*/
- window >>= code_size;
- bitsInWindow -= code_size;
+ d->window >>= code_size;
+ d->bitsInWindow -= code_size;
return ret;
}
@@ -1360,7 +1370,6 @@
* Types, defines and variables needed to write and compress a GIF.
*/
-typedef int (* ifunptr) _ANSI_ARGS_((void));
#define LSB(a) ((unsigned char) (((short)(a)) & 0x00FF))
#define MSB(a) ((unsigned char) (((short)(a)) >> 8))
@@ -1368,31 +1377,33 @@
#define GIFBITS 12
#define HSIZE 5003 /* 80% occupancy */
-static int ssize;
-static int csize;
-static int rsize;
-static unsigned char *pixelo;
-static int pixelSize;
-static int pixelPitch;
-static int greenOffset;
-static int blueOffset;
-static int alphaOffset;
-static int num;
-static unsigned char mapa[MAXCOLORMAPSIZE][3];
+typedef struct
+{
+ int ssize;
+ int csize;
+ int rsize;
+ unsigned char *pixelo;
+ int pixelSize;
+ int pixelPitch;
+ int greenOffset;
+ int blueOffset;
+ int alphaOffset;
+ int num;
+ unsigned char mapa[MAXCOLORMAPSIZE][3];
+} GIFDataSource;
+
+typedef int (* ifunptr) _ANSI_ARGS_((void));
/*
* Definition of new functions to write GIFs
*/
-static int color _ANSI_ARGS_((int red,int green, int blue,
- unsigned char mapa[MAXCOLORMAPSIZE][3]));
+static int color _ANSI_ARGS_((int red,int green, int blue, GIFDataSource *m));
static void compress _ANSI_ARGS_((int init_bits, Tcl_Channel handle,
- ifunptr readValue));
-static int nuevo _ANSI_ARGS_((int red, int green ,int blue,
- unsigned char mapa[MAXCOLORMAPSIZE][3]));
-static void savemap _ANSI_ARGS_((Tk_PhotoImageBlock *blockPtr,
- unsigned char mapa[MAXCOLORMAPSIZE][3]));
-static int ReadValue _ANSI_ARGS_((void));
+ GIFDataSource *m));
+static int nuevo _ANSI_ARGS_((int red, int green ,int blue, GIFDataSource *m));
+static void savemap _ANSI_ARGS_((Tk_PhotoImageBlock *blockPtr, GIFDataSource *m));
+static int ReadValue _ANSI_ARGS_((GIFDataSource *));
static int
FileWriteGIF(interp, filename, format, blockPtr)
@@ -1429,6 +1440,7 @@
Tcl_Obj *format;
Tk_PhotoImageBlock *blockPtr;
{
+ GIFDataSource CMAP, *m = &CMAP;
int resolution;
long width,height,x;
@@ -1438,39 +1450,39 @@
top = 0;
left = 0;
- pixelSize = blockPtr->pixelSize;
- greenOffset = blockPtr->offset[1]-blockPtr->offset[0];
- blueOffset = blockPtr->offset[2]-blockPtr->offset[0];
- alphaOffset = blockPtr->offset[0];
- if (alphaOffset < blockPtr->offset[2]) {
- alphaOffset = blockPtr->offset[2];
+ m->pixelSize = blockPtr->pixelSize;
+ m->greenOffset = blockPtr->offset[1]-blockPtr->offset[0];
+ m->blueOffset = blockPtr->offset[2]-blockPtr->offset[0];
+ m->alphaOffset = blockPtr->offset[0];
+ if (m->alphaOffset < blockPtr->offset[2]) {
+ m->alphaOffset = blockPtr->offset[2];
}
- if (++alphaOffset < pixelSize) {
- alphaOffset -= blockPtr->offset[0];
+ if (++m->alphaOffset < m->pixelSize) {
+ m->alphaOffset -= blockPtr->offset[0];
} else {
- alphaOffset = 0;
+ m->alphaOffset = 0;
}
- Tcl_Write(handle, (char *) (alphaOffset ? GIF89a : GIF87a), 6);
+ Tcl_Write(handle, (char *) (m->alphaOffset ? GIF89a : GIF87a), 6);
for (x=0 ; x<MAXCOLORMAPSIZE ; x++) {
- mapa[x][CM_RED] = 255;
- mapa[x][CM_GREEN] = 255;
- mapa[x][CM_BLUE] = 255;
+ m->mapa[x][CM_RED] = 255;
+ m->mapa[x][CM_GREEN] = 255;
+ m->mapa[x][CM_BLUE] = 255;
}
width = blockPtr->width;
height = blockPtr->height;
- pixelo = blockPtr->pixelPtr + blockPtr->offset[0];
- pixelPitch = blockPtr->pitch;
- savemap(blockPtr,mapa);
- if (num >= MAXCOLORMAPSIZE) {
+ m->pixelo = blockPtr->pixelPtr + blockPtr->offset[0];
+ m->pixelPitch = blockPtr->pitch;
+ savemap(blockPtr, m);
+ if (m->num >= MAXCOLORMAPSIZE) {
Tcl_AppendResult(interp, "too many colors", (char *) NULL);
return TCL_ERROR;
}
- if (num<2) {
- num = 2;
+ if (m->num<2) {
+ m->num = 2;
}
c = LSB(width);
Mputc(c,handle);
@@ -1482,13 +1494,13 @@
Mputc(c,handle);
resolution = 0;
- while (num >> resolution) {
+ while (m->num >> resolution) {
resolution++;
}
c = 111 + resolution * 17;
Mputc(c,handle);
- num = 1 << resolution;
+ m->num = 1 << resolution;
/*
* background color
@@ -1503,12 +1515,12 @@
Mputc(c,handle);
- for (x=0 ; x<num ; x++) {
- c = mapa[x][CM_RED];
+ for (x=0 ; x<m->num ; x++) {
+ c = m->mapa[x][CM_RED];
Mputc(c,handle);
- c = mapa[x][CM_GREEN];
+ c = m->mapa[x][CM_GREEN];
Mputc(c,handle);
- c = mapa[x][CM_BLUE];
+ c = m->mapa[x][CM_BLUE];
Mputc(c,handle);
}
@@ -1516,7 +1528,7 @@
* Write out extension for transparent colour index, if necessary.
*/
- if (alphaOffset) {
+ if (m->alphaOffset) {
c = GIF_EXTENSION;
Mputc(c, handle);
Tcl_Write(handle, "\371\4\1\0\0\0", 7);
@@ -1548,9 +1560,9 @@
c = resolution;
Mputc(c,handle);
- ssize = rsize = blockPtr->width;
- csize = blockPtr->height;
- compress(resolution+1, handle, ReadValue);
+ m->ssize = m->rsize = blockPtr->width;
+ m->csize = blockPtr->height;
+ compress(resolution+1, handle, m);
c = 0;
Mputc(c,handle);
@@ -1561,16 +1573,12 @@
}
static int
-color(red, green, blue, mapa)
- int red;
- int green;
- int blue;
- unsigned char mapa[MAXCOLORMAPSIZE][3];
+color(int red, int green, int blue, GIFDataSource *m)
{
int x;
- for (x=(alphaOffset != 0) ; x<=MAXCOLORMAPSIZE ; x++) {
- if ((mapa[x][CM_RED] == red) && (mapa[x][CM_GREEN] == green) &&
- (mapa[x][CM_BLUE] == blue)) {
+ for (x=(m->alphaOffset != 0) ; x<=MAXCOLORMAPSIZE ; x++) {
+ if ((m->mapa[x][CM_RED] == red) && (m->mapa[x][CM_GREEN] == green) &&
+ (m->mapa[x][CM_BLUE] == blue)) {
return x;
}
}
@@ -1579,14 +1587,12 @@
static int
-nuevo(red, green, blue, mapa)
- int red,green,blue;
- unsigned char mapa[MAXCOLORMAPSIZE][3];
+nuevo(int red, int green, int blue, GIFDataSource *m)
{
- int x = (alphaOffset != 0);
- for (; x<=num ; x++) {
- if ((mapa[x][CM_RED] == red) && (mapa[x][CM_GREEN] == green) &&
- (mapa[x][CM_BLUE] == blue)) {
+ int x = (m->alphaOffset != 0);
+ for (; x<=m->num ; x++) {
+ if ((m->mapa[x][CM_RED] == red) && (m->mapa[x][CM_GREEN] == green) &&
+ (m->mapa[x][CM_BLUE] == blue)) {
return 0;
}
}
@@ -1594,65 +1600,64 @@
}
static void
-savemap(blockPtr,mapa)
- Tk_PhotoImageBlock *blockPtr;
- unsigned char mapa[MAXCOLORMAPSIZE][3];
+savemap(Tk_PhotoImageBlock *blockPtr, GIFDataSource *m)
{
unsigned char *colores;
int x,y;
unsigned char red,green,blue;
- if (alphaOffset) {
- num = 0;
- mapa[0][CM_RED] = 0xd9;
- mapa[0][CM_GREEN] = 0xd9;
- mapa[0][CM_BLUE] = 0xd9;
+ if (m->alphaOffset) {
+ m->num = 0;
+ m->mapa[0][CM_RED] = 0xd9;
+ m->mapa[0][CM_GREEN] = 0xd9;
+ m->mapa[0][CM_BLUE] = 0xd9;
} else {
- num = -1;
+ m->num = -1;
}
for(y=0 ; y<blockPtr->height ; y++) {
colores = blockPtr->pixelPtr + blockPtr->offset[0]
+ y * blockPtr->pitch;
for(x=0 ; x<blockPtr->width ; x++) {
- if (!alphaOffset || (colores[alphaOffset] != 0)) {
+ if (!m->alphaOffset || (colores[m->alphaOffset] != 0)) {
red = colores[0];
- green = colores[greenOffset];
- blue = colores[blueOffset];
- if (nuevo(red,green,blue,mapa)) {
- num++;
- if (num >= MAXCOLORMAPSIZE) {
+ green = colores[m->greenOffset];
+ blue = colores[m->blueOffset];
+ if (nuevo(red,green,blue,m)) {
+ m->num++;
+ if (m->num >= MAXCOLORMAPSIZE) {
return;
}
- mapa[num][CM_RED] = red;
- mapa[num][CM_GREEN] = green;
- mapa[num][CM_BLUE] = blue;
+ m->mapa[m->num][CM_RED] = red;
+ m->mapa[m->num][CM_GREEN] = green;
+ m->mapa[m->num][CM_BLUE] = blue;
}
}
- colores += pixelSize;
+ colores += m->pixelSize;
}
}
return;
}
static int
-ReadValue()
+ReadValue(GIFDataSource *m)
{
unsigned int col;
- if (csize == 0) {
+ if (m->csize == 0) {
return EOF;
}
- if (alphaOffset && (pixelo[alphaOffset] == 0)) {
+ if (m->alphaOffset && (m->pixelo[m->alphaOffset] == 0)) {
col = 0;
} else {
- col = color(pixelo[0], pixelo[greenOffset], pixelo[blueOffset], mapa);
+ col = color(
+ m->pixelo[0], m->pixelo[m->greenOffset],m->pixelo[m->blueOffset],m);
}
- pixelo += pixelSize;
- if (--ssize <= 0) {
- ssize = rsize;
- csize--;
- pixelo += pixelPitch - (rsize * pixelSize);
+ m->pixelo += m->pixelSize;
+ if (--m->ssize <= 0) {
+ m->ssize = m->rsize;
+ m->csize--;
+ m->pixelo += m->pixelPitch - (m->rsize * m->pixelSize);
}
return col;
@@ -1709,27 +1714,29 @@
*-----------------------------------------------------------------------
*/
-static int rl_pixel;
-static int rl_basecode;
-static int rl_count;
-static int rl_table_pixel;
-static int rl_table_max;
-static int just_cleared;
-static int out_bits;
-static int out_bits_init;
-static int out_count;
-static int out_bump;
-static int out_bump_init;
-static int out_clear;
-static int out_clear_init;
-static int max_ocodes;
-static int code_clear;
-static int code_eof;
-static unsigned int obuf;
-static int obits;
-static Tcl_Channel ofile;
-static unsigned char oblock[256];
-static int oblen;
+typedef struct {
+ int rl_pixel;
+ int rl_basecode;
+ int rl_count;
+ int rl_table_pixel;
+ int rl_table_max;
+ int just_cleared;
+ int out_bits;
+ int out_bits_init;
+ int out_count;
+ int out_bump;
+ int out_bump_init;
+ int out_clear;
+ int out_clear_init;
+ int max_ocodes;
+ int code_clear;
+ int code_eof;
+ unsigned int obuf;
+ int obits;
+ Tcl_Channel ofile;
+ unsigned char oblock[256];
+ int oblen;
+} GIFEncodeBuf;
/*
* Used only when debugging GIF compression code
@@ -1785,97 +1792,94 @@
#endif
static void
-write_block()
+write_block(GIFEncodeBuf *e)
{
int i;
unsigned char c;
if (MIGIF_VERBOSE) {
- printf("write_block %d:", oblen);
- for (i=0 ; i<oblen ; i++) {
- printf(" %02x", oblock[i]);
+ printf("write_block %d:", e->oblen);
+ for (i=0 ; i<e->oblen ; i++) {
+ printf(" %02x", e->oblock[i]);
}
printf("\n");
}
- c = oblen;
- Tcl_Write(ofile, (char *) &c, 1);
- Tcl_Write(ofile, (char *) &oblock[0], oblen);
- oblen = 0;
+ c = e->oblen;
+ Tcl_Write(e->ofile, (char *) &c, 1);
+ Tcl_Write(e->ofile, (char *) &e->oblock[0], e->oblen);
+ e->oblen = 0;
}
static void
-block_out(c)
- unsigned char c;
+block_out(GIFEncodeBuf *e, unsigned char c)
{
DEBUGMSG(("block_out %s\n", binformat(c, 8)));
- oblock[oblen++] = c;
- if (oblen >= 255) {
- write_block();
+ e->oblock[e->oblen++] = c;
+ if (e->oblen >= 255) {
+ write_block(e);
}
}
static void
-block_flush()
+block_flush(GIFEncodeBuf *e)
{
DEBUGMSG(("block_flush\n"));
- if (oblen > 0) {
- write_block();
+ if (e->oblen > 0) {
+ write_block(e);
}
}
static void
-output(val)
- int val;
+output(GIFEncodeBuf *e, int val)
{
- DEBUGMSG(("output %s [%s %d %d]\n", binformat(val, out_bits),
- binformat(obuf, obits), obits, out_bits));
- obuf |= val << obits;
- obits += out_bits;
- while (obits >= 8) {
- block_out(obuf&0xff);
- obuf >>= 8;
- obits -= 8;
+ DEBUGMSG(("output %s [%s %d %d]\n", binformat(val, e->out_bits),
+ binformat(e->obuf, e->obits), e->obits, e->out_bits));
+ e->obuf |= val << e->obits;
+ e->obits += e->out_bits;
+ while (e->obits >= 8) {
+ block_out(e, e->obuf&0xff);
+ e->obuf >>= 8;
+ e->obits -= 8;
}
- DEBUGMSG(("output leaving [%s %d]\n", binformat(obuf, obits), obits));
+ DEBUGMSG(("output leaving [%s %d]\n",binformat(e->obuf,e->obits),e->obits));
}
static void
-output_flush()
+output_flush(GIFEncodeBuf *e)
{
DEBUGMSG(("output_flush\n"));
- if (obits > 0) {
- block_out(obuf);
+ if (e->obits > 0) {
+ block_out(e, e->obuf);
}
- block_flush();
+ block_flush(e);
}
static void
-did_clear()
+did_clear(GIFEncodeBuf *e)
{
DEBUGMSG(("did_clear\n"));
- out_bits = out_bits_init;
- out_bump = out_bump_init;
- out_clear = out_clear_init;
- out_count = 0;
- rl_table_max = 0;
- just_cleared = 1;
+ e->out_bits = e->out_bits_init;
+ e->out_bump = e->out_bump_init;
+ e->out_clear = e->out_clear_init;
+ e->out_count = 0;
+ e->rl_table_max = 0;
+ e->just_cleared = 1;
}
static void
-output_plain(c)
- int c;
+output_plain(GIFEncodeBuf *e, int c)
{
- DEBUGMSG(("output_plain %s\n", binformat(c, out_bits)));
- just_cleared = 0;
- output(c);
- out_count++;
- if (out_count >= out_bump) {
- out_bits++;
- out_bump += 1 << (out_bits - 1);
- }
- if (out_count >= out_clear) {
- output(code_clear);
- did_clear();
+ DEBUGMSG(("output_plain %s\n", binformat(c, e->out_bits)));
+ e->just_cleared = 0;
+ output(e, c);
+ e->out_count++;
+ if (e->out_count >= e->out_bump) {
+ e->out_bits++;
+ e->out_bump += 1 << (e->out_bits - 1);
+ }
+ if (e->out_count >= e->out_clear) {
+ output(e, e->code_clear);
+ did_clear(e);
}
}
@@ -1928,194 +1932,189 @@
}
static void
-max_out_clear()
+max_out_clear(GIFEncodeBuf *e)
{
- out_clear = max_ocodes;
+ e->out_clear = e->max_ocodes;
}
static void
-reset_out_clear()
+reset_out_clear(GIFEncodeBuf *e)
{
- out_clear = out_clear_init;
- if (out_count >= out_clear) {
- output(code_clear);
- did_clear();
+ e->out_clear = e->out_clear_init;
+ if (e->out_count >= e->out_clear) {
+ output(e, e->code_clear);
+ did_clear(e);
}
}
static void
-rl_flush_fromclear(count)
- int count;
+rl_flush_fromclear(GIFEncodeBuf *e, int count)
{
int n;
DEBUGMSG(("rl_flush_fromclear %d\n", count));
- max_out_clear();
- rl_table_pixel = rl_pixel;
+ max_out_clear(e);
+ e->rl_table_pixel = e->rl_pixel;
n = 1;
while (count > 0) {
if (n == 1) {
- rl_table_max = 1;
- output_plain(rl_pixel);
+ e->rl_table_max = 1;
+ output_plain(e, e->rl_pixel);
count--;
} else if (count >= n) {
- rl_table_max = n;
- output_plain(rl_basecode+n-2);
+ e->rl_table_max = n;
+ output_plain(e, e->rl_basecode+n-2);
count -= n;
} else if (count == 1) {
- rl_table_max++;
- output_plain(rl_pixel);
+ e->rl_table_max++;
+ output_plain(e, e->rl_pixel);
count = 0;
} else {
- rl_table_max++;
- output_plain(rl_basecode+count-2);
+ e->rl_table_max++;
+ output_plain(e, e->rl_basecode+count-2);
count = 0;
}
- if (out_count == 0) {
+ if (e->out_count == 0) {
n = 1;
} else {
n++;
}
}
- reset_out_clear();
- DEBUGMSG(("rl_flush_fromclear leaving table_max=%d\n", rl_table_max));
+ reset_out_clear(e);
+ DEBUGMSG(("rl_flush_fromclear leaving table_max=%d\n", e->rl_table_max));
}
static void
-rl_flush_clearorrep(count)
- int count;
+rl_flush_clearorrep(GIFEncodeBuf *e, int count)
{
int withclr;
DEBUGMSG(("rl_flush_clearorrep %d\n", count));
- withclr = 1 + compute_triangle_count(count, max_ocodes);
+ withclr = 1 + compute_triangle_count(count, e->max_ocodes);
if (withclr < count) {
- output(code_clear);
- did_clear();
- rl_flush_fromclear(count);
+ output(e, e->code_clear);
+ did_clear(e);
+ rl_flush_fromclear(e, count);
} else {
for (; count>0 ; count--) {
- output_plain(rl_pixel);
+ output_plain(e, e->rl_pixel);
}
}
}
static void
-rl_flush_withtable(count)
- int count;
+rl_flush_withtable(GIFEncodeBuf *e, int count)
{
int repmax;
int repleft;
int leftover;
DEBUGMSG(("rl_flush_withtable %d\n", count));
- repmax = count / rl_table_max;
- leftover = count % rl_table_max;
+ repmax = count / e->rl_table_max;
+ leftover = count % e->rl_table_max;
repleft = (leftover ? 1 : 0);
- if (out_count+repmax+repleft > max_ocodes) {
- repmax = max_ocodes - out_count;
- leftover = count - (repmax * rl_table_max);
- repleft = 1 + compute_triangle_count(leftover, max_ocodes);
+ if (e->out_count+repmax+repleft > e->max_ocodes) {
+ repmax = e->max_ocodes - e->out_count;
+ leftover = count - (repmax * e->rl_table_max);
+ repleft = 1 + compute_triangle_count(leftover, e->max_ocodes);
}
DEBUGMSG(("rl_flush_withtable repmax=%d leftover=%d repleft=%d\n",
repmax, leftover, repleft));
- if (1+(int)compute_triangle_count(count, max_ocodes) < repmax+repleft) {
- output(code_clear);
- did_clear();
- rl_flush_fromclear(count);
+ if (1+(int)compute_triangle_count(count, e->max_ocodes) < repmax+repleft) {
+ output(e, e->code_clear);
+ did_clear(e);
+ rl_flush_fromclear(e, count);
return;
}
- max_out_clear();
+ max_out_clear(e);
for (; repmax>0 ; repmax--) {
- output_plain(rl_basecode + rl_table_max - 2);
+ output_plain(e, e->rl_basecode + e->rl_table_max - 2);
}
if (leftover) {
- if (just_cleared) {
- rl_flush_fromclear(leftover);
+ if (e->just_cleared) {
+ rl_flush_fromclear(e, leftover);
} else if (leftover == 1) {
- output_plain(rl_pixel);
+ output_plain(e, e->rl_pixel);
} else {
- output_plain(rl_basecode + leftover - 2);
+ output_plain(e, e->rl_basecode + leftover - 2);
}
}
- reset_out_clear();
+ reset_out_clear(e);
}
static void
-rl_flush()
+rl_flush(GIFEncodeBuf *e)
{
- DEBUGMSG(("rl_flush [ %d %d\n", rl_count, rl_pixel));
- if (rl_count == 1) {
- output_plain(rl_pixel);
- rl_count = 0;
+ DEBUGMSG(("rl_flush [ %d %d\n", e->rl_count, e->rl_pixel));
+ if (e->rl_count == 1) {
+ output_plain(e, e->rl_pixel);
+ e->rl_count = 0;
DEBUGMSG(("rl_flush ]\n"));
return;
}
- if (just_cleared) {
- rl_flush_fromclear(rl_count);
- } else if ((rl_table_max < 2) || (rl_table_pixel != rl_pixel)) {
- rl_flush_clearorrep(rl_count);
+ if (e->just_cleared) {
+ rl_flush_fromclear(e, e->rl_count);
+ } else if ((e->rl_table_max < 2) || (e->rl_table_pixel != e->rl_pixel)) {
+ rl_flush_clearorrep(e, e->rl_count);
} else {
- rl_flush_withtable(rl_count);
+ rl_flush_withtable(e, e->rl_count);
}
DEBUGMSG(("rl_flush ]\n"));
- rl_count = 0;
+ e->rl_count = 0;
}
static void
-compress(init_bits, handle, readValue)
- int init_bits;
- Tcl_Channel handle;
- ifunptr readValue;
+compress(int init_bits, Tcl_Channel handle, GIFDataSource *m)
{
+ GIFEncodeBuf GEB, *e = &GEB;
int c;
- ofile = handle;
- obuf = 0;
- obits = 0;
- oblen = 0;
- code_clear = 1 << (init_bits - 1);
- code_eof = code_clear + 1;
- rl_basecode = code_eof + 1;
- out_bump_init = (1 << (init_bits - 1)) - 1;
+ e->ofile = handle;
+ e->obuf = 0;
+ e->obits = 0;
+ e->oblen = 0;
+ e->code_clear = 1 << (init_bits - 1);
+ e->code_eof = e->code_clear + 1;
+ e->rl_basecode = e->code_eof + 1;
+ e->out_bump_init = (1 << (init_bits - 1)) - 1;
/*
* For images with a lot of runs, making out_clear_init larger
* will give better compression.
*/
- out_clear_init = (init_bits <= 3) ? 9 : (out_bump_init-1);
+ e->out_clear_init = (init_bits <= 3) ? 9 : (e->out_bump_init-1);
#ifdef MIGIF_DEBUGGING_ENVARS
{
const char *ocienv;
ocienv = getenv("MIGIF_OUT_CLEAR_INIT");
if (ocienv) {
- out_clear_init = atoi(ocienv);
- DEBUGMSG(("[overriding out_clear_init to %d]\n", out_clear_init));
+ e->out_clear_init = atoi(ocienv);
+ DEBUGMSG(("[overriding out_clear_init to %d]\n",e->out_clear_init));
}
}
#endif
- out_bits_init = init_bits;
- max_ocodes = (1 << GIFBITS) - ((1 << (out_bits_init - 1)) + 3);
- did_clear();
- output(code_clear);
- rl_count = 0;
+ e->out_bits_init = init_bits;
+ e->max_ocodes = (1 << GIFBITS) - ((1 << (e->out_bits_init - 1)) + 3);
+ did_clear(e);
+ output(e, e->code_clear);
+ e->rl_count = 0;
while (1) {
- c = readValue();
- if ((rl_count > 0) && (c != rl_pixel)) {
- rl_flush();
+ c = ReadValue(m);
+ if ((e->rl_count > 0) && (c != e->rl_pixel)) {
+ rl_flush(e);
}
if (c == EOF) {
break;
}
- if (rl_pixel == c) {
- rl_count++;
+ if (e->rl_pixel == c) {
+ e->rl_count++;
} else {
- rl_pixel = c;
- rl_count = 1;
+ e->rl_pixel = c;
+ e->rl_count = 1;
}
}
- output(code_eof);
- output_flush();
+ output(e, e->code_eof);
+ output_flush(e);
}
/*