Merge pull request #12059 from Snuffleupagus/image-class
Convert the code in `src/core/image.js` to use ES6 classes
This commit is contained in:
commit
c4255fdbfd
@ -20,12 +20,11 @@ import { DecodeStream } from "./stream.js";
|
||||
import { JpegStream } from "./jpeg_stream.js";
|
||||
import { JpxImage } from "./jpx.js";
|
||||
|
||||
var PDFImage = (function PDFImageClosure() {
|
||||
/**
|
||||
/**
|
||||
* Decode and clamp a value. The formula is different from the spec because we
|
||||
* don't decode to float range [0,1], we decode it in the [0,max] range.
|
||||
*/
|
||||
function decodeAndClamp(value, addend, coefficient, max) {
|
||||
function decodeAndClamp(value, addend, coefficient, max) {
|
||||
value = addend + value * coefficient;
|
||||
// Clamp the value to the range
|
||||
if (value < 0) {
|
||||
@ -34,9 +33,9 @@ var PDFImage = (function PDFImageClosure() {
|
||||
value = max;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Resizes an image mask with 1 component.
|
||||
* @param {TypedArray} src - The source buffer.
|
||||
* @param {number} bpc - Number of bits per component.
|
||||
@ -46,7 +45,7 @@ var PDFImage = (function PDFImageClosure() {
|
||||
* @param {number} h2 - New height.
|
||||
* @returns {TypedArray} The resized image mask buffer.
|
||||
*/
|
||||
function resizeImageMask(src, bpc, w1, h1, w2, h2) {
|
||||
function resizeImageMask(src, bpc, w1, h1, w2, h2) {
|
||||
var length = w2 * h2;
|
||||
let dest;
|
||||
if (bpc <= 8) {
|
||||
@ -77,10 +76,10 @@ var PDFImage = (function PDFImageClosure() {
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-shadow
|
||||
function PDFImage({
|
||||
class PDFImage {
|
||||
constructor({
|
||||
xref,
|
||||
res,
|
||||
image,
|
||||
@ -247,11 +246,12 @@ var PDFImage = (function PDFImageClosure() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles processing of image data and returns the Promise that is resolved
|
||||
* with a PDFImage when the image is ready to be used.
|
||||
*/
|
||||
PDFImage.buildImage = async function ({
|
||||
static async buildImage({
|
||||
xref,
|
||||
res,
|
||||
image,
|
||||
@ -286,9 +286,9 @@ var PDFImage = (function PDFImageClosure() {
|
||||
pdfFunctionFactory,
|
||||
localColorSpaceCache,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
PDFImage.createMask = function ({
|
||||
static createMask({
|
||||
imgArray,
|
||||
width,
|
||||
height,
|
||||
@ -340,16 +340,15 @@ var PDFImage = (function PDFImageClosure() {
|
||||
}
|
||||
|
||||
return { data, width, height };
|
||||
};
|
||||
}
|
||||
|
||||
PDFImage.prototype = {
|
||||
get drawWidth() {
|
||||
return Math.max(
|
||||
this.width,
|
||||
(this.smask && this.smask.width) || 0,
|
||||
(this.mask && this.mask.width) || 0
|
||||
);
|
||||
},
|
||||
}
|
||||
|
||||
get drawHeight() {
|
||||
return Math.max(
|
||||
@ -357,7 +356,7 @@ var PDFImage = (function PDFImageClosure() {
|
||||
(this.smask && this.smask.height) || 0,
|
||||
(this.mask && this.mask.height) || 0
|
||||
);
|
||||
},
|
||||
}
|
||||
|
||||
decodeBuffer(buffer) {
|
||||
var bpc = this.bpc;
|
||||
@ -387,7 +386,7 @@ var PDFImage = (function PDFImageClosure() {
|
||||
index++;
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
getComponents(buffer) {
|
||||
var bpc = this.bpc;
|
||||
@ -477,7 +476,7 @@ var PDFImage = (function PDFImageClosure() {
|
||||
}
|
||||
}
|
||||
return output;
|
||||
},
|
||||
}
|
||||
|
||||
fillOpacity(rgbaBuf, width, height, actualHeight, image) {
|
||||
if (
|
||||
@ -499,14 +498,7 @@ var PDFImage = (function PDFImageClosure() {
|
||||
alphaBuf = new Uint8ClampedArray(sw * sh);
|
||||
smask.fillGrayBuffer(alphaBuf);
|
||||
if (sw !== width || sh !== height) {
|
||||
alphaBuf = resizeImageMask(
|
||||
alphaBuf,
|
||||
smask.bpc,
|
||||
sw,
|
||||
sh,
|
||||
width,
|
||||
height
|
||||
);
|
||||
alphaBuf = resizeImageMask(alphaBuf, smask.bpc, sw, sh, width, height);
|
||||
}
|
||||
} else if (mask) {
|
||||
if (mask instanceof PDFImage) {
|
||||
@ -522,14 +514,7 @@ var PDFImage = (function PDFImageClosure() {
|
||||
}
|
||||
|
||||
if (sw !== width || sh !== height) {
|
||||
alphaBuf = resizeImageMask(
|
||||
alphaBuf,
|
||||
mask.bpc,
|
||||
sw,
|
||||
sh,
|
||||
width,
|
||||
height
|
||||
);
|
||||
alphaBuf = resizeImageMask(alphaBuf, mask.bpc, sw, sh, width, height);
|
||||
}
|
||||
} else if (Array.isArray(mask)) {
|
||||
// Color key mask: if any of the components are outside the range
|
||||
@ -564,7 +549,7 @@ var PDFImage = (function PDFImageClosure() {
|
||||
rgbaBuf[j] = 255;
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
undoPreblend(buffer, width, height) {
|
||||
if (
|
||||
@ -600,7 +585,7 @@ var PDFImage = (function PDFImageClosure() {
|
||||
buffer[i + 1] = (buffer[i + 1] - matteG) * k + matteG;
|
||||
buffer[i + 2] = (buffer[i + 2] - matteB) * k + matteB;
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
createImageData(forceRGBA = false) {
|
||||
var drawWidth = this.drawWidth;
|
||||
@ -747,7 +732,7 @@ var PDFImage = (function PDFImageClosure() {
|
||||
}
|
||||
|
||||
return imgData;
|
||||
},
|
||||
}
|
||||
|
||||
fillGrayBuffer(buffer) {
|
||||
if (
|
||||
@ -803,7 +788,7 @@ var PDFImage = (function PDFImageClosure() {
|
||||
for (i = 0; i < length; ++i) {
|
||||
buffer[i] = scale * comps[i];
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
getImageBytes(length, drawWidth, drawHeight, forceRGB = false) {
|
||||
this.image.reset();
|
||||
@ -811,9 +796,7 @@ var PDFImage = (function PDFImageClosure() {
|
||||
this.image.drawHeight = drawHeight || this.height;
|
||||
this.image.forceRGB = !!forceRGB;
|
||||
return this.image.getBytes(length, /* forceClamped = */ true);
|
||||
},
|
||||
};
|
||||
return PDFImage;
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
export { PDFImage };
|
||||
|
Loading…
Reference in New Issue
Block a user