Skip to content

Split gd_color.c[ch] (sync with upstream) #17324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/gd/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ext/gd/config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
35 changes: 35 additions & 0 deletions ext/gd/libgd/gd_color.c
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions ext/gd/libgd/gd_color.h
Original file line number Diff line number Diff line change
@@ -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
26 changes: 1 addition & 25 deletions ext/gd/libgd/gd_crop.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#include <math.h>

#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
Expand Down Expand Up @@ -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
Loading