hdr-canvas 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hdr-canvas",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "HDR capable HTML canvas",
5
5
  "main": "dist/hdr-canvas.js",
6
6
  "files": [
@@ -46,7 +46,7 @@
46
46
  "@types/eslint__js": "^8.42.3",
47
47
  "eslint": "^9.6.0",
48
48
  "prettier": "^3.3.2",
49
- "rimraf": "^5.0.7",
49
+ "rimraf": "^6.0.0",
50
50
  "rollup": "^4.18.0",
51
51
  "rollup-plugin-dts": "^6.1.1",
52
52
  "three": "^0.166.1",
@@ -67,7 +67,7 @@ export class Uint16Image {
67
67
  this.data[pos + 3] = px[3];
68
68
  }
69
69
 
70
- // Only use this for aplha, since it doesn't to color space conversions
70
+ // Only use this for alpha, since it doesn't to color space conversions
71
71
  static scaleUint8ToUint16(val: number): number {
72
72
  return (val << 8) | val;
73
73
  }
@@ -125,6 +125,24 @@ export class Uint16Image {
125
125
  }
126
126
  }
127
127
 
128
+ static async loadSDRImageData(url: URL): Promise<HDRImageData | undefined> {
129
+ return fetch(url)
130
+ .then((response) => response.blob())
131
+ .then((blob: Blob) => {
132
+ return createImageBitmap(blob);
133
+ })
134
+ .then((bitmap: ImageBitmap) => {
135
+ const { width, height } = bitmap;
136
+ const offscreen = new OffscreenCanvas(width, height);
137
+ const ctx = offscreen.getContext("2d");
138
+ ctx?.drawImage(bitmap, 0, 0);
139
+ return ctx;
140
+ })
141
+ .then((ctx: OffscreenCanvasRenderingContext2D | null) => {
142
+ return ctx?.getImageData(0, 0, ctx?.canvas.width, ctx?.canvas.height);
143
+ });
144
+ }
145
+
128
146
  static fromImageData(imageData: HDRImageData): Uint16Image {
129
147
  const i = new Uint16Image(imageData.width, imageData.height);
130
148
  if (imageData.colorSpace == "srgb") {
@@ -139,6 +157,16 @@ export class Uint16Image {
139
157
  return i;
140
158
  }
141
159
 
160
+ static async fromURL(url: URL): Promise<Uint16Image | undefined> {
161
+ return Uint16Image.loadSDRImageData(url).then(
162
+ (data: HDRImageData | undefined) => {
163
+ if (data !== undefined) {
164
+ return Uint16Image.fromImageData(data);
165
+ }
166
+ },
167
+ );
168
+ }
169
+
142
170
  setImageData(imageData: HDRImageData): void {
143
171
  this.width = imageData.width;
144
172
  this.height = imageData.height;
@@ -153,4 +181,10 @@ export class Uint16Image {
153
181
  }
154
182
  this.colorSpace = Uint16Image.DEFAULT_COLORSPACE;
155
183
  }
184
+
185
+ clone(): Uint16Image {
186
+ const i = new Uint16Image(this.width, this.height, this.colorSpace);
187
+ i.data = this.data.slice();
188
+ return i;
189
+ }
156
190
  }