Back to index

php5  5.3.10
gd_color.c
Go to the documentation of this file.
00001 #if HAVE_GD_BUNDLED
00002 # include "gd.h"
00003 # include "gdhelpers.h"
00004 #else
00005 # include <gd.h>
00006 # include "libgd/gdhelpers.h"
00007 #endif
00008 
00009 #include "gd_intern.h"
00010 #include "php.h"
00011 
00012 /* bring the palette colors in im2 to be closer to im1
00013  *
00014  */
00015 int gdImageColorMatch (gdImagePtr im1, gdImagePtr im2)
00016 {
00017        unsigned long *buf; /* stores our calculations */
00018        unsigned long *bp; /* buf ptr */
00019        int color, rgb;
00020        int x,y;
00021        int count;
00022 
00023        if( !im1->trueColor ) {
00024               return -1; /* im1 must be True Color */
00025        }
00026        if( im2->trueColor ) {
00027               return -2; /* im2 must be indexed */
00028        }
00029        if( (im1->sx != im2->sx) || (im1->sy != im2->sy) ) {
00030               return -3; /* the images are meant to be the same dimensions */
00031        }
00032        if (im2->colorsTotal<1) {
00033               return -4; /* At least 1 color must be allocated */
00034        }
00035 
00036        buf = (unsigned long *)safe_emalloc(sizeof(unsigned long), 5 * im2->colorsTotal, 0);
00037        memset( buf, 0, sizeof(unsigned long) * 5 * im2->colorsTotal );
00038 
00039        for (x=0; x<im1->sx; x++) {
00040               for( y=0; y<im1->sy; y++ ) {
00041                      color = im2->pixels[y][x];
00042                      rgb = im1->tpixels[y][x];
00043                      bp = buf + (color * 5);
00044                      (*(bp++))++;
00045                      *(bp++) += gdTrueColorGetRed(rgb);
00046                      *(bp++) += gdTrueColorGetGreen(rgb);
00047                      *(bp++) += gdTrueColorGetBlue(rgb);
00048                      *(bp++) += gdTrueColorGetAlpha(rgb);
00049               }
00050        }
00051        bp = buf;
00052        for (color=0; color<im2->colorsTotal; color++) {
00053               count = *(bp++);
00054               if( count > 0 ) {
00055                      im2->red[color]             = *(bp++) / count;
00056                      im2->green[color]    = *(bp++) / count;
00057                      im2->blue[color]     = *(bp++) / count;
00058                      im2->alpha[color]    = *(bp++) / count;
00059               } else {
00060                      bp += 4;
00061               }
00062        }
00063        gdFree(buf);
00064        return 0;
00065 }
00066 
00067