diff --git a/ext/gd/tests/avif_decode_encode.phpt b/ext/gd/tests/avif_decode_encode.phpt index cb7f1ce95a67c..054eb52467637 100644 --- a/ext/gd/tests/avif_decode_encode.phpt +++ b/ext/gd/tests/avif_decode_encode.phpt @@ -14,7 +14,7 @@ gd --FILE-- --FILE-- --EXPECT-- -bool(true) +The images are equal. diff --git a/ext/gd/tests/bug43475.png b/ext/gd/tests/bug43475.png index 774270f63db86..d53a9a3322ae6 100644 Binary files a/ext/gd/tests/bug43475.png and b/ext/gd/tests/bug43475.png differ diff --git a/ext/gd/tests/bug52070.phpt b/ext/gd/tests/bug52070.phpt index 6bbe2363a650b..8bc653ae95ef8 100644 --- a/ext/gd/tests/bug52070.phpt +++ b/ext/gd/tests/bug52070.phpt @@ -20,6 +20,7 @@ imagedashedline($im, 800, 400, 500, 800, $color); imagedashedline($im, 800, 400, 600, 800, $color); imagedashedline($im, 800, 400, 700, 800, $color); imagedashedline($im, 800, 400, 800, 800, $color); +imagepalettetotruecolor($im); include_once __DIR__ . '/func.inc'; test_image_equals_file(__DIR__ . '/bug52070.png', $im); ?> diff --git a/ext/gd/tests/bug52070.png b/ext/gd/tests/bug52070.png index 5ce54b121bc2e..a6f9a3b875d56 100644 Binary files a/ext/gd/tests/bug52070.png and b/ext/gd/tests/bug52070.png differ diff --git a/ext/gd/tests/bug64641.phpt b/ext/gd/tests/bug64641.phpt index edfebee99c92a..d2e51817b9b48 100644 --- a/ext/gd/tests/bug64641.phpt +++ b/ext/gd/tests/bug64641.phpt @@ -13,7 +13,7 @@ if (!(imagetypes() & IMG_PNG)) { ?> --FILE-- --EXPECT-- -IDENTICAL +The images are equal. diff --git a/ext/gd/tests/bug72913.phpt b/ext/gd/tests/bug72913.phpt index 9eb9c24653530..eea77b6ed56b1 100644 --- a/ext/gd/tests/bug72913.phpt +++ b/ext/gd/tests/bug72913.phpt @@ -23,8 +23,16 @@ imagesavealpha($dst, true); imagecopy($dst, $src, 0,0, 0,0, 50,50); -include_once __DIR__ . '/func.inc'; -test_image_equals_file(__DIR__ . '/bug72913.png', $dst); +var_dump(imagecolorsforindex($dst, imagecolorat($dst, 0, 0))); ?> --EXPECT-- -The images are equal. +array(4) { + ["red"]=> + int(255) + ["green"]=> + int(255) + ["blue"]=> + int(255) + ["alpha"]=> + int(127) +} diff --git a/ext/gd/tests/bug72913.png b/ext/gd/tests/bug72913.png deleted file mode 100644 index 83eeb81fb16ac..0000000000000 Binary files a/ext/gd/tests/bug72913.png and /dev/null differ diff --git a/ext/gd/tests/func.inc b/ext/gd/tests/func.inc index 0f10aa7d83dee..0668742d2b8a7 100644 --- a/ext/gd/tests/func.inc +++ b/ext/gd/tests/func.inc @@ -78,9 +78,7 @@ function test_image_equals_file(string $filename, GdImage $actual): void save_actual_image($actual); return; } - $actual = test_to_truecolor($actual); $expected = imagecreatefrompng($filename); - $expected = test_to_truecolor($expected); 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 { + assert(imageistruecolor($expected) && imageistruecolor($actual)); $exp_x = imagesx($expected); $exp_y = imagesy($expected); $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. * * The image is saved right beside the temporary .php test file with the * 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']); $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; +} diff --git a/ext/gd/tests/gd276.phpt b/ext/gd/tests/gd276.phpt index a12bce3983049..ad7b6343f520d 100644 --- a/ext/gd/tests/gd276.phpt +++ b/ext/gd/tests/gd276.phpt @@ -15,6 +15,8 @@ $filename = __DIR__ . "/gd276.bmp"; imagebmp($orig, $filename, true); $saved = imagecreatefrombmp($filename); var_dump($saved !== false); +imagepalettetotruecolor($orig); +imagepalettetotruecolor($saved); test_image_equals_image($orig, $saved); ?> --EXPECT-- diff --git a/ext/gd/tests/imagebmp_basic.phpt b/ext/gd/tests/imagebmp_basic.phpt index b3d1212d014cd..31a62e674a845 100644 --- a/ext/gd/tests/imagebmp_basic.phpt +++ b/ext/gd/tests/imagebmp_basic.phpt @@ -13,7 +13,7 @@ $im = imagecreate(100, 100); imagecolorallocate($im, 0, 0, 0); $white = imagecolorallocate($im, 255, 255, 255); imageline($im, 10,10, 89,89, $white); - +imagepalettetotruecolor($im); require __DIR__ . "/func.inc"; test_image_equals_file(__DIR__ . "/imagebmp_basic.png", $im); ?> diff --git a/ext/gd/tests/imagebmp_basic.png b/ext/gd/tests/imagebmp_basic.png index 26fc7d0afcf9f..d52dd04fb0987 100644 Binary files a/ext/gd/tests/imagebmp_basic.png and b/ext/gd/tests/imagebmp_basic.png differ diff --git a/ext/gd/tests/imagecolorset_basic.phpt b/ext/gd/tests/imagecolorset_basic.phpt index 5ed9a1ab8600e..7677b85948aa4 100644 --- a/ext/gd/tests/imagecolorset_basic.phpt +++ b/ext/gd/tests/imagecolorset_basic.phpt @@ -25,6 +25,7 @@ $bg = imagecolorat($im, 0, 0); // Set the background to be blue imagecolorset($im, $bg, 0, 0, 255); +imagepalettetotruecolor($im); include_once __DIR__ . '/func.inc'; test_image_equals_file(__DIR__ . '/imagecolorset_basic.png', $im); imagedestroy($im); diff --git a/ext/gd/tests/imagecolorset_basic.png b/ext/gd/tests/imagecolorset_basic.png index b9e77d116932d..6a37d1d87176f 100644 Binary files a/ext/gd/tests/imagecolorset_basic.png and b/ext/gd/tests/imagecolorset_basic.png differ diff --git a/ext/gd/tests/imagecreatefromstring_bmp.phpt b/ext/gd/tests/imagecreatefromstring_bmp.phpt index 34db04297e3aa..746e566fdbfea 100644 --- a/ext/gd/tests/imagecreatefromstring_bmp.phpt +++ b/ext/gd/tests/imagecreatefromstring_bmp.phpt @@ -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" . "\x0A\x00\x00\x00\x0A\x00\x00\x00\x00\x01"; $im = imagecreatefromstring($bmp); - +imagepalettetotruecolor($im); include_once __DIR__ . '/func.inc'; test_image_equals_file(__DIR__ . '/imagecreatefromstring_bmp.png', $im); ?> diff --git a/ext/gd/tests/imagecreatefromstring_bmp.png b/ext/gd/tests/imagecreatefromstring_bmp.png index 04ffc83c468e3..a9ae65fdc9827 100644 Binary files a/ext/gd/tests/imagecreatefromstring_bmp.png and b/ext/gd/tests/imagecreatefromstring_bmp.png differ diff --git a/ext/gd/tests/imagegammacorrect_variation1.phpt b/ext/gd/tests/imagegammacorrect_variation1.phpt index 55370aa49cf87..67a8e403377ca 100644 --- a/ext/gd/tests/imagegammacorrect_variation1.phpt +++ b/ext/gd/tests/imagegammacorrect_variation1.phpt @@ -26,7 +26,7 @@ $half2 = imagefilledarc ( $image, 75, 75, 70, 70, 0, -180, $gray, IMG_ARC_PIE ) $gamma = imagegammacorrect($image, 1, 5); var_dump((bool) $gamma); - +imagepalettetotruecolor($image); include_once __DIR__ . '/func.inc'; test_image_equals_file(__DIR__ . '/imagegammacorrect_variation1.png', $image); ?> diff --git a/ext/gd/tests/imagegammacorrect_variation1.png b/ext/gd/tests/imagegammacorrect_variation1.png index f9718890968a2..8b0e80c5ea034 100644 Binary files a/ext/gd/tests/imagegammacorrect_variation1.png and b/ext/gd/tests/imagegammacorrect_variation1.png differ diff --git a/ext/gd/tests/imagegammacorrect_variation2.phpt b/ext/gd/tests/imagegammacorrect_variation2.phpt index 33c89d205a1a7..7621ff98c8945 100644 --- a/ext/gd/tests/imagegammacorrect_variation2.phpt +++ b/ext/gd/tests/imagegammacorrect_variation2.phpt @@ -33,6 +33,9 @@ function test_gamma($in, $out, $constructor) imagegammacorrect($im, $in, $out); + if ($constructor === "imagecreate") { + imagepalettetotruecolor($im); + } $filename = __DIR__ . DIRECTORY_SEPARATOR . "imagegammacorrect_variation2_{$in}_{$out}.png"; $kind = $constructor === 'imagecreate' ? 'palette' : 'truecolor'; diff --git a/ext/gd/tests/imagetruecolortopalette_basic.phpt b/ext/gd/tests/imagetruecolortopalette_basic.phpt index 2f1c2961a603c..4abe6cf0b37ad 100644 --- a/ext/gd/tests/imagetruecolortopalette_basic.phpt +++ b/ext/gd/tests/imagetruecolortopalette_basic.phpt @@ -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 ); var_dump(imagetruecolortopalette($image, true, 2)); - +imagepalettetotruecolor($image); include_once __DIR__ . '/func.inc'; test_image_equals_file(__DIR__ . '/imagetruecolortopalette_basic.png', $image); ?> diff --git a/ext/gd/tests/imagetruecolortopalette_basic.png b/ext/gd/tests/imagetruecolortopalette_basic.png index 7d042f2ac3da2..79e14eef35908 100644 Binary files a/ext/gd/tests/imagetruecolortopalette_basic.png and b/ext/gd/tests/imagetruecolortopalette_basic.png differ diff --git a/ext/gd/tests/similarity.inc b/ext/gd/tests/similarity.inc deleted file mode 100644 index cb0dba77f2064..0000000000000 --- a/ext/gd/tests/similarity.inc +++ /dev/null @@ -1,64 +0,0 @@ -> 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; -} diff --git a/ext/gd/tests/test_image_equals_file_palette.phpt b/ext/gd/tests/test_image_equals_file_palette.phpt index 2e294c04c4066..d8f6a0556b20c 100644 --- a/ext/gd/tests/test_image_equals_file_palette.phpt +++ b/ext/gd/tests/test_image_equals_file_palette.phpt @@ -18,6 +18,7 @@ $red = imagecolorallocate($im, 255, 0, 0); imagefilledrectangle($im, 3,3, 7,7, $red); $filename = __DIR__ . DIRECTORY_SEPARATOR . 'test_image_equals_file_palette.png'; +imagepalettetotruecolor($im); imagepng($im, $filename); $im = imagecreate(10, 10); @@ -25,6 +26,7 @@ imagecolorallocate($im, 255, 255, 255); $blue = imagecolorallocate($im, 0, 0, 255); imagefilledrectangle($im, 3,3, 7,7, $blue); +imagepalettetotruecolor($im); test_image_equals_file($filename, $im); $im = imagecreate(10, 10); @@ -33,6 +35,7 @@ imagecolorallocate($im, 0, 0, 0); $red = imagecolorallocate($im, 255, 0, 0); imagefilledrectangle($im, 3,3, 7,7, $red); +imagepalettetotruecolor($im); test_image_equals_file($filename, $im); ?> --EXPECT-- diff --git a/ext/gd/tests/webp_basic.phpt b/ext/gd/tests/webp_basic.phpt index 97b0451c09b8d..ef2fae8652560 100644 --- a/ext/gd/tests/webp_basic.phpt +++ b/ext/gd/tests/webp_basic.phpt @@ -12,7 +12,7 @@ if (!function_exists('imagewebp') || !function_exists('imagecreatefromwebp')) ?> --FILE--