-
Notifications
You must be signed in to change notification settings - Fork 1
/
QOI.js
302 lines (253 loc) · 11.3 KB
/
QOI.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
var QOI = {
decode: function(arrayBuffer, byteOffset, byteLength, outputChannels) {
if (typeof byteOffset === 'undefined' || byteOffset === null) {
byteOffset = 0;
}
if (typeof byteLength === 'undefined' || byteLength === null) {
byteLength = arrayBuffer.byteLength - byteOffset;
}
const uint8 = new Uint8Array(arrayBuffer, byteOffset, byteLength);
const magic1 = uint8[0];
const magic2 = uint8[1];
const magic3 = uint8[2];
const magic4 = uint8[3];
const width = ((uint8[4] << 24) | (uint8[5] << 16) | (uint8[6] << 8) | uint8[7]) >>> 0;
const height = ((uint8[8] << 24) | (uint8[9] << 16) | (uint8[10] << 8) | uint8[11]) >>> 0;
const channels = uint8[12];
const colorspace = uint8[13];
if (typeof outputChannels === 'undefined' || outputChannels === null) {
outputChannels = channels;
}
if (magic1 !== 0x71 || magic2 !== 0x6F || magic3 !== 0x69 || magic4 !== 0x66) {
throw new Error('QOI.decode: The signature of the QOI file is invalid');
}
if (channels < 3 || channels > 4) {
throw new Error('QOI.decode: The number of channels declared in the file is invalid');
}
if (colorspace > 1) {
throw new Error('QOI.decode: The colorspace declared in the file is invalid');
}
if (outputChannels < 3 || outputChannels > 4) {
throw new Error('QOI.decode: The number of channels for the output is invalid');
}
const pixelLength = width * height * outputChannels;
const result = new Uint8Array(pixelLength);
let arrayPosition = 14;
const index = new Uint8Array(64 * 4);
let indexPosition = 0;
let red = 0;
let green = 0;
let blue = 0;
let alpha = 255;
const chunksLength = byteLength - 8;
let run = 0;
let pixelPosition = 0;
for (; pixelPosition < pixelLength && arrayPosition < byteLength - 4; pixelPosition += outputChannels) {
if (run > 0) {
run--;
} else if (arrayPosition < chunksLength) {
const byte1 = uint8[arrayPosition++];
if (byte1 === 0b11111110) { // QOI_OP_RGB
red = uint8[arrayPosition++];
green = uint8[arrayPosition++];
blue = uint8[arrayPosition++];
} else if (byte1 === 0b11111111) { // QOI_OP_RGBA
red = uint8[arrayPosition++];
green = uint8[arrayPosition++];
blue = uint8[arrayPosition++];
alpha = uint8[arrayPosition++];
} else if ((byte1 & 0b11000000) === 0b00000000) { // QOI_OP_INDEX
red = index[byte1 * 4];
green = index[byte1 * 4 + 1];
blue = index[byte1 * 4 + 2];
alpha = index[byte1 * 4 + 3];
} else if ((byte1 & 0b11000000) === 0b01000000) { // QOI_OP_DIFF
red += ((byte1 >> 4) & 0b00000011) - 2;
green += ((byte1 >> 2) & 0b00000011) - 2;
blue += (byte1 & 0b00000011) - 2;
// handle wraparound
red = (red + 256) % 256;
green = (green + 256) % 256;
blue = (blue + 256) % 256;
} else if ((byte1 & 0b11000000) === 0b10000000) { // QOI_OP_LUMA
const byte2 = uint8[arrayPosition++];
const greenDiff = (byte1 & 0b00111111) - 32;
const redDiff = greenDiff + ((byte2 >> 4) & 0b00001111) - 8;
const blueDiff = greenDiff + (byte2 & 0b00001111) - 8;
// handle wraparound
red = (red + redDiff + 256) % 256;
green = (green + greenDiff + 256) % 256;
blue = (blue + blueDiff + 256) % 256;
} else if ((byte1 & 0b11000000) === 0b11000000) { // QOI_OP_RUN
run = byte1 & 0b00111111;
}
indexPosition = ((red * 3 + green * 5 + blue * 7 + alpha * 11) % 64) * 4;
index[indexPosition] = red;
index[indexPosition + 1] = green;
index[indexPosition + 2] = blue;
index[indexPosition + 3] = alpha;
}
if (outputChannels === 4) { // RGBA
result[pixelPosition] = red;
result[pixelPosition + 1] = green;
result[pixelPosition + 2] = blue;
result[pixelPosition + 3] = alpha;
} else { // RGB
result[pixelPosition] = red;
result[pixelPosition + 1] = green;
result[pixelPosition + 2] = blue;
}
}
if (pixelPosition < pixelLength) {
throw new Error('QOI.decode: Incomplete image');
}
// checking the 00000001 padding is not required, as per specs
return {
width: width,
height: height,
colorspace: colorspace,
channels: outputChannels,
data: result
};
},
encode: function(colorData, description) {
const width = description.width;
const height = description.height;
const channels = description.channels;
const colorspace = description.colorspace;
let red = 0;
let green = 0;
let blue = 0;
let alpha = 255;
let prevRed = red;
let prevGreen = green;
let prevBlue = blue;
let prevAlpha = alpha;
let run = 0;
let p = 0;
const pixelLength = width * height * channels;
const pixelEnd = pixelLength - channels;
if (width < 0 || width >= 4294967296) {
throw new Error('QOI.encode: Invalid description.width');
}
if (height < 0 || height >= 4294967296) {
throw new Error('QOI.encode: Invalid description.height');
}
if (colorData.constructor.name !== 'Uint8Array' && colorData.constructor.name !== 'Uint8ClampedArray') {
throw new Error('QOI.encode: The provided colorData must be instance of Uint8Array or Uint8ClampedArray');
}
if (colorData.length !== pixelLength) {
throw new Error('QOI.encode: The length of colorData is incorrect');
}
if (channels !== 3 && channels !== 4) {
throw new Error('QOI.encode: Invalid description.channels, must be 3 or 4');
}
if (colorspace !== 0 && colorspace !== 1) {
throw new Error('QOI.encode: Invalid description.colorspace, must be 0 or 1');
}
const maxSize = width * height * (channels + 1) + 14 + 8;
const result = new Uint8Array(maxSize);
const index = new Uint8Array(64 * 4);
// 0->3 : magic "qoif"
result[p++] = 0x71;
result[p++] = 0x6F;
result[p++] = 0x69;
result[p++] = 0x66;
// 4->7 : width
result[p++] = (width >> 24) & 0xFF;
result[p++] = (width >> 16) & 0xFF;
result[p++] = (width >> 8) & 0xFF;
result[p++] = width & 0xFF;
// 8->11 : height
result[p++] = (height >> 24) & 0xFF;
result[p++] = (height >> 16) & 0xFF;
result[p++] = (height >> 8) & 0xFF;
result[p++] = height & 0xFF;
// 12 : channels, 13 : colorspace
result[p++] = channels;
result[p++] = colorspace;
for (let pixelPos = 0; pixelPos < pixelLength; pixelPos += channels) {
if (channels === 4) {
red = colorData[pixelPos];
green = colorData[pixelPos + 1];
blue = colorData[pixelPos + 2];
alpha = colorData[pixelPos + 3];
} else {
red = colorData[pixelPos];
green = colorData[pixelPos + 1];
blue = colorData[pixelPos + 2];
}
if (prevRed === red && prevGreen === green && prevBlue === blue && prevAlpha === alpha) {
run++;
// reached the maximum run length, or reached the end of colorData
if (run === 62 || pixelPos === pixelEnd) {
// QOI_OP_RUN
result[p++] = 0b11000000 | (run - 1);
run = 0;
}
} else {
if (run > 0) {
// QOI_OP_RUN
result[p++] = 0b11000000 | (run - 1);
run = 0;
}
const indexPosition = ((red * 3 + green * 5 + blue * 7 + alpha * 11) % 64) * 4;
if (index[indexPosition] === red && index[indexPosition + 1] === green && index[indexPosition + 2] === blue && index[indexPosition + 3] === alpha) {
result[p++] = indexPosition / 4;
} else {
index[indexPosition] = red;
index[indexPosition + 1] = green;
index[indexPosition + 2] = blue;
index[indexPosition + 3] = alpha;
if (alpha === prevAlpha) {
// ternary with bitmask handles the wraparound
let vr = red - prevRed;
vr = vr & 0b10000000 ? (vr - 256) % 256 : (vr + 256) % 256;
let vg = green - prevGreen;
vg = vg & 0b10000000 ? (vg - 256) % 256 : (vg + 256) % 256;
let vb = blue - prevBlue;
vb = vb & 0b10000000 ? (vb - 256) % 256 : (vb + 256) % 256;
const vg_r = vr - vg;
const vg_b = vb - vg;
if (vr > -3 && vr < 2 && vg > -3 && vg < 2 && vb > -3 && vb < 2) {
// QOI_OP_DIFF
result[p++] = 0b01000000 | (vr + 2) << 4 | (vg + 2) << 2 | (vb + 2);
} else if (vg_r > -9 && vg_r < 8 && vg > -33 && vg < 32 && vg_b > -9 && vg_b < 8) {
// QOI_OP_LUMA
result[p++] = 0b10000000 | (vg + 32);
result[p++] = (vg_r + 8) << 4 | (vg_b + 8);
} else {
// QOI_OP_RGB
result[p++] = 0b11111110;
result[p++] = red;
result[p++] = green;
result[p++] = blue;
}
} else {
// QOI_OP_RGBA
result[p++] = 0b11111111;
result[p++] = red;
result[p++] = green;
result[p++] = blue;
result[p++] = alpha;
}
}
}
prevRed = red;
prevGreen = green;
prevBlue = blue;
prevAlpha = alpha;
}
// 00000001 end marker/padding
result[p++] = 0;
result[p++] = 0;
result[p++] = 0;
result[p++] = 0;
result[p++] = 0;
result[p++] = 0;
result[p++] = 0;
result[p++] = 1;
// return an ArrayBuffer trimmed to the correct length
return result.buffer.slice(0, p);
}
};