Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New Feature] Autocrop #71

Merged
merged 1 commit into from
Nov 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,92 @@ Jimp.prototype.crop = function (x, y, w, h, cb) {
else return this;
};

/**
* Autocrop same color borders from this image.
* All borders must be of the same color as the top left pixel, to be cropped.
* It should be possible to crop borders each with a different color,
* but since there are many ways for corners to intersect, it would
* introduce unnecessary complexity to the algorithm.
* @param (optional) cb a callback for when complete
* @returns this for chaining of methods
*/
Jimp.prototype.autocrop = function (cb) {
var w = this.bitmap.width;
var h = this.bitmap.height;
var northPixelsToCrop = 0;
var eastPixelsToCrop = 0;
var southPixelsToCrop = 0;
var westPixelsToCrop = 0;

var color = this.getPixelColor(0, 0); // get top left pixel color

// scan each side for same color borders
north: // north side
for (var y = northPixelsToCrop; y < h - (northPixelsToCrop + southPixelsToCrop); y++) {
for (var x = westPixelsToCrop; x < w - (eastPixelsToCrop + westPixelsToCrop); x++) {
if (this.getPixelColor(x, y) !== color) {
// this pixel is not the same color as the first one: abort this side scan
break north;
}
}
// this row contains all pixels with the same color: increment this side pixels to crop
northPixelsToCrop++;
}

east: // east side
for (var x = westPixelsToCrop; x < w - (eastPixelsToCrop + westPixelsToCrop); x++) {
for (var y = northPixelsToCrop; y < h - (northPixelsToCrop + southPixelsToCrop); y++) {
if (this.getPixelColor(x, y) !== color) {
// this pixel is not the same color as the first one: abort this side scan
break east;
}
}
// this column contains all pixels with the same color: increment this side pixels to crop
eastPixelsToCrop++;
}

south: // south side
for (var y = h - 1; y >= 0; y--) {
for (var x = w - 1; x >= 0; x--) {
if (this.getPixelColor(x, y) !== color) {
// this pixel is not the same color as the first one: abort this side scan
break south;
}
}
// this row contains all pixels with the same color: increment this side pixels to crop
southPixelsToCrop++;
}

west: // west side
for (var x = w - 1; x >= 0; x--) {
for (var y = h - 1; y >= 0; y--) {
if (this.getPixelColor(x, y) !== color) {
// this pixel is not the same color as the first one: abort this side scan
break west;
}
}
// this column contains all pixels with the same color: increment this side pixels to crop
westPixelsToCrop++;
}

// safety checks
var widthOfPixelsToCrop = w - (westPixelsToCrop + eastPixelsToCrop);
widthOfPixelsToCrop >= 0 ? widthOfPixelsToCrop : 0;
var heightOfPixelsToCrop = h - (southPixelsToCrop + northPixelsToCrop);
heightOfPixelsToCrop >= 0 ? heightOfPixelsToCrop : 0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Safety checks" do not work. Should probably be

var widthOfPixelsToCrop = w - (westPixelsToCrop + eastPixelsToCrop);
var heightOfPixelsToCrop = h - (southPixelsToCrop + northPixelsToCrop);

widthOfPixelsToCrop = widthOfPixelsToCrop >= 0 ? widthOfPixelsToCrop : 0;
heightOfPixelsToCrop = heightOfPixelsToCrop >= 0 ? heightOfPixelsToCrop : 0;

or more compact

var widthOfPixelsToCrop = Math.max(0, w - westPixelsToCrop - eastPixelsToCrop);
var heightOfPixelsToCrop = Math.max(0, h - southPixelsToCrop - northPixelsToCrop);

// crop image
this.crop(
westPixelsToCrop,
northPixelsToCrop,
widthOfPixelsToCrop,
heightOfPixelsToCrop
);

if (isNodePattern(cb)) return cb.call(this, null, this);
else return this;
};

/**
* Blits a source image on to this image
* @param src the source Jimp instance
Expand Down
77 changes: 77 additions & 0 deletions test/autocrop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
var Jimp = require("../index.js");

var imageBase = "peace.png";
var imageTransparent = "peace-transparent.png";
var imageWithOneColorBorder = "peace-with-border.png";
var imageWithManyColorsBorder = "peace-with-different-borders.png";

/*
new Jimp(imageWithOneColorBorder, function (err, image) {
if (err) {
return console.error("Error reading image", imageWithOneColorBorder, ":", err);
}
this.autocrop();
this.write("./output/" + imageWithOneColorBorder, function() {

var p1 = Jimp.read(imageBase);
var p2 = Jimp.read("./output/" + imageWithOneColorBorder);

Promise.all([p1, p2]).then(function(images) {

// compare image and imageWithOneColorBorder after autocrop for() equality
var distance = Jimp.distance(images[0], images[1]);
if (distance !== 0) {
console.error("imageWithOneColorBorder after autocrop() differs by imageBase!");
}
}).catch(function (err) {
console.error('t1:', err);
});
});
});
*/
new Jimp(imageTransparent, function (err, image) {
if (err) {
return console.error("Error reading image", imageTransparent, ":", err);
}
this.autocrop();
this.write("./output/" + imageTransparent, function() {

var p1 = Jimp.read(imageBase);
var p2 = Jimp.read("./output/" + imageTransparent);

Promise.all([p1, p2]).then(function(images) {

// compare image and imageTransparent after autocrop for() inequality
var distance = Jimp.distance(images[0], images[1]);
if (distance === 0) {
console.error("imageTransparent after autocrop() does not differ from imageBase!");
}
}).catch(function (err) {
console.error(err);
});
});
});
return;

new Jimp(imageWithManyColorsBorder, function (err, image) {
if (err) {
return console.error("Error reading image", imageWithManyColorsBorder, ":", err);
}
this.autocrop();
this.write("./output/" + imageWithManyColorsBorder, function() {

var p1 = Jimp.read(imageBase);
var p2 = Jimp.read("./output/" + imageWithManyColorsBorder);

Promise.all([p1, p2]).then(function(images) {

// compare image and imageWithManyColorsBorder after autocrop for() inequality
var distance = Jimp.distance(images[0], images[1]);
if (distance === 0) {
console.error("imageWithManyColorsBorder after autocrop() does not differ from imageBase!");
}
}).catch(function (err) {
console.error(err);
});
});
});
Binary file added test/peace-transparent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/peace-with-border.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/peace-with-different-borders.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/peace.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 13 additions & 9 deletions test/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,45 @@ function clean {
}

clean
echo "promise.js (1/9)"
echo "promise.js (1/10)"
node promise.js

clean
echo "callbacks.js (2/9)"
echo "callbacks.js (2/10)"
node callbacks.js

clean
echo "chained.js (3/9)"
echo "chained.js (3/10)"
node chained.js

clean
echo "create.js (4/9)"
echo "create.js (4/10)"
node create.js

clean
echo "rotation.js (5/9)"
echo "rotation.js (5/10)"
node rotation.js

clean
echo "filetype.js (6/9)"
echo "filetype.js (6/10)"
node filetypes.js

clean
echo "color.js (7/9)"
echo "color.js (7/10)"
node color.js

clean
echo "compare.js (8/9)"
echo "compare.js (8/10)"
node compare.js

clean
echo "exif.js (9/9)"
echo "exif.js (9/10)"
node exif.js

clean
echo "autocrop.js (10/10)"
node autocrop.js



clean
Expand Down