Attachment "tkImgGIF.diff" to
ticket [957190f1]
added by
chw
2024-07-25 08:46:01.
Index: generic/tkImgGIF.c
==================================================================
--- generic/tkImgGIF.c
+++ generic/tkImgGIF.c
@@ -216,10 +216,12 @@
int pixelSize;
int pixelPitch;
int greenOffset;
int blueOffset;
int alphaOffset;
+ int mask;
+ int madd;
int num;
unsigned char mapa[MAXCOLORMAPSIZE][3];
} GifWriterState;
typedef int (* ifunptr) (GifWriterState *statePtr);
@@ -434,11 +436,12 @@
}
if (i == (argc-1)) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"no value given for \"%s\" option",
Tcl_GetString(objv[i])));
- Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "OPT_VALUE", NULL);
+ Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "OPT_VALUE",
+ (char *) NULL);
return TCL_ERROR;
}
if (Tcl_GetIntFromObj(interp, objv[++i], &index) != TCL_OK) {
return TCL_ERROR;
}
@@ -449,17 +452,19 @@
*/
if (!ReadGIFHeader(gifConfPtr, chan, &fileWidth, &fileHeight)) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"couldn't read GIF header from file \"%s\"", fileName));
- Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "HEADER", NULL);
+ Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "HEADER",
+ (char *) NULL);
return TCL_ERROR;
}
if ((fileWidth <= 0) || (fileHeight <= 0)) {
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"GIF image file \"%s\" has dimension(s) <= 0", fileName));
- Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "BOGUS_SIZE", NULL);
+ Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "BOGUS_SIZE",
+ (char *) NULL);
return TCL_ERROR;
}
/*
* Get the general colormap information.
@@ -469,20 +474,22 @@
/*
* Bug [865af0148c]: 3 bytes should be there, but data ended before
*/
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"GIF file truncated", -1));
- Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "TRUNCATED", NULL);
+ Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "TRUNCATED",
+ (char *) NULL);
return TCL_ERROR;
}
bitPixel = 2 << (buf[0] & 0x07);
if (BitSet(buf[0], LOCALCOLORMAP)) { /* Global Colormap */
if (!ReadColorMap(gifConfPtr, chan, bitPixel, colorMap)) {
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"error reading color map", -1));
- Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "COLOR_MAP", NULL);
+ Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "COLOR_MAP",
+ (char *) NULL);
return TCL_ERROR;
}
}
if ((srcX + width) > fileWidth) {
@@ -517,19 +524,20 @@
*/
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"premature end of image data for this index", -1));
Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "PREMATURE_END",
- NULL);
+ (char *) NULL);
goto error;
}
switch (buf[0]) {
case GIF_TERMINATOR:
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"no image data for this index", -1));
- Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "NO_DATA", NULL);
+ Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "NO_DATA",
+ (char *) NULL);
goto error;
case GIF_EXTENSION:
/*
* This is a GIF extension.
@@ -538,29 +546,29 @@
if (Fread(gifConfPtr, buf, 1, 1, chan) != 1) {
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"error reading extension function code in GIF image",
-1));
Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "BAD_EXT",
- NULL);
+ (char *) NULL);
goto error;
}
if (DoExtension(gifConfPtr, chan, buf[0],
gifConfPtr->workingBuffer, &transparent) < 0) {
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"error reading extension in GIF image", -1));
Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "BAD_EXT",
- NULL);
+ (char *) NULL);
goto error;
}
continue;
case GIF_START:
if (Fread(gifConfPtr, buf, 1, 9, chan) != 9) {
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"couldn't read left/top/width/height in GIF image",
-1));
Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "DIMENSIONS",
- NULL);
+ (char *) NULL);
goto error;
}
break;
default:
/*
@@ -587,11 +595,11 @@
if (BitSet(buf[8], LOCALCOLORMAP)) {
if (!ReadColorMap(gifConfPtr, chan, bitPixel, colorMap)) {
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"error reading color map", -1));
Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF",
- "COLOR_MAP", NULL);
+ "COLOR_MAP", (char *) NULL);
goto error;
}
}
/*
@@ -649,11 +657,12 @@
if (BitSet(buf[8], LOCALCOLORMAP)) {
if (!ReadColorMap(gifConfPtr, chan, bitPixel, colorMap)) {
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"error reading color map", -1));
- Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "COLOR_MAP", NULL);
+ Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "COLOR_MAP",
+ (char *) NULL);
goto error;
}
}
/*
@@ -1077,11 +1086,12 @@
return TCL_ERROR;
}
if (initialCodeSize > MAX_LWZ_BITS) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("malformed image", -1));
- Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "MALFORMED", NULL);
+ Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "MALFORMED",
+ (char *) NULL);
return TCL_ERROR;
}
if (transparent != -1) {
cmap[transparent][CM_RED] = 0;
@@ -1766,11 +1776,12 @@
state.pixelOffset = blockPtr->pixelPtr + blockPtr->offset[0];
state.pixelPitch = blockPtr->pitch;
SaveMap(&state, blockPtr);
if (state.num >= MAXCOLORMAPSIZE) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("too many colors", -1));
- Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "COLORFUL", NULL);
+ Tcl_SetErrorCode(interp, "TK", "IMAGE", "GIF", "COLORFUL",
+ (char *) NULL);
return TCL_ERROR;
}
if (state.num<2) {
state.num = 2;
}
@@ -1867,10 +1878,16 @@
GifWriterState *statePtr,
int red, int green, int blue)
{
int x = (statePtr->alphaOffset != 0);
+ red &= statePtr->mask;
+ green &= statePtr->mask;
+ blue &= statePtr->mask;
+ red += statePtr->madd;
+ green += statePtr->madd;
+ blue += statePtr->madd;
for (; x <= MAXCOLORMAPSIZE; x++) {
if ((statePtr->mapa[x][CM_RED] == red) &&
(statePtr->mapa[x][CM_GREEN] == green) &&
(statePtr->mapa[x][CM_BLUE] == blue)) {
return x;
@@ -1900,12 +1917,13 @@
SaveMap(
GifWriterState *statePtr,
Tk_PhotoImageBlock *blockPtr)
{
unsigned char *colores;
- int x, y;
+ int retries, x, y;
unsigned char red, green, blue;
+ static const int madd[] = { 0, 0, 1, 2, 3, 7, 15, 31 };
if (statePtr->alphaOffset) {
statePtr->num = 0;
statePtr->mapa[0][CM_RED] = DEFAULT_BACKGROUND_VALUE;
statePtr->mapa[0][CM_GREEN] = DEFAULT_BACKGROUND_VALUE;
@@ -1912,21 +1930,34 @@
statePtr->mapa[0][CM_BLUE] = DEFAULT_BACKGROUND_VALUE;
} else {
statePtr->num = -1;
}
+ retries = 0;
+ statePtr->mask = 0xff;
+ statePtr->madd = 0;
+again:
for (y=0 ; y<blockPtr->height ; y++) {
colores = blockPtr->pixelPtr + blockPtr->offset[0] + y*blockPtr->pitch;
for (x=0 ; x<blockPtr->width ; x++) {
if (!statePtr->alphaOffset || colores[statePtr->alphaOffset]!=0) {
- red = colores[0];
- green = colores[statePtr->greenOffset];
- blue = colores[statePtr->blueOffset];
+ red = colores[0] & statePtr->mask;
+ green = colores[statePtr->greenOffset] & statePtr->mask;
+ blue = colores[statePtr->blueOffset] & statePtr->mask;
+ red += statePtr->madd;
+ green += statePtr->madd;
+ blue += statePtr->madd;
if (IsNewColor(statePtr, red, green, blue)) {
statePtr->num++;
if (statePtr->num >= MAXCOLORMAPSIZE) {
- return;
+ if (++retries > 6) {
+ return;
+ }
+ statePtr->num = statePtr->alphaOffset ? 0 : -1;
+ statePtr->mask = statePtr->mask << 1;
+ statePtr->madd = madd[retries];
+ goto again;
}
statePtr->mapa[statePtr->num][CM_RED] = red;
statePtr->mapa[statePtr->num][CM_GREEN] = green;
statePtr->mapa[statePtr->num][CM_BLUE] = blue;
}