|
13 | 13 | * limitations under the License. |
14 | 14 | */ |
15 | 15 |
|
16 | | -import { BaseException, warn } from "../shared/util.js"; |
17 | | -import { fetchBinaryData } from "./core_utils.js"; |
| 16 | +import { BaseException, shadow } from "../shared/util.js"; |
18 | 17 | import JBig2 from "../../external/jbig2/jbig2.js"; |
| 18 | +import { WasmImage } from "./wasm_image.js"; |
19 | 19 |
|
20 | 20 | class Jbig2Error extends BaseException { |
21 | 21 | constructor(msg) { |
22 | 22 | super(msg, "Jbig2Error"); |
23 | 23 | } |
24 | 24 | } |
25 | 25 |
|
26 | | -class JBig2CCITTFaxImage { |
27 | | - static #buffer = null; |
| 26 | +class JBig2CCITTFaxImage extends WasmImage { |
| 27 | + _filename = "jbig2.wasm"; |
28 | 28 |
|
29 | | - static #handler = null; |
| 29 | + _noWasmFilename = "jbig2_nowasm_fallback.js"; |
30 | 30 |
|
31 | | - static #modulePromise = null; |
32 | | - |
33 | | - static #useWasm = true; |
34 | | - |
35 | | - static #useWorkerFetch = true; |
36 | | - |
37 | | - static #wasmUrl = null; |
38 | | - |
39 | | - static setOptions({ handler, useWasm, useWorkerFetch, wasmUrl }) { |
40 | | - this.#useWasm = useWasm; |
41 | | - this.#useWorkerFetch = useWorkerFetch; |
42 | | - this.#wasmUrl = wasmUrl; |
43 | | - |
44 | | - if (!useWorkerFetch) { |
45 | | - this.#handler = handler; |
46 | | - } |
| 31 | + static get instance() { |
| 32 | + return shadow(this, "instance", new JBig2CCITTFaxImage()); |
47 | 33 | } |
48 | 34 |
|
49 | | - static async #getJsModule(fallbackCallback) { |
50 | | - const path = |
51 | | - typeof PDFJSDev === "undefined" |
52 | | - ? `../${this.#wasmUrl}jbig2_nowasm_fallback.js` |
53 | | - : `${this.#wasmUrl}jbig2_nowasm_fallback.js`; |
54 | | - |
55 | | - let instance = null; |
56 | | - try { |
57 | | - const mod = await (typeof PDFJSDev === "undefined" |
58 | | - ? import(path) // eslint-disable-line no-unsanitized/method |
59 | | - : __raw_import__(path)); |
60 | | - instance = mod.default(); |
61 | | - } catch (e) { |
62 | | - warn(`JBig2CCITTFaxImage#getJsModule: ${e}`); |
63 | | - } |
64 | | - fallbackCallback(instance); |
65 | | - } |
66 | | - |
67 | | - static async #instantiateWasm(fallbackCallback, imports, successCallback) { |
68 | | - const filename = "jbig2.wasm"; |
69 | | - try { |
70 | | - if (!this.#buffer) { |
71 | | - if (this.#useWorkerFetch) { |
72 | | - this.#buffer = await fetchBinaryData(`${this.#wasmUrl}${filename}`); |
73 | | - } else { |
74 | | - if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { |
75 | | - throw new Error("Only worker-thread fetching supported."); |
76 | | - } |
77 | | - this.#buffer = await this.#handler.sendWithPromise( |
78 | | - "FetchBinaryData", |
79 | | - { kind: "wasmUrl", filename } |
80 | | - ); |
81 | | - } |
82 | | - } |
83 | | - const results = await WebAssembly.instantiate(this.#buffer, imports); |
84 | | - return successCallback(results.instance); |
85 | | - } catch (reason) { |
86 | | - warn(`JBig2CCITTFaxImage#instantiateWasm: ${reason}`); |
87 | | - |
88 | | - this.#getJsModule(fallbackCallback); |
89 | | - return null; |
90 | | - } finally { |
91 | | - this.#handler = null; |
92 | | - } |
93 | | - } |
94 | | - |
95 | | - static async decode(bytes, width, height, globals, CCITTOptions) { |
96 | | - if (!this.#modulePromise) { |
97 | | - const { promise, resolve } = Promise.withResolvers(); |
98 | | - const promises = [promise]; |
99 | | - if (!this.#useWasm) { |
100 | | - this.#getJsModule(resolve); |
101 | | - } else { |
102 | | - promises.push( |
103 | | - JBig2({ |
104 | | - warn, |
105 | | - instantiateWasm: this.#instantiateWasm.bind(this, resolve), |
106 | | - }) |
107 | | - ); |
108 | | - } |
109 | | - this.#modulePromise = Promise.race(promises); |
110 | | - } |
111 | | - const module = await this.#modulePromise; |
| 35 | + async decode(bytes, width, height, globals, CCITTOptions) { |
| 36 | + const module = await this._getModule(JBig2); |
112 | 37 |
|
113 | 38 | if (!module) { |
114 | 39 | throw new Jbig2Error("JBig2 failed to initialize"); |
@@ -157,10 +82,6 @@ class JBig2CCITTFaxImage { |
157 | 82 | } |
158 | 83 | } |
159 | 84 | } |
160 | | - |
161 | | - static cleanup() { |
162 | | - this.#modulePromise = null; |
163 | | - } |
164 | 85 | } |
165 | 86 |
|
166 | 87 | export { JBig2CCITTFaxImage, Jbig2Error }; |
0 commit comments