Commits

tabe  committed 0879059

added gdImageColorReplaceThreshold. (see FS#170)
- added gd_color.[ch] for internal use

  • Participants
  • Parent commits 44a4f10
  • Branches GD-2.0

Comments (0)

Files changed (7)

File src/CMakeLists.txt

 	gd.c
 	gdfx.c
 	gdfx.h
+	gd_color.c
+	gd_color.h
 	gd_color_map.c
 	gd_color_map.h
 	gd_crop.c

File src/Makefile.am

 
 lib_LTLIBRARIES = libgd.la
 
-libgd_la_SOURCES = gd.c gd_color_map.c gd_transform.c gdfx.c gd_security.c gd_gd.c gd_gd2.c gd_io.c gd_io_dp.c gd_gif_in.c 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
+libgd_la_SOURCES = gd.c gd_color.c gd_color_map.c gd_transform.c gdfx.c gd_security.c gd_gd.c gd_gd2.c gd_io.c gd_io_dp.c gd_gif_in.c 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
 
 libgd_la_LDFLAGS = -version-info 2:0:0 $(XTRA_LDFLAGS)
 
 /* 2.03: don't include zlib here or we can't build without PNG */
 #include "gd.h"
 #include "gdhelpers.h"
+#include "gd_color.h"
 
 /* 2.0.12: this now checks the clipping rectangle */
 #define gdImageBoundsSafeMacro(im, x, y) (!((((y) < (im)->cy1) || ((y) > (im)->cy2)) || (((x) < (im)->cx1) || ((x) > (im)->cx2))))
 	return n;
 }
 
+BGD_DECLARE(int) gdImageColorReplaceThreshold (gdImagePtr im, int src, int dst, float threshold)
+{
+	register int x, y;
+	int n = 0;
+
+	if (src == dst) {
+		return 0;
+	}
+
+#define REPLACING_LOOP(pixel) do {										\
+		for (y = im->cy1; y <= im->cy2; y++) {							\
+			for (x = im->cx1; x <= im->cx2; x++) {						\
+				if (gdColorMatch(im, src, pixel(im, x, y), threshold)) { \
+					gdImageSetPixel(im, x, y, dst);						\
+					n++;												\
+				}														\
+			}															\
+		}																\
+	} while (0)
+
+	if (im->trueColor) {
+		REPLACING_LOOP(gdImageTrueColorPixel);
+	} else {
+		REPLACING_LOOP(gdImagePalettePixel);
+	}
+
+#undef REPLACING_LOOP
+
+	return n;
+}
+
 static int colorCmp (const void *x, const void *y)
 {
 	int a = *(int const *)x;
 BGD_DECLARE(void) gdImagePaletteCopy (gdImagePtr dst, gdImagePtr src);
 
 BGD_DECLARE(int) gdImageColorReplace(gdImagePtr im, int src, int dst);
+BGD_DECLARE(int) gdImageColorReplaceThreshold(gdImagePtr im, int src, int dst, float threshold);
 BGD_DECLARE(int) gdImageColorReplaceArray(gdImagePtr im, int len, int *src, int *dst);
 BGD_DECLARE(int) gdImageColorReplaceCallback(gdImagePtr im, int (*callback)(gdImagePtr imx, int src));
 

File src/gd_color.c

+#include "gd.h"
+#include "gd_color.h"
+
+int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold)
+{
+	const int dr = gdImageRed(im, col1) - gdImageRed(im, col2);
+	const int dg = gdImageGreen(im, col1) - gdImageGreen(im, col2);
+	const int db = gdImageBlue(im, col1) - gdImageBlue(im, col2);
+	const int da = gdImageAlpha(im, col1) - gdImageAlpha(im, col2);
+	const int dist = dr * dr + dg * dg + db * db + da * da;
+
+	return (100.0 * dist / 195075) < threshold;
+}
+
+/*
+ * To be implemented when we have more image formats.
+ * Buffer like gray8 gray16 or rgb8 will require some tweak
+ * and can be done in this function (called from the autocrop
+ * function. (Pierre)
+ */
+#if 0
+static int colors_equal (const int col1, const in col2)
+{
+
+}
+#endif

File src/gd_color.h

+#ifndef GD_COLOR_H
+#define GD_COLOR_H 1
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

File src/gd_crop.c

 
 #include <stdlib.h>
 #include "gd.h"
+#include "gd_color.h"
 
 static int gdGuessBackgroundColorFromCorners(gdImagePtr im, int *color);
-static int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold);
 
 BGD_DECLARE(gdImagePtr) gdImageCrop(gdImagePtr src, const gdRect *crop)
 {
 		return 0;
 	}
 }
-
-static int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold)
-{
-	const int dr = gdImageRed(im, col1) - gdImageRed(im, col2);
-	const int dg = gdImageGreen(im, col1) - gdImageGreen(im, col2);
-	const int db = gdImageBlue(im, col1) - gdImageBlue(im, col2);
-	const int da = gdImageAlpha(im, col1) - gdImageAlpha(im, col2);
-	const int dist = dr * dr + dg * dg + db * db + da * da;
-
-	return (100.0 * dist / 195075) < threshold;
-}
-
-/*
- * To be implemented when we have more image formats.
- * Buffer like gray8 gray16 or rgb8 will require some tweak
- * and can be done in this function (called from the autocrop
- * function. (Pierre)
- */
-#if 0
-static int colors_equal (const int col1, const in col2)
-{
-
-}
-#endif