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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Andreas Hahn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # GameBoy-Tile format Image Decoders
2
+
package/dist/index.cjs ADDED
@@ -0,0 +1,609 @@
1
+ 'use strict';
2
+
3
+ var BlendMode = /* @__PURE__ */ ((BlendMode2) => {
4
+ BlendMode2["NORMAL"] = "normal";
5
+ BlendMode2["NORMAL_S"] = "normal_s";
6
+ BlendMode2["LIGHTEN"] = "lighten";
7
+ BlendMode2["SCREEN"] = "screen";
8
+ BlendMode2["DODGE"] = "dodge";
9
+ BlendMode2["ADDITION"] = "addition";
10
+ BlendMode2["DARKEN"] = "darken";
11
+ BlendMode2["MULTIPLY"] = "multiply";
12
+ BlendMode2["BURN"] = "burn";
13
+ BlendMode2["OVERLAY"] = "overlay";
14
+ BlendMode2["SOFTLIGHT"] = "softlight";
15
+ BlendMode2["HARDLIGHT"] = "hardlight";
16
+ BlendMode2["DIFFERENCE"] = "difference";
17
+ BlendMode2["EXCLUSION"] = "exclusion";
18
+ return BlendMode2;
19
+ })(BlendMode || {});
20
+ const blendModeNewName = (oldName) => {
21
+ switch (oldName) {
22
+ case "normal" /* NORMAL */:
23
+ return "source-over";
24
+ case "normal_s" /* NORMAL_S */:
25
+ return "source-over";
26
+ case "lighten" /* LIGHTEN */:
27
+ return "lighten";
28
+ case "screen" /* SCREEN */:
29
+ return "screen";
30
+ case "dodge" /* DODGE */:
31
+ return "color-dodge";
32
+ case "addition" /* ADDITION */:
33
+ return "lighter";
34
+ case "darken" /* DARKEN */:
35
+ return "darken";
36
+ case "multiply" /* MULTIPLY */:
37
+ return "multiply";
38
+ case "burn" /* BURN */:
39
+ return "color-burn";
40
+ case "overlay" /* OVERLAY */:
41
+ return "overlay";
42
+ case "softlight" /* SOFTLIGHT */:
43
+ return "soft-light";
44
+ case "hardlight" /* HARDLIGHT */:
45
+ return "hard-light";
46
+ case "difference" /* DIFFERENCE */:
47
+ return "difference";
48
+ case "exclusion" /* EXCLUSION */:
49
+ return "exclusion";
50
+ default:
51
+ return "multiply";
52
+ }
53
+ };
54
+
55
+ const BLACK = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
56
+ const WHITE = "00000000000000000000000000000000";
57
+ const TILE_PIXEL_WIDTH = 8;
58
+ const TILE_PIXEL_HEIGHT = 8;
59
+ const TILES_PER_LINE = 20;
60
+ const BW_PALETTE = [16777215, 11184810, 5592405, 0];
61
+ const BLACK_LINE = [
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
+ BLACK,
81
+ BLACK
82
+ ];
83
+ const WHITE_LINE = [
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
+ WHITE,
103
+ WHITE
104
+ ];
105
+ const defaultPalette = {
106
+ r: [0, 84, 172, 255],
107
+ g: [0, 84, 172, 255],
108
+ b: [0, 84, 172, 255],
109
+ n: [0, 85, 170, 255],
110
+ blend: BlendMode.MULTIPLY
111
+ };
112
+
113
+ var ChannelKey = /* @__PURE__ */ ((ChannelKey2) => {
114
+ ChannelKey2["R"] = "r";
115
+ ChannelKey2["G"] = "g";
116
+ ChannelKey2["B"] = "b";
117
+ ChannelKey2["N"] = "n";
118
+ return ChannelKey2;
119
+ })(ChannelKey || {});
120
+ var ExportFrameMode = /* @__PURE__ */ ((ExportFrameMode2) => {
121
+ ExportFrameMode2["FRAMEMODE_KEEP"] = "keep";
122
+ ExportFrameMode2["FRAMEMODE_CROP"] = "crop";
123
+ ExportFrameMode2["FRAMEMODE_SQUARE_BLACK"] = "square_black";
124
+ ExportFrameMode2["FRAMEMODE_SQUARE_WHITE"] = "square_white";
125
+ return ExportFrameMode2;
126
+ })(ExportFrameMode || {});
127
+ const channels = ["r" /* R */, "g" /* G */, "b" /* B */, "n" /* N */];
128
+
129
+ const decodeTile = (rawBytes = BLACK) => {
130
+ const bytes = rawBytes.replace(/[^0-9A-F]/ig, "").padEnd(32, "f");
131
+ const byteArray = new Array(16);
132
+ for (let i = 0; i < byteArray.length; i += 1) {
133
+ byteArray[i] = parseInt(bytes.substr(i * 2, 2), 16);
134
+ }
135
+ const pixels = new Array(TILE_PIXEL_WIDTH * TILE_PIXEL_HEIGHT);
136
+ for (let y = 0; y < TILE_PIXEL_HEIGHT; y += 1) {
137
+ for (let x = 0; x < TILE_PIXEL_WIDTH; x += 1) {
138
+ const hiBit = byteArray[y * 2 + 1] >> 7 - x & 1;
139
+ const loBit = byteArray[y * 2] >> 7 - x & 1;
140
+ pixels[y * TILE_PIXEL_WIDTH + x] = hiBit << 1 | loBit;
141
+ }
142
+ }
143
+ return pixels;
144
+ };
145
+
146
+ const tileIndexIsPartOfFrame = (tileIndex, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) => {
147
+ const checkIndex = tileIndex - (handleExportFrame === ExportFrameMode.FRAMEMODE_KEEP ? 0 : 20);
148
+ if (checkIndex < 40) {
149
+ return true;
150
+ }
151
+ if (checkIndex >= 320) {
152
+ return true;
153
+ }
154
+ switch (checkIndex % 20) {
155
+ case 0:
156
+ case 1:
157
+ case 18:
158
+ case 19:
159
+ return true;
160
+ default:
161
+ return false;
162
+ }
163
+ };
164
+
165
+ const getRGBValue = ({
166
+ pixels,
167
+ index,
168
+ tileIndex,
169
+ handleExportFrame,
170
+ lockFrame,
171
+ invertPalette,
172
+ colorData
173
+ }) => {
174
+ const palette = lockFrame && // Must be actually locked
175
+ handleExportFrame !== ExportFrameMode.FRAMEMODE_CROP && tileIndexIsPartOfFrame(tileIndex, handleExportFrame) ? BW_PALETTE : colorData;
176
+ const value = invertPalette ? palette[3 - pixels[index]] : palette[pixels[index]];
177
+ return {
178
+ // eslint-disable-next-line no-bitwise
179
+ r: (value & 16711680) >> 16,
180
+ // eslint-disable-next-line no-bitwise
181
+ g: (value & 65280) >> 8,
182
+ // eslint-disable-next-line no-bitwise
183
+ b: value & 255
184
+ };
185
+ };
186
+
187
+ var __defProp$1 = Object.defineProperty;
188
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
189
+ var __publicField$1 = (obj, key, value) => {
190
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
191
+ return value;
192
+ };
193
+ class Decoder {
194
+ constructor() {
195
+ __publicField$1(this, "canvas");
196
+ __publicField$1(this, "tiles");
197
+ __publicField$1(this, "colors");
198
+ __publicField$1(this, "rawImageData");
199
+ __publicField$1(this, "lockFrame");
200
+ __publicField$1(this, "invertPalette");
201
+ __publicField$1(this, "colorData");
202
+ this.canvas = null;
203
+ this.tiles = [];
204
+ this.colors = [];
205
+ this.rawImageData = null;
206
+ this.lockFrame = false;
207
+ this.invertPalette = false;
208
+ this.colorData = [...BW_PALETTE];
209
+ }
210
+ update({
211
+ canvas = null,
212
+ tiles = [],
213
+ palette = [],
214
+ lockFrame = false,
215
+ invertPalette = false
216
+ }) {
217
+ const canvasChanged = canvas ? this.setCanvas(canvas) : false;
218
+ const paletteChanged = palette ? this.setPalette(palette, invertPalette) : false;
219
+ const lockFrameChanged = this.setLockFrame(lockFrame);
220
+ if (canvasChanged || paletteChanged || lockFrameChanged || !this.tiles.length) {
221
+ this.tiles = [];
222
+ }
223
+ const tilesChanged = this.setTiles(tiles);
224
+ const newHeight = this.getHeight();
225
+ if (!this.canvas) {
226
+ return;
227
+ }
228
+ if (newHeight === 0) {
229
+ this.canvas.height = 0;
230
+ return;
231
+ }
232
+ if (this.canvas.height !== newHeight || !this.rawImageData?.length) {
233
+ this.canvas.height = newHeight;
234
+ const newRawImageData = new Uint8ClampedArray(160 * newHeight * 4);
235
+ this.rawImageData?.forEach((value, index) => {
236
+ newRawImageData[index] = value;
237
+ });
238
+ this.rawImageData = newRawImageData;
239
+ }
240
+ tilesChanged.forEach(({ index, newTile }) => {
241
+ this.renderTile(index, newTile);
242
+ });
243
+ this.updateCanvas(newHeight);
244
+ }
245
+ updateCanvas(newHeight) {
246
+ if (!this.canvas || !this.rawImageData?.length) {
247
+ return;
248
+ }
249
+ const context = this.canvas.getContext("2d");
250
+ const imageData = new ImageData(this.rawImageData, 160, newHeight);
251
+ context?.putImageData(imageData, 0, 0);
252
+ }
253
+ getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
254
+ const handleFrameMode = this.tiles.length === 360 ? handleExportFrame : ExportFrameMode.FRAMEMODE_KEEP;
255
+ const {
256
+ initialHeight,
257
+ initialWidth,
258
+ tilesPerLine
259
+ } = Decoder.getScaledCanvasSize(handleFrameMode, this.getHeight());
260
+ const canvas = document.createElement("canvas");
261
+ const context = canvas.getContext("2d");
262
+ if (!context) {
263
+ throw new Error("no canvas context");
264
+ }
265
+ canvas.width = initialWidth * scaleFactor;
266
+ canvas.height = initialHeight * scaleFactor;
267
+ this.getExportTiles(handleFrameMode).forEach((tile, index) => {
268
+ this.paintTileScaled(decodeTile(tile), index, context, scaleFactor, tilesPerLine, handleExportFrame);
269
+ });
270
+ return canvas;
271
+ }
272
+ static getScaledCanvasSize(handleExportFrame, height) {
273
+ const FRAME_TILES = 4;
274
+ switch (handleExportFrame) {
275
+ case ExportFrameMode.FRAMEMODE_KEEP:
276
+ return {
277
+ initialHeight: height,
278
+ initialWidth: TILES_PER_LINE * TILE_PIXEL_WIDTH,
279
+ tilesPerLine: TILES_PER_LINE
280
+ };
281
+ case ExportFrameMode.FRAMEMODE_CROP:
282
+ return {
283
+ initialHeight: height - TILE_PIXEL_HEIGHT * FRAME_TILES,
284
+ initialWidth: TILES_PER_LINE * TILE_PIXEL_WIDTH - TILE_PIXEL_WIDTH * FRAME_TILES,
285
+ tilesPerLine: TILES_PER_LINE - FRAME_TILES
286
+ };
287
+ case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
288
+ case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
289
+ return {
290
+ initialHeight: height + 2 * TILE_PIXEL_HEIGHT,
291
+ initialWidth: TILES_PER_LINE * TILE_PIXEL_WIDTH,
292
+ tilesPerLine: TILES_PER_LINE
293
+ };
294
+ default:
295
+ throw new Error(`unknown export mode ${handleExportFrame}`);
296
+ }
297
+ }
298
+ getExportTiles(handleExportFrame) {
299
+ if (!this.tiles) {
300
+ throw new Error("no tiles to export");
301
+ }
302
+ switch (handleExportFrame) {
303
+ case ExportFrameMode.FRAMEMODE_KEEP:
304
+ return this.tiles;
305
+ case ExportFrameMode.FRAMEMODE_CROP:
306
+ return this.tiles.reduce((acc, tile, index) => tileIndexIsPartOfFrame(index, ExportFrameMode.FRAMEMODE_KEEP) ? acc : [...acc, tile], []);
307
+ case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
308
+ return [
309
+ ...BLACK_LINE,
310
+ ...this.tiles,
311
+ ...BLACK_LINE
312
+ ];
313
+ case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
314
+ return [
315
+ ...WHITE_LINE,
316
+ ...this.tiles,
317
+ ...WHITE_LINE
318
+ ];
319
+ default:
320
+ throw new Error(`unknown export mode ${handleExportFrame}`);
321
+ }
322
+ }
323
+ setCanvas(canvas) {
324
+ if (this.canvas === canvas) {
325
+ return false;
326
+ }
327
+ this.canvas = canvas;
328
+ return true;
329
+ }
330
+ setPalette(palette, invertPalette) {
331
+ if (this.colors[0] === palette[0] && this.colors[1] === palette[1] && this.colors[2] === palette[2] && this.colors[3] === palette[3] && this.invertPalette === invertPalette) {
332
+ return false;
333
+ }
334
+ this.colors = palette;
335
+ this.invertPalette = invertPalette;
336
+ this.colors.forEach((color, index) => {
337
+ this.colorData[index] = color.length !== 7 ? BW_PALETTE[index] : parseInt(color.substring(1), 16);
338
+ });
339
+ return true;
340
+ }
341
+ setLockFrame(lockFrame) {
342
+ if (lockFrame !== this.lockFrame) {
343
+ this.lockFrame = lockFrame;
344
+ return true;
345
+ }
346
+ return false;
347
+ }
348
+ setTiles(tiles) {
349
+ const changedTiles = tiles.reduce((acc, newTile, index) => {
350
+ const changed = newTile !== this.tiles[index];
351
+ if (!changed) {
352
+ return acc;
353
+ }
354
+ return [
355
+ ...acc,
356
+ {
357
+ index,
358
+ newTile
359
+ }
360
+ ];
361
+ }, []);
362
+ this.tiles = tiles;
363
+ return changedTiles;
364
+ }
365
+ renderTile(tileIndex, rawLine) {
366
+ const tile = decodeTile(rawLine);
367
+ this.paintTile(tile, tileIndex);
368
+ }
369
+ // This paints the tile with a specified offset and pixel width
370
+ paintTile(pixels, index) {
371
+ if (!this.rawImageData) {
372
+ return;
373
+ }
374
+ const tileXOffset = index % TILES_PER_LINE;
375
+ const tileYOffset = Math.floor(index / TILES_PER_LINE);
376
+ const pixelXOffset = TILE_PIXEL_WIDTH * tileXOffset;
377
+ const pixelYOffset = TILE_PIXEL_HEIGHT * tileYOffset;
378
+ for (let x = 0; x < TILE_PIXEL_WIDTH; x += 1) {
379
+ for (let y = 0; y < TILE_PIXEL_HEIGHT; y += 1) {
380
+ const rawIndex = (pixelXOffset + x + (pixelYOffset + y) * 160) * 4;
381
+ const color = getRGBValue({
382
+ pixels,
383
+ index: y * TILE_PIXEL_WIDTH + x,
384
+ tileIndex: index,
385
+ handleExportFrame: ExportFrameMode.FRAMEMODE_KEEP,
386
+ invertPalette: this.invertPalette,
387
+ lockFrame: this.lockFrame,
388
+ colorData: this.colorData
389
+ });
390
+ this.rawImageData[rawIndex] = color.r;
391
+ this.rawImageData[rawIndex + 1] = color.g;
392
+ this.rawImageData[rawIndex + 2] = color.b;
393
+ this.rawImageData[rawIndex + 3] = 255;
394
+ }
395
+ }
396
+ }
397
+ paintTileScaled(pixels, index, canvasContext, pixelSize, tilesPerLine, handleExportFrame) {
398
+ const tileXOffset = index % tilesPerLine;
399
+ const tileYOffset = Math.floor(index / tilesPerLine);
400
+ const pixelXOffset = TILE_PIXEL_WIDTH * tileXOffset * pixelSize;
401
+ const pixelYOffset = TILE_PIXEL_HEIGHT * tileYOffset * pixelSize;
402
+ for (let x = 0; x < TILE_PIXEL_WIDTH; x += 1) {
403
+ for (let y = 0; y < TILE_PIXEL_HEIGHT; y += 1) {
404
+ const color = getRGBValue({
405
+ pixels,
406
+ index: y * TILE_PIXEL_WIDTH + x,
407
+ tileIndex: index,
408
+ handleExportFrame,
409
+ lockFrame: this.lockFrame,
410
+ invertPalette: this.invertPalette,
411
+ colorData: this.colorData
412
+ });
413
+ canvasContext.fillStyle = `rgb(${color.r}, ${color.g}, ${color.b})`;
414
+ canvasContext.fillRect(
415
+ pixelXOffset + x * pixelSize,
416
+ pixelYOffset + y * pixelSize,
417
+ pixelSize + 1,
418
+ pixelSize + 1
419
+ );
420
+ }
421
+ }
422
+ }
423
+ getHeight() {
424
+ return TILE_PIXEL_HEIGHT * Math.ceil(this.tiles.length / TILES_PER_LINE);
425
+ }
426
+ }
427
+
428
+ const hx2 = (n) => (n || 0).toString(16).padStart(2, "0");
429
+ const paletteTemplates = {
430
+ r: (v) => `#${hx2(v)}0000`,
431
+ g: (v) => `#00${hx2(v)}00`,
432
+ b: (v) => `#0000${hx2(v)}`,
433
+ n: (v) => `#${hx2(v)}${hx2(v)}${hx2(v)}`
434
+ };
435
+
436
+ var __defProp = Object.defineProperty;
437
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
438
+ var __publicField = (obj, key, value) => {
439
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
440
+ return value;
441
+ };
442
+ const createChannel = (key) => {
443
+ const canvas = document.createElement("canvas");
444
+ const decoder = new Decoder();
445
+ decoder.update({
446
+ canvas,
447
+ tiles: [],
448
+ palette: [],
449
+ lockFrame: false,
450
+ invertPalette: false
451
+ });
452
+ return {
453
+ key,
454
+ decoder,
455
+ canvas
456
+ };
457
+ };
458
+ class RGBNDecoder {
459
+ constructor() {
460
+ __publicField(this, "canvas");
461
+ __publicField(this, "palette");
462
+ __publicField(this, "lockFrame");
463
+ __publicField(this, "channels");
464
+ this.canvas = null;
465
+ this.palette = defaultPalette;
466
+ this.lockFrame = false;
467
+ this.channels = {
468
+ r: createChannel(ChannelKey.R),
469
+ g: createChannel(ChannelKey.G),
470
+ b: createChannel(ChannelKey.B),
471
+ n: createChannel(ChannelKey.N)
472
+ };
473
+ }
474
+ update({
475
+ canvas = null,
476
+ tiles = {},
477
+ palette,
478
+ lockFrame = false
479
+ }) {
480
+ const canvasChanged = canvas ? this.setCanvas(canvas) : false;
481
+ const paletteChanged = this.setPalette(palette);
482
+ const lockFrameChanged = this.setLockFrame(lockFrame);
483
+ const shouldUpdate = canvasChanged || paletteChanged || lockFrameChanged;
484
+ if (!shouldUpdate) {
485
+ return;
486
+ }
487
+ const canvases = this.setTiles(tiles);
488
+ const newHeight = this.getHeight();
489
+ if (!this.canvas) {
490
+ return;
491
+ }
492
+ if (newHeight === 0) {
493
+ this.canvas.height = 0;
494
+ return;
495
+ }
496
+ if (this.canvas.height !== newHeight) {
497
+ this.canvas.height = newHeight;
498
+ }
499
+ const context = this.canvas?.getContext("2d");
500
+ if (!context) {
501
+ return;
502
+ }
503
+ this.blendCanvases(context, canvases);
504
+ }
505
+ setPalette(palette) {
506
+ if (!palette) {
507
+ return false;
508
+ }
509
+ if (JSON.stringify(this.palette) === JSON.stringify(palette)) {
510
+ return false;
511
+ }
512
+ this.palette = palette;
513
+ return true;
514
+ }
515
+ setCanvas(canvas) {
516
+ if (this.canvas === canvas) {
517
+ return false;
518
+ }
519
+ this.canvas = canvas;
520
+ return true;
521
+ }
522
+ setLockFrame(lockFrame) {
523
+ if (lockFrame !== this.lockFrame) {
524
+ this.lockFrame = lockFrame;
525
+ return true;
526
+ }
527
+ return false;
528
+ }
529
+ setTiles(tiles) {
530
+ return channels.reduce((acc, key) => {
531
+ const channel = this.channels[key];
532
+ channel.tiles = tiles[key];
533
+ const channelColors = this.palette[key];
534
+ const paletteFunction = paletteTemplates[key];
535
+ if (!channel.tiles || !channelColors) {
536
+ return acc;
537
+ }
538
+ const palette = [
539
+ paletteFunction(channelColors[3]),
540
+ paletteFunction(channelColors[2]),
541
+ paletteFunction(channelColors[1]),
542
+ paletteFunction(channelColors[0])
543
+ ];
544
+ channel.decoder.update({
545
+ invertPalette: false,
546
+ canvas: channel.canvas,
547
+ tiles: channel.tiles,
548
+ lockFrame: this.lockFrame,
549
+ palette
550
+ });
551
+ return {
552
+ ...acc,
553
+ [key]: channel.canvas
554
+ };
555
+ }, {});
556
+ }
557
+ blendCanvases(targetContext, sourceCanvases) {
558
+ channels.forEach((key) => {
559
+ const sourceCanvas = sourceCanvases[key];
560
+ if (sourceCanvas && sourceCanvas.width && sourceCanvas.height) {
561
+ if (key === ChannelKey.N) {
562
+ if (this.palette.blend === BlendMode.NORMAL) {
563
+ return;
564
+ }
565
+ targetContext.globalCompositeOperation = blendModeNewName(this.palette.blend);
566
+ } else {
567
+ targetContext.globalCompositeOperation = "lighter";
568
+ }
569
+ targetContext.drawImage(sourceCanvas, 0, 0);
570
+ }
571
+ });
572
+ }
573
+ getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
574
+ const canvas = document.createElement("canvas");
575
+ const handleFrameMode = this.maxTiles() === 360 ? handleExportFrame : ExportFrameMode.FRAMEMODE_KEEP;
576
+ const { initialHeight, initialWidth } = Decoder.getScaledCanvasSize(handleFrameMode, this.getHeight());
577
+ canvas.width = initialWidth * scaleFactor;
578
+ canvas.height = initialHeight * scaleFactor;
579
+ const context = canvas.getContext("2d");
580
+ if (!context) {
581
+ throw new Error("no canvas context");
582
+ }
583
+ const canvases = channels.reduce((acc, key) => {
584
+ const channel = this.channels[key];
585
+ const channelCanvas = channel.decoder.getScaledCanvas(scaleFactor, handleExportFrame);
586
+ return {
587
+ ...acc,
588
+ [key]: channelCanvas
589
+ };
590
+ }, {});
591
+ this.blendCanvases(context, canvases);
592
+ return canvas;
593
+ }
594
+ maxTiles() {
595
+ return Math.max(...channels.map((key) => this.channels[key].tiles?.length || 0));
596
+ }
597
+ getHeight() {
598
+ return TILE_PIXEL_HEIGHT * Math.ceil(this.maxTiles() / TILES_PER_LINE);
599
+ }
600
+ }
601
+
602
+ exports.BlendMode = BlendMode;
603
+ exports.Decoder = Decoder;
604
+ exports.ExportFrameMode = ExportFrameMode;
605
+ exports.RGBNDecoder = RGBNDecoder;
606
+ exports.blendModeNewName = blendModeNewName;
607
+ exports.decodeTile = decodeTile;
608
+ exports.getRGBValue = getRGBValue;
609
+ exports.tileIndexIsPartOfFrame = tileIndexIsPartOfFrame;