Class: SleipnirAPI::BitMap

Inherits:
Object
  • Object
show all
Includes:
FFI::GDI32
Defined in:
lib/sleipnir_api/bitmap.rb

Overview

:nodoc:

Constant Summary

Constants included from FFI::GDI32

FFI::GDI32::ASPECTX, FFI::GDI32::ASPECTXY, FFI::GDI32::ASPECTY, FFI::GDI32::BITSPIXEL, FFI::GDI32::BI_BITFIELDS, FFI::GDI32::BI_JPEG, FFI::GDI32::BI_PNG, FFI::GDI32::BI_RGB, FFI::GDI32::BI_RLE4, FFI::GDI32::BI_RLE8, FFI::GDI32::BLACKNESS, FFI::GDI32::CLIPCAPS, FFI::GDI32::CURVECAPS, FFI::GDI32::DIB_PAL_COLORS, FFI::GDI32::DIB_RGB_COLORS, FFI::GDI32::DRIVERVERSION, FFI::GDI32::DSTINVERT, FFI::GDI32::HORZRES, FFI::GDI32::HORZSIZE, FFI::GDI32::LINECAPS, FFI::GDI32::MERGECOPY, FFI::GDI32::MERGEPAINT, FFI::GDI32::NOTSRCCOPY, FFI::GDI32::NOTSRCERASE, FFI::GDI32::NUMBRUSHES, FFI::GDI32::NUMCOLORS, FFI::GDI32::NUMFONTS, FFI::GDI32::NUMMARKERS, FFI::GDI32::NUMPENS, FFI::GDI32::PATCOPY, FFI::GDI32::PATINVERT, FFI::GDI32::PATPAINT, FFI::GDI32::PDEVICESIZE, FFI::GDI32::PLANES, FFI::GDI32::POLYGONALCAPS, FFI::GDI32::RASTERCAPS, FFI::GDI32::SRCAND, FFI::GDI32::SRCCOPY, FFI::GDI32::SRCERASE, FFI::GDI32::SRCINVERT, FFI::GDI32::SRCPAINT, FFI::GDI32::TECHNOLOGY, FFI::GDI32::TEXTCAPS, FFI::GDI32::VERTRES, FFI::GDI32::VERTSIZE, FFI::GDI32::WHITENESS

Constants included from FFI::CStruct

FFI::CStruct::TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FFI::GDI32

with_delete_dc, with_delete_object

Methods included from FFI::Base

#define_ffi_entry

Methods included from FFI::CStruct

c_type, #define_c_struct, define_c_struct_under, define_c_type, sizeof

Constructor Details

#initialize(width, height) ⇒ BitMap

Returns a new instance of BitMap.



12
13
14
15
16
# File 'lib/sleipnir_api/bitmap.rb', line 12

def initialize(width, height)
  @width = width
  @height = height
  @data = nil
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



11
12
13
# File 'lib/sleipnir_api/bitmap.rb', line 11

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



11
12
13
# File 'lib/sleipnir_api/bitmap.rb', line 11

def width
  @width
end

Instance Method Details

#concat_bottom!(bmp) ⇒ Object Also known as: <<



41
42
43
44
45
46
47
48
49
# File 'lib/sleipnir_api/bitmap.rb', line 41

def concat_bottom!(bmp)
  if width.zero?
    replace(bmp)
  else
    check_bitmap_size(:width, @width, bmp.width)
    @data << bmp.data
    @height += bmp.height
  end
end

#concat_right!(bmp) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/sleipnir_api/bitmap.rb', line 52

def concat_right!(bmp)
  if height.zero?
    replace(bmp)
  else
    check_bitmap_size(:height, @height, bmp.height)
    @data = rows.zip(bmp.rows).flatten.join
    @width += bmp.width
  end
end

#create_bitmapfileheaderObject Also known as: header



70
71
72
73
74
75
76
# File 'lib/sleipnir_api/bitmap.rb', line 70

def create_bitmapfileheader
  header = BITMAPFILEHEADER.new
  header.bfType    = "BM".unpack("S")[0]
  header.bfSize    = header_size + data_size
  header.bfOffBits = header_size
  header
end

#create_bitmapinfoObject Also known as: info



79
80
81
82
83
84
85
86
87
88
# File 'lib/sleipnir_api/bitmap.rb', line 79

def create_bitmapinfo
  info = BITMAPINFO.new
  info.bmiHeader.biSize        = BITMAPINFOHEADER.packed_size
  info.bmiHeader.biWidth       = width
  info.bmiHeader.biHeight      = -height # negative because we want top-down bitmap
  info.bmiHeader.biPlanes      = 1
  info.bmiHeader.biBitCount    = 32      # we want RGBQUAD data
  info.bmiHeader.biCompression = BI_RGB
  info
end

#dataObject



26
27
28
# File 'lib/sleipnir_api/bitmap.rb', line 26

def data
  @data ||= "\0" * data_size
end

#data_sizeObject



22
23
24
# File 'lib/sleipnir_api/bitmap.rb', line 22

def data_size
  width * height * 4
end

#header_sizeObject



18
19
20
# File 'lib/sleipnir_api/bitmap.rb', line 18

def header_size
  BITMAPINFOHEADER.packed_size + BITMAPFILEHEADER.packed_size
end

#inspectObject



108
109
110
# File 'lib/sleipnir_api/bitmap.rb', line 108

def inspect
  "#<%s:0x%x @width=%d @height=%d @data=...>" % [self.class, self.object_id << 1, @height, @width]
end

#pack(&block) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/sleipnir_api/bitmap.rb', line 99

def pack(&block)
  v = [create_bitmapfileheader.pack, create_bitmapinfo.bmiHeader.pack, @data]
  if block
    v.each(&block)
  else
    v.join
  end
end

#replace(bmp) ⇒ Object



34
35
36
37
38
# File 'lib/sleipnir_api/bitmap.rb', line 34

def replace(bmp)
  @width = bmp.width
  @height = bmp.height
  @data = bmp.data
end

#rowsObject



30
31
32
# File 'lib/sleipnir_api/bitmap.rb', line 30

def rows
  (0..height).map{|i| data[i * width * 4, width * 4] }
end

#save(filename) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/sleipnir_api/bitmap.rb', line 91

def save(filename)
  open(filename, "wb") do |w|
    pack {|data|
      w.write data
    }
  end
end