From dbf5ae2e5283f0e0c3bee9794cad6bfee71f9f19 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Thu, 2 Jan 2025 16:50:01 +0100 Subject: [PATCH] Split gd_color.c[ch] (sync with upstream) --- ext/gd/config.m4 | 1 + ext/gd/config.w32 | 2 +- ext/gd/libgd/gd_color.c | 35 +++++++++++++++++++++++++++++++++++ ext/gd/libgd/gd_color.h | 14 ++++++++++++++ ext/gd/libgd/gd_crop.c | 26 +------------------------- 5 files changed, 52 insertions(+), 26 deletions(-) create mode 100644 ext/gd/libgd/gd_color.c create mode 100644 ext/gd/libgd/gd_color.h diff --git a/ext/gd/config.m4 b/ext/gd/config.m4 index 7da5b8cd1b2ec..5e9f20185bad2 100644 --- a/ext/gd/config.m4 +++ b/ext/gd/config.m4 @@ -219,6 +219,7 @@ if test "$PHP_GD" != "no"; then extra_sources=m4_normalize([" libgd/gd_avif.c libgd/gd_bmp.c + libgd/gd_color.c libgd/gd_color_match.c libgd/gd_crop.c libgd/gd_filter.c diff --git a/ext/gd/config.w32 b/ext/gd/config.w32 index 4e168fc3474f6..deefa715c7a49 100644 --- a/ext/gd/config.w32 +++ b/ext/gd/config.w32 @@ -53,7 +53,7 @@ if (PHP_GD != "no") { EXTENSION("gd", "gd.c", null, "-Iext/gd/libgd"); ADD_SOURCES("ext/gd/libgd", "gd2copypal.c gd.c \ - gdcache.c gdfontg.c gdfontl.c gdfontmb.c gdfonts.c gdfontt.c \ + gdcache.c gd_color.c gdfontg.c gdfontl.c gdfontmb.c gdfonts.c gdfontt.c \ 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 \ gd_io_file.c gd_io_ss.c gd_jpeg.c gdkanji.c gd_png.c gd_ss.c \ gdtables.c gd_topal.c gd_wbmp.c gdxpm.c wbmp.c gd_xbm.c gd_security.c gd_transform.c \ diff --git a/ext/gd/libgd/gd_color.c b/ext/gd/libgd/gd_color.c new file mode 100644 index 0000000000000..f2d845efc7e34 --- /dev/null +++ b/ext/gd/libgd/gd_color.c @@ -0,0 +1,35 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gd.h" +#include "gd_color.h" + +/** + * The threshold method works relatively well but it can be improved. + * Maybe L*a*b* and Delta-E will give better results (and a better + * granularity). + */ +int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold) +{ + const int dr = gdImageRed(im, col1) - gdImageRed(im, col2); + const int dg = gdImageGreen(im, col1) - gdImageGreen(im, col2); + const int db = gdImageBlue(im, col1) - gdImageBlue(im, col2); + const int da = gdImageAlpha(im, col1) - gdImageAlpha(im, col2); + const int dist = dr * dr + dg * dg + db * db + da * da; + + return 100.0 * dist < threshold * 195075.0; +} + +/* + * To be implemented when we have more image formats. + * Buffer like gray8 gray16 or rgb8 will require some tweak + * and can be done in this function (called from the autocrop + * function. (Pierre) + */ +#if 0 +static int colors_equal (const int col1, const in col2) +{ + +} +#endif diff --git a/ext/gd/libgd/gd_color.h b/ext/gd/libgd/gd_color.h new file mode 100644 index 0000000000000..08b06cee09bfd --- /dev/null +++ b/ext/gd/libgd/gd_color.h @@ -0,0 +1,14 @@ +#ifndef GD_COLOR_H +#define GD_COLOR_H 1 + +#ifdef __cplusplus +extern "C" { +#endif + + int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/ext/gd/libgd/gd_crop.c b/ext/gd/libgd/gd_crop.c index 676545c4dbc92..3d44bda46d7b3 100644 --- a/ext/gd/libgd/gd_crop.c +++ b/ext/gd/libgd/gd_crop.c @@ -24,9 +24,9 @@ #include #include "gd.h" +#include "gd_color.h" static int gdGuessBackgroundColorFromCorners(gdImagePtr im, int *color); -static int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold); /** * Function: gdImageCrop @@ -291,27 +291,3 @@ static int gdGuessBackgroundColorFromCorners(gdImagePtr im, int *color) return 0; } } - -static int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold) -{ - const int dr = gdImageRed(im, col1) - gdImageRed(im, col2); - const int dg = gdImageGreen(im, col1) - gdImageGreen(im, col2); - const int db = gdImageBlue(im, col1) - gdImageBlue(im, col2); - const int da = gdImageAlpha(im, col1) - gdImageAlpha(im, col2); - const int dist = dr * dr + dg * dg + db * db + da * da; - - return (100.0 * dist / 195075) < threshold; -} - -/* - * To be implemented when we have more image formats. - * Buffer like gray8 gray16 or rgb8 will require some tweak - * and can be done in this function (called from the autocrop - * function. (Pierre) - */ -#if 0 -static int colors_equal (const int col1, const in col2) -{ - -} -#endif