Commits

Anonymous committed 81e3d8d

added new filter gdImagePixelate(). (see FS#184)

Comments (0)

Files changed (4)

src/CMakeLists.txt

 	gd_jpeg.c
 	gd_nnquant.c
 	gd_nnquant.h
+	gd_pixelate.c
 	gd_png.c
 	gd_tiff.c
 	gd_tga.c
 
 lib_LTLIBRARIES = libgd.la
 
-libgd_la_SOURCES = gd.c gd_color.c gd_color_map.c gd_transform.c gdfx.c gd_security.c gd_gd.c gd_gd2.c gd_io.c gd_io_dp.c gd_gif_in.c gd_gif_out.c gd_io_file.c gd_io_ss.c gd_jpeg.c gd_png.c gd_ss.c gd_topal.c gd_wbmp.c gdcache.c gdfontg.c gdfontl.c gdfontmb.c gdfonts.c gdfontt.c gdft.c gdhelpers.c gdhelpers.h gdkanji.c gdtables.c gdxpm.c jisx0208.h wbmp.c wbmp.h
+libgd_la_SOURCES = gd.c gd_color.c gd_color_map.c gd_transform.c gdfx.c gd_security.c gd_gd.c gd_gd2.c gd_io.c gd_io_dp.c gd_gif_in.c gd_gif_out.c gd_io_file.c gd_io_ss.c gd_jpeg.c gd_png.c gd_ss.c gd_topal.c gd_wbmp.c gdcache.c gdfontg.c gdfontl.c gdfontmb.c gdfonts.c gdfontt.c gdft.c gdhelpers.c gdhelpers.h gdkanji.c gdtables.c gdxpm.c jisx0208.h wbmp.c wbmp.h gd_pixelate.c
 
 libgd_la_LDFLAGS = -version-info 2:0:0 $(XTRA_LDFLAGS)
 
 
 BGD_DECLARE(gdImagePtr) gdImageNeuQuant(gdImagePtr im, const int max_color, int sample_factor);
 
+enum gdPixelateMode {
+	GD_PIXELATE_UPPERLEFT,
+	GD_PIXELATE_AVERAGE
+};
+
+BGD_DECLARE(int) gdImagePixelate(gdImagePtr im, int block_size, const unsigned int mode);
+
 /* Macros to access information about images. */
 
 /* Returns nonzero if the image is a truecolor image,

src/gd_pixelate.c

+#include "gd.h"
+
+int gdImagePixelate(gdImagePtr im, int block_size, const unsigned int mode)
+{
+	int x, y;
+
+	if (block_size <= 0) {
+		return 0;
+	} else if (block_size == 1) {
+		return 1;
+	}
+	switch (mode) {
+	case GD_PIXELATE_UPPERLEFT:
+		for (y = 0; y < im->sy; y += block_size) {
+			for (x = 0; x < im->sx; x += block_size) {
+				if (gdImageBoundsSafe(im, x, y)) {
+					int c = gdImageGetPixel(im, x, y);
+					gdImageFilledRectangle(im, x, y, x + block_size - 1, y + block_size - 1, c);
+				}
+			}
+		}
+		break;
+	case GD_PIXELATE_AVERAGE:
+		for (y = 0; y < im->sy; y += block_size) {
+			for (x = 0; x < im->sx; x += block_size) {
+				int a, r, g, b, c;
+				int total;
+				int cx, cy;
+
+				a = r = g = b = c = total = 0;
+				/* sampling */
+				for (cy = 0; cy < block_size; cy++) {
+					for (cx = 0; cx < block_size; cx++) {
+						if (!gdImageBoundsSafe(im, x + cx, y + cy)) {
+							continue;
+						}
+						c = gdImageGetPixel(im, x + cx, y + cy);
+						a += gdImageAlpha(im, c);
+						r += gdImageRed(im, c);
+						g += gdImageGreen(im, c);
+						b += gdImageBlue(im, c);
+						total++;
+					}
+				}
+				/* drawing */
+				if (total > 0) {
+					c = gdImageColorResolveAlpha(im, r / total, g / total, b / total, a / total);
+					gdImageFilledRectangle(im, x, y, x + block_size - 1, y + block_size - 1, c);
+				}
+			}
+		}
+		break;
+	}
+	return 1;
+}
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.