Class: ImageRuby::BmpCEncoder

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

Instance Method Summary collapse

Instance Method Details

#encode(image, format, output) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/imageruby-bmp-c.rb', line 43

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