iliaa Wed Dec 4 12:28:56 2002 EDT Modified files: (Branch: PHP_4_3) /php4/ext/gd gd.c /php4/ext/gd/libgd gd_gd2.c gd_png.c gd_topal.c Log: MFH (truecolor fixes + gdImageTrueColorToPalette() fix).
Index: php4/ext/gd/gd.c diff -u php4/ext/gd/gd.c:1.221.2.2 php4/ext/gd/gd.c:1.221.2.3 --- php4/ext/gd/gd.c:1.221.2.2 Sat Nov 23 06:26:13 2002 +++ php4/ext/gd/gd.c Wed Dec 4 12:28:55 2002 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: gd.c,v 1.221.2.2 2002/11/23 11:26:13 moriyoshi Exp $ */ +/* $Id: gd.c,v 1.221.2.3 2002/12/04 17:28:55 iliaa Exp $ */ /* gd 1.2 is copyright 1994, 1995, Quest Protein Database Center, Cold Spring Harbor Labs. */ @@ -1525,6 +1525,12 @@ } (*func_p)(im, i, fp); break; + case PHP_GDIMG_TYPE_GD: + if(im->trueColor){ + gdImageTrueColorToPalette(im,1,256); + } + (*func_p)(im, fp); + break; default: (*func_p)(im, fp); break; @@ -1555,6 +1561,12 @@ if(gdImageRed(im, i) == 0) break; } (*func_p)(im, q, tmp); + break; + case PHP_GDIMG_TYPE_GD: + if (im->trueColor) { + gdImageTrueColorToPalette(im,1,256); + } + (*func_p)(im, tmp); break; default: (*func_p)(im, tmp); Index: php4/ext/gd/libgd/gd_gd2.c diff -u php4/ext/gd/libgd/gd_gd2.c:1.4 php4/ext/gd/libgd/gd_gd2.c:1.4.2.1 --- php4/ext/gd/libgd/gd_gd2.c:1.4 Tue Nov 12 08:12:58 2002 +++ php4/ext/gd/libgd/gd_gd2.c Wed Dec 4 12:28:55 2002 @@ -184,7 +184,7 @@ goto fail1; } - im = gdImageCreate (*sx, *sy); + im = gdImageCreateTrueColor(*sx, *sy); if (im == NULL) { GD2_DBG (printf ("Could not create gdImage\n")); @@ -286,144 +286,98 @@ bytesPerPixel = im->trueColor ? 4 : 1; nc = ncx * ncy; - if (fmt == GD2_FMT_COMPRESSED) - { - /* Find the maximum compressed chunk size. */ - compMax = 0; - for (i = 0; (i < nc); i++) - { - if (chunkIdx[i].size > compMax) - { - compMax = chunkIdx[i].size; - }; - }; - compMax++; - - /* Allocate buffers */ - chunkMax = cs * bytesPerPixel * cs; - chunkBuf = gdCalloc (chunkMax, 1); - compBuf = gdCalloc (compMax, 1); - GD2_DBG (printf ("Largest compressed chunk is %d bytes\n", compMax)); - }; - -/* if ( (ncx != sx / cs) || (ncy != sy / cs)) { */ -/* goto fail2; */ -/* }; */ - - /* Read the data... */ - for (cy = 0; (cy < ncy); cy++) - { - for (cx = 0; (cx < ncx); cx++) - { - - ylo = cy * cs; - yhi = ylo + cs; - if (yhi > im->sy) - { - yhi = im->sy; - }; - - GD2_DBG (printf ("Processing Chunk %d (%d, %d), y from %d to %d\n", chunkNum, cx, cy, ylo, yhi)); - - if (fmt == GD2_FMT_COMPRESSED) - { - - chunkLen = chunkMax; - - if (!_gd2ReadChunk (chunkIdx[chunkNum].offset, - compBuf, - chunkIdx[chunkNum].size, - chunkBuf, &chunkLen, in)) - { - GD2_DBG (printf ("Error reading comproessed chunk\n")); - goto fail2; - }; + if (fmt == GD2_FMT_COMPRESSED) { + /* Find the maximum compressed chunk size. */ + compMax = 0; + for (i = 0; i < nc; i++) { + if (chunkIdx[i].size > compMax) { + compMax = chunkIdx[i].size; + } + } + compMax++; - chunkPos = 0; - }; + /* Allocate buffers */ + chunkMax = cs * bytesPerPixel * cs; + chunkBuf = gdCalloc (chunkMax, 1); + compBuf = gdCalloc (compMax, 1); + GD2_DBG(printf("Largest compressed chunk is %d bytes\n", compMax)); + } - for (y = ylo; (y < yhi); y++) - { + /* Read the data... */ + for (cy = 0; cy < ncy; cy++) { + for (cx = 0; cx < ncx; cx++) { + ylo = cy * cs; + yhi = ylo + cs; + if (yhi > im->sy) { + yhi = im->sy; + } + GD2_DBG(printf("Processing Chunk %d (%d, %d), y from %d to +%d\n", chunkNum, cx, cy, ylo, yhi)); - xlo = cx * cs; - xhi = xlo + cs; - if (xhi > im->sx) - { - xhi = im->sx; - }; - /*GD2_DBG(printf("y=%d: ",y)); */ - if (fmt == GD2_FMT_RAW) - { - for (x = xlo; x < xhi; x++) - { + if (fmt == GD2_FMT_COMPRESSED) { + chunkLen = chunkMax; - if (im->trueColor) - { - if (!gdGetInt (&im->tpixels[y][x], in)) - { - /*printf("EOF while reading\n"); */ - /*gdImageDestroy(im); */ - /*return 0; */ - im->tpixels[y][x] = 0; - } + if (!_gd2ReadChunk (chunkIdx[chunkNum].offset, +compBuf, chunkIdx[chunkNum].size, chunkBuf, &chunkLen, in)) { + GD2_DBG(printf("Error reading comproessed +chunk\n")); + goto fail2; + } + chunkPos = 0; } - else - { - int ch; - if (!gdGetByte (&ch, in)) - { - /*printf("EOF while reading\n"); */ - /*gdImageDestroy(im); */ - /*return 0; */ - ch = 0; - } - im->pixels[y][x] = ch; + + for (y = ylo; y < yhi; y++) { + xlo = cx * cs; + xhi = xlo + cs; + if (xhi > im->sx) { + xhi = im->sx; + } + + if (fmt == GD2_FMT_RAW) { + for (x = xlo; x < xhi; x++) { + if (im->trueColor) { + if (!gdGetInt +(&im->tpixels[y][x], in)) { + im->tpixels[y][x] = 0; + } + } else { + int ch; + if (!gdGetByte (&ch, in)) { + ch = 0; + } + im->pixels[y][x] = ch; + } + } + } else { + for (x = xlo; x < xhi; x++) { + if (im->trueColor) { + int a = chunkBuf[chunkPos++] +<< 24; + int r = chunkBuf[chunkPos++] +<< 16; + int g = chunkBuf[chunkPos++] +<< 8; + int b = chunkBuf[chunkPos++]; + im->tpixels[y][x] = a + r + g ++ b; + } else { + im->pixels[y][x] = +chunkBuf[chunkPos++]; + } + } + } } - } + chunkNum++; } - else - { - for (x = xlo; x < xhi; x++) - { - if (im->trueColor) - { - /* 2.0.1: work around a gcc bug by being verbose. - TBB */ - int a = chunkBuf[chunkPos++] << 24; - int r = chunkBuf[chunkPos++] << 16; - int g = chunkBuf[chunkPos++] << 8; - int b = chunkBuf[chunkPos++]; - im->pixels[y][x] = a + r + g + b; - } - else - { - im->pixels[y][x] = chunkBuf[chunkPos++]; - } - }; - }; - /*GD2_DBG(printf("\n")); */ - }; - chunkNum++; - }; - }; + } - GD2_DBG (printf ("Freeing memory\n")); + GD2_DBG(printf("Freeing memory\n")); - gdFree (chunkBuf); - gdFree (compBuf); - gdFree (chunkIdx); + gdFree (chunkBuf); + gdFree (compBuf); + gdFree (chunkIdx); - GD2_DBG (printf ("Done\n")); + GD2_DBG(printf("Done\n")); - return im; + return im; fail2: - gdImageDestroy (im); - gdFree (chunkBuf); - gdFree (compBuf); - gdFree (chunkIdx); - return 0; - + gdImageDestroy (im); + gdFree (chunkBuf); + gdFree (compBuf); + gdFree (chunkIdx); + return 0; } gdImagePtr @@ -818,10 +772,10 @@ { for (x = xlo; x < xhi; x++) { - int p = im->pixels[y][x]; - /*GD2_DBG(printf("%d...",x)); */ + GD2_DBG(printf("%d...",x)); if (im->trueColor) { + int p = im->tpixels[y][x]; chunkData[chunkLen++] = gdTrueColorGetAlpha (p); chunkData[chunkLen++] = gdTrueColorGetRed (p); chunkData[chunkLen++] = gdTrueColorGetGreen (p); @@ -829,7 +783,7 @@ } else { - chunkData[chunkLen++] = p; + chunkData[chunkLen++] = im->pixels[y][x]; } }; } Index: php4/ext/gd/libgd/gd_png.c diff -u php4/ext/gd/libgd/gd_png.c:1.4 php4/ext/gd/libgd/gd_png.c:1.4.2.1 --- php4/ext/gd/libgd/gd_png.c:1.4 Tue Oct 29 18:08:01 2002 +++ php4/ext/gd/libgd/gd_png.c Wed Dec 4 12:28:55 2002 @@ -716,20 +716,10 @@ { png_bytep *row_pointers; row_pointers = gdMalloc (sizeof (png_bytep) * height); - if (row_pointers == NULL) - { - fprintf (stderr, "gd-png error: unable to allocate row_pointers\n"); - } for (j = 0; j < height; ++j) { - if ((row_pointers[j] = (png_bytep) gdMalloc (width)) == NULL) - { - fprintf (stderr, "gd-png error: unable to allocate rows\n"); - for (i = 0; i < j; ++i) - gdFree (row_pointers[i]); - return; - } - for (i = 0; i < width; ++i) + row_pointers[j] = (png_bytep) gdMalloc (width); + for (i = 0; i < width; ++i) row_pointers[j][i] = mapping[im->pixels[j][i]]; } Index: php4/ext/gd/libgd/gd_topal.c diff -u php4/ext/gd/libgd/gd_topal.c:1.8 php4/ext/gd/libgd/gd_topal.c:1.8.2.1 --- php4/ext/gd/libgd/gd_topal.c:1.8 Wed Oct 16 18:34:44 2002 +++ php4/ext/gd/libgd/gd_topal.c Wed Dec 4 12:28:55 2002 @@ -1490,200 +1490,118 @@ void gdImageTrueColorToPalette (gdImagePtr im, int dither, int colorsWanted) { - my_cquantize_ptr cquantize = 0; - int i; - size_t arraysize; - if (!im->trueColor || colorsWanted <= 0) - { - /* Nothing to do! */ - return; - } - if (colorsWanted > gdMaxColors) - { - colorsWanted = gdMaxColors; - } - im->pixels = gdCalloc (sizeof (unsigned char *), im->sy); - if (!im->pixels) - { - /* No can do */ - goto outOfMemory; - } - for (i = 0; (i < im->sy); i++) - { - im->pixels[i] = gdCalloc (sizeof (unsigned char *), im->sx); - if (!im->pixels[i]) - { - goto outOfMemory; - } - } - cquantize = (my_cquantize_ptr) gdCalloc (sizeof (my_cquantizer), 1); - if (!cquantize) - { - /* No can do */ - goto outOfMemory; - } - /* Allocate the histogram/inverse colormap storage */ - cquantize->histogram = (hist4d) gdMalloc (HIST_C0_ELEMS * sizeof (hist3d)); - for (i = 0; i < HIST_C0_ELEMS; i++) - { - int j; - cquantize->histogram[i] = (hist3d) gdCalloc (HIST_C1_ELEMS, - sizeof (hist2d)); - if (!cquantize->histogram[i]) - { - goto outOfMemory; - } - for (j = 0; (j < HIST_C1_ELEMS); j++) - { - cquantize->histogram[i][j] = (hist2d) gdCalloc (HIST_C2_ELEMS * HIST_C3_ELEMS, - sizeof (histcell)); - if (!cquantize->histogram[i][j]) - { - goto outOfMemory; - } - } - } - cquantize->fserrors = (FSERRPTR) gdMalloc (4 * sizeof (FSERROR)); - init_error_limit (im, cquantize); - arraysize = (size_t) ((im->sx + 2) * - (4 * sizeof (FSERROR))); - /* Allocate Floyd-Steinberg workspace. */ - cquantize->fserrors = gdCalloc (arraysize, 1); - if (!cquantize->fserrors) - { - goto outOfMemory; - } - cquantize->on_odd_row = FALSE; - - /* Do the work! */ - zeroHistogram (cquantize->histogram); - prescan_quantize (im, cquantize); - select_colors (im, cquantize, colorsWanted); - /* TBB HACK REMOVE */ -#if 0 - { - FILE *out = fopen ("palettemap.png", "wb"); - int i; - gdImagePtr im2 = gdImageCreateTrueColor (256, 256); - for (i = 0; (i < 256); i++) - { - gdImageFilledRectangle (im2, (i % 16) * 16, (i / 16) * 16, - (i % 16) * 16 + 15, (i / 16) * 16 + 15, - gdTrueColorAlpha (im->red[i], im->green[i], - im->blue[i], im->alpha[i])); - } - gdImagePng (im2, out); - fclose (out); - gdImageDestroy (im2); - } -#endif - zeroHistogram (cquantize->histogram); - if (dither) - { - pass2_fs_dither (im, cquantize); - } - else - { - pass2_no_dither (im, cquantize); - } - if (cquantize->transparentIsPresent) - { - int mt = -1; - int mtIndex = -1; - for (i = 0; (i < im->colorsTotal); i++) - { - if (im->alpha[i] > mt) - { - mtIndex = i; - mt = im->alpha[i]; - } - } - for (i = 0; (i < im->colorsTotal); i++) - { - if (im->alpha[i] == mt) - { - im->alpha[i] = gdAlphaTransparent; - } - } - } - if (cquantize->opaqueIsPresent) - { - int mo = 128; - int moIndex = -1; - for (i = 0; (i < im->colorsTotal); i++) - { - if (im->alpha[i] < mo) - { - moIndex = i; - mo = im->alpha[i]; - } - } - for (i = 0; (i < im->colorsTotal); i++) - { - if (im->alpha[i] == mo) - { - im->alpha[i] = gdAlphaOpaque; - } - } - } - /* Success! Get rid of the truecolor image data. */ - im->trueColor = 0; - /* Junk the truecolor pixels */ - for (i = 0; i < im->sy; i++) - { - gdFree (im->tpixels[i]); - } - gdFree (im->tpixels); - im->tpixels = 0; - /* Tediously free stuff. */ -outOfMemory: - if (im->trueColor) - { - /* On failure only */ - for (i = 0; i < im->sy; i++) - { - if (im->pixels[i]) - { - gdFree (im->pixels[i]); - } - } - if (im->pixels) - { - gdFree (im->pixels); - } - im->pixels = 0; - } - for (i = 0; i < HIST_C0_ELEMS; i++) - { - if (cquantize->histogram[i]) - { - int j; - for (j = 0; j < HIST_C1_ELEMS; j++) - { - if (cquantize->histogram[i][j]) - { - gdFree (cquantize->histogram[i][j]); - } - } - gdFree (cquantize->histogram[i]); - } - } - if (cquantize->histogram) - { - gdFree (cquantize->histogram); - } - if (cquantize->fserrors) - { - gdFree (cquantize->fserrors); - } - if (cquantize->error_limiter_storage) - { - gdFree (cquantize->error_limiter_storage); - } - if (cquantize) - { - gdFree (cquantize); - } + my_cquantize_ptr cquantize = 0; + int i; + size_t arraysize; + if (!im->trueColor || colorsWanted <= 0) { + /* Nothing to do! */ + return; + } + + if (colorsWanted > gdMaxColors) { + colorsWanted = gdMaxColors; + } + + im->pixels = gdCalloc (sizeof (unsigned char *), im->sy); + + for (i = 0; i < im->sy; i++) { + im->pixels[i] = gdCalloc (sizeof (unsigned char *), im->sx); + } + cquantize = (my_cquantize_ptr) gdCalloc (sizeof (my_cquantizer), 1); + + /* Allocate the histogram/inverse colormap storage */ + cquantize->histogram = (hist4d) gdMalloc (HIST_C0_ELEMS * sizeof (hist3d)); + for (i = 0; i < HIST_C0_ELEMS; i++) { + int j; + cquantize->histogram[i] = (hist3d) gdCalloc (HIST_C1_ELEMS, sizeof +(hist2d)); + for (j = 0; j < HIST_C1_ELEMS; j++) { + cquantize->histogram[i][j] = (hist2d) gdCalloc (HIST_C2_ELEMS +* HIST_C3_ELEMS, sizeof (histcell)); + } + } + + cquantize->fserrors = (FSERRPTR) gdMalloc (4 * sizeof (FSERROR)); + init_error_limit (im, cquantize); + arraysize = (size_t) ((im->sx + 2) * (4 * sizeof (FSERROR))); + gdFree(cquantize->fserrors); + /* Allocate Floyd-Steinberg workspace. */ + cquantize->fserrors = gdCalloc (arraysize, 1); + cquantize->on_odd_row = FALSE; + + /* Do the work! */ + zeroHistogram (cquantize->histogram); + prescan_quantize (im, cquantize); + select_colors (im, cquantize, colorsWanted); + + zeroHistogram (cquantize->histogram); + if (dither) { + pass2_fs_dither (im, cquantize); + } else { + pass2_no_dither (im, cquantize); + } + if (cquantize->transparentIsPresent) { + int mt = -1; + int mtIndex = -1; + for (i = 0; i < im->colorsTotal; i++) { + if (im->alpha[i] > mt) { + mtIndex = i; + mt = im->alpha[i]; + } + } + for (i = 0; i < im->colorsTotal; i++) { + if (im->alpha[i] == mt) { + im->alpha[i] = gdAlphaTransparent; + } + } + } + if (cquantize->opaqueIsPresent) { + int mo = 128; + int moIndex = -1; + for (i = 0; i < im->colorsTotal; i++) { + if (im->alpha[i] < mo) { + moIndex = i; + mo = im->alpha[i]; + } + } + for (i = 0; i < im->colorsTotal; i++) { + if (im->alpha[i] == mo) { + im->alpha[i] = gdAlphaOpaque; + } + } + } + + /* Success! Get rid of the truecolor image data. */ + im->trueColor = 0; + /* Junk the truecolor pixels */ + for (i = 0; i < im->sy; i++) { + gdFree(im->tpixels[i]); + } + gdFree (im->tpixels); + im->tpixels = 0; + /* Tediously free stuff. */ + + for (i = 0; i < HIST_C0_ELEMS; i++) { + if (cquantize->histogram[i]) { + int j; + for (j = 0; j < HIST_C1_ELEMS; j++) { + if (cquantize->histogram[i][j]) { + gdFree(cquantize->histogram[i][j]); + } + } + gdFree(cquantize->histogram[i]); + } + } + if (cquantize->histogram) { + gdFree(cquantize->histogram); + } + if (cquantize->fserrors) { + gdFree(cquantize->fserrors); + } + if (cquantize->error_limiter_storage) { + gdFree(cquantize->error_limiter_storage); + } + if (cquantize) { + gdFree(cquantize); + } } /* bring the palette colors in im2 to be closer to im1
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php