1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Pixeler |
5
|
|
|
* |
6
|
|
|
* UTF-8 Dot matrix renderer. |
7
|
|
|
* |
8
|
|
|
* @package pixeler |
9
|
|
|
* @author [email protected] |
10
|
|
|
* @version 1.0 |
11
|
|
|
* @copyright Stefano Azzolini - 2014 - http://dreamnoctis.com |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Pixeler; |
15
|
|
|
|
16
|
|
|
class Matrix { |
17
|
|
|
protected $matrix = null, |
18
|
|
|
$colors = null, |
19
|
|
|
$width = 0, |
20
|
|
|
$height = 0, |
21
|
|
|
$size = 0, |
22
|
|
|
$csize = 0; |
23
|
|
|
|
24
|
|
|
public function __construct($width,$height){ |
25
|
|
|
$this->width = 2 * ($w2=ceil($width/2)); |
|
|
|
|
26
|
|
|
$this->height = 4 * ($h2=ceil($height/4)); |
|
|
|
|
27
|
|
|
$this->size = $this->width * $this->height; |
|
|
|
|
28
|
|
|
$this->csize = $w2 * $h2; |
|
|
|
|
29
|
|
|
$this->matrix = new \SplFixedArray($this->size); |
30
|
|
|
$this->colors = new \SplFixedArray($this->csize); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setData(array $data) { |
34
|
|
|
$this->matrix = \SplFixedArray::fromArray(array_slice($data,0,$this->size),false); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function clearColors() { |
38
|
|
|
$this->colors = new \SplFixedArray($this->csize); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function clear() { |
42
|
|
|
$this->matrix = new \SplFixedArray($this->size); |
43
|
|
|
$this->colors = new \SplFixedArray($this->csize); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function setPixel($x, $y, $value = true,$color = null){ |
47
|
|
|
$y = (int)$y; $x = (int)$x; |
48
|
|
|
if ( $x < $this->width && $y < $this->height) { |
49
|
|
|
$this->matrix[$x + $y * $this->width] = !! $value; |
50
|
|
|
$this->colors[$x>>1 + ($y>>2) * $this->width] = $color; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getPixel($x, $y){ |
55
|
|
|
$y = (int)$y; $x = (int)$x; |
56
|
|
|
if ( $x < $this->width && $y < $this->height) { |
57
|
|
|
return [ $this->matrix[$x + $y * $this->width], $this->colors[$x + $y * $this->width] ]; |
58
|
|
|
} else { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function render(){ |
64
|
|
|
$i = 0; |
|
|
|
|
65
|
|
|
$w = $this->width; |
66
|
|
|
$w2 = $this->width >> 1; |
67
|
|
|
$h = $this->height; |
68
|
|
|
$m = $this->matrix; |
69
|
|
|
$c = $this->colors; |
70
|
|
|
$ESC = chr(27); |
71
|
|
|
ob_start(); |
72
|
|
|
for ($y = 0, $cy = 0; $y < $h; $y += 4, $cy++){ |
73
|
|
|
$cx = 0; $cy0 = $cy * $w2; |
74
|
|
|
$y0 = $y * $w; $y1 = ($y + 1) * $w; $y2 = ($y + 2) * $w; $y3 = ($y + 3) * $w; |
75
|
|
|
for ($x = 0; $x < $w; $x += 2, $cx++){ |
76
|
|
|
$cell = 0; |
77
|
|
|
$x1 = $x + 1; |
78
|
|
|
|
79
|
|
|
foreach([ |
80
|
|
|
0x01 => $x1 + $y3, |
81
|
|
|
0x02 => $x + $y3, |
82
|
|
|
0x04 => $x1 + $y2, |
83
|
|
|
0x08 => $x + $y2, |
84
|
|
|
0x10 => $x1 + $y1, |
85
|
|
|
0x20 => $x + $y1, |
86
|
|
|
0x40 => $x1 + $y0, |
87
|
|
|
0x80 => $x + $y0, |
88
|
|
|
] as $bit => $ofs) { |
89
|
|
|
if (!empty($m[$ofs])) $cell |= $bit; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$dots_r = 0x2800; |
93
|
|
|
|
94
|
|
|
if ($cell & 0x80) $dots_r |= 0x01; |
95
|
|
|
if ($cell & 0x40) $dots_r |= 0x08; |
96
|
|
|
if ($cell & 0x20) $dots_r |= 0x02; |
97
|
|
|
if ($cell & 0x10) $dots_r |= 0x10; |
98
|
|
|
if ($cell & 0x08) $dots_r |= 0x04; |
99
|
|
|
if ($cell & 0x04) $dots_r |= 0x20; |
100
|
|
|
if ($cell & 0x02) $dots_r |= 0x40; |
101
|
|
|
if ($cell & 0x01) $dots_r |= 0x80; |
102
|
|
|
|
103
|
|
|
$dots_r_64 = $dots_r % 64; |
104
|
|
|
$dots_r_4096 = $dots_r % 4096; |
105
|
|
|
|
106
|
|
|
// Print UTF-8 character and color |
107
|
|
|
echo |
108
|
|
|
$ESC.'[' . ($c[$cy0+$cx]?'38;5;'.$c[$cy0+$cx]:39).'m' |
109
|
|
|
. chr(224 + (($dots_r - $dots_r_4096) >> 12 )) |
110
|
|
|
. chr(128 + (($dots_r_4096 - $dots_r_64) >> 6 )) |
111
|
|
|
. chr(128 + $dots_r_64); |
112
|
|
|
} |
113
|
|
|
echo $ESC."[0m\n"; |
114
|
|
|
} |
115
|
|
|
$buffer = ob_get_contents(); |
116
|
|
|
ob_end_clean(); |
117
|
|
|
return $buffer; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function __toString(){ |
121
|
|
|
return $this->render(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.