open-ultrahdr 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/dist/index.mjs CHANGED
@@ -38,65 +38,61 @@ var smallSizeEncodeOptions = {
38
38
  };
39
39
 
40
40
  // src/index.ts
41
+ var WASM_FILENAME = "open_ultrahdr.wasm";
41
42
  var location = "";
42
- var wasmUrl = "";
43
+ var explicitWasmUrl = null;
43
44
  var wasmInstance = null;
44
45
  var initPromise = null;
46
+ var initGeneration = 0;
45
47
  function setLocation(newLocation) {
46
48
  location = newLocation;
49
+ explicitWasmUrl = null;
50
+ resetCache();
47
51
  }
48
52
  function setWasmUrl(url) {
49
- wasmUrl = url;
53
+ explicitWasmUrl = url;
54
+ resetCache();
50
55
  }
51
- function isWasmMetadataInstance(metadata) {
52
- return typeof metadata === "object" && metadata !== null && "__wbg_ptr" in metadata;
56
+ function resetCache() {
57
+ initGeneration += 1;
58
+ wasmInstance = null;
59
+ initPromise = null;
53
60
  }
54
- async function toWasmMetadata(metadata) {
55
- if (isWasmMetadataInstance(metadata)) {
56
- return metadata;
57
- }
58
- const wasm = await getWasm();
59
- const wasmMetadata = wasm.createDefaultMetadata();
60
- wasmMetadata.version = metadata.version;
61
- wasmMetadata.baseRenditionIsHdr = metadata.baseRenditionIsHdr;
62
- wasmMetadata.gainMapMin = metadata.gainMapMin;
63
- wasmMetadata.gainMapMax = metadata.gainMapMax;
64
- wasmMetadata.gamma = metadata.gamma;
65
- wasmMetadata.offsetSdr = metadata.offsetSdr;
66
- wasmMetadata.offsetHdr = metadata.offsetHdr;
67
- wasmMetadata.hdrCapacityMin = metadata.hdrCapacityMin;
68
- wasmMetadata.hdrCapacityMax = metadata.hdrCapacityMax;
69
- return wasmMetadata;
61
+ function joinPath(base, name) {
62
+ if (!base) return name;
63
+ return base.endsWith("/") ? `${base}${name}` : `${base}/${name}`;
70
64
  }
71
65
  async function getWasm() {
72
- if (wasmInstance) {
73
- return wasmInstance;
74
- }
75
- if (initPromise) {
76
- return initPromise;
77
- }
66
+ if (wasmInstance) return wasmInstance;
67
+ if (initPromise) return initPromise;
68
+ const generation = initGeneration;
78
69
  initPromise = (async () => {
79
70
  try {
80
- const UltraHdrWasm = await import("open-ultrahdr-wasm");
81
- if (wasmUrl) {
82
- await UltraHdrWasm.default(wasmUrl);
71
+ const wasmModule = await import("open-ultrahdr-wasm");
72
+ const factory = wasmModule.default;
73
+ const isNode = typeof process !== "undefined" && !!process.versions && !!process.versions.node;
74
+ const moduleOptions = {};
75
+ if (explicitWasmUrl) {
76
+ moduleOptions.locateFile = (path) => path.endsWith(".wasm") ? explicitWasmUrl : path;
83
77
  } else if (location) {
84
- const base = location.endsWith("/") ? location : `${location}/`;
85
- const wasmPath = base + "open_ultrahdr_bg.wasm";
86
- if (typeof process !== "undefined" && process.versions && process.versions.node) {
78
+ if (isNode) {
87
79
  const fs = await import("fs");
88
- const wasmBytes = await fs.promises.readFile(wasmPath);
89
- await UltraHdrWasm.default(wasmBytes);
80
+ const wasmBytes = await fs.promises.readFile(joinPath(location, WASM_FILENAME));
81
+ moduleOptions.wasmBinary = new Uint8Array(wasmBytes);
90
82
  } else {
91
- await UltraHdrWasm.default(wasmPath);
83
+ moduleOptions.locateFile = (path) => path.endsWith(".wasm") ? joinPath(location, WASM_FILENAME) : path;
92
84
  }
93
- } else {
94
- await UltraHdrWasm.default();
95
85
  }
96
- wasmInstance = UltraHdrWasm;
97
- return wasmInstance;
86
+ const instance = await factory(moduleOptions);
87
+ if (generation !== initGeneration) {
88
+ return getWasm();
89
+ }
90
+ wasmInstance = instance;
91
+ return instance;
98
92
  } catch (err) {
99
- initPromise = null;
93
+ if (generation === initGeneration) {
94
+ initPromise = null;
95
+ }
100
96
  throw err;
101
97
  }
102
98
  })();
@@ -107,27 +103,35 @@ async function isUltraHdr(buffer) {
107
103
  return wasm.isUltraHdr(new Uint8Array(buffer));
108
104
  }
109
105
  async function probeUltraHdr(buffer) {
110
- const wasm = await getWasm();
111
- return wasm.probeUltraHdr(new Uint8Array(buffer));
106
+ try {
107
+ const wasm = await getWasm();
108
+ return wasm.probeUltraHdr(new Uint8Array(buffer));
109
+ } catch {
110
+ return {
111
+ isValid: false,
112
+ hasPrimaryImage: false,
113
+ hasGainMap: false,
114
+ hasMetadata: false,
115
+ width: 0,
116
+ height: 0,
117
+ gainMapWidth: 0,
118
+ gainMapHeight: 0,
119
+ hdrCapacity: 0,
120
+ metadataVersion: ""
121
+ };
122
+ }
112
123
  }
113
- async function decodeUltraHdr(id, buffer) {
124
+ async function decodeUltraHdr(_id, buffer) {
114
125
  const wasm = await getWasm();
115
126
  return wasm.decodeUltraHdr(new Uint8Array(buffer));
116
127
  }
117
- async function encodeUltraHdr(id, sdrBuffer, hdrBuffer, options) {
128
+ async function encodeUltraHdr(_id, sdrBuffer, hdrBuffer, options) {
118
129
  const wasm = await getWasm();
119
- const wasmOpts = wasm.createDefaultOptions();
120
- const mergedOpts = { ...defaultEncodeOptions, ...options };
121
- wasmOpts.baseQuality = mergedOpts.baseQuality;
122
- wasmOpts.gainMapQuality = mergedOpts.gainMapQuality;
123
- wasmOpts.targetHdrCapacity = mergedOpts.targetHdrCapacity;
124
- wasmOpts.includeIsoMetadata = mergedOpts.includeIsoMetadata;
125
- wasmOpts.includeUltrahdrV1 = mergedOpts.includeUltrahdrV1;
126
- wasmOpts.gainMapScale = mergedOpts.gainMapScale;
130
+ const merged = { ...defaultEncodeOptions, ...options };
127
131
  const result = wasm.encodeUltraHdr(
128
132
  new Uint8Array(sdrBuffer),
129
133
  new Float32Array(hdrBuffer),
130
- wasmOpts
134
+ merged
131
135
  );
132
136
  return result.buffer.slice(
133
137
  result.byteOffset,
@@ -148,18 +152,15 @@ async function getMetadata(buffer) {
148
152
  }
149
153
  async function validateMetadata(metadata) {
150
154
  const wasm = await getWasm();
151
- const wasmMetadata = await toWasmMetadata(metadata);
152
- return wasm.validateMetadata(wasmMetadata);
155
+ return wasm.validateMetadata(metadata);
153
156
  }
154
157
  async function estimateHdrHeadroom(metadata) {
155
158
  const wasm = await getWasm();
156
- const wasmMetadata = await toWasmMetadata(metadata);
157
- return wasm.estimateHdrHeadroom(wasmMetadata);
159
+ return wasm.estimateHdrHeadroom(metadata);
158
160
  }
159
161
  async function isMeaningfulHdr(metadata) {
160
162
  const wasm = await getWasm();
161
- const wasmMetadata = await toWasmMetadata(metadata);
162
- return wasm.isMeaningfulHdr(wasmMetadata);
163
+ return wasm.isMeaningfulHdr(metadata);
163
164
  }
164
165
  export {
165
166
  ColorGamut,
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "open-ultrahdr",
3
- "version": "0.1.2",
4
- "description": "UltraHDR (ISO 21496-1) gain map support for JavaScript/TypeScript",
3
+ "version": "0.2.0",
4
+ "description": "UltraHDR (ISO 21496-1) gain map support for JavaScript/TypeScript, backed by upstream libultrahdr",
5
5
  "author": "Adam Silverstein",
6
- "license": "GPL-2.0-or-later",
6
+ "license": "Apache-2.0 OR MIT",
7
7
  "keywords": [
8
8
  "ultrahdr",
9
9
  "hdr",
@@ -52,7 +52,7 @@
52
52
  "lint:fix": "eslint src __tests__ --fix"
53
53
  },
54
54
  "dependencies": {
55
- "open-ultrahdr-wasm": "^0.1.2"
55
+ "open-ultrahdr-wasm": "^0.2.0"
56
56
  },
57
57
  "devDependencies": {
58
58
  "@eslint/js": "^9.0.0",