Skip to content

Commit a3ef48c

Browse files
authored
Port "move pixelate filter to gd_filter.c" (sync with upstream) (phpGH-17361)
Cf. <libgd/libgd@0f0894a>.
1 parent 025cc6f commit a3ef48c

File tree

4 files changed

+57
-59
lines changed

4 files changed

+57
-59
lines changed

ext/gd/config.m4

-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ if test "$PHP_GD" != "no"; then
233233
libgd/gd_io.c
234234
libgd/gd_jpeg.c
235235
libgd/gd_matrix.c
236-
libgd/gd_pixelate.c
237236
libgd/gd_png.c
238237
libgd/gd_rotate.c
239238
libgd/gd_security.c

ext/gd/config.w32

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ if (PHP_GD != "no") {
5757
gdft.c gd_gd2.c gd_gd.c gd_gif_in.c gd_gif_out.c gdhelpers.c gd_io.c gd_io_dp.c \
5858
gd_io_file.c gd_io_ss.c gd_jpeg.c gdkanji.c gd_png.c gd_ss.c \
5959
gdtables.c gd_topal.c gd_wbmp.c gdxpm.c wbmp.c gd_xbm.c gd_security.c gd_transform.c \
60-
gd_filter.c gd_pixelate.c gd_rotate.c gd_color_match.c gd_webp.c gd_avif.c \
60+
gd_filter.c gd_rotate.c gd_color_match.c gd_webp.c gd_avif.c \
6161
gd_crop.c gd_interpolation.c gd_matrix.c gd_bmp.c gd_tga.c", "gd");
6262
AC_DEFINE('HAVE_GD_BUNDLED', 1, "Define to 1 if gd extension uses GD library bundled in PHP.");
6363
AC_DEFINE('HAVE_GD_PNG', 1, "Define to 1 if gd extension has PNG support.");

ext/gd/libgd/gd_filter.c

+56
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,62 @@ int gdImageScatterEx(gdImagePtr im, gdScatterPtr scatter)
110110
return 1;
111111
}
112112

113+
int gdImagePixelate(gdImagePtr im, int block_size, const unsigned int mode)
114+
{
115+
int x, y;
116+
117+
if (block_size <= 0) {
118+
return 0;
119+
} else if (block_size == 1) {
120+
return 1;
121+
}
122+
switch (mode) {
123+
case GD_PIXELATE_UPPERLEFT:
124+
for (y = 0; y < im->sy; y += block_size) {
125+
for (x = 0; x < im->sx; x += block_size) {
126+
if (gdImageBoundsSafe(im, x, y)) {
127+
int c = gdImageGetPixel(im, x, y);
128+
gdImageFilledRectangle(im, x, y, x + block_size - 1, y + block_size - 1, c);
129+
}
130+
}
131+
}
132+
break;
133+
case GD_PIXELATE_AVERAGE:
134+
for (y = 0; y < im->sy; y += block_size) {
135+
for (x = 0; x < im->sx; x += block_size) {
136+
int a, r, g, b, c;
137+
int total;
138+
int cx, cy;
139+
140+
a = r = g = b = c = total = 0;
141+
/* sampling */
142+
for (cy = 0; cy < block_size; cy++) {
143+
for (cx = 0; cx < block_size; cx++) {
144+
if (!gdImageBoundsSafe(im, x + cx, y + cy)) {
145+
continue;
146+
}
147+
c = gdImageGetPixel(im, x + cx, y + cy);
148+
a += gdImageAlpha(im, c);
149+
r += gdImageRed(im, c);
150+
g += gdImageGreen(im, c);
151+
b += gdImageBlue(im, c);
152+
total++;
153+
}
154+
}
155+
/* drawing */
156+
if (total > 0) {
157+
c = gdImageColorResolveAlpha(im, r / total, g / total, b / total, a / total);
158+
gdImageFilledRectangle(im, x, y, x + block_size - 1, y + block_size - 1, c);
159+
}
160+
}
161+
}
162+
break;
163+
default:
164+
return 0;
165+
}
166+
return 1;
167+
}
168+
113169
/* invert src image */
114170
int gdImageNegate(gdImagePtr src)
115171
{

ext/gd/libgd/gd_pixelate.c

-57
This file was deleted.

0 commit comments

Comments
 (0)