New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

pngjs-image

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pngjs-image - npm Package Compare versions

Comparing version 0.11.5 to 0.11.6

4

CHANGELOG.md
CHANGELOG
=========
v0.11.6 - 06/05/15
* Speed improvements
* Convert processor arrays to internal 16-bit buffers
v0.11.5 - 05/29/15

@@ -5,0 +9,0 @@ * Various Bug-fixes for encoder and decoder

8

lib/png/chunk.js

@@ -162,10 +162,10 @@ // Copyright 2015 Yahoo! Inc.

* @method decode
* @param {int[]} values
* @param {Buffer} image
* @param {boolean} strict Should parsing be strict?
* @param {object} options Decoding options
* @return {int[]}
* @return {Buffer}
*/
Chunk.prototype.decode = function (values, strict, options) {
Chunk.prototype.decode = function (image, strict, options) {
// Do nothing by default
return values;
return image;
};

@@ -172,0 +172,0 @@

@@ -482,8 +482,8 @@ // Copyright 2015 Yahoo! Inc.

* @method decode
* @param {int[]} values
* @param {Buffer} image
* @param {boolean} strict Should parsing be strict?
* @param {object} options Decoding options
* @return {int[]}
* @return {Buffer}
*/
decode: function (values, strict, options) {
decode: function (image, strict, options) {

@@ -494,26 +494,25 @@ var compressor,

normalizer,
localValues,
image;
localImage;
// Combine all data chunks
image = this._combine();
localImage = this._combine();
// Decompress
compressor = new Compressor();
image = compressor.decompress(image);
localImage = compressor.decompress(localImage);
// Run through filters
filter = new Filter(this);
image = filter.reverse(image);
localImage = filter.reverse(localImage);
// Parses scanlines
parser = new Parser(this);
localValues = parser.decode(image);
localImage = parser.decode(localImage);
// Normalizes color values
normalizer = new Normalizer(this);
localValues = normalizer.decode(localValues);
localImage = normalizer.decode(localImage);
// Ignoring the incoming values - this is the first chunk creating these
return localValues;
return localImage;
},

@@ -528,3 +527,3 @@

* @method postDecode
* @param {int[]} values
* @param {Buffer} image
* @param {boolean} strict Should parsing be strict?

@@ -534,14 +533,14 @@ * @param {object} options Decoding options

*/
postDecode: function (values, strict, options) {
postDecode: function (image, strict, options) {
var scaler, interlace, image;
var scaler, interlace, localImage = image;
scaler = new Scaler(this);
image = scaler.decode(values);
localImage = scaler.decode(localImage);
// Run through interlace method
interlace = new Interlace(this);
image = interlace.reverse(image);
localImage = interlace.reverse(localImage);
return image;
return localImage;
},

@@ -548,0 +547,0 @@

@@ -184,9 +184,9 @@ // Copyright 2015 Yahoo! Inc.

* @method decode
* @param {int[]} values
* @param {Buffer} image
* @param {boolean} strict Should parsing be strict?
* @param {object} options Decoding options
* @param {boolean} [options.transparent=false] Apply transparency?
* @return {int[]}
* @return {Buffer}
*/
decode: function (values, strict, options) {
decode: function (image, strict, options) {

@@ -209,20 +209,17 @@ var i, len, index,

for (i = 0, len = values.length; i < len; i += 4) {
for (i = 0, len = image.length; i < len; i += 8) {
if (isIndexed && colors[values[i]]) { // Indexed-color with entry in tRNS table
index = image.readUInt16BE(i, false);
if (isIndexed) {
index = values[i];
alpha = colors[index].alpha;
if (alpha === undefined) {
alpha = values[i + 3]
if (alpha !== undefined) {
image.writeUInt16BE(alpha, i + 6);
}
values[i + 3] = alpha;
} else { // All other types
r = values[i];
g = values[i + 1];
b = values[i + 2];
r = index;
g = image.readUInt16BE(i + 2, false);
b = image.readUInt16BE(i + 4, false);

@@ -232,3 +229,3 @@ alpha = this.findAlpha(r, g, b);

if (alpha !== undefined) {
values[i + 3] = alpha;
image.writeUInt16BE(alpha, i + 6);
}

@@ -239,3 +236,3 @@ }

return values;
return image;
},

@@ -242,0 +239,0 @@

// Copyright 2015 Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.
var BufferedStream = require('../utils/bufferedStream');
/**

@@ -102,3 +104,3 @@ * @class Normalizer

valuesNeeded: 1,
writer: this._writeInIndexedBytes.bind(this)
writer: '_writeInIndexedBytes'
};

@@ -112,3 +114,3 @@

valuesNeeded: 4,
writer: this._writeInColorAlphaBytes.bind(this)
writer: '_writeInColorAlphaBytes'
};

@@ -118,3 +120,3 @@ } else {

valuesNeeded: 3,
writer: this._writeInColorNoAlphaBytes.bind(this)
writer: '_writeInColorNoAlphaBytes'
};

@@ -126,3 +128,3 @@ }

valuesNeeded: 2,
writer: this._writeInNoColorAlphaBytes.bind(this)
writer: '_writeInNoColorAlphaBytes'
};

@@ -132,3 +134,3 @@ } else {

valuesNeeded: 1,
writer: this._writeInNoColorNoAlphaBytes.bind(this)
writer: '_writeInNoColorNoAlphaBytes'
};

@@ -146,21 +148,33 @@ }

* @method decode
* @param {int[]} values
* @return {int[]}
* @param {Buffer} image
* @return {Buffer}
*/
Normalizer.prototype.decode = function (values) {
Normalizer.prototype.decode = function (image) {
var output = [],
writerInfo,
currentValues,
maxValue;
var writerInfo,
maxValue,
offset = 0,
len = image.length,
internalImageSize = this.getHeaderChunk().getImageSizeInBytes() * 2, // 16-bit
imageStream, outputStream;
writerInfo = this._determineWriter();
// Skip normalization since nothing needs to be done here.
if (writerInfo.valuesNeeded === 4) {
return image;
}
maxValue = this._determineMaxValue();
while (values.length >= writerInfo.valuesNeeded) {
currentValues = values.splice(0, writerInfo.valuesNeeded);
writerInfo.writer(currentValues, output, maxValue);
imageStream = new BufferedStream(image, false);
outputStream = new BufferedStream(null, null, internalImageSize);
while (len > offset) {
this[writerInfo.writer](imageStream, outputStream, maxValue);
offset += writerInfo.valuesNeeded * 2;
}
return output;
return outputStream.toBuffer();
};

@@ -173,11 +187,11 @@

* @method _writeInColorAlphaBytes
* @param {int[]} bytes Bytes that should be save to stream
* @param {int[]} output Output list
* @param {BufferedStream} input Input stream
* @param {BufferedStream} output Output stream
* @private
*/
Normalizer.prototype._writeInColorAlphaBytes = function (bytes, output) {
output.push(bytes[0]);
output.push(bytes[1]);
output.push(bytes[2]);
output.push(bytes[3]);
Normalizer.prototype._writeInColorAlphaBytes = function (input, output) {
output.writeUInt16BE(input.readUInt16BE());
output.writeUInt16BE(input.readUInt16BE());
output.writeUInt16BE(input.readUInt16BE());
output.writeUInt16BE(input.readUInt16BE());
};

@@ -189,12 +203,12 @@

* @method _writeInColorNoAlphaBytes
* @param {int[]} bytes Bytes that should be save to stream
* @param {int[]} output Output list
* @param {BufferedStream} input Input stream
* @param {BufferedStream} output Output stream
* @param {int} maxValue Max value for bit-depth
* @private
*/
Normalizer.prototype._writeInColorNoAlphaBytes = function (bytes, output, maxValue) {
output.push(bytes[0]);
output.push(bytes[1]);
output.push(bytes[2]);
output.push(maxValue);
Normalizer.prototype._writeInColorNoAlphaBytes = function (input, output, maxValue) {
output.writeUInt16BE(input.readUInt16BE());
output.writeUInt16BE(input.readUInt16BE());
output.writeUInt16BE(input.readUInt16BE());
output.writeUInt16BE(maxValue);
};

@@ -206,11 +220,12 @@

* @method _writeInNoColorAlphaBytes
* @param {int[]} bytes Bytes that should be save to stream
* @param {int[]} output Output list
* @param {BufferedStream} input Input stream
* @param {BufferedStream} output Output stream
* @private
*/
Normalizer.prototype._writeInNoColorAlphaBytes = function (bytes, output) {
output.push(bytes[0]);
output.push(bytes[0]);
output.push(bytes[0]);
output.push(bytes[1]);
Normalizer.prototype._writeInNoColorAlphaBytes = function (input, output) {
var value = input.readUInt16BE();
output.writeUInt16BE(value);
output.writeUInt16BE(value);
output.writeUInt16BE(value);
output.writeUInt16BE(input.readUInt16BE());
};

@@ -222,12 +237,13 @@

* @method _writeInNoColorNoAlphaBytes
* @param {int[]} bytes Bytes that should be save to stream
* @param {int[]} output Output list
* @param {BufferedStream} input Input stream
* @param {BufferedStream} output Output stream
* @param {int} maxValue Max value for bit-depth
* @private
*/
Normalizer.prototype._writeInNoColorNoAlphaBytes = function (bytes, output, maxValue) {
output.push(bytes[0]);
output.push(bytes[0]);
output.push(bytes[0]);
output.push(maxValue);
Normalizer.prototype._writeInNoColorNoAlphaBytes = function (input, output, maxValue) {
var value = input.readUInt16BE();
output.writeUInt16BE(value);
output.writeUInt16BE(value);
output.writeUInt16BE(value);
output.writeUInt16BE(maxValue);
};

@@ -239,14 +255,15 @@

* @method _writeInIndexedBytes
* @param {int[]} bytes Bytes that should be save to stream
* @param {int[]} output Output list
* @param {BufferedStream} input Input stream
* @param {BufferedStream} output Output stream
* @param {int} maxValue Max value for bit-depth
* @private
*/
Normalizer.prototype._writeInIndexedBytes = function (bytes, output, maxValue) {
output.push(bytes[0]);
output.push(bytes[0]);
output.push(bytes[0]);
output.push(maxValue);
Normalizer.prototype._writeInIndexedBytes = function (input, output, maxValue) {
var value = input.readUInt16BE();
output.writeUInt16BE(value);
output.writeUInt16BE(value);
output.writeUInt16BE(value);
output.writeUInt16BE(maxValue);
};
module.exports = Normalizer;

@@ -34,3 +34,3 @@ // Copyright 2015 Yahoo! Inc.

* @param {Buffer} image
* @return {int[]}
* @return {Buffer}
*/

@@ -53,27 +53,23 @@ Parser.prototype.encoder = function (image) {

var headerChunk = this.getHeaderChunk(),
bitDepth,
result = null;
bitDepth = headerChunk.getBitDepth();
switch (bitDepth) {
switch (headerChunk.getBitDepth()) {
case 1:
result = this._parse1bit;
result = '_parse1bit';
break;
case 2:
result = this._parse2bit;
result = '_parse2bit';
break;
case 4:
result = this._parse4bit;
result = '_parse4bit';
break;
case 8:
result = this._parse8bit;
result = '_parse8bit';
break;
case 16:
result = this._parse16bit;
result = '_parse16bit';
break;

@@ -90,3 +86,3 @@ }

* @param {Buffer} image
* @return {int[]} Values read
* @return {Buffer} 16-bit image
*/

@@ -98,26 +94,38 @@ Parser.prototype.decode = function (image) {

imageStream, scaledStream,
imageStream, outputStream,
paddingAt,
parserFactory,
values = [];
bitDepth = headerChunk.getBitDepth(),
internalImageSize = headerChunk.getImageSizeInBytes() * 2, // 16-bit
parser;
imageStream = new BufferedStream(image, false);
outputStream = new BufferedStream(null, null, internalImageSize);
parserFactory = this._determineParserFactory();
parser = this._determineParserFactory();
interlace.processPasses(function (width, height, scanLineLength) {
var i,
paddingAt,
length,
parserFn;
length = scanLineLength * height;
paddingAt = headerChunk.scanLineWithWidthPaddingAt(width);
parserFn = this[parser](paddingAt);
this._passDecode(
imageStream, scaledStream,
scanLineLength * height,
parserFactory(paddingAt),
values
);
if (bitDepth === 16) {
for(i = 0; i < length; i += 2) {
parserFn(imageStream.readUInt16BE(), outputStream);
}
} else {
for(i = 0; i < length; i += 1) {
parserFn(imageStream.readUInt8(), outputStream);
}
}
}.bind(this));
return values;
return outputStream.toBuffer();
};

@@ -137,22 +145,37 @@

return function (value, values) {
return function (value, output) {
values.push((value >> 7) & 1);
values.push((value >> 6) & 1);
values.push((value >> 5) & 1);
values.push((value >> 4) & 1);
values.push((value >> 3) & 1);
values.push((value >> 2) & 1);
values.push((value >> 1) & 1);
values.push(value & 1);
output.writeUInt16BE((value >> 7) & 1); byteCounter++;
if (!paddingAt || byteCounter < paddingAt) {
output.writeUInt16BE((value >> 6) & 1);
byteCounter++;
}
if (!paddingAt || byteCounter < paddingAt) {
output.writeUInt16BE((value >> 5) & 1);
byteCounter++;
}
if (!paddingAt || byteCounter < paddingAt) {
output.writeUInt16BE((value >> 4) & 1);
byteCounter++;
}
if (!paddingAt || byteCounter < paddingAt) {
output.writeUInt16BE((value >> 3) & 1);
byteCounter++;
}
if (!paddingAt || byteCounter < paddingAt) {
output.writeUInt16BE((value >> 2) & 1);
byteCounter++;
}
if (!paddingAt || byteCounter < paddingAt) {
output.writeUInt16BE((value >> 1) & 1);
byteCounter++;
}
if (!paddingAt || byteCounter < paddingAt) {
output.writeUInt16BE(value & 1);
byteCounter++;
}
// Make sure that padding is removed
if (paddingAt) {
byteCounter += 8;
if (byteCounter >= paddingAt) {
if (byteCounter > paddingAt) {
values.splice(-(byteCounter - paddingAt));
}
byteCounter = 0;
}
if (paddingAt && byteCounter >= paddingAt) {
byteCounter = 0;
}

@@ -174,20 +197,22 @@ };

return function (value, values) {
return function (value, output) {
values.push((value >> 6) & 3);
values.push((value >> 4) & 3);
values.push((value >> 2) & 3);
values.push(value & 3);
output.writeUInt16BE((value >> 6) & 3); byteCounter++;
if (!paddingAt || byteCounter < paddingAt) {
output.writeUInt16BE((value >> 4) & 3);
byteCounter++;
}
if (!paddingAt || byteCounter < paddingAt) {
output.writeUInt16BE((value >> 2) & 3);
byteCounter++;
}
if (!paddingAt || byteCounter < paddingAt) {
output.writeUInt16BE(value & 3);
byteCounter++;
}
// Make sure that padding is removed
if (paddingAt) {
byteCounter += 4;
if (byteCounter >= paddingAt) {
if (byteCounter > paddingAt) {
values.splice(-(byteCounter - paddingAt));
}
byteCounter = 0;
}
if (paddingAt && byteCounter >= paddingAt) {
byteCounter = 0;
}
};

@@ -208,18 +233,14 @@ };

return function (value, values) {
return function (value, output) {
values.push((value >> 4) & 15);
values.push(value & 15);
output.writeUInt16BE((value >> 4) & 15); byteCounter++;
if (!paddingAt || byteCounter < paddingAt) {
output.writeUInt16BE(value & 15);
byteCounter++;
}
// Make sure that padding is removed
if (paddingAt) {
byteCounter += 2;
if (byteCounter >= paddingAt) {
if (byteCounter > paddingAt) {
values.splice(-(byteCounter - paddingAt));
}
byteCounter = 0;
}
if (paddingAt && byteCounter >= paddingAt) {
byteCounter = 0;
}
};

@@ -236,4 +257,4 @@ };

Parser.prototype._parse8bit = function () {
return function (value, values) {
values.push(value);
return function (value, output) {
output.writeUInt16BE(value);
};

@@ -250,50 +271,7 @@ };

Parser.prototype._parse16bit = function () {
return function (value, values) {
values.push(value);
return function (value, output) {
output.writeUInt16BE(value);
};
};
/**
* Decodes one pass
*
* @method _passDecode
* @param {BufferedStream} input Input stream
* @param {BufferedStream} output Output stream
* @param {int} length Number of values to read
* @param {function} pixelParser Function that will be called to parse pixel from the stream
* @param {int[]} values Values read
*/
Parser.prototype._passDecode = function (
input, output,
length,
pixelParser,
values
) {
var i,
value,
increment = 1,
headerChunk,
twoBytes;
headerChunk = this.getHeaderChunk();
twoBytes = headerChunk.getBitDepth() === 16;
if (twoBytes) {
increment = 2;
}
for(i = 0; i < length; i += increment) {
if (twoBytes) {
value = input.readUInt16BE();
} else {
value = input.readUInt8();
}
pixelParser(value, values);
}
};
module.exports = Parser;

@@ -142,44 +142,19 @@ // Copyright 2015 Yahoo! Inc.

case 1:
result = {
scaleFactor: isIndexed ? 1 : Scaler.SCALE_FACTOR_1_TO_8_BIT,
scaler: function (v) {
return v * result.scaleFactor;
}
};
result = isIndexed ? 1 : Scaler.SCALE_FACTOR_1_TO_8_BIT;
break;
case 2:
result = {
scaleFactor: isIndexed ? 1 : Scaler.SCALE_FACTOR_2_TO_8_BIT,
scaler: function (v) {
return v * result.scaleFactor;
}
};
result = isIndexed ? 1 : Scaler.SCALE_FACTOR_2_TO_8_BIT;
break;
case 4:
result = {
scaleFactor: isIndexed ? 1 : Scaler.SCALE_FACTOR_4_TO_8_BIT,
scaler: function (v) {
return v * result.scaleFactor;
}
};
result = isIndexed ? 1 : Scaler.SCALE_FACTOR_4_TO_8_BIT;
break;
case 8:
result = {
scaleFactor: isIndexed ? 1 : Scaler.SCALE_FACTOR_8_TO_8_BIT,
scaler: function (v) {
return v * result.scaleFactor;
}
};
result = isIndexed ? 1 : Scaler.SCALE_FACTOR_8_TO_8_BIT;
break;
case 16:
result = {
scaleFactor: Scaler.SCALE_FACTOR_16_TO_8_BIT,
scaler: function (v) {
return Math.floor((v * result.scaleFactor) + 0.5);
}
};
result = Scaler.SCALE_FACTOR_16_TO_8_BIT;
break;

@@ -195,29 +170,37 @@ }

* @method decode
* @param {int[]} values
* @param {Buffer} image
* @return {Buffer}
*/
Scaler.prototype.decode = function (values) {
Scaler.prototype.decode = function (image) {
var headerChunk = this.getHeaderChunk(),
outputStream,
imageStream, outputStream,
scalerInfo,
currentValues,
scaleFactor,
offset = 0,
is16Bit,
samplesPerPixel = 4,
i, len;
len = image.length, // Since the image is here 16-bit
i;
imageStream = new BufferedStream(image, false);
outputStream = new BufferedStream(null, null, headerChunk.getImageSizeInBytes());
scalerInfo = this._determineScaler();
is16Bit = (this.getHeaderChunk().getBitDepth() == 16);
while (values.length >= samplesPerPixel) {
scaleFactor = this._determineScaler();
currentValues = values.splice(0, samplesPerPixel);
currentValues = currentValues.map(scalerInfo.scaler);
while (len > offset) {
for(i = 0, len = currentValues.length; i < len; i++) {
outputStream.writeUInt8(currentValues[i]);
for(i = 0; i < samplesPerPixel; i++) {
if (is16Bit) {
outputStream.writeUInt8(Math.floor((imageStream.readUInt16BE() * scaleFactor) + 0.5));
} else {
outputStream.writeUInt8(imageStream.readUInt16BE() * scaleFactor);
}
}
offset += samplesPerPixel * 2;
}

@@ -224,0 +207,0 @@

@@ -154,13 +154,13 @@ // Copyright 2015 Yahoo! Inc.

//BufferedStream.prototype.peekUInt16BE = function () {
// return (this._data[this.readOffset] << 8) | this._data[this.readOffset + 1]
//};
//
//BufferedStream.prototype.readUInt16BE = function () {
// var result = this.peekUInt16BE();
// this.readOffset += 2;
// this.readCounter += 2;
// return result;
//};
BufferedStream.prototype.peekUInt16BE = function () {
return (this._data[this.readOffset] << 8) | this._data[this.readOffset + 1]
};
BufferedStream.prototype.readUInt16BE = function () {
var result = (this._data[this.readOffset] << 8) | this._data[this.readOffset + 1];
this.readOffset += 2;
this.readCounter += 2;
return result;
};
BufferedStream.prototype.writeUInt16BE = function (value) {

@@ -167,0 +167,0 @@ this._writeCheck(2);

{
"name": "pngjs-image",
"version": "0.11.5",
"version": "0.11.6",
"description": "JavaScript-based PNG image encoder, decoder, and manipulator",

@@ -5,0 +5,0 @@ "license": "MIT",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc