Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Move processors from arrays to 16-bit buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelerz committed Jun 5, 2015
1 parent 96ad9c2 commit 4b1ce0d
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 266 deletions.
21 changes: 9 additions & 12 deletions examples/synchonous.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

var PNGImage = require('../index');

var image = PNGImage.readImageSync(__dirname + '/eye.png');

// Black-out an area
image.fillRect(17, 10, 15, 20, {
red: 0, green: 0, blue: 0, alpha: 255
var fs = require('fs');

//var image = PNGImage.readImageSync(__dirname + '/firefox 37.0 Windows 8.1 1.png');
process.stdin.on('data', function (data) {
console.log('started');
console.time('image');
var image = PNGImage.readImageSync(__dirname + '/firefox 37.0 Windows 8.1 1.png');
console.timeEnd('image');
console.log('Done');
});

console.log('Modified!');

image.writeImageSync(__dirname + '/eye_output.png');

console.log('Done');
8 changes: 4 additions & 4 deletions lib/png/chunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ Chunk.prototype.parse = function (stream, length, strict, options) {
* applying of changes before the image is scaled.
*
* @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;
};

/**
Expand Down
33 changes: 16 additions & 17 deletions lib/png/chunks/IHDR.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,41 +479,40 @@ module.exports = {
* Phase 2
*
* @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) {

var compressor,
filter,
parser,
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;
},


Expand All @@ -523,23 +522,23 @@ module.exports = {
* Phase 3
*
* @method postDecode
* @param {int[]} values
* @param {Buffer} image
* @param {boolean} strict Should parsing be strict?
* @param {object} options Decoding options
* @return {Buffer}
*/
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;
},

/**
Expand Down
29 changes: 13 additions & 16 deletions lib/png/chunks/tRNS.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ module.exports = {
* Phase 2
*
* @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) {

var i, len, index,
r, g, b, alpha,
Expand All @@ -205,35 +205,32 @@ module.exports = {
colors = paletteChunk.getColors();
}

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);

alpha = this.findAlpha(r, g, b);

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

return values;
return image;
},

/**
Expand Down
Loading

0 comments on commit 4b1ce0d

Please sign in to comment.