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

Commit

Permalink
Merge pull request #7 from yahoo/add_rotate
Browse files Browse the repository at this point in the history
Add rotation methods
  • Loading branch information
marcelerz committed Apr 21, 2015
2 parents 18320ba + 0c29f9b commit 47d39bf
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 67 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

v0.11.1 - 04/20/15
* Add rotateCW/rotateCCW methods
* Add experimental feature
* Add synchronous PNG loader (readImageSync)

v0.11.0 - 04/19/15
* Add experimental features:
* Add PNG decoder supporting true-color (+alpha) and index-color (with palette); supports auxiliary chunk tRNS
Expand Down
Binary file added examples/eye.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/eye_ouput.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/loadFromMemory.js
Expand Up @@ -25,7 +25,7 @@ var image = PNGImage.loadImage(blob, function (err, image) {
red: 0, green: 0, blue: 0, alpha: 255
});

console.log('modified!');
console.log('Modified!');

// Export it
image.writeImage(__dirname + '/export.png', function (err) {
Expand Down
20 changes: 20 additions & 0 deletions examples/rotate.js
@@ -0,0 +1,20 @@
// Copyright 2015, Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

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

PNGImage.readImage(__dirname + '/squirrel.png', function (err, image) {

if (err) {
throw err;
}

image = image.rotateCCW();
//image = image.rotateCW();

image.writeImage(__dirname + '/rotatedSquirrel.png', function (err) {
if (err) throw err;

console.log('Done');
});
});
Binary file added examples/rotatedSquirrel.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions examples/synchonous.js
@@ -0,0 +1,17 @@
// Copyright 2015, Yahoo! Inc.
// 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
});

console.log('Modified!');

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

console.log('Done');
60 changes: 0 additions & 60 deletions examples/testDecoder.js

This file was deleted.

17 changes: 17 additions & 0 deletions index.js
Expand Up @@ -5,6 +5,7 @@ var fs = require('fs'),
_ = require('underscore'),
PNG = require('pngjs').PNG,
pixel = require('./lib/pixel'),
modify = require('./lib/modify'),
conversion = require('./lib/conversion'),
filters = require('./lib/filters'),
streamBuffers = require("stream-buffers");
Expand Down Expand Up @@ -78,6 +79,7 @@ PNGImage.createImage = function (width, height) {
return new PNGImage(image);
};


/**
* Copies an already existing image
*
Expand All @@ -92,6 +94,7 @@ PNGImage.copyImage = function (image) {
return newImage;
};


/**
* Reads an image from the filesystem
*
Expand Down Expand Up @@ -120,6 +123,19 @@ PNGImage.readImage = function (filename, fn) {
return resultImage;
};

/**
* Reads an image from the filesystem synchronously
*
* @static
* @method readImageSync
* @param {string} filename
* @return {PNGImage}
*/
PNGImage.readImageSync = function (filename) {
return this.loadImageSync(fs.readFileSync(filename));
};


/**
* Loads an image from a blob
*
Expand Down Expand Up @@ -415,6 +431,7 @@ PNGImage.prototype.constructor = PNGImage;

// Add standard methods to the prototype
_.extend(PNGImage.prototype, pixel);
_.extend(PNGImage.prototype, modify);
_.extend(PNGImage.prototype, conversion);


Expand Down
55 changes: 55 additions & 0 deletions lib/modify.js
@@ -0,0 +1,55 @@
// Copyright 2015 Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

/**
* Image manipulation functions
*
* @class PNGImage
* @type {object}
*/
module.exports = {

/**
* Rotates the current image 90 degree counter-clockwise
*
* @method rotateCCW
* @return {PNGImage} Rotated image
*/
rotateCCW: function () {
var Class = this.constructor,
x, y,
width = this.getWidth(),
height = this.getHeight(),
image = Class.createImage(height, width);

for (x = 0; x < width; x++) {
for (y = 0; y < height; y++) {
image.setPixel(y, width - x - 1, this.getPixel(x, y));
}
}

return image;
},

/**
* Rotates the current image 90 degree clockwise
*
* @method rotateCW
* @return {PNGImage} Rotated image
*/
rotateCW: function () {
var Class = this.constructor,
x, y,
width = this.getWidth(),
height = this.getHeight(),
image = Class.createImage(height, width);

for (x = 0; x < width; x++) {
for (y = 0; y < height; y++) {
image.setPixel(height - y - 1, x, this.getPixel(x, y));
}
}

return image;
}
};
17 changes: 12 additions & 5 deletions lib/pixel.js
Expand Up @@ -52,18 +52,25 @@ module.exports = {
*
* @method setAtIndex
* @param {int} idx Index of pixel
* @param {object} color
* @param {object|int} color
* @param {int} [color.red] Red value for pixel
* @param {int} [color.green] Green value for pixel
* @param {int} [color.blue] Blue value for pixel
* @param {int} [color.alpha] Alpha value for pixel
* @param {number} [color.opacity] Opacity of color
*/
setAtIndex: function (idx, color) {
if (color.red !== undefined) this.setRed(idx, color.red, color.opacity);
if (color.green !== undefined) this.setGreen(idx, color.green, color.opacity);
if (color.blue !== undefined) this.setBlue(idx, color.blue, color.opacity);
if (color.alpha !== undefined) this.setAlpha(idx, color.alpha, color.opacity);
if (typeof color === 'object') {
if (color.red !== undefined) this.setRed(idx, color.red, color.opacity);
if (color.green !== undefined) this.setGreen(idx, color.green, color.opacity);
if (color.blue !== undefined) this.setBlue(idx, color.blue, color.opacity);
if (color.alpha !== undefined) this.setAlpha(idx, color.alpha, color.opacity);
} else {
this.setRed(idx, color & 0xff);
this.setGreen(idx, (color & 0xff00) >> 8);
this.setBlue(idx, (color & 0xff0000) >> 16);
this.setAlpha(idx, (color & 0xff000000) >> 24);
}
},

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "pngjs-image",
"version": "0.11.0",
"version": "0.11.1",
"description": "Native PNG image manipulation",
"license": "MIT",
"main": "index.js",
Expand Down

0 comments on commit 47d39bf

Please sign in to comment.