This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Encoder.php
238 lines (206 loc) · 6.95 KB
/
Encoder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
namespace GIFEndec;
use GIFEndec\IO\MemoryStream;
class Encoder
{
/**
* @var MemoryStream
*/
protected $output;
/**
* @var bool
*/
protected $hasHeader = false;
/**
* @var int
*/
protected $frameIndex = 0;
/**
* @var
*/
protected $firstFrameBytes;
/**
* @var string
*/
protected $globalColorTable;
/**
* @var int
*/
protected $globalColorTableSize;
/**
* @var int
*/
protected $repetitions = 0;
/**
* @param int $repetitions By default, repeat forever
*/
public function __construct($repetitions = 0)
{
$this->output = new MemoryStream();
$this->repetitions = $repetitions;
}
/**
* @param $bytes
* @param $frameIndex
*/
protected function checkIfNotAnimated(&$bytes, $frameIndex)
{
$packedFields = ord($bytes[10]);
$globalColorTableSize = 3 * (2 << ($packedFields & 0x07));
for ($j = (13 + $globalColorTableSize), $k = true; $k; $j++) {
switch ($bytes[$j]) {
case "!":
if ((substr($bytes, $j + 3, 8)) == "NETSCAPE") {
throw new \RuntimeException(
"Cannot make animated GIF from animated frame (#{$frameIndex})."
);
}
break;
case ";":
$k = false;
break;
}
}
}
/**
* Writes Header, Logical Screen Descriptor and Global Color Table to output
* @param $firstFrame
*/
protected function addHeader(Frame $firstFrame)
{
$this->firstFrameBytes = $firstFrame->getStream()->getContents();
$this->output->writeString('GIF89a');
$packedFields = ord($this->firstFrameBytes[10]);
if ($globalColorTableFlag = $packedFields & 0x80) {
$this->globalColorTableSize = 3 * (2 << ($packedFields & 0x07));
$this->globalColorTable = substr($this->firstFrameBytes, 13, $this->globalColorTableSize);
$this->output->writeString(
substr($this->firstFrameBytes, 6, 7). // copy Logical Screen Descriptor
$this->globalColorTable. // copy Global Color Table
"!\377\13NETSCAPE2.0\3\1".$this->convertUnsignedShort($this->repetitions)."\0"
);
}
$this->hasHeader = true;
}
/**
* @param Frame $frame
* @todo Detect transparency from frame binary source
*/
public function addFrame(Frame $frame)
{
$bytes = $frame->getStream()->getContents();
$this->checkIfNotAnimated($bytes, $this->frameIndex);
if (!$this->hasHeader) {
$this->addHeader($frame);
}
$tcolor = $frame->getTransparentColor();
//$tcolor = $color->red | ($color->green << 8) | ($color->blue << 16);
// ADD FRAMES START //
$localPackedFields = ord($bytes[10]);
$localColorTableSize = 3 * (2 << ($localPackedFields & 0x07));
$localColorTable = substr($bytes, 13, $localColorTableSize);
$imgData = substr($bytes, 13 + $localColorTableSize, -1);
// Local Graphic Control Extension
$localGCE = new MemoryStream();
$localGCE->writeBytes([
0x21, // Extension Introducer, contains the fixed value 0x21.
0xF9, // Graphic Control Label, contains the fixed value 0xF9.
0x04, // Block Size, contains the fixed value 4.
($frame->getDisposalMethod() << 2) + 0, // <Packed Fields>
]);
$localGCE->writeString($this->convertUnsignedShort($frame->getDuration()));
$localGCE->writeBytes([
0x00, // Transparent Color Index
0x00 // Block Terminator
]);
// If transparent color exists and Global Color Table is enabled
if ($tcolor instanceof Color && $localPackedFields & 0x80) {
// Look for transparent color in Global Color Table
for ($j = 0; $j < $localColorTableSize / 3; $j++) {
if (ord($localColorTable[3 * $j + 0]) == ($tcolor->red & 0xFF) &&
ord($localColorTable[3 * $j + 1]) == ($tcolor->green & 0xFF) &&
ord($localColorTable[3 * $j + 2]) == ($tcolor->blue & 0xFF)
) {
$localGCE->seek(3); // Enable Transparent Color Flag
$localGCE->writeBytes([($frame->getDisposalMethod() << 2) + 1]);
$localGCE->seek(6); // Set Transparent Color Index
$localGCE->writeBytes([$j]);
break;
}
}
}
switch (ord($imgData[0])) {
case 0x21: // Graphic Control Extension, This block is OPTIONAL
$gce = substr($imgData, 0, 8);
$gcePackedFields = $gce[3];
$gceTransparencyFlag = (ord($gcePackedFields) & 0x01) == 0x01;
$gceTransparencyIndex = ord($gce[6]);
$imgDescriptor = substr($imgData, 8, 10);
$imgData = substr($imgData, 18);
break;
case 0x2C: // Image Descriptor
$imgDescriptor = substr($imgData, 0, 10);
$imgData = substr($imgData, 10);
break;
default:
throw new \RuntimeException("Unexpected input.");
break;
}
$this->output->writeString($localGCE->getContents());
$applyLocalColorTable =
$localPackedFields & 0x80
&& $this->hasHeader
&& ($this->globalColorTableSize != $localColorTableSize || !$this->blockCompare($localColorTable));
if ($applyLocalColorTable) {
$byte = ord($imgDescriptor[9]);
$byte |= 0x80;
$byte &= 0xF8;
$byte |= (ord($this->firstFrameBytes[10]) & 0x07);
$imgDescriptor[9] = chr($byte);
$this->output->writeString($imgDescriptor.$localColorTable);
} else {
$this->output->writeString($imgDescriptor);
}
$this->output->writeString($imgData);
$this->frameIndex++;
}
/**
*
*/
public function addFooter()
{
$this->output->writeString(";");
}
/**
* @param $localBlock
* @return bool
*/
protected function blockCompare($localBlock)
{
$globalBlock = $this->globalColorTable;
$len = $this->globalColorTableSize;
for ($i = 0; $i < $len; $i++) {
if (
$globalBlock[$i] != $localBlock[$i]
) {
return false;
}
}
return true;
}
/**
* @param $int
* @return string
*/
protected function convertUnsignedShort($int)
{
return chr($int & 0xFF).chr(($int >> 8) & 0xFF);
}
/**
* @return MemoryStream
*/
public function getStream()
{
return $this->output;
}
}