Commits

Ondřej Surý committed 948a154

Merge gdImageColorMatch from php-libgd

Comments (0)

Files changed (3)

                    gd_gif_out.c gd_io_file.c gd_io_ss.c gd_jpeg.c gd_png.c gd_ss.c gd_topal.c gd_wbmp.c gdcache.c gdfontg.c gdfontl.c    \
                    gdfontmb.c gdfonts.c gdfontt.c gdft.c gdhelpers.c gdhelpers.h gdkanji.c gdtables.c gdxpm.c jisx0208.h wbmp.c wbmp.h   \
                    gd_filter.c gd_nnquant.c gd_rotate.c gd_matrix.c gd_interpolation.c gd_crop.c webpimg.c webpimg.h gd_webp.c gd_tiff.c \
-                   gd_tga.c gd_tga.h gd_bmp.c bmp.h gd_color.h gd_nnquant.h gd_tga.h gd_intern.h gd_io_stream.h gd_xbm.c
+                   gd_tga.c gd_tga.h gd_bmp.c bmp.h gd_color.h gd_nnquant.h gd_tga.h gd_intern.h gd_io_stream.h gd_xbm.c \
+		   gd_color_match.c
 
 libgd_la_LDFLAGS = -version-info $(GDLIB_CURRENT):$(GDLIB_REVISION):$(GDLIB_AGE) $(XTRA_LDFLAGS)
 
 
 BGD_DECLARE(int) gdImagePaletteToTrueColor(gdImagePtr src);
 
+/* An attempt at getting the results of gdImageTrueColorToPalette to
+ * look a bit more like the original (im1 is the original and im2 is
+ * the palette version */
+
+BGD_DECLARE(int) gdImageColorMatch(gdImagePtr im1, gdImagePtr im2);
+
 /* Selects quantization method used for subsequent gdImageTrueColorToPalette calls.
    See gdPaletteQuantizationMethod enum (e.g. GD_QUANT_NEUQUANT, GD_QUANT_LIQ).
    Speed is from 1 (highest quality) to 10 (fastest).

src/gd_color_match.c

+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "gd.h"
+#include "gdhelpers.h"
+
+/* bring the palette colors in im2 to be closer to im1
+ *
+ */
+BGD_DECLARE(int) gdImageColorMatch (gdImagePtr im1, gdImagePtr im2)
+{
+	unsigned long *buf; /* stores our calculations */
+	unsigned long *bp; /* buf ptr */
+	int color, rgb;
+	int x,y;
+	int count;
+
+	if (!im1->trueColor) {
+		return -1; /* im1 must be True Color */
+	}
+	if (im2->trueColor) {
+		return -2; /* im2 must be indexed */
+	}
+	if ((im1->sx != im2->sx) || (im1->sy != im2->sy)) {
+		return -3; /* the images are meant to be the same dimensions */
+	}
+	if (im2->colorsTotal < 1) {
+		return -4; /* At least 1 color must be allocated */
+	}
+
+	buf = (unsigned long *)gdMalloc(sizeof(unsigned long) * 5 * im2->colorsTotal);
+	memset (buf, 0, sizeof(unsigned long) * 5 * im2->colorsTotal );
+
+	for (x=0; x < im1->sx; x++) {
+		for( y=0; y<im1->sy; y++ ) {
+			color = im2->pixels[y][x];
+			rgb = im1->tpixels[y][x];
+			bp = buf + (color * 5);
+			(*(bp++))++;
+			*(bp++) += gdTrueColorGetRed(rgb);
+			*(bp++) += gdTrueColorGetGreen(rgb);
+			*(bp++) += gdTrueColorGetBlue(rgb);
+			*(bp++) += gdTrueColorGetAlpha(rgb);
+		}
+	}
+	bp = buf;
+	for (color=0; color < im2->colorsTotal; color++) {
+		count = *(bp++);
+		if( count > 0 ) {
+			im2->red[color]		= *(bp++) / count;
+			im2->green[color]	= *(bp++) / count;
+			im2->blue[color]	= *(bp++) / count;
+			im2->alpha[color]	= *(bp++) / count;
+		} else {
+			bp += 4;
+		}
+	}
+	gdFree(buf);
+	return 0;
+}