|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AmaTeam\Image\Projection\Conversion\Processor\FXAA; |
|
4
|
|
|
|
|
5
|
|
|
use AmaTeam\Image\Projection\Image\Utility\Luma; |
|
6
|
|
|
|
|
7
|
|
|
class EdgeDistanceCalculator |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @param Edge $edge |
|
11
|
|
|
* @param bool $inverse |
|
12
|
|
|
* @return int |
|
13
|
|
|
* |
|
14
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
|
15
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
16
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity) |
|
17
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
18
|
|
|
*/ |
|
19
|
|
|
public static function calculate(Edge $edge, $inverse = false) |
|
20
|
|
|
{ |
|
21
|
|
|
$width = $edge->image->getWidth(); |
|
22
|
|
|
$height = $edge->image->getHeight(); |
|
23
|
|
|
$offset = $inverse ? -1 : 1; |
|
24
|
|
|
if ($edge->horizontal) { |
|
25
|
|
|
$xOffset = $offset; |
|
26
|
|
|
$yOffset = 0; |
|
27
|
|
|
$xSampleOffset = 0; |
|
28
|
|
|
$ySampleOffset = $edge->inward ? -1 : 1; |
|
29
|
|
|
} else { |
|
30
|
|
|
$xOffset = 0; |
|
31
|
|
|
$yOffset = $offset; |
|
32
|
|
|
$xSampleOffset = $edge->inward ? -1 : 1; |
|
33
|
|
|
$ySampleOffset = 0; |
|
34
|
|
|
} |
|
35
|
|
|
$distance = 1; |
|
36
|
|
|
$averageLuma = $edge->averageLuma; |
|
37
|
|
|
for ($i = 0; $i < 10; $i++) { |
|
38
|
|
|
$edgeX = $edge->x + $distance * $xOffset; |
|
39
|
|
|
$edgeY = $edge->y + $distance * $yOffset; |
|
40
|
|
|
if ($edgeX < 0 || $edgeX >= $width || $edgeY < 0 || $edgeY >= $height) { |
|
41
|
|
|
break; |
|
42
|
|
|
} |
|
43
|
|
|
$sampleX = $edgeX + $xSampleOffset; |
|
44
|
|
|
$sampleY = $edgeY + $ySampleOffset; |
|
45
|
|
|
if ($sampleX < 0 || $sampleX >= $width || $sampleY < 0 || $sampleY >= $height) { |
|
46
|
|
|
break; |
|
47
|
|
|
} |
|
48
|
|
|
$edgeColor = $edge->image->getColorAt($edgeX, $edgeY); |
|
49
|
|
|
$sampleColor = $edge->image->getColorAt($sampleX, $sampleY); |
|
50
|
|
|
$edgeLuma = Luma::compute($edgeColor); |
|
51
|
|
|
$sampleLuma = Luma::compute($sampleColor); |
|
52
|
|
|
$luma = ($edgeLuma + $sampleLuma) / 2; |
|
53
|
|
|
if (abs($averageLuma - $luma) >= $edge->threshold) { |
|
54
|
|
|
break; |
|
55
|
|
|
} |
|
56
|
|
|
$multiplier = (int) ($distance / 4); |
|
57
|
|
|
$distance += 1 + $multiplier * $multiplier; |
|
58
|
|
|
} |
|
59
|
|
|
return $distance; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public static function apply(Edge $edge) |
|
63
|
|
|
{ |
|
64
|
|
|
$edge->forwardDistance = self::calculate($edge, false); |
|
65
|
|
|
$edge->backwardDistance = self::calculate($edge, true); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|