gb-image-decoder 0.0.3 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) Andreas Hahn
3
+ Copyright (c) 2024 Andreas Hahn
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,2 +1,82 @@
1
1
  # GameBoy-Tile format Image Decoders
2
2
 
3
+ This package renders GameBoy-encoded images to a canvas element.
4
+
5
+ You may need the [gbp-decode package](https://npmjs.com/gbp-decode) to decode various sources.
6
+ Or you may want to use the [arduino-gameboy-printer-emulator](https://github.com/mofosyne/arduino-gameboy-printer-emulator).
7
+ There are a lot more similar projects for aquiring raw tile data.
8
+
9
+ ## Usage
10
+ Import within your Project
11
+ ```typescript
12
+ import { RGBNDecoder, Decoder, RGBNTiles } from 'gb-image-decoder';
13
+ ```
14
+
15
+ Have a canvas element inside your DOM
16
+ ```typescript
17
+ const myCanvas: HTMLCanvasElement = document.createElement('canvas');
18
+ ```
19
+
20
+ ### For monochrome images:
21
+
22
+ Have a set of tiles (each string represents a 8x8 tile)
23
+ ```typescript
24
+ // For classic monochrome images:
25
+ const myTiles: string[] = [ // need 360 of these for a 160x144 image
26
+ '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
+ ];
28
+ ```
29
+
30
+ Have a palette for your monochrome image
31
+ ```typescript
32
+ const monoPalette = ['#ffffff', '#aaaaaa', '#555555', '#000000'];
33
+ ```
34
+
35
+ Initialize the decoder:
36
+ ```typescript
37
+ const decoder = new Decoder();
38
+ decoder.update({
39
+ canvas: myCanvas,
40
+ tiles: myTiles,
41
+ palette: monoPalette,
42
+ lockFrame: false,
43
+ invertPalette: false,
44
+ });
45
+ ```
46
+
47
+ ### For RGBN images:
48
+ RGB(N) Images can contain any of the channels. All channels are optional, but the r,g,b channels are recommended.
49
+
50
+ Your image needs a set of RGBN tiles:
51
+ ```typescript
52
+ // For RGBN images
53
+ const myTilesRGBN: RGBNTiles = { // need 360 of each for a 160x144 image
54
+ r: ['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'],
55
+ g: ['7F FF BF FF FF FF FF FF DF FF FF FF FF FF FF FF', 'A2 D5 FC C3 4C D3 FE C1 D8 E4 ED F2 FF F1 DB FC', 'EA 55 83 FC 48 F5 07 F8 A4 53 A8 07 A3 5C FE 01', 'FF 3F FF 3F FF 3F 7F BF FF 3F FF 7F 7D FF FF FF'],
56
+ b: ['F7 FF A3 FF FF FF FF FF FF FF FF FF FF FF FF FF', '0A F5 80 FF 40 FF F1 FE FB FD F0 FF 52 FD E1 FE', '88 77 40 BF 2A D5 18 EF 88 77 04 FB AA 55 17 E8', 'A0 5F 51 AE AA 55 54 AB AA 55 05 FA AA 55 67 98'],
57
+ // neutral image is optional
58
+ };
59
+ ```
60
+
61
+ For RGBN there is a default blend palette available:
62
+ ```typescript
63
+ import { defaultPalette as rgbnPalette } from 'gb-image-decoder';
64
+ ```
65
+
66
+ Initialize the decoder:
67
+ ```typescript
68
+ const decoder = new RGBNDecoder();
69
+ decoder.update({
70
+ canvas: myCanvas,
71
+ tiles: myTilesRGBN,
72
+ palette: rgbnPalette,
73
+ lockFrame: false,
74
+ });
75
+ ```
76
+
77
+ ## ToDos
78
+ * improve docs
79
+ * tests
80
+
81
+ ## License: MIT
82
+ [LICENSE](./LICENSE)
package/dist/index.cjs CHANGED
@@ -572,14 +572,6 @@ class RGBNDecoder {
572
572
  }
573
573
  getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
574
574
  const canvas = document.createElement("canvas");
575
- const handleFrameMode = this.maxTiles() === 360 ? handleExportFrame : ExportFrameMode.FRAMEMODE_KEEP;
576
- const { initialHeight, initialWidth } = Decoder.getScaledCanvasSize(handleFrameMode, this.getHeight());
577
- canvas.width = initialWidth * scaleFactor;
578
- canvas.height = initialHeight * scaleFactor;
579
- const context = canvas.getContext("2d");
580
- if (!context) {
581
- throw new Error("no canvas context");
582
- }
583
575
  const canvases = channels.reduce((acc, key) => {
584
576
  const channel = this.channels[key];
585
577
  const channelCanvas = channel.decoder.getScaledCanvas(scaleFactor, handleExportFrame);
@@ -588,6 +580,16 @@ class RGBNDecoder {
588
580
  [key]: channelCanvas
589
581
  };
590
582
  }, {});
583
+ const { width, height } = Object.values(canvases).reduce((acc, current) => ({
584
+ width: Math.max(current.width, acc.width),
585
+ height: Math.max(current.width, acc.height)
586
+ }), { width: 0, height: 0 });
587
+ canvas.width = width;
588
+ canvas.height = height;
589
+ const context = canvas.getContext("2d");
590
+ if (!context) {
591
+ throw new Error("no canvas context");
592
+ }
591
593
  this.blendCanvases(context, canvases);
592
594
  return canvas;
593
595
  }
package/dist/index.mjs CHANGED
@@ -570,14 +570,6 @@ class RGBNDecoder {
570
570
  }
571
571
  getScaledCanvas(scaleFactor, handleExportFrame = ExportFrameMode.FRAMEMODE_KEEP) {
572
572
  const canvas = document.createElement("canvas");
573
- const handleFrameMode = this.maxTiles() === 360 ? handleExportFrame : ExportFrameMode.FRAMEMODE_KEEP;
574
- const { initialHeight, initialWidth } = Decoder.getScaledCanvasSize(handleFrameMode, this.getHeight());
575
- canvas.width = initialWidth * scaleFactor;
576
- canvas.height = initialHeight * scaleFactor;
577
- const context = canvas.getContext("2d");
578
- if (!context) {
579
- throw new Error("no canvas context");
580
- }
581
573
  const canvases = channels.reduce((acc, key) => {
582
574
  const channel = this.channels[key];
583
575
  const channelCanvas = channel.decoder.getScaledCanvas(scaleFactor, handleExportFrame);
@@ -586,6 +578,16 @@ class RGBNDecoder {
586
578
  [key]: channelCanvas
587
579
  };
588
580
  }, {});
581
+ const { width, height } = Object.values(canvases).reduce((acc, current) => ({
582
+ width: Math.max(current.width, acc.width),
583
+ height: Math.max(current.width, acc.height)
584
+ }), { width: 0, height: 0 });
585
+ canvas.width = width;
586
+ canvas.height = height;
587
+ const context = canvas.getContext("2d");
588
+ if (!context) {
589
+ throw new Error("no canvas context");
590
+ }
589
591
  this.blendCanvases(context, canvases);
590
592
  return canvas;
591
593
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gb-image-decoder",
3
- "version": "0.0.3",
4
- "description": "Decoder classes for GameBoy images",
3
+ "version": "0.1.1",
4
+ "description": "Decoder classes for GameBoy-encoded images",
5
5
  "repository": "",
6
6
  "license": "MIT",
7
7
  "type": "module",