-
Notifications
You must be signed in to change notification settings - Fork 8k
Overhaul GD test helpers and affected tests #17309
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
Changes from 1 commit
41e9f7e
b5234c7
93c8b70
518474f
8464a45
a0f2699
fc0b229
9efc96f
2f1d25f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,39 +156,22 @@ function trycatch_dump(...$tests) { | |
} | ||
|
||
/** | ||
* A very simple algorithm for finding the dissimilarity between images, | ||
* mainly useful for checking lossy compression. | ||
* Calculate the mean squared error (in range 0..255**2) | ||
*/ | ||
|
||
/** | ||
* Calculates the euclidean distance of two RGB values. | ||
*/ | ||
function calc_pixel_distance(int $color1, int $color2): float | ||
{ | ||
return sqrt( | ||
((($color1 >> 16) & 0xFF) - (($color2 >> 16) & 0xFF)) ** 2 + | ||
((($color1 >> 8) & 0xFF) - (($color2 >> 8) & 0xFF)) ** 2 + | ||
((($color1 >> 0) & 0xFF) - (($color2 >> 0) & 0xFF)) ** 2 | ||
); | ||
} | ||
|
||
/** | ||
* Calculates dissimilarity of two images. | ||
* | ||
* The dissimilarity. 0 means the images are identical. The higher | ||
* the value, the more dissimilar are the images. | ||
*/ | ||
function calc_image_dissimilarity(GdImage $image1, GdImage $image2): float | ||
function mse(GdImage $image1, GdImage $image2): float | ||
{ | ||
assert(imageistruecolor($image1) && imageistruecolor($image2)); | ||
assert(imagesx($image1) === imagesx($image2) && imagesy($image1) === imagesy($image2)); | ||
$dissimilarity = 0; | ||
$e = $count = 0; | ||
for ($j = 0, $m = imagesy($image1); $j < $m; $j++) { | ||
for ($i = 0, $n = imagesx($image1); $i < $n; $i++) { | ||
$color1 = imagecolorat($image1, $i, $j); | ||
$color2 = imagecolorat($image2, $i, $j); | ||
$dissimilarity += calc_pixel_distance($color1, $color2); | ||
$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; | ||
Comment on lines
+170
to
+172
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just stumbled upon libgd/libgd#751, where the differences are divided by the maximum value, thus yielding values in range [0, 1] (instead of [0, 255]), so the squares would stay in range [0, 1] (instead of [0, 65025]), as would the final result of |
||
$count += 3; | ||
} | ||
} | ||
return $dissimilarity; | ||
return $e / $count; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.