gb-image-decoder 1.2.1 → 1.3.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/dist/index.cjs CHANGED
@@ -58,52 +58,15 @@ const SKIP_LINE = "skip";
58
58
  const TILE_PIXEL_WIDTH = 8;
59
59
  const TILE_PIXEL_HEIGHT = 8;
60
60
  const TILES_PER_LINE = 20;
61
+ const TILES_PER_COLUMN = 18;
62
+ const FRAME_SIZE = 2;
63
+ const FRAME_TILES = FRAME_SIZE * 2;
64
+ const DEFAULT_FULL_PIXEL_HEIGHT = TILES_PER_COLUMN * TILE_PIXEL_HEIGHT;
65
+ const DEFAULT_FULL_PIXEL_WIDTH = TILES_PER_LINE * TILE_PIXEL_WIDTH;
61
66
  const BW_PALETTE = [16777215, 11184810, 5592405, 0];
62
67
  const BW_PALETTE_HEX = ["#ffffff", "#aaaaaa", "#555555", "#000000"];
63
- const BLACK_LINE = [
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
- BLACK,
83
- BLACK
84
- ];
85
- const WHITE_LINE = [
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
- WHITE,
105
- WHITE
106
- ];
68
+ const BLACK_LINE = Array(TILES_PER_LINE).fill(BLACK);
69
+ const WHITE_LINE = Array(TILES_PER_LINE).fill(WHITE);
107
70
  const defaultPalette = {
108
71
  r: [0, 84, 172, 255],
109
72
  g: [0, 84, 172, 255],
@@ -167,6 +130,18 @@ const tileIndexIsPartOfFrame = (tileIndex, imageStartLine, handleExportFrame = E
167
130
  }
168
131
  };
169
132
 
133
+ const calculateImageStartLine = (handleExportFrame, imageStartLine) => {
134
+ switch (handleExportFrame) {
135
+ case ExportFrameMode.FRAMEMODE_CROP:
136
+ return 0;
137
+ case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
138
+ case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
139
+ return 2;
140
+ case ExportFrameMode.FRAMEMODE_KEEP:
141
+ default:
142
+ return imageStartLine;
143
+ }
144
+ };
170
145
  const getRGBValue = ({
171
146
  pixels,
172
147
  index,
@@ -176,7 +151,8 @@ const getRGBValue = ({
176
151
  colorData,
177
152
  frameColorData
178
153
  }) => {
179
- const palette = tileIndexIsPartOfFrame(tileIndex, imageStartLine, handleExportFrame) ? frameColorData : colorData;
154
+ const calculatedImageStartLine = calculateImageStartLine(handleExportFrame, imageStartLine);
155
+ const palette = tileIndexIsPartOfFrame(tileIndex, calculatedImageStartLine, handleExportFrame) ? frameColorData : colorData;
180
156
  const value = palette[pixels[index]];
181
157
  return {
182
158
  // eslint-disable-next-line no-bitwise
@@ -188,6 +164,15 @@ const getRGBValue = ({
188
164
  };
189
165
  };
190
166
 
167
+ const createCanvasElement = () => {
168
+ try {
169
+ return document.createElement("canvas");
170
+ } catch (error) {
171
+ throw new Error("cannot create canvas element");
172
+ }
173
+ };
174
+ const createImageData = (rawImageData, width, height) => new ImageData(rawImageData, width, height);
175
+
191
176
  var __defProp$1 = Object.defineProperty;
192
177
  var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
193
178
  var __publicField$1 = (obj, key, value) => {
@@ -206,6 +191,8 @@ class Decoder {
206
191
  __publicField$1(this, "frameColorData");
207
192
  __publicField$1(this, "tilesPerLine");
208
193
  __publicField$1(this, "imageStartLine");
194
+ __publicField$1(this, "canvasCreator");
195
+ __publicField$1(this, "imageDataCreator");
209
196
  this.canvas = null;
210
197
  this.tiles = [];
211
198
  this.colors = [];
@@ -216,6 +203,8 @@ class Decoder {
216
203
  this.frameColorData = [...BW_PALETTE];
217
204
  this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
218
205
  this.imageStartLine = 2;
206
+ this.canvasCreator = options?.canvasCreator || createCanvasElement;
207
+ this.imageDataCreator = options?.imageDataCreator || createImageData;
219
208
  }
220
209
  update({
221
210
  canvas,
@@ -226,12 +215,14 @@ class Decoder {
226
215
  }) {
227
216
  const startLineChanged = this.setImageStartLine(imageStartLine);
228
217
  const canvasChanged = canvas ? this.setCanvas(canvas) : false;
229
- const palettesChanged = this.setPalettes(palette, framePalette);
218
+ const usedFramePalette = this.tilesPerLine === TILES_PER_LINE ? framePalette : palette;
219
+ const palettesChanged = this.setPalettes(palette, usedFramePalette);
230
220
  if (startLineChanged || canvasChanged || palettesChanged || !this.tiles.length) {
231
221
  this.tiles = [];
232
222
  }
233
223
  const tilesChanged = this.setTiles(tiles);
234
224
  const newHeight = this.getHeight();
225
+ const newWidth = this.getWidth();
235
226
  if (!this.canvas) {
236
227
  return;
237
228
  }
@@ -239,9 +230,10 @@ class Decoder {
239
230
  this.canvas.height = 0;
240
231
  return;
241
232
  }
242
- if (this.canvas.height !== newHeight || !this.rawImageData?.length) {
233
+ if (this.canvas.height !== newHeight || this.canvas.width !== newWidth || !this.rawImageData?.length) {
243
234
  this.canvas.height = newHeight;
244
- const newRawImageData = new Uint8ClampedArray(this.tilesPerLine * TILE_PIXEL_WIDTH * newHeight * 4);
235
+ this.canvas.width = newWidth;
236
+ const newRawImageData = new Uint8ClampedArray(newWidth * newHeight * 4);
245
237
  this.rawImageData?.forEach((value, index) => {
246
238
  newRawImageData[index] = value;
247
239
  });
@@ -250,7 +242,7 @@ class Decoder {
250
242
  tilesChanged.forEach(({ index, newTile }) => {
251
243
  this.renderTile(index, newTile);
252
244
  });
253
- this.updateCanvas(newHeight);
245
+ this.updateCanvas(newWidth, newHeight);
254
246
  }
255
247
  setImageStartLine(imageStartLine) {
256
248
  if (this.imageStartLine === imageStartLine) {
@@ -259,22 +251,25 @@ class Decoder {
259
251
  this.imageStartLine = imageStartLine;
260
252
  return true;
261
253
  }
262
- updateCanvas(newHeight) {
254
+ updateCanvas(newWidth, newHeight) {
263
255
  if (!this.canvas || !this.rawImageData?.length) {
264
256
  return;
265
257
  }
266
258
  const context = this.canvas.getContext("2d");
267
- const imageData = new ImageData(this.rawImageData, this.tilesPerLine * TILE_PIXEL_WIDTH, newHeight);
259
+ const imageData = this.imageDataCreator(this.rawImageData, newWidth, newHeight);
268
260
  context?.putImageData(imageData, 0, 0);
269
261
  }
270
262
  getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
271
- const handleFrameMode = this.tiles.length === 360 ? handleExportFrame : ExportFrameMode.FRAMEMODE_KEEP;
263
+ let handleFrameMode = handleExportFrame;
264
+ if (this.tilesPerLine !== TILES_PER_LINE && handleFrameMode !== ExportFrameMode.FRAMEMODE_KEEP) {
265
+ handleFrameMode = ExportFrameMode.FRAMEMODE_KEEP;
266
+ }
272
267
  const {
273
268
  initialHeight,
274
269
  initialWidth,
275
270
  tilesPerLine
276
- } = this.getScaledCanvasSize(handleFrameMode, this.getHeight());
277
- const canvas = document.createElement("canvas");
271
+ } = this.getScaledCanvasSize(handleFrameMode);
272
+ const canvas = this.canvasCreator();
278
273
  const context = canvas.getContext("2d");
279
274
  if (!context) {
280
275
  throw new Error("no canvas context");
@@ -282,30 +277,31 @@ class Decoder {
282
277
  canvas.width = initialWidth * scaleFactor;
283
278
  canvas.height = initialHeight * scaleFactor;
284
279
  this.getExportTiles(handleFrameMode).forEach((tile, index) => {
285
- this.paintTileScaled(decodeTile(tile), index, context, scaleFactor, tilesPerLine, handleExportFrame);
280
+ this.paintTileScaled(decodeTile(tile), index, context, scaleFactor, tilesPerLine, handleFrameMode);
286
281
  });
287
282
  return canvas;
288
283
  }
289
- getScaledCanvasSize(handleExportFrame, height) {
290
- const FRAME_TILES = 4;
284
+ getScaledCanvasSize(handleExportFrame) {
285
+ const width = this.getWidth();
286
+ const height = this.getHeight();
291
287
  switch (handleExportFrame) {
292
288
  case ExportFrameMode.FRAMEMODE_KEEP:
293
289
  return {
294
290
  initialHeight: height,
295
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH,
291
+ initialWidth: width,
296
292
  tilesPerLine: this.tilesPerLine
297
293
  };
298
294
  case ExportFrameMode.FRAMEMODE_CROP:
299
295
  return {
300
- initialHeight: height - TILE_PIXEL_HEIGHT * FRAME_TILES,
301
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH - TILE_PIXEL_WIDTH * FRAME_TILES,
302
- tilesPerLine: this.tilesPerLine - FRAME_TILES
296
+ initialHeight: DEFAULT_FULL_PIXEL_HEIGHT - TILE_PIXEL_HEIGHT * FRAME_TILES,
297
+ initialWidth: DEFAULT_FULL_PIXEL_WIDTH - TILE_PIXEL_WIDTH * FRAME_TILES,
298
+ tilesPerLine: TILES_PER_LINE - FRAME_TILES
303
299
  };
304
300
  case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
305
301
  case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
306
302
  return {
307
- initialHeight: height + 2 * TILE_PIXEL_HEIGHT,
308
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH,
303
+ initialHeight: width,
304
+ initialWidth: width,
309
305
  tilesPerLine: this.tilesPerLine
310
306
  };
311
307
  default:
@@ -320,23 +316,34 @@ class Decoder {
320
316
  case ExportFrameMode.FRAMEMODE_KEEP:
321
317
  return this.tiles;
322
318
  case ExportFrameMode.FRAMEMODE_CROP:
323
- return this.tiles.reduce((acc, tile, index) => tileIndexIsPartOfFrame(index, this.imageStartLine, ExportFrameMode.FRAMEMODE_KEEP) ? acc : [...acc, tile], []);
319
+ return this.getCroppedTiles();
324
320
  case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
325
321
  return [
326
322
  ...BLACK_LINE,
327
- ...this.tiles,
323
+ ...this.getDefaultImageRange(),
328
324
  ...BLACK_LINE
329
325
  ];
330
326
  case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
331
327
  return [
332
328
  ...WHITE_LINE,
333
- ...this.tiles,
329
+ ...this.getDefaultImageRange(),
334
330
  ...WHITE_LINE
335
331
  ];
336
332
  default:
337
333
  throw new Error(`unknown export mode ${handleExportFrame}`);
338
334
  }
339
335
  }
336
+ getCroppedTiles() {
337
+ return this.tiles.reduce((acc, tile, index) => tileIndexIsPartOfFrame(index, this.imageStartLine, ExportFrameMode.FRAMEMODE_KEEP) ? acc : [...acc, tile], []);
338
+ }
339
+ // for wild frame image, this returns the part of theimage
340
+ // which has the image data in the default position by cropping
341
+ // away part of the wild frame which does not fit into 160x144
342
+ getDefaultImageRange() {
343
+ const wholeImageStartLine = this.imageStartLine - FRAME_SIZE;
344
+ const startIndex = wholeImageStartLine * TILES_PER_LINE;
345
+ return this.tiles.slice(startIndex, startIndex + TILES_PER_LINE * TILES_PER_COLUMN);
346
+ }
340
347
  setCanvas(canvas) {
341
348
  if (this.canvas === canvas) {
342
349
  return false;
@@ -447,6 +454,9 @@ class Decoder {
447
454
  getHeight() {
448
455
  return TILE_PIXEL_HEIGHT * Math.ceil(this.tiles.length / this.tilesPerLine);
449
456
  }
457
+ getWidth() {
458
+ return TILE_PIXEL_WIDTH * this.tilesPerLine;
459
+ }
450
460
  }
451
461
 
452
462
  const hx2 = (n) => (n || 0).toString(16).padStart(2, "0");
@@ -463,9 +473,9 @@ var __publicField = (obj, key, value) => {
463
473
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
464
474
  return value;
465
475
  };
466
- const createChannel = (key, tilesPerLine) => {
467
- const canvas = document.createElement("canvas");
468
- const decoder = new Decoder({ tilesPerLine });
476
+ const createChannel = (key, decoderOptions) => {
477
+ const canvas = decoderOptions.canvasCreator();
478
+ const decoder = new Decoder(decoderOptions);
469
479
  decoder.update({
470
480
  framePalette: [],
471
481
  canvas,
@@ -485,15 +495,24 @@ class RGBNDecoder {
485
495
  __publicField(this, "lockFrame");
486
496
  __publicField(this, "channels");
487
497
  __publicField(this, "tilesPerLine");
498
+ __publicField(this, "canvasCreator");
499
+ __publicField(this, "imageDataCreator");
488
500
  this.canvas = null;
489
501
  this.palette = defaultPalette;
490
502
  this.lockFrame = false;
491
503
  this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
504
+ this.canvasCreator = options?.canvasCreator || createCanvasElement;
505
+ this.imageDataCreator = options?.imageDataCreator || createImageData;
506
+ const channelDecoderOptions = {
507
+ tilesPerLine: this.tilesPerLine,
508
+ canvasCreator: this.canvasCreator,
509
+ imageDataCreator: this.imageDataCreator
510
+ };
492
511
  this.channels = {
493
- r: createChannel(ChannelKey.R, this.tilesPerLine),
494
- g: createChannel(ChannelKey.G, this.tilesPerLine),
495
- b: createChannel(ChannelKey.B, this.tilesPerLine),
496
- n: createChannel(ChannelKey.N, this.tilesPerLine)
512
+ r: createChannel(ChannelKey.R, channelDecoderOptions),
513
+ g: createChannel(ChannelKey.G, channelDecoderOptions),
514
+ b: createChannel(ChannelKey.B, channelDecoderOptions),
515
+ n: createChannel(ChannelKey.N, channelDecoderOptions)
497
516
  };
498
517
  }
499
518
  update({
@@ -507,7 +526,7 @@ class RGBNDecoder {
507
526
  const lockFrameChanged = this.setLockFrame(lockFrame);
508
527
  const shouldUpdate = canvasChanged || paletteChanged || lockFrameChanged;
509
528
  const canvases = this.setTiles(tiles);
510
- const newHeight = this.getHeight();
529
+ const { width: newWidth, height: newHeight } = this.getDimensions(canvases);
511
530
  if (!shouldUpdate) {
512
531
  return;
513
532
  }
@@ -521,6 +540,9 @@ class RGBNDecoder {
521
540
  if (this.canvas.height !== newHeight) {
522
541
  this.canvas.height = newHeight;
523
542
  }
543
+ if (this.canvas.width !== newWidth) {
544
+ this.canvas.width = newWidth;
545
+ }
524
546
  const context = this.canvas?.getContext("2d");
525
547
  if (!context) {
526
548
  return;
@@ -595,7 +617,7 @@ class RGBNDecoder {
595
617
  });
596
618
  }
597
619
  getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
598
- const canvas = document.createElement("canvas");
620
+ const canvas = this.canvasCreator();
599
621
  const canvases = channels.reduce((acc, key) => {
600
622
  const channel = this.channels[key];
601
623
  const channelCanvas = channel.decoder.getScaledCanvas(scaleFactor, handleExportFrame);
@@ -604,27 +626,28 @@ class RGBNDecoder {
604
626
  [key]: channelCanvas
605
627
  };
606
628
  }, {});
607
- const { width, height } = Object.values(canvases).reduce((acc, current) => ({
608
- width: Math.max(current.width, acc.width),
609
- height: Math.max(current.height, acc.height)
610
- }), { width: 0, height: 0 });
629
+ const { width, height } = this.getDimensions(canvases);
611
630
  canvas.width = width;
612
631
  canvas.height = height;
613
632
  const context = canvas.getContext("2d");
614
633
  if (!context) {
615
634
  throw new Error("no canvas context");
616
635
  }
636
+ context.fillStyle = "#000000";
637
+ context.fillRect(0, 0, 500, 500);
617
638
  this.blendCanvases(context, canvases);
618
639
  return canvas;
619
640
  }
620
- maxTiles() {
621
- return channels.reduce((acc, key) => {
622
- const channel = this.channels[key];
623
- return Math.max(acc, channel.tiles?.length || 0);
624
- }, 0);
625
- }
626
- getHeight() {
627
- return TILE_PIXEL_HEIGHT * Math.ceil(this.maxTiles() / this.tilesPerLine);
641
+ getDimensions(canvases) {
642
+ return Object.values(canvases).reduce((acc, current) => {
643
+ if (current.width === 0 || current.height === 0) {
644
+ return acc;
645
+ }
646
+ return {
647
+ width: Math.max(current.width, acc.width),
648
+ height: Math.max(current.height, acc.height)
649
+ };
650
+ }, { width: 0, height: 0 });
628
651
  }
629
652
  }
630
653
 
@@ -633,6 +656,7 @@ const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]
633
656
  exports.BW_PALETTE = BW_PALETTE;
634
657
  exports.BW_PALETTE_HEX = BW_PALETTE_HEX;
635
658
  exports.BlendMode = BlendMode;
659
+ exports.ChannelKey = ChannelKey;
636
660
  exports.Decoder = Decoder;
637
661
  exports.ExportFrameMode = ExportFrameMode;
638
662
  exports.RGBNDecoder = RGBNDecoder;
package/dist/index.d.cts CHANGED
@@ -38,6 +38,8 @@ interface Channel<DecoderType> {
38
38
  type RGBNTiles = Partial<Record<ChannelKey, string[]>>;
39
39
  type Channels<DecoderType> = Record<ChannelKey, Channel<DecoderType>>;
40
40
  type SourceCanvases = Partial<Record<ChannelKey, HTMLCanvasElement>>;
41
+ type CanvasCreator = () => HTMLCanvasElement;
42
+ type ImageDataCreator = (rawImageData: Uint8ClampedArray, width: number, height: number) => ImageData;
41
43
  interface ChangedTile {
42
44
  index: number;
43
45
  newTile: string;
@@ -63,6 +65,8 @@ interface RGBNPalette {
63
65
  }
64
66
  interface DecoderOptions {
65
67
  tilesPerLine?: number;
68
+ canvasCreator?: CanvasCreator;
69
+ imageDataCreator?: ImageDataCreator;
66
70
  }
67
71
  interface DecoderUpdateParams {
68
72
  canvas: HTMLCanvasElement | null;
@@ -89,6 +93,8 @@ declare class Decoder {
89
93
  private frameColorData;
90
94
  private tilesPerLine;
91
95
  private imageStartLine;
96
+ private canvasCreator;
97
+ private imageDataCreator;
92
98
  constructor(options?: DecoderOptions);
93
99
  update({ canvas, tiles, palette, framePalette, imageStartLine, }: DecoderUpdateParams): void;
94
100
  private setImageStartLine;
@@ -96,6 +102,8 @@ declare class Decoder {
96
102
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
97
103
  private getScaledCanvasSize;
98
104
  private getExportTiles;
105
+ private getCroppedTiles;
106
+ private getDefaultImageRange;
99
107
  private setCanvas;
100
108
  private setPalettes;
101
109
  private setTiles;
@@ -103,6 +111,7 @@ declare class Decoder {
103
111
  private paintTile;
104
112
  private paintTileScaled;
105
113
  private getHeight;
114
+ private getWidth;
106
115
  }
107
116
 
108
117
  declare class RGBNDecoder {
@@ -111,6 +120,8 @@ declare class RGBNDecoder {
111
120
  private lockFrame;
112
121
  private channels;
113
122
  private tilesPerLine;
123
+ private canvasCreator;
124
+ private imageDataCreator;
114
125
  constructor(options?: DecoderOptions);
115
126
  update({ canvas, tiles, palette, lockFrame, }: RGBNDecoderUpdateParams): void;
116
127
  private setPalette;
@@ -119,8 +130,7 @@ declare class RGBNDecoder {
119
130
  private setTiles;
120
131
  private blendCanvases;
121
132
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
122
- private maxTiles;
123
- private getHeight;
133
+ private getDimensions;
124
134
  }
125
135
 
126
136
  declare const decodeTile: (rawBytes?: string) => IndexedTilePixels;
@@ -143,4 +153,4 @@ declare const SKIP_LINE = "skip";
143
153
  declare const BW_PALETTE: number[];
144
154
  declare const BW_PALETTE_HEX: string[];
145
155
 
146
- export { type BWPalette, BW_PALETTE, BW_PALETTE_HEX, BlendMode, type ChangedTile, type Channel, type Channels, Decoder, type DecoderOptions, type DecoderUpdateParams, ExportFrameMode, type IndexedTilePixels, RGBNDecoder, type RGBNDecoderUpdateParams, type RGBNPalette, type RGBNTiles, type RGBValue, SKIP_LINE, type ScaledCanvasSize, type SourceCanvases, blendModeNewName, decodeTile, getRGBValue, maxTiles, tileIndexIsPartOfFrame };
156
+ export { type BWPalette, BW_PALETTE, BW_PALETTE_HEX, BlendMode, type CanvasCreator, type ChangedTile, type Channel, ChannelKey, type Channels, Decoder, type DecoderOptions, type DecoderUpdateParams, ExportFrameMode, type ImageDataCreator, type IndexedTilePixels, RGBNDecoder, type RGBNDecoderUpdateParams, type RGBNPalette, type RGBNTiles, type RGBValue, SKIP_LINE, type ScaledCanvasSize, type SourceCanvases, blendModeNewName, decodeTile, getRGBValue, maxTiles, tileIndexIsPartOfFrame };
package/dist/index.d.mts CHANGED
@@ -38,6 +38,8 @@ interface Channel<DecoderType> {
38
38
  type RGBNTiles = Partial<Record<ChannelKey, string[]>>;
39
39
  type Channels<DecoderType> = Record<ChannelKey, Channel<DecoderType>>;
40
40
  type SourceCanvases = Partial<Record<ChannelKey, HTMLCanvasElement>>;
41
+ type CanvasCreator = () => HTMLCanvasElement;
42
+ type ImageDataCreator = (rawImageData: Uint8ClampedArray, width: number, height: number) => ImageData;
41
43
  interface ChangedTile {
42
44
  index: number;
43
45
  newTile: string;
@@ -63,6 +65,8 @@ interface RGBNPalette {
63
65
  }
64
66
  interface DecoderOptions {
65
67
  tilesPerLine?: number;
68
+ canvasCreator?: CanvasCreator;
69
+ imageDataCreator?: ImageDataCreator;
66
70
  }
67
71
  interface DecoderUpdateParams {
68
72
  canvas: HTMLCanvasElement | null;
@@ -89,6 +93,8 @@ declare class Decoder {
89
93
  private frameColorData;
90
94
  private tilesPerLine;
91
95
  private imageStartLine;
96
+ private canvasCreator;
97
+ private imageDataCreator;
92
98
  constructor(options?: DecoderOptions);
93
99
  update({ canvas, tiles, palette, framePalette, imageStartLine, }: DecoderUpdateParams): void;
94
100
  private setImageStartLine;
@@ -96,6 +102,8 @@ declare class Decoder {
96
102
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
97
103
  private getScaledCanvasSize;
98
104
  private getExportTiles;
105
+ private getCroppedTiles;
106
+ private getDefaultImageRange;
99
107
  private setCanvas;
100
108
  private setPalettes;
101
109
  private setTiles;
@@ -103,6 +111,7 @@ declare class Decoder {
103
111
  private paintTile;
104
112
  private paintTileScaled;
105
113
  private getHeight;
114
+ private getWidth;
106
115
  }
107
116
 
108
117
  declare class RGBNDecoder {
@@ -111,6 +120,8 @@ declare class RGBNDecoder {
111
120
  private lockFrame;
112
121
  private channels;
113
122
  private tilesPerLine;
123
+ private canvasCreator;
124
+ private imageDataCreator;
114
125
  constructor(options?: DecoderOptions);
115
126
  update({ canvas, tiles, palette, lockFrame, }: RGBNDecoderUpdateParams): void;
116
127
  private setPalette;
@@ -119,8 +130,7 @@ declare class RGBNDecoder {
119
130
  private setTiles;
120
131
  private blendCanvases;
121
132
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
122
- private maxTiles;
123
- private getHeight;
133
+ private getDimensions;
124
134
  }
125
135
 
126
136
  declare const decodeTile: (rawBytes?: string) => IndexedTilePixels;
@@ -143,4 +153,4 @@ declare const SKIP_LINE = "skip";
143
153
  declare const BW_PALETTE: number[];
144
154
  declare const BW_PALETTE_HEX: string[];
145
155
 
146
- export { type BWPalette, BW_PALETTE, BW_PALETTE_HEX, BlendMode, type ChangedTile, type Channel, type Channels, Decoder, type DecoderOptions, type DecoderUpdateParams, ExportFrameMode, type IndexedTilePixels, RGBNDecoder, type RGBNDecoderUpdateParams, type RGBNPalette, type RGBNTiles, type RGBValue, SKIP_LINE, type ScaledCanvasSize, type SourceCanvases, blendModeNewName, decodeTile, getRGBValue, maxTiles, tileIndexIsPartOfFrame };
156
+ export { type BWPalette, BW_PALETTE, BW_PALETTE_HEX, BlendMode, type CanvasCreator, type ChangedTile, type Channel, ChannelKey, type Channels, Decoder, type DecoderOptions, type DecoderUpdateParams, ExportFrameMode, type ImageDataCreator, type IndexedTilePixels, RGBNDecoder, type RGBNDecoderUpdateParams, type RGBNPalette, type RGBNTiles, type RGBValue, SKIP_LINE, type ScaledCanvasSize, type SourceCanvases, blendModeNewName, decodeTile, getRGBValue, maxTiles, tileIndexIsPartOfFrame };
package/dist/index.d.ts CHANGED
@@ -38,6 +38,8 @@ interface Channel<DecoderType> {
38
38
  type RGBNTiles = Partial<Record<ChannelKey, string[]>>;
39
39
  type Channels<DecoderType> = Record<ChannelKey, Channel<DecoderType>>;
40
40
  type SourceCanvases = Partial<Record<ChannelKey, HTMLCanvasElement>>;
41
+ type CanvasCreator = () => HTMLCanvasElement;
42
+ type ImageDataCreator = (rawImageData: Uint8ClampedArray, width: number, height: number) => ImageData;
41
43
  interface ChangedTile {
42
44
  index: number;
43
45
  newTile: string;
@@ -63,6 +65,8 @@ interface RGBNPalette {
63
65
  }
64
66
  interface DecoderOptions {
65
67
  tilesPerLine?: number;
68
+ canvasCreator?: CanvasCreator;
69
+ imageDataCreator?: ImageDataCreator;
66
70
  }
67
71
  interface DecoderUpdateParams {
68
72
  canvas: HTMLCanvasElement | null;
@@ -89,6 +93,8 @@ declare class Decoder {
89
93
  private frameColorData;
90
94
  private tilesPerLine;
91
95
  private imageStartLine;
96
+ private canvasCreator;
97
+ private imageDataCreator;
92
98
  constructor(options?: DecoderOptions);
93
99
  update({ canvas, tiles, palette, framePalette, imageStartLine, }: DecoderUpdateParams): void;
94
100
  private setImageStartLine;
@@ -96,6 +102,8 @@ declare class Decoder {
96
102
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
97
103
  private getScaledCanvasSize;
98
104
  private getExportTiles;
105
+ private getCroppedTiles;
106
+ private getDefaultImageRange;
99
107
  private setCanvas;
100
108
  private setPalettes;
101
109
  private setTiles;
@@ -103,6 +111,7 @@ declare class Decoder {
103
111
  private paintTile;
104
112
  private paintTileScaled;
105
113
  private getHeight;
114
+ private getWidth;
106
115
  }
107
116
 
108
117
  declare class RGBNDecoder {
@@ -111,6 +120,8 @@ declare class RGBNDecoder {
111
120
  private lockFrame;
112
121
  private channels;
113
122
  private tilesPerLine;
123
+ private canvasCreator;
124
+ private imageDataCreator;
114
125
  constructor(options?: DecoderOptions);
115
126
  update({ canvas, tiles, palette, lockFrame, }: RGBNDecoderUpdateParams): void;
116
127
  private setPalette;
@@ -119,8 +130,7 @@ declare class RGBNDecoder {
119
130
  private setTiles;
120
131
  private blendCanvases;
121
132
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
122
- private maxTiles;
123
- private getHeight;
133
+ private getDimensions;
124
134
  }
125
135
 
126
136
  declare const decodeTile: (rawBytes?: string) => IndexedTilePixels;
@@ -143,4 +153,4 @@ declare const SKIP_LINE = "skip";
143
153
  declare const BW_PALETTE: number[];
144
154
  declare const BW_PALETTE_HEX: string[];
145
155
 
146
- export { type BWPalette, BW_PALETTE, BW_PALETTE_HEX, BlendMode, type ChangedTile, type Channel, type Channels, Decoder, type DecoderOptions, type DecoderUpdateParams, ExportFrameMode, type IndexedTilePixels, RGBNDecoder, type RGBNDecoderUpdateParams, type RGBNPalette, type RGBNTiles, type RGBValue, SKIP_LINE, type ScaledCanvasSize, type SourceCanvases, blendModeNewName, decodeTile, getRGBValue, maxTiles, tileIndexIsPartOfFrame };
156
+ export { type BWPalette, BW_PALETTE, BW_PALETTE_HEX, BlendMode, type CanvasCreator, type ChangedTile, type Channel, ChannelKey, type Channels, Decoder, type DecoderOptions, type DecoderUpdateParams, ExportFrameMode, type ImageDataCreator, type IndexedTilePixels, RGBNDecoder, type RGBNDecoderUpdateParams, type RGBNPalette, type RGBNTiles, type RGBValue, SKIP_LINE, type ScaledCanvasSize, type SourceCanvases, blendModeNewName, decodeTile, getRGBValue, maxTiles, tileIndexIsPartOfFrame };
package/dist/index.mjs CHANGED
@@ -56,52 +56,15 @@ const SKIP_LINE = "skip";
56
56
  const TILE_PIXEL_WIDTH = 8;
57
57
  const TILE_PIXEL_HEIGHT = 8;
58
58
  const TILES_PER_LINE = 20;
59
+ const TILES_PER_COLUMN = 18;
60
+ const FRAME_SIZE = 2;
61
+ const FRAME_TILES = FRAME_SIZE * 2;
62
+ const DEFAULT_FULL_PIXEL_HEIGHT = TILES_PER_COLUMN * TILE_PIXEL_HEIGHT;
63
+ const DEFAULT_FULL_PIXEL_WIDTH = TILES_PER_LINE * TILE_PIXEL_WIDTH;
59
64
  const BW_PALETTE = [16777215, 11184810, 5592405, 0];
60
65
  const BW_PALETTE_HEX = ["#ffffff", "#aaaaaa", "#555555", "#000000"];
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
- ];
66
+ const BLACK_LINE = Array(TILES_PER_LINE).fill(BLACK);
67
+ const WHITE_LINE = Array(TILES_PER_LINE).fill(WHITE);
105
68
  const defaultPalette = {
106
69
  r: [0, 84, 172, 255],
107
70
  g: [0, 84, 172, 255],
@@ -165,6 +128,18 @@ const tileIndexIsPartOfFrame = (tileIndex, imageStartLine, handleExportFrame = E
165
128
  }
166
129
  };
167
130
 
131
+ const calculateImageStartLine = (handleExportFrame, imageStartLine) => {
132
+ switch (handleExportFrame) {
133
+ case ExportFrameMode.FRAMEMODE_CROP:
134
+ return 0;
135
+ case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
136
+ case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
137
+ return 2;
138
+ case ExportFrameMode.FRAMEMODE_KEEP:
139
+ default:
140
+ return imageStartLine;
141
+ }
142
+ };
168
143
  const getRGBValue = ({
169
144
  pixels,
170
145
  index,
@@ -174,7 +149,8 @@ const getRGBValue = ({
174
149
  colorData,
175
150
  frameColorData
176
151
  }) => {
177
- const palette = tileIndexIsPartOfFrame(tileIndex, imageStartLine, handleExportFrame) ? frameColorData : colorData;
152
+ const calculatedImageStartLine = calculateImageStartLine(handleExportFrame, imageStartLine);
153
+ const palette = tileIndexIsPartOfFrame(tileIndex, calculatedImageStartLine, handleExportFrame) ? frameColorData : colorData;
178
154
  const value = palette[pixels[index]];
179
155
  return {
180
156
  // eslint-disable-next-line no-bitwise
@@ -186,6 +162,15 @@ const getRGBValue = ({
186
162
  };
187
163
  };
188
164
 
165
+ const createCanvasElement = () => {
166
+ try {
167
+ return document.createElement("canvas");
168
+ } catch (error) {
169
+ throw new Error("cannot create canvas element");
170
+ }
171
+ };
172
+ const createImageData = (rawImageData, width, height) => new ImageData(rawImageData, width, height);
173
+
189
174
  var __defProp$1 = Object.defineProperty;
190
175
  var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
191
176
  var __publicField$1 = (obj, key, value) => {
@@ -204,6 +189,8 @@ class Decoder {
204
189
  __publicField$1(this, "frameColorData");
205
190
  __publicField$1(this, "tilesPerLine");
206
191
  __publicField$1(this, "imageStartLine");
192
+ __publicField$1(this, "canvasCreator");
193
+ __publicField$1(this, "imageDataCreator");
207
194
  this.canvas = null;
208
195
  this.tiles = [];
209
196
  this.colors = [];
@@ -214,6 +201,8 @@ class Decoder {
214
201
  this.frameColorData = [...BW_PALETTE];
215
202
  this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
216
203
  this.imageStartLine = 2;
204
+ this.canvasCreator = options?.canvasCreator || createCanvasElement;
205
+ this.imageDataCreator = options?.imageDataCreator || createImageData;
217
206
  }
218
207
  update({
219
208
  canvas,
@@ -224,12 +213,14 @@ class Decoder {
224
213
  }) {
225
214
  const startLineChanged = this.setImageStartLine(imageStartLine);
226
215
  const canvasChanged = canvas ? this.setCanvas(canvas) : false;
227
- const palettesChanged = this.setPalettes(palette, framePalette);
216
+ const usedFramePalette = this.tilesPerLine === TILES_PER_LINE ? framePalette : palette;
217
+ const palettesChanged = this.setPalettes(palette, usedFramePalette);
228
218
  if (startLineChanged || canvasChanged || palettesChanged || !this.tiles.length) {
229
219
  this.tiles = [];
230
220
  }
231
221
  const tilesChanged = this.setTiles(tiles);
232
222
  const newHeight = this.getHeight();
223
+ const newWidth = this.getWidth();
233
224
  if (!this.canvas) {
234
225
  return;
235
226
  }
@@ -237,9 +228,10 @@ class Decoder {
237
228
  this.canvas.height = 0;
238
229
  return;
239
230
  }
240
- if (this.canvas.height !== newHeight || !this.rawImageData?.length) {
231
+ if (this.canvas.height !== newHeight || this.canvas.width !== newWidth || !this.rawImageData?.length) {
241
232
  this.canvas.height = newHeight;
242
- const newRawImageData = new Uint8ClampedArray(this.tilesPerLine * TILE_PIXEL_WIDTH * newHeight * 4);
233
+ this.canvas.width = newWidth;
234
+ const newRawImageData = new Uint8ClampedArray(newWidth * newHeight * 4);
243
235
  this.rawImageData?.forEach((value, index) => {
244
236
  newRawImageData[index] = value;
245
237
  });
@@ -248,7 +240,7 @@ class Decoder {
248
240
  tilesChanged.forEach(({ index, newTile }) => {
249
241
  this.renderTile(index, newTile);
250
242
  });
251
- this.updateCanvas(newHeight);
243
+ this.updateCanvas(newWidth, newHeight);
252
244
  }
253
245
  setImageStartLine(imageStartLine) {
254
246
  if (this.imageStartLine === imageStartLine) {
@@ -257,22 +249,25 @@ class Decoder {
257
249
  this.imageStartLine = imageStartLine;
258
250
  return true;
259
251
  }
260
- updateCanvas(newHeight) {
252
+ updateCanvas(newWidth, newHeight) {
261
253
  if (!this.canvas || !this.rawImageData?.length) {
262
254
  return;
263
255
  }
264
256
  const context = this.canvas.getContext("2d");
265
- const imageData = new ImageData(this.rawImageData, this.tilesPerLine * TILE_PIXEL_WIDTH, newHeight);
257
+ const imageData = this.imageDataCreator(this.rawImageData, newWidth, newHeight);
266
258
  context?.putImageData(imageData, 0, 0);
267
259
  }
268
260
  getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
269
- const handleFrameMode = this.tiles.length === 360 ? handleExportFrame : ExportFrameMode.FRAMEMODE_KEEP;
261
+ let handleFrameMode = handleExportFrame;
262
+ if (this.tilesPerLine !== TILES_PER_LINE && handleFrameMode !== ExportFrameMode.FRAMEMODE_KEEP) {
263
+ handleFrameMode = ExportFrameMode.FRAMEMODE_KEEP;
264
+ }
270
265
  const {
271
266
  initialHeight,
272
267
  initialWidth,
273
268
  tilesPerLine
274
- } = this.getScaledCanvasSize(handleFrameMode, this.getHeight());
275
- const canvas = document.createElement("canvas");
269
+ } = this.getScaledCanvasSize(handleFrameMode);
270
+ const canvas = this.canvasCreator();
276
271
  const context = canvas.getContext("2d");
277
272
  if (!context) {
278
273
  throw new Error("no canvas context");
@@ -280,30 +275,31 @@ class Decoder {
280
275
  canvas.width = initialWidth * scaleFactor;
281
276
  canvas.height = initialHeight * scaleFactor;
282
277
  this.getExportTiles(handleFrameMode).forEach((tile, index) => {
283
- this.paintTileScaled(decodeTile(tile), index, context, scaleFactor, tilesPerLine, handleExportFrame);
278
+ this.paintTileScaled(decodeTile(tile), index, context, scaleFactor, tilesPerLine, handleFrameMode);
284
279
  });
285
280
  return canvas;
286
281
  }
287
- getScaledCanvasSize(handleExportFrame, height) {
288
- const FRAME_TILES = 4;
282
+ getScaledCanvasSize(handleExportFrame) {
283
+ const width = this.getWidth();
284
+ const height = this.getHeight();
289
285
  switch (handleExportFrame) {
290
286
  case ExportFrameMode.FRAMEMODE_KEEP:
291
287
  return {
292
288
  initialHeight: height,
293
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH,
289
+ initialWidth: width,
294
290
  tilesPerLine: this.tilesPerLine
295
291
  };
296
292
  case ExportFrameMode.FRAMEMODE_CROP:
297
293
  return {
298
- initialHeight: height - TILE_PIXEL_HEIGHT * FRAME_TILES,
299
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH - TILE_PIXEL_WIDTH * FRAME_TILES,
300
- tilesPerLine: this.tilesPerLine - FRAME_TILES
294
+ initialHeight: DEFAULT_FULL_PIXEL_HEIGHT - TILE_PIXEL_HEIGHT * FRAME_TILES,
295
+ initialWidth: DEFAULT_FULL_PIXEL_WIDTH - TILE_PIXEL_WIDTH * FRAME_TILES,
296
+ tilesPerLine: TILES_PER_LINE - FRAME_TILES
301
297
  };
302
298
  case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
303
299
  case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
304
300
  return {
305
- initialHeight: height + 2 * TILE_PIXEL_HEIGHT,
306
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH,
301
+ initialHeight: width,
302
+ initialWidth: width,
307
303
  tilesPerLine: this.tilesPerLine
308
304
  };
309
305
  default:
@@ -318,23 +314,34 @@ class Decoder {
318
314
  case ExportFrameMode.FRAMEMODE_KEEP:
319
315
  return this.tiles;
320
316
  case ExportFrameMode.FRAMEMODE_CROP:
321
- return this.tiles.reduce((acc, tile, index) => tileIndexIsPartOfFrame(index, this.imageStartLine, ExportFrameMode.FRAMEMODE_KEEP) ? acc : [...acc, tile], []);
317
+ return this.getCroppedTiles();
322
318
  case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
323
319
  return [
324
320
  ...BLACK_LINE,
325
- ...this.tiles,
321
+ ...this.getDefaultImageRange(),
326
322
  ...BLACK_LINE
327
323
  ];
328
324
  case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
329
325
  return [
330
326
  ...WHITE_LINE,
331
- ...this.tiles,
327
+ ...this.getDefaultImageRange(),
332
328
  ...WHITE_LINE
333
329
  ];
334
330
  default:
335
331
  throw new Error(`unknown export mode ${handleExportFrame}`);
336
332
  }
337
333
  }
334
+ getCroppedTiles() {
335
+ return this.tiles.reduce((acc, tile, index) => tileIndexIsPartOfFrame(index, this.imageStartLine, ExportFrameMode.FRAMEMODE_KEEP) ? acc : [...acc, tile], []);
336
+ }
337
+ // for wild frame image, this returns the part of theimage
338
+ // which has the image data in the default position by cropping
339
+ // away part of the wild frame which does not fit into 160x144
340
+ getDefaultImageRange() {
341
+ const wholeImageStartLine = this.imageStartLine - FRAME_SIZE;
342
+ const startIndex = wholeImageStartLine * TILES_PER_LINE;
343
+ return this.tiles.slice(startIndex, startIndex + TILES_PER_LINE * TILES_PER_COLUMN);
344
+ }
338
345
  setCanvas(canvas) {
339
346
  if (this.canvas === canvas) {
340
347
  return false;
@@ -445,6 +452,9 @@ class Decoder {
445
452
  getHeight() {
446
453
  return TILE_PIXEL_HEIGHT * Math.ceil(this.tiles.length / this.tilesPerLine);
447
454
  }
455
+ getWidth() {
456
+ return TILE_PIXEL_WIDTH * this.tilesPerLine;
457
+ }
448
458
  }
449
459
 
450
460
  const hx2 = (n) => (n || 0).toString(16).padStart(2, "0");
@@ -461,9 +471,9 @@ var __publicField = (obj, key, value) => {
461
471
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
462
472
  return value;
463
473
  };
464
- const createChannel = (key, tilesPerLine) => {
465
- const canvas = document.createElement("canvas");
466
- const decoder = new Decoder({ tilesPerLine });
474
+ const createChannel = (key, decoderOptions) => {
475
+ const canvas = decoderOptions.canvasCreator();
476
+ const decoder = new Decoder(decoderOptions);
467
477
  decoder.update({
468
478
  framePalette: [],
469
479
  canvas,
@@ -483,15 +493,24 @@ class RGBNDecoder {
483
493
  __publicField(this, "lockFrame");
484
494
  __publicField(this, "channels");
485
495
  __publicField(this, "tilesPerLine");
496
+ __publicField(this, "canvasCreator");
497
+ __publicField(this, "imageDataCreator");
486
498
  this.canvas = null;
487
499
  this.palette = defaultPalette;
488
500
  this.lockFrame = false;
489
501
  this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
502
+ this.canvasCreator = options?.canvasCreator || createCanvasElement;
503
+ this.imageDataCreator = options?.imageDataCreator || createImageData;
504
+ const channelDecoderOptions = {
505
+ tilesPerLine: this.tilesPerLine,
506
+ canvasCreator: this.canvasCreator,
507
+ imageDataCreator: this.imageDataCreator
508
+ };
490
509
  this.channels = {
491
- r: createChannel(ChannelKey.R, this.tilesPerLine),
492
- g: createChannel(ChannelKey.G, this.tilesPerLine),
493
- b: createChannel(ChannelKey.B, this.tilesPerLine),
494
- n: createChannel(ChannelKey.N, this.tilesPerLine)
510
+ r: createChannel(ChannelKey.R, channelDecoderOptions),
511
+ g: createChannel(ChannelKey.G, channelDecoderOptions),
512
+ b: createChannel(ChannelKey.B, channelDecoderOptions),
513
+ n: createChannel(ChannelKey.N, channelDecoderOptions)
495
514
  };
496
515
  }
497
516
  update({
@@ -505,7 +524,7 @@ class RGBNDecoder {
505
524
  const lockFrameChanged = this.setLockFrame(lockFrame);
506
525
  const shouldUpdate = canvasChanged || paletteChanged || lockFrameChanged;
507
526
  const canvases = this.setTiles(tiles);
508
- const newHeight = this.getHeight();
527
+ const { width: newWidth, height: newHeight } = this.getDimensions(canvases);
509
528
  if (!shouldUpdate) {
510
529
  return;
511
530
  }
@@ -519,6 +538,9 @@ class RGBNDecoder {
519
538
  if (this.canvas.height !== newHeight) {
520
539
  this.canvas.height = newHeight;
521
540
  }
541
+ if (this.canvas.width !== newWidth) {
542
+ this.canvas.width = newWidth;
543
+ }
522
544
  const context = this.canvas?.getContext("2d");
523
545
  if (!context) {
524
546
  return;
@@ -593,7 +615,7 @@ class RGBNDecoder {
593
615
  });
594
616
  }
595
617
  getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
596
- const canvas = document.createElement("canvas");
618
+ const canvas = this.canvasCreator();
597
619
  const canvases = channels.reduce((acc, key) => {
598
620
  const channel = this.channels[key];
599
621
  const channelCanvas = channel.decoder.getScaledCanvas(scaleFactor, handleExportFrame);
@@ -602,30 +624,31 @@ class RGBNDecoder {
602
624
  [key]: channelCanvas
603
625
  };
604
626
  }, {});
605
- const { width, height } = Object.values(canvases).reduce((acc, current) => ({
606
- width: Math.max(current.width, acc.width),
607
- height: Math.max(current.height, acc.height)
608
- }), { width: 0, height: 0 });
627
+ const { width, height } = this.getDimensions(canvases);
609
628
  canvas.width = width;
610
629
  canvas.height = height;
611
630
  const context = canvas.getContext("2d");
612
631
  if (!context) {
613
632
  throw new Error("no canvas context");
614
633
  }
634
+ context.fillStyle = "#000000";
635
+ context.fillRect(0, 0, 500, 500);
615
636
  this.blendCanvases(context, canvases);
616
637
  return canvas;
617
638
  }
618
- maxTiles() {
619
- return channels.reduce((acc, key) => {
620
- const channel = this.channels[key];
621
- return Math.max(acc, channel.tiles?.length || 0);
622
- }, 0);
623
- }
624
- getHeight() {
625
- return TILE_PIXEL_HEIGHT * Math.ceil(this.maxTiles() / this.tilesPerLine);
639
+ getDimensions(canvases) {
640
+ return Object.values(canvases).reduce((acc, current) => {
641
+ if (current.width === 0 || current.height === 0) {
642
+ return acc;
643
+ }
644
+ return {
645
+ width: Math.max(current.width, acc.width),
646
+ height: Math.max(current.height, acc.height)
647
+ };
648
+ }, { width: 0, height: 0 });
626
649
  }
627
650
  }
628
651
 
629
652
  const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]?.length || 0));
630
653
 
631
- export { BW_PALETTE, BW_PALETTE_HEX, BlendMode, Decoder, ExportFrameMode, RGBNDecoder, SKIP_LINE, blendModeNewName, decodeTile, getRGBValue, maxTiles, tileIndexIsPartOfFrame };
654
+ export { BW_PALETTE, BW_PALETTE_HEX, BlendMode, ChannelKey, Decoder, ExportFrameMode, RGBNDecoder, SKIP_LINE, blendModeNewName, decodeTile, getRGBValue, maxTiles, tileIndexIsPartOfFrame };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gb-image-decoder",
3
- "version": "1.2.1",
3
+ "version": "1.3.1",
4
4
  "description": "Decoder classes for GameBoy-encoded images",
5
5
  "repository": "",
6
6
  "license": "MIT",
@@ -15,22 +15,30 @@
15
15
  "main": "./dist/index.cjs",
16
16
  "module": "./dist/index.mjs",
17
17
  "types": "./dist/index.d.ts",
18
- "files": ["dist"],
18
+ "files": [
19
+ "dist"
20
+ ],
19
21
  "scripts": {
20
- "prepack": "npm run eslint && npm run build",
22
+ "prepack": "npm run eslint && npm run test && npm run build",
21
23
  "build": "unbuild",
22
24
  "dev": "vitest dev",
25
+ "test:clean": "rimraf images/*",
26
+ "test": "npm run test:clean && vitest run",
27
+ "test:u": "npm run test:clean && vitest run -u",
23
28
  "eslint": "eslint -c .eslintrc --ext .js,.ts ."
24
29
  },
25
30
  "devDependencies": {
26
31
  "@types/node": "^20.12.7",
27
32
  "@typescript-eslint/eslint-plugin": "^7.9.0",
33
+ "canvas": "^2.11.2",
28
34
  "eslint": "^8.57.0",
29
35
  "eslint-config-airbnb-base": "^15.0.0",
30
36
  "eslint-import-resolver-typescript": "^3.6.1",
31
37
  "eslint-plugin-import": "^2.29.1",
38
+ "jsdom": "^25.0.1",
39
+ "ohash": "^1.1.4",
32
40
  "typescript": "^5.4.5",
33
41
  "unbuild": "^2.0.0",
34
- "vitest": "^1.6.0"
42
+ "vitest": "^2.1.3"
35
43
  }
36
44
  }