1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Undemanding\Difference; |
4
|
|
|
|
5
|
|
|
use Undemanding\Difference\Calculation; |
6
|
|
|
use Undemanding\Difference\Transformation; |
7
|
|
|
|
8
|
|
|
class Difference |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
private $bitmap; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
private $width; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $height; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Image $image |
27
|
|
|
*/ |
28
|
1 |
|
public function __construct(Image $image) |
29
|
|
|
{ |
30
|
1 |
|
$this->bitmap = $image->getBitmap(); |
31
|
1 |
|
$this->width = $image->getWidth(); |
32
|
1 |
|
$this->height = $image->getHeight(); |
33
|
1 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
1 |
|
public function getBitmap() |
39
|
|
|
{ |
40
|
1 |
|
return $this->bitmap; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return int |
45
|
|
|
*/ |
46
|
|
|
public function getWidth() |
47
|
|
|
{ |
48
|
|
|
return $this->width; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return int |
53
|
|
|
*/ |
54
|
|
|
public function getHeight() |
55
|
|
|
{ |
56
|
|
|
return $this->height; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* New Difference with scaled differences. |
61
|
|
|
* |
62
|
|
|
* @param float $factor |
63
|
|
|
* |
64
|
|
|
* @return Difference |
65
|
|
|
*/ |
66
|
1 |
View Code Duplication |
public function withScale($factor) |
|
|
|
|
67
|
|
|
{ |
68
|
1 |
|
$maximum = $this->maximum(); |
69
|
1 |
|
$transformation = new Transformation\Scale(); |
70
|
|
|
|
71
|
1 |
|
$bitmap = $transformation( |
72
|
1 |
|
$this->bitmap, $this->width, $this->height, $maximum, $factor |
73
|
1 |
|
); |
74
|
|
|
|
75
|
1 |
|
return $this->cloneWith("bitmap", $bitmap); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Maximum difference for all bitmap pixels. |
80
|
|
|
* |
81
|
|
|
* @return int |
82
|
|
|
*/ |
83
|
1 |
|
private function maximum() |
84
|
|
|
{ |
85
|
1 |
|
$calculation = new Calculation\Maximum(); |
86
|
|
|
|
87
|
1 |
|
return $calculation( |
88
|
1 |
|
$this->bitmap, $this->width, $this->height |
89
|
1 |
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* New Difference from reduced standard deviation. |
94
|
|
|
* |
95
|
|
|
* @return Difference |
96
|
|
|
*/ |
97
|
1 |
View Code Duplication |
public function withReducedStandardDeviation() |
|
|
|
|
98
|
|
|
{ |
99
|
1 |
|
$deviation = $this->standardDeviation(); |
100
|
1 |
|
$transformation = new Transformation\ReducedStandardDeviation(); |
101
|
|
|
|
102
|
1 |
|
$bitmap = $transformation( |
103
|
1 |
|
$this->bitmap, $this->width, $this->height, $deviation |
104
|
1 |
|
); |
105
|
|
|
|
106
|
1 |
|
return $this->cloneWith("bitmap", $bitmap); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Standard deviation for all bitmap pixels. |
111
|
|
|
* |
112
|
|
|
* @return float |
113
|
|
|
*/ |
114
|
1 |
|
private function standardDeviation() |
115
|
|
|
{ |
116
|
1 |
|
$average = $this->average(); |
117
|
1 |
|
$calculation = new Calculation\StandardDeviation(); |
118
|
|
|
|
119
|
1 |
|
return $calculation( |
120
|
1 |
|
$this->bitmap, $this->width, $this->height, $average |
121
|
1 |
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Average difference for all bitmap pixels. |
126
|
|
|
* |
127
|
|
|
* @return int |
128
|
|
|
*/ |
129
|
1 |
|
private function average() |
130
|
|
|
{ |
131
|
1 |
|
$calculation = new Calculation\Average(); |
132
|
|
|
|
133
|
1 |
|
return $calculation( |
134
|
1 |
|
$this->bitmap, $this->width, $this->height |
135
|
1 |
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $property |
140
|
|
|
* @param mixed $value |
141
|
|
|
* |
142
|
|
|
* @return Difference |
143
|
|
|
*/ |
144
|
1 |
|
private function cloneWith($property, $value) |
145
|
|
|
{ |
146
|
1 |
|
$clone = clone $this; |
147
|
1 |
|
$clone->$property = $value; |
148
|
|
|
|
149
|
1 |
|
return $clone; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Boundary for all significant pixels. |
154
|
|
|
* |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
1 |
|
public function boundary() |
158
|
|
|
{ |
159
|
1 |
|
$ax = $this->width; |
160
|
1 |
|
$bx = 0; |
161
|
1 |
|
$ay = $this->width; |
162
|
1 |
|
$by = 0; |
163
|
|
|
|
164
|
1 |
|
for ($y = 0; $y < $this->height; $y++) { |
165
|
1 |
|
for ($x = 0; $x < $this->width; $x++) { |
166
|
1 |
|
if ($this->bitmap[$y][$x] > 0) { |
167
|
1 |
|
if ($x > $bx) { |
168
|
1 |
|
$bx = $x; |
169
|
1 |
|
} |
170
|
|
|
|
171
|
1 |
|
if ($x < $ax) { |
172
|
1 |
|
$ax = $x; |
173
|
1 |
|
} |
174
|
|
|
|
175
|
1 |
|
if ($y > $by) { |
176
|
1 |
|
$by = $y; |
177
|
1 |
|
} |
178
|
|
|
|
179
|
1 |
|
if ($y < $ay) { |
180
|
1 |
|
$ay = $y; |
181
|
1 |
|
} |
182
|
1 |
|
} |
183
|
1 |
|
} |
184
|
1 |
|
} |
185
|
|
|
|
186
|
1 |
|
$ax = ($ax / $this->width) * $this->width; |
187
|
1 |
|
$bx = ((($bx + 1) / $this->width) * $this->width) - $ax; |
188
|
1 |
|
$ay = ($ay / $this->height) * $this->height; |
189
|
1 |
|
$by = ((($by + 1) / $this->height) * $this->height) - $ay; |
190
|
|
|
|
191
|
|
|
return [ |
192
|
1 |
|
"left" => $ax, |
193
|
1 |
|
"top" => $ay, |
194
|
1 |
|
"width" => $bx, |
195
|
|
|
"height" => $by |
196
|
1 |
|
]; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Percentage of different pixels in the bitmap. |
201
|
|
|
* |
202
|
|
|
* @return float |
203
|
|
|
*/ |
204
|
1 |
|
public function percentage() |
205
|
|
|
{ |
206
|
1 |
|
$calculation = new Calculation\Percentage(); |
207
|
|
|
|
208
|
1 |
|
return $calculation( |
209
|
1 |
|
$this->bitmap, $this->width, $this->height |
210
|
1 |
|
); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.