Module: BMP::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/bmp/utils.rb

Instance Method Summary collapse

Instance Method Details

#calc_file_size(img_size, header_size = BMP::HEADER_SIZE) ⇒ Integer

Returns:

  • (Integer)


20
21
22
# File 'lib/bmp/utils.rb', line 20

def calc_file_size(img_size, header_size=BMP::HEADER_SIZE)
  img_size + header_size
end

#calc_image_data_size(arg_width, arg_height) ⇒ Integer

Returns:

  • (Integer)


7
8
9
# File 'lib/bmp/utils.rb', line 7

def calc_image_data_size(arg_width, arg_height)
  ((BMP::BITS_PER_PIXEL * arg_width ) / 32.0).ceil * 4 * arg_height
end

#int_to_octets_string_endian_little(int) ⇒ Object



11
12
13
# File 'lib/bmp/utils.rb', line 11

def int_to_octets_string_endian_little(int)
  [int].pack("V").each_byte.map {|b| b.to_s(16).rjust(2, '0')}.join(':')
end

#octets_string_endian_little_to_int(octets_str) ⇒ Object



15
16
17
# File 'lib/bmp/utils.rb', line 15

def octets_string_endian_little_to_int(octets_str)
  octets_str.split(':').map {|o| o.to_i(16).chr}.join.unpack('V').first
end

#parse_image(img_in) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bmp/utils.rb', line 45

def parse_image(img_in)
  img_width             = img_in.width
  img_height            = img_in.height
  img_data_size         = calc_image_data_size(img_width, img_height)
  img_file_size         = calc_file_size(img_data_size)
  img_pixels            = pixel_array_template(img_width, img_height)

  img_height.times do |row|
    img_width.times do |col|
      rgba = ChunkyPNG::Color.to_hex(img_in.get_pixel(row,col), true)[1..-1]
      rr, gg, bb, aa = rgba.scan(/../)
      img_pixels[col][row] = [bb,gg,rr,aa].join

      #puts row.to_s + ',' + col.to_s + ' => ' + rgba.inspect if @debug
    end
  end

  hash = {}
  hash[:file_size]     = img_file_size
  hash[:image_size]    = img_data_size
  hash[:image_width]   = img_width
  hash[:image_height]  = img_height
  hash[:pixel_array]   = write_pixel_array(img_pixels)

  hash
end

#pixel_array_template(arg_width, arg_height) ⇒ Object



24
25
26
# File 'lib/bmp/utils.rb', line 24

def pixel_array_template(arg_width, arg_height)
  Array.new(arg_height) {Array.new(arg_width) {nil}}
end

#pixel_binstring(rgba_string) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
# File 'lib/bmp/utils.rb', line 40

def pixel_binstring(rgba_string)
  raise ArgumentError unless rgba_string =~ /\A\h{8}\z/
  [rgba_string].pack("H*")
end

#write_pixel_array(img_px) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bmp/utils.rb', line 28

def write_pixel_array(img_px)
  str = ''

  img_px.reverse_each do |row|
    row.each do |color|
      str << pixel_binstring(color)
    end
  end

  return str
end