hdr-canvas 0.1.2 → 0.2.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 +32 -10
- package/dist/@types/hdr-canvas.d.ts +4 -20
- package/dist/hdr-canvas.js +93 -5949
- package/dist/hdr-canvas.js.map +1 -1
- package/dist/hdr-canvas.min.js +1 -1
- package/dist/hdr-canvas.min.js.map +1 -1
- package/dist/index.d.ts +4 -20
- package/docs/release-notes-0.2.0.md +26 -0
- package/package.json +26 -30
- package/rollup.config.mjs +6 -10
- package/scripts/check-import.sh +11 -0
- package/src/Float16Image.ts +16 -34
- package/src/HDRImage.ts +0 -10
- package/src/hdr-canvas.ts +8 -17
- package/src/hdr-check.ts +35 -18
- package/src/index.ts +1 -2
- package/three/HDRWebGPUBackend.js +11 -2
- package/tsconfig.json +4 -3
- package/dist/hdr-canvas.umd.js +0 -6411
- package/dist/hdr-canvas.umd.js.map +0 -1
- package/src/Uint16Image.ts +0 -192
- package/src/browser-util.ts +0 -9
package/src/Uint16Image.ts
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import { HDRImage } from "./HDRImage";
|
|
2
|
-
|
|
3
|
-
import Color from "colorjs.io";
|
|
4
|
-
import type { Coords } from "colorjs.io";
|
|
5
|
-
|
|
6
|
-
import type { HDRPredefinedColorSpace, HDRImageData, HDRImagePixelCallback } from "./types/HDRCanvas.d.ts";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Represents an image using a `Uint16Array` for its pixel data,
|
|
10
|
-
* providing support for high dynamic range (HDR) color spaces.
|
|
11
|
-
* **Don't use this anymore, it's just here for migrating to Float16Array!**
|
|
12
|
-
* @deprecated Use Uint16Image instead
|
|
13
|
-
*/
|
|
14
|
-
export class Uint16Image extends HDRImage {
|
|
15
|
-
/** The raw pixel data stored as a `Uint16Array`. */
|
|
16
|
-
data: Uint16Array;
|
|
17
|
-
|
|
18
|
-
/** The color space of the image. */
|
|
19
|
-
colorSpace: HDRPredefinedColorSpace;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Creates a new `Uint16Image` instance.
|
|
23
|
-
*
|
|
24
|
-
* @param {number} width - The width of the image in pixels.
|
|
25
|
-
* @param {number} height - The height of the image in pixels.
|
|
26
|
-
* @param {string} [colorspace] - The color space to use for the image. Defaults to `DEFAULT_COLORSPACE`.
|
|
27
|
-
*/
|
|
28
|
-
constructor(width: number, height: number, colorspace?: string) {
|
|
29
|
-
super(width, height);
|
|
30
|
-
if (colorspace === undefined || colorspace === null) {
|
|
31
|
-
this.colorSpace = HDRImage.DEFAULT_COLORSPACE;
|
|
32
|
-
} else {
|
|
33
|
-
this.colorSpace = colorspace as HDRPredefinedColorSpace;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
this.data = new Uint16Array(height * width * 4);
|
|
37
|
-
console.warn("Uint16Image isn't suported anymore, your browser will certainly drop support soon, use Float16Image instead.");
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Fills the entire image with a single color.
|
|
42
|
-
*
|
|
43
|
-
* @param {number[]} color - An array of four numbers representing the R, G, B, and A channels (0-65535).
|
|
44
|
-
* @returns {Uint16Image | undefined} The `Uint16Image` instance for method chaining, or `undefined` if the color array is invalid.
|
|
45
|
-
*/
|
|
46
|
-
fill(color: number[]): this | undefined {
|
|
47
|
-
// Was Uint16Image
|
|
48
|
-
if (color.length != 4) {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
for (let i = 0; i < this.data.length; i += 4) {
|
|
52
|
-
this.data[i] = color[0];
|
|
53
|
-
this.data[i + 1] = color[1];
|
|
54
|
-
this.data[i + 2] = color[2];
|
|
55
|
-
this.data[i + 3] = color[3];
|
|
56
|
-
}
|
|
57
|
-
return this;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// Only use this for alpha, since it doesn't to color space conversions
|
|
61
|
-
/**
|
|
62
|
-
* Scales an 8-bit value to a 16-bit value. This is typically used for the alpha channel.
|
|
63
|
-
*
|
|
64
|
-
* @param {number} val - The 8-bit value to scale (0-255).
|
|
65
|
-
* @returns {number} The corresponding 16-bit value.
|
|
66
|
-
*/
|
|
67
|
-
static scaleUint8ToUint16(val: number): number {
|
|
68
|
-
return (val << 8) | val;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Creates a standard `ImageData` object from the `Uint16Image` data.
|
|
73
|
-
*
|
|
74
|
-
* @returns {ImageData | null} An `ImageData` object, or `null` if the data is undefined.
|
|
75
|
-
*/
|
|
76
|
-
getImageData(): ImageData | null {
|
|
77
|
-
if (this.data === undefined || this.data === null) {
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
81
|
-
return new ImageData(this.data as any, this.width, this.height, {
|
|
82
|
-
colorSpace: this.colorSpace as PredefinedColorSpace
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Converts a single 8-bit pixel (from sRGB color space) to a 16-bit pixel
|
|
88
|
-
* in the `rec2100-hlg` color space.
|
|
89
|
-
*
|
|
90
|
-
* @param {Uint8ClampedArray} pixel - An array of four 8-bit numbers (R, G, B, A).
|
|
91
|
-
* @returns {Uint16Array} The converted 16-bit pixel in the `rec2100-hlg` color space.
|
|
92
|
-
*/
|
|
93
|
-
static convertPixelToRec2100_hlg(pixel: Uint8ClampedArray): Uint16Array {
|
|
94
|
-
const colorJScolorSpace = <string>Uint16Image.COLORSPACES["rec2100-hlg" as HDRPredefinedColorSpace];
|
|
95
|
-
|
|
96
|
-
const srgbColor = new Color(
|
|
97
|
-
"srgb",
|
|
98
|
-
Array.from(pixel.slice(0, 3)).map((band: number) => {
|
|
99
|
-
return band / 255;
|
|
100
|
-
}) as Coords,
|
|
101
|
-
pixel[3] / 255
|
|
102
|
-
);
|
|
103
|
-
const rec2100hlgColor = srgbColor.to(colorJScolorSpace);
|
|
104
|
-
const hlg: Array<number> = rec2100hlgColor.coords.map((band: number) => {
|
|
105
|
-
return Math.round(band * Uint16Image.SDR_MULTIPLIER);
|
|
106
|
-
});
|
|
107
|
-
// Readd alpha
|
|
108
|
-
hlg.push(rec2100hlgColor.alpha * Uint16Image.SDR_MULTIPLIER);
|
|
109
|
-
|
|
110
|
-
return Uint16Array.from(hlg);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Converts a `Uint8ClampedArray` of sRGB pixel data to a `Uint16Array`
|
|
115
|
-
* of pixels in the `rec2100-hlg` color space.
|
|
116
|
-
*
|
|
117
|
-
* @param {Uint8ClampedArray} data - The array of 8-bit pixel data.
|
|
118
|
-
* @returns {Uint16Array} The converted 16-bit pixel data.
|
|
119
|
-
*/
|
|
120
|
-
static convertArrayToRec2100_hlg(data: Uint8ClampedArray): Uint16Array {
|
|
121
|
-
const uint16Data = new Uint16Array(data.length);
|
|
122
|
-
for (let i = 0; i < data.length; i += 4) {
|
|
123
|
-
const rgbPixel: Uint8ClampedArray = data.slice(i, i + 4);
|
|
124
|
-
const pixel = Uint16Image.convertPixelToRec2100_hlg(rgbPixel);
|
|
125
|
-
uint16Data.set(pixel, i);
|
|
126
|
-
}
|
|
127
|
-
return uint16Data;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Iterates through each pixel of the image and applies a callback function to its data.
|
|
132
|
-
*
|
|
133
|
-
* @param {HDRImagePixelCallback} fn - The callback function to apply to each pixel.
|
|
134
|
-
*/
|
|
135
|
-
pixelCallback(fn: HDRImagePixelCallback) {
|
|
136
|
-
for (let i = 0; i < this.data.length; i += 4) {
|
|
137
|
-
this.data.set(fn(this.data[i], this.data[i + 1], this.data[i + 2], this.data[i + 3]), i);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Creates a `Uint16Image` instance from an `HDRImageData` object.
|
|
143
|
-
*
|
|
144
|
-
* @param {HDRImageData} imageData - The image data to use.
|
|
145
|
-
* @returns {Uint16Image} The new `Uint16Image` instance.
|
|
146
|
-
* @throws {Error} If the color space of the `HDRImageData` is not supported.
|
|
147
|
-
*/
|
|
148
|
-
static fromImageData(imageData: HDRImageData): Uint16Image {
|
|
149
|
-
const i = new Uint16Image(imageData.width, imageData.height);
|
|
150
|
-
if (imageData.colorSpace == "srgb") {
|
|
151
|
-
i.data = Uint16Image.convertArrayToRec2100_hlg(<Uint8ClampedArray>imageData.data);
|
|
152
|
-
} else if (imageData.colorSpace == HDRImage.DEFAULT_COLORSPACE) {
|
|
153
|
-
i.data = <Uint16Array>imageData.data;
|
|
154
|
-
} else {
|
|
155
|
-
throw new Error(`ColorSpace ${imageData.colorSpace} isn't supported!`);
|
|
156
|
-
}
|
|
157
|
-
return i;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Loads an image from a URL and creates a `Uint16Image` instance from it.
|
|
162
|
-
*
|
|
163
|
-
* @param {URL} url - The URL of the image to load.
|
|
164
|
-
* @returns {Promise<Uint16Image | undefined>} A promise that resolves with a `Uint16Image` instance, or `undefined` if the image could not be loaded.
|
|
165
|
-
*/
|
|
166
|
-
static async fromURL(url: URL): Promise<Uint16Image | undefined> {
|
|
167
|
-
return Uint16Image.loadSDRImageData(url).then((data: HDRImageData | undefined) => {
|
|
168
|
-
if (data !== undefined) {
|
|
169
|
-
return Uint16Image.fromImageData(data);
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Sets the image data of the current `Uint16Image` instance.
|
|
176
|
-
*
|
|
177
|
-
* @param {HDRImageData} imageData - The image data to set.
|
|
178
|
-
* @throws {Error} If the color space of the `HDRImageData` is not supported.
|
|
179
|
-
*/
|
|
180
|
-
setImageData(imageData: HDRImageData): void {
|
|
181
|
-
this.width = imageData.width;
|
|
182
|
-
this.height = imageData.height;
|
|
183
|
-
if (imageData.colorSpace == "srgb") {
|
|
184
|
-
this.data = Uint16Image.convertArrayToRec2100_hlg(<Uint8ClampedArray>imageData.data);
|
|
185
|
-
} else if (imageData.colorSpace == HDRImage.DEFAULT_COLORSPACE) {
|
|
186
|
-
this.data = <Uint16Array>imageData.data;
|
|
187
|
-
} else {
|
|
188
|
-
throw new Error(`ColorSpace ${imageData.colorSpace} isn't supported!`);
|
|
189
|
-
}
|
|
190
|
-
this.colorSpace = HDRImage.DEFAULT_COLORSPACE;
|
|
191
|
-
}
|
|
192
|
-
}
|
package/src/browser-util.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export function getBrowserVersion(): number | null {
|
|
2
|
-
const majorVersionStr = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
|
|
3
|
-
if (majorVersionStr == null) {
|
|
4
|
-
console.warn(`Unsupported / untested browser (${navigator.userAgent}) detected - using more modern defaults`);
|
|
5
|
-
} else if (majorVersionStr.length >= 3) {
|
|
6
|
-
return Number(majorVersionStr[2]);
|
|
7
|
-
}
|
|
8
|
-
return null;
|
|
9
|
-
}
|