|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thermal\Graphics\Filter; |
|
4
|
|
|
|
|
5
|
|
|
use Thermal\Graphics\Filter; |
|
6
|
|
|
|
|
7
|
|
|
class BayerOrdered implements Filter |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Convert an image resource to black and white aplying dither. |
|
11
|
|
|
* The original image resource will not be changed, a new image resource will be created. |
|
12
|
|
|
* |
|
13
|
|
|
* @param ImageResource $image The source image resource |
|
14
|
|
|
* @return ImageResource The black and white image resource |
|
15
|
|
|
*/ |
|
16
|
1 |
|
public function process($image) |
|
17
|
|
|
{ |
|
18
|
1 |
|
$width = imagesx($image); |
|
19
|
1 |
|
$height = imagesy($image); |
|
20
|
1 |
|
$new_image = imagecreatetruecolor($width, $height); |
|
21
|
|
|
// sets background to black |
|
22
|
1 |
|
$black = imagecolorallocate($new_image, 0, 0, 0); |
|
|
|
|
|
|
23
|
1 |
|
$white = imagecolorallocate($new_image, 255, 255, 255); |
|
24
|
|
|
$pattern = [ |
|
25
|
1 |
|
[ 15, 195, 60, 240], |
|
26
|
|
|
[135, 75, 180, 120], |
|
27
|
|
|
[ 45, 225, 30, 210], |
|
28
|
|
|
[165, 105, 150, 90] |
|
29
|
|
|
]; |
|
30
|
1 |
|
for ($y = 0; $y < $height; $y++) { |
|
31
|
1 |
|
for ($x = 0; $x < $width; $x++) { |
|
32
|
1 |
|
$color = imagecolorat($image, $x, $y); |
|
33
|
1 |
|
$red = ($color >> 16) & 0xFF; |
|
34
|
1 |
|
$green = ($color >> 8) & 0xFF; |
|
35
|
1 |
|
$blue = $color & 0xFF; |
|
36
|
1 |
|
$gray = (int)($red * 0.3 + $green * 0.59 + $blue * 0.11); |
|
37
|
1 |
|
$threshold = $pattern[$y % 4][$x % 4]; |
|
38
|
1 |
|
$gray = ($gray + $threshold) >> 1; |
|
39
|
1 |
|
if ($gray > 127) { |
|
40
|
1 |
|
imagesetpixel($new_image, $x, $y, $white); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
1 |
|
return $new_image; |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.