From 8970786d5bc848ff0e150e6c6bb54d1eb8a601d2 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 20 Apr 2024 09:32:36 +0200 Subject: [PATCH] Update `JpxImage.parseImageProperties` to support TypedArray data in IMAGE_DECODERS builds Given that the `decode` method only returns the actual image-data, a user would now need to invoke `parseImageProperties` to obtain e.g. the width and height. This method only accepts `BaseStream`-instances, which are (obviously) not exposed, hence we extend it in IMAGE_DECODERS builds to wrap TypedArray data into the expected format. --- src/core/jpx.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/core/jpx.js b/src/core/jpx.js index 580b9a6a5a705..c7dc42d2ecfa0 100644 --- a/src/core/jpx.js +++ b/src/core/jpx.js @@ -15,6 +15,7 @@ import { BaseException } from "../shared/util.js"; import OpenJPEG from "../../external/openjpeg/openjpeg.js"; +import { Stream } from "./stream.js"; class JpxError extends BaseException { constructor(msg) { @@ -39,6 +40,13 @@ class JpxImage { } static parseImageProperties(stream) { + if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("IMAGE_DECODERS")) { + if (stream instanceof ArrayBuffer || ArrayBuffer.isView(stream)) { + stream = new Stream(stream); + } else { + throw new JpxError("Invalid data format, must be a TypedArray."); + } + } // No need to use OpenJPEG here since we're only getting very basic // information which are located in the first bytes of the file. let newByte = stream.getByte();