gb-image-decoder 1.2.0 → 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
@@ -146,6 +146,9 @@ const decodeTile = (rawBytes = BLACK) => {
146
146
  };
147
147
 
148
148
  const tileIndexIsPartOfFrame = (tileIndex, imageStartLine, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) => {
149
+ if (handleExportFrame === ExportFrameMode.FRAMEMODE_CROP) {
150
+ return false;
151
+ }
149
152
  const checkIndex = tileIndex - (handleExportFrame === ExportFrameMode.FRAMEMODE_KEEP ? 0 : 20);
150
153
  if (checkIndex < imageStartLine * 20) {
151
154
  return true;
@@ -185,6 +188,15 @@ const getRGBValue = ({
185
188
  };
186
189
  };
187
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
+
188
200
  var __defProp$1 = Object.defineProperty;
189
201
  var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
190
202
  var __publicField$1 = (obj, key, value) => {
@@ -203,6 +215,8 @@ class Decoder {
203
215
  __publicField$1(this, "frameColorData");
204
216
  __publicField$1(this, "tilesPerLine");
205
217
  __publicField$1(this, "imageStartLine");
218
+ __publicField$1(this, "canvasCreator");
219
+ __publicField$1(this, "imageDataCreator");
206
220
  this.canvas = null;
207
221
  this.tiles = [];
208
222
  this.colors = [];
@@ -213,6 +227,8 @@ class Decoder {
213
227
  this.frameColorData = [...BW_PALETTE];
214
228
  this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
215
229
  this.imageStartLine = 2;
230
+ this.canvasCreator = options?.canvasCreator || createCanvasElement;
231
+ this.imageDataCreator = options?.imageDataCreator || createImageData;
216
232
  }
217
233
  update({
218
234
  canvas,
@@ -223,12 +239,14 @@ class Decoder {
223
239
  }) {
224
240
  const startLineChanged = this.setImageStartLine(imageStartLine);
225
241
  const canvasChanged = canvas ? this.setCanvas(canvas) : false;
226
- const palettesChanged = this.setPalettes(palette, framePalette);
242
+ const usedFramePalette = this.tilesPerLine === TILES_PER_LINE ? framePalette : palette;
243
+ const palettesChanged = this.setPalettes(palette, usedFramePalette);
227
244
  if (startLineChanged || canvasChanged || palettesChanged || !this.tiles.length) {
228
245
  this.tiles = [];
229
246
  }
230
247
  const tilesChanged = this.setTiles(tiles);
231
248
  const newHeight = this.getHeight();
249
+ const newWidth = this.getWidth();
232
250
  if (!this.canvas) {
233
251
  return;
234
252
  }
@@ -236,9 +254,10 @@ class Decoder {
236
254
  this.canvas.height = 0;
237
255
  return;
238
256
  }
239
- if (this.canvas.height !== newHeight || !this.rawImageData?.length) {
257
+ if (this.canvas.height !== newHeight || this.canvas.width !== newWidth || !this.rawImageData?.length) {
240
258
  this.canvas.height = newHeight;
241
- const newRawImageData = new Uint8ClampedArray(this.tilesPerLine * TILE_PIXEL_WIDTH * newHeight * 4);
259
+ this.canvas.width = newWidth;
260
+ const newRawImageData = new Uint8ClampedArray(newWidth * newHeight * 4);
242
261
  this.rawImageData?.forEach((value, index) => {
243
262
  newRawImageData[index] = value;
244
263
  });
@@ -247,7 +266,7 @@ class Decoder {
247
266
  tilesChanged.forEach(({ index, newTile }) => {
248
267
  this.renderTile(index, newTile);
249
268
  });
250
- this.updateCanvas(newHeight);
269
+ this.updateCanvas(newWidth, newHeight);
251
270
  }
252
271
  setImageStartLine(imageStartLine) {
253
272
  if (this.imageStartLine === imageStartLine) {
@@ -256,22 +275,25 @@ class Decoder {
256
275
  this.imageStartLine = imageStartLine;
257
276
  return true;
258
277
  }
259
- updateCanvas(newHeight) {
278
+ updateCanvas(newWidth, newHeight) {
260
279
  if (!this.canvas || !this.rawImageData?.length) {
261
280
  return;
262
281
  }
263
282
  const context = this.canvas.getContext("2d");
264
- const imageData = new ImageData(this.rawImageData, this.tilesPerLine * TILE_PIXEL_WIDTH, newHeight);
283
+ const imageData = this.imageDataCreator(this.rawImageData, newWidth, newHeight);
265
284
  context?.putImageData(imageData, 0, 0);
266
285
  }
267
286
  getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
268
- 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
+ }
269
291
  const {
270
292
  initialHeight,
271
293
  initialWidth,
272
294
  tilesPerLine
273
- } = this.getScaledCanvasSize(handleFrameMode, this.getHeight());
274
- const canvas = document.createElement("canvas");
295
+ } = this.getScaledCanvasSize(handleFrameMode);
296
+ const canvas = this.canvasCreator();
275
297
  const context = canvas.getContext("2d");
276
298
  if (!context) {
277
299
  throw new Error("no canvas context");
@@ -279,30 +301,32 @@ class Decoder {
279
301
  canvas.width = initialWidth * scaleFactor;
280
302
  canvas.height = initialHeight * scaleFactor;
281
303
  this.getExportTiles(handleFrameMode).forEach((tile, index) => {
282
- this.paintTileScaled(decodeTile(tile), index, context, scaleFactor, tilesPerLine, handleExportFrame);
304
+ this.paintTileScaled(decodeTile(tile), index, context, scaleFactor, tilesPerLine, handleFrameMode);
283
305
  });
284
306
  return canvas;
285
307
  }
286
- getScaledCanvasSize(handleExportFrame, height) {
308
+ getScaledCanvasSize(handleExportFrame) {
287
309
  const FRAME_TILES = 4;
310
+ const width = this.getWidth();
311
+ const height = this.getHeight();
288
312
  switch (handleExportFrame) {
289
313
  case ExportFrameMode.FRAMEMODE_KEEP:
290
314
  return {
291
315
  initialHeight: height,
292
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH,
316
+ initialWidth: width,
293
317
  tilesPerLine: this.tilesPerLine
294
318
  };
295
319
  case ExportFrameMode.FRAMEMODE_CROP:
296
320
  return {
297
321
  initialHeight: height - TILE_PIXEL_HEIGHT * FRAME_TILES,
298
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH - TILE_PIXEL_WIDTH * FRAME_TILES,
322
+ initialWidth: width - TILE_PIXEL_WIDTH * FRAME_TILES,
299
323
  tilesPerLine: this.tilesPerLine - FRAME_TILES
300
324
  };
301
325
  case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
302
326
  case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
303
327
  return {
304
328
  initialHeight: height + 2 * TILE_PIXEL_HEIGHT,
305
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH,
329
+ initialWidth: width,
306
330
  tilesPerLine: this.tilesPerLine
307
331
  };
308
332
  default:
@@ -444,6 +468,9 @@ class Decoder {
444
468
  getHeight() {
445
469
  return TILE_PIXEL_HEIGHT * Math.ceil(this.tiles.length / this.tilesPerLine);
446
470
  }
471
+ getWidth() {
472
+ return TILE_PIXEL_WIDTH * this.tilesPerLine;
473
+ }
447
474
  }
448
475
 
449
476
  const hx2 = (n) => (n || 0).toString(16).padStart(2, "0");
@@ -460,9 +487,9 @@ var __publicField = (obj, key, value) => {
460
487
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
461
488
  return value;
462
489
  };
463
- const createChannel = (key, tilesPerLine) => {
464
- const canvas = document.createElement("canvas");
465
- const decoder = new Decoder({ tilesPerLine });
490
+ const createChannel = (key, decoderOptions) => {
491
+ const canvas = decoderOptions.canvasCreator();
492
+ const decoder = new Decoder(decoderOptions);
466
493
  decoder.update({
467
494
  framePalette: [],
468
495
  canvas,
@@ -482,15 +509,24 @@ class RGBNDecoder {
482
509
  __publicField(this, "lockFrame");
483
510
  __publicField(this, "channels");
484
511
  __publicField(this, "tilesPerLine");
512
+ __publicField(this, "canvasCreator");
513
+ __publicField(this, "imageDataCreator");
485
514
  this.canvas = null;
486
515
  this.palette = defaultPalette;
487
516
  this.lockFrame = false;
488
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
+ };
489
525
  this.channels = {
490
- r: createChannel(ChannelKey.R, this.tilesPerLine),
491
- g: createChannel(ChannelKey.G, this.tilesPerLine),
492
- b: createChannel(ChannelKey.B, this.tilesPerLine),
493
- 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)
494
530
  };
495
531
  }
496
532
  update({
@@ -504,7 +540,7 @@ class RGBNDecoder {
504
540
  const lockFrameChanged = this.setLockFrame(lockFrame);
505
541
  const shouldUpdate = canvasChanged || paletteChanged || lockFrameChanged;
506
542
  const canvases = this.setTiles(tiles);
507
- const newHeight = this.getHeight();
543
+ const { width: newWidth, height: newHeight } = this.getDimensions(canvases);
508
544
  if (!shouldUpdate) {
509
545
  return;
510
546
  }
@@ -518,6 +554,9 @@ class RGBNDecoder {
518
554
  if (this.canvas.height !== newHeight) {
519
555
  this.canvas.height = newHeight;
520
556
  }
557
+ if (this.canvas.width !== newWidth) {
558
+ this.canvas.width = newWidth;
559
+ }
521
560
  const context = this.canvas?.getContext("2d");
522
561
  if (!context) {
523
562
  return;
@@ -592,7 +631,7 @@ class RGBNDecoder {
592
631
  });
593
632
  }
594
633
  getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
595
- const canvas = document.createElement("canvas");
634
+ const canvas = this.canvasCreator();
596
635
  const canvases = channels.reduce((acc, key) => {
597
636
  const channel = this.channels[key];
598
637
  const channelCanvas = channel.decoder.getScaledCanvas(scaleFactor, handleExportFrame);
@@ -601,27 +640,28 @@ class RGBNDecoder {
601
640
  [key]: channelCanvas
602
641
  };
603
642
  }, {});
604
- const { width, height } = Object.values(canvases).reduce((acc, current) => ({
605
- width: Math.max(current.width, acc.width),
606
- height: Math.max(current.height, acc.height)
607
- }), { width: 0, height: 0 });
643
+ const { width, height } = this.getDimensions(canvases);
608
644
  canvas.width = width;
609
645
  canvas.height = height;
610
646
  const context = canvas.getContext("2d");
611
647
  if (!context) {
612
648
  throw new Error("no canvas context");
613
649
  }
650
+ context.fillStyle = "#000000";
651
+ context.fillRect(0, 0, 500, 500);
614
652
  this.blendCanvases(context, canvases);
615
653
  return canvas;
616
654
  }
617
- maxTiles() {
618
- return channels.reduce((acc, key) => {
619
- const channel = this.channels[key];
620
- return Math.max(acc, channel.tiles?.length || 0);
621
- }, 0);
622
- }
623
- getHeight() {
624
- 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 });
625
665
  }
626
666
  }
627
667
 
@@ -630,6 +670,7 @@ const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]
630
670
  exports.BW_PALETTE = BW_PALETTE;
631
671
  exports.BW_PALETTE_HEX = BW_PALETTE_HEX;
632
672
  exports.BlendMode = BlendMode;
673
+ exports.ChannelKey = ChannelKey;
633
674
  exports.Decoder = Decoder;
634
675
  exports.ExportFrameMode = ExportFrameMode;
635
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
@@ -144,6 +144,9 @@ const decodeTile = (rawBytes = BLACK) => {
144
144
  };
145
145
 
146
146
  const tileIndexIsPartOfFrame = (tileIndex, imageStartLine, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) => {
147
+ if (handleExportFrame === ExportFrameMode.FRAMEMODE_CROP) {
148
+ return false;
149
+ }
147
150
  const checkIndex = tileIndex - (handleExportFrame === ExportFrameMode.FRAMEMODE_KEEP ? 0 : 20);
148
151
  if (checkIndex < imageStartLine * 20) {
149
152
  return true;
@@ -183,6 +186,15 @@ const getRGBValue = ({
183
186
  };
184
187
  };
185
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
+
186
198
  var __defProp$1 = Object.defineProperty;
187
199
  var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
188
200
  var __publicField$1 = (obj, key, value) => {
@@ -201,6 +213,8 @@ class Decoder {
201
213
  __publicField$1(this, "frameColorData");
202
214
  __publicField$1(this, "tilesPerLine");
203
215
  __publicField$1(this, "imageStartLine");
216
+ __publicField$1(this, "canvasCreator");
217
+ __publicField$1(this, "imageDataCreator");
204
218
  this.canvas = null;
205
219
  this.tiles = [];
206
220
  this.colors = [];
@@ -211,6 +225,8 @@ class Decoder {
211
225
  this.frameColorData = [...BW_PALETTE];
212
226
  this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
213
227
  this.imageStartLine = 2;
228
+ this.canvasCreator = options?.canvasCreator || createCanvasElement;
229
+ this.imageDataCreator = options?.imageDataCreator || createImageData;
214
230
  }
215
231
  update({
216
232
  canvas,
@@ -221,12 +237,14 @@ class Decoder {
221
237
  }) {
222
238
  const startLineChanged = this.setImageStartLine(imageStartLine);
223
239
  const canvasChanged = canvas ? this.setCanvas(canvas) : false;
224
- const palettesChanged = this.setPalettes(palette, framePalette);
240
+ const usedFramePalette = this.tilesPerLine === TILES_PER_LINE ? framePalette : palette;
241
+ const palettesChanged = this.setPalettes(palette, usedFramePalette);
225
242
  if (startLineChanged || canvasChanged || palettesChanged || !this.tiles.length) {
226
243
  this.tiles = [];
227
244
  }
228
245
  const tilesChanged = this.setTiles(tiles);
229
246
  const newHeight = this.getHeight();
247
+ const newWidth = this.getWidth();
230
248
  if (!this.canvas) {
231
249
  return;
232
250
  }
@@ -234,9 +252,10 @@ class Decoder {
234
252
  this.canvas.height = 0;
235
253
  return;
236
254
  }
237
- if (this.canvas.height !== newHeight || !this.rawImageData?.length) {
255
+ if (this.canvas.height !== newHeight || this.canvas.width !== newWidth || !this.rawImageData?.length) {
238
256
  this.canvas.height = newHeight;
239
- const newRawImageData = new Uint8ClampedArray(this.tilesPerLine * TILE_PIXEL_WIDTH * newHeight * 4);
257
+ this.canvas.width = newWidth;
258
+ const newRawImageData = new Uint8ClampedArray(newWidth * newHeight * 4);
240
259
  this.rawImageData?.forEach((value, index) => {
241
260
  newRawImageData[index] = value;
242
261
  });
@@ -245,7 +264,7 @@ class Decoder {
245
264
  tilesChanged.forEach(({ index, newTile }) => {
246
265
  this.renderTile(index, newTile);
247
266
  });
248
- this.updateCanvas(newHeight);
267
+ this.updateCanvas(newWidth, newHeight);
249
268
  }
250
269
  setImageStartLine(imageStartLine) {
251
270
  if (this.imageStartLine === imageStartLine) {
@@ -254,22 +273,25 @@ class Decoder {
254
273
  this.imageStartLine = imageStartLine;
255
274
  return true;
256
275
  }
257
- updateCanvas(newHeight) {
276
+ updateCanvas(newWidth, newHeight) {
258
277
  if (!this.canvas || !this.rawImageData?.length) {
259
278
  return;
260
279
  }
261
280
  const context = this.canvas.getContext("2d");
262
- const imageData = new ImageData(this.rawImageData, this.tilesPerLine * TILE_PIXEL_WIDTH, newHeight);
281
+ const imageData = this.imageDataCreator(this.rawImageData, newWidth, newHeight);
263
282
  context?.putImageData(imageData, 0, 0);
264
283
  }
265
284
  getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
266
- 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
+ }
267
289
  const {
268
290
  initialHeight,
269
291
  initialWidth,
270
292
  tilesPerLine
271
- } = this.getScaledCanvasSize(handleFrameMode, this.getHeight());
272
- const canvas = document.createElement("canvas");
293
+ } = this.getScaledCanvasSize(handleFrameMode);
294
+ const canvas = this.canvasCreator();
273
295
  const context = canvas.getContext("2d");
274
296
  if (!context) {
275
297
  throw new Error("no canvas context");
@@ -277,30 +299,32 @@ class Decoder {
277
299
  canvas.width = initialWidth * scaleFactor;
278
300
  canvas.height = initialHeight * scaleFactor;
279
301
  this.getExportTiles(handleFrameMode).forEach((tile, index) => {
280
- this.paintTileScaled(decodeTile(tile), index, context, scaleFactor, tilesPerLine, handleExportFrame);
302
+ this.paintTileScaled(decodeTile(tile), index, context, scaleFactor, tilesPerLine, handleFrameMode);
281
303
  });
282
304
  return canvas;
283
305
  }
284
- getScaledCanvasSize(handleExportFrame, height) {
306
+ getScaledCanvasSize(handleExportFrame) {
285
307
  const FRAME_TILES = 4;
308
+ const width = this.getWidth();
309
+ const height = this.getHeight();
286
310
  switch (handleExportFrame) {
287
311
  case ExportFrameMode.FRAMEMODE_KEEP:
288
312
  return {
289
313
  initialHeight: height,
290
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH,
314
+ initialWidth: width,
291
315
  tilesPerLine: this.tilesPerLine
292
316
  };
293
317
  case ExportFrameMode.FRAMEMODE_CROP:
294
318
  return {
295
319
  initialHeight: height - TILE_PIXEL_HEIGHT * FRAME_TILES,
296
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH - TILE_PIXEL_WIDTH * FRAME_TILES,
320
+ initialWidth: width - TILE_PIXEL_WIDTH * FRAME_TILES,
297
321
  tilesPerLine: this.tilesPerLine - FRAME_TILES
298
322
  };
299
323
  case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
300
324
  case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
301
325
  return {
302
326
  initialHeight: height + 2 * TILE_PIXEL_HEIGHT,
303
- initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH,
327
+ initialWidth: width,
304
328
  tilesPerLine: this.tilesPerLine
305
329
  };
306
330
  default:
@@ -442,6 +466,9 @@ class Decoder {
442
466
  getHeight() {
443
467
  return TILE_PIXEL_HEIGHT * Math.ceil(this.tiles.length / this.tilesPerLine);
444
468
  }
469
+ getWidth() {
470
+ return TILE_PIXEL_WIDTH * this.tilesPerLine;
471
+ }
445
472
  }
446
473
 
447
474
  const hx2 = (n) => (n || 0).toString(16).padStart(2, "0");
@@ -458,9 +485,9 @@ var __publicField = (obj, key, value) => {
458
485
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
459
486
  return value;
460
487
  };
461
- const createChannel = (key, tilesPerLine) => {
462
- const canvas = document.createElement("canvas");
463
- const decoder = new Decoder({ tilesPerLine });
488
+ const createChannel = (key, decoderOptions) => {
489
+ const canvas = decoderOptions.canvasCreator();
490
+ const decoder = new Decoder(decoderOptions);
464
491
  decoder.update({
465
492
  framePalette: [],
466
493
  canvas,
@@ -480,15 +507,24 @@ class RGBNDecoder {
480
507
  __publicField(this, "lockFrame");
481
508
  __publicField(this, "channels");
482
509
  __publicField(this, "tilesPerLine");
510
+ __publicField(this, "canvasCreator");
511
+ __publicField(this, "imageDataCreator");
483
512
  this.canvas = null;
484
513
  this.palette = defaultPalette;
485
514
  this.lockFrame = false;
486
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
+ };
487
523
  this.channels = {
488
- r: createChannel(ChannelKey.R, this.tilesPerLine),
489
- g: createChannel(ChannelKey.G, this.tilesPerLine),
490
- b: createChannel(ChannelKey.B, this.tilesPerLine),
491
- 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)
492
528
  };
493
529
  }
494
530
  update({
@@ -502,7 +538,7 @@ class RGBNDecoder {
502
538
  const lockFrameChanged = this.setLockFrame(lockFrame);
503
539
  const shouldUpdate = canvasChanged || paletteChanged || lockFrameChanged;
504
540
  const canvases = this.setTiles(tiles);
505
- const newHeight = this.getHeight();
541
+ const { width: newWidth, height: newHeight } = this.getDimensions(canvases);
506
542
  if (!shouldUpdate) {
507
543
  return;
508
544
  }
@@ -516,6 +552,9 @@ class RGBNDecoder {
516
552
  if (this.canvas.height !== newHeight) {
517
553
  this.canvas.height = newHeight;
518
554
  }
555
+ if (this.canvas.width !== newWidth) {
556
+ this.canvas.width = newWidth;
557
+ }
519
558
  const context = this.canvas?.getContext("2d");
520
559
  if (!context) {
521
560
  return;
@@ -590,7 +629,7 @@ class RGBNDecoder {
590
629
  });
591
630
  }
592
631
  getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
593
- const canvas = document.createElement("canvas");
632
+ const canvas = this.canvasCreator();
594
633
  const canvases = channels.reduce((acc, key) => {
595
634
  const channel = this.channels[key];
596
635
  const channelCanvas = channel.decoder.getScaledCanvas(scaleFactor, handleExportFrame);
@@ -599,30 +638,31 @@ class RGBNDecoder {
599
638
  [key]: channelCanvas
600
639
  };
601
640
  }, {});
602
- const { width, height } = Object.values(canvases).reduce((acc, current) => ({
603
- width: Math.max(current.width, acc.width),
604
- height: Math.max(current.height, acc.height)
605
- }), { width: 0, height: 0 });
641
+ const { width, height } = this.getDimensions(canvases);
606
642
  canvas.width = width;
607
643
  canvas.height = height;
608
644
  const context = canvas.getContext("2d");
609
645
  if (!context) {
610
646
  throw new Error("no canvas context");
611
647
  }
648
+ context.fillStyle = "#000000";
649
+ context.fillRect(0, 0, 500, 500);
612
650
  this.blendCanvases(context, canvases);
613
651
  return canvas;
614
652
  }
615
- maxTiles() {
616
- return channels.reduce((acc, key) => {
617
- const channel = this.channels[key];
618
- return Math.max(acc, channel.tiles?.length || 0);
619
- }, 0);
620
- }
621
- getHeight() {
622
- 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 });
623
663
  }
624
664
  }
625
665
 
626
666
  const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]?.length || 0));
627
667
 
628
- 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.0",
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
  }