gb-image-decoder 1.2.1 → 1.3.0

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
@@ -188,6 +188,15 @@ const getRGBValue = ({
188
188
  };
189
189
  };
190
190
 
191
+ const createCanvasElement = () => {
192
+ try {
193
+ return document.createElement("canvas");
194
+ } catch (error) {
195
+ throw new Error("cannot create canvas element");
196
+ }
197
+ };
198
+ const createImageData = (rawImageData, width, height) => new ImageData(rawImageData, width, height);
199
+
191
200
  var __defProp$1 = Object.defineProperty;
192
201
  var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
193
202
  var __publicField$1 = (obj, key, value) => {
@@ -206,6 +215,8 @@ class Decoder {
206
215
  __publicField$1(this, "frameColorData");
207
216
  __publicField$1(this, "tilesPerLine");
208
217
  __publicField$1(this, "imageStartLine");
218
+ __publicField$1(this, "canvasCreator");
219
+ __publicField$1(this, "imageDataCreator");
209
220
  this.canvas = null;
210
221
  this.tiles = [];
211
222
  this.colors = [];
@@ -216,6 +227,8 @@ class Decoder {
216
227
  this.frameColorData = [...BW_PALETTE];
217
228
  this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
218
229
  this.imageStartLine = 2;
230
+ this.canvasCreator = options?.canvasCreator || createCanvasElement;
231
+ this.imageDataCreator = options?.imageDataCreator || createImageData;
219
232
  }
220
233
  update({
221
234
  canvas,
@@ -226,12 +239,14 @@ class Decoder {
226
239
  }) {
227
240
  const startLineChanged = this.setImageStartLine(imageStartLine);
228
241
  const canvasChanged = canvas ? this.setCanvas(canvas) : false;
229
- const palettesChanged = this.setPalettes(palette, framePalette);
242
+ const usedFramePalette = this.tilesPerLine === TILES_PER_LINE ? framePalette : palette;
243
+ const palettesChanged = this.setPalettes(palette, usedFramePalette);
230
244
  if (startLineChanged || canvasChanged || palettesChanged || !this.tiles.length) {
231
245
  this.tiles = [];
232
246
  }
233
247
  const tilesChanged = this.setTiles(tiles);
234
248
  const newHeight = this.getHeight();
249
+ const newWidth = this.getWidth();
235
250
  if (!this.canvas) {
236
251
  return;
237
252
  }
@@ -239,9 +254,10 @@ class Decoder {
239
254
  this.canvas.height = 0;
240
255
  return;
241
256
  }
242
- if (this.canvas.height !== newHeight || !this.rawImageData?.length) {
257
+ if (this.canvas.height !== newHeight || this.canvas.width !== newWidth || !this.rawImageData?.length) {
243
258
  this.canvas.height = newHeight;
244
- const newRawImageData = new Uint8ClampedArray(this.tilesPerLine * TILE_PIXEL_WIDTH * newHeight * 4);
259
+ this.canvas.width = newWidth;
260
+ const newRawImageData = new Uint8ClampedArray(newWidth * newHeight * 4);
245
261
  this.rawImageData?.forEach((value, index) => {
246
262
  newRawImageData[index] = value;
247
263
  });
@@ -250,7 +266,7 @@ class Decoder {
250
266
  tilesChanged.forEach(({ index, newTile }) => {
251
267
  this.renderTile(index, newTile);
252
268
  });
253
- this.updateCanvas(newHeight);
269
+ this.updateCanvas(newWidth, newHeight);
254
270
  }
255
271
  setImageStartLine(imageStartLine) {
256
272
  if (this.imageStartLine === imageStartLine) {
@@ -259,22 +275,25 @@ class Decoder {
259
275
  this.imageStartLine = imageStartLine;
260
276
  return true;
261
277
  }
262
- updateCanvas(newHeight) {
278
+ updateCanvas(newWidth, newHeight) {
263
279
  if (!this.canvas || !this.rawImageData?.length) {
264
280
  return;
265
281
  }
266
282
  const context = this.canvas.getContext("2d");
267
- const imageData = new ImageData(this.rawImageData, this.tilesPerLine * TILE_PIXEL_WIDTH, newHeight);
283
+ const imageData = this.imageDataCreator(this.rawImageData, newWidth, newHeight);
268
284
  context?.putImageData(imageData, 0, 0);
269
285
  }
270
286
  getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
271
- const handleFrameMode = this.tiles.length === 360 ? handleExportFrame : ExportFrameMode.FRAMEMODE_KEEP;
287
+ let handleFrameMode = handleExportFrame;
288
+ if ((this.tiles.length !== 360 || this.tilesPerLine !== TILES_PER_LINE) && handleFrameMode !== ExportFrameMode.FRAMEMODE_KEEP) {
289
+ handleFrameMode = ExportFrameMode.FRAMEMODE_KEEP;
290
+ }
272
291
  const {
273
292
  initialHeight,
274
293
  initialWidth,
275
294
  tilesPerLine
276
- } = this.getScaledCanvasSize(handleFrameMode, this.getHeight());
277
- const canvas = document.createElement("canvas");
295
+ } = this.getScaledCanvasSize(handleFrameMode);
296
+ const canvas = this.canvasCreator();
278
297
  const context = canvas.getContext("2d");
279
298
  if (!context) {
280
299
  throw new Error("no canvas context");
@@ -282,30 +301,32 @@ class Decoder {
282
301
  canvas.width = initialWidth * scaleFactor;
283
302
  canvas.height = initialHeight * scaleFactor;
284
303
  this.getExportTiles(handleFrameMode).forEach((tile, index) => {
285
- this.paintTileScaled(decodeTile(tile), index, context, scaleFactor, tilesPerLine, handleExportFrame);
304
+ this.paintTileScaled(decodeTile(tile), index, context, scaleFactor, tilesPerLine, handleFrameMode);
286
305
  });
287
306
  return canvas;
288
307
  }
289
- getScaledCanvasSize(handleExportFrame, height) {
308
+ getScaledCanvasSize(handleExportFrame) {
290
309
  const FRAME_TILES = 4;
310
+ const width = this.getWidth();
311
+ const height = this.getHeight();
291
312
  switch (handleExportFrame) {
292
313
  case ExportFrameMode.FRAMEMODE_KEEP:
293
314
  return {
294
315
  initialHeight: height,
295
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH,
316
+ initialWidth: width,
296
317
  tilesPerLine: this.tilesPerLine
297
318
  };
298
319
  case ExportFrameMode.FRAMEMODE_CROP:
299
320
  return {
300
321
  initialHeight: height - TILE_PIXEL_HEIGHT * FRAME_TILES,
301
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH - TILE_PIXEL_WIDTH * FRAME_TILES,
322
+ initialWidth: width - TILE_PIXEL_WIDTH * FRAME_TILES,
302
323
  tilesPerLine: this.tilesPerLine - FRAME_TILES
303
324
  };
304
325
  case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
305
326
  case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
306
327
  return {
307
328
  initialHeight: height + 2 * TILE_PIXEL_HEIGHT,
308
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH,
329
+ initialWidth: width,
309
330
  tilesPerLine: this.tilesPerLine
310
331
  };
311
332
  default:
@@ -447,6 +468,9 @@ class Decoder {
447
468
  getHeight() {
448
469
  return TILE_PIXEL_HEIGHT * Math.ceil(this.tiles.length / this.tilesPerLine);
449
470
  }
471
+ getWidth() {
472
+ return TILE_PIXEL_WIDTH * this.tilesPerLine;
473
+ }
450
474
  }
451
475
 
452
476
  const hx2 = (n) => (n || 0).toString(16).padStart(2, "0");
@@ -463,9 +487,9 @@ var __publicField = (obj, key, value) => {
463
487
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
464
488
  return value;
465
489
  };
466
- const createChannel = (key, tilesPerLine) => {
467
- const canvas = document.createElement("canvas");
468
- const decoder = new Decoder({ tilesPerLine });
490
+ const createChannel = (key, decoderOptions) => {
491
+ const canvas = decoderOptions.canvasCreator();
492
+ const decoder = new Decoder(decoderOptions);
469
493
  decoder.update({
470
494
  framePalette: [],
471
495
  canvas,
@@ -485,15 +509,24 @@ class RGBNDecoder {
485
509
  __publicField(this, "lockFrame");
486
510
  __publicField(this, "channels");
487
511
  __publicField(this, "tilesPerLine");
512
+ __publicField(this, "canvasCreator");
513
+ __publicField(this, "imageDataCreator");
488
514
  this.canvas = null;
489
515
  this.palette = defaultPalette;
490
516
  this.lockFrame = false;
491
517
  this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
518
+ this.canvasCreator = options?.canvasCreator || createCanvasElement;
519
+ this.imageDataCreator = options?.imageDataCreator || createImageData;
520
+ const channelDecoderOptions = {
521
+ tilesPerLine: this.tilesPerLine,
522
+ canvasCreator: this.canvasCreator,
523
+ imageDataCreator: this.imageDataCreator
524
+ };
492
525
  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)
526
+ r: createChannel(ChannelKey.R, channelDecoderOptions),
527
+ g: createChannel(ChannelKey.G, channelDecoderOptions),
528
+ b: createChannel(ChannelKey.B, channelDecoderOptions),
529
+ n: createChannel(ChannelKey.N, channelDecoderOptions)
497
530
  };
498
531
  }
499
532
  update({
@@ -507,7 +540,7 @@ class RGBNDecoder {
507
540
  const lockFrameChanged = this.setLockFrame(lockFrame);
508
541
  const shouldUpdate = canvasChanged || paletteChanged || lockFrameChanged;
509
542
  const canvases = this.setTiles(tiles);
510
- const newHeight = this.getHeight();
543
+ const { width: newWidth, height: newHeight } = this.getDimensions(canvases);
511
544
  if (!shouldUpdate) {
512
545
  return;
513
546
  }
@@ -521,6 +554,9 @@ class RGBNDecoder {
521
554
  if (this.canvas.height !== newHeight) {
522
555
  this.canvas.height = newHeight;
523
556
  }
557
+ if (this.canvas.width !== newWidth) {
558
+ this.canvas.width = newWidth;
559
+ }
524
560
  const context = this.canvas?.getContext("2d");
525
561
  if (!context) {
526
562
  return;
@@ -595,7 +631,7 @@ class RGBNDecoder {
595
631
  });
596
632
  }
597
633
  getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
598
- const canvas = document.createElement("canvas");
634
+ const canvas = this.canvasCreator();
599
635
  const canvases = channels.reduce((acc, key) => {
600
636
  const channel = this.channels[key];
601
637
  const channelCanvas = channel.decoder.getScaledCanvas(scaleFactor, handleExportFrame);
@@ -604,27 +640,28 @@ class RGBNDecoder {
604
640
  [key]: channelCanvas
605
641
  };
606
642
  }, {});
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 });
643
+ const { width, height } = this.getDimensions(canvases);
611
644
  canvas.width = width;
612
645
  canvas.height = height;
613
646
  const context = canvas.getContext("2d");
614
647
  if (!context) {
615
648
  throw new Error("no canvas context");
616
649
  }
650
+ context.fillStyle = "#000000";
651
+ context.fillRect(0, 0, 500, 500);
617
652
  this.blendCanvases(context, canvases);
618
653
  return canvas;
619
654
  }
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);
655
+ getDimensions(canvases) {
656
+ return Object.values(canvases).reduce((acc, current) => {
657
+ if (current.width === 0 || current.height === 0) {
658
+ return acc;
659
+ }
660
+ return {
661
+ width: Math.max(current.width, acc.width),
662
+ height: Math.max(current.height, acc.height)
663
+ };
664
+ }, { width: 0, height: 0 });
628
665
  }
629
666
  }
630
667
 
@@ -633,6 +670,7 @@ const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]
633
670
  exports.BW_PALETTE = BW_PALETTE;
634
671
  exports.BW_PALETTE_HEX = BW_PALETTE_HEX;
635
672
  exports.BlendMode = BlendMode;
673
+ exports.ChannelKey = ChannelKey;
636
674
  exports.Decoder = Decoder;
637
675
  exports.ExportFrameMode = ExportFrameMode;
638
676
  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;
@@ -103,6 +109,7 @@ declare class Decoder {
103
109
  private paintTile;
104
110
  private paintTileScaled;
105
111
  private getHeight;
112
+ private getWidth;
106
113
  }
107
114
 
108
115
  declare class RGBNDecoder {
@@ -111,6 +118,8 @@ declare class RGBNDecoder {
111
118
  private lockFrame;
112
119
  private channels;
113
120
  private tilesPerLine;
121
+ private canvasCreator;
122
+ private imageDataCreator;
114
123
  constructor(options?: DecoderOptions);
115
124
  update({ canvas, tiles, palette, lockFrame, }: RGBNDecoderUpdateParams): void;
116
125
  private setPalette;
@@ -119,8 +128,7 @@ declare class RGBNDecoder {
119
128
  private setTiles;
120
129
  private blendCanvases;
121
130
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
122
- private maxTiles;
123
- private getHeight;
131
+ private getDimensions;
124
132
  }
125
133
 
126
134
  declare const decodeTile: (rawBytes?: string) => IndexedTilePixels;
@@ -143,4 +151,4 @@ declare const SKIP_LINE = "skip";
143
151
  declare const BW_PALETTE: number[];
144
152
  declare const BW_PALETTE_HEX: string[];
145
153
 
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 };
154
+ 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;
@@ -103,6 +109,7 @@ declare class Decoder {
103
109
  private paintTile;
104
110
  private paintTileScaled;
105
111
  private getHeight;
112
+ private getWidth;
106
113
  }
107
114
 
108
115
  declare class RGBNDecoder {
@@ -111,6 +118,8 @@ declare class RGBNDecoder {
111
118
  private lockFrame;
112
119
  private channels;
113
120
  private tilesPerLine;
121
+ private canvasCreator;
122
+ private imageDataCreator;
114
123
  constructor(options?: DecoderOptions);
115
124
  update({ canvas, tiles, palette, lockFrame, }: RGBNDecoderUpdateParams): void;
116
125
  private setPalette;
@@ -119,8 +128,7 @@ declare class RGBNDecoder {
119
128
  private setTiles;
120
129
  private blendCanvases;
121
130
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
122
- private maxTiles;
123
- private getHeight;
131
+ private getDimensions;
124
132
  }
125
133
 
126
134
  declare const decodeTile: (rawBytes?: string) => IndexedTilePixels;
@@ -143,4 +151,4 @@ declare const SKIP_LINE = "skip";
143
151
  declare const BW_PALETTE: number[];
144
152
  declare const BW_PALETTE_HEX: string[];
145
153
 
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 };
154
+ 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;
@@ -103,6 +109,7 @@ declare class Decoder {
103
109
  private paintTile;
104
110
  private paintTileScaled;
105
111
  private getHeight;
112
+ private getWidth;
106
113
  }
107
114
 
108
115
  declare class RGBNDecoder {
@@ -111,6 +118,8 @@ declare class RGBNDecoder {
111
118
  private lockFrame;
112
119
  private channels;
113
120
  private tilesPerLine;
121
+ private canvasCreator;
122
+ private imageDataCreator;
114
123
  constructor(options?: DecoderOptions);
115
124
  update({ canvas, tiles, palette, lockFrame, }: RGBNDecoderUpdateParams): void;
116
125
  private setPalette;
@@ -119,8 +128,7 @@ declare class RGBNDecoder {
119
128
  private setTiles;
120
129
  private blendCanvases;
121
130
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
122
- private maxTiles;
123
- private getHeight;
131
+ private getDimensions;
124
132
  }
125
133
 
126
134
  declare const decodeTile: (rawBytes?: string) => IndexedTilePixels;
@@ -143,4 +151,4 @@ declare const SKIP_LINE = "skip";
143
151
  declare const BW_PALETTE: number[];
144
152
  declare const BW_PALETTE_HEX: string[];
145
153
 
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 };
154
+ 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
@@ -186,6 +186,15 @@ const getRGBValue = ({
186
186
  };
187
187
  };
188
188
 
189
+ const createCanvasElement = () => {
190
+ try {
191
+ return document.createElement("canvas");
192
+ } catch (error) {
193
+ throw new Error("cannot create canvas element");
194
+ }
195
+ };
196
+ const createImageData = (rawImageData, width, height) => new ImageData(rawImageData, width, height);
197
+
189
198
  var __defProp$1 = Object.defineProperty;
190
199
  var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
191
200
  var __publicField$1 = (obj, key, value) => {
@@ -204,6 +213,8 @@ class Decoder {
204
213
  __publicField$1(this, "frameColorData");
205
214
  __publicField$1(this, "tilesPerLine");
206
215
  __publicField$1(this, "imageStartLine");
216
+ __publicField$1(this, "canvasCreator");
217
+ __publicField$1(this, "imageDataCreator");
207
218
  this.canvas = null;
208
219
  this.tiles = [];
209
220
  this.colors = [];
@@ -214,6 +225,8 @@ class Decoder {
214
225
  this.frameColorData = [...BW_PALETTE];
215
226
  this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
216
227
  this.imageStartLine = 2;
228
+ this.canvasCreator = options?.canvasCreator || createCanvasElement;
229
+ this.imageDataCreator = options?.imageDataCreator || createImageData;
217
230
  }
218
231
  update({
219
232
  canvas,
@@ -224,12 +237,14 @@ class Decoder {
224
237
  }) {
225
238
  const startLineChanged = this.setImageStartLine(imageStartLine);
226
239
  const canvasChanged = canvas ? this.setCanvas(canvas) : false;
227
- const palettesChanged = this.setPalettes(palette, framePalette);
240
+ const usedFramePalette = this.tilesPerLine === TILES_PER_LINE ? framePalette : palette;
241
+ const palettesChanged = this.setPalettes(palette, usedFramePalette);
228
242
  if (startLineChanged || canvasChanged || palettesChanged || !this.tiles.length) {
229
243
  this.tiles = [];
230
244
  }
231
245
  const tilesChanged = this.setTiles(tiles);
232
246
  const newHeight = this.getHeight();
247
+ const newWidth = this.getWidth();
233
248
  if (!this.canvas) {
234
249
  return;
235
250
  }
@@ -237,9 +252,10 @@ class Decoder {
237
252
  this.canvas.height = 0;
238
253
  return;
239
254
  }
240
- if (this.canvas.height !== newHeight || !this.rawImageData?.length) {
255
+ if (this.canvas.height !== newHeight || this.canvas.width !== newWidth || !this.rawImageData?.length) {
241
256
  this.canvas.height = newHeight;
242
- const newRawImageData = new Uint8ClampedArray(this.tilesPerLine * TILE_PIXEL_WIDTH * newHeight * 4);
257
+ this.canvas.width = newWidth;
258
+ const newRawImageData = new Uint8ClampedArray(newWidth * newHeight * 4);
243
259
  this.rawImageData?.forEach((value, index) => {
244
260
  newRawImageData[index] = value;
245
261
  });
@@ -248,7 +264,7 @@ class Decoder {
248
264
  tilesChanged.forEach(({ index, newTile }) => {
249
265
  this.renderTile(index, newTile);
250
266
  });
251
- this.updateCanvas(newHeight);
267
+ this.updateCanvas(newWidth, newHeight);
252
268
  }
253
269
  setImageStartLine(imageStartLine) {
254
270
  if (this.imageStartLine === imageStartLine) {
@@ -257,22 +273,25 @@ class Decoder {
257
273
  this.imageStartLine = imageStartLine;
258
274
  return true;
259
275
  }
260
- updateCanvas(newHeight) {
276
+ updateCanvas(newWidth, newHeight) {
261
277
  if (!this.canvas || !this.rawImageData?.length) {
262
278
  return;
263
279
  }
264
280
  const context = this.canvas.getContext("2d");
265
- const imageData = new ImageData(this.rawImageData, this.tilesPerLine * TILE_PIXEL_WIDTH, newHeight);
281
+ const imageData = this.imageDataCreator(this.rawImageData, newWidth, newHeight);
266
282
  context?.putImageData(imageData, 0, 0);
267
283
  }
268
284
  getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
269
- const handleFrameMode = this.tiles.length === 360 ? handleExportFrame : ExportFrameMode.FRAMEMODE_KEEP;
285
+ let handleFrameMode = handleExportFrame;
286
+ if ((this.tiles.length !== 360 || this.tilesPerLine !== TILES_PER_LINE) && handleFrameMode !== ExportFrameMode.FRAMEMODE_KEEP) {
287
+ handleFrameMode = ExportFrameMode.FRAMEMODE_KEEP;
288
+ }
270
289
  const {
271
290
  initialHeight,
272
291
  initialWidth,
273
292
  tilesPerLine
274
- } = this.getScaledCanvasSize(handleFrameMode, this.getHeight());
275
- const canvas = document.createElement("canvas");
293
+ } = this.getScaledCanvasSize(handleFrameMode);
294
+ const canvas = this.canvasCreator();
276
295
  const context = canvas.getContext("2d");
277
296
  if (!context) {
278
297
  throw new Error("no canvas context");
@@ -280,30 +299,32 @@ class Decoder {
280
299
  canvas.width = initialWidth * scaleFactor;
281
300
  canvas.height = initialHeight * scaleFactor;
282
301
  this.getExportTiles(handleFrameMode).forEach((tile, index) => {
283
- this.paintTileScaled(decodeTile(tile), index, context, scaleFactor, tilesPerLine, handleExportFrame);
302
+ this.paintTileScaled(decodeTile(tile), index, context, scaleFactor, tilesPerLine, handleFrameMode);
284
303
  });
285
304
  return canvas;
286
305
  }
287
- getScaledCanvasSize(handleExportFrame, height) {
306
+ getScaledCanvasSize(handleExportFrame) {
288
307
  const FRAME_TILES = 4;
308
+ const width = this.getWidth();
309
+ const height = this.getHeight();
289
310
  switch (handleExportFrame) {
290
311
  case ExportFrameMode.FRAMEMODE_KEEP:
291
312
  return {
292
313
  initialHeight: height,
293
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH,
314
+ initialWidth: width,
294
315
  tilesPerLine: this.tilesPerLine
295
316
  };
296
317
  case ExportFrameMode.FRAMEMODE_CROP:
297
318
  return {
298
319
  initialHeight: height - TILE_PIXEL_HEIGHT * FRAME_TILES,
299
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH - TILE_PIXEL_WIDTH * FRAME_TILES,
320
+ initialWidth: width - TILE_PIXEL_WIDTH * FRAME_TILES,
300
321
  tilesPerLine: this.tilesPerLine - FRAME_TILES
301
322
  };
302
323
  case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
303
324
  case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
304
325
  return {
305
326
  initialHeight: height + 2 * TILE_PIXEL_HEIGHT,
306
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH,
327
+ initialWidth: width,
307
328
  tilesPerLine: this.tilesPerLine
308
329
  };
309
330
  default:
@@ -445,6 +466,9 @@ class Decoder {
445
466
  getHeight() {
446
467
  return TILE_PIXEL_HEIGHT * Math.ceil(this.tiles.length / this.tilesPerLine);
447
468
  }
469
+ getWidth() {
470
+ return TILE_PIXEL_WIDTH * this.tilesPerLine;
471
+ }
448
472
  }
449
473
 
450
474
  const hx2 = (n) => (n || 0).toString(16).padStart(2, "0");
@@ -461,9 +485,9 @@ var __publicField = (obj, key, value) => {
461
485
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
462
486
  return value;
463
487
  };
464
- const createChannel = (key, tilesPerLine) => {
465
- const canvas = document.createElement("canvas");
466
- const decoder = new Decoder({ tilesPerLine });
488
+ const createChannel = (key, decoderOptions) => {
489
+ const canvas = decoderOptions.canvasCreator();
490
+ const decoder = new Decoder(decoderOptions);
467
491
  decoder.update({
468
492
  framePalette: [],
469
493
  canvas,
@@ -483,15 +507,24 @@ class RGBNDecoder {
483
507
  __publicField(this, "lockFrame");
484
508
  __publicField(this, "channels");
485
509
  __publicField(this, "tilesPerLine");
510
+ __publicField(this, "canvasCreator");
511
+ __publicField(this, "imageDataCreator");
486
512
  this.canvas = null;
487
513
  this.palette = defaultPalette;
488
514
  this.lockFrame = false;
489
515
  this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
516
+ this.canvasCreator = options?.canvasCreator || createCanvasElement;
517
+ this.imageDataCreator = options?.imageDataCreator || createImageData;
518
+ const channelDecoderOptions = {
519
+ tilesPerLine: this.tilesPerLine,
520
+ canvasCreator: this.canvasCreator,
521
+ imageDataCreator: this.imageDataCreator
522
+ };
490
523
  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)
524
+ r: createChannel(ChannelKey.R, channelDecoderOptions),
525
+ g: createChannel(ChannelKey.G, channelDecoderOptions),
526
+ b: createChannel(ChannelKey.B, channelDecoderOptions),
527
+ n: createChannel(ChannelKey.N, channelDecoderOptions)
495
528
  };
496
529
  }
497
530
  update({
@@ -505,7 +538,7 @@ class RGBNDecoder {
505
538
  const lockFrameChanged = this.setLockFrame(lockFrame);
506
539
  const shouldUpdate = canvasChanged || paletteChanged || lockFrameChanged;
507
540
  const canvases = this.setTiles(tiles);
508
- const newHeight = this.getHeight();
541
+ const { width: newWidth, height: newHeight } = this.getDimensions(canvases);
509
542
  if (!shouldUpdate) {
510
543
  return;
511
544
  }
@@ -519,6 +552,9 @@ class RGBNDecoder {
519
552
  if (this.canvas.height !== newHeight) {
520
553
  this.canvas.height = newHeight;
521
554
  }
555
+ if (this.canvas.width !== newWidth) {
556
+ this.canvas.width = newWidth;
557
+ }
522
558
  const context = this.canvas?.getContext("2d");
523
559
  if (!context) {
524
560
  return;
@@ -593,7 +629,7 @@ class RGBNDecoder {
593
629
  });
594
630
  }
595
631
  getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
596
- const canvas = document.createElement("canvas");
632
+ const canvas = this.canvasCreator();
597
633
  const canvases = channels.reduce((acc, key) => {
598
634
  const channel = this.channels[key];
599
635
  const channelCanvas = channel.decoder.getScaledCanvas(scaleFactor, handleExportFrame);
@@ -602,30 +638,31 @@ class RGBNDecoder {
602
638
  [key]: channelCanvas
603
639
  };
604
640
  }, {});
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 });
641
+ const { width, height } = this.getDimensions(canvases);
609
642
  canvas.width = width;
610
643
  canvas.height = height;
611
644
  const context = canvas.getContext("2d");
612
645
  if (!context) {
613
646
  throw new Error("no canvas context");
614
647
  }
648
+ context.fillStyle = "#000000";
649
+ context.fillRect(0, 0, 500, 500);
615
650
  this.blendCanvases(context, canvases);
616
651
  return canvas;
617
652
  }
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);
653
+ getDimensions(canvases) {
654
+ return Object.values(canvases).reduce((acc, current) => {
655
+ if (current.width === 0 || current.height === 0) {
656
+ return acc;
657
+ }
658
+ return {
659
+ width: Math.max(current.width, acc.width),
660
+ height: Math.max(current.height, acc.height)
661
+ };
662
+ }, { width: 0, height: 0 });
626
663
  }
627
664
  }
628
665
 
629
666
  const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]?.length || 0));
630
667
 
631
- export { BW_PALETTE, BW_PALETTE_HEX, BlendMode, Decoder, ExportFrameMode, RGBNDecoder, SKIP_LINE, blendModeNewName, decodeTile, getRGBValue, maxTiles, tileIndexIsPartOfFrame };
668
+ 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.0",
4
4
  "description": "Decoder classes for GameBoy-encoded images",
5
5
  "repository": "",
6
6
  "license": "MIT",
@@ -15,22 +15,29 @@
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": "rimraf images && vitest run",
26
+ "test:u": "rimraf images && vitest run -u",
23
27
  "eslint": "eslint -c .eslintrc --ext .js,.ts ."
24
28
  },
25
29
  "devDependencies": {
26
30
  "@types/node": "^20.12.7",
27
31
  "@typescript-eslint/eslint-plugin": "^7.9.0",
32
+ "canvas": "^2.11.2",
28
33
  "eslint": "^8.57.0",
29
34
  "eslint-config-airbnb-base": "^15.0.0",
30
35
  "eslint-import-resolver-typescript": "^3.6.1",
31
36
  "eslint-plugin-import": "^2.29.1",
37
+ "jsdom": "^25.0.1",
38
+ "ohash": "^1.1.4",
32
39
  "typescript": "^5.4.5",
33
40
  "unbuild": "^2.0.0",
34
- "vitest": "^1.6.0"
41
+ "vitest": "^2.1.3"
35
42
  }
36
43
  }