Class: ImageRuby::BmpEncoder

Inherits:
Encoder
  • Object
show all
Defined in:
lib/imageruby-bmp.rb

Instance Method Summary collapse

Instance Method Details

#encode(image, format, output) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/imageruby-bmp.rb', line 64

def encode(image, format, output)
  unless format == :bmp
    raise UnableToEncodeException
  end

  width = image.width
  height = image.height

  totalsize = 56 + width * height * 3

  output << build_bmp_header(totalsize)
  output << build_bmp_dib_header(image)

  output << "\000\000" # for allignment

  padding_size = ( 4 - (width * 3 % 4) ) % 4
  padding =  "\000" * padding_size

  # write pixel data
  height.times do |y|
    index = ((height-y-1)*width)*3
    output << image.pixel_data[index..index+width*3-1]
    output << padding
  end
end