gb-image-decoder 0.1.3 → 0.1.4

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
@@ -191,7 +191,7 @@ var __publicField$1 = (obj, key, value) => {
191
191
  return value;
192
192
  };
193
193
  class Decoder {
194
- constructor() {
194
+ constructor(options) {
195
195
  __publicField$1(this, "canvas");
196
196
  __publicField$1(this, "tiles");
197
197
  __publicField$1(this, "colors");
@@ -199,6 +199,7 @@ class Decoder {
199
199
  __publicField$1(this, "lockFrame");
200
200
  __publicField$1(this, "invertPalette");
201
201
  __publicField$1(this, "colorData");
202
+ __publicField$1(this, "tilesPerLine");
202
203
  this.canvas = null;
203
204
  this.tiles = [];
204
205
  this.colors = [];
@@ -206,6 +207,7 @@ class Decoder {
206
207
  this.lockFrame = false;
207
208
  this.invertPalette = false;
208
209
  this.colorData = [...BW_PALETTE];
210
+ this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
209
211
  }
210
212
  update({
211
213
  canvas = null,
@@ -256,7 +258,7 @@ class Decoder {
256
258
  initialHeight,
257
259
  initialWidth,
258
260
  tilesPerLine
259
- } = Decoder.getScaledCanvasSize(handleFrameMode, this.getHeight());
261
+ } = this.getScaledCanvasSize(handleFrameMode, this.getHeight());
260
262
  const canvas = document.createElement("canvas");
261
263
  const context = canvas.getContext("2d");
262
264
  if (!context) {
@@ -269,27 +271,27 @@ class Decoder {
269
271
  });
270
272
  return canvas;
271
273
  }
272
- static getScaledCanvasSize(handleExportFrame, height) {
274
+ getScaledCanvasSize(handleExportFrame, height) {
273
275
  const FRAME_TILES = 4;
274
276
  switch (handleExportFrame) {
275
277
  case ExportFrameMode.FRAMEMODE_KEEP:
276
278
  return {
277
279
  initialHeight: height,
278
- initialWidth: TILES_PER_LINE * TILE_PIXEL_WIDTH,
279
- tilesPerLine: TILES_PER_LINE
280
+ initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH,
281
+ tilesPerLine: this.tilesPerLine
280
282
  };
281
283
  case ExportFrameMode.FRAMEMODE_CROP:
282
284
  return {
283
285
  initialHeight: height - TILE_PIXEL_HEIGHT * FRAME_TILES,
284
- initialWidth: TILES_PER_LINE * TILE_PIXEL_WIDTH - TILE_PIXEL_WIDTH * FRAME_TILES,
285
- tilesPerLine: TILES_PER_LINE - FRAME_TILES
286
+ initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH - TILE_PIXEL_WIDTH * FRAME_TILES,
287
+ tilesPerLine: this.tilesPerLine - FRAME_TILES
286
288
  };
287
289
  case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
288
290
  case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
289
291
  return {
290
292
  initialHeight: height + 2 * TILE_PIXEL_HEIGHT,
291
- initialWidth: TILES_PER_LINE * TILE_PIXEL_WIDTH,
292
- tilesPerLine: TILES_PER_LINE
293
+ initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH,
294
+ tilesPerLine: this.tilesPerLine
293
295
  };
294
296
  default:
295
297
  throw new Error(`unknown export mode ${handleExportFrame}`);
@@ -371,8 +373,8 @@ class Decoder {
371
373
  if (!this.rawImageData) {
372
374
  return;
373
375
  }
374
- const tileXOffset = index % TILES_PER_LINE;
375
- const tileYOffset = Math.floor(index / TILES_PER_LINE);
376
+ const tileXOffset = index % this.tilesPerLine;
377
+ const tileYOffset = Math.floor(index / this.tilesPerLine);
376
378
  const pixelXOffset = TILE_PIXEL_WIDTH * tileXOffset;
377
379
  const pixelYOffset = TILE_PIXEL_HEIGHT * tileYOffset;
378
380
  for (let x = 0; x < TILE_PIXEL_WIDTH; x += 1) {
@@ -421,7 +423,7 @@ class Decoder {
421
423
  }
422
424
  }
423
425
  getHeight() {
424
- return TILE_PIXEL_HEIGHT * Math.ceil(this.tiles.length / TILES_PER_LINE);
426
+ return TILE_PIXEL_HEIGHT * Math.ceil(this.tiles.length / this.tilesPerLine);
425
427
  }
426
428
  }
427
429
 
@@ -439,9 +441,9 @@ var __publicField = (obj, key, value) => {
439
441
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
440
442
  return value;
441
443
  };
442
- const createChannel = (key) => {
444
+ const createChannel = (key, tilesPerLine) => {
443
445
  const canvas = document.createElement("canvas");
444
- const decoder = new Decoder();
446
+ const decoder = new Decoder({ tilesPerLine });
445
447
  decoder.update({
446
448
  canvas,
447
449
  tiles: [],
@@ -456,19 +458,21 @@ const createChannel = (key) => {
456
458
  };
457
459
  };
458
460
  class RGBNDecoder {
459
- constructor() {
461
+ constructor(options) {
460
462
  __publicField(this, "canvas");
461
463
  __publicField(this, "palette");
462
464
  __publicField(this, "lockFrame");
463
465
  __publicField(this, "channels");
466
+ __publicField(this, "tilesPerLine");
464
467
  this.canvas = null;
465
468
  this.palette = defaultPalette;
466
469
  this.lockFrame = false;
470
+ this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
467
471
  this.channels = {
468
- r: createChannel(ChannelKey.R),
469
- g: createChannel(ChannelKey.G),
470
- b: createChannel(ChannelKey.B),
471
- n: createChannel(ChannelKey.N)
472
+ r: createChannel(ChannelKey.R, this.tilesPerLine),
473
+ g: createChannel(ChannelKey.G, this.tilesPerLine),
474
+ b: createChannel(ChannelKey.B, this.tilesPerLine),
475
+ n: createChannel(ChannelKey.N, this.tilesPerLine)
472
476
  };
473
477
  }
474
478
  update({
@@ -600,7 +604,7 @@ class RGBNDecoder {
600
604
  }, 0);
601
605
  }
602
606
  getHeight() {
603
- return TILE_PIXEL_HEIGHT * Math.ceil(this.maxTiles() / TILES_PER_LINE);
607
+ return TILE_PIXEL_HEIGHT * Math.ceil(this.maxTiles() / this.tilesPerLine);
604
608
  }
605
609
  }
606
610
 
package/dist/index.d.cts CHANGED
@@ -61,6 +61,9 @@ interface RGBNPalette {
61
61
  n?: number[];
62
62
  blend?: BlendMode;
63
63
  }
64
+ interface DecoderOptions {
65
+ tilesPerLine?: number;
66
+ }
64
67
 
65
68
  declare class Decoder {
66
69
  private canvas;
@@ -70,7 +73,8 @@ declare class Decoder {
70
73
  private lockFrame;
71
74
  private invertPalette;
72
75
  private colorData;
73
- constructor();
76
+ private tilesPerLine;
77
+ constructor(options?: DecoderOptions);
74
78
  update({ canvas, tiles, palette, lockFrame, invertPalette, }: {
75
79
  canvas: HTMLCanvasElement | null;
76
80
  tiles: string[];
@@ -80,7 +84,7 @@ declare class Decoder {
80
84
  }): void;
81
85
  private updateCanvas;
82
86
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
83
- static getScaledCanvasSize(handleExportFrame: ExportFrameMode, height: number): ScaledCanvasSize;
87
+ private getScaledCanvasSize;
84
88
  private getExportTiles;
85
89
  private setCanvas;
86
90
  private setPalette;
@@ -97,7 +101,8 @@ declare class RGBNDecoder {
97
101
  private palette;
98
102
  private lockFrame;
99
103
  private channels;
100
- constructor();
104
+ private tilesPerLine;
105
+ constructor(options: DecoderOptions);
101
106
  update({ canvas, tiles, palette, lockFrame, }: {
102
107
  canvas: HTMLCanvasElement | null;
103
108
  tiles: RGBNTiles;
package/dist/index.d.mts CHANGED
@@ -61,6 +61,9 @@ interface RGBNPalette {
61
61
  n?: number[];
62
62
  blend?: BlendMode;
63
63
  }
64
+ interface DecoderOptions {
65
+ tilesPerLine?: number;
66
+ }
64
67
 
65
68
  declare class Decoder {
66
69
  private canvas;
@@ -70,7 +73,8 @@ declare class Decoder {
70
73
  private lockFrame;
71
74
  private invertPalette;
72
75
  private colorData;
73
- constructor();
76
+ private tilesPerLine;
77
+ constructor(options?: DecoderOptions);
74
78
  update({ canvas, tiles, palette, lockFrame, invertPalette, }: {
75
79
  canvas: HTMLCanvasElement | null;
76
80
  tiles: string[];
@@ -80,7 +84,7 @@ declare class Decoder {
80
84
  }): void;
81
85
  private updateCanvas;
82
86
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
83
- static getScaledCanvasSize(handleExportFrame: ExportFrameMode, height: number): ScaledCanvasSize;
87
+ private getScaledCanvasSize;
84
88
  private getExportTiles;
85
89
  private setCanvas;
86
90
  private setPalette;
@@ -97,7 +101,8 @@ declare class RGBNDecoder {
97
101
  private palette;
98
102
  private lockFrame;
99
103
  private channels;
100
- constructor();
104
+ private tilesPerLine;
105
+ constructor(options: DecoderOptions);
101
106
  update({ canvas, tiles, palette, lockFrame, }: {
102
107
  canvas: HTMLCanvasElement | null;
103
108
  tiles: RGBNTiles;
package/dist/index.d.ts CHANGED
@@ -61,6 +61,9 @@ interface RGBNPalette {
61
61
  n?: number[];
62
62
  blend?: BlendMode;
63
63
  }
64
+ interface DecoderOptions {
65
+ tilesPerLine?: number;
66
+ }
64
67
 
65
68
  declare class Decoder {
66
69
  private canvas;
@@ -70,7 +73,8 @@ declare class Decoder {
70
73
  private lockFrame;
71
74
  private invertPalette;
72
75
  private colorData;
73
- constructor();
76
+ private tilesPerLine;
77
+ constructor(options?: DecoderOptions);
74
78
  update({ canvas, tiles, palette, lockFrame, invertPalette, }: {
75
79
  canvas: HTMLCanvasElement | null;
76
80
  tiles: string[];
@@ -80,7 +84,7 @@ declare class Decoder {
80
84
  }): void;
81
85
  private updateCanvas;
82
86
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
83
- static getScaledCanvasSize(handleExportFrame: ExportFrameMode, height: number): ScaledCanvasSize;
87
+ private getScaledCanvasSize;
84
88
  private getExportTiles;
85
89
  private setCanvas;
86
90
  private setPalette;
@@ -97,7 +101,8 @@ declare class RGBNDecoder {
97
101
  private palette;
98
102
  private lockFrame;
99
103
  private channels;
100
- constructor();
104
+ private tilesPerLine;
105
+ constructor(options: DecoderOptions);
101
106
  update({ canvas, tiles, palette, lockFrame, }: {
102
107
  canvas: HTMLCanvasElement | null;
103
108
  tiles: RGBNTiles;
package/dist/index.mjs CHANGED
@@ -189,7 +189,7 @@ var __publicField$1 = (obj, key, value) => {
189
189
  return value;
190
190
  };
191
191
  class Decoder {
192
- constructor() {
192
+ constructor(options) {
193
193
  __publicField$1(this, "canvas");
194
194
  __publicField$1(this, "tiles");
195
195
  __publicField$1(this, "colors");
@@ -197,6 +197,7 @@ class Decoder {
197
197
  __publicField$1(this, "lockFrame");
198
198
  __publicField$1(this, "invertPalette");
199
199
  __publicField$1(this, "colorData");
200
+ __publicField$1(this, "tilesPerLine");
200
201
  this.canvas = null;
201
202
  this.tiles = [];
202
203
  this.colors = [];
@@ -204,6 +205,7 @@ class Decoder {
204
205
  this.lockFrame = false;
205
206
  this.invertPalette = false;
206
207
  this.colorData = [...BW_PALETTE];
208
+ this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
207
209
  }
208
210
  update({
209
211
  canvas = null,
@@ -254,7 +256,7 @@ class Decoder {
254
256
  initialHeight,
255
257
  initialWidth,
256
258
  tilesPerLine
257
- } = Decoder.getScaledCanvasSize(handleFrameMode, this.getHeight());
259
+ } = this.getScaledCanvasSize(handleFrameMode, this.getHeight());
258
260
  const canvas = document.createElement("canvas");
259
261
  const context = canvas.getContext("2d");
260
262
  if (!context) {
@@ -267,27 +269,27 @@ class Decoder {
267
269
  });
268
270
  return canvas;
269
271
  }
270
- static getScaledCanvasSize(handleExportFrame, height) {
272
+ getScaledCanvasSize(handleExportFrame, height) {
271
273
  const FRAME_TILES = 4;
272
274
  switch (handleExportFrame) {
273
275
  case ExportFrameMode.FRAMEMODE_KEEP:
274
276
  return {
275
277
  initialHeight: height,
276
- initialWidth: TILES_PER_LINE * TILE_PIXEL_WIDTH,
277
- tilesPerLine: TILES_PER_LINE
278
+ initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH,
279
+ tilesPerLine: this.tilesPerLine
278
280
  };
279
281
  case ExportFrameMode.FRAMEMODE_CROP:
280
282
  return {
281
283
  initialHeight: height - TILE_PIXEL_HEIGHT * FRAME_TILES,
282
- initialWidth: TILES_PER_LINE * TILE_PIXEL_WIDTH - TILE_PIXEL_WIDTH * FRAME_TILES,
283
- tilesPerLine: TILES_PER_LINE - FRAME_TILES
284
+ initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH - TILE_PIXEL_WIDTH * FRAME_TILES,
285
+ tilesPerLine: this.tilesPerLine - FRAME_TILES
284
286
  };
285
287
  case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
286
288
  case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
287
289
  return {
288
290
  initialHeight: height + 2 * TILE_PIXEL_HEIGHT,
289
- initialWidth: TILES_PER_LINE * TILE_PIXEL_WIDTH,
290
- tilesPerLine: TILES_PER_LINE
291
+ initialWidth: this.tilesPerLine * TILE_PIXEL_WIDTH,
292
+ tilesPerLine: this.tilesPerLine
291
293
  };
292
294
  default:
293
295
  throw new Error(`unknown export mode ${handleExportFrame}`);
@@ -369,8 +371,8 @@ class Decoder {
369
371
  if (!this.rawImageData) {
370
372
  return;
371
373
  }
372
- const tileXOffset = index % TILES_PER_LINE;
373
- const tileYOffset = Math.floor(index / TILES_PER_LINE);
374
+ const tileXOffset = index % this.tilesPerLine;
375
+ const tileYOffset = Math.floor(index / this.tilesPerLine);
374
376
  const pixelXOffset = TILE_PIXEL_WIDTH * tileXOffset;
375
377
  const pixelYOffset = TILE_PIXEL_HEIGHT * tileYOffset;
376
378
  for (let x = 0; x < TILE_PIXEL_WIDTH; x += 1) {
@@ -419,7 +421,7 @@ class Decoder {
419
421
  }
420
422
  }
421
423
  getHeight() {
422
- return TILE_PIXEL_HEIGHT * Math.ceil(this.tiles.length / TILES_PER_LINE);
424
+ return TILE_PIXEL_HEIGHT * Math.ceil(this.tiles.length / this.tilesPerLine);
423
425
  }
424
426
  }
425
427
 
@@ -437,9 +439,9 @@ var __publicField = (obj, key, value) => {
437
439
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
438
440
  return value;
439
441
  };
440
- const createChannel = (key) => {
442
+ const createChannel = (key, tilesPerLine) => {
441
443
  const canvas = document.createElement("canvas");
442
- const decoder = new Decoder();
444
+ const decoder = new Decoder({ tilesPerLine });
443
445
  decoder.update({
444
446
  canvas,
445
447
  tiles: [],
@@ -454,19 +456,21 @@ const createChannel = (key) => {
454
456
  };
455
457
  };
456
458
  class RGBNDecoder {
457
- constructor() {
459
+ constructor(options) {
458
460
  __publicField(this, "canvas");
459
461
  __publicField(this, "palette");
460
462
  __publicField(this, "lockFrame");
461
463
  __publicField(this, "channels");
464
+ __publicField(this, "tilesPerLine");
462
465
  this.canvas = null;
463
466
  this.palette = defaultPalette;
464
467
  this.lockFrame = false;
468
+ this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
465
469
  this.channels = {
466
- r: createChannel(ChannelKey.R),
467
- g: createChannel(ChannelKey.G),
468
- b: createChannel(ChannelKey.B),
469
- n: createChannel(ChannelKey.N)
470
+ r: createChannel(ChannelKey.R, this.tilesPerLine),
471
+ g: createChannel(ChannelKey.G, this.tilesPerLine),
472
+ b: createChannel(ChannelKey.B, this.tilesPerLine),
473
+ n: createChannel(ChannelKey.N, this.tilesPerLine)
470
474
  };
471
475
  }
472
476
  update({
@@ -598,7 +602,7 @@ class RGBNDecoder {
598
602
  }, 0);
599
603
  }
600
604
  getHeight() {
601
- return TILE_PIXEL_HEIGHT * Math.ceil(this.maxTiles() / TILES_PER_LINE);
605
+ return TILE_PIXEL_HEIGHT * Math.ceil(this.maxTiles() / this.tilesPerLine);
602
606
  }
603
607
  }
604
608
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gb-image-decoder",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Decoder classes for GameBoy-encoded images",
5
5
  "repository": "",
6
6
  "license": "MIT",