/
simple_exif.cr
60 lines (47 loc) · 1.33 KB
/
simple_exif.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class FastImage
abstract class SimpleExif
struct Orientation
getter value : UInt8
def initialize(value)
@value = value.to_u8
end
def rotated?
value && value.not_nil! >= 5
end
end
TAG_MARK = 42
def self.decode(io : IO, byte_format)
width : UInt16? = nil
height : UInt16? = nil
orientation : Orientation? = nil
validate_tag_mark!(io, byte_format)
offset = byte_format.decode(UInt32, io)
io.skip(offset - 8)
byte_format.decode(UInt16, io).times do
type = byte_format.decode(UInt16, io)
io.skip(6)
case type
when 0x0100
width = byte_format.decode(UInt16, io)
io.skip(2)
when 0x0101
height = byte_format.decode(UInt16, io)
io.skip(2)
when 0x0112
orientation = Orientation.new(byte_format.decode(UInt16, io))
io.skip(2)
else
io.skip(4)
end
break if width && height && orientation
end
orientation ||= Orientation.new(1)
return width, height, orientation
end
private def self.validate_tag_mark!(io, byte_format)
raise FormatError.new(
"Exif should start with [0x42, 0x00] or [0x00, 0x42]"
) unless byte_format.decode(UInt16, io) == TAG_MARK
end
end
end