-
Notifications
You must be signed in to change notification settings - Fork 59
/
Tools.hx
390 lines (359 loc) · 12.9 KB
/
Tools.hx
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package format.gif;
import format.gif.Data;
import haxe.io.Bytes;
import haxe.io.BytesData;
/**
* Tools for gif data.
* @author Yanrishatum
*/
class Tools
{
/**
* Returns amount of frames in Gif data.
*/
public static function framesCount(data:Data):Int
{
var frames:Int = 0;
for (block in data.blocks)
{
switch(block)
{
case Block.BFrame(_):
frames++;
default :
}
}
return frames;
}
/**
* Returns frame at given index.
* @param data Gif data.
* @param frameIndex Index of frame.
* @return Frame at given index or null, if there is no frame at that index.
*/
public static function frame(data:Data, frameIndex:Int):Frame
{
var counter:Int = 0;
for (block in data.blocks)
{
switch (block)
{
case Block.BFrame(frame):
if (counter == frameIndex) return frame;
counter++;
default :
}
}
return null;
}
/**
* Returns Graphic Control extension for frame at given index.
* @param data Gif data.
* @param frameIndex Index of frame.
* @return GCE extension if it is exists for given frame, null otherwise.
*/
public static function graphicControl(data:Data, frameIndex:Int):GraphicControlExtension
{
var counter:Int = 0;
var gce:GraphicControlExtension = null;
for (block in data.blocks)
{
switch (block)
{
case Block.BFrame(frame):
if (counter == frameIndex) return gce;
gce = null;
counter++;
case Block.BExtension(Extension.EGraphicControl(g)):
gce = g;
default :
}
}
return null;
}
//==========================================================
// Extracting.
//==========================================================
/**
* Extracts frame pixel data in Blue-Green-Red-Alpha pixel format.
* This function extracts only exact frame and does put previous frame pixel data into resulting Bytes. Note that frame size may not equal to Gif logical screen size.
* @param data Gif data.
* @param frameIndex Frame index.
* @return BGRA pixel data with dimensions equals to specified Frame size. If frame does not present in Gif data returns null.
*/
public static function extractBGRA(data:Data, frameIndex:Int):Bytes
{
var gce:GraphicControlExtension = null;
var frameCaret:Int = 0;
for (block in data.blocks)
{
switch (block)
{
case Block.BExtension(ext):
switch(ext)
{
case Extension.EGraphicControl(g):
gce = g;
default:
}
case Block.BFrame(frame):
if (frameCaret == frameIndex)
{
var bytes:Bytes = Bytes.alloc(frame.width * frame.height * 4);
var ct:Bytes = frame.localColorTable ? frame.colorTable : data.globalColorTable;
if (ct == null) throw "Frame does not have a color table!";
var transparentIndex:Int = gce != null && gce.hasTransparentColor ? gce.transparentIndex * 3 : -1;
var writeCaret:Int = 0;
for (i in 0...frame.pixels.length)
{
var index:Int = frame.pixels.get(i) * 3;
bytes.set(writeCaret , ct.get(index + 2)); // B
bytes.set(writeCaret + 1, ct.get(index + 1)); // G
bytes.set(writeCaret + 2, ct.get(index )); // R
if (transparentIndex == index) bytes.set(writeCaret + 3, 0); // A = 0
else bytes.set(writeCaret + 3, 0xFF); // A = FF
writeCaret += 4;
}
return bytes;
}
frameCaret++;
gce = null;
default:
}
}
return null;
}
/**
* Extracts frame pixel data in Red-Green-Blue-Alpha pixel format.
* This function extracts only exact frame and does put previous frame pixel data into resulting Bytes. Note that frame size may not equal to Gif logical screen size.
* @param data Gif data.
* @param frameIndex Frame index.
* @return RGBA pixel data with dimensions equals to specified Frame size. If frame does not present in Gif data returns null.
*/
public static function extractRGBA(data:Data, frameIndex:Int):Bytes
{
var gce:GraphicControlExtension = null;
var frameCaret:Int = 0;
for (block in data.blocks)
{
switch (block)
{
case Block.BExtension(ext):
switch(ext)
{
case Extension.EGraphicControl(g):
gce = g;
default:
}
case Block.BFrame(frame):
if (frameCaret == frameIndex)
{
var bytes:Bytes = Bytes.alloc(frame.width * frame.height * 4);
var ct:Bytes = frame.localColorTable ? frame.colorTable : data.globalColorTable;
if (ct == null) throw "Frame does not have a color table!";
var transparentIndex:Int = gce != null && gce.hasTransparentColor ? gce.transparentIndex * 3 : -1;
var writeCaret:Int = 0;
for (i in 0...frame.pixels.length)
{
var index:Int = frame.pixels.get(i) * 3;
bytes.set(writeCaret , ct.get(index )); // R
bytes.set(writeCaret + 1, ct.get(index + 1)); // G
bytes.set(writeCaret + 2, ct.get(index + 2)); // B
if (transparentIndex == index) bytes.set(writeCaret + 3, 0); // A = 0
else bytes.set(writeCaret + 3, 0xFF); // A = FF
writeCaret += 4;
}
return bytes;
}
frameCaret++;
gce = null;
default:
}
}
return null;
}
/**
* Extracts full Gif pixel data to specified frame in Blue-Green-Red-Alpha pixel format.
* This functions returns full representation of frame including rendering of all other frames before.
* @param data Gif data.
* @param frameIndex Frame index.
* @return BGRA pixel data with dimensions equals to Gif logical screen with full pixel data of Gif image at specified frame.
*/
public static function extractFullBGRA(data:Data, frameIndex:Int):Bytes
{
var gce:GraphicControlExtension = null;
var frameCaret:Int = 0;
var bytes:Bytes = Bytes.alloc(data.logicalScreenDescriptor.width* data.logicalScreenDescriptor.height * 4);
for (block in data.blocks)
{
switch (block)
{
case Block.BExtension(ext):
switch(ext)
{
case Extension.EGraphicControl(g):
gce = g;
default:
}
case Block.BFrame(frame):
var ct:Bytes = frame.localColorTable ? frame.colorTable : data.globalColorTable;
if (ct == null) throw "Frame does not have a color table!";
var transparentIndex:Int = gce != null && gce.hasTransparentColor ? gce.transparentIndex * 3 : -1;
var pixels:Bytes = frame.pixels;
var x:Int = 0;
var writeCaret:Int = (frame.y * data.logicalScreenDescriptor.width + frame.x) * 4;
var lineSkip:Int = (data.logicalScreenDescriptor.width - frame.width) * 4 + 4;
var disposalMethod:DisposalMethod = frameCaret != frameIndex && gce != null ? gce.disposalMethod : DisposalMethod.NO_ACTION;
switch (disposalMethod)
{
case DisposalMethod.RENDER_PREVIOUS:
// Do not render frame at all
case DisposalMethod.FILL_BACKGROUND:
for (i in 0...pixels.length)
{
bytes.set(writeCaret , 0); // B
bytes.set(writeCaret + 1, 0); // G
bytes.set(writeCaret + 2, 0); // R
bytes.set(writeCaret + 3, 0); // A
if (++x == frame.width)
{
x = 0;
writeCaret += lineSkip;
}
else writeCaret += 4;
}
default:
for (i in 0...pixels.length)
{
var index:Int = pixels.get(i) * 3;
if (transparentIndex != index) // Render only if pixel non-transparent
{
bytes.set(writeCaret , ct.get(index + 2)); // B
bytes.set(writeCaret + 1, ct.get(index + 1)); // G
bytes.set(writeCaret + 2, ct.get(index )); // R
bytes.set(writeCaret + 3, 0xFF); // A
}
if (++x == frame.width)
{
x = 0;
writeCaret += lineSkip;
}
else writeCaret += 4;
}
}
if (frameCaret == frameIndex) return bytes;
frameCaret++;
gce = null;
default:
}
}
return bytes;
}
/**
* Extracts full Gif pixel data to specified frame in Red-Green-Blue-Alpha pixel format.
* This functions returns full representation of frame including rendering of all other frames before.
* @param data Gif data.
* @param frameIndex Frame index.
* @return RGBA pixel data with dimensions equals to Gif logical screen with full pixel data of Gif image at specified frame.
*/
public static function extractFullRGBA(data:Data, frameIndex:Int):Bytes
{
var gce:GraphicControlExtension = null;
var frameCaret:Int = 0;
var bytes:Bytes = Bytes.alloc(data.logicalScreenDescriptor.width* data.logicalScreenDescriptor.height * 4);
for (block in data.blocks)
{
switch (block)
{
case Block.BExtension(ext):
switch(ext)
{
case Extension.EGraphicControl(g):
gce = g;
default:
}
case Block.BFrame(frame):
var ct:Bytes = frame.localColorTable ? frame.colorTable : data.globalColorTable;
if (ct == null) throw "Frame does not have a color table!";
var transparentIndex:Int = gce != null && gce.hasTransparentColor ? gce.transparentIndex * 3 : -1;
var pixels:Bytes = frame.pixels;
var x:Int = 0;
var writeCaret:Int = (frame.y * data.logicalScreenDescriptor.width + frame.x) * 4;
var lineSkip:Int = (data.logicalScreenDescriptor.width - frame.width) * 4 + 4;
var disposalMethod:DisposalMethod = frameCaret != frameIndex && gce != null ? gce.disposalMethod : DisposalMethod.NO_ACTION;
switch (disposalMethod)
{
case DisposalMethod.RENDER_PREVIOUS:
// Do not render frame at all
case DisposalMethod.FILL_BACKGROUND:
for (i in 0...pixels.length)
{
bytes.set(writeCaret , 0); // R
bytes.set(writeCaret + 1, 0); // G
bytes.set(writeCaret + 2, 0); // B
bytes.set(writeCaret + 3, 0); // A
if (++x == frame.width)
{
x = 0;
writeCaret += lineSkip;
}
else writeCaret += 4;
}
default:
for (i in 0...pixels.length)
{
var index:Int = pixels.get(i) * 3;
if (transparentIndex != index) // Render only if pixel non-transparent
{
bytes.set(writeCaret , ct.get(index )); // R
bytes.set(writeCaret + 1, ct.get(index + 1)); // G
bytes.set(writeCaret + 2, ct.get(index + 2)); // B
bytes.set(writeCaret + 3, 0xFF); // A
}
if (++x == frame.width)
{
x = 0;
writeCaret += lineSkip;
}
else writeCaret += 4;
}
}
if (frameCaret == frameIndex) return bytes;
frameCaret++;
gce = null;
default:
}
}
return bytes;
}
/**
* Returns amount of animation repeats stored in Gif data.
* This is link to Netscape Looping application extension. If this extension does not present amount of loops equals to 1.
* @param data Gif data.
* @return Amount of animation repeats. Zero equals to infinite amount of repeats.
*/
public static function loopCount(data:Data):Int
{
for (block in data.blocks)
{
switch(block)
{
case Block.BExtension(Extension.EApplicationExtension(ApplicationExtension.AENetscapeLooping(loops))): return loops;
default :
}
}
return 1;
}
//==========================================================
// In-Dev writer tools.
//==========================================================
//public static function buildFrameFromTrueColor(pixels:Bytes, width:Int, height:Int):Void
//{
//
//}
private static var LN2:Float = Math.log(2);
@:noCompletion public static inline function log2(val:Float):Float
{
return Math.log(val) / LN2;
}
}