From 5ad3a9cc72de0fb4bf5a02c0084be5a40742b01c Mon Sep 17 00:00:00 2001 From: Brendan Dahl Date: Wed, 14 Dec 2011 12:27:56 -0800 Subject: [PATCH] Add basic rescale support for smacks. --- src/image.js | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/image.js b/src/image.js index 2f99dee54..d00ae3590 100644 --- a/src/image.js +++ b/src/image.js @@ -127,6 +127,31 @@ var PDFImage = (function PDFImageClosure() { smaskPromise.resolve(null); }; + /** + * Resize an image using the nearest neighbor algorithm. Currently only + * supports one component images. + * @param {TypedArray} pixels The original image with one component. + * @param {Number} w1 Original width. + * @param {Number} h1 Original height. + * @param {Number} w2 New width. + * @param {Number} h2 New height. + * @return {TypedArray} Resized image data. + */ + PDFImage.resize = function resize(pixels, w1, h1, w2, h2) { + var temp = new Uint8Array(w2 * h2); + var xRatio = w1 / w2; + var yRatio = h1 / h2; + var px, py; + for (var i = 0; i < h2; i++) { + for (var j = 0; j < w2; j++) { + px = Math.floor(j * xRatio); + py = Math.floor(i * yRatio); + temp[(i * w2) + j] = pixels[((py * w1) + px)]; + } + } + return temp; + }; + PDFImage.prototype = { getComponents: function getComponents(buffer) { var bpc = this.bpc; @@ -221,18 +246,18 @@ var PDFImage = (function PDFImageClosure() { var smask = this.smask; var width = this.width; var height = this.height; - var buf = new Uint8Array(width * height); + var buf; if (smask) { var sw = smask.width; var sh = smask.height; - if (sw != this.width || sh != this.height) - error('smask dimensions do not match image dimensions: ' + sw + - ' != ' + this.width + ', ' + sh + ' != ' + this.height); - + buf = new Uint8Array(sw * sh) smask.fillGrayBuffer(buf); + if (sw != this.width || sh != this.height) + buf = PDFImage.resize(buf, sw, sh, this.width, this.height); return buf; } else { + buf = new Uint8Array(width * height) for (var i = 0, ii = width * height; i < ii; ++i) buf[i] = 255; }