mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-25 09:38:06 +02:00
Merge pull request #11601 from Snuffleupagus/rm-nativeImageDecoderSupport
[api-minor] Decode all JPEG images with the built-in PDF.js decoder in `src/core/jpg.js`
This commit is contained in:
commit
3b615e4ca3
23 changed files with 57 additions and 682 deletions
|
@ -15,103 +15,7 @@
|
|||
/* eslint no-var: error */
|
||||
|
||||
import { assert, info, shadow } from "../shared/util.js";
|
||||
import { ColorSpace } from "./colorspace.js";
|
||||
import { JpegStream } from "./jpeg_stream.js";
|
||||
import { RefSetCache } from "./primitives.js";
|
||||
import { Stream } from "./stream.js";
|
||||
|
||||
class NativeImageDecoder {
|
||||
constructor({
|
||||
xref,
|
||||
resources,
|
||||
handler,
|
||||
forceDataSchema = false,
|
||||
pdfFunctionFactory,
|
||||
}) {
|
||||
this.xref = xref;
|
||||
this.resources = resources;
|
||||
this.handler = handler;
|
||||
this.forceDataSchema = forceDataSchema;
|
||||
this.pdfFunctionFactory = pdfFunctionFactory;
|
||||
}
|
||||
|
||||
canDecode(image) {
|
||||
return (
|
||||
image instanceof JpegStream &&
|
||||
image.maybeValidDimensions &&
|
||||
NativeImageDecoder.isDecodable(
|
||||
image,
|
||||
this.xref,
|
||||
this.resources,
|
||||
this.pdfFunctionFactory
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
decode(image) {
|
||||
// For natively supported JPEGs send them to the main thread for decoding.
|
||||
const dict = image.dict;
|
||||
let colorSpace = dict.get("ColorSpace", "CS");
|
||||
colorSpace = ColorSpace.parse(
|
||||
colorSpace,
|
||||
this.xref,
|
||||
this.resources,
|
||||
this.pdfFunctionFactory
|
||||
);
|
||||
|
||||
return this.handler
|
||||
.sendWithPromise("JpegDecode", [
|
||||
image.getIR(this.forceDataSchema),
|
||||
colorSpace.numComps,
|
||||
])
|
||||
.then(function ({ data, width, height }) {
|
||||
return new Stream(data, 0, data.length, dict);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the image can be decoded and displayed by the browser without any
|
||||
* further processing such as color space conversions.
|
||||
*/
|
||||
static isSupported(image, xref, res, pdfFunctionFactory) {
|
||||
const dict = image.dict;
|
||||
if (dict.has("DecodeParms") || dict.has("DP")) {
|
||||
return false;
|
||||
}
|
||||
const cs = ColorSpace.parse(
|
||||
dict.get("ColorSpace", "CS"),
|
||||
xref,
|
||||
res,
|
||||
pdfFunctionFactory
|
||||
);
|
||||
// isDefaultDecode() of DeviceGray and DeviceRGB needs no `bpc` argument.
|
||||
return (
|
||||
(cs.name === "DeviceGray" || cs.name === "DeviceRGB") &&
|
||||
cs.isDefaultDecode(dict.getArray("Decode", "D"))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the image can be decoded by the browser.
|
||||
*/
|
||||
static isDecodable(image, xref, res, pdfFunctionFactory) {
|
||||
const dict = image.dict;
|
||||
if (dict.has("DecodeParms") || dict.has("DP")) {
|
||||
return false;
|
||||
}
|
||||
const cs = ColorSpace.parse(
|
||||
dict.get("ColorSpace", "CS"),
|
||||
xref,
|
||||
res,
|
||||
pdfFunctionFactory
|
||||
);
|
||||
const bpc = dict.get("BitsPerComponent", "BPC") || 1;
|
||||
return (
|
||||
(cs.numComps === 1 || cs.numComps === 3) &&
|
||||
cs.isDefaultDecode(dict.getArray("Decode", "D"), bpc)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class GlobalImageCache {
|
||||
static get NUM_PAGES_THRESHOLD() {
|
||||
|
@ -207,4 +111,4 @@ class GlobalImageCache {
|
|||
}
|
||||
}
|
||||
|
||||
export { NativeImageDecoder, GlobalImageCache };
|
||||
export { GlobalImageCache };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue