BMP = '240x320.bmp'
def readbytes(fp, len)
bin = fp.read(len)
raise 'read error' if bin.size < len
bin.bytes.reverse.inject(0) {|v, b|
v <<= 8
v |= b
}
end
def read16(fp)
readbytes(fp, 2)
end
def read32(fp)
readbytes(fp, 4)
end
begin
bmp = File.new(BMP, 'r')
=begin
puts 'ADDR: +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F'
for row in 0...16
print sprintf("%04X:", row * 16)
line = f.read(16)
line.each_byte {|b|
print sprintf(" %02X", b)
}
puts ''
end
=end
puts "BMP signature = #{sprintf('%04X', read16(bmp))}"
size = read32(bmp)
puts "file size = #{size} (#{sprintf('%08x', size)})"
puts "(skip data) = #{sprintf('%08x', read32(bmp))}"
puts "image offset = #{sprintf('%08x', read32(bmp))}"
puts "header size = #{sprintf('%08x', read32(bmp))}"
bmpWidth = read32(bmp)
bmpHeight = read32(bmp)
puts "image width = #{bmpWidth}"
puts "image height = #{bmpHeight}"
puts "planes = #{read16(bmp)}"
puts "bit per pixel = #{read16(bmp)}"
rescue => e
p e
ensure
bmp.close
end