hdr-canvas 0.1.1 → 0.1.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.
@@ -0,0 +1,16 @@
1
+ # `hdr-canvas` 0.1.2
2
+
3
+ # Introduction
4
+
5
+ The color scaling of `Float16Image` was off.
6
+
7
+ This is certainly the last release before the removal:
8
+
9
+ - of the ThreeJS components, they aren't needed anymore.
10
+ - of the Uint16Image it's outdated and only still works since Chrom(e|ium) due to backwards compatibility.
11
+ - the support for Chrom(e|ium) < 140
12
+ - of the bundle UMD build since ESM is the way to go.
13
+
14
+ # Key Changes & New Features
15
+
16
+ - Fix `Float16Image` color scaling
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hdr-canvas",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "HDR capable HTML canvas",
5
5
  "main": "dist/hdr-canvas.cjs",
6
6
  "module": "dist/hdr-canvas.js",
@@ -74,10 +74,10 @@
74
74
  "@eslint/markdown": "^7.5.1",
75
75
  "@fontsource-utils/scss": "^0.2.2",
76
76
  "@fontsource-variable/league-spartan": "^5.2.8",
77
+ "@napi-rs/canvas": "^0.1.83",
77
78
  "@rollup/plugin-node-resolve": "^16.0.3",
78
79
  "@rollup/plugin-terser": "^0.4.4",
79
80
  "@rollup/plugin-typescript": "^12.3.0",
80
- "@napi-rs/canvas": "^0.1.83",
81
81
  "@types/jsdom": "^27.0.0",
82
82
  "@types/semver": "^7.7.1",
83
83
  "@types/three": "^0.181.0",
@@ -107,6 +107,7 @@
107
107
  "vitest": "^4.0.14"
108
108
  },
109
109
  "dependencies": {
110
+ "@petamoriken/float16": "^3.9.3",
110
111
  "colorjs.io": "^0.5.2"
111
112
  }
112
113
  }
package/scripts/check.sh CHANGED
@@ -10,3 +10,4 @@ npx rollup --config
10
10
  npx prettier --check .
11
11
  npm run build
12
12
  npm run site
13
+ npm publish --dry-run
@@ -2,6 +2,7 @@ import { HDRImage } from "./HDRImage";
2
2
 
3
3
  import Color from "colorjs.io";
4
4
  import type { Coords } from "colorjs.io";
5
+ import { f16round } from "@petamoriken/float16";
5
6
 
6
7
  import type { HDRPredefinedColorSpace, HDRImageData, HDRImagePixelCallback } from "./types/HDRCanvas.d.ts";
7
8
 
@@ -97,6 +98,7 @@ export class Float16Image extends HDRImage {
97
98
  *
98
99
  * @param {Uint8ClampedArray} pixel - An array of four 8-bit numbers (R, G, B, A).
99
100
  * @returns {Float16Array} The converted 16-bit pixel in the `rec2100-hlg` color space.
101
+ * @deprecated
100
102
  */
101
103
  static convertPixelToRec2100_hlg(pixel: Uint8ClampedArray): Float16Array {
102
104
  const colorJScolorSpace = <string>Float16Image.COLORSPACES["rec2100-hlg" as HDRPredefinedColorSpace];
@@ -126,13 +128,14 @@ export class Float16Image extends HDRImage {
126
128
  * @returns {Float16Array} The converted 16-bit pixel data.
127
129
  */
128
130
  static convertArrayToRec2100_hlg(data: Uint8ClampedArray): Float16Array {
129
- const uint16Data = new Float16Array(data.length);
130
- for (let i = 0; i < data.length; i += 4) {
131
- const rgbPixel: Uint8ClampedArray = data.slice(i, i + 4);
132
- const pixel = Float16Image.convertPixelToRec2100_hlg(rgbPixel);
133
- uint16Data.set(pixel, i);
131
+ const float16Array = new Float16Array(data.length);
132
+
133
+ for (let i = 0; i < data.length; i++) {
134
+ const normalizedFloat = data[i] / 255.0;
135
+ float16Array[i] = f16round(normalizedFloat);
134
136
  }
135
- return uint16Data;
137
+
138
+ return float16Array;
136
139
  }
137
140
 
138
141
  /**
@@ -142,7 +145,17 @@ export class Float16Image extends HDRImage {
142
145
  */
143
146
  pixelCallback(fn: HDRImagePixelCallback) {
144
147
  for (let i = 0; i < this.data.length; i += 4) {
145
- this.data.set(fn(this.data[i], this.data[i + 1], this.data[i + 2], this.data[i + 3]), i);
148
+ const pixel = fn(this.data[i], this.data[i + 1], this.data[i + 2], this.data[i + 3]);
149
+
150
+ //TODO: Check if we should implicitly should operate on int pixel vaulues
151
+ /*
152
+ for (let i = 0; i < pixel.length; i++) {
153
+ const normalizedFloat = pixel[i] / 255.0;
154
+ pixel[i] = f16round(normalizedFloat);
155
+ }
156
+ */
157
+
158
+ this.data.set(pixel, i);
146
159
  }
147
160
  }
148
161
 
@@ -9,6 +9,7 @@ import type { HDRPredefinedColorSpace, HDRImageData, HDRImagePixelCallback } fro
9
9
  * Represents an image using a `Uint16Array` for its pixel data,
10
10
  * providing support for high dynamic range (HDR) color spaces.
11
11
  * **Don't use this anymore, it's just here for migrating to Float16Array!**
12
+ * @deprecated Use Uint16Image instead
12
13
  */
13
14
  export class Uint16Image extends HDRImage {
14
15
  /** The raw pixel data stored as a `Uint16Array`. */
package/src/hdr-canvas.ts CHANGED
@@ -15,6 +15,7 @@ export function getHdrOptions(): CanvasRenderingContext2DHDRSettings {
15
15
  if (browserMajorVersion == null) {
16
16
  console.warn(`Unsupported / untested browser (${navigator.userAgent}) detected - using more modern defaults`);
17
17
  hdrOptions["colorType"] = "float16";
18
+ hdrOptions["toneMapping"] = { mode: "extended" };
18
19
  } else {
19
20
  if (browserMajorVersion < 134) {
20
21
  console.warn("Older Chrome / chromium based browser detected, using older `pixelFormat`");
@@ -22,7 +23,6 @@ export function getHdrOptions(): CanvasRenderingContext2DHDRSettings {
22
23
  hdrOptions["pixelFormat"] = "float16";
23
24
  }
24
25
  }
25
-
26
26
  return hdrOptions;
27
27
  }
28
28
 
@@ -34,7 +34,10 @@ export function getHdrOptions(): CanvasRenderingContext2DHDRSettings {
34
34
  * @returns {CanvasRenderingContext2DHDR | null} The 2D rendering context, or `null` if the context cannot be created. Can be cast down to `CanvasRenderingContext2D` or `RenderingContext`
35
35
  */
36
36
  export function initHDRCanvas(canvas: HDRHTMLCanvasElement): CanvasRenderingContext2DHDR | null {
37
- canvas.configureHighDynamicRange({ mode: "extended" });
37
+ const browserMajorVersion = getBrowserVersion();
38
+ if (browserMajorVersion !== null && browserMajorVersion < 134) {
39
+ canvas.configureHighDynamicRange({ mode: "extended" });
40
+ }
38
41
  const ctx = canvas.getContext("2d", getHdrOptions());
39
42
  return ctx as CanvasRenderingContext2DHDR;
40
43
  }
@@ -21,6 +21,8 @@ interface HDRImageData {
21
21
  // See https://github.com/w3c/ColorWeb-CG/blob/main/hdr_html_canvas_element.md
22
22
  // "rec2100-display-linear" is left out because of mapping issues
23
23
  type HDRPredefinedColorSpace = "display-p3" | "srgb" | "rec2100-hlg" | "rec2100-pq";
24
+ type HDRCanvasToneMapping = { mode: HDRCanvasToneMappingMode };
25
+ type HDRCanvasToneMappingMode = "standard" | "extended";
24
26
 
25
27
  //enum HDRPredefinedColorSpace {"display-p3", "srgb", "rec2100-hlg", "rec2100-pq", "rec2100-display-linear"};
26
28
 
@@ -43,11 +45,14 @@ interface CanvasRenderingContext2DHDRSettings {
43
45
  colorSpace: HDRPredefinedColorSpace;
44
46
  pixelFormat?: "uint8" | "float16";
45
47
  colorType?: "unorm8" | "float16";
48
+ toneMapping?: HDRCanvasToneMapping;
46
49
  }
47
50
 
48
51
  export {
49
52
  HDRHTMLCanvasElement,
50
53
  HDRPredefinedColorSpace,
54
+ HDRCanvasToneMapping,
55
+ HDRCanvasToneMappingMode,
51
56
  HDRImageDataArray,
52
57
  HDRImageData,
53
58
  HDRImagePixelCallback,
@@ -9,6 +9,7 @@ import { REVISION, HalfFloatType } from 'three/src/constants.js';
9
9
  *
10
10
  * @class
11
11
  * @augments {WebGPUBackend}
12
+ * @deprecated Use WebGPUBackend instead
12
13
  */
13
14
 
14
15
  class HDRWebGPUBackend extends WebGPUBackend {
@@ -12,6 +12,7 @@ import HDRWebGPUBackend from './HDRWebGPUBackend.js';
12
12
  * @fires contextrestored
13
13
  * @fires contextlost
14
14
  * @see {@link https://threejs.org/docs/#api/en/renderers/WebGLRenderer | WebGLRenderer}
15
+ * @deprecated Use WebGPURenderer instead
15
16
  */
16
17
  class HDRWebGPURenderer extends Renderer {
17
18
  /**