gb-image-decoder 2.0.0 → 2.0.2
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 +5 -1
- package/dist/index.cjs +116 -36
- package/dist/index.d.cts +8 -2
- package/dist/index.d.mts +8 -2
- package/dist/index.d.ts +8 -2
- package/dist/index.mjs +116 -36
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ There are a lot more similar projects for aquiring raw tile data.
|
|
|
11
11
|
### Browser
|
|
12
12
|
#### Monochrome images
|
|
13
13
|
```typescript
|
|
14
|
-
import { getMonochromeImageUrl, ExportFrameMode } from 'gb-image-decoder';
|
|
14
|
+
import { getMonochromeImageUrl, ExportFrameMode, Rotation } from 'gb-image-decoder';
|
|
15
15
|
|
|
16
16
|
const tiles: string[] = [ // need 360 of these for a 160x144 image
|
|
17
17
|
'7D FF 0A FF 7D FF FF FF 5F FF BB FF 5D FF FF FF', '75 FF A2 FF 44 FF FF FF 5D FF FF FF FF FF FF FF', '55 FF 1F FF 57 FF FF FF 7D FF FF FF FF FF FF FF', '77 FF FD FF 57 FF FF FF 75 FF FF FF DF FF FF FF',
|
|
@@ -27,6 +27,7 @@ const imageSrc = await getMonochromeImageUrl({
|
|
|
27
27
|
// imageStartLine: 2,
|
|
28
28
|
// tilesPerLine: 20,
|
|
29
29
|
// scaleFactor: 1,
|
|
30
|
+
// rotation: Rotation.DEG_0,
|
|
30
31
|
// handleExportFrame: ExportFrameMode.FRAMEMODE_KEEP,
|
|
31
32
|
})
|
|
32
33
|
```
|
|
@@ -53,6 +54,7 @@ const imageSrc = await getRGBNImageUrl({
|
|
|
53
54
|
// imageStartLine: 2,
|
|
54
55
|
// tilesPerLine: 20,
|
|
55
56
|
// scaleFactor: 1,
|
|
57
|
+
// rotation: Rotation.DEG_0,
|
|
56
58
|
// handleExportFrame: ExportFrameMode.FRAMEMODE_KEEP,
|
|
57
59
|
})
|
|
58
60
|
```
|
|
@@ -76,6 +78,7 @@ const fullParams: FullMonochromeImageCreationParams = {
|
|
|
76
78
|
imageStartLine: 2,
|
|
77
79
|
tilesPerLine: 20,
|
|
78
80
|
scaleFactor: 1,
|
|
81
|
+
rotation: Rotation.DEG_0,
|
|
79
82
|
handleExportFrame: ExportFrameMode.FRAMEMODE_KEEP,
|
|
80
83
|
};
|
|
81
84
|
|
|
@@ -94,6 +97,7 @@ const fullParams: FullRGBNImageCreationParams = {
|
|
|
94
97
|
imageStartLine: 2,
|
|
95
98
|
tilesPerLine: 20,
|
|
96
99
|
scaleFactor: 1,
|
|
100
|
+
rotation: Rotation.DEG_0,
|
|
97
101
|
handleExportFrame: ExportFrameMode.FRAMEMODE_KEEP,
|
|
98
102
|
};
|
|
99
103
|
|
package/dist/index.cjs
CHANGED
|
@@ -89,6 +89,13 @@ var ExportFrameMode = /* @__PURE__ */ ((ExportFrameMode2) => {
|
|
|
89
89
|
ExportFrameMode2["FRAMEMODE_SQUARE_WHITE"] = "square_white";
|
|
90
90
|
return ExportFrameMode2;
|
|
91
91
|
})(ExportFrameMode || {});
|
|
92
|
+
var Rotation = /* @__PURE__ */ ((Rotation2) => {
|
|
93
|
+
Rotation2[Rotation2["DEG_0"] = 0] = "DEG_0";
|
|
94
|
+
Rotation2[Rotation2["DEG_90"] = 1] = "DEG_90";
|
|
95
|
+
Rotation2[Rotation2["DEG_180"] = 2] = "DEG_180";
|
|
96
|
+
Rotation2[Rotation2["DEG_270"] = 3] = "DEG_270";
|
|
97
|
+
return Rotation2;
|
|
98
|
+
})(Rotation || {});
|
|
92
99
|
const channels = ["r" /* R */, "g" /* G */, "b" /* B */, "n" /* N */];
|
|
93
100
|
|
|
94
101
|
const decodeTile = (rawBytes = BLACK) => {
|
|
@@ -217,6 +224,96 @@ const createCanvasElement = () => {
|
|
|
217
224
|
}
|
|
218
225
|
};
|
|
219
226
|
|
|
227
|
+
const scaleRawImageData = (data, width, height, scale) => {
|
|
228
|
+
const newWidth = width * scale;
|
|
229
|
+
const newHeight = height * scale;
|
|
230
|
+
const scaled = new Uint8ClampedArray(newWidth * newHeight * 4);
|
|
231
|
+
for (let y = 0; y < height; y += 1) {
|
|
232
|
+
for (let x = 0; x < width; x += 1) {
|
|
233
|
+
const srcIndex = (y * width + x) * 4;
|
|
234
|
+
const r = data[srcIndex];
|
|
235
|
+
const g = data[srcIndex + 1];
|
|
236
|
+
const b = data[srcIndex + 2];
|
|
237
|
+
const a = data[srcIndex + 3];
|
|
238
|
+
for (let dy = 0; dy < scale; dy += 1) {
|
|
239
|
+
for (let dx = 0; dx < scale; dx += 1) {
|
|
240
|
+
const destX = x * scale + dx;
|
|
241
|
+
const destY = y * scale + dy;
|
|
242
|
+
const destIndex = (destY * newWidth + destX) * 4;
|
|
243
|
+
scaled[destIndex] = r;
|
|
244
|
+
scaled[destIndex + 1] = g;
|
|
245
|
+
scaled[destIndex + 2] = b;
|
|
246
|
+
scaled[destIndex + 3] = a;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
data: scaled,
|
|
253
|
+
dimensions: {
|
|
254
|
+
width: newWidth,
|
|
255
|
+
height: newHeight
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
const rotateImageData = (input, width, height, rotation) => {
|
|
261
|
+
if (rotation === Rotation.DEG_0) {
|
|
262
|
+
return {
|
|
263
|
+
data: input,
|
|
264
|
+
dimensions: {
|
|
265
|
+
width,
|
|
266
|
+
height
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
const channels = 4;
|
|
271
|
+
const output = new Uint8ClampedArray(input.length);
|
|
272
|
+
const getIndex = (x, y, w) => (y * w + x) * channels;
|
|
273
|
+
let newWidth = width;
|
|
274
|
+
let newHeight = height;
|
|
275
|
+
switch (rotation) {
|
|
276
|
+
case Rotation.DEG_90:
|
|
277
|
+
case Rotation.DEG_270:
|
|
278
|
+
newWidth = height;
|
|
279
|
+
newHeight = width;
|
|
280
|
+
break;
|
|
281
|
+
case Rotation.DEG_180:
|
|
282
|
+
}
|
|
283
|
+
for (let y = 0; y < height; y += 1) {
|
|
284
|
+
for (let x = 0; x < width; x += 1) {
|
|
285
|
+
const srcIdx = getIndex(x, y, width);
|
|
286
|
+
let dstX = x;
|
|
287
|
+
let dstY = y;
|
|
288
|
+
switch (rotation) {
|
|
289
|
+
case Rotation.DEG_90:
|
|
290
|
+
dstX = height - y - 1;
|
|
291
|
+
dstY = x;
|
|
292
|
+
break;
|
|
293
|
+
case Rotation.DEG_180:
|
|
294
|
+
dstX = width - x - 1;
|
|
295
|
+
dstY = height - y - 1;
|
|
296
|
+
break;
|
|
297
|
+
case Rotation.DEG_270:
|
|
298
|
+
dstX = y;
|
|
299
|
+
dstY = width - x - 1;
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
const dstIdx = getIndex(dstX, dstY, newWidth);
|
|
303
|
+
for (let i = 0; i < channels; i += 1) {
|
|
304
|
+
output[dstIdx + i] = input[srcIdx + i];
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return {
|
|
309
|
+
data: output,
|
|
310
|
+
dimensions: {
|
|
311
|
+
width: newWidth,
|
|
312
|
+
height: newHeight
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
};
|
|
316
|
+
|
|
220
317
|
const padLines = {
|
|
221
318
|
[ExportFrameMode.FRAMEMODE_SQUARE_BLACK]: BLACK_LINE,
|
|
222
319
|
[ExportFrameMode.FRAMEMODE_SQUARE_WHITE]: WHITE_LINE
|
|
@@ -323,40 +420,9 @@ const getFullParams$1 = (params) => ({
|
|
|
323
420
|
imageStartLine: typeof params.imageStartLine === "number" ? params.imageStartLine : FRAME_WIDTH,
|
|
324
421
|
tilesPerLine: params.tilesPerLine || TILES_PER_LINE,
|
|
325
422
|
scaleFactor: params.scaleFactor || 1,
|
|
423
|
+
rotation: params.rotation || Rotation.DEG_0,
|
|
326
424
|
handleExportFrame: params.handleExportFrame || ExportFrameMode.FRAMEMODE_KEEP
|
|
327
425
|
});
|
|
328
|
-
const scaleRawImageData = (data, width, height, scale) => {
|
|
329
|
-
const newWidth = width * scale;
|
|
330
|
-
const newHeight = height * scale;
|
|
331
|
-
const scaled = new Uint8ClampedArray(newWidth * newHeight * 4);
|
|
332
|
-
for (let y = 0; y < height; y += 1) {
|
|
333
|
-
for (let x = 0; x < width; x += 1) {
|
|
334
|
-
const srcIndex = (y * width + x) * 4;
|
|
335
|
-
const r = data[srcIndex];
|
|
336
|
-
const g = data[srcIndex + 1];
|
|
337
|
-
const b = data[srcIndex + 2];
|
|
338
|
-
const a = data[srcIndex + 3];
|
|
339
|
-
for (let dy = 0; dy < scale; dy += 1) {
|
|
340
|
-
for (let dx = 0; dx < scale; dx += 1) {
|
|
341
|
-
const destX = x * scale + dx;
|
|
342
|
-
const destY = y * scale + dy;
|
|
343
|
-
const destIndex = (destY * newWidth + destX) * 4;
|
|
344
|
-
scaled[destIndex] = r;
|
|
345
|
-
scaled[destIndex + 1] = g;
|
|
346
|
-
scaled[destIndex + 2] = b;
|
|
347
|
-
scaled[destIndex + 3] = a;
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
return {
|
|
353
|
-
data: scaled,
|
|
354
|
-
dimensions: {
|
|
355
|
-
width: newWidth,
|
|
356
|
-
height: newHeight
|
|
357
|
-
}
|
|
358
|
-
};
|
|
359
|
-
};
|
|
360
426
|
const getRawMonochromeImageData = (params) => {
|
|
361
427
|
const {
|
|
362
428
|
tiles,
|
|
@@ -365,7 +431,8 @@ const getRawMonochromeImageData = (params) => {
|
|
|
365
431
|
framePalette,
|
|
366
432
|
tilesPerLine,
|
|
367
433
|
handleExportFrame,
|
|
368
|
-
scaleFactor
|
|
434
|
+
scaleFactor,
|
|
435
|
+
rotation
|
|
369
436
|
} = params;
|
|
370
437
|
const imageContext = {
|
|
371
438
|
tilesPerLine,
|
|
@@ -392,7 +459,14 @@ const getRawMonochromeImageData = (params) => {
|
|
|
392
459
|
usedTiles.forEach((newTile, index) => {
|
|
393
460
|
renderTile(rawImageData, newTile, index, updatedContext);
|
|
394
461
|
});
|
|
395
|
-
|
|
462
|
+
const {
|
|
463
|
+
data: rotatedData,
|
|
464
|
+
dimensions: {
|
|
465
|
+
width: rWidth,
|
|
466
|
+
height: rHeight
|
|
467
|
+
}
|
|
468
|
+
} = rotateImageData(rawImageData, width, height, rotation);
|
|
469
|
+
return scaleRawImageData(rotatedData, rWidth, rHeight, scaleFactor);
|
|
396
470
|
};
|
|
397
471
|
const getMonochromeImageUrl = async (params, canvasCreator = createCanvasElement) => {
|
|
398
472
|
const urlCache = new UrlCache();
|
|
@@ -413,6 +487,7 @@ const getFullParams = (params) => ({
|
|
|
413
487
|
imageStartLine: typeof params.imageStartLine === "number" ? params.imageStartLine : FRAME_WIDTH,
|
|
414
488
|
tilesPerLine: params.tilesPerLine || TILES_PER_LINE,
|
|
415
489
|
scaleFactor: params.scaleFactor || 1,
|
|
490
|
+
rotation: params.rotation || Rotation.DEG_0,
|
|
416
491
|
handleExportFrame: params.handleExportFrame || ExportFrameMode.FRAMEMODE_KEEP
|
|
417
492
|
});
|
|
418
493
|
const singleChannelPalette = (palette, channelKey) => {
|
|
@@ -476,9 +551,13 @@ const getRawRGBNImageData = (params, canvasCreator) => {
|
|
|
476
551
|
lockFrame,
|
|
477
552
|
tilesPerLine,
|
|
478
553
|
handleExportFrame,
|
|
479
|
-
scaleFactor
|
|
554
|
+
scaleFactor,
|
|
555
|
+
rotation
|
|
480
556
|
} = params;
|
|
481
557
|
const canvases = Object.entries(tiles).reduce((acc, [key, channelTiles]) => {
|
|
558
|
+
if (!channelTiles?.length) {
|
|
559
|
+
return acc;
|
|
560
|
+
}
|
|
482
561
|
const channelKey = key;
|
|
483
562
|
const channelShades = palette[channelKey] || RGBN_SHADES;
|
|
484
563
|
const channelPalette = singleChannelPalette(channelShades, channelKey);
|
|
@@ -492,6 +571,7 @@ const getRawRGBNImageData = (params, canvasCreator) => {
|
|
|
492
571
|
handleExportFrame,
|
|
493
572
|
imageStartLine,
|
|
494
573
|
scaleFactor,
|
|
574
|
+
rotation,
|
|
495
575
|
tiles: channelTiles,
|
|
496
576
|
tilesPerLine
|
|
497
577
|
});
|
|
@@ -531,6 +611,7 @@ exports.ChannelKey = ChannelKey;
|
|
|
531
611
|
exports.ExportFrameMode = ExportFrameMode;
|
|
532
612
|
exports.FRAME_WIDTH = FRAME_WIDTH;
|
|
533
613
|
exports.RGBN_SHADES = RGBN_SHADES;
|
|
614
|
+
exports.Rotation = Rotation;
|
|
534
615
|
exports.SKIP_LINE = SKIP_LINE;
|
|
535
616
|
exports.TILES_PER_COLUMN = TILES_PER_COLUMN;
|
|
536
617
|
exports.TILES_PER_LINE = TILES_PER_LINE;
|
|
@@ -550,5 +631,4 @@ exports.getRGBValue = getRGBValue;
|
|
|
550
631
|
exports.getRawMonochromeImageData = getRawMonochromeImageData;
|
|
551
632
|
exports.getRawRGBNImageData = getRawRGBNImageData;
|
|
552
633
|
exports.maxTiles = maxTiles;
|
|
553
|
-
exports.scaleRawImageData = scaleRawImageData;
|
|
554
634
|
exports.tileIndexIsPartOfFrame = tileIndexIsPartOfFrame;
|
package/dist/index.d.cts
CHANGED
|
@@ -28,6 +28,12 @@ declare enum ExportFrameMode {
|
|
|
28
28
|
FRAMEMODE_SQUARE_BLACK = "square_black",
|
|
29
29
|
FRAMEMODE_SQUARE_WHITE = "square_white"
|
|
30
30
|
}
|
|
31
|
+
declare enum Rotation {
|
|
32
|
+
DEG_0 = 0,
|
|
33
|
+
DEG_90 = 1,
|
|
34
|
+
DEG_180 = 2,
|
|
35
|
+
DEG_270 = 3
|
|
36
|
+
}
|
|
31
37
|
declare const channels: ChannelKey[];
|
|
32
38
|
|
|
33
39
|
type RGBNTiles = Partial<Record<ChannelKey, string[]>>;
|
|
@@ -52,6 +58,7 @@ interface BaseImageCreationParams<TilesType> {
|
|
|
52
58
|
imageStartLine?: number;
|
|
53
59
|
tilesPerLine?: number;
|
|
54
60
|
scaleFactor?: number;
|
|
61
|
+
rotation?: Rotation;
|
|
55
62
|
handleExportFrame?: ExportFrameMode;
|
|
56
63
|
}
|
|
57
64
|
interface MonochromeImageCreationParams extends BaseImageCreationParams<string[]> {
|
|
@@ -88,7 +95,6 @@ interface RawOutput {
|
|
|
88
95
|
}
|
|
89
96
|
|
|
90
97
|
declare const getDimensions: (tilesLength: number, tilesPerLine: number) => PixelDimensions;
|
|
91
|
-
declare const scaleRawImageData: (data: Uint8ClampedArray, width: number, height: number, scale: number) => RawOutput;
|
|
92
98
|
declare const getRawMonochromeImageData: (params: FullMonochromeImageCreationParams) => RawOutput;
|
|
93
99
|
declare const getMonochromeImageUrl: (params: MonochromeImageCreationParams, canvasCreator?: CanvasCreator) => Promise<string>;
|
|
94
100
|
|
|
@@ -130,4 +136,4 @@ declare const WHITE_LINE: string[];
|
|
|
130
136
|
declare const RGBN_SHADES: number[];
|
|
131
137
|
declare const defaultRGBNPalette: RGBNPalette;
|
|
132
138
|
|
|
133
|
-
export { BLACK, BLACK_LINE, type BWPalette, BW_PALETTE, BW_PALETTE_HEX, type BaseImageContext, type BaseImageCreationParams, BlendMode, type CanvasCreator, ChannelKey, type CropResult, ExportFrameMode, FRAME_WIDTH, type FullMonochromeImageCreationParams, type FullRGBNImageCreationParams, type IndexedTilePixels, type MonochromeImageContext, type MonochromeImageCreationParams, type PixelDimensions, type RGBNImageCreationParams, type RGBNPalette, type RGBNTiles, RGBN_SHADES, type RGBValue, type RawOutput, SKIP_LINE, type SourceCanvases, TILES_PER_COLUMN, TILES_PER_LINE, TILE_PIXEL_HEIGHT, TILE_PIXEL_WIDTH, UrlCache, WHITE, WHITE_LINE, blendModeNewName, channels, decodeTile, defaultRGBNPalette, getDimensions, getMonochromeImageUrl, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles,
|
|
139
|
+
export { BLACK, BLACK_LINE, type BWPalette, BW_PALETTE, BW_PALETTE_HEX, type BaseImageContext, type BaseImageCreationParams, BlendMode, type CanvasCreator, ChannelKey, type CropResult, ExportFrameMode, FRAME_WIDTH, type FullMonochromeImageCreationParams, type FullRGBNImageCreationParams, type IndexedTilePixels, type MonochromeImageContext, type MonochromeImageCreationParams, type PixelDimensions, type RGBNImageCreationParams, type RGBNPalette, type RGBNTiles, RGBN_SHADES, type RGBValue, type RawOutput, Rotation, SKIP_LINE, type SourceCanvases, TILES_PER_COLUMN, TILES_PER_LINE, TILE_PIXEL_HEIGHT, TILE_PIXEL_WIDTH, UrlCache, WHITE, WHITE_LINE, blendModeNewName, channels, decodeTile, defaultRGBNPalette, getDimensions, getMonochromeImageUrl, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame };
|
package/dist/index.d.mts
CHANGED
|
@@ -28,6 +28,12 @@ declare enum ExportFrameMode {
|
|
|
28
28
|
FRAMEMODE_SQUARE_BLACK = "square_black",
|
|
29
29
|
FRAMEMODE_SQUARE_WHITE = "square_white"
|
|
30
30
|
}
|
|
31
|
+
declare enum Rotation {
|
|
32
|
+
DEG_0 = 0,
|
|
33
|
+
DEG_90 = 1,
|
|
34
|
+
DEG_180 = 2,
|
|
35
|
+
DEG_270 = 3
|
|
36
|
+
}
|
|
31
37
|
declare const channels: ChannelKey[];
|
|
32
38
|
|
|
33
39
|
type RGBNTiles = Partial<Record<ChannelKey, string[]>>;
|
|
@@ -52,6 +58,7 @@ interface BaseImageCreationParams<TilesType> {
|
|
|
52
58
|
imageStartLine?: number;
|
|
53
59
|
tilesPerLine?: number;
|
|
54
60
|
scaleFactor?: number;
|
|
61
|
+
rotation?: Rotation;
|
|
55
62
|
handleExportFrame?: ExportFrameMode;
|
|
56
63
|
}
|
|
57
64
|
interface MonochromeImageCreationParams extends BaseImageCreationParams<string[]> {
|
|
@@ -88,7 +95,6 @@ interface RawOutput {
|
|
|
88
95
|
}
|
|
89
96
|
|
|
90
97
|
declare const getDimensions: (tilesLength: number, tilesPerLine: number) => PixelDimensions;
|
|
91
|
-
declare const scaleRawImageData: (data: Uint8ClampedArray, width: number, height: number, scale: number) => RawOutput;
|
|
92
98
|
declare const getRawMonochromeImageData: (params: FullMonochromeImageCreationParams) => RawOutput;
|
|
93
99
|
declare const getMonochromeImageUrl: (params: MonochromeImageCreationParams, canvasCreator?: CanvasCreator) => Promise<string>;
|
|
94
100
|
|
|
@@ -130,4 +136,4 @@ declare const WHITE_LINE: string[];
|
|
|
130
136
|
declare const RGBN_SHADES: number[];
|
|
131
137
|
declare const defaultRGBNPalette: RGBNPalette;
|
|
132
138
|
|
|
133
|
-
export { BLACK, BLACK_LINE, type BWPalette, BW_PALETTE, BW_PALETTE_HEX, type BaseImageContext, type BaseImageCreationParams, BlendMode, type CanvasCreator, ChannelKey, type CropResult, ExportFrameMode, FRAME_WIDTH, type FullMonochromeImageCreationParams, type FullRGBNImageCreationParams, type IndexedTilePixels, type MonochromeImageContext, type MonochromeImageCreationParams, type PixelDimensions, type RGBNImageCreationParams, type RGBNPalette, type RGBNTiles, RGBN_SHADES, type RGBValue, type RawOutput, SKIP_LINE, type SourceCanvases, TILES_PER_COLUMN, TILES_PER_LINE, TILE_PIXEL_HEIGHT, TILE_PIXEL_WIDTH, UrlCache, WHITE, WHITE_LINE, blendModeNewName, channels, decodeTile, defaultRGBNPalette, getDimensions, getMonochromeImageUrl, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles,
|
|
139
|
+
export { BLACK, BLACK_LINE, type BWPalette, BW_PALETTE, BW_PALETTE_HEX, type BaseImageContext, type BaseImageCreationParams, BlendMode, type CanvasCreator, ChannelKey, type CropResult, ExportFrameMode, FRAME_WIDTH, type FullMonochromeImageCreationParams, type FullRGBNImageCreationParams, type IndexedTilePixels, type MonochromeImageContext, type MonochromeImageCreationParams, type PixelDimensions, type RGBNImageCreationParams, type RGBNPalette, type RGBNTiles, RGBN_SHADES, type RGBValue, type RawOutput, Rotation, SKIP_LINE, type SourceCanvases, TILES_PER_COLUMN, TILES_PER_LINE, TILE_PIXEL_HEIGHT, TILE_PIXEL_WIDTH, UrlCache, WHITE, WHITE_LINE, blendModeNewName, channels, decodeTile, defaultRGBNPalette, getDimensions, getMonochromeImageUrl, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame };
|
package/dist/index.d.ts
CHANGED
|
@@ -28,6 +28,12 @@ declare enum ExportFrameMode {
|
|
|
28
28
|
FRAMEMODE_SQUARE_BLACK = "square_black",
|
|
29
29
|
FRAMEMODE_SQUARE_WHITE = "square_white"
|
|
30
30
|
}
|
|
31
|
+
declare enum Rotation {
|
|
32
|
+
DEG_0 = 0,
|
|
33
|
+
DEG_90 = 1,
|
|
34
|
+
DEG_180 = 2,
|
|
35
|
+
DEG_270 = 3
|
|
36
|
+
}
|
|
31
37
|
declare const channels: ChannelKey[];
|
|
32
38
|
|
|
33
39
|
type RGBNTiles = Partial<Record<ChannelKey, string[]>>;
|
|
@@ -52,6 +58,7 @@ interface BaseImageCreationParams<TilesType> {
|
|
|
52
58
|
imageStartLine?: number;
|
|
53
59
|
tilesPerLine?: number;
|
|
54
60
|
scaleFactor?: number;
|
|
61
|
+
rotation?: Rotation;
|
|
55
62
|
handleExportFrame?: ExportFrameMode;
|
|
56
63
|
}
|
|
57
64
|
interface MonochromeImageCreationParams extends BaseImageCreationParams<string[]> {
|
|
@@ -88,7 +95,6 @@ interface RawOutput {
|
|
|
88
95
|
}
|
|
89
96
|
|
|
90
97
|
declare const getDimensions: (tilesLength: number, tilesPerLine: number) => PixelDimensions;
|
|
91
|
-
declare const scaleRawImageData: (data: Uint8ClampedArray, width: number, height: number, scale: number) => RawOutput;
|
|
92
98
|
declare const getRawMonochromeImageData: (params: FullMonochromeImageCreationParams) => RawOutput;
|
|
93
99
|
declare const getMonochromeImageUrl: (params: MonochromeImageCreationParams, canvasCreator?: CanvasCreator) => Promise<string>;
|
|
94
100
|
|
|
@@ -130,4 +136,4 @@ declare const WHITE_LINE: string[];
|
|
|
130
136
|
declare const RGBN_SHADES: number[];
|
|
131
137
|
declare const defaultRGBNPalette: RGBNPalette;
|
|
132
138
|
|
|
133
|
-
export { BLACK, BLACK_LINE, type BWPalette, BW_PALETTE, BW_PALETTE_HEX, type BaseImageContext, type BaseImageCreationParams, BlendMode, type CanvasCreator, ChannelKey, type CropResult, ExportFrameMode, FRAME_WIDTH, type FullMonochromeImageCreationParams, type FullRGBNImageCreationParams, type IndexedTilePixels, type MonochromeImageContext, type MonochromeImageCreationParams, type PixelDimensions, type RGBNImageCreationParams, type RGBNPalette, type RGBNTiles, RGBN_SHADES, type RGBValue, type RawOutput, SKIP_LINE, type SourceCanvases, TILES_PER_COLUMN, TILES_PER_LINE, TILE_PIXEL_HEIGHT, TILE_PIXEL_WIDTH, UrlCache, WHITE, WHITE_LINE, blendModeNewName, channels, decodeTile, defaultRGBNPalette, getDimensions, getMonochromeImageUrl, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles,
|
|
139
|
+
export { BLACK, BLACK_LINE, type BWPalette, BW_PALETTE, BW_PALETTE_HEX, type BaseImageContext, type BaseImageCreationParams, BlendMode, type CanvasCreator, ChannelKey, type CropResult, ExportFrameMode, FRAME_WIDTH, type FullMonochromeImageCreationParams, type FullRGBNImageCreationParams, type IndexedTilePixels, type MonochromeImageContext, type MonochromeImageCreationParams, type PixelDimensions, type RGBNImageCreationParams, type RGBNPalette, type RGBNTiles, RGBN_SHADES, type RGBValue, type RawOutput, Rotation, SKIP_LINE, type SourceCanvases, TILES_PER_COLUMN, TILES_PER_LINE, TILE_PIXEL_HEIGHT, TILE_PIXEL_WIDTH, UrlCache, WHITE, WHITE_LINE, blendModeNewName, channels, decodeTile, defaultRGBNPalette, getDimensions, getMonochromeImageUrl, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame };
|
package/dist/index.mjs
CHANGED
|
@@ -87,6 +87,13 @@ var ExportFrameMode = /* @__PURE__ */ ((ExportFrameMode2) => {
|
|
|
87
87
|
ExportFrameMode2["FRAMEMODE_SQUARE_WHITE"] = "square_white";
|
|
88
88
|
return ExportFrameMode2;
|
|
89
89
|
})(ExportFrameMode || {});
|
|
90
|
+
var Rotation = /* @__PURE__ */ ((Rotation2) => {
|
|
91
|
+
Rotation2[Rotation2["DEG_0"] = 0] = "DEG_0";
|
|
92
|
+
Rotation2[Rotation2["DEG_90"] = 1] = "DEG_90";
|
|
93
|
+
Rotation2[Rotation2["DEG_180"] = 2] = "DEG_180";
|
|
94
|
+
Rotation2[Rotation2["DEG_270"] = 3] = "DEG_270";
|
|
95
|
+
return Rotation2;
|
|
96
|
+
})(Rotation || {});
|
|
90
97
|
const channels = ["r" /* R */, "g" /* G */, "b" /* B */, "n" /* N */];
|
|
91
98
|
|
|
92
99
|
const decodeTile = (rawBytes = BLACK) => {
|
|
@@ -215,6 +222,96 @@ const createCanvasElement = () => {
|
|
|
215
222
|
}
|
|
216
223
|
};
|
|
217
224
|
|
|
225
|
+
const scaleRawImageData = (data, width, height, scale) => {
|
|
226
|
+
const newWidth = width * scale;
|
|
227
|
+
const newHeight = height * scale;
|
|
228
|
+
const scaled = new Uint8ClampedArray(newWidth * newHeight * 4);
|
|
229
|
+
for (let y = 0; y < height; y += 1) {
|
|
230
|
+
for (let x = 0; x < width; x += 1) {
|
|
231
|
+
const srcIndex = (y * width + x) * 4;
|
|
232
|
+
const r = data[srcIndex];
|
|
233
|
+
const g = data[srcIndex + 1];
|
|
234
|
+
const b = data[srcIndex + 2];
|
|
235
|
+
const a = data[srcIndex + 3];
|
|
236
|
+
for (let dy = 0; dy < scale; dy += 1) {
|
|
237
|
+
for (let dx = 0; dx < scale; dx += 1) {
|
|
238
|
+
const destX = x * scale + dx;
|
|
239
|
+
const destY = y * scale + dy;
|
|
240
|
+
const destIndex = (destY * newWidth + destX) * 4;
|
|
241
|
+
scaled[destIndex] = r;
|
|
242
|
+
scaled[destIndex + 1] = g;
|
|
243
|
+
scaled[destIndex + 2] = b;
|
|
244
|
+
scaled[destIndex + 3] = a;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
data: scaled,
|
|
251
|
+
dimensions: {
|
|
252
|
+
width: newWidth,
|
|
253
|
+
height: newHeight
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
const rotateImageData = (input, width, height, rotation) => {
|
|
259
|
+
if (rotation === Rotation.DEG_0) {
|
|
260
|
+
return {
|
|
261
|
+
data: input,
|
|
262
|
+
dimensions: {
|
|
263
|
+
width,
|
|
264
|
+
height
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
const channels = 4;
|
|
269
|
+
const output = new Uint8ClampedArray(input.length);
|
|
270
|
+
const getIndex = (x, y, w) => (y * w + x) * channels;
|
|
271
|
+
let newWidth = width;
|
|
272
|
+
let newHeight = height;
|
|
273
|
+
switch (rotation) {
|
|
274
|
+
case Rotation.DEG_90:
|
|
275
|
+
case Rotation.DEG_270:
|
|
276
|
+
newWidth = height;
|
|
277
|
+
newHeight = width;
|
|
278
|
+
break;
|
|
279
|
+
case Rotation.DEG_180:
|
|
280
|
+
}
|
|
281
|
+
for (let y = 0; y < height; y += 1) {
|
|
282
|
+
for (let x = 0; x < width; x += 1) {
|
|
283
|
+
const srcIdx = getIndex(x, y, width);
|
|
284
|
+
let dstX = x;
|
|
285
|
+
let dstY = y;
|
|
286
|
+
switch (rotation) {
|
|
287
|
+
case Rotation.DEG_90:
|
|
288
|
+
dstX = height - y - 1;
|
|
289
|
+
dstY = x;
|
|
290
|
+
break;
|
|
291
|
+
case Rotation.DEG_180:
|
|
292
|
+
dstX = width - x - 1;
|
|
293
|
+
dstY = height - y - 1;
|
|
294
|
+
break;
|
|
295
|
+
case Rotation.DEG_270:
|
|
296
|
+
dstX = y;
|
|
297
|
+
dstY = width - x - 1;
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
const dstIdx = getIndex(dstX, dstY, newWidth);
|
|
301
|
+
for (let i = 0; i < channels; i += 1) {
|
|
302
|
+
output[dstIdx + i] = input[srcIdx + i];
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return {
|
|
307
|
+
data: output,
|
|
308
|
+
dimensions: {
|
|
309
|
+
width: newWidth,
|
|
310
|
+
height: newHeight
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
};
|
|
314
|
+
|
|
218
315
|
const padLines = {
|
|
219
316
|
[ExportFrameMode.FRAMEMODE_SQUARE_BLACK]: BLACK_LINE,
|
|
220
317
|
[ExportFrameMode.FRAMEMODE_SQUARE_WHITE]: WHITE_LINE
|
|
@@ -321,40 +418,9 @@ const getFullParams$1 = (params) => ({
|
|
|
321
418
|
imageStartLine: typeof params.imageStartLine === "number" ? params.imageStartLine : FRAME_WIDTH,
|
|
322
419
|
tilesPerLine: params.tilesPerLine || TILES_PER_LINE,
|
|
323
420
|
scaleFactor: params.scaleFactor || 1,
|
|
421
|
+
rotation: params.rotation || Rotation.DEG_0,
|
|
324
422
|
handleExportFrame: params.handleExportFrame || ExportFrameMode.FRAMEMODE_KEEP
|
|
325
423
|
});
|
|
326
|
-
const scaleRawImageData = (data, width, height, scale) => {
|
|
327
|
-
const newWidth = width * scale;
|
|
328
|
-
const newHeight = height * scale;
|
|
329
|
-
const scaled = new Uint8ClampedArray(newWidth * newHeight * 4);
|
|
330
|
-
for (let y = 0; y < height; y += 1) {
|
|
331
|
-
for (let x = 0; x < width; x += 1) {
|
|
332
|
-
const srcIndex = (y * width + x) * 4;
|
|
333
|
-
const r = data[srcIndex];
|
|
334
|
-
const g = data[srcIndex + 1];
|
|
335
|
-
const b = data[srcIndex + 2];
|
|
336
|
-
const a = data[srcIndex + 3];
|
|
337
|
-
for (let dy = 0; dy < scale; dy += 1) {
|
|
338
|
-
for (let dx = 0; dx < scale; dx += 1) {
|
|
339
|
-
const destX = x * scale + dx;
|
|
340
|
-
const destY = y * scale + dy;
|
|
341
|
-
const destIndex = (destY * newWidth + destX) * 4;
|
|
342
|
-
scaled[destIndex] = r;
|
|
343
|
-
scaled[destIndex + 1] = g;
|
|
344
|
-
scaled[destIndex + 2] = b;
|
|
345
|
-
scaled[destIndex + 3] = a;
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
return {
|
|
351
|
-
data: scaled,
|
|
352
|
-
dimensions: {
|
|
353
|
-
width: newWidth,
|
|
354
|
-
height: newHeight
|
|
355
|
-
}
|
|
356
|
-
};
|
|
357
|
-
};
|
|
358
424
|
const getRawMonochromeImageData = (params) => {
|
|
359
425
|
const {
|
|
360
426
|
tiles,
|
|
@@ -363,7 +429,8 @@ const getRawMonochromeImageData = (params) => {
|
|
|
363
429
|
framePalette,
|
|
364
430
|
tilesPerLine,
|
|
365
431
|
handleExportFrame,
|
|
366
|
-
scaleFactor
|
|
432
|
+
scaleFactor,
|
|
433
|
+
rotation
|
|
367
434
|
} = params;
|
|
368
435
|
const imageContext = {
|
|
369
436
|
tilesPerLine,
|
|
@@ -390,7 +457,14 @@ const getRawMonochromeImageData = (params) => {
|
|
|
390
457
|
usedTiles.forEach((newTile, index) => {
|
|
391
458
|
renderTile(rawImageData, newTile, index, updatedContext);
|
|
392
459
|
});
|
|
393
|
-
|
|
460
|
+
const {
|
|
461
|
+
data: rotatedData,
|
|
462
|
+
dimensions: {
|
|
463
|
+
width: rWidth,
|
|
464
|
+
height: rHeight
|
|
465
|
+
}
|
|
466
|
+
} = rotateImageData(rawImageData, width, height, rotation);
|
|
467
|
+
return scaleRawImageData(rotatedData, rWidth, rHeight, scaleFactor);
|
|
394
468
|
};
|
|
395
469
|
const getMonochromeImageUrl = async (params, canvasCreator = createCanvasElement) => {
|
|
396
470
|
const urlCache = new UrlCache();
|
|
@@ -411,6 +485,7 @@ const getFullParams = (params) => ({
|
|
|
411
485
|
imageStartLine: typeof params.imageStartLine === "number" ? params.imageStartLine : FRAME_WIDTH,
|
|
412
486
|
tilesPerLine: params.tilesPerLine || TILES_PER_LINE,
|
|
413
487
|
scaleFactor: params.scaleFactor || 1,
|
|
488
|
+
rotation: params.rotation || Rotation.DEG_0,
|
|
414
489
|
handleExportFrame: params.handleExportFrame || ExportFrameMode.FRAMEMODE_KEEP
|
|
415
490
|
});
|
|
416
491
|
const singleChannelPalette = (palette, channelKey) => {
|
|
@@ -474,9 +549,13 @@ const getRawRGBNImageData = (params, canvasCreator) => {
|
|
|
474
549
|
lockFrame,
|
|
475
550
|
tilesPerLine,
|
|
476
551
|
handleExportFrame,
|
|
477
|
-
scaleFactor
|
|
552
|
+
scaleFactor,
|
|
553
|
+
rotation
|
|
478
554
|
} = params;
|
|
479
555
|
const canvases = Object.entries(tiles).reduce((acc, [key, channelTiles]) => {
|
|
556
|
+
if (!channelTiles?.length) {
|
|
557
|
+
return acc;
|
|
558
|
+
}
|
|
480
559
|
const channelKey = key;
|
|
481
560
|
const channelShades = palette[channelKey] || RGBN_SHADES;
|
|
482
561
|
const channelPalette = singleChannelPalette(channelShades, channelKey);
|
|
@@ -490,6 +569,7 @@ const getRawRGBNImageData = (params, canvasCreator) => {
|
|
|
490
569
|
handleExportFrame,
|
|
491
570
|
imageStartLine,
|
|
492
571
|
scaleFactor,
|
|
572
|
+
rotation,
|
|
493
573
|
tiles: channelTiles,
|
|
494
574
|
tilesPerLine
|
|
495
575
|
});
|
|
@@ -520,4 +600,4 @@ const getRGBNImageUrl = async (params, canvasCreator = createCanvasElement) => {
|
|
|
520
600
|
|
|
521
601
|
const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]?.length || 0));
|
|
522
602
|
|
|
523
|
-
export { BLACK, BLACK_LINE, BW_PALETTE, BW_PALETTE_HEX, BlendMode, ChannelKey, ExportFrameMode, FRAME_WIDTH, RGBN_SHADES, SKIP_LINE, TILES_PER_COLUMN, TILES_PER_LINE, TILE_PIXEL_HEIGHT, TILE_PIXEL_WIDTH, UrlCache, WHITE, WHITE_LINE, blendModeNewName, channels, decodeTile, defaultRGBNPalette, getDimensions$1 as getDimensions, getMonochromeImageUrl, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles,
|
|
603
|
+
export { BLACK, BLACK_LINE, BW_PALETTE, BW_PALETTE_HEX, BlendMode, ChannelKey, ExportFrameMode, FRAME_WIDTH, RGBN_SHADES, Rotation, SKIP_LINE, TILES_PER_COLUMN, TILES_PER_LINE, TILE_PIXEL_HEIGHT, TILE_PIXEL_WIDTH, UrlCache, WHITE, WHITE_LINE, blendModeNewName, channels, decodeTile, defaultRGBNPalette, getDimensions$1 as getDimensions, getMonochromeImageUrl, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame };
|