gb-image-decoder 1.3.0 → 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
@@ -285,7 +261,7 @@ class Decoder {
285
261
  }
286
262
  getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
287
263
  let handleFrameMode = handleExportFrame;
288
- if ((this.tiles.length !== 360 || this.tilesPerLine !== TILES_PER_LINE) && handleFrameMode !== ExportFrameMode.FRAMEMODE_KEEP) {
264
+ if (this.tilesPerLine !== TILES_PER_LINE && handleFrameMode !== ExportFrameMode.FRAMEMODE_KEEP) {
289
265
  handleFrameMode = ExportFrameMode.FRAMEMODE_KEEP;
290
266
  }
291
267
  const {
@@ -306,7 +282,6 @@ class Decoder {
306
282
  return canvas;
307
283
  }
308
284
  getScaledCanvasSize(handleExportFrame) {
309
- const FRAME_TILES = 4;
310
285
  const width = this.getWidth();
311
286
  const height = this.getHeight();
312
287
  switch (handleExportFrame) {
@@ -318,14 +293,14 @@ class Decoder {
318
293
  };
319
294
  case ExportFrameMode.FRAMEMODE_CROP:
320
295
  return {
321
- initialHeight: height - TILE_PIXEL_HEIGHT * FRAME_TILES,
322
- initialWidth: width - TILE_PIXEL_WIDTH * FRAME_TILES,
323
- 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
324
299
  };
325
300
  case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
326
301
  case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
327
302
  return {
328
- initialHeight: height + 2 * TILE_PIXEL_HEIGHT,
303
+ initialHeight: width,
329
304
  initialWidth: width,
330
305
  tilesPerLine: this.tilesPerLine
331
306
  };
@@ -341,23 +316,34 @@ class Decoder {
341
316
  case ExportFrameMode.FRAMEMODE_KEEP:
342
317
  return this.tiles;
343
318
  case ExportFrameMode.FRAMEMODE_CROP:
344
- return this.tiles.reduce((acc, tile, index) => tileIndexIsPartOfFrame(index, this.imageStartLine, ExportFrameMode.FRAMEMODE_KEEP) ? acc : [...acc, tile], []);
319
+ return this.getCroppedTiles();
345
320
  case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
346
321
  return [
347
322
  ...BLACK_LINE,
348
- ...this.tiles,
323
+ ...this.getDefaultImageRange(),
349
324
  ...BLACK_LINE
350
325
  ];
351
326
  case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
352
327
  return [
353
328
  ...WHITE_LINE,
354
- ...this.tiles,
329
+ ...this.getDefaultImageRange(),
355
330
  ...WHITE_LINE
356
331
  ];
357
332
  default:
358
333
  throw new Error(`unknown export mode ${handleExportFrame}`);
359
334
  }
360
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
+ }
361
347
  setCanvas(canvas) {
362
348
  if (this.canvas === canvas) {
363
349
  return false;
package/dist/index.d.cts CHANGED
@@ -102,6 +102,8 @@ declare class Decoder {
102
102
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
103
103
  private getScaledCanvasSize;
104
104
  private getExportTiles;
105
+ private getCroppedTiles;
106
+ private getDefaultImageRange;
105
107
  private setCanvas;
106
108
  private setPalettes;
107
109
  private setTiles;
package/dist/index.d.mts CHANGED
@@ -102,6 +102,8 @@ declare class Decoder {
102
102
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
103
103
  private getScaledCanvasSize;
104
104
  private getExportTiles;
105
+ private getCroppedTiles;
106
+ private getDefaultImageRange;
105
107
  private setCanvas;
106
108
  private setPalettes;
107
109
  private setTiles;
package/dist/index.d.ts CHANGED
@@ -102,6 +102,8 @@ declare class Decoder {
102
102
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
103
103
  private getScaledCanvasSize;
104
104
  private getExportTiles;
105
+ private getCroppedTiles;
106
+ private getDefaultImageRange;
105
107
  private setCanvas;
106
108
  private setPalettes;
107
109
  private setTiles;
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
@@ -283,7 +259,7 @@ class Decoder {
283
259
  }
284
260
  getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
285
261
  let handleFrameMode = handleExportFrame;
286
- if ((this.tiles.length !== 360 || this.tilesPerLine !== TILES_PER_LINE) && handleFrameMode !== ExportFrameMode.FRAMEMODE_KEEP) {
262
+ if (this.tilesPerLine !== TILES_PER_LINE && handleFrameMode !== ExportFrameMode.FRAMEMODE_KEEP) {
287
263
  handleFrameMode = ExportFrameMode.FRAMEMODE_KEEP;
288
264
  }
289
265
  const {
@@ -304,7 +280,6 @@ class Decoder {
304
280
  return canvas;
305
281
  }
306
282
  getScaledCanvasSize(handleExportFrame) {
307
- const FRAME_TILES = 4;
308
283
  const width = this.getWidth();
309
284
  const height = this.getHeight();
310
285
  switch (handleExportFrame) {
@@ -316,14 +291,14 @@ class Decoder {
316
291
  };
317
292
  case ExportFrameMode.FRAMEMODE_CROP:
318
293
  return {
319
- initialHeight: height - TILE_PIXEL_HEIGHT * FRAME_TILES,
320
- initialWidth: width - TILE_PIXEL_WIDTH * FRAME_TILES,
321
- 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
322
297
  };
323
298
  case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
324
299
  case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
325
300
  return {
326
- initialHeight: height + 2 * TILE_PIXEL_HEIGHT,
301
+ initialHeight: width,
327
302
  initialWidth: width,
328
303
  tilesPerLine: this.tilesPerLine
329
304
  };
@@ -339,23 +314,34 @@ class Decoder {
339
314
  case ExportFrameMode.FRAMEMODE_KEEP:
340
315
  return this.tiles;
341
316
  case ExportFrameMode.FRAMEMODE_CROP:
342
- return this.tiles.reduce((acc, tile, index) => tileIndexIsPartOfFrame(index, this.imageStartLine, ExportFrameMode.FRAMEMODE_KEEP) ? acc : [...acc, tile], []);
317
+ return this.getCroppedTiles();
343
318
  case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
344
319
  return [
345
320
  ...BLACK_LINE,
346
- ...this.tiles,
321
+ ...this.getDefaultImageRange(),
347
322
  ...BLACK_LINE
348
323
  ];
349
324
  case ExportFrameMode.FRAMEMODE_SQUARE_WHITE:
350
325
  return [
351
326
  ...WHITE_LINE,
352
- ...this.tiles,
327
+ ...this.getDefaultImageRange(),
353
328
  ...WHITE_LINE
354
329
  ];
355
330
  default:
356
331
  throw new Error(`unknown export mode ${handleExportFrame}`);
357
332
  }
358
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
+ }
359
345
  setCanvas(canvas) {
360
346
  if (this.canvas === canvas) {
361
347
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gb-image-decoder",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Decoder classes for GameBoy-encoded images",
5
5
  "repository": "",
6
6
  "license": "MIT",
@@ -22,8 +22,9 @@
22
22
  "prepack": "npm run eslint && npm run test && npm run build",
23
23
  "build": "unbuild",
24
24
  "dev": "vitest dev",
25
- "test": "rimraf images && vitest run",
26
- "test:u": "rimraf images && vitest run -u",
25
+ "test:clean": "rimraf images/*",
26
+ "test": "npm run test:clean && vitest run",
27
+ "test:u": "npm run test:clean && vitest run -u",
27
28
  "eslint": "eslint -c .eslintrc --ext .js,.ts ."
28
29
  },
29
30
  "devDependencies": {