Back to index

php5  5.3.10
Functions
gd_color.c File Reference
#include <gd.h>
#include "libgd/gdhelpers.h"
#include "gd_intern.h"
#include "php.h"

Go to the source code of this file.

Functions

int gdImageColorMatch (gdImagePtr im1, gdImagePtr im2)

Function Documentation

Definition at line 15 of file gd_color.c.

{
       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 *)safe_emalloc(sizeof(unsigned long), 5 * im2->colorsTotal, 0);
       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;
}

Here is the call graph for this function: