From 5b6c06d5c6037894554b60bdc3bd71634414e74a Mon Sep 17 00:00:00 2001 From: notmasteryet Date: Fri, 13 Jan 2012 18:24:02 -0600 Subject: [PATCH] Address review comments --- src/stream.js | 96 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 63 insertions(+), 33 deletions(-) diff --git a/src/stream.js b/src/stream.js index e0fac72e5..cff83357a 100644 --- a/src/stream.js +++ b/src/stream.js @@ -835,7 +835,7 @@ var JpegStream = (function JpegStreamClosure() { return bytesToString(this.bytes); }; JpegStream.prototype.getChar = function jpegStreamGetChar() { - error('internal error: getChar is not valid on JpegStream'); + error('internal error: getChar is not valid on JpegStream'); }; /** * Checks if the image can be decoded and displayed by the browser without any @@ -873,17 +873,17 @@ var JpegStream = (function JpegStreamClosure() { * For JPEG 2000's we use a library to decode these images and * the stream behaves like all the other DecodeStreams. */ -var JpxStream = (function jpxStream() { - function constructor(bytes, dict) { +var JpxStream = (function JpxStreamClosure() { + function JpxStream(bytes, dict) { this.dict = dict; this.bytes = bytes; DecodeStream.call(this); } - constructor.prototype = Object.create(DecodeStream.prototype); + JpxStream.prototype = Object.create(DecodeStream.prototype); - constructor.prototype.ensureBuffer = function jpxStreamEnsureBuffer(req) { + JpxStream.prototype.ensureBuffer = function jpxStreamEnsureBuffer(req) { if (this.bufferLength) return; @@ -899,45 +899,75 @@ var JpxStream = (function jpxStream() { var data = new Uint8Array(width * height * componentsCount); for (var k = 0, kk = jpxImage.tiles.length; k < kk; k++) { - var tile = jpxImage.tiles[k]; - var tileWidth = tile[0].width; - var tileHeight = tile[0].height; - var tileLeft = tile[0].left; - var tileTop = tile[0].top; + var tileCompoments = jpxImage.tiles[k]; + var tileWidth = tileCompoments[0].width; + var tileHeight = tileCompoments[0].height; + var tileLeft = tileCompoments[0].left; + var tileTop = tileCompoments[0].top; - var data0 = tile[0].items; - var data1 = componentsCount > 1 ? tile[1].items : null; - var data2 = componentsCount > 1 ? tile[2].items : null; - var data3 = componentsCount > 3 ? tile[3].items : null; + var dataPosition, sourcePosition, data0, data1, data2, data3, rowFeed; + switch (componentsCount) { + case 1: + data0 = tileCompoments[0].items; - var dataPosition = (width * tileTop + tileLeft) * componentsCount; - var sourcePosition = 0; - for (var j = 0; j < tileHeight; j++) { - for (var i = 0; i < tileWidth; i++) { - data[dataPosition++] = data0[sourcePosition]; - if (componentsCount > 1) { - data[dataPosition++] = data1[sourcePosition]; - data[dataPosition++] = data2[sourcePosition]; - if (componentsCount > 3) - data[dataPosition++] = data3[sourcePosition]; + dataPosition = width * tileTop + tileLeft; + rowFeed = width - tileWidth; + sourcePosition = 0; + for (var j = 0; j < tileHeight; j++) { + for (var i = 0; i < tileWidth; i++) + data[dataPosition++] = data0[sourcePosition++]; + dataPosition += rowFeed; } - sourcePosition++; - } - dataPosition += componentsCount * (width - tileWidth); + break; + case 3: + data0 = tileCompoments[0].items; + data1 = tileCompoments[1].items; + data2 = tileCompoments[2].items; + + dataPosition = (width * tileTop + tileLeft) * 3; + rowFeed = (width - tileWidth) * 3; + sourcePosition = 0; + for (var j = 0; j < tileHeight; j++) { + for (var i = 0; i < tileWidth; i++) { + data[dataPosition++] = data0[sourcePosition]; + data[dataPosition++] = data1[sourcePosition]; + data[dataPosition++] = data2[sourcePosition]; + sourcePosition++; + } + dataPosition += rowFeed; + } + break; + case 4: + data0 = tileCompoments[0].items; + data1 = tileCompoments[1].items; + data2 = tileCompoments[2].items; + data3 = tileCompoments[3].items; + + dataPosition = (width * tileTop + tileLeft) * 4; + rowFeed = (width - tileWidth) * 4; + sourcePosition = 0; + for (var j = 0; j < tileHeight; j++) { + for (var i = 0; i < tileWidth; i++) { + data[dataPosition++] = data0[sourcePosition]; + data[dataPosition++] = data1[sourcePosition]; + data[dataPosition++] = data2[sourcePosition]; + data[dataPosition++] = data3[sourcePosition]; + sourcePosition++; + } + dataPosition += rowFeed; + } + break; } } this.buffer = data; this.bufferLength = data.length; }; - constructor.prototype.getIR = function jpxStreamGetIR() { - return this.src; - }; - constructor.prototype.getChar = function jpxStreamGetChar() { - error('internal error: getChar is not valid on JpxStream'); + JpxStream.prototype.getChar = function jpxStreamGetChar() { + error('internal error: getChar is not valid on JpxStream'); }; - return constructor; + return JpxStream; })(); var DecryptStream = (function DecryptStreamClosure() {