gb-image-decoder 1.0.0 → 1.2.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/README.md CHANGED
@@ -39,8 +39,8 @@ decoder.update({
39
39
  canvas: myCanvas,
40
40
  tiles: myTiles,
41
41
  palette: monoPalette,
42
+ framePalette: monoPalette,
42
43
  lockFrame: false,
43
- invertPalette: false,
44
44
  });
45
45
  ```
46
46
 
package/dist/index.cjs CHANGED
@@ -59,6 +59,7 @@ const TILE_PIXEL_WIDTH = 8;
59
59
  const TILE_PIXEL_HEIGHT = 8;
60
60
  const TILES_PER_LINE = 20;
61
61
  const BW_PALETTE = [16777215, 11184810, 5592405, 0];
62
+ const BW_PALETTE_HEX = ["#ffffff", "#aaaaaa", "#555555", "#000000"];
62
63
  const BLACK_LINE = [
63
64
  BLACK,
64
65
  BLACK,
@@ -144,12 +145,12 @@ const decodeTile = (rawBytes = BLACK) => {
144
145
  return pixels;
145
146
  };
146
147
 
147
- const tileIndexIsPartOfFrame = (tileIndex, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) => {
148
+ const tileIndexIsPartOfFrame = (tileIndex, imageStartLine, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) => {
148
149
  const checkIndex = tileIndex - (handleExportFrame === ExportFrameMode.FRAMEMODE_KEEP ? 0 : 20);
149
- if (checkIndex < 40) {
150
+ if (checkIndex < imageStartLine * 20) {
150
151
  return true;
151
152
  }
152
- if (checkIndex >= 320) {
153
+ if (checkIndex >= imageStartLine * 20 + 280) {
153
154
  return true;
154
155
  }
155
156
  switch (checkIndex % 20) {
@@ -167,14 +168,13 @@ const getRGBValue = ({
167
168
  pixels,
168
169
  index,
169
170
  tileIndex,
171
+ imageStartLine,
170
172
  handleExportFrame,
171
- lockFrame,
172
- invertPalette,
173
- colorData
173
+ colorData,
174
+ frameColorData
174
175
  }) => {
175
- const palette = lockFrame && // Must be actually locked
176
- handleExportFrame !== ExportFrameMode.FRAMEMODE_CROP && tileIndexIsPartOfFrame(tileIndex, handleExportFrame) ? BW_PALETTE : colorData;
177
- const value = invertPalette ? palette[3 - pixels[index]] : palette[pixels[index]];
176
+ const palette = tileIndexIsPartOfFrame(tileIndex, imageStartLine, handleExportFrame) ? frameColorData : colorData;
177
+ const value = palette[pixels[index]];
178
178
  return {
179
179
  // eslint-disable-next-line no-bitwise
180
180
  r: (value & 16711680) >> 16,
@@ -196,31 +196,35 @@ class Decoder {
196
196
  __publicField$1(this, "canvas");
197
197
  __publicField$1(this, "tiles");
198
198
  __publicField$1(this, "colors");
199
+ __publicField$1(this, "frameColors");
199
200
  __publicField$1(this, "rawImageData");
200
201
  __publicField$1(this, "lockFrame");
201
- __publicField$1(this, "invertPalette");
202
202
  __publicField$1(this, "colorData");
203
+ __publicField$1(this, "frameColorData");
203
204
  __publicField$1(this, "tilesPerLine");
205
+ __publicField$1(this, "imageStartLine");
204
206
  this.canvas = null;
205
207
  this.tiles = [];
206
208
  this.colors = [];
209
+ this.frameColors = [];
207
210
  this.rawImageData = null;
208
211
  this.lockFrame = false;
209
- this.invertPalette = false;
210
212
  this.colorData = [...BW_PALETTE];
213
+ this.frameColorData = [...BW_PALETTE];
211
214
  this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
215
+ this.imageStartLine = 2;
212
216
  }
213
217
  update({
214
- canvas = null,
215
- tiles = [],
216
- palette = [],
217
- lockFrame = false,
218
- invertPalette = false
218
+ canvas,
219
+ tiles,
220
+ palette,
221
+ framePalette,
222
+ imageStartLine = 2
219
223
  }) {
224
+ const startLineChanged = this.setImageStartLine(imageStartLine);
220
225
  const canvasChanged = canvas ? this.setCanvas(canvas) : false;
221
- const paletteChanged = palette ? this.setPalette(palette, invertPalette) : false;
222
- const lockFrameChanged = this.setLockFrame(lockFrame);
223
- if (canvasChanged || paletteChanged || lockFrameChanged || !this.tiles.length) {
226
+ const palettesChanged = this.setPalettes(palette, framePalette);
227
+ if (startLineChanged || canvasChanged || palettesChanged || !this.tiles.length) {
224
228
  this.tiles = [];
225
229
  }
226
230
  const tilesChanged = this.setTiles(tiles);
@@ -245,6 +249,13 @@ class Decoder {
245
249
  });
246
250
  this.updateCanvas(newHeight);
247
251
  }
252
+ setImageStartLine(imageStartLine) {
253
+ if (this.imageStartLine === imageStartLine) {
254
+ return false;
255
+ }
256
+ this.imageStartLine = imageStartLine;
257
+ return true;
258
+ }
248
259
  updateCanvas(newHeight) {
249
260
  if (!this.canvas || !this.rawImageData?.length) {
250
261
  return;
@@ -306,7 +317,7 @@ class Decoder {
306
317
  case ExportFrameMode.FRAMEMODE_KEEP:
307
318
  return this.tiles;
308
319
  case ExportFrameMode.FRAMEMODE_CROP:
309
- return this.tiles.reduce((acc, tile, index) => tileIndexIsPartOfFrame(index, ExportFrameMode.FRAMEMODE_KEEP) ? acc : [...acc, tile], []);
320
+ return this.tiles.reduce((acc, tile, index) => tileIndexIsPartOfFrame(index, this.imageStartLine, ExportFrameMode.FRAMEMODE_KEEP) ? acc : [...acc, tile], []);
310
321
  case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
311
322
  return [
312
323
  ...BLACK_LINE,
@@ -330,24 +341,20 @@ class Decoder {
330
341
  this.canvas = canvas;
331
342
  return true;
332
343
  }
333
- setPalette(palette, invertPalette) {
334
- if (this.colors[0] === palette[0] && this.colors[1] === palette[1] && this.colors[2] === palette[2] && this.colors[3] === palette[3] && this.invertPalette === invertPalette) {
344
+ setPalettes(palette, framePalette) {
345
+ if (this.colors[0] === palette[0] && this.colors[1] === palette[1] && this.colors[2] === palette[2] && this.colors[3] === palette[3] && this.frameColors[0] === framePalette[0] && this.frameColors[1] === framePalette[1] && this.frameColors[2] === framePalette[2] && this.frameColors[3] === framePalette[3]) {
335
346
  return false;
336
347
  }
337
348
  this.colors = palette;
338
- this.invertPalette = invertPalette;
349
+ this.frameColors = framePalette;
339
350
  this.colors.forEach((color, index) => {
340
351
  this.colorData[index] = color.length !== 7 ? BW_PALETTE[index] : parseInt(color.substring(1), 16);
341
352
  });
353
+ this.frameColors.forEach((color, index) => {
354
+ this.frameColorData[index] = color.length !== 7 ? BW_PALETTE[index] : parseInt(color.substring(1), 16);
355
+ });
342
356
  return true;
343
357
  }
344
- setLockFrame(lockFrame) {
345
- if (lockFrame !== this.lockFrame) {
346
- this.lockFrame = lockFrame;
347
- return true;
348
- }
349
- return false;
350
- }
351
358
  setTiles(tiles) {
352
359
  const changedTiles = tiles.reduce((acc, newTile, index) => {
353
360
  const changed = newTile !== this.tiles[index];
@@ -390,10 +397,10 @@ class Decoder {
390
397
  pixels,
391
398
  index: y * TILE_PIXEL_WIDTH + x,
392
399
  tileIndex: index,
400
+ imageStartLine: this.imageStartLine,
393
401
  handleExportFrame: ExportFrameMode.FRAMEMODE_KEEP,
394
- invertPalette: this.invertPalette,
395
- lockFrame: this.lockFrame,
396
- colorData: this.colorData
402
+ colorData: this.colorData,
403
+ frameColorData: this.frameColorData
397
404
  });
398
405
  this.rawImageData[rawIndex] = color.r;
399
406
  this.rawImageData[rawIndex + 1] = color.g;
@@ -419,10 +426,10 @@ class Decoder {
419
426
  pixels,
420
427
  index: y * TILE_PIXEL_WIDTH + x,
421
428
  tileIndex: index,
429
+ imageStartLine: this.imageStartLine,
422
430
  handleExportFrame,
423
- lockFrame: this.lockFrame,
424
- invertPalette: this.invertPalette,
425
- colorData: this.colorData
431
+ colorData: this.colorData,
432
+ frameColorData: this.frameColorData
426
433
  });
427
434
  canvasContext.fillStyle = `rgb(${color.r}, ${color.g}, ${color.b})`;
428
435
  canvasContext.fillRect(
@@ -457,11 +464,10 @@ const createChannel = (key, tilesPerLine) => {
457
464
  const canvas = document.createElement("canvas");
458
465
  const decoder = new Decoder({ tilesPerLine });
459
466
  decoder.update({
467
+ framePalette: [],
460
468
  canvas,
461
469
  tiles: [],
462
- palette: [],
463
- lockFrame: false,
464
- invertPalette: false
470
+ palette: []
465
471
  });
466
472
  return {
467
473
  key,
@@ -488,8 +494,8 @@ class RGBNDecoder {
488
494
  };
489
495
  }
490
496
  update({
491
- canvas = null,
492
- tiles = {},
497
+ canvas,
498
+ tiles,
493
499
  palette,
494
500
  lockFrame = false
495
501
  }) {
@@ -558,10 +564,9 @@ class RGBNDecoder {
558
564
  paletteFunction(channelColors[0])
559
565
  ];
560
566
  channel.decoder.update({
561
- invertPalette: false,
562
567
  canvas: channel.canvas,
563
568
  tiles: channel.tiles,
564
- lockFrame: this.lockFrame,
569
+ framePalette: this.lockFrame ? BW_PALETTE_HEX : palette,
565
570
  palette
566
571
  });
567
572
  return {
@@ -623,6 +628,7 @@ class RGBNDecoder {
623
628
  const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]?.length || 0));
624
629
 
625
630
  exports.BW_PALETTE = BW_PALETTE;
631
+ exports.BW_PALETTE_HEX = BW_PALETTE_HEX;
626
632
  exports.BlendMode = BlendMode;
627
633
  exports.Decoder = Decoder;
628
634
  exports.ExportFrameMode = ExportFrameMode;
package/dist/index.d.cts CHANGED
@@ -64,31 +64,40 @@ interface RGBNPalette {
64
64
  interface DecoderOptions {
65
65
  tilesPerLine?: number;
66
66
  }
67
+ interface DecoderUpdateParams {
68
+ canvas: HTMLCanvasElement | null;
69
+ tiles: string[];
70
+ palette: string[];
71
+ framePalette: string[];
72
+ imageStartLine?: number;
73
+ }
74
+ interface RGBNDecoderUpdateParams {
75
+ canvas: HTMLCanvasElement | null;
76
+ tiles: RGBNTiles;
77
+ palette: RGBNPalette;
78
+ lockFrame?: boolean;
79
+ }
67
80
 
68
81
  declare class Decoder {
69
82
  private canvas;
70
83
  private tiles;
71
84
  private colors;
85
+ private frameColors;
72
86
  private rawImageData;
73
87
  private lockFrame;
74
- private invertPalette;
75
88
  private colorData;
89
+ private frameColorData;
76
90
  private tilesPerLine;
91
+ private imageStartLine;
77
92
  constructor(options?: DecoderOptions);
78
- update({ canvas, tiles, palette, lockFrame, invertPalette, }: {
79
- canvas: HTMLCanvasElement | null;
80
- tiles: string[];
81
- palette: string[];
82
- lockFrame: boolean;
83
- invertPalette: boolean;
84
- }): void;
93
+ update({ canvas, tiles, palette, framePalette, imageStartLine, }: DecoderUpdateParams): void;
94
+ private setImageStartLine;
85
95
  private updateCanvas;
86
96
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
87
97
  private getScaledCanvasSize;
88
98
  private getExportTiles;
89
99
  private setCanvas;
90
- private setPalette;
91
- private setLockFrame;
100
+ private setPalettes;
92
101
  private setTiles;
93
102
  private renderTile;
94
103
  private paintTile;
@@ -103,12 +112,7 @@ declare class RGBNDecoder {
103
112
  private channels;
104
113
  private tilesPerLine;
105
114
  constructor(options?: DecoderOptions);
106
- update({ canvas, tiles, palette, lockFrame, }: {
107
- canvas: HTMLCanvasElement | null;
108
- tiles: RGBNTiles;
109
- palette: RGBNPalette;
110
- lockFrame: boolean;
111
- }): void;
115
+ update({ canvas, tiles, palette, lockFrame, }: RGBNDecoderUpdateParams): void;
112
116
  private setPalette;
113
117
  private setCanvas;
114
118
  private setLockFrame;
@@ -121,21 +125,22 @@ declare class RGBNDecoder {
121
125
 
122
126
  declare const decodeTile: (rawBytes?: string) => IndexedTilePixels;
123
127
 
124
- declare const getRGBValue: ({ pixels, index, tileIndex, handleExportFrame, lockFrame, invertPalette, colorData, }: {
128
+ declare const getRGBValue: ({ pixels, index, tileIndex, imageStartLine, handleExportFrame, colorData, frameColorData, }: {
125
129
  pixels: IndexedTilePixels;
126
130
  index: number;
127
131
  tileIndex: number;
132
+ imageStartLine: number;
128
133
  handleExportFrame: ExportFrameMode;
129
- lockFrame: boolean;
130
- invertPalette: boolean;
131
134
  colorData: BWPalette;
135
+ frameColorData: BWPalette;
132
136
  }) => RGBValue;
133
137
 
134
- declare const tileIndexIsPartOfFrame: (tileIndex: number, handleExportFrame?: ExportFrameMode) => boolean;
138
+ declare const tileIndexIsPartOfFrame: (tileIndex: number, imageStartLine: number, handleExportFrame?: ExportFrameMode) => boolean;
135
139
 
136
140
  declare const maxTiles: (rgbnTiles: RGBNTiles) => number;
137
141
 
138
142
  declare const SKIP_LINE = "skip";
139
143
  declare const BW_PALETTE: number[];
144
+ declare const BW_PALETTE_HEX: string[];
140
145
 
141
- export { type BWPalette, BW_PALETTE, BlendMode, type ChangedTile, type Channel, type Channels, Decoder, ExportFrameMode, type IndexedTilePixels, RGBNDecoder, type RGBNPalette, type RGBNTiles, type RGBValue, SKIP_LINE, type ScaledCanvasSize, type SourceCanvases, blendModeNewName, decodeTile, getRGBValue, maxTiles, tileIndexIsPartOfFrame };
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 };
package/dist/index.d.mts CHANGED
@@ -64,31 +64,40 @@ interface RGBNPalette {
64
64
  interface DecoderOptions {
65
65
  tilesPerLine?: number;
66
66
  }
67
+ interface DecoderUpdateParams {
68
+ canvas: HTMLCanvasElement | null;
69
+ tiles: string[];
70
+ palette: string[];
71
+ framePalette: string[];
72
+ imageStartLine?: number;
73
+ }
74
+ interface RGBNDecoderUpdateParams {
75
+ canvas: HTMLCanvasElement | null;
76
+ tiles: RGBNTiles;
77
+ palette: RGBNPalette;
78
+ lockFrame?: boolean;
79
+ }
67
80
 
68
81
  declare class Decoder {
69
82
  private canvas;
70
83
  private tiles;
71
84
  private colors;
85
+ private frameColors;
72
86
  private rawImageData;
73
87
  private lockFrame;
74
- private invertPalette;
75
88
  private colorData;
89
+ private frameColorData;
76
90
  private tilesPerLine;
91
+ private imageStartLine;
77
92
  constructor(options?: DecoderOptions);
78
- update({ canvas, tiles, palette, lockFrame, invertPalette, }: {
79
- canvas: HTMLCanvasElement | null;
80
- tiles: string[];
81
- palette: string[];
82
- lockFrame: boolean;
83
- invertPalette: boolean;
84
- }): void;
93
+ update({ canvas, tiles, palette, framePalette, imageStartLine, }: DecoderUpdateParams): void;
94
+ private setImageStartLine;
85
95
  private updateCanvas;
86
96
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
87
97
  private getScaledCanvasSize;
88
98
  private getExportTiles;
89
99
  private setCanvas;
90
- private setPalette;
91
- private setLockFrame;
100
+ private setPalettes;
92
101
  private setTiles;
93
102
  private renderTile;
94
103
  private paintTile;
@@ -103,12 +112,7 @@ declare class RGBNDecoder {
103
112
  private channels;
104
113
  private tilesPerLine;
105
114
  constructor(options?: DecoderOptions);
106
- update({ canvas, tiles, palette, lockFrame, }: {
107
- canvas: HTMLCanvasElement | null;
108
- tiles: RGBNTiles;
109
- palette: RGBNPalette;
110
- lockFrame: boolean;
111
- }): void;
115
+ update({ canvas, tiles, palette, lockFrame, }: RGBNDecoderUpdateParams): void;
112
116
  private setPalette;
113
117
  private setCanvas;
114
118
  private setLockFrame;
@@ -121,21 +125,22 @@ declare class RGBNDecoder {
121
125
 
122
126
  declare const decodeTile: (rawBytes?: string) => IndexedTilePixels;
123
127
 
124
- declare const getRGBValue: ({ pixels, index, tileIndex, handleExportFrame, lockFrame, invertPalette, colorData, }: {
128
+ declare const getRGBValue: ({ pixels, index, tileIndex, imageStartLine, handleExportFrame, colorData, frameColorData, }: {
125
129
  pixels: IndexedTilePixels;
126
130
  index: number;
127
131
  tileIndex: number;
132
+ imageStartLine: number;
128
133
  handleExportFrame: ExportFrameMode;
129
- lockFrame: boolean;
130
- invertPalette: boolean;
131
134
  colorData: BWPalette;
135
+ frameColorData: BWPalette;
132
136
  }) => RGBValue;
133
137
 
134
- declare const tileIndexIsPartOfFrame: (tileIndex: number, handleExportFrame?: ExportFrameMode) => boolean;
138
+ declare const tileIndexIsPartOfFrame: (tileIndex: number, imageStartLine: number, handleExportFrame?: ExportFrameMode) => boolean;
135
139
 
136
140
  declare const maxTiles: (rgbnTiles: RGBNTiles) => number;
137
141
 
138
142
  declare const SKIP_LINE = "skip";
139
143
  declare const BW_PALETTE: number[];
144
+ declare const BW_PALETTE_HEX: string[];
140
145
 
141
- export { type BWPalette, BW_PALETTE, BlendMode, type ChangedTile, type Channel, type Channels, Decoder, ExportFrameMode, type IndexedTilePixels, RGBNDecoder, type RGBNPalette, type RGBNTiles, type RGBValue, SKIP_LINE, type ScaledCanvasSize, type SourceCanvases, blendModeNewName, decodeTile, getRGBValue, maxTiles, tileIndexIsPartOfFrame };
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 };
package/dist/index.d.ts CHANGED
@@ -64,31 +64,40 @@ interface RGBNPalette {
64
64
  interface DecoderOptions {
65
65
  tilesPerLine?: number;
66
66
  }
67
+ interface DecoderUpdateParams {
68
+ canvas: HTMLCanvasElement | null;
69
+ tiles: string[];
70
+ palette: string[];
71
+ framePalette: string[];
72
+ imageStartLine?: number;
73
+ }
74
+ interface RGBNDecoderUpdateParams {
75
+ canvas: HTMLCanvasElement | null;
76
+ tiles: RGBNTiles;
77
+ palette: RGBNPalette;
78
+ lockFrame?: boolean;
79
+ }
67
80
 
68
81
  declare class Decoder {
69
82
  private canvas;
70
83
  private tiles;
71
84
  private colors;
85
+ private frameColors;
72
86
  private rawImageData;
73
87
  private lockFrame;
74
- private invertPalette;
75
88
  private colorData;
89
+ private frameColorData;
76
90
  private tilesPerLine;
91
+ private imageStartLine;
77
92
  constructor(options?: DecoderOptions);
78
- update({ canvas, tiles, palette, lockFrame, invertPalette, }: {
79
- canvas: HTMLCanvasElement | null;
80
- tiles: string[];
81
- palette: string[];
82
- lockFrame: boolean;
83
- invertPalette: boolean;
84
- }): void;
93
+ update({ canvas, tiles, palette, framePalette, imageStartLine, }: DecoderUpdateParams): void;
94
+ private setImageStartLine;
85
95
  private updateCanvas;
86
96
  getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
87
97
  private getScaledCanvasSize;
88
98
  private getExportTiles;
89
99
  private setCanvas;
90
- private setPalette;
91
- private setLockFrame;
100
+ private setPalettes;
92
101
  private setTiles;
93
102
  private renderTile;
94
103
  private paintTile;
@@ -103,12 +112,7 @@ declare class RGBNDecoder {
103
112
  private channels;
104
113
  private tilesPerLine;
105
114
  constructor(options?: DecoderOptions);
106
- update({ canvas, tiles, palette, lockFrame, }: {
107
- canvas: HTMLCanvasElement | null;
108
- tiles: RGBNTiles;
109
- palette: RGBNPalette;
110
- lockFrame: boolean;
111
- }): void;
115
+ update({ canvas, tiles, palette, lockFrame, }: RGBNDecoderUpdateParams): void;
112
116
  private setPalette;
113
117
  private setCanvas;
114
118
  private setLockFrame;
@@ -121,21 +125,22 @@ declare class RGBNDecoder {
121
125
 
122
126
  declare const decodeTile: (rawBytes?: string) => IndexedTilePixels;
123
127
 
124
- declare const getRGBValue: ({ pixels, index, tileIndex, handleExportFrame, lockFrame, invertPalette, colorData, }: {
128
+ declare const getRGBValue: ({ pixels, index, tileIndex, imageStartLine, handleExportFrame, colorData, frameColorData, }: {
125
129
  pixels: IndexedTilePixels;
126
130
  index: number;
127
131
  tileIndex: number;
132
+ imageStartLine: number;
128
133
  handleExportFrame: ExportFrameMode;
129
- lockFrame: boolean;
130
- invertPalette: boolean;
131
134
  colorData: BWPalette;
135
+ frameColorData: BWPalette;
132
136
  }) => RGBValue;
133
137
 
134
- declare const tileIndexIsPartOfFrame: (tileIndex: number, handleExportFrame?: ExportFrameMode) => boolean;
138
+ declare const tileIndexIsPartOfFrame: (tileIndex: number, imageStartLine: number, handleExportFrame?: ExportFrameMode) => boolean;
135
139
 
136
140
  declare const maxTiles: (rgbnTiles: RGBNTiles) => number;
137
141
 
138
142
  declare const SKIP_LINE = "skip";
139
143
  declare const BW_PALETTE: number[];
144
+ declare const BW_PALETTE_HEX: string[];
140
145
 
141
- export { type BWPalette, BW_PALETTE, BlendMode, type ChangedTile, type Channel, type Channels, Decoder, ExportFrameMode, type IndexedTilePixels, RGBNDecoder, type RGBNPalette, type RGBNTiles, type RGBValue, SKIP_LINE, type ScaledCanvasSize, type SourceCanvases, blendModeNewName, decodeTile, getRGBValue, maxTiles, tileIndexIsPartOfFrame };
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 };
package/dist/index.mjs CHANGED
@@ -57,6 +57,7 @@ const TILE_PIXEL_WIDTH = 8;
57
57
  const TILE_PIXEL_HEIGHT = 8;
58
58
  const TILES_PER_LINE = 20;
59
59
  const BW_PALETTE = [16777215, 11184810, 5592405, 0];
60
+ const BW_PALETTE_HEX = ["#ffffff", "#aaaaaa", "#555555", "#000000"];
60
61
  const BLACK_LINE = [
61
62
  BLACK,
62
63
  BLACK,
@@ -142,12 +143,12 @@ const decodeTile = (rawBytes = BLACK) => {
142
143
  return pixels;
143
144
  };
144
145
 
145
- const tileIndexIsPartOfFrame = (tileIndex, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) => {
146
+ const tileIndexIsPartOfFrame = (tileIndex, imageStartLine, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) => {
146
147
  const checkIndex = tileIndex - (handleExportFrame === ExportFrameMode.FRAMEMODE_KEEP ? 0 : 20);
147
- if (checkIndex < 40) {
148
+ if (checkIndex < imageStartLine * 20) {
148
149
  return true;
149
150
  }
150
- if (checkIndex >= 320) {
151
+ if (checkIndex >= imageStartLine * 20 + 280) {
151
152
  return true;
152
153
  }
153
154
  switch (checkIndex % 20) {
@@ -165,14 +166,13 @@ const getRGBValue = ({
165
166
  pixels,
166
167
  index,
167
168
  tileIndex,
169
+ imageStartLine,
168
170
  handleExportFrame,
169
- lockFrame,
170
- invertPalette,
171
- colorData
171
+ colorData,
172
+ frameColorData
172
173
  }) => {
173
- const palette = lockFrame && // Must be actually locked
174
- handleExportFrame !== ExportFrameMode.FRAMEMODE_CROP && tileIndexIsPartOfFrame(tileIndex, handleExportFrame) ? BW_PALETTE : colorData;
175
- const value = invertPalette ? palette[3 - pixels[index]] : palette[pixels[index]];
174
+ const palette = tileIndexIsPartOfFrame(tileIndex, imageStartLine, handleExportFrame) ? frameColorData : colorData;
175
+ const value = palette[pixels[index]];
176
176
  return {
177
177
  // eslint-disable-next-line no-bitwise
178
178
  r: (value & 16711680) >> 16,
@@ -194,31 +194,35 @@ class Decoder {
194
194
  __publicField$1(this, "canvas");
195
195
  __publicField$1(this, "tiles");
196
196
  __publicField$1(this, "colors");
197
+ __publicField$1(this, "frameColors");
197
198
  __publicField$1(this, "rawImageData");
198
199
  __publicField$1(this, "lockFrame");
199
- __publicField$1(this, "invertPalette");
200
200
  __publicField$1(this, "colorData");
201
+ __publicField$1(this, "frameColorData");
201
202
  __publicField$1(this, "tilesPerLine");
203
+ __publicField$1(this, "imageStartLine");
202
204
  this.canvas = null;
203
205
  this.tiles = [];
204
206
  this.colors = [];
207
+ this.frameColors = [];
205
208
  this.rawImageData = null;
206
209
  this.lockFrame = false;
207
- this.invertPalette = false;
208
210
  this.colorData = [...BW_PALETTE];
211
+ this.frameColorData = [...BW_PALETTE];
209
212
  this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
213
+ this.imageStartLine = 2;
210
214
  }
211
215
  update({
212
- canvas = null,
213
- tiles = [],
214
- palette = [],
215
- lockFrame = false,
216
- invertPalette = false
216
+ canvas,
217
+ tiles,
218
+ palette,
219
+ framePalette,
220
+ imageStartLine = 2
217
221
  }) {
222
+ const startLineChanged = this.setImageStartLine(imageStartLine);
218
223
  const canvasChanged = canvas ? this.setCanvas(canvas) : false;
219
- const paletteChanged = palette ? this.setPalette(palette, invertPalette) : false;
220
- const lockFrameChanged = this.setLockFrame(lockFrame);
221
- if (canvasChanged || paletteChanged || lockFrameChanged || !this.tiles.length) {
224
+ const palettesChanged = this.setPalettes(palette, framePalette);
225
+ if (startLineChanged || canvasChanged || palettesChanged || !this.tiles.length) {
222
226
  this.tiles = [];
223
227
  }
224
228
  const tilesChanged = this.setTiles(tiles);
@@ -243,6 +247,13 @@ class Decoder {
243
247
  });
244
248
  this.updateCanvas(newHeight);
245
249
  }
250
+ setImageStartLine(imageStartLine) {
251
+ if (this.imageStartLine === imageStartLine) {
252
+ return false;
253
+ }
254
+ this.imageStartLine = imageStartLine;
255
+ return true;
256
+ }
246
257
  updateCanvas(newHeight) {
247
258
  if (!this.canvas || !this.rawImageData?.length) {
248
259
  return;
@@ -304,7 +315,7 @@ class Decoder {
304
315
  case ExportFrameMode.FRAMEMODE_KEEP:
305
316
  return this.tiles;
306
317
  case ExportFrameMode.FRAMEMODE_CROP:
307
- return this.tiles.reduce((acc, tile, index) => tileIndexIsPartOfFrame(index, ExportFrameMode.FRAMEMODE_KEEP) ? acc : [...acc, tile], []);
318
+ return this.tiles.reduce((acc, tile, index) => tileIndexIsPartOfFrame(index, this.imageStartLine, ExportFrameMode.FRAMEMODE_KEEP) ? acc : [...acc, tile], []);
308
319
  case ExportFrameMode.FRAMEMODE_SQUARE_BLACK:
309
320
  return [
310
321
  ...BLACK_LINE,
@@ -328,24 +339,20 @@ class Decoder {
328
339
  this.canvas = canvas;
329
340
  return true;
330
341
  }
331
- setPalette(palette, invertPalette) {
332
- if (this.colors[0] === palette[0] && this.colors[1] === palette[1] && this.colors[2] === palette[2] && this.colors[3] === palette[3] && this.invertPalette === invertPalette) {
342
+ setPalettes(palette, framePalette) {
343
+ if (this.colors[0] === palette[0] && this.colors[1] === palette[1] && this.colors[2] === palette[2] && this.colors[3] === palette[3] && this.frameColors[0] === framePalette[0] && this.frameColors[1] === framePalette[1] && this.frameColors[2] === framePalette[2] && this.frameColors[3] === framePalette[3]) {
333
344
  return false;
334
345
  }
335
346
  this.colors = palette;
336
- this.invertPalette = invertPalette;
347
+ this.frameColors = framePalette;
337
348
  this.colors.forEach((color, index) => {
338
349
  this.colorData[index] = color.length !== 7 ? BW_PALETTE[index] : parseInt(color.substring(1), 16);
339
350
  });
351
+ this.frameColors.forEach((color, index) => {
352
+ this.frameColorData[index] = color.length !== 7 ? BW_PALETTE[index] : parseInt(color.substring(1), 16);
353
+ });
340
354
  return true;
341
355
  }
342
- setLockFrame(lockFrame) {
343
- if (lockFrame !== this.lockFrame) {
344
- this.lockFrame = lockFrame;
345
- return true;
346
- }
347
- return false;
348
- }
349
356
  setTiles(tiles) {
350
357
  const changedTiles = tiles.reduce((acc, newTile, index) => {
351
358
  const changed = newTile !== this.tiles[index];
@@ -388,10 +395,10 @@ class Decoder {
388
395
  pixels,
389
396
  index: y * TILE_PIXEL_WIDTH + x,
390
397
  tileIndex: index,
398
+ imageStartLine: this.imageStartLine,
391
399
  handleExportFrame: ExportFrameMode.FRAMEMODE_KEEP,
392
- invertPalette: this.invertPalette,
393
- lockFrame: this.lockFrame,
394
- colorData: this.colorData
400
+ colorData: this.colorData,
401
+ frameColorData: this.frameColorData
395
402
  });
396
403
  this.rawImageData[rawIndex] = color.r;
397
404
  this.rawImageData[rawIndex + 1] = color.g;
@@ -417,10 +424,10 @@ class Decoder {
417
424
  pixels,
418
425
  index: y * TILE_PIXEL_WIDTH + x,
419
426
  tileIndex: index,
427
+ imageStartLine: this.imageStartLine,
420
428
  handleExportFrame,
421
- lockFrame: this.lockFrame,
422
- invertPalette: this.invertPalette,
423
- colorData: this.colorData
429
+ colorData: this.colorData,
430
+ frameColorData: this.frameColorData
424
431
  });
425
432
  canvasContext.fillStyle = `rgb(${color.r}, ${color.g}, ${color.b})`;
426
433
  canvasContext.fillRect(
@@ -455,11 +462,10 @@ const createChannel = (key, tilesPerLine) => {
455
462
  const canvas = document.createElement("canvas");
456
463
  const decoder = new Decoder({ tilesPerLine });
457
464
  decoder.update({
465
+ framePalette: [],
458
466
  canvas,
459
467
  tiles: [],
460
- palette: [],
461
- lockFrame: false,
462
- invertPalette: false
468
+ palette: []
463
469
  });
464
470
  return {
465
471
  key,
@@ -486,8 +492,8 @@ class RGBNDecoder {
486
492
  };
487
493
  }
488
494
  update({
489
- canvas = null,
490
- tiles = {},
495
+ canvas,
496
+ tiles,
491
497
  palette,
492
498
  lockFrame = false
493
499
  }) {
@@ -556,10 +562,9 @@ class RGBNDecoder {
556
562
  paletteFunction(channelColors[0])
557
563
  ];
558
564
  channel.decoder.update({
559
- invertPalette: false,
560
565
  canvas: channel.canvas,
561
566
  tiles: channel.tiles,
562
- lockFrame: this.lockFrame,
567
+ framePalette: this.lockFrame ? BW_PALETTE_HEX : palette,
563
568
  palette
564
569
  });
565
570
  return {
@@ -620,4 +625,4 @@ class RGBNDecoder {
620
625
 
621
626
  const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]?.length || 0));
622
627
 
623
- export { BW_PALETTE, BlendMode, Decoder, ExportFrameMode, RGBNDecoder, SKIP_LINE, blendModeNewName, decodeTile, getRGBValue, maxTiles, tileIndexIsPartOfFrame };
628
+ export { BW_PALETTE, BW_PALETTE_HEX, BlendMode, 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.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "Decoder classes for GameBoy-encoded images",
5
5
  "repository": "",
6
6
  "license": "MIT",