gb-image-decoder 2.0.3 → 2.1.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 +11 -2
- package/dist/index.cjs +68 -15
- package/dist/index.d.cts +7 -1
- package/dist/index.d.mts +7 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.mjs +68 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,6 +28,7 @@ const imageSrc = await getMonochromeImageUrl({
|
|
|
28
28
|
// tilesPerLine: 20,
|
|
29
29
|
// scaleFactor: 1,
|
|
30
30
|
// rotation: Rotation.DEG_0,
|
|
31
|
+
// mirror: Mirror.NO_FLIP,
|
|
31
32
|
// handleExportFrame: ExportFrameMode.FRAMEMODE_KEEP,
|
|
32
33
|
})
|
|
33
34
|
```
|
|
@@ -55,6 +56,7 @@ const imageSrc = await getRGBNImageUrl({
|
|
|
55
56
|
// tilesPerLine: 20,
|
|
56
57
|
// scaleFactor: 1,
|
|
57
58
|
// rotation: Rotation.DEG_0,
|
|
59
|
+
// mirror: Mirror.NO_FLIP,
|
|
58
60
|
// handleExportFrame: ExportFrameMode.FRAMEMODE_KEEP,
|
|
59
61
|
})
|
|
60
62
|
```
|
|
@@ -65,9 +67,11 @@ Within a node environment, `getMonochromeImageUrl` and `getRGBNImageUrl` cannot
|
|
|
65
67
|
Instead the internally used functions need to be called. All parameters are mandatory.
|
|
66
68
|
The resulting data and dimensions can be used to create an image e.g. by using the [canvas package](https://www.npmjs.com/package/canvas)
|
|
67
69
|
|
|
68
|
-
#### Monochrome images
|
|
70
|
+
#### Monochrome images
|
|
71
|
+
|
|
69
72
|
```typescript
|
|
70
73
|
import { getRawMonochromeImageData, FullMonochromeImageCreationParams, ExportFrameMode } from 'gb-image-decoder';
|
|
74
|
+
import { Mirror } from './enums';
|
|
71
75
|
|
|
72
76
|
const monoPalette = ['#ffffff', '#aaaaaa', '#555555', '#000000'];
|
|
73
77
|
|
|
@@ -79,10 +83,14 @@ const fullParams: FullMonochromeImageCreationParams = {
|
|
|
79
83
|
tilesPerLine: 20,
|
|
80
84
|
scaleFactor: 1,
|
|
81
85
|
rotation: Rotation.DEG_0,
|
|
86
|
+
mirror: Mirror.NO_FLIP,
|
|
82
87
|
handleExportFrame: ExportFrameMode.FRAMEMODE_KEEP,
|
|
83
88
|
};
|
|
84
89
|
|
|
85
|
-
const {
|
|
90
|
+
const {
|
|
91
|
+
data,
|
|
92
|
+
dimensions
|
|
93
|
+
} = getRawMonochromeImageData(fullParams);
|
|
86
94
|
```
|
|
87
95
|
|
|
88
96
|
#### RGB(N) images
|
|
@@ -98,6 +106,7 @@ const fullParams: FullRGBNImageCreationParams = {
|
|
|
98
106
|
tilesPerLine: 20,
|
|
99
107
|
scaleFactor: 1,
|
|
100
108
|
rotation: Rotation.DEG_0,
|
|
109
|
+
mirror: Mirror.NO_FLIP,
|
|
101
110
|
handleExportFrame: ExportFrameMode.FRAMEMODE_KEEP,
|
|
102
111
|
};
|
|
103
112
|
|
package/dist/index.cjs
CHANGED
|
@@ -96,6 +96,12 @@ var Rotation = /* @__PURE__ */ ((Rotation2) => {
|
|
|
96
96
|
Rotation2[Rotation2["DEG_270"] = 3] = "DEG_270";
|
|
97
97
|
return Rotation2;
|
|
98
98
|
})(Rotation || {});
|
|
99
|
+
var Mirror = /* @__PURE__ */ ((Mirror2) => {
|
|
100
|
+
Mirror2[Mirror2["NO_FLIP"] = 0] = "NO_FLIP";
|
|
101
|
+
Mirror2[Mirror2["FLIP_V"] = 1] = "FLIP_V";
|
|
102
|
+
Mirror2[Mirror2["FLIP_H"] = 2] = "FLIP_H";
|
|
103
|
+
return Mirror2;
|
|
104
|
+
})(Mirror || {});
|
|
99
105
|
const channels = ["r" /* R */, "g" /* G */, "b" /* B */, "n" /* N */];
|
|
100
106
|
|
|
101
107
|
const decodeTile = (rawBytes = BLACK) => {
|
|
@@ -185,10 +191,10 @@ class UrlCache {
|
|
|
185
191
|
const blobFromRawOutput = async ({
|
|
186
192
|
data,
|
|
187
193
|
dimensions: { width, height }
|
|
188
|
-
},
|
|
194
|
+
}, canvasCreator, type = "image/png") => {
|
|
189
195
|
const canvas = canvasCreator();
|
|
190
|
-
canvas.width = width
|
|
191
|
-
canvas.height = height
|
|
196
|
+
canvas.width = width;
|
|
197
|
+
canvas.height = height;
|
|
192
198
|
const context = canvas.getContext("2d");
|
|
193
199
|
const imageData = new ImageData(data, canvas.width, canvas.height);
|
|
194
200
|
context?.putImageData(imageData, 0, 0);
|
|
@@ -207,10 +213,10 @@ const blobFromRawOutput = async ({
|
|
|
207
213
|
});
|
|
208
214
|
};
|
|
209
215
|
|
|
210
|
-
const dataUrlFromRawOutput = async (rawOutput,
|
|
216
|
+
const dataUrlFromRawOutput = async (rawOutput, hash, canvasCreator) => {
|
|
211
217
|
const urlCache = new UrlCache();
|
|
212
218
|
urlCache.setUrl(hash, new Promise((resolve) => {
|
|
213
|
-
blobFromRawOutput(rawOutput,
|
|
219
|
+
blobFromRawOutput(rawOutput, canvasCreator).then((blob) => {
|
|
214
220
|
resolve(URL.createObjectURL(blob));
|
|
215
221
|
});
|
|
216
222
|
}));
|
|
@@ -262,8 +268,8 @@ const scaleRawImageData = (data, width, height, scale) => {
|
|
|
262
268
|
};
|
|
263
269
|
};
|
|
264
270
|
|
|
265
|
-
const
|
|
266
|
-
if (rotation === Rotation.DEG_0) {
|
|
271
|
+
const transformImageData = (input, width, height, rotation, mirror) => {
|
|
272
|
+
if (rotation === Rotation.DEG_0 && mirror === Mirror.NO_FLIP) {
|
|
267
273
|
return {
|
|
268
274
|
data: input,
|
|
269
275
|
dimensions: {
|
|
@@ -275,8 +281,49 @@ const rotateImageData = (input, width, height, rotation) => {
|
|
|
275
281
|
const channels = 4;
|
|
276
282
|
const output = new Uint8ClampedArray(input.length);
|
|
277
283
|
const getIndex = (x, y, w) => (y * w + x) * channels;
|
|
284
|
+
let source = input;
|
|
278
285
|
let newWidth = width;
|
|
279
286
|
let newHeight = height;
|
|
287
|
+
switch (mirror) {
|
|
288
|
+
case Mirror.FLIP_H: {
|
|
289
|
+
const mirrored = new Uint8ClampedArray(input.length);
|
|
290
|
+
for (let y = 0; y < height; y += 1) {
|
|
291
|
+
for (let x = 0; x < width; x += 1) {
|
|
292
|
+
const srcIdx = getIndex(x, y, width);
|
|
293
|
+
const dstIdx = getIndex(width - x - 1, y, width);
|
|
294
|
+
for (let i = 0; i < channels; i += 1) {
|
|
295
|
+
mirrored[dstIdx + i] = input[srcIdx + i];
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
source = mirrored;
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
case Mirror.FLIP_V: {
|
|
303
|
+
const mirrored = new Uint8ClampedArray(input.length);
|
|
304
|
+
for (let y = 0; y < height; y += 1) {
|
|
305
|
+
for (let x = 0; x < width; x += 1) {
|
|
306
|
+
const srcIdx = getIndex(x, y, width);
|
|
307
|
+
const dstIdx = getIndex(x, height - y - 1, width);
|
|
308
|
+
for (let i = 0; i < channels; i += 1) {
|
|
309
|
+
mirrored[dstIdx + i] = input[srcIdx + i];
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
source = mirrored;
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
case Mirror.NO_FLIP:
|
|
317
|
+
}
|
|
318
|
+
if (rotation === Rotation.DEG_0) {
|
|
319
|
+
return {
|
|
320
|
+
data: source,
|
|
321
|
+
dimensions: {
|
|
322
|
+
width,
|
|
323
|
+
height
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
}
|
|
280
327
|
switch (rotation) {
|
|
281
328
|
case Rotation.DEG_90:
|
|
282
329
|
case Rotation.DEG_270:
|
|
@@ -306,7 +353,7 @@ const rotateImageData = (input, width, height, rotation) => {
|
|
|
306
353
|
}
|
|
307
354
|
const dstIdx = getIndex(dstX, dstY, newWidth);
|
|
308
355
|
for (let i = 0; i < channels; i += 1) {
|
|
309
|
-
output[dstIdx + i] =
|
|
356
|
+
output[dstIdx + i] = source[srcIdx + i];
|
|
310
357
|
}
|
|
311
358
|
}
|
|
312
359
|
}
|
|
@@ -426,6 +473,7 @@ const getFullParams$1 = (params) => ({
|
|
|
426
473
|
tilesPerLine: params.tilesPerLine || TILES_PER_LINE,
|
|
427
474
|
scaleFactor: params.scaleFactor || 1,
|
|
428
475
|
rotation: params.rotation || Rotation.DEG_0,
|
|
476
|
+
mirror: params.mirror || Mirror.NO_FLIP,
|
|
429
477
|
handleExportFrame: params.handleExportFrame || ExportFrameMode.FRAMEMODE_KEEP
|
|
430
478
|
});
|
|
431
479
|
const getRawMonochromeImageData = (params) => {
|
|
@@ -437,7 +485,8 @@ const getRawMonochromeImageData = (params) => {
|
|
|
437
485
|
tilesPerLine,
|
|
438
486
|
handleExportFrame,
|
|
439
487
|
scaleFactor,
|
|
440
|
-
rotation
|
|
488
|
+
rotation,
|
|
489
|
+
mirror
|
|
441
490
|
} = params;
|
|
442
491
|
const imageContext = {
|
|
443
492
|
tilesPerLine,
|
|
@@ -470,7 +519,7 @@ const getRawMonochromeImageData = (params) => {
|
|
|
470
519
|
width: rWidth,
|
|
471
520
|
height: rHeight
|
|
472
521
|
}
|
|
473
|
-
} =
|
|
522
|
+
} = transformImageData(rawImageData, width, height, rotation, mirror);
|
|
474
523
|
return scaleRawImageData(rotatedData, rWidth, rHeight, scaleFactor);
|
|
475
524
|
};
|
|
476
525
|
const getMonochromeImageUrl = async (params, canvasCreator = createCanvasElement) => {
|
|
@@ -482,12 +531,12 @@ const getMonochromeImageUrl = async (params, canvasCreator = createCanvasElement
|
|
|
482
531
|
return cachedUrl;
|
|
483
532
|
}
|
|
484
533
|
const rawOutput = getRawMonochromeImageData(fullParams);
|
|
485
|
-
return dataUrlFromRawOutput(rawOutput,
|
|
534
|
+
return dataUrlFromRawOutput(rawOutput, hash, canvasCreator);
|
|
486
535
|
};
|
|
487
536
|
const getMonochromeImageBlob = async (params, fileType, canvasCreator = createCanvasElement) => {
|
|
488
537
|
const fullParams = getFullParams$1(params);
|
|
489
538
|
const rawOutput = getRawMonochromeImageData(fullParams);
|
|
490
|
-
return blobFromRawOutput(rawOutput,
|
|
539
|
+
return blobFromRawOutput(rawOutput, canvasCreator, fileType);
|
|
491
540
|
};
|
|
492
541
|
|
|
493
542
|
const getFullParams = (params) => ({
|
|
@@ -498,6 +547,7 @@ const getFullParams = (params) => ({
|
|
|
498
547
|
tilesPerLine: params.tilesPerLine || TILES_PER_LINE,
|
|
499
548
|
scaleFactor: params.scaleFactor || 1,
|
|
500
549
|
rotation: params.rotation || Rotation.DEG_0,
|
|
550
|
+
mirror: params.mirror || Mirror.NO_FLIP,
|
|
501
551
|
handleExportFrame: params.handleExportFrame || ExportFrameMode.FRAMEMODE_KEEP
|
|
502
552
|
});
|
|
503
553
|
const singleChannelPalette = (palette, channelKey) => {
|
|
@@ -562,7 +612,8 @@ const getRawRGBNImageData = (params, canvasCreator) => {
|
|
|
562
612
|
tilesPerLine,
|
|
563
613
|
handleExportFrame,
|
|
564
614
|
scaleFactor,
|
|
565
|
-
rotation
|
|
615
|
+
rotation,
|
|
616
|
+
mirror
|
|
566
617
|
} = params;
|
|
567
618
|
const canvases = Object.entries(tiles).reduce((acc, [key, channelTiles]) => {
|
|
568
619
|
if (!channelTiles?.length) {
|
|
@@ -582,6 +633,7 @@ const getRawRGBNImageData = (params, canvasCreator) => {
|
|
|
582
633
|
imageStartLine,
|
|
583
634
|
scaleFactor,
|
|
584
635
|
rotation,
|
|
636
|
+
mirror,
|
|
585
637
|
tiles: channelTiles,
|
|
586
638
|
tilesPerLine
|
|
587
639
|
});
|
|
@@ -607,12 +659,12 @@ const getRGBNImageUrl = async (params, canvasCreator = createCanvasElement) => {
|
|
|
607
659
|
return cachedUrl;
|
|
608
660
|
}
|
|
609
661
|
const rawOutput = getRawRGBNImageData(fullParams, canvasCreator);
|
|
610
|
-
return dataUrlFromRawOutput(rawOutput,
|
|
662
|
+
return dataUrlFromRawOutput(rawOutput, hash, canvasCreator);
|
|
611
663
|
};
|
|
612
664
|
const getRGBNImageBlob = async (params, fileType, canvasCreator = createCanvasElement) => {
|
|
613
665
|
const fullParams = getFullParams(params);
|
|
614
666
|
const rawOutput = getRawRGBNImageData(fullParams, canvasCreator);
|
|
615
|
-
return blobFromRawOutput(rawOutput,
|
|
667
|
+
return blobFromRawOutput(rawOutput, canvasCreator, fileType);
|
|
616
668
|
};
|
|
617
669
|
|
|
618
670
|
const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]?.length || 0));
|
|
@@ -625,6 +677,7 @@ exports.BlendMode = BlendMode;
|
|
|
625
677
|
exports.ChannelKey = ChannelKey;
|
|
626
678
|
exports.ExportFrameMode = ExportFrameMode;
|
|
627
679
|
exports.FRAME_WIDTH = FRAME_WIDTH;
|
|
680
|
+
exports.Mirror = Mirror;
|
|
628
681
|
exports.RGBN_SHADES = RGBN_SHADES;
|
|
629
682
|
exports.Rotation = Rotation;
|
|
630
683
|
exports.SKIP_LINE = SKIP_LINE;
|
package/dist/index.d.cts
CHANGED
|
@@ -34,6 +34,11 @@ declare enum Rotation {
|
|
|
34
34
|
DEG_180 = 2,
|
|
35
35
|
DEG_270 = 3
|
|
36
36
|
}
|
|
37
|
+
declare enum Mirror {
|
|
38
|
+
NO_FLIP = 0,
|
|
39
|
+
FLIP_V = 1,
|
|
40
|
+
FLIP_H = 2
|
|
41
|
+
}
|
|
37
42
|
declare const channels: ChannelKey[];
|
|
38
43
|
|
|
39
44
|
type RGBNTiles = Partial<Record<ChannelKey, string[]>>;
|
|
@@ -59,6 +64,7 @@ interface BaseImageCreationParams<TilesType> {
|
|
|
59
64
|
tilesPerLine?: number;
|
|
60
65
|
scaleFactor?: number;
|
|
61
66
|
rotation?: Rotation;
|
|
67
|
+
mirror?: Mirror;
|
|
62
68
|
handleExportFrame?: ExportFrameMode;
|
|
63
69
|
}
|
|
64
70
|
interface MonochromeImageCreationParams extends BaseImageCreationParams<string[]> {
|
|
@@ -138,4 +144,4 @@ declare const WHITE_LINE: string[];
|
|
|
138
144
|
declare const RGBN_SHADES: number[];
|
|
139
145
|
declare const defaultRGBNPalette: RGBNPalette;
|
|
140
146
|
|
|
141
|
-
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, getMonochromeImageBlob, getMonochromeImageUrl, getRGBNImageBlob, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame };
|
|
147
|
+
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, Mirror, 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, getMonochromeImageBlob, getMonochromeImageUrl, getRGBNImageBlob, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame };
|
package/dist/index.d.mts
CHANGED
|
@@ -34,6 +34,11 @@ declare enum Rotation {
|
|
|
34
34
|
DEG_180 = 2,
|
|
35
35
|
DEG_270 = 3
|
|
36
36
|
}
|
|
37
|
+
declare enum Mirror {
|
|
38
|
+
NO_FLIP = 0,
|
|
39
|
+
FLIP_V = 1,
|
|
40
|
+
FLIP_H = 2
|
|
41
|
+
}
|
|
37
42
|
declare const channels: ChannelKey[];
|
|
38
43
|
|
|
39
44
|
type RGBNTiles = Partial<Record<ChannelKey, string[]>>;
|
|
@@ -59,6 +64,7 @@ interface BaseImageCreationParams<TilesType> {
|
|
|
59
64
|
tilesPerLine?: number;
|
|
60
65
|
scaleFactor?: number;
|
|
61
66
|
rotation?: Rotation;
|
|
67
|
+
mirror?: Mirror;
|
|
62
68
|
handleExportFrame?: ExportFrameMode;
|
|
63
69
|
}
|
|
64
70
|
interface MonochromeImageCreationParams extends BaseImageCreationParams<string[]> {
|
|
@@ -138,4 +144,4 @@ declare const WHITE_LINE: string[];
|
|
|
138
144
|
declare const RGBN_SHADES: number[];
|
|
139
145
|
declare const defaultRGBNPalette: RGBNPalette;
|
|
140
146
|
|
|
141
|
-
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, getMonochromeImageBlob, getMonochromeImageUrl, getRGBNImageBlob, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame };
|
|
147
|
+
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, Mirror, 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, getMonochromeImageBlob, getMonochromeImageUrl, getRGBNImageBlob, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame };
|
package/dist/index.d.ts
CHANGED
|
@@ -34,6 +34,11 @@ declare enum Rotation {
|
|
|
34
34
|
DEG_180 = 2,
|
|
35
35
|
DEG_270 = 3
|
|
36
36
|
}
|
|
37
|
+
declare enum Mirror {
|
|
38
|
+
NO_FLIP = 0,
|
|
39
|
+
FLIP_V = 1,
|
|
40
|
+
FLIP_H = 2
|
|
41
|
+
}
|
|
37
42
|
declare const channels: ChannelKey[];
|
|
38
43
|
|
|
39
44
|
type RGBNTiles = Partial<Record<ChannelKey, string[]>>;
|
|
@@ -59,6 +64,7 @@ interface BaseImageCreationParams<TilesType> {
|
|
|
59
64
|
tilesPerLine?: number;
|
|
60
65
|
scaleFactor?: number;
|
|
61
66
|
rotation?: Rotation;
|
|
67
|
+
mirror?: Mirror;
|
|
62
68
|
handleExportFrame?: ExportFrameMode;
|
|
63
69
|
}
|
|
64
70
|
interface MonochromeImageCreationParams extends BaseImageCreationParams<string[]> {
|
|
@@ -138,4 +144,4 @@ declare const WHITE_LINE: string[];
|
|
|
138
144
|
declare const RGBN_SHADES: number[];
|
|
139
145
|
declare const defaultRGBNPalette: RGBNPalette;
|
|
140
146
|
|
|
141
|
-
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, getMonochromeImageBlob, getMonochromeImageUrl, getRGBNImageBlob, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame };
|
|
147
|
+
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, Mirror, 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, getMonochromeImageBlob, getMonochromeImageUrl, getRGBNImageBlob, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame };
|
package/dist/index.mjs
CHANGED
|
@@ -94,6 +94,12 @@ var Rotation = /* @__PURE__ */ ((Rotation2) => {
|
|
|
94
94
|
Rotation2[Rotation2["DEG_270"] = 3] = "DEG_270";
|
|
95
95
|
return Rotation2;
|
|
96
96
|
})(Rotation || {});
|
|
97
|
+
var Mirror = /* @__PURE__ */ ((Mirror2) => {
|
|
98
|
+
Mirror2[Mirror2["NO_FLIP"] = 0] = "NO_FLIP";
|
|
99
|
+
Mirror2[Mirror2["FLIP_V"] = 1] = "FLIP_V";
|
|
100
|
+
Mirror2[Mirror2["FLIP_H"] = 2] = "FLIP_H";
|
|
101
|
+
return Mirror2;
|
|
102
|
+
})(Mirror || {});
|
|
97
103
|
const channels = ["r" /* R */, "g" /* G */, "b" /* B */, "n" /* N */];
|
|
98
104
|
|
|
99
105
|
const decodeTile = (rawBytes = BLACK) => {
|
|
@@ -183,10 +189,10 @@ class UrlCache {
|
|
|
183
189
|
const blobFromRawOutput = async ({
|
|
184
190
|
data,
|
|
185
191
|
dimensions: { width, height }
|
|
186
|
-
},
|
|
192
|
+
}, canvasCreator, type = "image/png") => {
|
|
187
193
|
const canvas = canvasCreator();
|
|
188
|
-
canvas.width = width
|
|
189
|
-
canvas.height = height
|
|
194
|
+
canvas.width = width;
|
|
195
|
+
canvas.height = height;
|
|
190
196
|
const context = canvas.getContext("2d");
|
|
191
197
|
const imageData = new ImageData(data, canvas.width, canvas.height);
|
|
192
198
|
context?.putImageData(imageData, 0, 0);
|
|
@@ -205,10 +211,10 @@ const blobFromRawOutput = async ({
|
|
|
205
211
|
});
|
|
206
212
|
};
|
|
207
213
|
|
|
208
|
-
const dataUrlFromRawOutput = async (rawOutput,
|
|
214
|
+
const dataUrlFromRawOutput = async (rawOutput, hash, canvasCreator) => {
|
|
209
215
|
const urlCache = new UrlCache();
|
|
210
216
|
urlCache.setUrl(hash, new Promise((resolve) => {
|
|
211
|
-
blobFromRawOutput(rawOutput,
|
|
217
|
+
blobFromRawOutput(rawOutput, canvasCreator).then((blob) => {
|
|
212
218
|
resolve(URL.createObjectURL(blob));
|
|
213
219
|
});
|
|
214
220
|
}));
|
|
@@ -260,8 +266,8 @@ const scaleRawImageData = (data, width, height, scale) => {
|
|
|
260
266
|
};
|
|
261
267
|
};
|
|
262
268
|
|
|
263
|
-
const
|
|
264
|
-
if (rotation === Rotation.DEG_0) {
|
|
269
|
+
const transformImageData = (input, width, height, rotation, mirror) => {
|
|
270
|
+
if (rotation === Rotation.DEG_0 && mirror === Mirror.NO_FLIP) {
|
|
265
271
|
return {
|
|
266
272
|
data: input,
|
|
267
273
|
dimensions: {
|
|
@@ -273,8 +279,49 @@ const rotateImageData = (input, width, height, rotation) => {
|
|
|
273
279
|
const channels = 4;
|
|
274
280
|
const output = new Uint8ClampedArray(input.length);
|
|
275
281
|
const getIndex = (x, y, w) => (y * w + x) * channels;
|
|
282
|
+
let source = input;
|
|
276
283
|
let newWidth = width;
|
|
277
284
|
let newHeight = height;
|
|
285
|
+
switch (mirror) {
|
|
286
|
+
case Mirror.FLIP_H: {
|
|
287
|
+
const mirrored = new Uint8ClampedArray(input.length);
|
|
288
|
+
for (let y = 0; y < height; y += 1) {
|
|
289
|
+
for (let x = 0; x < width; x += 1) {
|
|
290
|
+
const srcIdx = getIndex(x, y, width);
|
|
291
|
+
const dstIdx = getIndex(width - x - 1, y, width);
|
|
292
|
+
for (let i = 0; i < channels; i += 1) {
|
|
293
|
+
mirrored[dstIdx + i] = input[srcIdx + i];
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
source = mirrored;
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
case Mirror.FLIP_V: {
|
|
301
|
+
const mirrored = new Uint8ClampedArray(input.length);
|
|
302
|
+
for (let y = 0; y < height; y += 1) {
|
|
303
|
+
for (let x = 0; x < width; x += 1) {
|
|
304
|
+
const srcIdx = getIndex(x, y, width);
|
|
305
|
+
const dstIdx = getIndex(x, height - y - 1, width);
|
|
306
|
+
for (let i = 0; i < channels; i += 1) {
|
|
307
|
+
mirrored[dstIdx + i] = input[srcIdx + i];
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
source = mirrored;
|
|
312
|
+
break;
|
|
313
|
+
}
|
|
314
|
+
case Mirror.NO_FLIP:
|
|
315
|
+
}
|
|
316
|
+
if (rotation === Rotation.DEG_0) {
|
|
317
|
+
return {
|
|
318
|
+
data: source,
|
|
319
|
+
dimensions: {
|
|
320
|
+
width,
|
|
321
|
+
height
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
}
|
|
278
325
|
switch (rotation) {
|
|
279
326
|
case Rotation.DEG_90:
|
|
280
327
|
case Rotation.DEG_270:
|
|
@@ -304,7 +351,7 @@ const rotateImageData = (input, width, height, rotation) => {
|
|
|
304
351
|
}
|
|
305
352
|
const dstIdx = getIndex(dstX, dstY, newWidth);
|
|
306
353
|
for (let i = 0; i < channels; i += 1) {
|
|
307
|
-
output[dstIdx + i] =
|
|
354
|
+
output[dstIdx + i] = source[srcIdx + i];
|
|
308
355
|
}
|
|
309
356
|
}
|
|
310
357
|
}
|
|
@@ -424,6 +471,7 @@ const getFullParams$1 = (params) => ({
|
|
|
424
471
|
tilesPerLine: params.tilesPerLine || TILES_PER_LINE,
|
|
425
472
|
scaleFactor: params.scaleFactor || 1,
|
|
426
473
|
rotation: params.rotation || Rotation.DEG_0,
|
|
474
|
+
mirror: params.mirror || Mirror.NO_FLIP,
|
|
427
475
|
handleExportFrame: params.handleExportFrame || ExportFrameMode.FRAMEMODE_KEEP
|
|
428
476
|
});
|
|
429
477
|
const getRawMonochromeImageData = (params) => {
|
|
@@ -435,7 +483,8 @@ const getRawMonochromeImageData = (params) => {
|
|
|
435
483
|
tilesPerLine,
|
|
436
484
|
handleExportFrame,
|
|
437
485
|
scaleFactor,
|
|
438
|
-
rotation
|
|
486
|
+
rotation,
|
|
487
|
+
mirror
|
|
439
488
|
} = params;
|
|
440
489
|
const imageContext = {
|
|
441
490
|
tilesPerLine,
|
|
@@ -468,7 +517,7 @@ const getRawMonochromeImageData = (params) => {
|
|
|
468
517
|
width: rWidth,
|
|
469
518
|
height: rHeight
|
|
470
519
|
}
|
|
471
|
-
} =
|
|
520
|
+
} = transformImageData(rawImageData, width, height, rotation, mirror);
|
|
472
521
|
return scaleRawImageData(rotatedData, rWidth, rHeight, scaleFactor);
|
|
473
522
|
};
|
|
474
523
|
const getMonochromeImageUrl = async (params, canvasCreator = createCanvasElement) => {
|
|
@@ -480,12 +529,12 @@ const getMonochromeImageUrl = async (params, canvasCreator = createCanvasElement
|
|
|
480
529
|
return cachedUrl;
|
|
481
530
|
}
|
|
482
531
|
const rawOutput = getRawMonochromeImageData(fullParams);
|
|
483
|
-
return dataUrlFromRawOutput(rawOutput,
|
|
532
|
+
return dataUrlFromRawOutput(rawOutput, hash$1, canvasCreator);
|
|
484
533
|
};
|
|
485
534
|
const getMonochromeImageBlob = async (params, fileType, canvasCreator = createCanvasElement) => {
|
|
486
535
|
const fullParams = getFullParams$1(params);
|
|
487
536
|
const rawOutput = getRawMonochromeImageData(fullParams);
|
|
488
|
-
return blobFromRawOutput(rawOutput,
|
|
537
|
+
return blobFromRawOutput(rawOutput, canvasCreator, fileType);
|
|
489
538
|
};
|
|
490
539
|
|
|
491
540
|
const getFullParams = (params) => ({
|
|
@@ -496,6 +545,7 @@ const getFullParams = (params) => ({
|
|
|
496
545
|
tilesPerLine: params.tilesPerLine || TILES_PER_LINE,
|
|
497
546
|
scaleFactor: params.scaleFactor || 1,
|
|
498
547
|
rotation: params.rotation || Rotation.DEG_0,
|
|
548
|
+
mirror: params.mirror || Mirror.NO_FLIP,
|
|
499
549
|
handleExportFrame: params.handleExportFrame || ExportFrameMode.FRAMEMODE_KEEP
|
|
500
550
|
});
|
|
501
551
|
const singleChannelPalette = (palette, channelKey) => {
|
|
@@ -560,7 +610,8 @@ const getRawRGBNImageData = (params, canvasCreator) => {
|
|
|
560
610
|
tilesPerLine,
|
|
561
611
|
handleExportFrame,
|
|
562
612
|
scaleFactor,
|
|
563
|
-
rotation
|
|
613
|
+
rotation,
|
|
614
|
+
mirror
|
|
564
615
|
} = params;
|
|
565
616
|
const canvases = Object.entries(tiles).reduce((acc, [key, channelTiles]) => {
|
|
566
617
|
if (!channelTiles?.length) {
|
|
@@ -580,6 +631,7 @@ const getRawRGBNImageData = (params, canvasCreator) => {
|
|
|
580
631
|
imageStartLine,
|
|
581
632
|
scaleFactor,
|
|
582
633
|
rotation,
|
|
634
|
+
mirror,
|
|
583
635
|
tiles: channelTiles,
|
|
584
636
|
tilesPerLine
|
|
585
637
|
});
|
|
@@ -605,14 +657,14 @@ const getRGBNImageUrl = async (params, canvasCreator = createCanvasElement) => {
|
|
|
605
657
|
return cachedUrl;
|
|
606
658
|
}
|
|
607
659
|
const rawOutput = getRawRGBNImageData(fullParams, canvasCreator);
|
|
608
|
-
return dataUrlFromRawOutput(rawOutput,
|
|
660
|
+
return dataUrlFromRawOutput(rawOutput, hash$1, canvasCreator);
|
|
609
661
|
};
|
|
610
662
|
const getRGBNImageBlob = async (params, fileType, canvasCreator = createCanvasElement) => {
|
|
611
663
|
const fullParams = getFullParams(params);
|
|
612
664
|
const rawOutput = getRawRGBNImageData(fullParams, canvasCreator);
|
|
613
|
-
return blobFromRawOutput(rawOutput,
|
|
665
|
+
return blobFromRawOutput(rawOutput, canvasCreator, fileType);
|
|
614
666
|
};
|
|
615
667
|
|
|
616
668
|
const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]?.length || 0));
|
|
617
669
|
|
|
618
|
-
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, getMonochromeImageBlob, getMonochromeImageUrl, getRGBNImageBlob, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame };
|
|
670
|
+
export { BLACK, BLACK_LINE, BW_PALETTE, BW_PALETTE_HEX, BlendMode, ChannelKey, ExportFrameMode, FRAME_WIDTH, Mirror, 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, getMonochromeImageBlob, getMonochromeImageUrl, getRGBNImageBlob, getRGBNImageUrl, getRGBValue, getRawMonochromeImageData, getRawRGBNImageData, maxTiles, tileIndexIsPartOfFrame };
|