goto fail1;
}
- im = gdImageCreate (*sx, *sy);
+ im = gdImageCreateTrueColor(*sx, *sy);
if (im == NULL)
{
GD2_DBG (printf ("Could not create gdImage\n"));
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
{
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);
}
else
{
- chunkData[chunkLen++] = p;
+ chunkData[chunkLen++] = im->pixels[y][x];
}
};
}
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;
+ my_cquantize_ptr cquantize = 0;
+ int i;
+ size_t arraysize;
+ if (!im->trueColor || colorsWanted <= 0) {
+ /* Nothing to do! */
+ return;
}
- 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;
- }
+
+ if (colorsWanted > gdMaxColors) {
+ colorsWanted = gdMaxColors;
}
- }
- 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];
- }
+ im->pixels = gdCalloc (sizeof (unsigned char *), im->sy);
+
+ for (i = 0; i < im->sy; i++) {
+ im->pixels[i] = gdCalloc (sizeof (unsigned char *), im->sx);
}
- for (i = 0; (i < im->colorsTotal); i++)
- {
- if (im->alpha[i] == mt)
- {
- im->alpha[i] = gdAlphaTransparent;
- }
+ 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));
+ }
}
- }
- 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];
- }
+
+ 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);
}
- for (i = 0; (i < im->colorsTotal); i++)
- {
- if (im->alpha[i] == mo)
- {
- im->alpha[i] = gdAlphaOpaque;
- }
+ 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;
+ }
+ }
}
- }
- /* 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 (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;
+ }
+ }
}
- if (im->pixels)
- {
- gdFree (im->pixels);
+
+ /* 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]);
}
- 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 (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]);
}
- }
- 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);
- }
+ 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