Introduce a fetchSync helper function for the IccColorSpace classes

This reduces code duplication a tiny bit, which shouldn't hurt.
This commit is contained in:
Jonas Jenwald 2025-03-17 14:58:53 +01:00
parent e37236e9af
commit 1fb6edc713

View File

@ -28,6 +28,17 @@ import { shadow, warn } from "../shared/util.js";
import { ColorSpace } from "./colorspace.js"; import { ColorSpace } from "./colorspace.js";
import { QCMS } from "../../external/qcms/qcms_utils.js"; import { QCMS } from "../../external/qcms/qcms_utils.js";
function fetchSync(url) {
// Parsing and using color spaces is still synchronous,
// so we must load the wasm module synchronously.
// TODO: Make the color space stuff asynchronous and use fetch.
const xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.responseType = "arraybuffer";
xhr.send(null);
return xhr.response;
}
class IccColorSpace extends ColorSpace { class IccColorSpace extends ColorSpace {
#transformer; #transformer;
@ -132,7 +143,9 @@ class IccColorSpace extends ColorSpace {
if (this.#useWasm) { if (this.#useWasm) {
if (this.#wasmUrl) { if (this.#wasmUrl) {
try { try {
this._module = QCMS._module = this.#load(); this._module = QCMS._module = initSync({
module: fetchSync(`${this.#wasmUrl}qcms_bg.wasm`),
});
isUsable = !!this._module; isUsable = !!this._module;
} catch (e) { } catch (e) {
warn(`ICCBased color space: "${e}".`); warn(`ICCBased color space: "${e}".`);
@ -144,30 +157,16 @@ class IccColorSpace extends ColorSpace {
return shadow(this, "isUsable", isUsable); return shadow(this, "isUsable", isUsable);
} }
static #load() {
// Parsing and using color spaces is still synchronous,
// so we must load the wasm module synchronously.
// TODO: Make the color space stuff asynchronous and use fetch.
const filename = "qcms_bg.wasm";
const xhr = new XMLHttpRequest();
xhr.open("GET", `${this.#wasmUrl}${filename}`, false);
xhr.responseType = "arraybuffer";
xhr.send(null);
return initSync({ module: xhr.response });
}
} }
class CmykICCBasedCS extends IccColorSpace { class CmykICCBasedCS extends IccColorSpace {
static #iccUrl; static #iccUrl;
constructor() { constructor() {
const filename = "CGATS001Compat-v2-micro.icc"; const iccProfile = new Uint8Array(
const xhr = new XMLHttpRequest(); fetchSync(`${CmykICCBasedCS.#iccUrl}CGATS001Compat-v2-micro.icc`)
xhr.open("GET", `${CmykICCBasedCS.#iccUrl}${filename}`, false); );
xhr.responseType = "arraybuffer"; super(iccProfile, "DeviceCMYK", 4);
xhr.send(null);
super(new Uint8Array(xhr.response), "DeviceCMYK", 4);
} }
static setOptions({ iccUrl }) { static setOptions({ iccUrl }) {