1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

Overhaul GD test helpers and affected tests (GH-17309)

* Use type declarations instead of doc-block annotations

* Inline the terrible get_rgb() function

* Always traverse pixels in Z order

libgd stores the pixel as an array of rows, so we should use row-major-
order traversal to improve caching.

* Add assertions to avoid misuse of the functions

The assertion regarding the image dimensions won't break any tests, and
we had it already as a comment.

However, asserting that the images are truecolor images is important
for `calc_image_dissimilarity()` which otherwise would calculate
nonsense, and not unreasonable for `test_image_equals_image()` which
otherwise is overspecified (for our purposes, it doesn't matter which
palette entry a pixel refers to, but rather whether the actual colors
referred by a palette color match).

Since the truecolor assertions break two tests, we fix these by
converting to truecolor.  That should likely be backported to lower
branches.

* Drop implicit conversion to truecolor

Conversion to truecolor is a relatively expensive operation, and as
such should not be implicit; instead test authors are encouraged to use
truecolor images in the first place where possible, or to even find
better ways to verify expectations than doing a full image comparison.

* Merge similarity.inc into func.inc

There is no particular reason to have a separate file for similarity
comparisons.

* Simplify bug43475.phpt and bug64641.phpt

`calc_image_dissimilarity()` calculates the sum of the euclidean
distance of the RGB channels of all pixels.  The euclidean distance is
either zero or greater than or equal to one (but never in ]0, 1[).  The
sum of these values also has this property, so it doesn't make sense to
check for less than 1e-5.  Thus we just call `test_image_equals_file()`
instead.

* Replace calc_image_dissimilarity() with the well-known mse()

`calc_image_dissimilarity()` has the drawback that it did sum up the
pixel differences, so for large images the result could be way larger
than for small images.  It also has the drawback that it likely is not
as well understood as the mean squared error.  Thus we replace it with
the latter, and calculate the mean squared error of the individual RGB
channels (to be precise).  The result is always in range 0..255**2 what
makes reasoning about thresholds easier.
This commit is contained in:
Christoph M. Becker
2025-01-25 19:48:38 +01:00
committed by GitHub
parent f698c62361
commit 5890761ef9
24 changed files with 61 additions and 118 deletions

View File

@@ -14,7 +14,7 @@ gd
--FILE-- --FILE--
<?php <?php
require_once __DIR__ . '/similarity.inc'; require_once __DIR__ . '/func.inc';
$infile = __DIR__ . '/girl.avif'; $infile = __DIR__ . '/girl.avif';
$outfile = __DIR__ . '/test.avif'; $outfile = __DIR__ . '/test.avif';
@@ -58,8 +58,8 @@ gd
// Note we could also forgive a certain number of pixel differences. // Note we could also forgive a certain number of pixel differences.
// With the current test image, we just didn't need to. // With the current test image, we just didn't need to.
echo 'How many pixels are different in the two images? '; echo 'What is the mean squared error of the two images? ',
print_r(calc_image_dissimilarity($img, $img_from_avif)); mse($img, $img_from_avif);
unlink($outfile); unlink($outfile);
@@ -79,4 +79,4 @@ Encoding AVIF with illegal quality: imageavif(): Argument #3 ($quality) must be
Encoding AVIF with illegal speed: imageavif(): Argument #4 ($speed) must be between -1 and 10 Encoding AVIF with illegal speed: imageavif(): Argument #4 ($speed) must be between -1 and 10
Encoding AVIF losslessly... ok Encoding AVIF losslessly... ok
Decoding the AVIF we just wrote... Decoding the AVIF we just wrote...
How many pixels are different in the two images? 0 What is the mean squared error of the two images? 0

View File

@@ -13,7 +13,7 @@ gd
?> ?>
--FILE-- --FILE--
<?php <?php
require_once __DIR__ . '/similarity.inc'; require_once __DIR__ . '/func.inc';
function setStyleAndThickness($im, $color, $thickness) function setStyleAndThickness($im, $color, $thickness)
{ {
@@ -59,8 +59,8 @@ imageline($im, 200, 100, 700, 100, IMG_COLOR_STYLED);
imageline($im, 700, 100, 700, 600, IMG_COLOR_STYLED); imageline($im, 700, 100, 700, 600, IMG_COLOR_STYLED);
imageline($im, 700, 600, 200, 100, IMG_COLOR_STYLED); imageline($im, 700, 600, 200, 100, IMG_COLOR_STYLED);
$ex = imagecreatefrompng(__DIR__ . '/bug43475.png'); imagepalettetotruecolor($im);
var_dump(calc_image_dissimilarity($ex, $im) < 1e-5); test_image_equals_file(__DIR__ . '/bug43475.png', $im);
?> ?>
--EXPECT-- --EXPECT--
bool(true) The images are equal.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

@@ -20,6 +20,7 @@ imagedashedline($im, 800, 400, 500, 800, $color);
imagedashedline($im, 800, 400, 600, 800, $color); imagedashedline($im, 800, 400, 600, 800, $color);
imagedashedline($im, 800, 400, 700, 800, $color); imagedashedline($im, 800, 400, 700, 800, $color);
imagedashedline($im, 800, 400, 800, 800, $color); imagedashedline($im, 800, 400, 800, 800, $color);
imagepalettetotruecolor($im);
include_once __DIR__ . '/func.inc'; include_once __DIR__ . '/func.inc';
test_image_equals_file(__DIR__ . '/bug52070.png', $im); test_image_equals_file(__DIR__ . '/bug52070.png', $im);
?> ?>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 8.5 KiB

View File

@@ -13,7 +13,7 @@ if (!(imagetypes() & IMG_PNG)) {
?> ?>
--FILE-- --FILE--
<?php <?php
require_once __DIR__ . '/similarity.inc'; require_once __DIR__ . '/func.inc';
$im = imagecreatetruecolor(640, 480); $im = imagecreatetruecolor(640, 480);
@@ -31,15 +31,7 @@ $points = array(
); );
imagefilledpolygon($im, $points, 0xFFFF00); imagefilledpolygon($im, $points, 0xFFFF00);
$ex = imagecreatefrompng(__DIR__ . '/bug64641.png'); test_image_equals_file(__DIR__ . '/bug64641.png', $im);
if (($diss = calc_image_dissimilarity($ex, $im)) < 1e-5) {
echo "IDENTICAL";
} else {
echo "DISSIMILARITY: $diss";
}
imagedestroy($ex);
imagedestroy($im);
?> ?>
--EXPECT-- --EXPECT--
IDENTICAL The images are equal.

View File

@@ -23,8 +23,16 @@ imagesavealpha($dst, true);
imagecopy($dst, $src, 0,0, 0,0, 50,50); imagecopy($dst, $src, 0,0, 0,0, 50,50);
include_once __DIR__ . '/func.inc'; var_dump(imagecolorsforindex($dst, imagecolorat($dst, 0, 0)));
test_image_equals_file(__DIR__ . '/bug72913.png', $dst);
?> ?>
--EXPECT-- --EXPECT--
The images are equal. array(4) {
["red"]=>
int(255)
["green"]=>
int(255)
["blue"]=>
int(255)
["alpha"]=>
int(127)
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 147 B

View File

@@ -78,9 +78,7 @@ function test_image_equals_file(string $filename, GdImage $actual): void
save_actual_image($actual); save_actual_image($actual);
return; return;
} }
$actual = test_to_truecolor($actual);
$expected = imagecreatefrompng($filename); $expected = imagecreatefrompng($filename);
$expected = test_to_truecolor($expected);
test_image_equals_image($expected, $actual, true); test_image_equals_image($expected, $actual, true);
} }
@@ -95,6 +93,7 @@ function test_image_equals_file(string $filename, GdImage $actual): void
*/ */
function test_image_equals_image(GdImage $expected, GdImage $actual, bool $save_actual = false): void function test_image_equals_image(GdImage $expected, GdImage $actual, bool $save_actual = false): void
{ {
assert(imageistruecolor($expected) && imageistruecolor($actual));
$exp_x = imagesx($expected); $exp_x = imagesx($expected);
$exp_y = imagesy($expected); $exp_y = imagesy($expected);
$act_x = imagesx($actual); $act_x = imagesx($actual);
@@ -126,35 +125,13 @@ function test_image_equals_image(GdImage $expected, GdImage $actual, bool $save_
} }
} }
/**
* Returns the truecolor version of an image.
*
* @param resource $image
* @return resource
*/
function test_to_truecolor($image)
{
if (imageistruecolor($image)) {
return $image;
} else {
$width = imagesx($image);
$height = imagesy($image);
$result = imagecreatetruecolor($width, $height);
imagecopy($result, $image, 0,0, 0,0, $width, $height);
return $result;
}
}
/** /**
* Saves an actual image to disk. * Saves an actual image to disk.
* *
* The image is saved right beside the temporary .php test file with the * The image is saved right beside the temporary .php test file with the
* extension .out.png. * extension .out.png.
*
* @param resource $image
* @return void
*/ */
function save_actual_image($image) function save_actual_image(GdImage $image): void
{ {
$pathinfo = pathinfo($_SERVER['SCRIPT_FILENAME']); $pathinfo = pathinfo($_SERVER['SCRIPT_FILENAME']);
$filename = "{$pathinfo['dirname']}/{$pathinfo['filename']}.out.png"; $filename = "{$pathinfo['dirname']}/{$pathinfo['filename']}.out.png";
@@ -178,3 +155,23 @@ function trycatch_dump(...$tests) {
} }
} }
/**
* Calculate the mean squared error (in range 0..255**2)
*/
function mse(GdImage $image1, GdImage $image2): float
{
assert(imageistruecolor($image1) && imageistruecolor($image2));
assert(imagesx($image1) === imagesx($image2) && imagesy($image1) === imagesy($image2));
$e = $count = 0;
for ($j = 0, $m = imagesy($image1); $j < $m; $j++) {
for ($i = 0, $n = imagesx($image1); $i < $n; $i++) {
$c1 = imagecolorat($image1, $i, $j);
$c2 = imagecolorat($image2, $i, $j);
$e += ((($c1 >> 16) & 255) - (($c2 >> 16) & 255)) ** 2
+ ((($c1 >> 8) & 255) - (($c2 >> 8) & 255)) ** 2
+ ((($c1 >> 0) & 255) - (($c2 >> 0) & 255)) ** 2;
$count += 3;
}
}
return $e / $count;
}

View File

@@ -15,6 +15,8 @@ $filename = __DIR__ . "/gd276.bmp";
imagebmp($orig, $filename, true); imagebmp($orig, $filename, true);
$saved = imagecreatefrombmp($filename); $saved = imagecreatefrombmp($filename);
var_dump($saved !== false); var_dump($saved !== false);
imagepalettetotruecolor($orig);
imagepalettetotruecolor($saved);
test_image_equals_image($orig, $saved); test_image_equals_image($orig, $saved);
?> ?>
--EXPECT-- --EXPECT--

View File

@@ -13,7 +13,7 @@ $im = imagecreate(100, 100);
imagecolorallocate($im, 0, 0, 0); imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255); $white = imagecolorallocate($im, 255, 255, 255);
imageline($im, 10,10, 89,89, $white); imageline($im, 10,10, 89,89, $white);
imagepalettetotruecolor($im);
require __DIR__ . "/func.inc"; require __DIR__ . "/func.inc";
test_image_equals_file(__DIR__ . "/imagebmp_basic.png", $im); test_image_equals_file(__DIR__ . "/imagebmp_basic.png", $im);
?> ?>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 141 B

After

Width:  |  Height:  |  Size: 584 B

View File

@@ -25,6 +25,7 @@ $bg = imagecolorat($im, 0, 0);
// Set the background to be blue // Set the background to be blue
imagecolorset($im, $bg, 0, 0, 255); imagecolorset($im, $bg, 0, 0, 255);
imagepalettetotruecolor($im);
include_once __DIR__ . '/func.inc'; include_once __DIR__ . '/func.inc';
test_image_equals_file(__DIR__ . '/imagecolorset_basic.png', $im); test_image_equals_file(__DIR__ . '/imagecolorset_basic.png', $im);
imagedestroy($im); imagedestroy($im);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 99 B

After

Width:  |  Height:  |  Size: 434 B

View File

@@ -22,7 +22,7 @@ $bmp = "\x42\x4D\x3E\x00\x00\x00\x00\x00\x00\x00\x3E\x00\x00\x00\x28\x00"
. "\x01\x00\x05\x00\x00\x00\x02\x00\x01\x01\x01\x00\x06\x00\x00\x00" . "\x01\x00\x05\x00\x00\x00\x02\x00\x01\x01\x01\x00\x06\x00\x00\x00"
. "\x0A\x00\x00\x00\x0A\x00\x00\x00\x00\x01"; . "\x0A\x00\x00\x00\x0A\x00\x00\x00\x00\x01";
$im = imagecreatefromstring($bmp); $im = imagecreatefromstring($bmp);
imagepalettetotruecolor($im);
include_once __DIR__ . '/func.inc'; include_once __DIR__ . '/func.inc';
test_image_equals_file(__DIR__ . '/imagecreatefromstring_bmp.png', $im); test_image_equals_file(__DIR__ . '/imagecreatefromstring_bmp.png', $im);
?> ?>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 126 B

After

Width:  |  Height:  |  Size: 134 B

View File

@@ -26,7 +26,7 @@ $half2 = imagefilledarc ( $image, 75, 75, 70, 70, 0, -180, $gray, IMG_ARC_PIE )
$gamma = imagegammacorrect($image, 1, 5); $gamma = imagegammacorrect($image, 1, 5);
var_dump((bool) $gamma); var_dump((bool) $gamma);
imagepalettetotruecolor($image);
include_once __DIR__ . '/func.inc'; include_once __DIR__ . '/func.inc';
test_image_equals_file(__DIR__ . '/imagegammacorrect_variation1.png', $image); test_image_equals_file(__DIR__ . '/imagegammacorrect_variation1.png', $image);
?> ?>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 199 B

After

Width:  |  Height:  |  Size: 686 B

View File

@@ -33,6 +33,9 @@ function test_gamma($in, $out, $constructor)
imagegammacorrect($im, $in, $out); imagegammacorrect($im, $in, $out);
if ($constructor === "imagecreate") {
imagepalettetotruecolor($im);
}
$filename = __DIR__ . DIRECTORY_SEPARATOR $filename = __DIR__ . DIRECTORY_SEPARATOR
. "imagegammacorrect_variation2_{$in}_{$out}.png"; . "imagegammacorrect_variation2_{$in}_{$out}.png";
$kind = $constructor === 'imagecreate' ? 'palette' : 'truecolor'; $kind = $constructor === 'imagecreate' ? 'palette' : 'truecolor';

View File

@@ -25,7 +25,7 @@ $half = imagefilledarc ( $image, 75, 75, 70, 70, 0, 180, $a, IMG_ARC_PIE );
$half2 = imagefilledarc ( $image, 75, 55, 80, 70, 0, -180, $b, IMG_ARC_PIE ); $half2 = imagefilledarc ( $image, 75, 55, 80, 70, 0, -180, $b, IMG_ARC_PIE );
var_dump(imagetruecolortopalette($image, true, 2)); var_dump(imagetruecolortopalette($image, true, 2));
imagepalettetotruecolor($image);
include_once __DIR__ . '/func.inc'; include_once __DIR__ . '/func.inc';
test_image_equals_file(__DIR__ . '/imagetruecolortopalette_basic.png', $image); test_image_equals_file(__DIR__ . '/imagetruecolortopalette_basic.png', $image);
?> ?>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 208 B

After

Width:  |  Height:  |  Size: 709 B

View File

@@ -1,64 +0,0 @@
<?php
/**
* A very simple algorithm for finding the dissimilarity between images,
* mainly useful for checking lossy compression.
*/
/**
* Gets the individual components of an RGB value.
*
* @param int $color
* @param int $red
* @param int $green
* @param int $blue
*
* @return void
*/
function get_rgb($color, &$red, &$green, &$blue)
{
// assumes $color is an RGB value
$red = ($color >> 16) & 0xFF;
$green = ($color >> 8) & 0xFF;
$blue = $color & 0xFF;
}
/**
* Calculates the euclidean distance of two RGB values.
*
* @param int $color1
* @param int $color2
*
* @return int
*/
function calc_pixel_distance($color1, $color2)
{
get_rgb($color1, $red1, $green1, $blue1);
get_rgb($color2, $red2, $green2, $blue2);
return sqrt(
pow($red1 - $red2, 2) + pow($green1 - $green2, 2) + pow($blue1 - $blue2, 2)
);
}
/**
* Calculates dissimilarity of two images.
*
* @param resource $image1
* @param resource $image2
*
* @return int The dissimilarity. 0 means the images are identical. The higher
* the value, the more dissimilar are the images.
*/
function calc_image_dissimilarity($image1, $image2)
{
// assumes image1 and image2 have same width and height
$dissimilarity = 0;
for ($i = 0, $n = imagesx($image1); $i < $n; $i++) {
for ($j = 0, $m = imagesy($image1); $j < $m; $j++) {
$color1 = imagecolorat($image1, $i, $j);
$color2 = imagecolorat($image2, $i, $j);
$dissimilarity += calc_pixel_distance($color1, $color2);
}
}
return $dissimilarity;
}

View File

@@ -18,6 +18,7 @@ $red = imagecolorallocate($im, 255, 0, 0);
imagefilledrectangle($im, 3,3, 7,7, $red); imagefilledrectangle($im, 3,3, 7,7, $red);
$filename = __DIR__ . DIRECTORY_SEPARATOR . 'test_image_equals_file_palette.png'; $filename = __DIR__ . DIRECTORY_SEPARATOR . 'test_image_equals_file_palette.png';
imagepalettetotruecolor($im);
imagepng($im, $filename); imagepng($im, $filename);
$im = imagecreate(10, 10); $im = imagecreate(10, 10);
@@ -25,6 +26,7 @@ imagecolorallocate($im, 255, 255, 255);
$blue = imagecolorallocate($im, 0, 0, 255); $blue = imagecolorallocate($im, 0, 0, 255);
imagefilledrectangle($im, 3,3, 7,7, $blue); imagefilledrectangle($im, 3,3, 7,7, $blue);
imagepalettetotruecolor($im);
test_image_equals_file($filename, $im); test_image_equals_file($filename, $im);
$im = imagecreate(10, 10); $im = imagecreate(10, 10);
@@ -33,6 +35,7 @@ imagecolorallocate($im, 0, 0, 0);
$red = imagecolorallocate($im, 255, 0, 0); $red = imagecolorallocate($im, 255, 0, 0);
imagefilledrectangle($im, 3,3, 7,7, $red); imagefilledrectangle($im, 3,3, 7,7, $red);
imagepalettetotruecolor($im);
test_image_equals_file($filename, $im); test_image_equals_file($filename, $im);
?> ?>
--EXPECT-- --EXPECT--

View File

@@ -12,7 +12,7 @@ if (!function_exists('imagewebp') || !function_exists('imagecreatefromwebp'))
?> ?>
--FILE-- --FILE--
<?php <?php
require_once __DIR__ . '/similarity.inc'; require_once __DIR__ . '/func.inc';
$filename = __DIR__ . '/webp_basic.webp'; $filename = __DIR__ . '/webp_basic.webp';
@@ -30,12 +30,12 @@ imagewebp($im1, $filename);
$im2 = imagecreatefromwebp($filename); $im2 = imagecreatefromwebp($filename);
imagewebp($im2, $filename); imagewebp($im2, $filename);
echo 'Is lossy conversion close enough? '; echo 'Is lossy conversion close enough? ';
var_dump(calc_image_dissimilarity($im1, $im2) < 10e5); var_dump(mse($im1, $im2) < 500);
imagewebp($im1, $filename, IMG_WEBP_LOSSLESS); imagewebp($im1, $filename, IMG_WEBP_LOSSLESS);
$im_lossless = imagecreatefromwebp($filename); $im_lossless = imagecreatefromwebp($filename);
echo 'Does lossless conversion work? '; echo 'Does lossless conversion work? ';
var_dump(calc_image_dissimilarity($im1, $im_lossless) == 0); var_dump(mse($im1, $im_lossless) == 0);
try { try {
imagewebp($im1, $filename, -10); imagewebp($im1, $filename, -10);