gb-image-decoder 1.1.0 → 1.2.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/README.md +1 -1
- package/dist/index.cjs +35 -40
- package/dist/index.d.cts +22 -21
- package/dist/index.d.mts +22 -21
- package/dist/index.d.ts +22 -21
- package/dist/index.mjs +35 -41
- package/package.json +1 -1
package/README.md
CHANGED
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,
|
|
@@ -145,6 +146,9 @@ const decodeTile = (rawBytes = BLACK) => {
|
|
|
145
146
|
};
|
|
146
147
|
|
|
147
148
|
const tileIndexIsPartOfFrame = (tileIndex, imageStartLine, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) => {
|
|
149
|
+
if (handleExportFrame === ExportFrameMode.FRAMEMODE_CROP) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
148
152
|
const checkIndex = tileIndex - (handleExportFrame === ExportFrameMode.FRAMEMODE_KEEP ? 0 : 20);
|
|
149
153
|
if (checkIndex < imageStartLine * 20) {
|
|
150
154
|
return true;
|
|
@@ -169,13 +173,11 @@ const getRGBValue = ({
|
|
|
169
173
|
tileIndex,
|
|
170
174
|
imageStartLine,
|
|
171
175
|
handleExportFrame,
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
colorData
|
|
176
|
+
colorData,
|
|
177
|
+
frameColorData
|
|
175
178
|
}) => {
|
|
176
|
-
const palette =
|
|
177
|
-
|
|
178
|
-
const value = invertPalette ? palette[3 - pixels[index]] : palette[pixels[index]];
|
|
179
|
+
const palette = tileIndexIsPartOfFrame(tileIndex, imageStartLine, handleExportFrame) ? frameColorData : colorData;
|
|
180
|
+
const value = palette[pixels[index]];
|
|
179
181
|
return {
|
|
180
182
|
// eslint-disable-next-line no-bitwise
|
|
181
183
|
r: (value & 16711680) >> 16,
|
|
@@ -197,35 +199,35 @@ class Decoder {
|
|
|
197
199
|
__publicField$1(this, "canvas");
|
|
198
200
|
__publicField$1(this, "tiles");
|
|
199
201
|
__publicField$1(this, "colors");
|
|
202
|
+
__publicField$1(this, "frameColors");
|
|
200
203
|
__publicField$1(this, "rawImageData");
|
|
201
204
|
__publicField$1(this, "lockFrame");
|
|
202
|
-
__publicField$1(this, "invertPalette");
|
|
203
205
|
__publicField$1(this, "colorData");
|
|
206
|
+
__publicField$1(this, "frameColorData");
|
|
204
207
|
__publicField$1(this, "tilesPerLine");
|
|
205
208
|
__publicField$1(this, "imageStartLine");
|
|
206
209
|
this.canvas = null;
|
|
207
210
|
this.tiles = [];
|
|
208
211
|
this.colors = [];
|
|
212
|
+
this.frameColors = [];
|
|
209
213
|
this.rawImageData = null;
|
|
210
214
|
this.lockFrame = false;
|
|
211
|
-
this.invertPalette = false;
|
|
212
|
-
this.imageStartLine = 2;
|
|
213
215
|
this.colorData = [...BW_PALETTE];
|
|
216
|
+
this.frameColorData = [...BW_PALETTE];
|
|
214
217
|
this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
|
|
218
|
+
this.imageStartLine = 2;
|
|
215
219
|
}
|
|
216
220
|
update({
|
|
217
|
-
canvas
|
|
218
|
-
tiles
|
|
219
|
-
palette
|
|
220
|
-
|
|
221
|
-
invertPalette = false,
|
|
221
|
+
canvas,
|
|
222
|
+
tiles,
|
|
223
|
+
palette,
|
|
224
|
+
framePalette,
|
|
222
225
|
imageStartLine = 2
|
|
223
226
|
}) {
|
|
224
227
|
const startLineChanged = this.setImageStartLine(imageStartLine);
|
|
225
228
|
const canvasChanged = canvas ? this.setCanvas(canvas) : false;
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
if (startLineChanged || canvasChanged || paletteChanged || lockFrameChanged || !this.tiles.length) {
|
|
229
|
+
const palettesChanged = this.setPalettes(palette, framePalette);
|
|
230
|
+
if (startLineChanged || canvasChanged || palettesChanged || !this.tiles.length) {
|
|
229
231
|
this.tiles = [];
|
|
230
232
|
}
|
|
231
233
|
const tilesChanged = this.setTiles(tiles);
|
|
@@ -342,24 +344,20 @@ class Decoder {
|
|
|
342
344
|
this.canvas = canvas;
|
|
343
345
|
return true;
|
|
344
346
|
}
|
|
345
|
-
|
|
346
|
-
if (this.colors[0] === palette[0] && this.colors[1] === palette[1] && this.colors[2] === palette[2] && this.colors[3] === palette[3] && this.
|
|
347
|
+
setPalettes(palette, framePalette) {
|
|
348
|
+
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]) {
|
|
347
349
|
return false;
|
|
348
350
|
}
|
|
349
351
|
this.colors = palette;
|
|
350
|
-
this.
|
|
352
|
+
this.frameColors = framePalette;
|
|
351
353
|
this.colors.forEach((color, index) => {
|
|
352
354
|
this.colorData[index] = color.length !== 7 ? BW_PALETTE[index] : parseInt(color.substring(1), 16);
|
|
353
355
|
});
|
|
356
|
+
this.frameColors.forEach((color, index) => {
|
|
357
|
+
this.frameColorData[index] = color.length !== 7 ? BW_PALETTE[index] : parseInt(color.substring(1), 16);
|
|
358
|
+
});
|
|
354
359
|
return true;
|
|
355
360
|
}
|
|
356
|
-
setLockFrame(lockFrame) {
|
|
357
|
-
if (lockFrame !== this.lockFrame) {
|
|
358
|
-
this.lockFrame = lockFrame;
|
|
359
|
-
return true;
|
|
360
|
-
}
|
|
361
|
-
return false;
|
|
362
|
-
}
|
|
363
361
|
setTiles(tiles) {
|
|
364
362
|
const changedTiles = tiles.reduce((acc, newTile, index) => {
|
|
365
363
|
const changed = newTile !== this.tiles[index];
|
|
@@ -404,9 +402,8 @@ class Decoder {
|
|
|
404
402
|
tileIndex: index,
|
|
405
403
|
imageStartLine: this.imageStartLine,
|
|
406
404
|
handleExportFrame: ExportFrameMode.FRAMEMODE_KEEP,
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
colorData: this.colorData
|
|
405
|
+
colorData: this.colorData,
|
|
406
|
+
frameColorData: this.frameColorData
|
|
410
407
|
});
|
|
411
408
|
this.rawImageData[rawIndex] = color.r;
|
|
412
409
|
this.rawImageData[rawIndex + 1] = color.g;
|
|
@@ -434,9 +431,8 @@ class Decoder {
|
|
|
434
431
|
tileIndex: index,
|
|
435
432
|
imageStartLine: this.imageStartLine,
|
|
436
433
|
handleExportFrame,
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
colorData: this.colorData
|
|
434
|
+
colorData: this.colorData,
|
|
435
|
+
frameColorData: this.frameColorData
|
|
440
436
|
});
|
|
441
437
|
canvasContext.fillStyle = `rgb(${color.r}, ${color.g}, ${color.b})`;
|
|
442
438
|
canvasContext.fillRect(
|
|
@@ -471,11 +467,10 @@ const createChannel = (key, tilesPerLine) => {
|
|
|
471
467
|
const canvas = document.createElement("canvas");
|
|
472
468
|
const decoder = new Decoder({ tilesPerLine });
|
|
473
469
|
decoder.update({
|
|
470
|
+
framePalette: [],
|
|
474
471
|
canvas,
|
|
475
472
|
tiles: [],
|
|
476
|
-
palette: []
|
|
477
|
-
lockFrame: false,
|
|
478
|
-
invertPalette: false
|
|
473
|
+
palette: []
|
|
479
474
|
});
|
|
480
475
|
return {
|
|
481
476
|
key,
|
|
@@ -502,8 +497,8 @@ class RGBNDecoder {
|
|
|
502
497
|
};
|
|
503
498
|
}
|
|
504
499
|
update({
|
|
505
|
-
canvas
|
|
506
|
-
tiles
|
|
500
|
+
canvas,
|
|
501
|
+
tiles,
|
|
507
502
|
palette,
|
|
508
503
|
lockFrame = false
|
|
509
504
|
}) {
|
|
@@ -572,10 +567,9 @@ class RGBNDecoder {
|
|
|
572
567
|
paletteFunction(channelColors[0])
|
|
573
568
|
];
|
|
574
569
|
channel.decoder.update({
|
|
575
|
-
invertPalette: false,
|
|
576
570
|
canvas: channel.canvas,
|
|
577
571
|
tiles: channel.tiles,
|
|
578
|
-
|
|
572
|
+
framePalette: this.lockFrame ? BW_PALETTE_HEX : palette,
|
|
579
573
|
palette
|
|
580
574
|
});
|
|
581
575
|
return {
|
|
@@ -637,6 +631,7 @@ class RGBNDecoder {
|
|
|
637
631
|
const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]?.length || 0));
|
|
638
632
|
|
|
639
633
|
exports.BW_PALETTE = BW_PALETTE;
|
|
634
|
+
exports.BW_PALETTE_HEX = BW_PALETTE_HEX;
|
|
640
635
|
exports.BlendMode = BlendMode;
|
|
641
636
|
exports.Decoder = Decoder;
|
|
642
637
|
exports.ExportFrameMode = ExportFrameMode;
|
package/dist/index.d.cts
CHANGED
|
@@ -64,34 +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;
|
|
77
91
|
private imageStartLine;
|
|
78
92
|
constructor(options?: DecoderOptions);
|
|
79
|
-
update({ canvas, tiles, palette,
|
|
80
|
-
canvas: HTMLCanvasElement | null;
|
|
81
|
-
tiles: string[];
|
|
82
|
-
palette: string[];
|
|
83
|
-
lockFrame?: boolean;
|
|
84
|
-
invertPalette?: boolean;
|
|
85
|
-
imageStartLine?: number;
|
|
86
|
-
}): void;
|
|
93
|
+
update({ canvas, tiles, palette, framePalette, imageStartLine, }: DecoderUpdateParams): void;
|
|
87
94
|
private setImageStartLine;
|
|
88
95
|
private updateCanvas;
|
|
89
96
|
getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
|
|
90
97
|
private getScaledCanvasSize;
|
|
91
98
|
private getExportTiles;
|
|
92
99
|
private setCanvas;
|
|
93
|
-
private
|
|
94
|
-
private setLockFrame;
|
|
100
|
+
private setPalettes;
|
|
95
101
|
private setTiles;
|
|
96
102
|
private renderTile;
|
|
97
103
|
private paintTile;
|
|
@@ -106,12 +112,7 @@ declare class RGBNDecoder {
|
|
|
106
112
|
private channels;
|
|
107
113
|
private tilesPerLine;
|
|
108
114
|
constructor(options?: DecoderOptions);
|
|
109
|
-
update({ canvas, tiles, palette, lockFrame, }:
|
|
110
|
-
canvas: HTMLCanvasElement | null;
|
|
111
|
-
tiles: RGBNTiles;
|
|
112
|
-
palette: RGBNPalette;
|
|
113
|
-
lockFrame?: boolean;
|
|
114
|
-
}): void;
|
|
115
|
+
update({ canvas, tiles, palette, lockFrame, }: RGBNDecoderUpdateParams): void;
|
|
115
116
|
private setPalette;
|
|
116
117
|
private setCanvas;
|
|
117
118
|
private setLockFrame;
|
|
@@ -124,15 +125,14 @@ declare class RGBNDecoder {
|
|
|
124
125
|
|
|
125
126
|
declare const decodeTile: (rawBytes?: string) => IndexedTilePixels;
|
|
126
127
|
|
|
127
|
-
declare const getRGBValue: ({ pixels, index, tileIndex, imageStartLine, handleExportFrame,
|
|
128
|
+
declare const getRGBValue: ({ pixels, index, tileIndex, imageStartLine, handleExportFrame, colorData, frameColorData, }: {
|
|
128
129
|
pixels: IndexedTilePixels;
|
|
129
130
|
index: number;
|
|
130
131
|
tileIndex: number;
|
|
131
132
|
imageStartLine: number;
|
|
132
133
|
handleExportFrame: ExportFrameMode;
|
|
133
|
-
lockFrame: boolean;
|
|
134
|
-
invertPalette: boolean;
|
|
135
134
|
colorData: BWPalette;
|
|
135
|
+
frameColorData: BWPalette;
|
|
136
136
|
}) => RGBValue;
|
|
137
137
|
|
|
138
138
|
declare const tileIndexIsPartOfFrame: (tileIndex: number, imageStartLine: number, handleExportFrame?: ExportFrameMode) => boolean;
|
|
@@ -141,5 +141,6 @@ declare const maxTiles: (rgbnTiles: RGBNTiles) => number;
|
|
|
141
141
|
|
|
142
142
|
declare const SKIP_LINE = "skip";
|
|
143
143
|
declare const BW_PALETTE: number[];
|
|
144
|
+
declare const BW_PALETTE_HEX: string[];
|
|
144
145
|
|
|
145
|
-
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,34 +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;
|
|
77
91
|
private imageStartLine;
|
|
78
92
|
constructor(options?: DecoderOptions);
|
|
79
|
-
update({ canvas, tiles, palette,
|
|
80
|
-
canvas: HTMLCanvasElement | null;
|
|
81
|
-
tiles: string[];
|
|
82
|
-
palette: string[];
|
|
83
|
-
lockFrame?: boolean;
|
|
84
|
-
invertPalette?: boolean;
|
|
85
|
-
imageStartLine?: number;
|
|
86
|
-
}): void;
|
|
93
|
+
update({ canvas, tiles, palette, framePalette, imageStartLine, }: DecoderUpdateParams): void;
|
|
87
94
|
private setImageStartLine;
|
|
88
95
|
private updateCanvas;
|
|
89
96
|
getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
|
|
90
97
|
private getScaledCanvasSize;
|
|
91
98
|
private getExportTiles;
|
|
92
99
|
private setCanvas;
|
|
93
|
-
private
|
|
94
|
-
private setLockFrame;
|
|
100
|
+
private setPalettes;
|
|
95
101
|
private setTiles;
|
|
96
102
|
private renderTile;
|
|
97
103
|
private paintTile;
|
|
@@ -106,12 +112,7 @@ declare class RGBNDecoder {
|
|
|
106
112
|
private channels;
|
|
107
113
|
private tilesPerLine;
|
|
108
114
|
constructor(options?: DecoderOptions);
|
|
109
|
-
update({ canvas, tiles, palette, lockFrame, }:
|
|
110
|
-
canvas: HTMLCanvasElement | null;
|
|
111
|
-
tiles: RGBNTiles;
|
|
112
|
-
palette: RGBNPalette;
|
|
113
|
-
lockFrame?: boolean;
|
|
114
|
-
}): void;
|
|
115
|
+
update({ canvas, tiles, palette, lockFrame, }: RGBNDecoderUpdateParams): void;
|
|
115
116
|
private setPalette;
|
|
116
117
|
private setCanvas;
|
|
117
118
|
private setLockFrame;
|
|
@@ -124,15 +125,14 @@ declare class RGBNDecoder {
|
|
|
124
125
|
|
|
125
126
|
declare const decodeTile: (rawBytes?: string) => IndexedTilePixels;
|
|
126
127
|
|
|
127
|
-
declare const getRGBValue: ({ pixels, index, tileIndex, imageStartLine, handleExportFrame,
|
|
128
|
+
declare const getRGBValue: ({ pixels, index, tileIndex, imageStartLine, handleExportFrame, colorData, frameColorData, }: {
|
|
128
129
|
pixels: IndexedTilePixels;
|
|
129
130
|
index: number;
|
|
130
131
|
tileIndex: number;
|
|
131
132
|
imageStartLine: number;
|
|
132
133
|
handleExportFrame: ExportFrameMode;
|
|
133
|
-
lockFrame: boolean;
|
|
134
|
-
invertPalette: boolean;
|
|
135
134
|
colorData: BWPalette;
|
|
135
|
+
frameColorData: BWPalette;
|
|
136
136
|
}) => RGBValue;
|
|
137
137
|
|
|
138
138
|
declare const tileIndexIsPartOfFrame: (tileIndex: number, imageStartLine: number, handleExportFrame?: ExportFrameMode) => boolean;
|
|
@@ -141,5 +141,6 @@ declare const maxTiles: (rgbnTiles: RGBNTiles) => number;
|
|
|
141
141
|
|
|
142
142
|
declare const SKIP_LINE = "skip";
|
|
143
143
|
declare const BW_PALETTE: number[];
|
|
144
|
+
declare const BW_PALETTE_HEX: string[];
|
|
144
145
|
|
|
145
|
-
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,34 +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;
|
|
77
91
|
private imageStartLine;
|
|
78
92
|
constructor(options?: DecoderOptions);
|
|
79
|
-
update({ canvas, tiles, palette,
|
|
80
|
-
canvas: HTMLCanvasElement | null;
|
|
81
|
-
tiles: string[];
|
|
82
|
-
palette: string[];
|
|
83
|
-
lockFrame?: boolean;
|
|
84
|
-
invertPalette?: boolean;
|
|
85
|
-
imageStartLine?: number;
|
|
86
|
-
}): void;
|
|
93
|
+
update({ canvas, tiles, palette, framePalette, imageStartLine, }: DecoderUpdateParams): void;
|
|
87
94
|
private setImageStartLine;
|
|
88
95
|
private updateCanvas;
|
|
89
96
|
getScaledCanvas(scaleFactor: number, handleExportFrame?: ExportFrameMode): HTMLCanvasElement;
|
|
90
97
|
private getScaledCanvasSize;
|
|
91
98
|
private getExportTiles;
|
|
92
99
|
private setCanvas;
|
|
93
|
-
private
|
|
94
|
-
private setLockFrame;
|
|
100
|
+
private setPalettes;
|
|
95
101
|
private setTiles;
|
|
96
102
|
private renderTile;
|
|
97
103
|
private paintTile;
|
|
@@ -106,12 +112,7 @@ declare class RGBNDecoder {
|
|
|
106
112
|
private channels;
|
|
107
113
|
private tilesPerLine;
|
|
108
114
|
constructor(options?: DecoderOptions);
|
|
109
|
-
update({ canvas, tiles, palette, lockFrame, }:
|
|
110
|
-
canvas: HTMLCanvasElement | null;
|
|
111
|
-
tiles: RGBNTiles;
|
|
112
|
-
palette: RGBNPalette;
|
|
113
|
-
lockFrame?: boolean;
|
|
114
|
-
}): void;
|
|
115
|
+
update({ canvas, tiles, palette, lockFrame, }: RGBNDecoderUpdateParams): void;
|
|
115
116
|
private setPalette;
|
|
116
117
|
private setCanvas;
|
|
117
118
|
private setLockFrame;
|
|
@@ -124,15 +125,14 @@ declare class RGBNDecoder {
|
|
|
124
125
|
|
|
125
126
|
declare const decodeTile: (rawBytes?: string) => IndexedTilePixels;
|
|
126
127
|
|
|
127
|
-
declare const getRGBValue: ({ pixels, index, tileIndex, imageStartLine, handleExportFrame,
|
|
128
|
+
declare const getRGBValue: ({ pixels, index, tileIndex, imageStartLine, handleExportFrame, colorData, frameColorData, }: {
|
|
128
129
|
pixels: IndexedTilePixels;
|
|
129
130
|
index: number;
|
|
130
131
|
tileIndex: number;
|
|
131
132
|
imageStartLine: number;
|
|
132
133
|
handleExportFrame: ExportFrameMode;
|
|
133
|
-
lockFrame: boolean;
|
|
134
|
-
invertPalette: boolean;
|
|
135
134
|
colorData: BWPalette;
|
|
135
|
+
frameColorData: BWPalette;
|
|
136
136
|
}) => RGBValue;
|
|
137
137
|
|
|
138
138
|
declare const tileIndexIsPartOfFrame: (tileIndex: number, imageStartLine: number, handleExportFrame?: ExportFrameMode) => boolean;
|
|
@@ -141,5 +141,6 @@ declare const maxTiles: (rgbnTiles: RGBNTiles) => number;
|
|
|
141
141
|
|
|
142
142
|
declare const SKIP_LINE = "skip";
|
|
143
143
|
declare const BW_PALETTE: number[];
|
|
144
|
+
declare const BW_PALETTE_HEX: string[];
|
|
144
145
|
|
|
145
|
-
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,
|
|
@@ -143,6 +144,9 @@ const decodeTile = (rawBytes = BLACK) => {
|
|
|
143
144
|
};
|
|
144
145
|
|
|
145
146
|
const tileIndexIsPartOfFrame = (tileIndex, imageStartLine, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) => {
|
|
147
|
+
if (handleExportFrame === ExportFrameMode.FRAMEMODE_CROP) {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
146
150
|
const checkIndex = tileIndex - (handleExportFrame === ExportFrameMode.FRAMEMODE_KEEP ? 0 : 20);
|
|
147
151
|
if (checkIndex < imageStartLine * 20) {
|
|
148
152
|
return true;
|
|
@@ -167,13 +171,11 @@ const getRGBValue = ({
|
|
|
167
171
|
tileIndex,
|
|
168
172
|
imageStartLine,
|
|
169
173
|
handleExportFrame,
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
colorData
|
|
174
|
+
colorData,
|
|
175
|
+
frameColorData
|
|
173
176
|
}) => {
|
|
174
|
-
const palette =
|
|
175
|
-
|
|
176
|
-
const value = invertPalette ? palette[3 - pixels[index]] : palette[pixels[index]];
|
|
177
|
+
const palette = tileIndexIsPartOfFrame(tileIndex, imageStartLine, handleExportFrame) ? frameColorData : colorData;
|
|
178
|
+
const value = palette[pixels[index]];
|
|
177
179
|
return {
|
|
178
180
|
// eslint-disable-next-line no-bitwise
|
|
179
181
|
r: (value & 16711680) >> 16,
|
|
@@ -195,35 +197,35 @@ class Decoder {
|
|
|
195
197
|
__publicField$1(this, "canvas");
|
|
196
198
|
__publicField$1(this, "tiles");
|
|
197
199
|
__publicField$1(this, "colors");
|
|
200
|
+
__publicField$1(this, "frameColors");
|
|
198
201
|
__publicField$1(this, "rawImageData");
|
|
199
202
|
__publicField$1(this, "lockFrame");
|
|
200
|
-
__publicField$1(this, "invertPalette");
|
|
201
203
|
__publicField$1(this, "colorData");
|
|
204
|
+
__publicField$1(this, "frameColorData");
|
|
202
205
|
__publicField$1(this, "tilesPerLine");
|
|
203
206
|
__publicField$1(this, "imageStartLine");
|
|
204
207
|
this.canvas = null;
|
|
205
208
|
this.tiles = [];
|
|
206
209
|
this.colors = [];
|
|
210
|
+
this.frameColors = [];
|
|
207
211
|
this.rawImageData = null;
|
|
208
212
|
this.lockFrame = false;
|
|
209
|
-
this.invertPalette = false;
|
|
210
|
-
this.imageStartLine = 2;
|
|
211
213
|
this.colorData = [...BW_PALETTE];
|
|
214
|
+
this.frameColorData = [...BW_PALETTE];
|
|
212
215
|
this.tilesPerLine = options?.tilesPerLine || TILES_PER_LINE;
|
|
216
|
+
this.imageStartLine = 2;
|
|
213
217
|
}
|
|
214
218
|
update({
|
|
215
|
-
canvas
|
|
216
|
-
tiles
|
|
217
|
-
palette
|
|
218
|
-
|
|
219
|
-
invertPalette = false,
|
|
219
|
+
canvas,
|
|
220
|
+
tiles,
|
|
221
|
+
palette,
|
|
222
|
+
framePalette,
|
|
220
223
|
imageStartLine = 2
|
|
221
224
|
}) {
|
|
222
225
|
const startLineChanged = this.setImageStartLine(imageStartLine);
|
|
223
226
|
const canvasChanged = canvas ? this.setCanvas(canvas) : false;
|
|
224
|
-
const
|
|
225
|
-
|
|
226
|
-
if (startLineChanged || canvasChanged || paletteChanged || lockFrameChanged || !this.tiles.length) {
|
|
227
|
+
const palettesChanged = this.setPalettes(palette, framePalette);
|
|
228
|
+
if (startLineChanged || canvasChanged || palettesChanged || !this.tiles.length) {
|
|
227
229
|
this.tiles = [];
|
|
228
230
|
}
|
|
229
231
|
const tilesChanged = this.setTiles(tiles);
|
|
@@ -340,24 +342,20 @@ class Decoder {
|
|
|
340
342
|
this.canvas = canvas;
|
|
341
343
|
return true;
|
|
342
344
|
}
|
|
343
|
-
|
|
344
|
-
if (this.colors[0] === palette[0] && this.colors[1] === palette[1] && this.colors[2] === palette[2] && this.colors[3] === palette[3] && this.
|
|
345
|
+
setPalettes(palette, framePalette) {
|
|
346
|
+
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]) {
|
|
345
347
|
return false;
|
|
346
348
|
}
|
|
347
349
|
this.colors = palette;
|
|
348
|
-
this.
|
|
350
|
+
this.frameColors = framePalette;
|
|
349
351
|
this.colors.forEach((color, index) => {
|
|
350
352
|
this.colorData[index] = color.length !== 7 ? BW_PALETTE[index] : parseInt(color.substring(1), 16);
|
|
351
353
|
});
|
|
354
|
+
this.frameColors.forEach((color, index) => {
|
|
355
|
+
this.frameColorData[index] = color.length !== 7 ? BW_PALETTE[index] : parseInt(color.substring(1), 16);
|
|
356
|
+
});
|
|
352
357
|
return true;
|
|
353
358
|
}
|
|
354
|
-
setLockFrame(lockFrame) {
|
|
355
|
-
if (lockFrame !== this.lockFrame) {
|
|
356
|
-
this.lockFrame = lockFrame;
|
|
357
|
-
return true;
|
|
358
|
-
}
|
|
359
|
-
return false;
|
|
360
|
-
}
|
|
361
359
|
setTiles(tiles) {
|
|
362
360
|
const changedTiles = tiles.reduce((acc, newTile, index) => {
|
|
363
361
|
const changed = newTile !== this.tiles[index];
|
|
@@ -402,9 +400,8 @@ class Decoder {
|
|
|
402
400
|
tileIndex: index,
|
|
403
401
|
imageStartLine: this.imageStartLine,
|
|
404
402
|
handleExportFrame: ExportFrameMode.FRAMEMODE_KEEP,
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
colorData: this.colorData
|
|
403
|
+
colorData: this.colorData,
|
|
404
|
+
frameColorData: this.frameColorData
|
|
408
405
|
});
|
|
409
406
|
this.rawImageData[rawIndex] = color.r;
|
|
410
407
|
this.rawImageData[rawIndex + 1] = color.g;
|
|
@@ -432,9 +429,8 @@ class Decoder {
|
|
|
432
429
|
tileIndex: index,
|
|
433
430
|
imageStartLine: this.imageStartLine,
|
|
434
431
|
handleExportFrame,
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
colorData: this.colorData
|
|
432
|
+
colorData: this.colorData,
|
|
433
|
+
frameColorData: this.frameColorData
|
|
438
434
|
});
|
|
439
435
|
canvasContext.fillStyle = `rgb(${color.r}, ${color.g}, ${color.b})`;
|
|
440
436
|
canvasContext.fillRect(
|
|
@@ -469,11 +465,10 @@ const createChannel = (key, tilesPerLine) => {
|
|
|
469
465
|
const canvas = document.createElement("canvas");
|
|
470
466
|
const decoder = new Decoder({ tilesPerLine });
|
|
471
467
|
decoder.update({
|
|
468
|
+
framePalette: [],
|
|
472
469
|
canvas,
|
|
473
470
|
tiles: [],
|
|
474
|
-
palette: []
|
|
475
|
-
lockFrame: false,
|
|
476
|
-
invertPalette: false
|
|
471
|
+
palette: []
|
|
477
472
|
});
|
|
478
473
|
return {
|
|
479
474
|
key,
|
|
@@ -500,8 +495,8 @@ class RGBNDecoder {
|
|
|
500
495
|
};
|
|
501
496
|
}
|
|
502
497
|
update({
|
|
503
|
-
canvas
|
|
504
|
-
tiles
|
|
498
|
+
canvas,
|
|
499
|
+
tiles,
|
|
505
500
|
palette,
|
|
506
501
|
lockFrame = false
|
|
507
502
|
}) {
|
|
@@ -570,10 +565,9 @@ class RGBNDecoder {
|
|
|
570
565
|
paletteFunction(channelColors[0])
|
|
571
566
|
];
|
|
572
567
|
channel.decoder.update({
|
|
573
|
-
invertPalette: false,
|
|
574
568
|
canvas: channel.canvas,
|
|
575
569
|
tiles: channel.tiles,
|
|
576
|
-
|
|
570
|
+
framePalette: this.lockFrame ? BW_PALETTE_HEX : palette,
|
|
577
571
|
palette
|
|
578
572
|
});
|
|
579
573
|
return {
|
|
@@ -634,4 +628,4 @@ class RGBNDecoder {
|
|
|
634
628
|
|
|
635
629
|
const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]?.length || 0));
|
|
636
630
|
|
|
637
|
-
export { BW_PALETTE, BlendMode, Decoder, ExportFrameMode, RGBNDecoder, SKIP_LINE, blendModeNewName, decodeTile, getRGBValue, maxTiles, tileIndexIsPartOfFrame };
|
|
631
|
+
export { BW_PALETTE, BW_PALETTE_HEX, BlendMode, Decoder, ExportFrameMode, RGBNDecoder, SKIP_LINE, blendModeNewName, decodeTile, getRGBValue, maxTiles, tileIndexIsPartOfFrame };
|