|
|
@ -68,6 +68,39 @@ var PDFImage = (function PDFImageClosure() { |
|
|
|
return (value < 0 ? 0 : (value > max ? max : value)); |
|
|
|
return (value < 0 ? 0 : (value > max ? max : value)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Resizes an image mask with 1 component. |
|
|
|
|
|
|
|
* @param {TypedArray} src - The source buffer. |
|
|
|
|
|
|
|
* @param {Number} bpc - Number of bits per component. |
|
|
|
|
|
|
|
* @param {Number} w1 - Original width. |
|
|
|
|
|
|
|
* @param {Number} h1 - Original height. |
|
|
|
|
|
|
|
* @param {Number} w2 - New width. |
|
|
|
|
|
|
|
* @param {Number} h2 - New height. |
|
|
|
|
|
|
|
* @returns {TypedArray} The resized image mask buffer. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
function resizeImageMask(src, bpc, w1, h1, w2, h2) { |
|
|
|
|
|
|
|
var length = w2 * h2; |
|
|
|
|
|
|
|
var dest = (bpc <= 8 ? new Uint8Array(length) : |
|
|
|
|
|
|
|
(bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length))); |
|
|
|
|
|
|
|
var xRatio = w1 / w2; |
|
|
|
|
|
|
|
var yRatio = h1 / h2; |
|
|
|
|
|
|
|
var i, j, py, newIndex = 0, oldIndex; |
|
|
|
|
|
|
|
var xScaled = new Uint16Array(w2); |
|
|
|
|
|
|
|
var w1Scanline = w1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < w2; i++) { |
|
|
|
|
|
|
|
xScaled[i] = Math.floor(i * xRatio); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (i = 0; i < h2; i++) { |
|
|
|
|
|
|
|
py = Math.floor(i * yRatio) * w1Scanline; |
|
|
|
|
|
|
|
for (j = 0; j < w2; j++) { |
|
|
|
|
|
|
|
oldIndex = py + xScaled[j]; |
|
|
|
|
|
|
|
dest[newIndex++] = src[oldIndex]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return dest; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function PDFImage(xref, res, image, inline, smask, mask, isMask) { |
|
|
|
function PDFImage(xref, res, image, inline, smask, mask, isMask) { |
|
|
|
this.image = image; |
|
|
|
this.image = image; |
|
|
|
var dict = image.dict; |
|
|
|
var dict = image.dict; |
|
|
@ -209,66 +242,6 @@ var PDFImage = (function PDFImageClosure() { |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Resize an image using the nearest neighbor algorithm. Currently only |
|
|
|
|
|
|
|
* supports one and three component images. |
|
|
|
|
|
|
|
* @param {TypedArray} pixels The original image with one component. |
|
|
|
|
|
|
|
* @param {Number} bpc Number of bits per component. |
|
|
|
|
|
|
|
* @param {Number} components Number of color components, 1 or 3 is supported. |
|
|
|
|
|
|
|
* @param {Number} w1 Original width. |
|
|
|
|
|
|
|
* @param {Number} h1 Original height. |
|
|
|
|
|
|
|
* @param {Number} w2 New width. |
|
|
|
|
|
|
|
* @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. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
PDFImage.resize = function PDFImage_resize(pixels, bpc, components, |
|
|
|
|
|
|
|
w1, h1, w2, h2, dest, alpha01) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (components !== 1 && components !== 3) { |
|
|
|
|
|
|
|
error('Unsupported component count for resizing.'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var length = w2 * h2 * components; |
|
|
|
|
|
|
|
var temp = dest ? dest : (bpc <= 8 ? new Uint8Array(length) : |
|
|
|
|
|
|
|
(bpc <= 16 ? new Uint16Array(length) : new Uint32Array(length))); |
|
|
|
|
|
|
|
var xRatio = w1 / w2; |
|
|
|
|
|
|
|
var yRatio = h1 / h2; |
|
|
|
|
|
|
|
var i, j, py, newIndex = 0, oldIndex; |
|
|
|
|
|
|
|
var xScaled = new Uint16Array(w2); |
|
|
|
|
|
|
|
var w1Scanline = w1 * components; |
|
|
|
|
|
|
|
if (alpha01 !== 1) { |
|
|
|
|
|
|
|
alpha01 = 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (j = 0; j < w2; j++) { |
|
|
|
|
|
|
|
xScaled[j] = Math.floor(j * xRatio) * components; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (components === 1) { |
|
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
|
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++]; |
|
|
|
|
|
|
|
temp[newIndex++] = pixels[oldIndex++]; |
|
|
|
|
|
|
|
temp[newIndex++] = pixels[oldIndex++]; |
|
|
|
|
|
|
|
newIndex += alpha01; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return temp; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PDFImage.createMask = |
|
|
|
PDFImage.createMask = |
|
|
|
function PDFImage_createMask(imgArray, width, height, |
|
|
|
function PDFImage_createMask(imgArray, width, height, |
|
|
|
imageIsFromDecodeStream, inverseDecode) { |
|
|
|
imageIsFromDecodeStream, inverseDecode) { |
|
|
@ -439,8 +412,8 @@ var PDFImage = (function PDFImageClosure() { |
|
|
|
alphaBuf = new Uint8Array(sw * sh); |
|
|
|
alphaBuf = new Uint8Array(sw * sh); |
|
|
|
smask.fillGrayBuffer(alphaBuf); |
|
|
|
smask.fillGrayBuffer(alphaBuf); |
|
|
|
if (sw !== width || sh !== height) { |
|
|
|
if (sw !== width || sh !== height) { |
|
|
|
alphaBuf = PDFImage.resize(alphaBuf, smask.bpc, 1, sw, sh, width, |
|
|
|
alphaBuf = resizeImageMask(alphaBuf, smask.bpc, sw, sh, |
|
|
|
height); |
|
|
|
width, height); |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (mask) { |
|
|
|
} else if (mask) { |
|
|
|
if (mask instanceof PDFImage) { |
|
|
|
if (mask instanceof PDFImage) { |
|
|
@ -456,8 +429,8 @@ var PDFImage = (function PDFImageClosure() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (sw !== width || sh !== height) { |
|
|
|
if (sw !== width || sh !== height) { |
|
|
|
alphaBuf = PDFImage.resize(alphaBuf, mask.bpc, 1, sw, sh, width, |
|
|
|
alphaBuf = resizeImageMask(alphaBuf, mask.bpc, sw, sh, |
|
|
|
height); |
|
|
|
width, height); |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (isArray(mask)) { |
|
|
|
} else if (isArray(mask)) { |
|
|
|
// Color key mask: if any of the compontents are outside the range
|
|
|
|
// Color key mask: if any of the compontents are outside the range
|
|
|
@ -693,7 +666,4 @@ var PDFImage = (function PDFImageClosure() { |
|
|
|
})(); |
|
|
|
})(); |
|
|
|
|
|
|
|
|
|
|
|
exports.PDFImage = PDFImage; |
|
|
|
exports.PDFImage = PDFImage; |
|
|
|
|
|
|
|
|
|
|
|
// TODO refactor to remove dependency on colorspace.js
|
|
|
|
|
|
|
|
coreColorSpace._setCoreImage(exports); |
|
|
|
|
|
|
|
})); |
|
|
|
})); |
|
|
|