Module: ZPNG::BMP::Reader

Included in:
Image
Defined in:
lib/zpng/bmp/reader.rb

Instance Method Summary collapse

Instance Method Details

#_read_bmp(io) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/zpng/bmp/reader.rb', line 70

def _read_bmp io
  filesize, reserved1, reserved2, imagedata_offset = io.read(4+2+2+4).unpack('VvvV')
  # DIB Header immediately follows the Bitmap File Header
  hdr = BITMAPINFOHEADER.read(io)
  if hdr.biSize != BITMAPINFOHEADER::SIZE
    raise "dib_hdr_size #{hdr.biSize} unsupported, want #{BITMAPINFOHEADER::SIZE}"
  end

  @new_image = true
  @color_class = BMP::Color
  @format = :bmp
  @chunks << BmpHdrPseudoChunk.new(hdr)

  # http://en.wikipedia.org/wiki/BMP_file_format#Pixel_storage
  row_size = ((hdr.biBitCount*self.width+31)/32)*4
  # XXX hidden data in non-significant tail bits/bytes

  io.seek(imagedata_offset)

  @scanlines = []
  self.height.times do |idx|
    offset = io.tell - imagedata_offset
    data = io.read(row_size)
    # BMP scanlines layout is upside-down
    @scanlines.unshift ScanLine.new(self, self.height-idx-1,
                                    :decoded_bytes => data,
                                    :size   => row_size,
                                    :offset => offset
                                   )
  end

  extend ImageMixin
end