|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the kaloa/image package. |
|
5
|
|
|
* |
|
6
|
|
|
* For full copyright and license information, please view the LICENSE file |
|
7
|
|
|
* that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Kaloa\Image\Filter; |
|
11
|
|
|
|
|
12
|
|
|
use Kaloa\Image\Filter\AbstractFilter; |
|
13
|
|
|
use Kaloa\Image\Image; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* |
|
17
|
|
|
* |
|
18
|
|
|
* The class is modeled after java.awt.image.RescaleOp |
|
19
|
|
|
* <http://docs.oracle.com/javase/7/docs/api/java/awt/image/RescaleOp.html> |
|
20
|
|
|
*/ |
|
21
|
|
|
final class RescaleOpFilter extends AbstractFilter |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* |
|
25
|
|
|
* @var array (float[3]) |
|
26
|
|
|
*/ |
|
27
|
|
|
private $scaleFactors; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* |
|
31
|
|
|
* @var array (float[3]) |
|
32
|
|
|
*/ |
|
33
|
|
|
private $offsets; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $scaleFactors Scaling factors for (r, g, b) components |
|
38
|
|
|
* @param array $offsets Offsets for (r, g, b) components |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function __construct(array $scaleFactors, array $offsets) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$this->scaleFactors = $scaleFactors; |
|
43
|
1 |
|
$this->offsets = $offsets; |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Apply scaling factors and offsets to each pixel |
|
48
|
|
|
* |
|
49
|
|
|
* The algorithm uses micro-optimization techniques. As it is written in |
|
50
|
|
|
* PHP, nevertheless expect runtimes in the range of a second per 200,000 |
|
51
|
|
|
* pixels or so. That is really slow compared to non-PHP implementations. |
|
52
|
|
|
* |
|
53
|
|
|
* @param resource $img GD image resource |
|
54
|
|
|
* @return resource GD image resource |
|
55
|
|
|
*/ |
|
56
|
1 |
|
private function loop($img) |
|
57
|
|
|
{ |
|
58
|
|
|
// Cache already computed pixels |
|
59
|
1 |
|
$cache = array(); |
|
60
|
|
|
|
|
61
|
|
|
// Compute all 256 rescaled values for each color dimension (R, G, B) |
|
62
|
1 |
|
for ($dimension = 0; $dimension <= 2; ++$dimension) { |
|
63
|
1 |
|
$s = $this->scaleFactors[$dimension]; |
|
64
|
1 |
|
$o = $this->offsets[$dimension]; |
|
65
|
|
|
|
|
66
|
1 |
|
$table[$dimension] = array_fill(0, 256, 0); |
|
|
|
|
|
|
67
|
1 |
|
for ($i = 0; $i <= 255; $i++) { |
|
68
|
1 |
|
$tmp = (int) round($i * $s + $o); |
|
69
|
1 |
|
if ($tmp > 255) $tmp = 255; else if ($tmp < 0) $tmp = 0; |
|
70
|
1 |
|
$table[$dimension][$i] = $tmp; |
|
|
|
|
|
|
71
|
1 |
|
} |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
// Dereferencing ($table[0]) would be slower |
|
|
|
|
|
|
75
|
1 |
|
list($table0, $table1, $table2) = $table; |
|
76
|
|
|
|
|
77
|
1 |
|
$width = imagesx($img); |
|
78
|
1 |
|
$y = imagesy($img); |
|
79
|
|
|
|
|
80
|
|
|
// Loops through the image row by row from bottom right to top left. The |
|
81
|
|
|
// idea here is to reduce the number of opcodes in the inner loop |
|
82
|
|
|
do { |
|
83
|
1 |
|
--$y; |
|
84
|
1 |
|
$x = $width; |
|
85
|
|
|
do |
|
86
|
1 |
|
if (isset($cache[$rgb = imagecolorat($img, --$x, $y)])) |
|
87
|
1 |
|
imagesetpixel($img, $x, $y, $cache[$rgb]); |
|
88
|
|
|
else |
|
89
|
1 |
|
imagesetpixel($img, $x, $y, |
|
90
|
1 |
|
$cache[$rgb] = imagecolorallocate( |
|
91
|
1 |
|
$img, |
|
92
|
1 |
|
$table0[$rgb >> 16 & 0xFF], |
|
93
|
1 |
|
$table1[$rgb >> 8 & 0xFF], |
|
94
|
1 |
|
$table2[$rgb & 0xFF])); |
|
95
|
1 |
|
while ($x); |
|
96
|
1 |
|
} while ($y); |
|
97
|
|
|
|
|
98
|
1 |
|
return $img; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* |
|
103
|
|
|
* |
|
104
|
|
|
* For each pixel do: pixelVector = pixelVector * scaleFactors + offsets |
|
105
|
|
|
* |
|
106
|
|
|
* @param resource $img |
|
|
|
|
|
|
107
|
|
|
* @return resource |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function render(Image $srcImg) |
|
110
|
|
|
{ |
|
111
|
1 |
|
$img = $srcImg->getResource(); |
|
112
|
|
|
|
|
113
|
1 |
|
$img = $this->loop($img); |
|
114
|
|
|
|
|
115
|
1 |
|
return $img; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.