Skip to content
This repository
Browse code

Fixed autoOrient to work with all types of exif orientations.

  • Loading branch information...
commit c97679e0ecb944dd20bfd2cbd8ea2f28eb93418f 1 parent d586685
Luc Castera authored December 13, 2012 aheckmann committed December 13, 2012
2  .gitignore
... ...
@@ -1,3 +1,4 @@
  1
+.DS_Store
1 2
 node_modules
2 3
 examples/imgs/*
3 4
 !examples/imgs/original.jpg
@@ -6,5 +7,6 @@ examples/imgs/*
6 7
 !examples/imgs/originalSideways.jpg
7 8
 !examples/imgs/morpher.jpg
8 9
 !examples/imgs/photo.JPG
  10
+!examples/imgs/orientation
9 11
 *.swp
10 12
 *.swo
BIN  examples/imgs/orientation/Landscape_1.jpg
BIN  examples/imgs/orientation/Landscape_2.jpg
BIN  examples/imgs/orientation/Landscape_3.jpg
BIN  examples/imgs/orientation/Landscape_4.jpg
BIN  examples/imgs/orientation/Landscape_5.jpg
BIN  examples/imgs/orientation/Landscape_6.jpg
BIN  examples/imgs/orientation/Landscape_7.jpg
BIN  examples/imgs/orientation/Landscape_8.jpg
BIN  examples/imgs/orientation/Portrait_1.jpg
BIN  examples/imgs/orientation/Portrait_2.jpg
BIN  examples/imgs/orientation/Portrait_3.jpg
BIN  examples/imgs/orientation/Portrait_4.jpg
BIN  examples/imgs/orientation/Portrait_5.jpg
BIN  examples/imgs/orientation/Portrait_6.jpg
BIN  examples/imgs/orientation/Portrait_7.jpg
BIN  examples/imgs/orientation/Portrait_8.jpg
8  lib/convenience/autoOrient.js
@@ -9,12 +9,12 @@ module.exports = function (proto) {
9 9
 
10 10
   var exifTransforms = {
11 11
       topleft:     ''
12  
-    , topright:    ['-flip', 'horizontal']
  12
+    , topright:    ['-flop']
13 13
     , bottomright: ['-rotate', 180]
14  
-    , bottomleft:  ['-flip', 'vertical' ]
15  
-    , lefttop:     ['-transpose']
  14
+    , bottomleft:  ['-flip']
  15
+    , lefttop:     ['-flip', '-rotate', 90]
16 16
     , righttop:    ['-rotate', 90]
17  
-    , rightbottom: ['-transverse']
  17
+    , rightbottom: ['-flop', '-rotate', 90]
18 18
     , leftbottom:  ['-rotate', 270]
19 19
   }
20 20
 
100  test/autoOrientAll.js
... ...
@@ -0,0 +1,100 @@
  1
+
  2
+// gm - Copyright Aaron Heckmann <aaron.heckmann+github@gmail.com> (MIT Licensed)
  3
+
  4
+var assert = require('assert'),
  5
+    fs = require('fs');
  6
+
  7
+module.exports = function (_, dir, finish, gm) {
  8
+  var filename = dir + '/autoOrient.jpg';
  9
+
  10
+	var beforeValues = {
  11
+		'Landscape_1.jpg': ['TopLeft', 1, '600x450'],
  12
+		'Landscape_2.jpg': ['TopRight', 2, '600x450'],
  13
+		'Landscape_3.jpg': ['BottomRight', 3, '600x450'],
  14
+		'Landscape_4.jpg': ['BottomLeft', 4, '600x450'],
  15
+		'Landscape_5.jpg': ['LeftTop', 5, '450x600'],
  16
+		'Landscape_6.jpg': ['RightTop', 6, '450x600'],
  17
+		'Landscape_7.jpg': ['RightBottom', 7, '450x600'],
  18
+		'Landscape_8.jpg': ['LeftBottom', 8, '450x600'],
  19
+		'Portrait_1.jpg': ['TopLeft', 1, '450x600'],
  20
+		'Portrait_2.jpg': ['TopRight', 2, '450x600'],
  21
+		'Portrait_3.jpg': ['BottomRight', 3, '450x600'],
  22
+		'Portrait_4.jpg': ['BottomLeft', 4, '450x600'],
  23
+		'Portrait_5.jpg': ['LeftTop', 5, '600x450'],
  24
+		'Portrait_6.jpg': ['RightTop', 6, '600x450'],
  25
+		'Portrait_7.jpg': ['RightBottom', 7, '600x450'],
  26
+		'Portrait_8.jpg': ['LeftBottom', 8, '600x450']
  27
+	};
  28
+	var afterValues = {
  29
+		'Landscape_1.jpg': ['TopLeft', false, '600x450'],
  30
+		'Landscape_2.jpg': ['Unknown', true, '600x450'],
  31
+		'Landscape_3.jpg': ['Unknown', true, '600x450'],
  32
+		'Landscape_4.jpg': ['Unknown', true, '600x450'],
  33
+		'Landscape_5.jpg': ['Unknown', true, '600x450'],
  34
+		'Landscape_6.jpg': ['Unknown', true, '600x450'],
  35
+		'Landscape_7.jpg': ['Unknown', true, '600x450'],
  36
+		'Landscape_8.jpg': ['Unknown', true, '600x450'],
  37
+		'Portrait_1.jpg': ['TopLeft', false, '450x600'],
  38
+		'Portrait_2.jpg': ['Unknown', true, '450x600'],
  39
+		'Portrait_3.jpg': ['Unknown', true, '450x600'],
  40
+		'Portrait_4.jpg': ['Unknown', true, '450x600'],
  41
+		'Portrait_5.jpg': ['Unknown', true, '450x600'],
  42
+		'Portrait_6.jpg': ['Unknown', true, '450x600'],
  43
+		'Portrait_7.jpg': ['Unknown', true, '450x600'],
  44
+		'Portrait_8.jpg': ['Unknown', true, '450x600']
  45
+	};
  46
+	fs.readdir(dir + '/orientation/', function(err, files) {
  47
+		if (err) return finish(err);
  48
+
  49
+		var originalFiles = files.filter(function(file) {
  50
+			if (/\.oriented\.jpg$/.test(file)) {
  51
+				return false;
  52
+			} else {
  53
+				return true;
  54
+			}
  55
+		});
  56
+
  57
+		var count = originalFiles.length;
  58
+
  59
+		originalFiles.forEach(function(filename, index) {
  60
+			var fileToAutoOrient = dir + '/orientation/' + filename;
  61
+			var newFilename = fileToAutoOrient + '.oriented.jpg';
  62
+
  63
+			gm(fileToAutoOrient).orientation(function (err, o) {
  64
+				if (err) return finish(err);
  65
+
  66
+				assert.equal(beforeValues[filename][0], o);
  67
+				assert.equal(beforeValues[filename][1], this.data['Profile-EXIF'].Orientation, 'No Profile-EXIF data found');
  68
+				assert.equal(beforeValues[filename][2], this.data.Geometry);
  69
+
  70
+				// this image is sideways, but may be auto-oriented by modern OS's
  71
+				// try opening it in a browser to see its true orientation
  72
+				gm(fileToAutoOrient)
  73
+				.autoOrient()
  74
+				.write(newFilename, function autoOrient (err) {
  75
+					if (err) return finish(err);
  76
+
  77
+					// fs race condition
  78
+					setTimeout(function () {
  79
+						gm(newFilename).identify(function (err) {
  80
+							if (err) return finish(err);
  81
+
  82
+							assert.equal(afterValues[filename][0], this.data.Orientation, 'Bad-Orientation for ' + filename);
  83
+							assert.equal(afterValues[filename][1], !this.data['Profile-EXIF'], 'Profile-EXIF still exists');
  84
+							assert.equal(afterValues[filename][2], this.data.Geometry, 'Bad-Geometry for ' + filename);
  85
+
  86
+							count = count - 1;
  87
+							if (count === 0) {
  88
+								finish(err);
  89
+							}
  90
+						});
  91
+					}, 500);
  92
+				});
  93
+			});
  94
+
  95
+		});
  96
+		
  97
+	});
  98
+
  99
+};
  100
+

0 notes on commit c97679e

Please sign in to comment.
Something went wrong with that request. Please try again.