gb-image-decoder 2.0.1 → 2.0.3

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
@@ -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) => {
@@ -175,32 +182,37 @@ class UrlCache {
175
182
  }
176
183
  }
177
184
 
178
- const toObjectUrl = async (canvas) => new Promise((resolve, reject) => {
179
- canvas.toBlob((blob) => {
180
- if (!blob) {
181
- reject(new Error("Could not generate Blob from canvas"));
182
- return;
183
- }
184
- try {
185
- resolve(URL.createObjectURL(blob));
186
- } catch (error) {
187
- reject(error);
188
- }
189
- });
190
- });
191
- const dataUrlFromRawOutput = async ({
185
+ const blobFromRawOutput = async ({
192
186
  data,
193
187
  dimensions: { width, height }
194
- }, scaleFactor, hash, canvasCreator) => {
188
+ }, scaleFactor, canvasCreator, type = "image/png") => {
189
+ const canvas = canvasCreator();
190
+ canvas.width = width * scaleFactor;
191
+ canvas.height = height * scaleFactor;
192
+ const context = canvas.getContext("2d");
193
+ const imageData = new ImageData(data, canvas.width, canvas.height);
194
+ context?.putImageData(imageData, 0, 0);
195
+ return new Promise((resolve, reject) => {
196
+ canvas.toBlob((blob) => {
197
+ if (!blob) {
198
+ reject(new Error("Could not generate Blob from canvas"));
199
+ return;
200
+ }
201
+ try {
202
+ resolve(blob);
203
+ } catch (error) {
204
+ reject(error);
205
+ }
206
+ }, type, 1);
207
+ });
208
+ };
209
+
210
+ const dataUrlFromRawOutput = async (rawOutput, scaleFactor, hash, canvasCreator) => {
195
211
  const urlCache = new UrlCache();
196
212
  urlCache.setUrl(hash, new Promise((resolve) => {
197
- const canvas = canvasCreator();
198
- canvas.width = width * scaleFactor;
199
- canvas.height = height * scaleFactor;
200
- const context = canvas.getContext("2d");
201
- const imageData = new ImageData(data, canvas.width, canvas.height);
202
- context?.putImageData(imageData, 0, 0);
203
- resolve(toObjectUrl(canvas));
213
+ blobFromRawOutput(rawOutput, scaleFactor, canvasCreator).then((blob) => {
214
+ resolve(URL.createObjectURL(blob));
215
+ });
204
216
  }));
205
217
  const url = await urlCache.getUrl(hash);
206
218
  if (!url) {
@@ -217,6 +229,96 @@ const createCanvasElement = () => {
217
229
  }
218
230
  };
219
231
 
232
+ const scaleRawImageData = (data, width, height, scale) => {
233
+ const newWidth = width * scale;
234
+ const newHeight = height * scale;
235
+ const scaled = new Uint8ClampedArray(newWidth * newHeight * 4);
236
+ for (let y = 0; y < height; y += 1) {
237
+ for (let x = 0; x < width; x += 1) {
238
+ const srcIndex = (y * width + x) * 4;
239
+ const r = data[srcIndex];
240
+ const g = data[srcIndex + 1];
241
+ const b = data[srcIndex + 2];
242
+ const a = data[srcIndex + 3];
243
+ for (let dy = 0; dy < scale; dy += 1) {
244
+ for (let dx = 0; dx < scale; dx += 1) {
245
+ const destX = x * scale + dx;
246
+ const destY = y * scale + dy;
247
+ const destIndex = (destY * newWidth + destX) * 4;
248
+ scaled[destIndex] = r;
249
+ scaled[destIndex + 1] = g;
250
+ scaled[destIndex + 2] = b;
251
+ scaled[destIndex + 3] = a;
252
+ }
253
+ }
254
+ }
255
+ }
256
+ return {
257
+ data: scaled,
258
+ dimensions: {
259
+ width: newWidth,
260
+ height: newHeight
261
+ }
262
+ };
263
+ };
264
+
265
+ const rotateImageData = (input, width, height, rotation) => {
266
+ if (rotation === Rotation.DEG_0) {
267
+ return {
268
+ data: input,
269
+ dimensions: {
270
+ width,
271
+ height
272
+ }
273
+ };
274
+ }
275
+ const channels = 4;
276
+ const output = new Uint8ClampedArray(input.length);
277
+ const getIndex = (x, y, w) => (y * w + x) * channels;
278
+ let newWidth = width;
279
+ let newHeight = height;
280
+ switch (rotation) {
281
+ case Rotation.DEG_90:
282
+ case Rotation.DEG_270:
283
+ newWidth = height;
284
+ newHeight = width;
285
+ break;
286
+ case Rotation.DEG_180:
287
+ }
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
+ let dstX = x;
292
+ let dstY = y;
293
+ switch (rotation) {
294
+ case Rotation.DEG_90:
295
+ dstX = height - y - 1;
296
+ dstY = x;
297
+ break;
298
+ case Rotation.DEG_180:
299
+ dstX = width - x - 1;
300
+ dstY = height - y - 1;
301
+ break;
302
+ case Rotation.DEG_270:
303
+ dstX = y;
304
+ dstY = width - x - 1;
305
+ break;
306
+ }
307
+ const dstIdx = getIndex(dstX, dstY, newWidth);
308
+ for (let i = 0; i < channels; i += 1) {
309
+ output[dstIdx + i] = input[srcIdx + i];
310
+ }
311
+ }
312
+ }
313
+ return {
314
+ data: output,
315
+ dimensions: {
316
+ width: newWidth,
317
+ height: newHeight
318
+ }
319
+ };
320
+ };
321
+
220
322
  const padLines = {
221
323
  [ExportFrameMode.FRAMEMODE_SQUARE_BLACK]: BLACK_LINE,
222
324
  [ExportFrameMode.FRAMEMODE_SQUARE_WHITE]: WHITE_LINE
@@ -323,40 +425,9 @@ const getFullParams$1 = (params) => ({
323
425
  imageStartLine: typeof params.imageStartLine === "number" ? params.imageStartLine : FRAME_WIDTH,
324
426
  tilesPerLine: params.tilesPerLine || TILES_PER_LINE,
325
427
  scaleFactor: params.scaleFactor || 1,
428
+ rotation: params.rotation || Rotation.DEG_0,
326
429
  handleExportFrame: params.handleExportFrame || ExportFrameMode.FRAMEMODE_KEEP
327
430
  });
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
431
  const getRawMonochromeImageData = (params) => {
361
432
  const {
362
433
  tiles,
@@ -365,7 +436,8 @@ const getRawMonochromeImageData = (params) => {
365
436
  framePalette,
366
437
  tilesPerLine,
367
438
  handleExportFrame,
368
- scaleFactor
439
+ scaleFactor,
440
+ rotation
369
441
  } = params;
370
442
  const imageContext = {
371
443
  tilesPerLine,
@@ -392,7 +464,14 @@ const getRawMonochromeImageData = (params) => {
392
464
  usedTiles.forEach((newTile, index) => {
393
465
  renderTile(rawImageData, newTile, index, updatedContext);
394
466
  });
395
- return scaleRawImageData(rawImageData, width, height, scaleFactor);
467
+ const {
468
+ data: rotatedData,
469
+ dimensions: {
470
+ width: rWidth,
471
+ height: rHeight
472
+ }
473
+ } = rotateImageData(rawImageData, width, height, rotation);
474
+ return scaleRawImageData(rotatedData, rWidth, rHeight, scaleFactor);
396
475
  };
397
476
  const getMonochromeImageUrl = async (params, canvasCreator = createCanvasElement) => {
398
477
  const urlCache = new UrlCache();
@@ -405,6 +484,11 @@ const getMonochromeImageUrl = async (params, canvasCreator = createCanvasElement
405
484
  const rawOutput = getRawMonochromeImageData(fullParams);
406
485
  return dataUrlFromRawOutput(rawOutput, fullParams.scaleFactor, hash, canvasCreator);
407
486
  };
487
+ const getMonochromeImageBlob = async (params, fileType, canvasCreator = createCanvasElement) => {
488
+ const fullParams = getFullParams$1(params);
489
+ const rawOutput = getRawMonochromeImageData(fullParams);
490
+ return blobFromRawOutput(rawOutput, fullParams.scaleFactor, canvasCreator, fileType);
491
+ };
408
492
 
409
493
  const getFullParams = (params) => ({
410
494
  tiles: params.tiles,
@@ -413,6 +497,7 @@ const getFullParams = (params) => ({
413
497
  imageStartLine: typeof params.imageStartLine === "number" ? params.imageStartLine : FRAME_WIDTH,
414
498
  tilesPerLine: params.tilesPerLine || TILES_PER_LINE,
415
499
  scaleFactor: params.scaleFactor || 1,
500
+ rotation: params.rotation || Rotation.DEG_0,
416
501
  handleExportFrame: params.handleExportFrame || ExportFrameMode.FRAMEMODE_KEEP
417
502
  });
418
503
  const singleChannelPalette = (palette, channelKey) => {
@@ -476,7 +561,8 @@ const getRawRGBNImageData = (params, canvasCreator) => {
476
561
  lockFrame,
477
562
  tilesPerLine,
478
563
  handleExportFrame,
479
- scaleFactor
564
+ scaleFactor,
565
+ rotation
480
566
  } = params;
481
567
  const canvases = Object.entries(tiles).reduce((acc, [key, channelTiles]) => {
482
568
  if (!channelTiles?.length) {
@@ -495,6 +581,7 @@ const getRawRGBNImageData = (params, canvasCreator) => {
495
581
  handleExportFrame,
496
582
  imageStartLine,
497
583
  scaleFactor,
584
+ rotation,
498
585
  tiles: channelTiles,
499
586
  tilesPerLine
500
587
  });
@@ -522,6 +609,11 @@ const getRGBNImageUrl = async (params, canvasCreator = createCanvasElement) => {
522
609
  const rawOutput = getRawRGBNImageData(fullParams, canvasCreator);
523
610
  return dataUrlFromRawOutput(rawOutput, fullParams.scaleFactor, hash, canvasCreator);
524
611
  };
612
+ const getRGBNImageBlob = async (params, fileType, canvasCreator = createCanvasElement) => {
613
+ const fullParams = getFullParams(params);
614
+ const rawOutput = getRawRGBNImageData(fullParams, canvasCreator);
615
+ return blobFromRawOutput(rawOutput, fullParams.scaleFactor, canvasCreator, fileType);
616
+ };
525
617
 
526
618
  const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]?.length || 0));
527
619
 
@@ -534,6 +626,7 @@ exports.ChannelKey = ChannelKey;
534
626
  exports.ExportFrameMode = ExportFrameMode;
535
627
  exports.FRAME_WIDTH = FRAME_WIDTH;
536
628
  exports.RGBN_SHADES = RGBN_SHADES;
629
+ exports.Rotation = Rotation;
537
630
  exports.SKIP_LINE = SKIP_LINE;
538
631
  exports.TILES_PER_COLUMN = TILES_PER_COLUMN;
539
632
  exports.TILES_PER_LINE = TILES_PER_LINE;
@@ -547,11 +640,12 @@ exports.channels = channels;
547
640
  exports.decodeTile = decodeTile;
548
641
  exports.defaultRGBNPalette = defaultRGBNPalette;
549
642
  exports.getDimensions = getDimensions$1;
643
+ exports.getMonochromeImageBlob = getMonochromeImageBlob;
550
644
  exports.getMonochromeImageUrl = getMonochromeImageUrl;
645
+ exports.getRGBNImageBlob = getRGBNImageBlob;
551
646
  exports.getRGBNImageUrl = getRGBNImageUrl;
552
647
  exports.getRGBValue = getRGBValue;
553
648
  exports.getRawMonochromeImageData = getRawMonochromeImageData;
554
649
  exports.getRawRGBNImageData = getRawRGBNImageData;
555
650
  exports.maxTiles = maxTiles;
556
- exports.scaleRawImageData = scaleRawImageData;
557
651
  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,12 +95,13 @@ 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>;
100
+ declare const getMonochromeImageBlob: (params: MonochromeImageCreationParams, fileType: string, canvasCreator?: CanvasCreator) => Promise<Blob>;
94
101
 
95
102
  declare const getRawRGBNImageData: (params: FullRGBNImageCreationParams, canvasCreator: CanvasCreator) => RawOutput;
96
103
  declare const getRGBNImageUrl: (params: RGBNImageCreationParams, canvasCreator?: CanvasCreator) => Promise<string>;
104
+ declare const getRGBNImageBlob: (params: RGBNImageCreationParams, fileType: string, canvasCreator?: CanvasCreator) => Promise<Blob>;
97
105
 
98
106
  declare class UrlCache {
99
107
  static rendered: Record<string, string>;
@@ -130,4 +138,4 @@ declare const WHITE_LINE: string[];
130
138
  declare const RGBN_SHADES: number[];
131
139
  declare const defaultRGBNPalette: RGBNPalette;
132
140
 
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, scaleRawImageData, tileIndexIsPartOfFrame };
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 };
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,12 +95,13 @@ 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>;
100
+ declare const getMonochromeImageBlob: (params: MonochromeImageCreationParams, fileType: string, canvasCreator?: CanvasCreator) => Promise<Blob>;
94
101
 
95
102
  declare const getRawRGBNImageData: (params: FullRGBNImageCreationParams, canvasCreator: CanvasCreator) => RawOutput;
96
103
  declare const getRGBNImageUrl: (params: RGBNImageCreationParams, canvasCreator?: CanvasCreator) => Promise<string>;
104
+ declare const getRGBNImageBlob: (params: RGBNImageCreationParams, fileType: string, canvasCreator?: CanvasCreator) => Promise<Blob>;
97
105
 
98
106
  declare class UrlCache {
99
107
  static rendered: Record<string, string>;
@@ -130,4 +138,4 @@ declare const WHITE_LINE: string[];
130
138
  declare const RGBN_SHADES: number[];
131
139
  declare const defaultRGBNPalette: RGBNPalette;
132
140
 
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, scaleRawImageData, tileIndexIsPartOfFrame };
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 };
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,12 +95,13 @@ 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>;
100
+ declare const getMonochromeImageBlob: (params: MonochromeImageCreationParams, fileType: string, canvasCreator?: CanvasCreator) => Promise<Blob>;
94
101
 
95
102
  declare const getRawRGBNImageData: (params: FullRGBNImageCreationParams, canvasCreator: CanvasCreator) => RawOutput;
96
103
  declare const getRGBNImageUrl: (params: RGBNImageCreationParams, canvasCreator?: CanvasCreator) => Promise<string>;
104
+ declare const getRGBNImageBlob: (params: RGBNImageCreationParams, fileType: string, canvasCreator?: CanvasCreator) => Promise<Blob>;
97
105
 
98
106
  declare class UrlCache {
99
107
  static rendered: Record<string, string>;
@@ -130,4 +138,4 @@ declare const WHITE_LINE: string[];
130
138
  declare const RGBN_SHADES: number[];
131
139
  declare const defaultRGBNPalette: RGBNPalette;
132
140
 
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, scaleRawImageData, tileIndexIsPartOfFrame };
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 };
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) => {
@@ -173,32 +180,37 @@ class UrlCache {
173
180
  }
174
181
  }
175
182
 
176
- const toObjectUrl = async (canvas) => new Promise((resolve, reject) => {
177
- canvas.toBlob((blob) => {
178
- if (!blob) {
179
- reject(new Error("Could not generate Blob from canvas"));
180
- return;
181
- }
182
- try {
183
- resolve(URL.createObjectURL(blob));
184
- } catch (error) {
185
- reject(error);
186
- }
187
- });
188
- });
189
- const dataUrlFromRawOutput = async ({
183
+ const blobFromRawOutput = async ({
190
184
  data,
191
185
  dimensions: { width, height }
192
- }, scaleFactor, hash, canvasCreator) => {
186
+ }, scaleFactor, canvasCreator, type = "image/png") => {
187
+ const canvas = canvasCreator();
188
+ canvas.width = width * scaleFactor;
189
+ canvas.height = height * scaleFactor;
190
+ const context = canvas.getContext("2d");
191
+ const imageData = new ImageData(data, canvas.width, canvas.height);
192
+ context?.putImageData(imageData, 0, 0);
193
+ return new Promise((resolve, reject) => {
194
+ canvas.toBlob((blob) => {
195
+ if (!blob) {
196
+ reject(new Error("Could not generate Blob from canvas"));
197
+ return;
198
+ }
199
+ try {
200
+ resolve(blob);
201
+ } catch (error) {
202
+ reject(error);
203
+ }
204
+ }, type, 1);
205
+ });
206
+ };
207
+
208
+ const dataUrlFromRawOutput = async (rawOutput, scaleFactor, hash, canvasCreator) => {
193
209
  const urlCache = new UrlCache();
194
210
  urlCache.setUrl(hash, new Promise((resolve) => {
195
- const canvas = canvasCreator();
196
- canvas.width = width * scaleFactor;
197
- canvas.height = height * scaleFactor;
198
- const context = canvas.getContext("2d");
199
- const imageData = new ImageData(data, canvas.width, canvas.height);
200
- context?.putImageData(imageData, 0, 0);
201
- resolve(toObjectUrl(canvas));
211
+ blobFromRawOutput(rawOutput, scaleFactor, canvasCreator).then((blob) => {
212
+ resolve(URL.createObjectURL(blob));
213
+ });
202
214
  }));
203
215
  const url = await urlCache.getUrl(hash);
204
216
  if (!url) {
@@ -215,6 +227,96 @@ const createCanvasElement = () => {
215
227
  }
216
228
  };
217
229
 
230
+ const scaleRawImageData = (data, width, height, scale) => {
231
+ const newWidth = width * scale;
232
+ const newHeight = height * scale;
233
+ const scaled = new Uint8ClampedArray(newWidth * newHeight * 4);
234
+ for (let y = 0; y < height; y += 1) {
235
+ for (let x = 0; x < width; x += 1) {
236
+ const srcIndex = (y * width + x) * 4;
237
+ const r = data[srcIndex];
238
+ const g = data[srcIndex + 1];
239
+ const b = data[srcIndex + 2];
240
+ const a = data[srcIndex + 3];
241
+ for (let dy = 0; dy < scale; dy += 1) {
242
+ for (let dx = 0; dx < scale; dx += 1) {
243
+ const destX = x * scale + dx;
244
+ const destY = y * scale + dy;
245
+ const destIndex = (destY * newWidth + destX) * 4;
246
+ scaled[destIndex] = r;
247
+ scaled[destIndex + 1] = g;
248
+ scaled[destIndex + 2] = b;
249
+ scaled[destIndex + 3] = a;
250
+ }
251
+ }
252
+ }
253
+ }
254
+ return {
255
+ data: scaled,
256
+ dimensions: {
257
+ width: newWidth,
258
+ height: newHeight
259
+ }
260
+ };
261
+ };
262
+
263
+ const rotateImageData = (input, width, height, rotation) => {
264
+ if (rotation === Rotation.DEG_0) {
265
+ return {
266
+ data: input,
267
+ dimensions: {
268
+ width,
269
+ height
270
+ }
271
+ };
272
+ }
273
+ const channels = 4;
274
+ const output = new Uint8ClampedArray(input.length);
275
+ const getIndex = (x, y, w) => (y * w + x) * channels;
276
+ let newWidth = width;
277
+ let newHeight = height;
278
+ switch (rotation) {
279
+ case Rotation.DEG_90:
280
+ case Rotation.DEG_270:
281
+ newWidth = height;
282
+ newHeight = width;
283
+ break;
284
+ case Rotation.DEG_180:
285
+ }
286
+ for (let y = 0; y < height; y += 1) {
287
+ for (let x = 0; x < width; x += 1) {
288
+ const srcIdx = getIndex(x, y, width);
289
+ let dstX = x;
290
+ let dstY = y;
291
+ switch (rotation) {
292
+ case Rotation.DEG_90:
293
+ dstX = height - y - 1;
294
+ dstY = x;
295
+ break;
296
+ case Rotation.DEG_180:
297
+ dstX = width - x - 1;
298
+ dstY = height - y - 1;
299
+ break;
300
+ case Rotation.DEG_270:
301
+ dstX = y;
302
+ dstY = width - x - 1;
303
+ break;
304
+ }
305
+ const dstIdx = getIndex(dstX, dstY, newWidth);
306
+ for (let i = 0; i < channels; i += 1) {
307
+ output[dstIdx + i] = input[srcIdx + i];
308
+ }
309
+ }
310
+ }
311
+ return {
312
+ data: output,
313
+ dimensions: {
314
+ width: newWidth,
315
+ height: newHeight
316
+ }
317
+ };
318
+ };
319
+
218
320
  const padLines = {
219
321
  [ExportFrameMode.FRAMEMODE_SQUARE_BLACK]: BLACK_LINE,
220
322
  [ExportFrameMode.FRAMEMODE_SQUARE_WHITE]: WHITE_LINE
@@ -321,40 +423,9 @@ const getFullParams$1 = (params) => ({
321
423
  imageStartLine: typeof params.imageStartLine === "number" ? params.imageStartLine : FRAME_WIDTH,
322
424
  tilesPerLine: params.tilesPerLine || TILES_PER_LINE,
323
425
  scaleFactor: params.scaleFactor || 1,
426
+ rotation: params.rotation || Rotation.DEG_0,
324
427
  handleExportFrame: params.handleExportFrame || ExportFrameMode.FRAMEMODE_KEEP
325
428
  });
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
429
  const getRawMonochromeImageData = (params) => {
359
430
  const {
360
431
  tiles,
@@ -363,7 +434,8 @@ const getRawMonochromeImageData = (params) => {
363
434
  framePalette,
364
435
  tilesPerLine,
365
436
  handleExportFrame,
366
- scaleFactor
437
+ scaleFactor,
438
+ rotation
367
439
  } = params;
368
440
  const imageContext = {
369
441
  tilesPerLine,
@@ -390,7 +462,14 @@ const getRawMonochromeImageData = (params) => {
390
462
  usedTiles.forEach((newTile, index) => {
391
463
  renderTile(rawImageData, newTile, index, updatedContext);
392
464
  });
393
- return scaleRawImageData(rawImageData, width, height, scaleFactor);
465
+ const {
466
+ data: rotatedData,
467
+ dimensions: {
468
+ width: rWidth,
469
+ height: rHeight
470
+ }
471
+ } = rotateImageData(rawImageData, width, height, rotation);
472
+ return scaleRawImageData(rotatedData, rWidth, rHeight, scaleFactor);
394
473
  };
395
474
  const getMonochromeImageUrl = async (params, canvasCreator = createCanvasElement) => {
396
475
  const urlCache = new UrlCache();
@@ -403,6 +482,11 @@ const getMonochromeImageUrl = async (params, canvasCreator = createCanvasElement
403
482
  const rawOutput = getRawMonochromeImageData(fullParams);
404
483
  return dataUrlFromRawOutput(rawOutput, fullParams.scaleFactor, hash$1, canvasCreator);
405
484
  };
485
+ const getMonochromeImageBlob = async (params, fileType, canvasCreator = createCanvasElement) => {
486
+ const fullParams = getFullParams$1(params);
487
+ const rawOutput = getRawMonochromeImageData(fullParams);
488
+ return blobFromRawOutput(rawOutput, fullParams.scaleFactor, canvasCreator, fileType);
489
+ };
406
490
 
407
491
  const getFullParams = (params) => ({
408
492
  tiles: params.tiles,
@@ -411,6 +495,7 @@ const getFullParams = (params) => ({
411
495
  imageStartLine: typeof params.imageStartLine === "number" ? params.imageStartLine : FRAME_WIDTH,
412
496
  tilesPerLine: params.tilesPerLine || TILES_PER_LINE,
413
497
  scaleFactor: params.scaleFactor || 1,
498
+ rotation: params.rotation || Rotation.DEG_0,
414
499
  handleExportFrame: params.handleExportFrame || ExportFrameMode.FRAMEMODE_KEEP
415
500
  });
416
501
  const singleChannelPalette = (palette, channelKey) => {
@@ -474,7 +559,8 @@ const getRawRGBNImageData = (params, canvasCreator) => {
474
559
  lockFrame,
475
560
  tilesPerLine,
476
561
  handleExportFrame,
477
- scaleFactor
562
+ scaleFactor,
563
+ rotation
478
564
  } = params;
479
565
  const canvases = Object.entries(tiles).reduce((acc, [key, channelTiles]) => {
480
566
  if (!channelTiles?.length) {
@@ -493,6 +579,7 @@ const getRawRGBNImageData = (params, canvasCreator) => {
493
579
  handleExportFrame,
494
580
  imageStartLine,
495
581
  scaleFactor,
582
+ rotation,
496
583
  tiles: channelTiles,
497
584
  tilesPerLine
498
585
  });
@@ -520,7 +607,12 @@ const getRGBNImageUrl = async (params, canvasCreator = createCanvasElement) => {
520
607
  const rawOutput = getRawRGBNImageData(fullParams, canvasCreator);
521
608
  return dataUrlFromRawOutput(rawOutput, fullParams.scaleFactor, hash$1, canvasCreator);
522
609
  };
610
+ const getRGBNImageBlob = async (params, fileType, canvasCreator = createCanvasElement) => {
611
+ const fullParams = getFullParams(params);
612
+ const rawOutput = getRawRGBNImageData(fullParams, canvasCreator);
613
+ return blobFromRawOutput(rawOutput, fullParams.scaleFactor, canvasCreator, fileType);
614
+ };
523
615
 
524
616
  const maxTiles = (rgbnTiles) => Math.max(...channels.map((key) => rgbnTiles[key]?.length || 0));
525
617
 
526
- 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, scaleRawImageData, tileIndexIsPartOfFrame };
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gb-image-decoder",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "Decoder classes for GameBoy-encoded images",
5
5
  "repository": "",
6
6
  "license": "MIT",