class Bmp def self.load(filename) new File.read(filename) end attr_reader :width, :height, :bpp, :numcolors, :numsigcolors def initialize(data) sig, sz, res1, res2, off, hlen = data.unpack('a2LSSLL') raise 'invalid BMP signature' if sig != 'BM' raise 'unsupported BMP header' if hlen != 40 @width, @height, planes, @bpp, compr, size, xppm, yppm, @numcolors, @numsigcolors = data[18...54].unpack('LLSSLLLLLL') raise 'unsupported data format' if @bpp != 24 @data = data[off..(sz-off)] if $DEBUG puts "size r: #{data.length}, hdr: #{sz}, hsz: #{hlen}, pxoff: #{off}" puts "pxlen: #{size}, w #@width h #@height w*h*bpp: #{@width*@height*@bpp/8}" end end def pix(x, y) linelen = ((@width * @bpp/8) + 3) & 0xfffffffc off = x*@bpp/8 + linelen*y [@data[off+2], @data[off+1], @data[off]] end end