-
Notifications
You must be signed in to change notification settings - Fork 19
/
gifenc.js
845 lines (836 loc) · 22.1 KB
/
gifenc.js
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
var __defProp = Object.defineProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, {get: all[name], enumerable: true});
};
// src/index.js
__markAsModule(exports);
__export(exports, {
GIFEncoder: () => GIFEncoder,
applyPalette: () => applyPalette,
default: () => src_default,
nearestColor: () => nearestColor,
nearestColorIndex: () => nearestColorIndex,
nearestColorIndexWithDistance: () => nearestColorIndexWithDistance,
prequantize: () => prequantize,
quantize: () => quantize,
snapColorsToPalette: () => snapColorsToPalette
});
// src/constants.js
var constants_default = {
signature: "GIF",
version: "89a",
trailer: 59,
extensionIntroducer: 33,
applicationExtensionLabel: 255,
graphicControlExtensionLabel: 249,
imageSeparator: 44,
signatureSize: 3,
versionSize: 3,
globalColorTableFlagMask: 128,
colorResolutionMask: 112,
sortFlagMask: 8,
globalColorTableSizeMask: 7,
applicationIdentifierSize: 8,
applicationAuthCodeSize: 3,
disposalMethodMask: 28,
userInputFlagMask: 2,
transparentColorFlagMask: 1,
localColorTableFlagMask: 128,
interlaceFlagMask: 64,
idSortFlagMask: 32,
localColorTableSizeMask: 7
};
// src/stream.js
function createStream(initialCapacity = 256) {
let cursor = 0;
let contents = new Uint8Array(initialCapacity);
return {
get buffer() {
return contents.buffer;
},
reset() {
cursor = 0;
},
bytesView() {
return contents.subarray(0, cursor);
},
bytes() {
return contents.slice(0, cursor);
},
writeByte(byte) {
expand(cursor + 1);
contents[cursor] = byte;
cursor++;
},
writeBytes(data, offset = 0, byteLength = data.length) {
expand(cursor + byteLength);
for (let i = 0; i < byteLength; i++) {
contents[cursor++] = data[i + offset];
}
},
writeBytesView(data, offset = 0, byteLength = data.byteLength) {
expand(cursor + byteLength);
contents.set(data.subarray(offset, offset + byteLength), cursor);
cursor += byteLength;
}
};
function expand(newCapacity) {
var prevCapacity = contents.length;
if (prevCapacity >= newCapacity)
return;
var CAPACITY_DOUBLING_MAX = 1024 * 1024;
newCapacity = Math.max(newCapacity, prevCapacity * (prevCapacity < CAPACITY_DOUBLING_MAX ? 2 : 1.125) >>> 0);
if (prevCapacity != 0)
newCapacity = Math.max(newCapacity, 256);
const oldContents = contents;
contents = new Uint8Array(newCapacity);
if (cursor > 0)
contents.set(oldContents.subarray(0, cursor), 0);
}
}
// src/lzwEncode.js
var BITS = 12;
var DEFAULT_HSIZE = 5003;
var MASKS = [
0,
1,
3,
7,
15,
31,
63,
127,
255,
511,
1023,
2047,
4095,
8191,
16383,
32767,
65535
];
function lzwEncode(width, height, pixels, colorDepth, outStream, accum, htab, codetab) {
outStream = outStream || createStream(512);
accum = accum || new Uint8Array(256);
htab = htab || new Int32Array(DEFAULT_HSIZE);
codetab = codetab || new Int32Array(DEFAULT_HSIZE);
const hsize = htab.length;
const initCodeSize = Math.max(2, colorDepth);
accum.fill(0);
codetab.fill(0);
clear_hash();
let cur_accum = 0;
let cur_bits = 0;
const init_bits = initCodeSize + 1;
const g_init_bits = init_bits;
let clear_flg = false;
let n_bits = g_init_bits;
let maxcode = MAXCODE(n_bits);
const ClearCode = 1 << init_bits - 1;
const EOFCode = ClearCode + 1;
let free_ent = ClearCode + 2;
let a_count = 0;
let ent = pixels[0];
let hshift = 0;
for (let fcode = hsize; fcode < 65536; fcode *= 2) {
++hshift;
}
hshift = 8 - hshift;
outStream.writeByte(initCodeSize);
output(ClearCode);
for (let idx = 1; idx < pixels.length; idx++) {
inner(pixels[idx]);
}
output(ent);
output(EOFCode);
outStream.writeByte(0);
return outStream.bytesView();
function char_out(c) {
accum[a_count++] = c;
if (a_count >= 254)
flush_char();
}
function clear_block() {
clear_hash();
free_ent = ClearCode + 2;
clear_flg = true;
output(ClearCode);
}
function clear_hash() {
htab.fill(-1);
}
function inner(c) {
const fcode = (c << BITS) + ent;
let i = c << hshift ^ ent;
if (htab[i] === fcode) {
ent = codetab[i];
} else {
if (htab[i] >= 0) {
const disp = i === 0 ? 1 : hsize - i;
do {
i -= disp;
if (i < 0)
i += hsize;
if (htab[i] === fcode) {
ent = codetab[i];
return;
}
} while (htab[i] >= 0);
}
output(ent);
ent = c;
if (free_ent < 1 << BITS) {
codetab[i] = free_ent++;
htab[i] = fcode;
} else {
clear_block();
}
}
}
function flush_char() {
if (a_count > 0) {
outStream.writeByte(a_count);
outStream.writeBytesView(accum, 0, a_count);
a_count = 0;
}
}
function MAXCODE(n_bits2) {
return (1 << n_bits2) - 1;
}
function output(code) {
cur_accum &= MASKS[cur_bits];
if (cur_bits > 0)
cur_accum |= code << cur_bits;
else
cur_accum = code;
cur_bits += n_bits;
while (cur_bits >= 8) {
char_out(cur_accum & 255);
cur_accum >>= 8;
cur_bits -= 8;
}
if (free_ent > maxcode || clear_flg) {
if (clear_flg) {
n_bits = g_init_bits;
maxcode = MAXCODE(n_bits);
clear_flg = false;
} else {
++n_bits;
if (n_bits == BITS)
maxcode = 1 << BITS;
else
maxcode = MAXCODE(n_bits);
}
}
if (code == EOFCode) {
while (cur_bits > 0) {
char_out(cur_accum & 255);
cur_accum >>= 8;
cur_bits -= 8;
}
flush_char();
}
}
}
var lzwEncode_default = lzwEncode;
// src/rgb-packing.js
function rgb888_to_rgb565(r, g, b) {
return r << 8 & 63488 | g << 2 & 992 | b >> 3;
}
function rgba8888_to_rgba4444(r, g, b, a) {
return r >> 4 | g & 240 | (b & 240) << 4 | (a & 240) << 8;
}
function rgb888_to_rgb444(r, g, b) {
return r >> 4 << 8 | g & 240 | b >> 4;
}
// src/pnnquant2.js
function clamp(value, min, max) {
return value < min ? min : value > max ? max : value;
}
function sqr(value) {
return value * value;
}
function find_nn(bins, idx, hasAlpha) {
var nn = 0;
var err = 1e100;
var bin1 = bins[idx];
var n1 = bin1.cnt;
var wa = bin1.ac;
var wr = bin1.rc;
var wg = bin1.gc;
var wb = bin1.bc;
for (var i = bin1.fw; i != 0; i = bins[i].fw) {
var n2 = bins[i].cnt, nerr2 = n1 * n2 / (n1 + n2);
if (nerr2 >= err)
continue;
var nerr = 0;
if (hasAlpha) {
nerr += nerr2 * sqr(bins[i].ac - wa);
if (nerr >= err)
continue;
}
nerr += nerr2 * sqr(bins[i].rc - wr);
if (nerr >= err)
continue;
nerr += nerr2 * sqr(bins[i].gc - wg);
if (nerr >= err)
continue;
nerr += nerr2 * sqr(bins[i].bc - wb);
if (nerr >= err)
continue;
err = nerr;
nn = i;
}
bin1.err = err;
bin1.nn = nn;
}
function create_bin() {
return {
ac: 0,
rc: 0,
gc: 0,
bc: 0,
cnt: 0,
nn: 0,
fw: 0,
bk: 0,
tm: 0,
mtm: 0,
err: 0
};
}
function create_bin_list(data, format) {
const bincount = format === "rgb444" ? 4096 : 65536;
const bins = new Array(bincount);
const size = data.length;
if (format === "rgba4444") {
for (let i = 0; i < size; ++i) {
const color = data[i];
const a = color >> 24 & 255;
const b = color >> 16 & 255;
const g = color >> 8 & 255;
const r = color & 255;
const index = rgba8888_to_rgba4444(r, g, b, a);
let bin = index in bins ? bins[index] : bins[index] = create_bin();
bin.rc += r;
bin.gc += g;
bin.bc += b;
bin.ac += a;
bin.cnt++;
}
} else if (format === "rgb444") {
for (let i = 0; i < size; ++i) {
const color = data[i];
const b = color >> 16 & 255;
const g = color >> 8 & 255;
const r = color & 255;
const index = rgb888_to_rgb444(r, g, b);
let bin = index in bins ? bins[index] : bins[index] = create_bin();
bin.rc += r;
bin.gc += g;
bin.bc += b;
bin.cnt++;
}
} else {
for (let i = 0; i < size; ++i) {
const color = data[i];
const b = color >> 16 & 255;
const g = color >> 8 & 255;
const r = color & 255;
const index = rgb888_to_rgb565(r, g, b);
let bin = index in bins ? bins[index] : bins[index] = create_bin();
bin.rc += r;
bin.gc += g;
bin.bc += b;
bin.cnt++;
}
}
return bins;
}
function quantize(rgba, maxColors, opts) {
const {
format = "rgb565",
clearAlpha = true,
clearAlphaColor = 0,
clearAlphaThreshold = 0,
oneBitAlpha = false
} = opts || {};
if (!rgba || !rgba.buffer) {
throw new Error("quantize() expected RGBA Uint8Array data");
}
if (!(rgba instanceof Uint8Array) && !(rgba instanceof Uint8ClampedArray)) {
throw new Error("quantize() expected RGBA Uint8Array data");
}
const data = new Uint32Array(rgba.buffer);
let useSqrt = opts.useSqrt !== false;
const hasAlpha = format === "rgba4444";
const bins = create_bin_list(data, format);
const bincount = bins.length;
const bincountMinusOne = bincount - 1;
const heap = new Uint32Array(bincount + 1);
var maxbins = 0;
for (var i = 0; i < bins.length; ++i) {
const bin = bins[i];
if (bin != null) {
var d = 1 / bin.cnt;
if (hasAlpha)
bin.ac *= d;
bin.rc *= d;
bin.gc *= d;
bin.bc *= d;
bins[maxbins++] = bin;
}
}
if (sqr(maxColors) / maxbins < 0.022) {
useSqrt = false;
}
var i = 0;
for (; i < maxbins - 1; ++i) {
bins[i].fw = i + 1;
bins[i + 1].bk = i;
if (useSqrt)
bins[i].cnt = Math.sqrt(bins[i].cnt);
}
if (useSqrt)
bins[i].cnt = Math.sqrt(bins[i].cnt);
var h, l, l2;
for (i = 0; i < maxbins; ++i) {
find_nn(bins, i, false);
var err = bins[i].err;
for (l = ++heap[0]; l > 1; l = l2) {
l2 = l >> 1;
if (bins[h = heap[l2]].err <= err)
break;
heap[l] = h;
}
heap[l] = i;
}
var extbins = maxbins - maxColors;
for (i = 0; i < extbins; ) {
var tb;
for (; ; ) {
var b1 = heap[1];
tb = bins[b1];
if (tb.tm >= tb.mtm && bins[tb.nn].mtm <= tb.tm)
break;
if (tb.mtm == bincountMinusOne)
b1 = heap[1] = heap[heap[0]--];
else {
find_nn(bins, b1, false);
tb.tm = i;
}
var err = bins[b1].err;
for (l = 1; (l2 = l + l) <= heap[0]; l = l2) {
if (l2 < heap[0] && bins[heap[l2]].err > bins[heap[l2 + 1]].err)
l2++;
if (err <= bins[h = heap[l2]].err)
break;
heap[l] = h;
}
heap[l] = b1;
}
var nb = bins[tb.nn];
var n1 = tb.cnt;
var n2 = nb.cnt;
var d = 1 / (n1 + n2);
if (hasAlpha)
tb.ac = d * (n1 * tb.ac + n2 * nb.ac);
tb.rc = d * (n1 * tb.rc + n2 * nb.rc);
tb.gc = d * (n1 * tb.gc + n2 * nb.gc);
tb.bc = d * (n1 * tb.bc + n2 * nb.bc);
tb.cnt += nb.cnt;
tb.mtm = ++i;
bins[nb.bk].fw = nb.fw;
bins[nb.fw].bk = nb.bk;
nb.mtm = bincountMinusOne;
}
let palette = [];
var k = 0;
for (i = 0; ; ++k) {
let r = clamp(Math.round(bins[i].rc), 0, 255);
let g = clamp(Math.round(bins[i].gc), 0, 255);
let b = clamp(Math.round(bins[i].bc), 0, 255);
let a = 255;
if (hasAlpha) {
a = clamp(Math.round(bins[i].ac), 0, 255);
if (oneBitAlpha) {
const threshold = typeof oneBitAlpha === "number" ? oneBitAlpha : 127;
a = a <= threshold ? 0 : 255;
}
if (clearAlpha && a <= clearAlphaThreshold) {
r = g = b = clearAlphaColor;
a = 0;
}
}
const color = hasAlpha ? [r, g, b, a] : [r, g, b];
const exists = existsInPalette(palette, color);
if (!exists)
palette.push(color);
if ((i = bins[i].fw) == 0)
break;
}
return palette;
}
function existsInPalette(palette, color) {
for (let i = 0; i < palette.length; i++) {
const p = palette[i];
let matchesRGB = p[0] === color[0] && p[1] === color[1] && p[2] === color[2];
let matchesAlpha = p.length >= 4 && color.length >= 4 ? p[3] === color[3] : true;
if (matchesRGB && matchesAlpha)
return true;
}
return false;
}
// src/color.js
function euclideanDistanceSquared(a, b) {
var sum = 0;
var n;
for (n = 0; n < a.length; n++) {
const dx = a[n] - b[n];
sum += dx * dx;
}
return sum;
}
// src/palettize.js
function roundStep(byte, step) {
return step > 1 ? Math.round(byte / step) * step : byte;
}
function prequantize(data, {roundRGB = 5, roundAlpha = 10, oneBitAlpha = null} = {}) {
for (let i = 0; i < data.length; i++) {
const color = data[i];
let a = color >> 24 & 255;
let b = color >> 16 & 255;
let g = color >> 8 & 255;
let r = color & 255;
a = roundStep(a, roundAlpha);
if (oneBitAlpha) {
const threshold = typeof oneBitAlpha === "number" ? oneBitAlpha : 127;
a = a <= threshold ? 0 : 255;
}
r = roundStep(r, roundRGB);
g = roundStep(g, roundRGB);
b = roundStep(b, roundRGB);
data[i] = a << 24 | b << 16 | g << 8 | r << 0;
}
}
function applyPalette(rgba, palette, format = "rgb565") {
if (!rgba || !rgba.buffer) {
throw new Error("quantize() expected RGBA Uint8Array data");
}
if (!(rgba instanceof Uint8Array) && !(rgba instanceof Uint8ClampedArray)) {
throw new Error("quantize() expected RGBA Uint8Array data");
}
const data = new Uint32Array(rgba.buffer);
const bincount = format === "rgb444" ? 4096 : 65536;
const index = new Uint8Array(data.length);
const cache = new Array(bincount);
const hasAlpha = format === "rgba4444";
if (format === "rgba4444") {
for (let i = 0; i < data.length; i++) {
const color = data[i];
const a = color >> 24 & 255;
const b = color >> 16 & 255;
const g = color >> 8 & 255;
const r = color & 255;
const key = rgba8888_to_rgba4444(r, g, b, a);
let idx;
if (cache[key] != null) {
idx = cache[key];
} else {
idx = nearestColorIndexRGBA(r, g, b, a, palette);
cache[key] = idx;
}
index[i] = idx;
}
} else {
const rgb888_to_key = format === "rgb444" ? rgb888_to_rgb444 : rgb888_to_rgb565;
for (let i = 0; i < data.length; i++) {
const color = data[i];
const b = color >> 16 & 255;
const g = color >> 8 & 255;
const r = color & 255;
const key = rgb888_to_key(r, g, b);
let idx;
if (cache[key] != null) {
idx = cache[key];
} else {
idx = nearestColorIndexRGB(r, g, b, palette);
cache[key] = idx;
}
index[i] = idx;
}
}
return index;
}
function nearestColorIndexRGBA(r, g, b, a, palette) {
let k = 0;
let mindist = 1e100;
for (let i = 0; i < palette.length; i++) {
const px2 = palette[i];
const a2 = px2[3];
let curdist = sqr2(a2 - a);
if (curdist > mindist)
continue;
const r2 = px2[0];
curdist += sqr2(r2 - r);
if (curdist > mindist)
continue;
const g2 = px2[1];
curdist += sqr2(g2 - g);
if (curdist > mindist)
continue;
const b2 = px2[2];
curdist += sqr2(b2 - b);
if (curdist > mindist)
continue;
mindist = curdist;
k = i;
}
return k;
}
function nearestColorIndexRGB(r, g, b, palette) {
let k = 0;
let mindist = 1e100;
for (let i = 0; i < palette.length; i++) {
const px2 = palette[i];
const r2 = px2[0];
let curdist = sqr2(r2 - r);
if (curdist > mindist)
continue;
const g2 = px2[1];
curdist += sqr2(g2 - g);
if (curdist > mindist)
continue;
const b2 = px2[2];
curdist += sqr2(b2 - b);
if (curdist > mindist)
continue;
mindist = curdist;
k = i;
}
return k;
}
function snapColorsToPalette(palette, knownColors, threshold = 5) {
if (!palette.length || !knownColors.length)
return;
const paletteRGB = palette.map((p) => p.slice(0, 3));
const thresholdSq = threshold * threshold;
const dim = palette[0].length;
for (let i = 0; i < knownColors.length; i++) {
let color = knownColors[i];
if (color.length < dim) {
color = [color[0], color[1], color[2], 255];
} else if (color.length > dim) {
color = color.slice(0, 3);
} else {
color = color.slice();
}
const r = nearestColorIndexWithDistance(paletteRGB, color.slice(0, 3), euclideanDistanceSquared);
const idx = r[0];
const distanceSq = r[1];
if (distanceSq > 0 && distanceSq <= thresholdSq) {
palette[idx] = color;
}
}
}
function sqr2(a) {
return a * a;
}
function nearestColorIndex(colors, pixel, distanceFn = euclideanDistanceSquared) {
let minDist = Infinity;
let minDistIndex = -1;
for (let j = 0; j < colors.length; j++) {
const paletteColor = colors[j];
const dist = distanceFn(pixel, paletteColor);
if (dist < minDist) {
minDist = dist;
minDistIndex = j;
}
}
return minDistIndex;
}
function nearestColorIndexWithDistance(colors, pixel, distanceFn = euclideanDistanceSquared) {
let minDist = Infinity;
let minDistIndex = -1;
for (let j = 0; j < colors.length; j++) {
const paletteColor = colors[j];
const dist = distanceFn(pixel, paletteColor);
if (dist < minDist) {
minDist = dist;
minDistIndex = j;
}
}
return [minDistIndex, minDist];
}
function nearestColor(colors, pixel, distanceFn = euclideanDistanceSquared) {
return colors[nearestColorIndex(colors, pixel, distanceFn)];
}
// src/index.js
function GIFEncoder(opt = {}) {
const {initialCapacity = 4096, auto = true} = opt;
const stream = createStream(initialCapacity);
const HSIZE = 5003;
const accum = new Uint8Array(256);
const htab = new Int32Array(HSIZE);
const codetab = new Int32Array(HSIZE);
let hasInit = false;
return {
reset() {
stream.reset();
hasInit = false;
},
finish() {
stream.writeByte(constants_default.trailer);
},
bytes() {
return stream.bytes();
},
bytesView() {
return stream.bytesView();
},
get buffer() {
return stream.buffer;
},
get stream() {
return stream;
},
writeHeader,
writeFrame(index, width, height, opts = {}) {
const {
transparent = false,
transparentIndex = 0,
delay = 0,
palette = null,
repeat = 0,
colorDepth = 8,
dispose = -1
} = opts;
let first = false;
if (auto) {
if (!hasInit) {
first = true;
writeHeader();
hasInit = true;
}
} else {
first = Boolean(opts.first);
}
width = Math.max(0, Math.floor(width));
height = Math.max(0, Math.floor(height));
if (first) {
if (!palette) {
throw new Error("First frame must include a { palette } option");
}
encodeLogicalScreenDescriptor(stream, width, height, palette, colorDepth);
encodeColorTable(stream, palette);
if (repeat >= 0) {
encodeNetscapeExt(stream, repeat);
}
}
const delayTime = Math.round(delay / 10);
encodeGraphicControlExt(stream, dispose, delayTime, transparent, transparentIndex);
const useLocalColorTable = Boolean(palette) && !first;
encodeImageDescriptor(stream, width, height, useLocalColorTable ? palette : null);
if (useLocalColorTable)
encodeColorTable(stream, palette);
encodePixels(stream, index, width, height, colorDepth, accum, htab, codetab);
}
};
function writeHeader() {
writeUTFBytes(stream, "GIF89a");
}
}
function encodeGraphicControlExt(stream, dispose, delay, transparent, transparentIndex) {
stream.writeByte(33);
stream.writeByte(249);
stream.writeByte(4);
if (transparentIndex < 0) {
transparentIndex = 0;
transparent = false;
}
var transp, disp;
if (!transparent) {
transp = 0;
disp = 0;
} else {
transp = 1;
disp = 2;
}
if (dispose >= 0) {
disp = dispose & 7;
}
disp <<= 2;
const userInput = 0;
stream.writeByte(0 | disp | userInput | transp);
writeUInt16(stream, delay);
stream.writeByte(transparentIndex || 0);
stream.writeByte(0);
}
function encodeLogicalScreenDescriptor(stream, width, height, palette, colorDepth = 8) {
const globalColorTableFlag = 1;
const sortFlag = 0;
const globalColorTableSize = colorTableSize(palette.length) - 1;
const fields = globalColorTableFlag << 7 | colorDepth - 1 << 4 | sortFlag << 3 | globalColorTableSize;
const backgroundColorIndex = 0;
const pixelAspectRatio = 0;
writeUInt16(stream, width);
writeUInt16(stream, height);
stream.writeBytes([fields, backgroundColorIndex, pixelAspectRatio]);
}
function encodeNetscapeExt(stream, repeat) {
stream.writeByte(33);
stream.writeByte(255);
stream.writeByte(11);
writeUTFBytes(stream, "NETSCAPE2.0");
stream.writeByte(3);
stream.writeByte(1);
writeUInt16(stream, repeat);
stream.writeByte(0);
}
function encodeColorTable(stream, palette) {
const colorTableLength = 1 << colorTableSize(palette.length);
for (let i = 0; i < colorTableLength; i++) {
let color = [0, 0, 0];
if (i < palette.length) {
color = palette[i];
}
stream.writeByte(color[0]);
stream.writeByte(color[1]);
stream.writeByte(color[2]);
}
}
function encodeImageDescriptor(stream, width, height, localPalette) {
stream.writeByte(44);
writeUInt16(stream, 0);
writeUInt16(stream, 0);
writeUInt16(stream, width);
writeUInt16(stream, height);
if (localPalette) {
const interlace = 0;
const sorted = 0;
const palSize = colorTableSize(localPalette.length) - 1;
stream.writeByte(128 | interlace | sorted | 0 | palSize);
} else {
stream.writeByte(0);
}
}
function encodePixels(stream, index, width, height, colorDepth = 8, accum, htab, codetab) {
lzwEncode_default(width, height, index, colorDepth, stream, accum, htab, codetab);
}
function writeUInt16(stream, short) {
stream.writeByte(short & 255);
stream.writeByte(short >> 8 & 255);
}
function writeUTFBytes(stream, text) {
for (var i = 0; i < text.length; i++) {
stream.writeByte(text.charCodeAt(i));
}
}
function colorTableSize(length) {
return Math.max(Math.ceil(Math.log2(length)), 1);
}
var src_default = GIFEncoder;
//# sourceMappingURL=gifenc.js.map