-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[v1.x] feat: Add ability to define pixel difference calculators (#18)
This is a backport of PR #17 to the v1.x release branch. Co-authored-by: Ben Thomson <15900351+bennothommo@users.noreply.github.com>
- Loading branch information
1 parent
e550e5e
commit 743303a
Showing
10 changed files
with
230 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace AssertGD; | ||
|
||
use AssertGD\GDImage; | ||
|
||
/** | ||
* Difference calculator. | ||
* | ||
* Determines the difference between two given images. | ||
*/ | ||
interface DiffCalculator | ||
{ | ||
/** | ||
* Calculates the difference between two pixels at the given coordinates. | ||
* | ||
* This method will be provided with two `GDImage` objects representing the images being compared, and co-ordinates | ||
* of the pixel being compared. | ||
* | ||
* The method should return a float value between 0 and 1 inclusive, with 0 meaning that the pixels of both images | ||
* at the given co-ordinates are an exact match, and 1 meaning that the pixels are the complete opposite. | ||
*/ | ||
public function calculate(GDImage $imageA, GDImage $imageB, $pixelX, $pixelY); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace AssertGD\DiffCalculator; | ||
|
||
use AssertGD\DiffCalculator; | ||
use AssertGD\GDImage; | ||
|
||
/** | ||
* Calculate the difference between two pixels using the RGBA channels. | ||
* | ||
* This is the default calculation method used by the `AssertGD` package. It simply takes each individual channel and | ||
* compares the delta between the channel values of the two images. | ||
* | ||
* This works well for most images, but may not work for images with transparent pixels if the transparent pixels have | ||
* different RGB values. | ||
*/ | ||
class RgbaChannels implements DiffCalculator | ||
{ | ||
public function calculate(GDImage $imageA, GDImage $imageB, $pixelX, $pixelY) | ||
{ | ||
$pixelA = $imageA->getPixel($pixelX, $pixelY); | ||
$pixelB = $imageB->getPixel($pixelX, $pixelY); | ||
|
||
$diffR = abs($pixelA['red'] - $pixelB['red']) / 255; | ||
$diffG = abs($pixelA['green'] - $pixelB['green']) / 255; | ||
$diffB = abs($pixelA['blue'] - $pixelB['blue']) / 255; | ||
$diffA = abs($pixelA['alpha'] - $pixelB['alpha']) / 127; | ||
|
||
return ($diffR + $diffG + $diffB + $diffA) / 4; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace AssertGD\DiffCalculator; | ||
|
||
use AssertGD\DiffCalculator; | ||
use AssertGD\GDImage; | ||
|
||
/** | ||
* Calculate the difference between two pixels using the RGB channels, and scales down the RGB difference by the alpha | ||
* channel. | ||
* | ||
* This calculation will pre-multiply the RGB channels by the opacity percentage (alpha) of the pixel, meaning that a | ||
* translucent pixel will have less of an impact on the overall difference than an opaque pixel. For transparent pixels, | ||
* this will mean that the RGB difference will be scaled down to zero, effectively meaning that transparent pixels will | ||
* match regardless of their RGB values. | ||
* | ||
* This calculation method is useful for images with transparent pixels or images that have been anti-aliased or | ||
* blurred over a transparent background, effectively making translucent pixels less likely to cause a false positive as | ||
* being different. | ||
*/ | ||
class ScaledRgbChannels implements DiffCalculator | ||
{ | ||
public function calculate(GDImage $imageA, GDImage $imageB, $pixelX, $pixelY) | ||
{ | ||
$pixelA = $this->premultiply($imageA->getPixel($pixelX, $pixelY)); | ||
$pixelB = $this->premultiply($imageB->getPixel($pixelX, $pixelY)); | ||
|
||
$diffR = abs($pixelA['red'] - $pixelB['red']) / 255; | ||
$diffG = abs($pixelA['green'] - $pixelB['green']) / 255; | ||
$diffB = abs($pixelA['blue'] - $pixelB['blue']) / 255; | ||
|
||
return ($diffR + $diffG + $diffB) / 4; | ||
} | ||
|
||
protected function premultiply(array $pixel) | ||
{ | ||
$alpha = 1 - ($pixel['alpha'] / 127); | ||
|
||
return array( | ||
'red' => $pixel['red'] * $alpha, | ||
'green' => $pixel['green'] * $alpha, | ||
'blue' => $pixel['blue'] * $alpha, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.