Permalink
Browse files

Fixed autoOrient to work with all types of exif orientations.

  • Loading branch information...
1 parent d586685 commit c97679e0ecb944dd20bfd2cbd8ea2f28eb93418f @luccastera luccastera committed with Dec 13, 2012
View
2 .gitignore
@@ -1,3 +1,4 @@
+.DS_Store
node_modules
examples/imgs/*
!examples/imgs/original.jpg
@@ -6,5 +7,6 @@ examples/imgs/*
!examples/imgs/originalSideways.jpg
!examples/imgs/morpher.jpg
!examples/imgs/photo.JPG
+!examples/imgs/orientation
*.swp
*.swo
View
BIN examples/imgs/orientation/Landscape_1.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN examples/imgs/orientation/Landscape_2.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN examples/imgs/orientation/Landscape_3.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN examples/imgs/orientation/Landscape_4.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN examples/imgs/orientation/Landscape_5.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN examples/imgs/orientation/Landscape_6.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN examples/imgs/orientation/Landscape_7.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN examples/imgs/orientation/Landscape_8.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN examples/imgs/orientation/Portrait_1.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN examples/imgs/orientation/Portrait_2.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN examples/imgs/orientation/Portrait_3.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN examples/imgs/orientation/Portrait_4.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN examples/imgs/orientation/Portrait_5.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN examples/imgs/orientation/Portrait_6.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN examples/imgs/orientation/Portrait_7.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN examples/imgs/orientation/Portrait_8.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
8 lib/convenience/autoOrient.js
@@ -9,12 +9,12 @@ module.exports = function (proto) {
var exifTransforms = {
topleft: ''
- , topright: ['-flip', 'horizontal']
+ , topright: ['-flop']
, bottomright: ['-rotate', 180]
- , bottomleft: ['-flip', 'vertical' ]
- , lefttop: ['-transpose']
+ , bottomleft: ['-flip']
+ , lefttop: ['-flip', '-rotate', 90]
, righttop: ['-rotate', 90]
- , rightbottom: ['-transverse']
+ , rightbottom: ['-flop', '-rotate', 90]
, leftbottom: ['-rotate', 270]
}
View
100 test/autoOrientAll.js
@@ -0,0 +1,100 @@
+
+// gm - Copyright Aaron Heckmann <aaron.heckmann+github@gmail.com> (MIT Licensed)
+
+var assert = require('assert'),
+ fs = require('fs');
+
+module.exports = function (_, dir, finish, gm) {
+ var filename = dir + '/autoOrient.jpg';
+
+ var beforeValues = {
+ 'Landscape_1.jpg': ['TopLeft', 1, '600x450'],
+ 'Landscape_2.jpg': ['TopRight', 2, '600x450'],
+ 'Landscape_3.jpg': ['BottomRight', 3, '600x450'],
+ 'Landscape_4.jpg': ['BottomLeft', 4, '600x450'],
+ 'Landscape_5.jpg': ['LeftTop', 5, '450x600'],
+ 'Landscape_6.jpg': ['RightTop', 6, '450x600'],
+ 'Landscape_7.jpg': ['RightBottom', 7, '450x600'],
+ 'Landscape_8.jpg': ['LeftBottom', 8, '450x600'],
+ 'Portrait_1.jpg': ['TopLeft', 1, '450x600'],
+ 'Portrait_2.jpg': ['TopRight', 2, '450x600'],
+ 'Portrait_3.jpg': ['BottomRight', 3, '450x600'],
+ 'Portrait_4.jpg': ['BottomLeft', 4, '450x600'],
+ 'Portrait_5.jpg': ['LeftTop', 5, '600x450'],
+ 'Portrait_6.jpg': ['RightTop', 6, '600x450'],
+ 'Portrait_7.jpg': ['RightBottom', 7, '600x450'],
+ 'Portrait_8.jpg': ['LeftBottom', 8, '600x450']
+ };
+ var afterValues = {
+ 'Landscape_1.jpg': ['TopLeft', false, '600x450'],
+ 'Landscape_2.jpg': ['Unknown', true, '600x450'],
+ 'Landscape_3.jpg': ['Unknown', true, '600x450'],
+ 'Landscape_4.jpg': ['Unknown', true, '600x450'],
+ 'Landscape_5.jpg': ['Unknown', true, '600x450'],
+ 'Landscape_6.jpg': ['Unknown', true, '600x450'],
+ 'Landscape_7.jpg': ['Unknown', true, '600x450'],
+ 'Landscape_8.jpg': ['Unknown', true, '600x450'],
+ 'Portrait_1.jpg': ['TopLeft', false, '450x600'],
+ 'Portrait_2.jpg': ['Unknown', true, '450x600'],
+ 'Portrait_3.jpg': ['Unknown', true, '450x600'],
+ 'Portrait_4.jpg': ['Unknown', true, '450x600'],
+ 'Portrait_5.jpg': ['Unknown', true, '450x600'],
+ 'Portrait_6.jpg': ['Unknown', true, '450x600'],
+ 'Portrait_7.jpg': ['Unknown', true, '450x600'],
+ 'Portrait_8.jpg': ['Unknown', true, '450x600']
+ };
+ fs.readdir(dir + '/orientation/', function(err, files) {
+ if (err) return finish(err);
+
+ var originalFiles = files.filter(function(file) {
+ if (/\.oriented\.jpg$/.test(file)) {
+ return false;
+ } else {
+ return true;
+ }
+ });
+
+ var count = originalFiles.length;
+
+ originalFiles.forEach(function(filename, index) {
+ var fileToAutoOrient = dir + '/orientation/' + filename;
+ var newFilename = fileToAutoOrient + '.oriented.jpg';
+
+ gm(fileToAutoOrient).orientation(function (err, o) {
+ if (err) return finish(err);
+
+ assert.equal(beforeValues[filename][0], o);
+ assert.equal(beforeValues[filename][1], this.data['Profile-EXIF'].Orientation, 'No Profile-EXIF data found');
+ assert.equal(beforeValues[filename][2], this.data.Geometry);
+
+ // this image is sideways, but may be auto-oriented by modern OS's
+ // try opening it in a browser to see its true orientation
+ gm(fileToAutoOrient)
+ .autoOrient()
+ .write(newFilename, function autoOrient (err) {
+ if (err) return finish(err);
+
+ // fs race condition
+ setTimeout(function () {
+ gm(newFilename).identify(function (err) {
+ if (err) return finish(err);
+
+ assert.equal(afterValues[filename][0], this.data.Orientation, 'Bad-Orientation for ' + filename);
+ assert.equal(afterValues[filename][1], !this.data['Profile-EXIF'], 'Profile-EXIF still exists');
+ assert.equal(afterValues[filename][2], this.data.Geometry, 'Bad-Geometry for ' + filename);
+
+ count = count - 1;
+ if (count === 0) {
+ finish(err);
+ }
+ });
+ }, 500);
+ });
+ });
+
+ });
+
+ });
+
+};
+

0 comments on commit c97679e

Please sign in to comment.