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
61
|
# File 'lib/parser/jpeg.rb', line 23
def self.dimensions(stream)
stream.rewind
raise ImageSpec::Error, 'malformed JPEG' unless stream.readbyte.chr == "\xFF" && stream.readbyte.chr == "\xD8"
class << stream
def readint
(readbyte.ord << 8) + readbyte.ord
end
def readframe
read(readint - 2)
end
def readsof
[readint, readbyte.chr, readint, readint, readbyte.chr]
end
def next
c = readbyte.chr while c != "\xFF"
c = readbyte.chr while c == "\xFF"
c
end
end
while marker = stream.next
case marker
when "\xC0".."\xC3", "\xC5".."\xC7", "\xC9".."\xCB", "\xCD".."\xCF"
length, bits, height, width, components = stream.readsof
raise ImageSpec::Error, 'malformed JPEG' unless length == 8 + components[0].ord * 3
return [width, height]
when "\xD9", "\xDA"
break
when "\xFE"
@comment = stream.readframe
else
stream.readframe
end
end
end
|