Class: SFML::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/sfml/graphics/image.rb

Overview

CPU-side bitmap. Lives in main memory and supports per-pixel reads / writes — handy for procedural generation, screenshots, masks, and anything that needs to inspect or modify pixel data before upload.

img = SFML::Image.new(800, 600, fill: SFML::Color.cornflower_blue)
img = SFML::Image.load("assets/hero.png")

img[10, 20] = SFML::Color.red
img[10, 20]                       #=> Color(255, 0, 0, 255)

img.flip_vertically
img.save("out.png")

Convert to a GPU-side texture for drawing:

tex = SFML::Texture.from_image(img)
sprite = SFML::Sprite.new(tex)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, fill: nil) ⇒ Image

Create a blank image of the given size. With ‘fill:` it’s filled with that colour; without, with transparent black.

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sfml/graphics/image.rb', line 22

def initialize(width, height, fill: nil)
  size = C::System::Vector2u.new
  size[:x] = Integer(width)
  size[:y] = Integer(height)

  ptr = if fill
          C::Graphics.sfImage_createFromColor(size, fill.to_native)
        else
          C::Graphics.sfImage_create(size)
        end
  raise Error, "sfImage_create returned NULL" if ptr.null?
  _take_ownership(ptr)
end

Instance Attribute Details

#handleObject (readonly)

:nodoc:



117
118
119
# File 'lib/sfml/graphics/image.rb', line 117

def handle
  @handle
end

Class Method Details

.from_pixels(width, height, pixels) ⇒ Object

Build an image from a raw RGBA byte string. ‘pixels` must be exactly width*height*4 bytes, row-major from the top-left.

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sfml/graphics/image.rb', line 46

def self.from_pixels(width, height, pixels)
  expected = Integer(width) * Integer(height) * 4
  raise ArgumentError, "expected #{expected} bytes, got #{pixels.bytesize}" if pixels.bytesize != expected

  buf = FFI::MemoryPointer.new(:uint8, expected)
  buf.write_bytes(pixels)

  size = C::System::Vector2u.new
  size[:x] = Integer(width)
  size[:y] = Integer(height)
  ptr = C::Graphics.sfImage_createFromPixels(size, buf)
  raise Error, "sfImage_createFromPixels returned NULL" if ptr.null?

  img = allocate
  img.send(:_take_ownership, ptr)
  img
end

.load(path) ⇒ Object

Raises:



36
37
38
39
40
41
42
# File 'lib/sfml/graphics/image.rb', line 36

def self.load(path)
  ptr = C::Graphics.sfImage_createFromFile(path.to_s)
  raise Error, "Could not load image from #{path.inspect}" if ptr.null?
  img = allocate
  img.send(:_take_ownership, ptr)
  img
end

Instance Method Details

#[](x, y) ⇒ Object

Read the colour of a single pixel.



72
73
74
75
76
# File 'lib/sfml/graphics/image.rb', line 72

def [](x, y)
  coord = C::System::Vector2u.new
  coord[:x] = Integer(x); coord[:y] = Integer(y)
  Color.from_native(C::Graphics.sfImage_getPixel(@handle, coord))
end

#[]=(x, y, color) ⇒ Object

Write a single pixel.



79
80
81
82
83
# File 'lib/sfml/graphics/image.rb', line 79

def []=(x, y, color)
  coord = C::System::Vector2u.new
  coord[:x] = Integer(x); coord[:y] = Integer(y)
  C::Graphics.sfImage_setPixel(@handle, coord, color.to_native)
end

#flip_horizontallyObject



107
108
109
110
# File 'lib/sfml/graphics/image.rb', line 107

def flip_horizontally
  C::Graphics.sfImage_flipHorizontally(@handle)
  self
end

#flip_verticallyObject



112
113
114
115
# File 'lib/sfml/graphics/image.rb', line 112

def flip_vertically
  C::Graphics.sfImage_flipVertically(@handle)
  self
end

#heightObject



69
# File 'lib/sfml/graphics/image.rb', line 69

def height = size.y

#mask_color!(color, alpha: 0) ⇒ Object

Replace any pixel matching ‘color` with that colour at `alpha` opacity — typical use is to turn a fixed background colour transparent: img.mask_color!(SFML::Color.magenta, alpha: 0).



102
103
104
105
# File 'lib/sfml/graphics/image.rb', line 102

def mask_color!(color, alpha: 0)
  C::Graphics.sfImage_createMaskFromColor(@handle, color.to_native, Integer(alpha))
  self
end

#pixelsObject

Write the entire pixel buffer back as a Ruby String. Useful for piping to image-processing libraries or writing custom file formats. Format: width*height*4 bytes, RGBA, row-major from top-left.



88
89
90
91
# File 'lib/sfml/graphics/image.rb', line 88

def pixels
  ptr = C::Graphics.sfImage_getPixelsPtr(@handle)
  ptr.read_bytes(width * height * 4)
end

#save(path) ⇒ Object

Raises:



93
94
95
96
97
# File 'lib/sfml/graphics/image.rb', line 93

def save(path)
  ok = C::Graphics.sfImage_saveToFile(@handle, path.to_s)
  raise Error, "Could not save image to #{path.inspect}" unless ok
  path
end

#sizeObject



64
65
66
# File 'lib/sfml/graphics/image.rb', line 64

def size
  Vector2.from_native(C::Graphics.sfImage_getSize(@handle))
end

#widthObject



68
# File 'lib/sfml/graphics/image.rb', line 68

def width  = size.x