mirror of
https://github.com/danbulant/pngjs
synced 2026-06-19 22:41:58 +00:00
Support for 16 bit
This commit is contained in:
parent
d601f60c79
commit
2d91824d0c
4 changed files with 25 additions and 21 deletions
|
|
@ -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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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++) {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue