php-src/ext/gd/tests/avif_decode_encode.phpt
Christoph M. Becker 5890761ef9
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.
2025-01-25 19:48:38 +01:00

82 lines
2.3 KiB
PHP

--TEST--
avif decoding/encoding tests
--EXTENSIONS--
gd
--SKIPIF--
<?php
if (!function_exists("imagecreatefrompng") || !function_exists("imagepng")) {
die("skip png support unavailable");
}
if (!function_exists("imagecreatefromavif") || !function_exists("imageavif")) {
die("skip avif support unavailable");
}
?>
--FILE--
<?php
require_once __DIR__ . '/func.inc';
$infile = __DIR__ . '/girl.avif';
$outfile = __DIR__ . '/test.avif';
echo 'Decoding AVIF image: ';
$img = imagecreatefromavif($infile);
echo_status($img);
echo 'Default AVIF encoding: ';
echo_status(imageavif($img, $outfile));
echo 'Encoding AVIF at quality 70: ';
echo_status(imageavif($img, $outfile, 70));
echo 'Encoding AVIF at quality 70 with speed 5: ';
echo_status(imageavif($img, $outfile, 70, 5));
echo 'Encoding AVIF with default quality: ';
echo_status(imageavif($img, $outfile, -1));
echo 'Encoding AVIF with illegal quality: ';
try {
imageavif($img, $outfile, 1234);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
echo 'Encoding AVIF with illegal speed: ';
try {
imageavif($img, $outfile, 70, 1234);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
echo 'Encoding AVIF losslessly... ';
echo_status(imageavif($img, $outfile, 100, 0));
echo "Decoding the AVIF we just wrote...\n";
$img_from_avif = imagecreatefromavif($outfile);
// Note we could also forgive a certain number of pixel differences.
// With the current test image, we just didn't need to.
echo 'What is the mean squared error of the two images? ',
mse($img, $img_from_avif);
unlink($outfile);
function echo_status($success) {
echo $success ? "ok\n" : "failed\n";
}
?>
--EXPECT--
Decoding AVIF image: ok
Default AVIF encoding: ok
Encoding AVIF at quality 70: ok
Encoding AVIF at quality 70 with speed 5: ok
Encoding AVIF with default quality: ok
Encoding AVIF with illegal quality: imageavif(): Argument #3 ($quality) must be between -1 and 100
Encoding AVIF with illegal speed: imageavif(): Argument #4 ($speed) must be between -1 and 10
Encoding AVIF losslessly... ok
Decoding the AVIF we just wrote...
What is the mean squared error of the two images? 0