From d7384c2e07bae442f451728742f005f2f41b38f4 Mon Sep 17 00:00:00 2001 From: dan Date: Sun, 25 Jul 1999 20:28:30 +0000 Subject: [PATCH] Fixed buggy RSaveXPM() in wraster library. --- ChangeLog | 2 ++ wrlib/nxpm.c | 23 ++++++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index afee7043..eaee6584 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,8 @@ Changes since version 0.60.0: - fixed crash when displaying workspace name in some positions. - fixed compilation problem when WS_INDICATOR was defined. - added edge attraction. See NEWS. +- fixed buggy RSaveXPM() in wraster library. Its output was messed up, and + caused crashes for icons with more than 1024 colors. Changes since version 0.53.0: diff --git a/wrlib/nxpm.c b/wrlib/nxpm.c index c62c8db7..7c66a0e4 100644 --- a/wrlib/nxpm.c +++ b/wrlib/nxpm.c @@ -483,13 +483,13 @@ addcolor(XPMColor **list, unsigned r, unsigned g, unsigned b, int *colors) static char* -index2str(char *buffer, int index, int colorCount) +index2str(char *buffer, int index, int charsPerPixel) { int i; - for (i=0; i>= 5; + index >>= 6; } buffer[i] = 0; @@ -498,7 +498,7 @@ index2str(char *buffer, int index, int colorCount) static void -outputcolormap(FILE *file, XPMColor *colormap, int colorCount) +outputcolormap(FILE *file, XPMColor *colormap, int charsPerPixel) { int index; char buf[128]; @@ -509,7 +509,7 @@ outputcolormap(FILE *file, XPMColor *colormap, int colorCount) for (index=0; colormap!=NULL; colormap=colormap->next,index++) { colormap->index = index; fprintf(file, "\"%s c #%02x%02x%02x\",\n", - index2str(buf, index, colorCount), colormap->red, + index2str(buf, index, charsPerPixel), colormap->red, colormap->green, colormap->blue); } } @@ -536,6 +536,7 @@ RSaveXPM(RImage *image, char *filename) FILE *file; int x, y; int colorCount=0; + int charsPerPixel; XPMColor *colormap = NULL; XPMColor *tmpc; int i; @@ -572,13 +573,17 @@ RSaveXPM(RImage *image, char *filename) } } + charsPerPixel = 1; + while ((1 << charsPerPixel*6) < colorCount) + charsPerPixel++; + /* write header info */ fprintf(file, "\"%i %i %i %i\",\n", image->width, image->height, - colorCount, colorCount/64+1); + colorCount, charsPerPixel); /* write colormap data */ if (image->data[3]) { - for (i=0; idata[0]; g = image->data[1]; @@ -604,7 +609,7 @@ RSaveXPM(RImage *image, char *filename) if (!a || *a++>127) { tmpc = lookfor(colormap, (unsigned)*r<<16|(unsigned)*g<<8|(unsigned)*b); - fprintf(file, index2str(buf, tmpc->index, colorCount)); + fprintf(file, index2str(buf, tmpc->index, charsPerPixel)); } else { fprintf(file, transp); }