1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 10:08:06 +02:00

Remove manual clamping/rounding from ColorSpace and PDFImage, by having their methods use Uint8ClampedArrays

The built-in image decoders are already using `Uint8ClampedArray` when returning data, and this patch simply extends that to the rest of the image/colorspace code.

As far as I can tell, the only reason for using manual clamping/rounding in the first place was because TypedArrays used to be polyfilled (using regular arrays). And trying to polyfill the native clamping/rounding would probably have been had too much overhead, but given that TypedArray support is required in PDF.js version `2.0` that's no longer a concern.

*Please note:* Because of different rounding behaviour, basically `Math.round` in `Uint8ClampedArray` respectively `Math.floor` in the old code, there will be very slight movement in quite a few existing test-cases. However, the changes should be imperceivable to the naked eye, given that the absolute difference is *at most* `1` for each RGB component when comparing `master` and this patch (see also the updated expectation values in the unit-tests).
This commit is contained in:
Jonas Jenwald 2018-06-11 17:25:40 +02:00
parent 55199aa281
commit 731f2e6dfc
9 changed files with 150 additions and 157 deletions

View file

@ -259,10 +259,10 @@ var PDFImage = (function PDFImageClosure() {
// form, so we can just transfer it.
data = imgArray;
} else if (!inverseDecode) {
data = new Uint8Array(actualLength);
data = new Uint8ClampedArray(actualLength);
data.set(imgArray);
} else {
data = new Uint8Array(computedLength);
data = new Uint8ClampedArray(computedLength);
data.set(imgArray);
for (i = actualLength; i < computedLength; i++) {
data[i] = 0xff;
@ -406,7 +406,7 @@ var PDFImage = (function PDFImageClosure() {
if (smask) {
sw = smask.width;
sh = smask.height;
alphaBuf = new Uint8Array(sw * sh);
alphaBuf = new Uint8ClampedArray(sw * sh);
smask.fillGrayBuffer(alphaBuf);
if (sw !== width || sh !== height) {
alphaBuf = resizeImageMask(alphaBuf, smask.bpc, sw, sh,
@ -416,7 +416,7 @@ var PDFImage = (function PDFImageClosure() {
if (mask instanceof PDFImage) {
sw = mask.width;
sh = mask.height;
alphaBuf = new Uint8Array(sw * sh);
alphaBuf = new Uint8ClampedArray(sw * sh);
mask.numComps = 1;
mask.fillGrayBuffer(alphaBuf);
@ -432,7 +432,7 @@ var PDFImage = (function PDFImageClosure() {
} else if (Array.isArray(mask)) {
// Color key mask: if any of the components are outside the range
// then they should be painted.
alphaBuf = new Uint8Array(width * height);
alphaBuf = new Uint8ClampedArray(width * height);
var numComps = this.numComps;
for (i = 0, ii = width * height; i < ii; ++i) {
var opacity = 0;
@ -474,7 +474,6 @@ var PDFImage = (function PDFImageClosure() {
var matteG = matteRgb[1];
var matteB = matteRgb[2];
var length = width * height * 4;
var r, g, b;
for (var i = 0; i < length; i += 4) {
var alpha = buffer[i + 3];
if (alpha === 0) {
@ -486,12 +485,9 @@ var PDFImage = (function PDFImageClosure() {
continue;
}
var k = 255 / alpha;
r = (buffer[i] - matteR) * k + matteR;
g = (buffer[i + 1] - matteG) * k + matteG;
b = (buffer[i + 2] - matteB) * k + matteB;
buffer[i] = r <= 0 ? 0 : r >= 255 ? 255 : r | 0;
buffer[i + 1] = g <= 0 ? 0 : g >= 255 ? 255 : g | 0;
buffer[i + 2] = b <= 0 ? 0 : b >= 255 ? 255 : b | 0;
buffer[i] = (buffer[i] - matteR) * k + matteR;
buffer[i + 1] = (buffer[i + 1] - matteG) * k + matteG;
buffer[i + 2] = (buffer[i + 2] - matteB) * k + matteB;
}
},
@ -501,6 +497,8 @@ var PDFImage = (function PDFImageClosure() {
var imgData = { // other fields are filled in below
width: drawWidth,
height: drawHeight,
kind: 0,
data: null,
};
var numComps = this.numComps;
@ -540,7 +538,7 @@ var PDFImage = (function PDFImageClosure() {
if (this.image instanceof DecodeStream) {
imgData.data = imgArray;
} else {
var newArray = new Uint8Array(imgArray.length);
var newArray = new Uint8ClampedArray(imgArray.length);
newArray.set(imgArray);
imgData.data = newArray;
}
@ -584,12 +582,12 @@ var PDFImage = (function PDFImageClosure() {
var alpha01, maybeUndoPreblend;
if (!forceRGBA && !this.smask && !this.mask) {
imgData.kind = ImageKind.RGB_24BPP;
imgData.data = new Uint8Array(drawWidth * drawHeight * 3);
imgData.data = new Uint8ClampedArray(drawWidth * drawHeight * 3);
alpha01 = 0;
maybeUndoPreblend = false;
} else {
imgData.kind = ImageKind.RGBA_32BPP;
imgData.data = new Uint8Array(drawWidth * drawHeight * 4);
imgData.data = new Uint8ClampedArray(drawWidth * drawHeight * 4);
alpha01 = 1;
maybeUndoPreblend = true;
@ -653,7 +651,7 @@ var PDFImage = (function PDFImageClosure() {
// we aren't using a colorspace so we need to scale the value
var scale = 255 / ((1 << bpc) - 1);
for (i = 0; i < length; ++i) {
buffer[i] = (scale * comps[i]) | 0;
buffer[i] = scale * comps[i];
}
},
@ -662,7 +660,7 @@ var PDFImage = (function PDFImageClosure() {
this.image.drawWidth = drawWidth || this.width;
this.image.drawHeight = drawHeight || this.height;
this.image.forceRGB = !!forceRGB;
return this.image.getBytes(length);
return this.image.getBytes(length, /* forceClamped = */ true);
},
};
return PDFImage;