]> granicus.if.org Git - php/commitdiff
MFH (truecolor fixes + gdImageTrueColorToPalette() fix).
authorIlia Alshanetsky <iliaa@php.net>
Wed, 4 Dec 2002 17:28:55 +0000 (17:28 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Wed, 4 Dec 2002 17:28:55 +0000 (17:28 +0000)
ext/gd/gd.c
ext/gd/libgd/gd_gd2.c
ext/gd/libgd/gd_png.c
ext/gd/libgd/gd_topal.c

index 22ea92b9c1c62666fa73c02da6f3374b6b62290c..ab0d41322bf96a9535be8c025e65964f7c7159b5 100644 (file)
@@ -1525,6 +1525,12 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char
                                }
                                (*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;
@@ -1556,6 +1562,12 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char
                                }
                                (*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);
                                break;
index 8fe705146890bbff815103cda0f94a0558274309..f0eda6c73c2451b127769474b3aed1a08238b27c 100644 (file)
@@ -184,7 +184,7 @@ _gd2CreateFromFile (gdIOCtxPtr in, int *sx, int *sy,
       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 @@ gdImageCreateFromGd2Ctx (gdIOCtxPtr in)
   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 @@ _gdImageGd2 (gdImagePtr im, gdIOCtx * out, int cs, int fmt)
                {
                  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 @@ _gdImageGd2 (gdImagePtr im, gdIOCtx * out, int cs, int fmt)
                        }
                      else
                        {
-                         chunkData[chunkLen++] = p;
+                         chunkData[chunkLen++] = im->pixels[y][x];
                        }
                    };
                }
index 1c91eaa16cf944e9864063d218a7de70ec92227b..5cd24ce971ddf614ad08bfdfc33ce2ea89f345f5 100644 (file)
@@ -716,20 +716,10 @@ gdImagePngCtx (gdImagePtr im, gdIOCtx * outfile)
        {
          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 7e2de25fc5d33d0b9a7dcc7c19bb1edb7179c835..eb006259d5f61a0bf7f4082cb40863911b9f168d 100644 (file)
@@ -1490,200 +1490,118 @@ zeroHistogram (hist4d histogram)
 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