Commits

Anonymous committed 7393744

added gdImageColorReplace, gdImageColorReplaceArray, and gdImageColorReplaceCallback. (see FS#170)

Comments (0)

Files changed (2)

 
 }
 
+BGD_DECLARE(int) gdImageColorReplace (gdImagePtr im, int src, int dst)
+{
+	int x, y, 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 (pixel(im, x, y) == src) {					\
+					gdImageSetPixel(im, x, y, dst);				\
+					n++;										\
+				}												\
+			}													\
+		}														\
+	} while (0)
+
+	if (im->trueColor) {
+		REPLACING_LOOP(gdImageTrueColorPixel);
+	} else {
+		REPLACING_LOOP(gdImagePalettePixel);
+	}
+
+#undef REPLACING_LOOP
+
+	return n;
+}
+
+BGD_DECLARE(int) gdImageColorReplaceArray (gdImagePtr im, unsigned int len, int *src, int *dst)
+{
+	int x, y, c, n = 0;
+	unsigned int i;
+
+	if (len == 0) {
+		return 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++) {			\
+				c = pixel(im, x, y);						\
+				for (i = 0; i < len; i++) {					\
+					if (c == src[i] && c != dst[i]) {		\
+						gdImageSetPixel(im, x, y, dst[i]);	\
+						n++;								\
+						break;								\
+					}										\
+				}											\
+			}												\
+		}													\
+	} while (0)
+
+	if (im->trueColor) {
+		REPLACING_LOOP(gdImageTrueColorPixel);
+	} else {
+		REPLACING_LOOP(gdImagePalettePixel);
+	}
+
+#undef REPLACING_LOOP
+
+	return n;
+}
+
+BGD_DECLARE(int) gdImageColorReplaceCallback (gdImagePtr im, int (*callback)(gdImagePtr imx, int src))
+{
+	int x, y, c, d, n = 0;
+
+	if (!callback) {
+		return 0;
+	}
+	if (im->trueColor) {
+		for (y = im->cy1; y <= im->cy2; y++) {
+			for (x = im->cx1; x <= im->cx2; x++) {
+				c = gdImageTrueColorPixel(im, x, y);
+				if ( (d = callback(im, c)) != c) {
+					gdImageSetPixel(im, x, y, d);
+					n++;
+				}
+			}
+		}
+	} else { /* palette */
+		int *sarr, *darr;
+		unsigned int k, len = 0;
+
+		sarr = (int *)gdCalloc(im->colorsTotal, sizeof(int));
+		if (!sarr) {
+			return -1;
+		}
+		for (c = 0; c < im->colorsTotal; c++) {
+			if (!im->open[c]) {
+				sarr[len++] = c;
+			}
+		}
+		darr = (int *)gdCalloc(len, sizeof(int));
+		if (!darr) {
+			gdFree(sarr);
+			return -1;
+		}
+		for (k = 0; k < len; k++) {
+			darr[k] = callback(im, sarr[k]);
+		}
+		n = gdImageColorReplaceArray(im, k, sarr, darr);
+		gdFree(darr);
+		gdFree(sarr);
+	}
+	return n;
+}
+
 /* 2.0.10: before the drawing routines, some code to clip points that are
  * outside the drawing window.  Nick Atty (nick@canalplan.org.uk)
  *
 BGD_DECLARE(void) gdImageColorTransparent (gdImagePtr im, int color);
 
 BGD_DECLARE(void) gdImagePaletteCopy (gdImagePtr dst, gdImagePtr src);
+
+BGD_DECLARE(int) gdImageColorReplace(gdImagePtr im, int src, int dst);
+BGD_DECLARE(int) gdImageColorReplaceArray(gdImagePtr im, unsigned int len, int *src, int *dst);
+BGD_DECLARE(int) gdImageColorReplaceCallback(gdImagePtr im, int (*callback)(gdImagePtr imx, int src));
+
 BGD_DECLARE(void) gdImageGif (gdImagePtr im, FILE * out);
 BGD_DECLARE(void) gdImagePng (gdImagePtr im, FILE * out);
 BGD_DECLARE(void) gdImagePngCtx (gdImagePtr im, gdIOCtx * out);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.