[api-major] Remove the standalone image_decoders build

This is removed now, since:
 - There's most likely very few users of it, since we've only ever seen a handful of questions/issues regarding it over the eight years it has existed.
   Few issues could obviously mean that everything just works, but that explanation seems quite unlikely given the experience from the PDF.js project as a whole.

 - The JBig2 and JPEG 2000 image decoders are unlikely to still work, ever since the old JavaScript decoders were replaced with WebAssembly ones.
   However, there has been no discussions/issues opened about this.

 - The Wasm decoders are built specifically for the PDF.js use-case, and the kind of images contained in PDFs, and may not be well suited for general JBig2 and JPEG 2000 images.

  - The PDF.js JPEG decoder is still implemented in JavaScript, however in browsers it can largely be replaced by the native `ImageDecoder` API.
    In Node.js environments that functionality could be polyfilled e.g. with the `@napi-rs/webcodecs` NPM package.

 - This way we slightly simplify the PDF.js image handling code and the build-system.

 - If such a build-target was proposed today, I cannot see that we'd actually consider adding it.

*Note:* Any users of the standalone `image_decoders` build can most likely just stay on an older (and working) version.
This commit is contained in:
Jonas Jenwald
2026-11-04 13:33:09 +01:00
parent 236ed7f853
commit 75ceea3a0f
14 changed files with 9 additions and 344 deletions
+1 -1
View File
@@ -170,7 +170,7 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC")) {
}
```
Common flags: `GENERIC`, `MOZCENTRAL`, `CHROME`, `MINIFIED`, `TESTING`, `LIB`, `SKIP_BABEL`, `IMAGE_DECODERS`
Common flags: `GENERIC`, `MOZCENTRAL`, `CHROME`, `MINIFIED`, `TESTING`, `LIB`, `SKIP_BABEL`
### Testing
-40
View File
@@ -1,40 +0,0 @@
<!doctype html>
<!--
Copyright 2018 Mozilla Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html dir="ltr" mozdisallowselectionprint>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<meta name="google" content="notranslate" />
<title>PDF.js standalone JpegImage parser</title>
<style>
body {
background-color: #808080;
margin: 0;
padding: 0;
}
</style>
<script src="../../node_modules/pdfjs-dist/image_decoders/pdf.image_decoders.mjs" type="module"></script>
</head>
<body tabindex="1">
<canvas id="jpegCanvas" width="0" height="0"></canvas>
<script src="jpeg_viewer.mjs" type="module"></script>
</body>
</html>
-59
View File
@@ -1,59 +0,0 @@
/* Copyright 2018 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
if (!pdfjsImageDecoders.JpegImage) {
// eslint-disable-next-line no-alert
alert("Please build the pdfjs-dist library using `gulp dist-install`");
}
const JPEG_IMAGE = "fish.jpg";
const jpegCanvas = document.getElementById("jpegCanvas");
const jpegCtx = jpegCanvas.getContext("2d");
// Load the image data, and convert it to a Uint8Array.
//
const response = await fetch(JPEG_IMAGE);
if (!response.ok) {
throw new Error(response.statusText);
}
const typedArrayImage = await response.bytes();
// Parse the image data using `JpegImage`.
//
const jpegImage = new pdfjsImageDecoders.JpegImage();
jpegImage.parse(typedArrayImage);
const width = jpegImage.width,
height = jpegImage.height;
const jpegData = jpegImage.getData({
width,
height,
forceRGB: true,
});
// Render the JPEG image on a <canvas>.
//
const imageData = jpegCtx.createImageData(width, height);
const imageBytes = imageData.data;
for (let j = 0, k = 0, jj = width * height * 4; j < jj;) {
imageBytes[j++] = jpegData[k++];
imageBytes[j++] = jpegData[k++];
imageBytes[j++] = jpegData[k++];
imageBytes[j++] = 255;
}
jpegCanvas.width = width;
jpegCanvas.height = height;
jpegCtx.putImageData(imageData, 0, 0);
+1 -76
View File
@@ -60,8 +60,6 @@ const GENERIC_DIR = BUILD_DIR + "generic/";
const GENERIC_LEGACY_DIR = BUILD_DIR + "generic-legacy/";
const COMPONENTS_DIR = BUILD_DIR + "components/";
const COMPONENTS_LEGACY_DIR = BUILD_DIR + "components-legacy/";
const IMAGE_DECODERS_DIR = BUILD_DIR + "image_decoders/";
const IMAGE_DECODERS_LEGACY_DIR = BUILD_DIR + "image_decoders-legacy/";
const DEFAULT_PREFERENCES_DIR = BUILD_DIR + "default_preferences/";
const MINIFIED_DIR = BUILD_DIR + "minified/";
const MINIFIED_LEGACY_DIR = BUILD_DIR + "minified-legacy/";
@@ -125,7 +123,6 @@ const DEFINES = Object.freeze({
MINIFIED: false,
COMPONENTS: false,
LIB: false,
IMAGE_DECODERS: false,
INTERNAL_VIEWER: false,
});
@@ -715,21 +712,6 @@ function createComponentsBundle(defines) {
);
}
function createImageDecodersBundle(defines) {
const componentsFileConfig = createWebpackConfig(defines, {
filename: defines.MINIFIED
? "pdf.image_decoders.min.mjs"
: "pdf.image_decoders.mjs",
library: {
type: "module",
},
});
return createWebpackStream(
"./src/pdf.image_decoders.js",
componentsFileConfig
);
}
function createCMapBundle() {
return gulp.src(["external/bcmaps/*.bcmap", "external/bcmaps/LICENSE"], {
base: "external/bcmaps",
@@ -1582,35 +1564,6 @@ gulp.task(
})
);
gulp.task(
"image_decoders",
gulp.series(createBuildNumber, function createImageDecoders() {
console.log("\n### Creating image decoders");
const defines = { ...DEFINES, GENERIC: true, IMAGE_DECODERS: true };
return createImageDecodersBundle(defines).pipe(
gulp.dest(IMAGE_DECODERS_DIR)
);
})
);
gulp.task(
"image_decoders-legacy",
gulp.series(createBuildNumber, function createImageDecodersLegacy() {
console.log("\n### Creating (legacy) image decoders");
const defines = {
...DEFINES,
GENERIC: true,
IMAGE_DECODERS: true,
SKIP_BABEL: false,
};
return createImageDecodersBundle(defines).pipe(
gulp.dest(IMAGE_DECODERS_LEGACY_DIR)
);
})
);
function buildMinified(defines, dir) {
fs.rmSync(dir, { recursive: true, force: true });
@@ -1619,9 +1572,6 @@ function buildMinified(defines, dir) {
createWorkerBundle(defines).pipe(gulp.dest(dir + "build")),
createRendererWorkerBundle(defines).pipe(gulp.dest(dir + "build")),
createSandboxBundle(defines).pipe(gulp.dest(dir + "build")),
createImageDecodersBundle({ ...defines, IMAGE_DECODERS: true }).pipe(
gulp.dest(dir + "image_decoders")
),
]);
}
@@ -2391,7 +2341,7 @@ function buildLib(defines, dir) {
gulp.src(
[
"src/{core,display,shared}/**/*.js",
"src/{pdf,pdf.image_decoders,pdf.worker,pdf.renderer}.js",
"src/{pdf,pdf.worker,pdf.renderer}.js",
],
{ base: "src/", encoding: false, sourcemaps: enableSourceMaps }
),
@@ -3536,8 +3486,6 @@ gulp.task(
"generic-legacy",
"components",
"components-legacy",
"image_decoders",
"image_decoders-legacy",
"minified",
"minified-legacy",
"types",
@@ -3617,11 +3565,6 @@ gulp.task(
}
)
.pipe(gulp.dest(DIST_DIR + "build/")),
gulp
.src(MINIFIED_DIR + "image_decoders/pdf.image_decoders.min.mjs", {
encoding: false,
})
.pipe(gulp.dest(DIST_DIR + "image_decoders/")),
gulp
.src(
MINIFIED_LEGACY_DIR +
@@ -3629,12 +3572,6 @@ gulp.task(
{ encoding: false }
)
.pipe(gulp.dest(DIST_DIR + "legacy/build/")),
gulp
.src(
MINIFIED_LEGACY_DIR + "image_decoders/pdf.image_decoders.min.mjs",
{ encoding: false }
)
.pipe(gulp.dest(DIST_DIR + "legacy/image_decoders/")),
gulp
.src(COMPONENTS_DIR + "**/*", {
base: COMPONENTS_DIR,
@@ -3647,18 +3584,6 @@ gulp.task(
encoding: false,
})
.pipe(gulp.dest(DIST_DIR + "legacy/web/")),
gulp
.src(IMAGE_DECODERS_DIR + "**/*", {
base: IMAGE_DECODERS_DIR,
encoding: false,
})
.pipe(gulp.dest(DIST_DIR + "image_decoders/")),
gulp
.src(IMAGE_DECODERS_LEGACY_DIR + "**/*", {
base: IMAGE_DECODERS_LEGACY_DIR,
encoding: false,
})
.pipe(gulp.dest(DIST_DIR + "legacy/image_decoders/")),
gulp
.src(TYPES_DIR + "**/*", { base: TYPES_DIR, encoding: false })
.pipe(gulp.dest(DIST_DIR + "types/")),
+1 -5
View File
@@ -29,11 +29,7 @@ class JBig2CCITTFaxImage extends WasmImage {
_noWasmFilename = "jbig2_nowasm_fallback.js";
static get instance() {
return shadow(
this,
"instance",
new JBig2CCITTFaxImage(/* trackInstance = */ true)
);
return shadow(this, "instance", new JBig2CCITTFaxImage());
}
async decode(bytes, width, height, globals, CCITTOptions) {
+1 -38
View File
@@ -800,11 +800,6 @@ function skipData(data, view, offset) {
class JpegImage {
constructor(options) {
this._colorTransform = options?.colorTransform ?? -1;
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("IMAGE_DECODERS")) {
this._decodeTransform = options?.decodeTransform || null;
this._isSourcePDF = false;
}
}
static canUseImageDecoder(data, colorTransform = -1) {
@@ -1213,7 +1208,7 @@ class JpegImage {
scaleY = this.height / height;
let component, componentScaleX, componentScaleY, blocksPerScanline;
let x, y, i, j, k;
let x, y, i, j;
let offset = 0;
const numComponents = this.components.length;
const dataLength = width * height * numComponents;
@@ -1249,35 +1244,6 @@ class JpegImage {
}
}
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("IMAGE_DECODERS")) {
// decodeTransform contains pairs of multiplier (-256..256) and additive
let transform = this._decodeTransform;
// In PDF files, JPEG images with CMYK colour spaces are usually inverted
// (this can be observed by extracting the raw image data).
// Since the conversion algorithms (see below) were written primarily for
// the PDF use-cases, attempting to use `JpegImage` to parse standalone
// JPEG (CMYK) images may thus result in inverted images (see issue 9513).
//
// Unfortunately it's not (always) possible to tell, from the image data
// alone, if it needs to be inverted. Thus in an attempt to provide better
// out-of-the-box behaviour when `JpegImage` is used standalone, default
// to inverting JPEG (CMYK) images if and only if the image data does
// *not* come from a PDF file and no `decodeTransform` was provided.
if (!this._isSourcePDF && numComponents === 4) {
transform ||= new Int32Array([
-256, 255, -256, 255, -256, 255, -256, 255,
]);
}
if (transform) {
for (i = 0; i < dataLength;) {
for (j = 0, k = 0; j < numComponents; j++, i++, k += 2) {
data[i] = ((data[i] * transform[k]) >> 8) + transform[k + 1];
}
}
}
}
return data;
}
@@ -1385,9 +1351,6 @@ class JpegImage {
if (this.numComponents > 4) {
throw new JpegError("Unsupported color mode");
}
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("IMAGE_DECODERS")) {
this._isSourcePDF = arguments[0]?.isSourcePDF === true;
}
// Type of data: Uint8ClampedArray(width * height * numComponents)
const data = this.#getLinearizedBlockData(width, height);
+1 -9
View File
@@ -15,7 +15,6 @@
import { BaseException, shadow } from "../shared/util.js";
import OpenJPEG from "../../external/openjpeg/openjpeg.js";
import { Stream } from "./stream.js";
import { WasmImage } from "./wasm_image.js";
class JpxError extends BaseException {
@@ -30,7 +29,7 @@ class JpxImage extends WasmImage {
_noWasmFilename = "openjpeg_nowasm_fallback.js";
static get instance() {
return shadow(this, "instance", new JpxImage(/* trackInstance = */ true));
return shadow(this, "instance", new JpxImage());
}
async decode(
@@ -84,13 +83,6 @@ class JpxImage extends WasmImage {
}
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();
+3 -6
View File
@@ -42,18 +42,15 @@ class WasmImage {
}
}
constructor(trackInstance = false) {
constructor() {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === WasmImage
) {
unreachable("Cannot initialize WasmImage.");
}
if (trackInstance) {
// Keep track of the instances for `cleanup` purposes.
WasmImage.#instances.add(this);
}
// Keep track of the instances for `cleanup` purposes.
WasmImage.#instances.add(this);
}
async #instantiateWasm(failureCallback, imports, successCallback) {
-50
View File
@@ -1,50 +0,0 @@
/* Copyright 2018 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
getVerbosityLevel,
setVerbosityLevel,
VerbosityLevel,
} from "./shared/util.js";
import {
Jbig2Error,
JBig2CCITTFaxImage as Jbig2Image,
} from "./core/jbig2_ccittFax.js";
import { JpegError, JpegImage } from "./core/jpg.js";
import { JpxError, JpxImage } from "./core/jpx.js";
globalThis.pdfjsImageDecoders = {
getVerbosityLevel,
Jbig2Error,
Jbig2Image,
JpegError,
JpegImage,
JpxError,
JpxImage,
setVerbosityLevel,
VerbosityLevel,
};
export {
getVerbosityLevel,
Jbig2Error,
Jbig2Image,
JpegError,
JpegImage,
JpxError,
JpxImage,
setVerbosityLevel,
VerbosityLevel,
};
+1 -1
View File
@@ -49,7 +49,7 @@ const RAW_DATA = (() => {
})();
const DEFLATED_DATA = new Uint8Array(deflateSync(RAW_DATA));
const JPEG_DATA = readFile("examples/image_decoders/fish.jpg");
const JPEG_DATA = readFile("test/images/fish.jpg");
describe("core", () => {
benchmark("Lexer: tokenize content stream", () => {

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 22 KiB

-1
View File
@@ -46,7 +46,6 @@
"operator_list_dependencies_spec.js",
"parser_spec.js",
"pattern_spec.js",
"pdf.image_decoders_spec.js",
"pdf.worker_spec.js",
"pdf_find_controller_spec.js",
"pdf_find_utils_spec.js",
-1
View File
@@ -92,7 +92,6 @@ async function initializePDFJS(callback) {
"pdfjs-test/unit/operator_list_dependencies_spec.js",
"pdfjs-test/unit/parser_spec.js",
"pdfjs-test/unit/pattern_spec.js",
"pdfjs-test/unit/pdf.image_decoders_spec.js",
"pdfjs-test/unit/pdf.worker_spec.js",
"pdfjs-test/unit/pdf_find_controller_spec.js",
"pdfjs-test/unit/pdf_find_utils_spec.js",
-57
View File
@@ -1,57 +0,0 @@
/* Copyright 2023 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
getVerbosityLevel,
setVerbosityLevel,
VerbosityLevel,
} from "../../src/shared/util.js";
import {
Jbig2Error,
JBig2CCITTFaxImage as Jbig2Image,
} from "../../src/core/jbig2_ccittFax.js";
import { JpegError, JpegImage } from "../../src/core/jpg.js";
import { JpxError, JpxImage } from "../../src/core/jpx.js";
const expectedAPI = Object.freeze({
getVerbosityLevel,
Jbig2Error,
Jbig2Image,
JpegError,
JpegImage,
JpxError,
JpxImage,
setVerbosityLevel,
VerbosityLevel,
});
describe("pdfimage_api", function () {
it("checks that the *official* PDF.js-image decoders API exposes the expected functionality", async function () {
// eslint-disable-next-line no-unsanitized/method
const pdfimageAPI = await import(
typeof PDFJSDev !== "undefined" && PDFJSDev.test("LIB")
? "../../pdf.image_decoders.js"
: "../../src/pdf.image_decoders.js"
);
// The imported Object contains an (automatically) inserted Symbol,
// hence we copy the data to allow using a simple comparison below.
expect({ ...pdfimageAPI }).toEqual(expectedAPI);
expect(Object.keys(globalThis.pdfjsImageDecoders).sort()).toEqual(
Object.keys(expectedAPI).sort()
);
});
});