-
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.
feat: Add ability to define pixel difference calculators (#17)
- Loading branch information
1 parent
3322a78
commit 8e550d0
Showing
9 changed files
with
210 additions
and
31 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, int $pixelX, int $pixelY): float; | ||
} |
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, int $pixelX, int $pixelY): float | ||
{ | ||
$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, int $pixelX, int $pixelY): float | ||
{ | ||
$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 [ | ||
'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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.