Back to index

php5  5.3.10
gd_pixelate.c
Go to the documentation of this file.
00001 #include "gd.h"
00002 
00003 int gdImagePixelate(gdImagePtr im, int block_size, const unsigned int mode)
00004 {
00005        int x, y;
00006 
00007        if (block_size <= 0) {
00008               return 0;
00009        } else if (block_size == 1) {
00010               return 1;
00011        }
00012        switch (mode) {
00013        case GD_PIXELATE_UPPERLEFT:
00014               for (y = 0; y < im->sy; y += block_size) {
00015                      for (x = 0; x < im->sx; x += block_size) {
00016                             if (gdImageBoundsSafe(im, x, y)) {
00017                                    int c = gdImageGetPixel(im, x, y);
00018                                    gdImageFilledRectangle(im, x, y, x + block_size - 1, y + block_size - 1, c);
00019                             }
00020                      }
00021               }
00022               break;
00023        case GD_PIXELATE_AVERAGE:
00024               for (y = 0; y < im->sy; y += block_size) {
00025                      for (x = 0; x < im->sx; x += block_size) {
00026                             int a, r, g, b, c;
00027                             int total;
00028                             int cx, cy;
00029 
00030                             a = r = g = b = c = total = 0;
00031                             /* sampling */
00032                             for (cy = 0; cy < block_size; cy++) {
00033                                    for (cx = 0; cx < block_size; cx++) {
00034                                           if (!gdImageBoundsSafe(im, x + cx, y + cy)) {
00035                                                  continue;
00036                                           }
00037                                           c = gdImageGetPixel(im, x + cx, y + cy);
00038                                           a += gdImageAlpha(im, c);
00039                                           r += gdImageRed(im, c);
00040                                           g += gdImageGreen(im, c);
00041                                           b += gdImageBlue(im, c);
00042                                           total++;
00043                                    }
00044                             }
00045                             /* drawing */
00046                             if (total > 0) {
00047                                    c = gdImageColorResolveAlpha(im, r / total, g / total, b / total, a / total);
00048                                    gdImageFilledRectangle(im, x, y, x + block_size - 1, y + block_size - 1, c);
00049                             }
00050                      }
00051               }
00052               break;
00053        default:
00054               return 0;
00055        }
00056        return 1;
00057 }