Class: Skia::Pixmap

Inherits:
Base
  • Object
show all
Defined in:
lib/skia/pixmap.rb

Instance Attribute Summary

Attributes inherited from Base

#ptr

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#null?, release_callback, #to_ptr

Constructor Details

#initialize(ptr = nil) ⇒ Pixmap



5
6
7
8
# File 'lib/skia/pixmap.rb', line 5

def initialize(ptr = nil)
  super(ptr || Native.sk_pixmap_new, :sk_pixmap_destructor)
  @pixel_storage = nil
end

Class Method Details

.coerce_image_info(image_info) ⇒ Object

Raises:

  • (ArgumentError)


97
98
99
100
101
# File 'lib/skia/pixmap.rb', line 97

def self.coerce_image_info(image_info)
  return image_info if image_info.is_a?(ImageInfo)

  raise ArgumentError, 'image_info must be a Skia::ImageInfo'
end

.from_pixels(image_info, pixels, row_bytes: nil) ⇒ Object

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/skia/pixmap.rb', line 10

def self.from_pixels(image_info, pixels, row_bytes: nil)
  info = coerce_image_info(image_info)
  bytes = pixels.is_a?(String) ? pixels.dup : pixels.pack('C*')
  row_bytes ||= info.min_row_bytes

  if bytes.bytesize < row_bytes * info.height
    raise ArgumentError, "pixel buffer is too small: #{bytes.bytesize} bytes"
  end

  storage = FFI::MemoryPointer.new(:uint8, bytes.bytesize)
  storage.write_bytes(bytes)

  ptr = Native.sk_pixmap_new_with_params(info.to_struct, storage, row_bytes)
  raise Error, 'Failed to create pixmap from pixels' if ptr.nil? || ptr.null?

  pixmap = new(ptr)
  pixmap.instance_variable_set(:@pixel_storage, storage)
  pixmap
end

Instance Method Details

#color_spaceObject



40
41
42
# File 'lib/skia/pixmap.rb', line 40

def color_space
  ColorSpace.wrap(Native.sk_pixmap_get_colorspace(@ptr), retain: true)
end

#color_space=(value) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/skia/pixmap.rb', line 44

def color_space=(value)
  unless value.nil? || value.is_a?(ColorSpace)
    raise ArgumentError, 'color_space must be a Skia::ColorSpace or nil'
  end

  Native.sk_pixmap_set_colorspace(@ptr, value&.ptr)
end

#extract_subset(rect) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/skia/pixmap.rb', line 80

def extract_subset(rect)
  irect = coerce_irect(rect)
  subset = self.class.new
  success = Native.sk_pixmap_extract_subset(@ptr, subset.ptr, irect.to_struct)
  return nil unless success

  subset
end

#infoObject



30
31
32
33
34
# File 'lib/skia/pixmap.rb', line 30

def info
  info_struct = Native::SKImageInfo.new
  Native.sk_pixmap_get_info(@ptr, info_struct)
  ImageInfo.from_struct(info_struct)
end

#opaque?Boolean



56
57
58
# File 'lib/skia/pixmap.rb', line 56

def opaque?
  Native.sk_pixmap_compute_is_opaque(@ptr)
end

#pixel_color(x, y) ⇒ Object



52
53
54
# File 'lib/skia/pixmap.rb', line 52

def pixel_color(x, y)
  Color.new(Native.sk_pixmap_get_pixel_color(@ptr, x.to_i, y.to_i))
end

#read_pixels(dst_info, src_x: 0, src_y: 0, row_bytes: nil) ⇒ Object

Raises:



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/skia/pixmap.rb', line 68

def read_pixels(dst_info, src_x: 0, src_y: 0, row_bytes: nil)
  info = self.class.coerce_image_info(dst_info)
  row_bytes ||= info.min_row_bytes
  byte_size = row_bytes * info.height
  pixels = FFI::MemoryPointer.new(:uint8, byte_size)

  ok = Native.sk_pixmap_read_pixels(@ptr, info.to_struct, pixels, row_bytes, src_x.to_i, src_y.to_i)
  raise Error, 'Failed to read pixels from pixmap' unless ok

  pixels.read_bytes(byte_size)
end

#resetObject



89
90
91
92
93
# File 'lib/skia/pixmap.rb', line 89

def reset
  Native.sk_pixmap_reset(@ptr)
  @pixel_storage = nil
  self
end

#row_bytesObject



36
37
38
# File 'lib/skia/pixmap.rb', line 36

def row_bytes
  Native.sk_pixmap_get_row_bytes(@ptr)
end

#writable_addr(x = nil, y = nil) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/skia/pixmap.rb', line 60

def writable_addr(x = nil, y = nil)
  if x.nil? || y.nil?
    Native.sk_pixmap_get_writable_addr(@ptr)
  else
    Native.sk_pixmap_get_writeable_addr_with_xy(@ptr, x.to_i, y.to_i)
  end
end