class Bitmap attr_reader :filename, :file, :magic, :file_size, :pixel_width, :pixel_height, :bmp_data, :full_data, :bmp_size @filename @file @magic @file_size @app1 @app2 @bitmap_offset @pixel_width @pixel_height @color_planes @bits_per_pixel @compression @bmp_size @horizontal_resolution @vertical_resolution @palette_size @important_colours #2D Array @bmp_data @full_data def initialize(filename) @filename = filename @file = open("#{@filename}", "rb") @full_data = [] @bmp_data = [] temp_index = 0 @file.each_byte do |byte| @full_data[temp_index] = to_hex(byte) temp_index += 1 end @file.close @magic = @full_data[0..1].to_s if @magic != "424d" then print "Oh heck, the magic number is wrong -- things may be wacky!" end @file_size = @full_data[2..5].to_s.to_i @app1 = @full_data[6..7].to_s @app2 = @full_data[8..9].to_s @bitmap_offset = @full_data[10..13].to_s @pixel_width = @full_data[14..17].to_s.to_i @pixel_height = @full_data[18..21].to_s.to_i @color_planes = @full_data[22..23].to_s.to_i @bits_per_pixel = @full_data[24..25].to_s.to_i @compression = @full_data[26..29].to_s.to_i @bmp_size = @full_data[30..33].to_s.to_i @horizontal_resolution = @full_data[34..37].to_s.to_i @vertical_resolution = @full_data[38..41].to_s.to_i @palette_size = @full_data[42..45].to_s.to_i @important_colours = @full_data[46..49].to_s.to_i 0.upto(@pixel_height-1) do |height| @bmp_data[height] = @full_data[(50+height*@pixel_width) .. ((50+height*@pixel_width)+@pixel_width*@bits_per_pixel)-1] end end def convert_small_hex(num) if num.class == Fixnum then if num < 0 or num > 15 then "error" elsif num <= 9 then num.to_s else (num+87).chr end end end def to_hex(num) str = "" if num < 16 then convert_small_hex(num) else str += to_hex(num/16).to_s str += convert_small_hex(num - (num/16 * 16)) end end end b = Bitmap.new("test.bmp")