Skip to content

Commit 4079378

Browse files
committed
Add support for EXIF "unknown" orientation
In our handling of EXIF orientations we were too smart. We would take the EXIF orientation as tagged in the file, and we would subtract 1 from it, so that we could then fetch a corresponding symbol from our enum-ish array of orientations. There is however an EXIF orientation called "Unknown", which occupies the enum value of 0 - we would take that value and subtract -1 from it. This would in turn make us pick orientation 8, which was not the correct one and would indicate that the image is rotated while in fact it is not. To mitigate, we replace the array enum with a Hash - so that it becomes impossible to rollover the enum index when it becomes negative, and simplify the rotated orientation detection.
1 parent 5951d66 commit 4079378

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

lib/parsers/exif_parser.rb

+13-18
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@
22
require 'delegate'
33

44
module FormatParser::EXIFParser
5-
ORIENTATIONS = [
6-
:top_left,
7-
:top_right,
8-
:bottom_right,
9-
:bottom_left,
10-
:left_top,
11-
:right_top,
12-
:right_bottom,
13-
:left_bottom
14-
]
15-
ROTATED_ORIENTATIONS = ORIENTATIONS - [
16-
:bottom_left,
17-
:bottom_right,
18-
:top_left,
19-
:top_right
20-
]
5+
ORIENTATIONS = {
6+
0 => :unknown, # Non-rotated
7+
1 => :top_left, # Non-rotated
8+
2 => :top_right, # Non-rotated
9+
3 => :bottom_right, # Non-rotated
10+
4 => :bottom_left, # Non-rotated
11+
5 => :left_top,
12+
6 => :right_top,
13+
7 => :right_bottom,
14+
8 => :left_bottom
15+
}
2116

2217
# EXIFR kindly requests the presence of a few more methods than what our IOConstraint
2318
# is willing to provide, but they can be derived from the available ones
@@ -47,7 +42,7 @@ def seek(n, seek_mode = IO::SEEK_SET)
4742

4843
class EXIFResult < SimpleDelegator
4944
def rotated?
50-
ROTATED_ORIENTATIONS.include?(orientation)
45+
__getobj__.orientation.to_i > 4
5146
end
5247

5348
def to_json(*maybe_coder)
@@ -58,7 +53,7 @@ def to_json(*maybe_coder)
5853

5954
def orientation
6055
value = __getobj__.orientation.to_i
61-
ORIENTATIONS.fetch(value - 1)
56+
ORIENTATIONS.fetch(value)
6257
end
6358
end
6459

0 commit comments

Comments
 (0)