From 2d91824d0ca83d0a5f6cf03cc10c51c41cef19f4 Mon Sep 17 00:00:00 2001 From: Luke Page Date: Sat, 1 Aug 2015 22:53:29 +0100 Subject: [PATCH] Support for 16 bit --- lib/bitmapper.js | 31 +++++++++++++++++++++---------- lib/filter.js | 2 +- lib/parser.js | 10 ++-------- test/test.js | 3 +-- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/lib/bitmapper.js b/lib/bitmapper.js index 924dada..af6dc98 100644 --- a/lib/bitmapper.js +++ b/lib/bitmapper.js @@ -9,9 +9,12 @@ function bitRetriever(data, depth) { default: throw new Error("unrecognised depth"); break; -/* case 8: + case 16: + var byte2 = data[i]; + i++; + byte = Math.floor((((byte << 8) + byte2) * 255) / (Math.pow(2, 16) - 1) + 0.5); leftOver.push(byte); - break;*/ + break; case 4: var byte2 = byte & 0x0f; var byte1 = byte >> 4; @@ -39,32 +42,32 @@ function bitRetriever(data, depth) { } return { get: function(count) { - var returner; - if (depth === 8) { - returner = data.slice(i, i + count); - i += count; - return returner; - } while(leftOver.length < count) { split(); } - returner = leftOver.slice(0, count); + var returner = leftOver.slice(0, count); leftOver = leftOver.slice(count); return returner; }, resetAfterLine: function() { leftOver.length = 0; + }, + end: function() { + if (i !== data.length) { + throw new Error("extra data found"); + } } }; } exports.dataToBitMap = function(data, width, height, bpp, depth) { + if (depth !== 8) { var bits = bitRetriever(data, depth); } var pxData = new Buffer(width * height * 4); var pxPos = 0; - var maxBit = Math.pow(2, depth) - 1; + var maxBit = depth >= 8 ? 255 : (Math.pow(2, depth) - 1); var rawPos = 0; var pixelData; @@ -82,12 +85,20 @@ exports.dataToBitMap = function(data, width, height, bpp, depth) { } pxPos++; } + //console.log("R", pxData[pxPos - 4], "G", pxData[pxPos - 3], "B", pxData[pxPos - 2], "A", pxData[pxPos - 1]); rawPos += bpp; } if (depth !== 8) { bits.resetAfterLine(); } } + if (depth === 8) { + if (rawPos !== data.length) { + throw new Error("extra data found"); + } + } else { + bits.end(); + } return pxData; }; diff --git a/lib/filter.js b/lib/filter.js index 15077f4..e5a599d 100755 --- a/lib/filter.js +++ b/lib/filter.js @@ -68,7 +68,7 @@ Filter.prototype._reverseFilterLine = function(rawData) { var filter = rawData[0]; - var xComparison = this._depth === 8 ? this._Bpp : 1; + var xComparison = this._depth >= 8 ? ((this._depth === 16) ? this._Bpp * 2 : this._Bpp) : 1; var xBiggerThan = xComparison - 1; for (var x = 0; x < this._byteWidth; x++) { diff --git a/lib/parser.js b/lib/parser.js index ac138ef..a20d14c 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -164,7 +164,7 @@ Parser.prototype._parseIHDR = function(data) { // 'compr', compr, 'filter', filter, 'interlace', interlace // ); - if (depth !== 8 && depth !== 4 && depth !== 2 && depth !== 1) { + if (depth !== 8 && depth !== 4 && depth !== 2 && depth !== 1 && depth !== 16) { this.error(new Error('Unsupported bit depth ' + depth)); return; } @@ -312,17 +312,11 @@ Parser.prototype.reverseFiltered = function(data, depth, width, height) { var pxPos = pxRowPos + (x << 2), color = this._palette[data[pxPos]]; - if (!color) { - console.error("data - " + data[pxPos] + " got no colour"); - console.log("depth is ", depth); - return; - } - for (var i = 0; i < 4; i++) data[pxPos + i] = color[i]; } } - } else if (depth !== 8) { + } else if (depth < 8) { //console.log("adjusting"); var pxLineLength = width << 2; var maxOutSample = 255; diff --git a/test/test.js b/test/test.js index dadd8d9..531d73e 100644 --- a/test/test.js +++ b/test/test.js @@ -12,8 +12,7 @@ fs.readdir(__dirname + '/in/', function(err, files) { var expectedError = false; if (file.match(/^x/) || - file.match(/^...i/) ||// interlace - file.match(/^......(16)/) // 1/2/4/16 bit + file.match(/^...i/) // interlace ) { expectedError = true; }