PDFImage_resize: copy in place with alpha and ~5x speed up
This commit is contained in:
parent
bda1865fb8
commit
8f9bd33a57
@ -198,30 +198,50 @@ var PDFImage = (function PDFImageClosure() {
|
|||||||
* @param {Number} h1 Original height.
|
* @param {Number} h1 Original height.
|
||||||
* @param {Number} w2 New width.
|
* @param {Number} w2 New width.
|
||||||
* @param {Number} h2 New height.
|
* @param {Number} h2 New height.
|
||||||
|
* @param {TypedArray} dest (Optional) The destination buffer.
|
||||||
|
* @param {Number} alpha01 (Optional) Size reserved for the alpha channel.
|
||||||
* @return {TypedArray} Resized image data.
|
* @return {TypedArray} Resized image data.
|
||||||
*/
|
*/
|
||||||
PDFImage.resize = function PDFImage_resize(pixels, bpc, components,
|
PDFImage.resize = function PDFImage_resize(pixels, bpc, components,
|
||||||
w1, h1, w2, h2) {
|
w1, h1, w2, h2, dest, alpha01) {
|
||||||
|
|
||||||
|
if (components !== 1 && components !== 3) {
|
||||||
|
error('Unsupported component count for resizing.');
|
||||||
|
}
|
||||||
|
|
||||||
var length = w2 * h2 * components;
|
var length = w2 * h2 * components;
|
||||||
var temp = (bpc <= 8 ? new Uint8Array(length) :
|
var temp = dest ? dest : (bpc <= 8 ? new Uint8Array(length) :
|
||||||
(bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length)));
|
(bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length)));
|
||||||
var xRatio = w1 / w2;
|
var xRatio = w1 / w2;
|
||||||
var yRatio = h1 / h2;
|
var yRatio = h1 / h2;
|
||||||
var px, py, newIndex, oldIndex;
|
var i, j, py, newIndex = 0, oldIndex;
|
||||||
for (var i = 0; i < h2; i++) {
|
var xScaled = new Uint16Array(w2);
|
||||||
for (var j = 0; j < w2; j++) {
|
var w1Scanline = w1 * components;
|
||||||
px = Math.floor(j * xRatio);
|
if (alpha01 !== 1) {
|
||||||
py = Math.floor(i * yRatio);
|
alpha01 = 0;
|
||||||
newIndex = (i * w2) + j;
|
}
|
||||||
oldIndex = ((py * w1) + px);
|
|
||||||
|
for (j = 0; j < w2; j++) {
|
||||||
|
xScaled[j] = Math.floor(j * xRatio) * components;
|
||||||
|
}
|
||||||
|
|
||||||
if (components === 1) {
|
if (components === 1) {
|
||||||
temp[newIndex] = pixels[oldIndex];
|
for (i = 0; i < h2; i++) {
|
||||||
|
py = Math.floor(i * yRatio) * w1Scanline;
|
||||||
|
for (j = 0; j < w2; j++) {
|
||||||
|
oldIndex = py + xScaled[j];
|
||||||
|
temp[newIndex++] = pixels[oldIndex];
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if (components === 3) {
|
} else if (components === 3) {
|
||||||
newIndex *= 3;
|
for (i = 0; i < h2; i++) {
|
||||||
oldIndex *= 3;
|
py = Math.floor(i * yRatio) * w1Scanline;
|
||||||
temp[newIndex] = pixels[oldIndex];
|
for (j = 0; j < w2; j++) {
|
||||||
temp[newIndex + 1] = pixels[oldIndex + 1];
|
oldIndex = py + xScaled[j];
|
||||||
temp[newIndex + 2] = pixels[oldIndex + 2];
|
temp[newIndex++] = pixels[oldIndex++];
|
||||||
|
temp[newIndex++] = pixels[oldIndex++];
|
||||||
|
temp[newIndex++] = pixels[oldIndex++];
|
||||||
|
newIndex += alpha01;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -145,9 +145,9 @@ var ColorSpace = (function ColorSpaceClosure() {
|
|||||||
|
|
||||||
if (rgbBuf) {
|
if (rgbBuf) {
|
||||||
if (needsResizing) {
|
if (needsResizing) {
|
||||||
rgbBuf = PDFImage.resize(rgbBuf, bpc, 3, originalWidth,
|
PDFImage.resize(rgbBuf, bpc, 3, originalWidth, originalHeight, width,
|
||||||
originalHeight, width, height);
|
height, dest, alpha01);
|
||||||
}
|
} else {
|
||||||
rgbPos = 0;
|
rgbPos = 0;
|
||||||
destPos = 0;
|
destPos = 0;
|
||||||
for (i = 0, ii = width * actualHeight; i < ii; i++) {
|
for (i = 0, ii = width * actualHeight; i < ii; i++) {
|
||||||
@ -157,6 +157,7 @@ var ColorSpace = (function ColorSpaceClosure() {
|
|||||||
destPos += alpha01;
|
destPos += alpha01;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* True if the colorspace has components in the default range of [0, 1].
|
* True if the colorspace has components in the default range of [0, 1].
|
||||||
|
Loading…
Reference in New Issue
Block a user