def examine(io)
class << io
unless method_defined?(:readbyte)
def readbyte
getc
end
end
def readint; (readbyte << 8) + readbyte; end
def readframe; read(readint - 2); end
def readsof; [readint, readbyte, readint, readint, readbyte]; end
def next
c = readbyte while c != 0xFF
c = readbyte while c == 0xFF
c
end
end
raise 'malformed JPEG!' unless io.readbyte == 0xFF && io.readbyte == 0xD8
while marker = io.next
case marker
when 0xC0..0xC3, 0xC5..0xC7, 0xC9..0xCB, 0xCD..0xCF
length, @bits, @height, @width, components = io.readsof
raise 'malformed JPEG' unless length == 8 + components * 3
when 0xD9, 0xDA then break
when 0xFE then @comment = io.readframe
when 0xE1 then io.readframe
else io.readframe
end
end
end