1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +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

@ -261,6 +261,7 @@ class Annotation {
/**
* Set the color and take care of color space conversion.
* The default value is black, in RGB color space.
*
* @public
* @memberof Annotation
@ -269,7 +270,7 @@ class Annotation {
* 4 (CMYK) elements
*/
setColor(color) {
let rgbColor = new Uint8Array(3); // Black in RGB color space (default)
let rgbColor = new Uint8ClampedArray(3);
if (!Array.isArray(color)) {
this.color = rgbColor;
return;

View file

@ -65,7 +65,7 @@ var ColorSpace = (function ColorSpaceClosure() {
* of the rgb components, each value ranging from [0,255].
*/
getRgb(src, srcOffset) {
var rgb = new Uint8Array(3);
let rgb = new Uint8ClampedArray(3);
this.getRgbItem(src, srcOffset, rgb, 0);
return rgb;
},
@ -118,7 +118,7 @@ var ColorSpace = (function ColorSpaceClosure() {
if (this.isPassthrough(bpc)) {
rgbBuf = comps;
} else if (this.numComps === 1 && count > numComponentColors &&
this.name !== 'DeviceGray' && this.name !== 'DeviceRGB') {
this.name !== 'DeviceGray' && this.name !== 'DeviceRGB') {
// Optimization: create a color map when there is just one component and
// we are converting more colors than the size of the color map. We
// don't build the map if the colorspace is gray or rgb since those
@ -134,7 +134,7 @@ var ColorSpace = (function ColorSpaceClosure() {
for (i = 0; i < numComponentColors; i++) {
allColors[i] = i;
}
var colorMap = new Uint8Array(numComponentColors * 3);
var colorMap = new Uint8ClampedArray(numComponentColors * 3);
this.getRgbBuffer(allColors, 0, numComponentColors, colorMap, 0, bpc,
/* alpha01 = */ 0);
@ -165,7 +165,7 @@ var ColorSpace = (function ColorSpaceClosure() {
this.getRgbBuffer(comps, 0, width * actualHeight, dest, 0, bpc,
alpha01);
} else {
rgbBuf = new Uint8Array(count * 3);
rgbBuf = new Uint8ClampedArray(count * 3);
this.getRgbBuffer(comps, 0, count, rgbBuf, 0, bpc,
/* alpha01 = */ 0);
}
@ -446,7 +446,8 @@ var AlternateCS = (function AlternateCSClosure() {
var isPassthrough = (base.isPassthrough(8) || !usesZeroToOneRange) &&
alpha01 === 0;
var pos = isPassthrough ? destOffset : 0;
var baseBuf = isPassthrough ? dest : new Uint8Array(baseNumComps * count);
let baseBuf = isPassthrough ?
dest : new Uint8ClampedArray(baseNumComps * count);
var numComps = this.numComps;
var scaled = new Float32Array(numComps);
@ -569,15 +570,14 @@ var DeviceGrayCS = (function DeviceGrayCSClosure() {
DeviceGrayCS.prototype = {
getRgb: ColorSpace.prototype.getRgb,
getRgbItem(src, srcOffset, dest, destOffset) {
var c = (src[srcOffset] * 255) | 0;
c = c < 0 ? 0 : c > 255 ? 255 : c;
let c = src[srcOffset] * 255;
dest[destOffset] = dest[destOffset + 1] = dest[destOffset + 2] = c;
},
getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
var scale = 255 / ((1 << bits) - 1);
var j = srcOffset, q = destOffset;
for (var i = 0; i < count; ++i) {
var c = (scale * src[j++]) | 0;
let c = scale * src[j++];
dest[q++] = c;
dest[q++] = c;
dest[q++] = c;
@ -606,12 +606,9 @@ var DeviceRgbCS = (function DeviceRgbCSClosure() {
DeviceRgbCS.prototype = {
getRgb: ColorSpace.prototype.getRgb,
getRgbItem(src, srcOffset, dest, destOffset) {
var r = (src[srcOffset] * 255) | 0;
var g = (src[srcOffset + 1] * 255) | 0;
var b = (src[srcOffset + 2] * 255) | 0;
dest[destOffset] = r < 0 ? 0 : r > 255 ? 255 : r;
dest[destOffset + 1] = g < 0 ? 0 : g > 255 ? 255 : g;
dest[destOffset + 2] = b < 0 ? 0 : b > 255 ? 255 : b;
dest[destOffset] = src[srcOffset] * 255;
dest[destOffset + 1] = src[srcOffset + 1] * 255;
dest[destOffset + 2] = src[srcOffset + 2] * 255;
},
getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
if (bits === 8 && alpha01 === 0) {
@ -621,9 +618,9 @@ var DeviceRgbCS = (function DeviceRgbCSClosure() {
var scale = 255 / ((1 << bits) - 1);
var j = srcOffset, q = destOffset;
for (var i = 0; i < count; ++i) {
dest[q++] = (scale * src[j++]) | 0;
dest[q++] = (scale * src[j++]) | 0;
dest[q++] = (scale * src[j++]) | 0;
dest[q++] = scale * src[j++];
dest[q++] = scale * src[j++];
dest[q++] = scale * src[j++];
q += alpha01;
}
},
@ -650,41 +647,39 @@ var DeviceCmykCS = (function DeviceCmykCSClosure() {
// CMYK color conversion using the estimation below:
// f(A, B,.. N) = Acc+Bcm+Ccy+Dck+c+Fmm+Gmy+Hmk+Im+Jyy+Kyk+Ly+Mkk+Nk+255
function convertToRgb(src, srcOffset, srcScale, dest, destOffset) {
var c = src[srcOffset + 0] * srcScale;
var c = src[srcOffset] * srcScale;
var m = src[srcOffset + 1] * srcScale;
var y = src[srcOffset + 2] * srcScale;
var k = src[srcOffset + 3] * srcScale;
var r =
(c * (-4.387332384609988 * c + 54.48615194189176 * m +
18.82290502165302 * y + 212.25662451639585 * k +
-285.2331026137004) +
m * (1.7149763477362134 * m - 5.6096736904047315 * y +
-17.873870861415444 * k - 5.497006427196366) +
y * (-2.5217340131683033 * y - 21.248923337353073 * k +
17.5119270841813) +
k * (-21.86122147463605 * k - 189.48180835922747) + 255) | 0;
var g =
(c * (8.841041422036149 * c + 60.118027045597366 * m +
6.871425592049007 * y + 31.159100130055922 * k +
-79.2970844816548) +
m * (-15.310361306967817 * m + 17.575251261109482 * y +
131.35250912493976 * k - 190.9453302588951) +
y * (4.444339102852739 * y + 9.8632861493405 * k - 24.86741582555878) +
k * (-20.737325471181034 * k - 187.80453709719578) + 255) | 0;
var b =
(c * (0.8842522430003296 * c + 8.078677503112928 * m +
30.89978309703729 * y - 0.23883238689178934 * k +
-14.183576799673286) +
m * (10.49593273432072 * m + 63.02378494754052 * y +
50.606957656360734 * k - 112.23884253719248) +
y * (0.03296041114873217 * y + 115.60384449646641 * k +
-193.58209356861505) +
k * (-22.33816807309886 * k - 180.12613974708367) + 255) | 0;
dest[destOffset] = 255 +
c * (-4.387332384609988 * c + 54.48615194189176 * m +
18.82290502165302 * y + 212.25662451639585 * k +
-285.2331026137004) +
m * (1.7149763477362134 * m - 5.6096736904047315 * y +
-17.873870861415444 * k - 5.497006427196366) +
y * (-2.5217340131683033 * y - 21.248923337353073 * k +
17.5119270841813) +
k * (-21.86122147463605 * k - 189.48180835922747);
dest[destOffset] = r > 255 ? 255 : r < 0 ? 0 : r;
dest[destOffset + 1] = g > 255 ? 255 : g < 0 ? 0 : g;
dest[destOffset + 2] = b > 255 ? 255 : b < 0 ? 0 : b;
dest[destOffset + 1] = 255 +
c * (8.841041422036149 * c + 60.118027045597366 * m +
6.871425592049007 * y + 31.159100130055922 * k +
-79.2970844816548) +
m * (-15.310361306967817 * m + 17.575251261109482 * y +
131.35250912493976 * k - 190.9453302588951) +
y * (4.444339102852739 * y + 9.8632861493405 * k - 24.86741582555878) +
k * (-20.737325471181034 * k - 187.80453709719578);
dest[destOffset + 2] = 255 +
c * (0.8842522430003296 * c + 8.078677503112928 * m +
30.89978309703729 * y - 0.23883238689178934 * k +
-14.183576799673286) +
m * (10.49593273432072 * m + 63.02378494754052 * y +
50.606957656360734 * k - 112.23884253719248) +
y * (0.03296041114873217 * y + 115.60384449646641 * k +
-193.58209356861505) +
k * (-22.33816807309886 * k - 180.12613974708367);
}
function DeviceCmykCS() {
@ -782,7 +777,7 @@ var CalGrayCS = (function CalGrayCSClosure() {
var L = cs.YW * AG;
// http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html, Ch 4.
// Convert values to rgb range [0, 255].
var val = Math.max(295.8 * Math.pow(L, 0.333333333333333333) - 40.8, 0) | 0;
let val = Math.max(295.8 * Math.pow(L, 0.333333333333333333) - 40.8, 0);
dest[destOffset] = val;
dest[destOffset + 1] = val;
dest[destOffset + 2] = val;
@ -1071,14 +1066,10 @@ var CalRGBCS = (function CalRGBCSClosure() {
var SRGB = tempConvertMatrix1;
matrixProduct(SRGB_D65_XYZ_TO_RGB_MATRIX, XYZ_D65, SRGB);
var sR = sRGBTransferFunction(SRGB[0]);
var sG = sRGBTransferFunction(SRGB[1]);
var sB = sRGBTransferFunction(SRGB[2]);
// Convert the values to rgb range [0, 255].
dest[destOffset] = Math.round(sR * 255);
dest[destOffset + 1] = Math.round(sG * 255);
dest[destOffset + 2] = Math.round(sB * 255);
dest[destOffset] = sRGBTransferFunction(SRGB[0]) * 255;
dest[destOffset + 1] = sRGBTransferFunction(SRGB[1]) * 255;
dest[destOffset + 2] = sRGBTransferFunction(SRGB[2]) * 255;
}
CalRGBCS.prototype = {
@ -1218,10 +1209,10 @@ var LabCS = (function LabCSClosure() {
g = X * -0.9689 + Y * 1.8758 + Z * 0.0415;
b = X * 0.0557 + Y * -0.2040 + Z * 1.0570;
}
// clamp color values to [0,1] range then convert to [0,255] range.
dest[destOffset] = r <= 0 ? 0 : r >= 1 ? 255 : Math.sqrt(r) * 255 | 0;
dest[destOffset + 1] = g <= 0 ? 0 : g >= 1 ? 255 : Math.sqrt(g) * 255 | 0;
dest[destOffset + 2] = b <= 0 ? 0 : b >= 1 ? 255 : Math.sqrt(b) * 255 | 0;
// Convert the color values to the [0,255] range (clamping is automatic).
dest[destOffset] = Math.sqrt(r) * 255;
dest[destOffset + 1] = Math.sqrt(g) * 255;
dest[destOffset + 2] = Math.sqrt(b) * 255;
}
LabCS.prototype = {

View file

@ -377,7 +377,8 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
var width = dict.get('Width', 'W');
var height = dict.get('Height', 'H');
var bitStrideLength = (width + 7) >> 3;
var imgArray = image.getBytes(bitStrideLength * height);
var imgArray = image.getBytes(bitStrideLength * height,
/* forceClamped = */ true);
var decode = dict.getArray('Decode', 'D');
imgData = PDFImage.createMask({

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;

View file

@ -114,7 +114,7 @@ var Catalog = (function CatalogClosure() {
// To avoid recursion, keep track of the already processed items.
var processed = new RefSet();
processed.put(obj);
var xref = this.xref, blackColor = new Uint8Array(3);
var xref = this.xref, blackColor = new Uint8ClampedArray(3);
while (queue.length > 0) {
var i = queue.shift();

View file

@ -671,7 +671,7 @@ var PDFDocumentProxy = (function PDFDocumentProxyClosure() {
* title: string,
* bold: boolean,
* italic: boolean,
* color: rgb Uint8Array,
* color: rgb Uint8ClampedArray,
* dest: dest obj,
* url: string,
* items: array of more items like this