summaryrefslogtreecommitdiff
path: root/src/gd_color_match.c
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2013-04-18 12:17:40 +0200
committerOndřej Surý <ondrej@sury.org>2013-04-22 08:44:21 +0200
commit948a1542667de6305f371bbca5290f65ca0eeee8 (patch)
tree5f6c605f9be27919c8e6fb7ff72666e77ea3589c /src/gd_color_match.c
parentdecf4407d41230fc54dea8058bf887a2696fd4c2 (diff)
downloadlibgd-948a1542667de6305f371bbca5290f65ca0eeee8.tar.gz
Merge gdImageColorMatch from php-libgd
Diffstat (limited to 'src/gd_color_match.c')
-rw-r--r--src/gd_color_match.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/gd_color_match.c b/src/gd_color_match.c
new file mode 100644
index 0000000..551c844
--- /dev/null
+++ b/src/gd_color_match.c
@@ -0,0 +1,60 @@
+#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;
+}