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