Skip to content

Commit

Permalink
bug fix rotate method #49
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-moran committed Oct 13, 2015
1 parent 89ef0bc commit 2463827
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1189,19 +1189,24 @@ function advancedRotate(deg, resize) {
var rad = (deg % 360) * Math.PI / 180;
var cosine = Math.cos(rad);
var sine = Math.sin(rad);

var w, h; // the final width and height if resize == true

if (resize == true) {
// resize the image and blit the existing image onto the centre so that when it is rotated the image is kept in bounds
// resize the image to it maximum dimention and blit the existing image onto the centre so that when it is rotated the image is kept in bounds

// http://stackoverflow.com/questions/3231176/how-to-get-size-of-a-rotated-rectangle
var w = Math.round(Math.abs(this.bitmap.width * sine) + Math.abs(this.bitmap.height * cosine));
var h = Math.round(Math.abs(this.bitmap.width * cosine) + Math.abs(this.bitmap.height * sine));
w = Math.round(Math.abs(this.bitmap.width * cosine) + Math.abs(this.bitmap.height * sine));
h = Math.round(Math.abs(this.bitmap.width * sine) + Math.abs(this.bitmap.height * cosine));

var c = this.clone();
this.scan(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {
this.bitmap.data.writeUInt32BE(this._background, idx);
});
this.resize(w, h);

var max = (this.bitmap.width > this.bitmap.height) ? this.bitmap.width : this.bitmap.height;
this.resize(max, max);

this.blit(c, this.bitmap.width / 2 - c.bitmap.width / 2, this.bitmap.height / 2 - c.bitmap.height / 2);
}

Expand Down Expand Up @@ -1240,6 +1245,13 @@ function advancedRotate(deg, resize) {
}
}
this.bitmap.data = dstBuffer;

if (resize == true) {
// now crop the image to the final size
var x = (this.bitmap.width / 2) - (w/2);
var y = (this.bitmap.height / 2) - (h/2);
this.crop(x, y, w, h);
}
};


Expand Down
Binary file added test/panoramic.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 5 additions & 16 deletions test/rotation.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
var Jimp = require("../index.js");

new Jimp("lenna.png", function (err, image) {
var clone1 = image.clone().scale(0.75);
clone1.name = "lenna-rot-118";
clone1.background(0xFF0000FF);
clone1.rotate(118, save);

var clone2 = image.clone().scale(0.75);
clone2.name = "lenna-rot-noresize-118";
clone2.background(0x00FF0000);
clone2.rotate(118, false, save);
new Jimp("panoramic.jpg", function (err, image) {
this.scale(0.1);
for (var r = 0; r <= 360; r += 10) {
this.clone().rotate(r).write("./output/panoramic" + "-" + r + ".jpg");
}
});

function save(err, image) {
if (err) throw err;
image.write("./output/" + image.name + ".png");
image.write("./output/" + image.name + ".jpg");
}

0 comments on commit 2463827

Please sign in to comment.