Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert the code in src/core/image.js to use ES6 classes #12059

Merged
merged 1 commit into from
Jul 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 19 additions & 36 deletions src/core/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ 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.
Expand Down Expand Up @@ -79,8 +78,8 @@ var PDFImage = (function PDFImageClosure() {
return dest;
}

// eslint-disable-next-line no-shadow
function PDFImage({
class PDFImage {
constructor({
xref,
res,
image,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -286,9 +286,9 @@ var PDFImage = (function PDFImageClosure() {
pdfFunctionFactory,
localColorSpaceCache,
});
};
}

PDFImage.createMask = function ({
static createMask({
imgArray,
width,
height,
Expand Down Expand Up @@ -340,24 +340,23 @@ 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(
this.height,
(this.smask && this.smask.height) || 0,
(this.mask && this.mask.height) || 0
);
},
}

decodeBuffer(buffer) {
var bpc = this.bpc;
Expand Down Expand Up @@ -387,7 +386,7 @@ var PDFImage = (function PDFImageClosure() {
index++;
}
}
},
}

getComponents(buffer) {
var bpc = this.bpc;
Expand Down Expand Up @@ -477,7 +476,7 @@ var PDFImage = (function PDFImageClosure() {
}
}
return output;
},
}

fillOpacity(rgbaBuf, width, height, actualHeight, image) {
if (
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -564,7 +549,7 @@ var PDFImage = (function PDFImageClosure() {
rgbaBuf[j] = 255;
}
}
},
}

undoPreblend(buffer, width, height) {
if (
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -747,7 +732,7 @@ var PDFImage = (function PDFImageClosure() {
}

return imgData;
},
}

fillGrayBuffer(buffer) {
if (
Expand Down Expand Up @@ -803,17 +788,15 @@ var PDFImage = (function PDFImageClosure() {
for (i = 0; i < length; ++i) {
buffer[i] = scale * comps[i];
}
},
}

getImageBytes(length, drawWidth, drawHeight, forceRGB = false) {
this.image.reset();
this.image.drawWidth = drawWidth || this.width;
this.image.drawHeight = drawHeight || this.height;
this.image.forceRGB = !!forceRGB;
return this.image.getBytes(length, /* forceClamped = */ true);
},
};
return PDFImage;
})();
}
}

export { PDFImage };