gb-image-decoder 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/dist/index.cjs +609 -0
- package/dist/index.d.cts +131 -0
- package/dist/index.d.mts +131 -0
- package/dist/index.d.ts +131 -0
- package/dist/index.mjs +600 -0
- package/package.json +36 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
var BlendMode = /* @__PURE__ */ ((BlendMode2) => {
|
|
2
|
+
BlendMode2["NORMAL"] = "normal";
|
|
3
|
+
BlendMode2["NORMAL_S"] = "normal_s";
|
|
4
|
+
BlendMode2["LIGHTEN"] = "lighten";
|
|
5
|
+
BlendMode2["SCREEN"] = "screen";
|
|
6
|
+
BlendMode2["DODGE"] = "dodge";
|
|
7
|
+
BlendMode2["ADDITION"] = "addition";
|
|
8
|
+
BlendMode2["DARKEN"] = "darken";
|
|
9
|
+
BlendMode2["MULTIPLY"] = "multiply";
|
|
10
|
+
BlendMode2["BURN"] = "burn";
|
|
11
|
+
BlendMode2["OVERLAY"] = "overlay";
|
|
12
|
+
BlendMode2["SOFTLIGHT"] = "softlight";
|
|
13
|
+
BlendMode2["HARDLIGHT"] = "hardlight";
|
|
14
|
+
BlendMode2["DIFFERENCE"] = "difference";
|
|
15
|
+
BlendMode2["EXCLUSION"] = "exclusion";
|
|
16
|
+
return BlendMode2;
|
|
17
|
+
})(BlendMode || {});
|
|
18
|
+
const blendModeNewName = (oldName) => {
|
|
19
|
+
switch (oldName) {
|
|
20
|
+
case "normal" /* NORMAL */:
|
|
21
|
+
return "source-over";
|
|
22
|
+
case "normal_s" /* NORMAL_S */:
|
|
23
|
+
return "source-over";
|
|
24
|
+
case "lighten" /* LIGHTEN */:
|
|
25
|
+
return "lighten";
|
|
26
|
+
case "screen" /* SCREEN */:
|
|
27
|
+
return "screen";
|
|
28
|
+
case "dodge" /* DODGE */:
|
|
29
|
+
return "color-dodge";
|
|
30
|
+
case "addition" /* ADDITION */:
|
|
31
|
+
return "lighter";
|
|
32
|
+
case "darken" /* DARKEN */:
|
|
33
|
+
return "darken";
|
|
34
|
+
case "multiply" /* MULTIPLY */:
|
|
35
|
+
return "multiply";
|
|
36
|
+
case "burn" /* BURN */:
|
|
37
|
+
return "color-burn";
|
|
38
|
+
case "overlay" /* OVERLAY */:
|
|
39
|
+
return "overlay";
|
|
40
|
+
case "softlight" /* SOFTLIGHT */:
|
|
41
|
+
return "soft-light";
|
|
42
|
+
case "hardlight" /* HARDLIGHT */:
|
|
43
|
+
return "hard-light";
|
|
44
|
+
case "difference" /* DIFFERENCE */:
|
|
45
|
+
return "difference";
|
|
46
|
+
case "exclusion" /* EXCLUSION */:
|
|
47
|
+
return "exclusion";
|
|
48
|
+
default:
|
|
49
|
+
return "multiply";
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const BLACK = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
|
|
54
|
+
const WHITE = "00000000000000000000000000000000";
|
|
55
|
+
const TILE_PIXEL_WIDTH = 8;
|
|
56
|
+
const TILE_PIXEL_HEIGHT = 8;
|
|
57
|
+
const TILES_PER_LINE = 20;
|
|
58
|
+
const BW_PALETTE = [16777215, 11184810, 5592405, 0];
|
|
59
|
+
const BLACK_LINE = [
|
|
60
|
+
BLACK,
|
|
61
|
+
BLACK,
|
|
62
|
+
BLACK,
|
|
63
|
+
BLACK,
|
|
64
|
+
BLACK,
|
|
65
|
+
BLACK,
|
|
66
|
+
BLACK,
|
|
67
|
+
BLACK,
|
|
68
|
+
BLACK,
|
|
69
|
+
BLACK,
|
|
70
|
+
BLACK,
|
|
71
|
+
BLACK,
|
|
72
|
+
BLACK,
|
|
73
|
+
BLACK,
|
|
74
|
+
BLACK,
|
|
75
|
+
BLACK,
|
|
76
|
+
BLACK,
|
|
77
|
+
BLACK,
|
|
78
|
+
BLACK,
|
|
79
|
+
BLACK
|
|
80
|
+
];
|
|
81
|
+
const WHITE_LINE = [
|
|
82
|
+
WHITE,
|
|
83
|
+
WHITE,
|
|
84
|
+
WHITE,
|
|
85
|
+
WHITE,
|
|
86
|
+
WHITE,
|
|
87
|
+
WHITE,
|
|
88
|
+
WHITE,
|
|
89
|
+
WHITE,
|
|
90
|
+
WHITE,
|
|
91
|
+
WHITE,
|
|
92
|
+
WHITE,
|
|
93
|
+
WHITE,
|
|
94
|
+
WHITE,
|
|
95
|
+
WHITE,
|
|
96
|
+
WHITE,
|
|
97
|
+
WHITE,
|
|
98
|
+
WHITE,
|
|
99
|
+
WHITE,
|
|
100
|
+
WHITE,
|
|
101
|
+
WHITE
|
|
102
|
+
];
|
|
103
|
+
const defaultPalette = {
|
|
104
|
+
r: [0, 84, 172, 255],
|
|
105
|
+
g: [0, 84, 172, 255],
|
|
106
|
+
b: [0, 84, 172, 255],
|
|
107
|
+
n: [0, 85, 170, 255],
|
|
108
|
+
blend: BlendMode.MULTIPLY
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
var ChannelKey = /* @__PURE__ */ ((ChannelKey2) => {
|
|
112
|
+
ChannelKey2["R"] = "r";
|
|
113
|
+
ChannelKey2["G"] = "g";
|
|
114
|
+
ChannelKey2["B"] = "b";
|
|
115
|
+
ChannelKey2["N"] = "n";
|
|
116
|
+
return ChannelKey2;
|
|
117
|
+
})(ChannelKey || {});
|
|
118
|
+
var ExportFrameMode = /* @__PURE__ */ ((ExportFrameMode2) => {
|
|
119
|
+
ExportFrameMode2["FRAMEMODE_KEEP"] = "keep";
|
|
120
|
+
ExportFrameMode2["FRAMEMODE_CROP"] = "crop";
|
|
121
|
+
ExportFrameMode2["FRAMEMODE_SQUARE_BLACK"] = "square_black";
|
|
122
|
+
ExportFrameMode2["FRAMEMODE_SQUARE_WHITE"] = "square_white";
|
|
123
|
+
return ExportFrameMode2;
|
|
124
|
+
})(ExportFrameMode || {});
|
|
125
|
+
const channels = ["r" /* R */, "g" /* G */, "b" /* B */, "n" /* N */];
|
|
126
|
+
|
|
127
|
+
const decodeTile = (rawBytes = BLACK) => {
|
|
128
|
+
const bytes = rawBytes.replace(/[^0-9A-F]/ig, "").padEnd(32, "f");
|
|
129
|
+
const byteArray = new Array(16);
|
|
130
|
+
for (let i = 0; i < byteArray.length; i += 1) {
|
|
131
|
+
byteArray[i] = parseInt(bytes.substr(i * 2, 2), 16);
|
|
132
|
+
}
|
|
133
|
+
const pixels = new Array(TILE_PIXEL_WIDTH * TILE_PIXEL_HEIGHT);
|
|
134
|
+
for (let y = 0; y < TILE_PIXEL_HEIGHT; y += 1) {
|
|
135
|
+
for (let x = 0; x < TILE_PIXEL_WIDTH; x += 1) {
|
|
136
|
+
const hiBit = byteArray[y * 2 + 1] >> 7 - x & 1;
|
|
137
|
+
const loBit = byteArray[y * 2] >> 7 - x & 1;
|
|
138
|
+
pixels[y * TILE_PIXEL_WIDTH + x] = hiBit << 1 | loBit;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return pixels;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const tileIndexIsPartOfFrame = (tileIndex, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) => {
|
|
145
|
+
const checkIndex = tileIndex - (handleExportFrame === ExportFrameMode.FRAMEMODE_KEEP ? 0 : 20);
|
|
146
|
+
if (checkIndex < 40) {
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
if (checkIndex >= 320) {
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
switch (checkIndex % 20) {
|
|
153
|
+
case 0:
|
|
154
|
+
case 1:
|
|
155
|
+
case 18:
|
|
156
|
+
case 19:
|
|
157
|
+
return true;
|
|
158
|
+
default:
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const getRGBValue = ({
|
|
164
|
+
pixels,
|
|
165
|
+
index,
|
|
166
|
+
tileIndex,
|
|
167
|
+
handleExportFrame,
|
|
168
|
+
lockFrame,
|
|
169
|
+
invertPalette,
|
|
170
|
+
colorData
|
|
171
|
+
}) => {
|
|
172
|
+
const palette = lockFrame && // Must be actually locked
|
|
173
|
+
handleExportFrame !== ExportFrameMode.FRAMEMODE_CROP && tileIndexIsPartOfFrame(tileIndex, handleExportFrame) ? BW_PALETTE : colorData;
|
|
174
|
+
const value = invertPalette ? palette[3 - pixels[index]] : palette[pixels[index]];
|
|
175
|
+
return {
|
|
176
|
+
// eslint-disable-next-line no-bitwise
|
|
177
|
+
r: (value & 16711680) >> 16,
|
|
178
|
+
// eslint-disable-next-line no-bitwise
|
|
179
|
+
g: (value & 65280) >> 8,
|
|
180
|
+
// eslint-disable-next-line no-bitwise
|
|
181
|
+
b: value & 255
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
var __defProp$1 = Object.defineProperty;
|
|
186
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
187
|
+
var __publicField$1 = (obj, key, value) => {
|
|
188
|
+
__defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
189
|
+
return value;
|
|
190
|
+
};
|
|
191
|
+
class Decoder {
|
|
192
|
+
constructor() {
|
|
193
|
+
__publicField$1(this, "canvas");
|
|
194
|
+
__publicField$1(this, "tiles");
|
|
195
|
+
__publicField$1(this, "colors");
|
|
196
|
+
__publicField$1(this, "rawImageData");
|
|
197
|
+
__publicField$1(this, "lockFrame");
|
|
198
|
+
__publicField$1(this, "invertPalette");
|
|
199
|
+
__publicField$1(this, "colorData");
|
|
200
|
+
this.canvas = null;
|
|
201
|
+
this.tiles = [];
|
|
202
|
+
this.colors = [];
|
|
203
|
+
this.rawImageData = null;
|
|
204
|
+
this.lockFrame = false;
|
|
205
|
+
this.invertPalette = false;
|
|
206
|
+
this.colorData = [...BW_PALETTE];
|
|
207
|
+
}
|
|
208
|
+
update({
|
|
209
|
+
canvas = null,
|
|
210
|
+
tiles = [],
|
|
211
|
+
palette = [],
|
|
212
|
+
lockFrame = false,
|
|
213
|
+
invertPalette = false
|
|
214
|
+
}) {
|
|
215
|
+
const canvasChanged = canvas ? this.setCanvas(canvas) : false;
|
|
216
|
+
const paletteChanged = palette ? this.setPalette(palette, invertPalette) : false;
|
|
217
|
+
const lockFrameChanged = this.setLockFrame(lockFrame);
|
|
218
|
+
if (canvasChanged || paletteChanged || lockFrameChanged || !this.tiles.length) {
|
|
219
|
+
this.tiles = [];
|
|
220
|
+
}
|
|
221
|
+
const tilesChanged = this.setTiles(tiles);
|
|
222
|
+
const newHeight = this.getHeight();
|
|
223
|
+
if (!this.canvas) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (newHeight === 0) {
|
|
227
|
+
this.canvas.height = 0;
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (this.canvas.height !== newHeight || !this.rawImageData?.length) {
|
|
231
|
+
this.canvas.height = newHeight;
|
|
232
|
+
const newRawImageData = new Uint8ClampedArray(160 * newHeight * 4);
|
|
233
|
+
this.rawImageData?.forEach((value, index) => {
|
|
234
|
+
newRawImageData[index] = value;
|
|
235
|
+
});
|
|
236
|
+
this.rawImageData = newRawImageData;
|
|
237
|
+
}
|
|
238
|
+
tilesChanged.forEach(({ index, newTile }) => {
|
|
239
|
+
this.renderTile(index, newTile);
|
|
240
|
+
});
|
|
241
|
+
this.updateCanvas(newHeight);
|
|
242
|
+
}
|
|
243
|
+
updateCanvas(newHeight) {
|
|
244
|
+
if (!this.canvas || !this.rawImageData?.length) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const context = this.canvas.getContext("2d");
|
|
248
|
+
const imageData = new ImageData(this.rawImageData, 160, newHeight);
|
|
249
|
+
context?.putImageData(imageData, 0, 0);
|
|
250
|
+
}
|
|
251
|
+
getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
|
|
252
|
+
const handleFrameMode = this.tiles.length === 360 ? handleExportFrame : ExportFrameMode.FRAMEMODE_KEEP;
|
|
253
|
+
const {
|
|
254
|
+
initialHeight,
|
|
255
|
+
initialWidth,
|
|
256
|
+
tilesPerLine
|
|
257
|
+
} = Decoder.getScaledCanvasSize(handleFrameMode, this.getHeight());
|
|
258
|
+
const canvas = document.createElement("canvas");
|
|
259
|
+
const context = canvas.getContext("2d");
|
|
260
|
+
if (!context) {
|
|
261
|
+
throw new Error("no canvas context");
|
|
262
|
+
}
|
|
263
|
+
canvas.width = initialWidth * scaleFactor;
|
|
264
|
+
canvas.height = initialHeight * scaleFactor;
|
|
265
|
+
this.getExportTiles(handleFrameMode).forEach((tile, index) => {
|
|
266
|
+
this.paintTileScaled(decodeTile(tile), index, context, scaleFactor, tilesPerLine, handleExportFrame);
|
|
267
|
+
});
|
|
268
|
+
return canvas;
|
|
269
|
+
}
|
|
270
|
+
static getScaledCanvasSize(handleExportFrame, height) {
|
|
271
|
+
const FRAME_TILES = 4;
|
|
272
|
+
switch (handleExportFrame) {
|
|
273
|
+
case ExportFrameMode.FRAMEMODE_KEEP:
|
|
274
|
+
return {
|
|
275
|
+
initialHeight: height,
|
|
276
|
+
initialWidth: TILES_PER_LINE * TILE_PIXEL_WIDTH,
|
|
277
|
+
tilesPerLine: TILES_PER_LINE
|
|
278
|
+
};
|
|
279
|
+
case ExportFrameMode.FRAMEMODE_CROP:
|
|
280
|
+
return {
|
|
281
|
+
initialHeight: height - TILE_PIXEL_HEIGHT * FRAME_TILES,
|
|
282
|
+
initialWidth: TILES_PER_LINE * TILE_PIXEL_WIDTH - TILE_PIXEL_WIDTH * FRAME_TILES,
|
|
283
|
+
tilesPerLine: TILES_PER_LINE - FRAME_TILES
|
|
284
|
+
};
|
|
285
|
+
case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
|
|
286
|
+
case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
|
|
287
|
+
return {
|
|
288
|
+
initialHeight: height + 2 * TILE_PIXEL_HEIGHT,
|
|
289
|
+
initialWidth: TILES_PER_LINE * TILE_PIXEL_WIDTH,
|
|
290
|
+
tilesPerLine: TILES_PER_LINE
|
|
291
|
+
};
|
|
292
|
+
default:
|
|
293
|
+
throw new Error(`unknown export mode ${handleExportFrame}`);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
getExportTiles(handleExportFrame) {
|
|
297
|
+
if (!this.tiles) {
|
|
298
|
+
throw new Error("no tiles to export");
|
|
299
|
+
}
|
|
300
|
+
switch (handleExportFrame) {
|
|
301
|
+
case ExportFrameMode.FRAMEMODE_KEEP:
|
|
302
|
+
return this.tiles;
|
|
303
|
+
case ExportFrameMode.FRAMEMODE_CROP:
|
|
304
|
+
return this.tiles.reduce((acc, tile, index) => tileIndexIsPartOfFrame(index, ExportFrameMode.FRAMEMODE_KEEP) ? acc : [...acc, tile], []);
|
|
305
|
+
case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
|
|
306
|
+
return [
|
|
307
|
+
...BLACK_LINE,
|
|
308
|
+
...this.tiles,
|
|
309
|
+
...BLACK_LINE
|
|
310
|
+
];
|
|
311
|
+
case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
|
|
312
|
+
return [
|
|
313
|
+
...WHITE_LINE,
|
|
314
|
+
...this.tiles,
|
|
315
|
+
...WHITE_LINE
|
|
316
|
+
];
|
|
317
|
+
default:
|
|
318
|
+
throw new Error(`unknown export mode ${handleExportFrame}`);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
setCanvas(canvas) {
|
|
322
|
+
if (this.canvas === canvas) {
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
this.canvas = canvas;
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
setPalette(palette, invertPalette) {
|
|
329
|
+
if (this.colors[0] === palette[0] && this.colors[1] === palette[1] && this.colors[2] === palette[2] && this.colors[3] === palette[3] && this.invertPalette === invertPalette) {
|
|
330
|
+
return false;
|
|
331
|
+
}
|
|
332
|
+
this.colors = palette;
|
|
333
|
+
this.invertPalette = invertPalette;
|
|
334
|
+
this.colors.forEach((color, index) => {
|
|
335
|
+
this.colorData[index] = color.length !== 7 ? BW_PALETTE[index] : parseInt(color.substring(1), 16);
|
|
336
|
+
});
|
|
337
|
+
return true;
|
|
338
|
+
}
|
|
339
|
+
setLockFrame(lockFrame) {
|
|
340
|
+
if (lockFrame !== this.lockFrame) {
|
|
341
|
+
this.lockFrame = lockFrame;
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
344
|
+
return false;
|
|
345
|
+
}
|
|
346
|
+
setTiles(tiles) {
|
|
347
|
+
const changedTiles = tiles.reduce((acc, newTile, index) => {
|
|
348
|
+
const changed = newTile !== this.tiles[index];
|
|
349
|
+
if (!changed) {
|
|
350
|
+
return acc;
|
|
351
|
+
}
|
|
352
|
+
return [
|
|
353
|
+
...acc,
|
|
354
|
+
{
|
|
355
|
+
index,
|
|
356
|
+
newTile
|
|
357
|
+
}
|
|
358
|
+
];
|
|
359
|
+
}, []);
|
|
360
|
+
this.tiles = tiles;
|
|
361
|
+
return changedTiles;
|
|
362
|
+
}
|
|
363
|
+
renderTile(tileIndex, rawLine) {
|
|
364
|
+
const tile = decodeTile(rawLine);
|
|
365
|
+
this.paintTile(tile, tileIndex);
|
|
366
|
+
}
|
|
367
|
+
// This paints the tile with a specified offset and pixel width
|
|
368
|
+
paintTile(pixels, index) {
|
|
369
|
+
if (!this.rawImageData) {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
const tileXOffset = index % TILES_PER_LINE;
|
|
373
|
+
const tileYOffset = Math.floor(index / TILES_PER_LINE);
|
|
374
|
+
const pixelXOffset = TILE_PIXEL_WIDTH * tileXOffset;
|
|
375
|
+
const pixelYOffset = TILE_PIXEL_HEIGHT * tileYOffset;
|
|
376
|
+
for (let x = 0; x < TILE_PIXEL_WIDTH; x += 1) {
|
|
377
|
+
for (let y = 0; y < TILE_PIXEL_HEIGHT; y += 1) {
|
|
378
|
+
const rawIndex = (pixelXOffset + x + (pixelYOffset + y) * 160) * 4;
|
|
379
|
+
const color = getRGBValue({
|
|
380
|
+
pixels,
|
|
381
|
+
index: y * TILE_PIXEL_WIDTH + x,
|
|
382
|
+
tileIndex: index,
|
|
383
|
+
handleExportFrame: ExportFrameMode.FRAMEMODE_KEEP,
|
|
384
|
+
invertPalette: this.invertPalette,
|
|
385
|
+
lockFrame: this.lockFrame,
|
|
386
|
+
colorData: this.colorData
|
|
387
|
+
});
|
|
388
|
+
this.rawImageData[rawIndex] = color.r;
|
|
389
|
+
this.rawImageData[rawIndex + 1] = color.g;
|
|
390
|
+
this.rawImageData[rawIndex + 2] = color.b;
|
|
391
|
+
this.rawImageData[rawIndex + 3] = 255;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
paintTileScaled(pixels, index, canvasContext, pixelSize, tilesPerLine, handleExportFrame) {
|
|
396
|
+
const tileXOffset = index % tilesPerLine;
|
|
397
|
+
const tileYOffset = Math.floor(index / tilesPerLine);
|
|
398
|
+
const pixelXOffset = TILE_PIXEL_WIDTH * tileXOffset * pixelSize;
|
|
399
|
+
const pixelYOffset = TILE_PIXEL_HEIGHT * tileYOffset * pixelSize;
|
|
400
|
+
for (let x = 0; x < TILE_PIXEL_WIDTH; x += 1) {
|
|
401
|
+
for (let y = 0; y < TILE_PIXEL_HEIGHT; y += 1) {
|
|
402
|
+
const color = getRGBValue({
|
|
403
|
+
pixels,
|
|
404
|
+
index: y * TILE_PIXEL_WIDTH + x,
|
|
405
|
+
tileIndex: index,
|
|
406
|
+
handleExportFrame,
|
|
407
|
+
lockFrame: this.lockFrame,
|
|
408
|
+
invertPalette: this.invertPalette,
|
|
409
|
+
colorData: this.colorData
|
|
410
|
+
});
|
|
411
|
+
canvasContext.fillStyle = `rgb(${color.r}, ${color.g}, ${color.b})`;
|
|
412
|
+
canvasContext.fillRect(
|
|
413
|
+
pixelXOffset + x * pixelSize,
|
|
414
|
+
pixelYOffset + y * pixelSize,
|
|
415
|
+
pixelSize + 1,
|
|
416
|
+
pixelSize + 1
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
getHeight() {
|
|
422
|
+
return TILE_PIXEL_HEIGHT * Math.ceil(this.tiles.length / TILES_PER_LINE);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const hx2 = (n) => (n || 0).toString(16).padStart(2, "0");
|
|
427
|
+
const paletteTemplates = {
|
|
428
|
+
r: (v) => `#${hx2(v)}0000`,
|
|
429
|
+
g: (v) => `#00${hx2(v)}00`,
|
|
430
|
+
b: (v) => `#0000${hx2(v)}`,
|
|
431
|
+
n: (v) => `#${hx2(v)}${hx2(v)}${hx2(v)}`
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
var __defProp = Object.defineProperty;
|
|
435
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
436
|
+
var __publicField = (obj, key, value) => {
|
|
437
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
438
|
+
return value;
|
|
439
|
+
};
|
|
440
|
+
const createChannel = (key) => {
|
|
441
|
+
const canvas = document.createElement("canvas");
|
|
442
|
+
const decoder = new Decoder();
|
|
443
|
+
decoder.update({
|
|
444
|
+
canvas,
|
|
445
|
+
tiles: [],
|
|
446
|
+
palette: [],
|
|
447
|
+
lockFrame: false,
|
|
448
|
+
invertPalette: false
|
|
449
|
+
});
|
|
450
|
+
return {
|
|
451
|
+
key,
|
|
452
|
+
decoder,
|
|
453
|
+
canvas
|
|
454
|
+
};
|
|
455
|
+
};
|
|
456
|
+
class RGBNDecoder {
|
|
457
|
+
constructor() {
|
|
458
|
+
__publicField(this, "canvas");
|
|
459
|
+
__publicField(this, "palette");
|
|
460
|
+
__publicField(this, "lockFrame");
|
|
461
|
+
__publicField(this, "channels");
|
|
462
|
+
this.canvas = null;
|
|
463
|
+
this.palette = defaultPalette;
|
|
464
|
+
this.lockFrame = false;
|
|
465
|
+
this.channels = {
|
|
466
|
+
r: createChannel(ChannelKey.R),
|
|
467
|
+
g: createChannel(ChannelKey.G),
|
|
468
|
+
b: createChannel(ChannelKey.B),
|
|
469
|
+
n: createChannel(ChannelKey.N)
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
update({
|
|
473
|
+
canvas = null,
|
|
474
|
+
tiles = {},
|
|
475
|
+
palette,
|
|
476
|
+
lockFrame = false
|
|
477
|
+
}) {
|
|
478
|
+
const canvasChanged = canvas ? this.setCanvas(canvas) : false;
|
|
479
|
+
const paletteChanged = this.setPalette(palette);
|
|
480
|
+
const lockFrameChanged = this.setLockFrame(lockFrame);
|
|
481
|
+
const shouldUpdate = canvasChanged || paletteChanged || lockFrameChanged;
|
|
482
|
+
if (!shouldUpdate) {
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
const canvases = this.setTiles(tiles);
|
|
486
|
+
const newHeight = this.getHeight();
|
|
487
|
+
if (!this.canvas) {
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
if (newHeight === 0) {
|
|
491
|
+
this.canvas.height = 0;
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
if (this.canvas.height !== newHeight) {
|
|
495
|
+
this.canvas.height = newHeight;
|
|
496
|
+
}
|
|
497
|
+
const context = this.canvas?.getContext("2d");
|
|
498
|
+
if (!context) {
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
this.blendCanvases(context, canvases);
|
|
502
|
+
}
|
|
503
|
+
setPalette(palette) {
|
|
504
|
+
if (!palette) {
|
|
505
|
+
return false;
|
|
506
|
+
}
|
|
507
|
+
if (JSON.stringify(this.palette) === JSON.stringify(palette)) {
|
|
508
|
+
return false;
|
|
509
|
+
}
|
|
510
|
+
this.palette = palette;
|
|
511
|
+
return true;
|
|
512
|
+
}
|
|
513
|
+
setCanvas(canvas) {
|
|
514
|
+
if (this.canvas === canvas) {
|
|
515
|
+
return false;
|
|
516
|
+
}
|
|
517
|
+
this.canvas = canvas;
|
|
518
|
+
return true;
|
|
519
|
+
}
|
|
520
|
+
setLockFrame(lockFrame) {
|
|
521
|
+
if (lockFrame !== this.lockFrame) {
|
|
522
|
+
this.lockFrame = lockFrame;
|
|
523
|
+
return true;
|
|
524
|
+
}
|
|
525
|
+
return false;
|
|
526
|
+
}
|
|
527
|
+
setTiles(tiles) {
|
|
528
|
+
return channels.reduce((acc, key) => {
|
|
529
|
+
const channel = this.channels[key];
|
|
530
|
+
channel.tiles = tiles[key];
|
|
531
|
+
const channelColors = this.palette[key];
|
|
532
|
+
const paletteFunction = paletteTemplates[key];
|
|
533
|
+
if (!channel.tiles || !channelColors) {
|
|
534
|
+
return acc;
|
|
535
|
+
}
|
|
536
|
+
const palette = [
|
|
537
|
+
paletteFunction(channelColors[3]),
|
|
538
|
+
paletteFunction(channelColors[2]),
|
|
539
|
+
paletteFunction(channelColors[1]),
|
|
540
|
+
paletteFunction(channelColors[0])
|
|
541
|
+
];
|
|
542
|
+
channel.decoder.update({
|
|
543
|
+
invertPalette: false,
|
|
544
|
+
canvas: channel.canvas,
|
|
545
|
+
tiles: channel.tiles,
|
|
546
|
+
lockFrame: this.lockFrame,
|
|
547
|
+
palette
|
|
548
|
+
});
|
|
549
|
+
return {
|
|
550
|
+
...acc,
|
|
551
|
+
[key]: channel.canvas
|
|
552
|
+
};
|
|
553
|
+
}, {});
|
|
554
|
+
}
|
|
555
|
+
blendCanvases(targetContext, sourceCanvases) {
|
|
556
|
+
channels.forEach((key) => {
|
|
557
|
+
const sourceCanvas = sourceCanvases[key];
|
|
558
|
+
if (sourceCanvas && sourceCanvas.width && sourceCanvas.height) {
|
|
559
|
+
if (key === ChannelKey.N) {
|
|
560
|
+
if (this.palette.blend === BlendMode.NORMAL) {
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
targetContext.globalCompositeOperation = blendModeNewName(this.palette.blend);
|
|
564
|
+
} else {
|
|
565
|
+
targetContext.globalCompositeOperation = "lighter";
|
|
566
|
+
}
|
|
567
|
+
targetContext.drawImage(sourceCanvas, 0, 0);
|
|
568
|
+
}
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
|
|
572
|
+
const canvas = document.createElement("canvas");
|
|
573
|
+
const handleFrameMode = this.maxTiles() === 360 ? handleExportFrame : ExportFrameMode.FRAMEMODE_KEEP;
|
|
574
|
+
const { initialHeight, initialWidth } = Decoder.getScaledCanvasSize(handleFrameMode, this.getHeight());
|
|
575
|
+
canvas.width = initialWidth * scaleFactor;
|
|
576
|
+
canvas.height = initialHeight * scaleFactor;
|
|
577
|
+
const context = canvas.getContext("2d");
|
|
578
|
+
if (!context) {
|
|
579
|
+
throw new Error("no canvas context");
|
|
580
|
+
}
|
|
581
|
+
const canvases = channels.reduce((acc, key) => {
|
|
582
|
+
const channel = this.channels[key];
|
|
583
|
+
const channelCanvas = channel.decoder.getScaledCanvas(scaleFactor, handleExportFrame);
|
|
584
|
+
return {
|
|
585
|
+
...acc,
|
|
586
|
+
[key]: channelCanvas
|
|
587
|
+
};
|
|
588
|
+
}, {});
|
|
589
|
+
this.blendCanvases(context, canvases);
|
|
590
|
+
return canvas;
|
|
591
|
+
}
|
|
592
|
+
maxTiles() {
|
|
593
|
+
return Math.max(...channels.map((key) => this.channels[key].tiles?.length || 0));
|
|
594
|
+
}
|
|
595
|
+
getHeight() {
|
|
596
|
+
return TILE_PIXEL_HEIGHT * Math.ceil(this.maxTiles() / TILES_PER_LINE);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
export { BlendMode, Decoder, ExportFrameMode, RGBNDecoder, blendModeNewName, decodeTile, getRGBValue, tileIndexIsPartOfFrame };
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "gb-image-decoder",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Decoder classes for GameBoy images",
|
|
5
|
+
"repository": "",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"main": "./dist/index.cjs",
|
|
16
|
+
"module": "./dist/index.mjs",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"files": ["dist"],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"prepack": "npm run eslint && npm run build",
|
|
21
|
+
"build": "unbuild",
|
|
22
|
+
"dev": "vitest dev",
|
|
23
|
+
"eslint": "eslint -c .eslintrc --ext .js,.ts ."
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^20.12.7",
|
|
27
|
+
"@typescript-eslint/eslint-plugin": "^7.9.0",
|
|
28
|
+
"eslint": "^8.57.0",
|
|
29
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
30
|
+
"eslint-import-resolver-typescript": "^3.6.1",
|
|
31
|
+
"eslint-plugin-import": "^2.29.1",
|
|
32
|
+
"typescript": "^5.4.5",
|
|
33
|
+
"unbuild": "^2.0.0",
|
|
34
|
+
"vitest": "^1.6.0"
|
|
35
|
+
}
|
|
36
|
+
}
|