gifframe.js
5.02 KB
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
'use strict';
const BitmapImage = require('./bitmapimage');
const { GifError } = require('./gif');
/** @class GifFrame */
class GifFrame extends BitmapImage {
// xOffset - x offset of bitmap on GIF (defaults to 0)
// yOffset - y offset of bitmap on GIF (defaults to 0)
// disposalMethod - pixel disposal method when handling partial images
// delayCentisecs - duration of frame in hundredths of a second
// interlaced - whether the image is interlaced (defaults to false)
/**
* GifFrame is a class representing an image frame of a GIF. GIFs contain one or more instances of GifFrame.
*
* Property | Description
* --- | ---
* xOffset | x-coord of position within GIF at which to render the image (defaults to 0)
* yOffset | y-coord of position within GIF at which to render the image (defaults to 0)
* disposalMethod | GIF disposal method; only relevant when the frames aren't all the same size (defaults to 2, disposing to background color)
* delayCentisecs | duration of the frame in hundreths of a second
* interlaced | boolean indicating whether the frame renders interlaced
*
* Its constructor supports the following signatures:
*
* * new GifFrame(bitmap: {width: number, height: number, data: Buffer}, options?)
* * new GifFrame(bitmapImage: BitmapImage, options?)
* * new GifFrame(width: number, height: number, buffer: Buffer, options?)
* * new GifFrame(width: number, height: number, backgroundRGBA?: number, options?)
* * new GifFrame(frame: GifFrame)
*
* See the base class BitmapImage for a discussion of all parameters but `options` and `frame`. `options` is an optional argument providing initial values for the above-listed GifFrame properties. Each property within option is itself optional.
*
* Provide a `frame` to the constructor to create a clone of the provided frame. The new frame includes a copy of the provided frame's pixel data so that each can subsequently be modified without affecting each other.
*/
constructor(...args) {
super(...args);
if (args[0] instanceof GifFrame) {
// copy a provided GifFrame
const source = args[0];
this.xOffset = source.xOffset;
this.yOffset = source.yOffset;
this.disposalMethod = source.disposalMethod;
this.delayCentisecs = source.delayCentisecs;
this.interlaced = source.interlaced;
}
else {
const lastArg = args[args.length - 1];
let options = {};
if (typeof lastArg === 'object' && !(lastArg instanceof BitmapImage)) {
options = lastArg;
}
this.xOffset = options.xOffset || 0;
this.yOffset = options.yOffset || 0;
this.disposalMethod = (options.disposalMethod !== undefined ?
options.disposalMethod : GifFrame.DisposeToBackgroundColor);
this.delayCentisecs = options.delayCentisecs || 8;
this.interlaced = options.interlaced || false;
}
}
/**
* Get a summary of the colors found within the frame. The return value is an object of the following form:
*
* Property | Description
* --- | ---
* colors | An array of all the opaque colors found within the frame. Each color is given as an RGB number of the form 0xRRGGBB. The array is sorted by increasing number. Will be an empty array when the image is completely transparent.
* usesTransparency | boolean indicating whether there are any transparent pixels within the frame. A pixel is considered transparent if its alpha value is 0x00.
* indexCount | The number of color indexes required to represent this palette of colors. It is equal to the number of opaque colors plus one if the image includes transparency.
*
* @return {object} An object representing a color palette as described above.
*/
getPalette() {
// returns with colors sorted low to high
const colorSet = new Set();
const buf = this.bitmap.data;
let i = 0;
let usesTransparency = false;
while (i < buf.length) {
if (buf[i + 3] === 0) {
usesTransparency = true;
}
else {
// can eliminate the bitshift by starting one byte prior
const color = (buf.readUInt32BE(i, true) >> 8) & 0xFFFFFF;
colorSet.add(color);
}
i += 4; // skip alpha
}
const colors = new Array(colorSet.size);
const iter = colorSet.values();
for (i = 0; i < colors.length; ++i) {
colors[i] = iter.next().value;
}
colors.sort((a, b) => (a - b));
let indexCount = colors.length;
if (usesTransparency) {
++indexCount;
}
return { colors, usesTransparency, indexCount };
}
}
GifFrame.DisposeToAnything = 0;
GifFrame.DisposeNothing = 1;
GifFrame.DisposeToBackgroundColor = 2;
GifFrame.DisposeToPrevious = 3;
exports.GifFrame = GifFrame;