Class: Skia::Bitmap

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

Instance Attribute Summary

Attributes inherited from Base

#ptr

Instance Method Summary collapse

Methods inherited from Base

#null?, release_callback, #to_ptr

Constructor Details

#initialize(ptr = nil) ⇒ Bitmap

Returns a new instance of Bitmap.



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

def initialize(ptr = nil)
  super(ptr || Native.sk_bitmap_new, nil)
  @pixel_storage = nil
end

Instance Method Details

#alloc_pixels(image_info, flags: nil) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/skia/bitmap.rb', line 57

def alloc_pixels(image_info, flags: nil)
  info = coerce_image_info(image_info)
  row_bytes = info.min_row_bytes
  @pixel_storage = nil
  return Native.sk_bitmap_try_alloc_pixels_with_flags(@ptr, info.to_struct, flags.to_i) if flags

  Native.sk_bitmap_try_alloc_pixels(@ptr, info.to_struct, row_bytes)
end

#allocated?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/skia/bitmap.rb', line 43

def allocated?
  !Native.sk_bitmap_is_null(@ptr)
end

#byte_countObject



28
29
30
# File 'lib/skia/bitmap.rb', line 28

def byte_count
  Native.sk_bitmap_get_byte_count(@ptr)
end

#erase(color, rect: nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/skia/bitmap.rb', line 66

def erase(color, rect: nil)
  color_value = color.is_a?(Color) ? color.to_i : color
  if rect
    irect = coerce_irect(rect)
    Native.sk_bitmap_erase_rect(@ptr, color_value, irect.to_struct)
  else
    Native.sk_bitmap_erase(@ptr, color_value)
  end
  self
end

#extract_subset(rect) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/skia/bitmap.rb', line 88

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

  subset
end

#heightObject



20
21
22
# File 'lib/skia/bitmap.rb', line 20

def height
  info.height
end

#immutable=(value) ⇒ Object



51
52
53
54
55
# File 'lib/skia/bitmap.rb', line 51

def immutable=(value)
  return unless value

  Native.sk_bitmap_set_immutable(@ptr)
end

#immutable?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/skia/bitmap.rb', line 47

def immutable?
  Native.sk_bitmap_is_immutable(@ptr)
end

#infoObject



10
11
12
13
14
# File 'lib/skia/bitmap.rb', line 10

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

#make_shader(tile_x: :clamp, tile_y: :clamp, matrix: nil) ⇒ Object

Raises:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/skia/bitmap.rb', line 97

def make_shader(tile_x: :clamp, tile_y: :clamp, matrix: nil)
  matrix_struct = matrix&.to_struct
  sampling = Native::SKSamplingOptions.new
  sampling[:fMaxAniso] = 0
  sampling[:fUseCubic] = 0
  sampling[:fCubicB] = 0.0
  sampling[:fCubicC] = 0.0
  sampling[:fFilter] = :nearest
  sampling[:fMipmap] = :none

  ptr = Native.sk_bitmap_make_shader(@ptr, tile_x, tile_y, sampling, matrix_struct)
  raise Error, 'Failed to create shader from bitmap' if ptr.nil? || ptr.null?

  Shader.new(ptr)
end

#peek_pixelsObject



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

def peek_pixels
  pixmap = Pixmap.new
  return nil unless Native.sk_bitmap_peek_pixels(@ptr, pixmap.ptr)

  pixmap
end

#pixel_color(x, y) ⇒ Object



77
78
79
# File 'lib/skia/bitmap.rb', line 77

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

#pixelsObject



36
37
38
39
40
41
# File 'lib/skia/bitmap.rb', line 36

def pixels
  ptr = pixels_ptr
  return nil if ptr.nil? || ptr.null?

  ptr.read_bytes(byte_count)
end

#pixels_ptrObject



32
33
34
# File 'lib/skia/bitmap.rb', line 32

def pixels_ptr
  Native.sk_bitmap_get_pixels(@ptr, nil)
end

#resetObject



120
121
122
123
124
# File 'lib/skia/bitmap.rb', line 120

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

#row_bytesObject



24
25
26
# File 'lib/skia/bitmap.rb', line 24

def row_bytes
  Native.sk_bitmap_get_row_bytes(@ptr)
end

#to_imageObject

Raises:



113
114
115
116
117
118
# File 'lib/skia/bitmap.rb', line 113

def to_image
  ptr = Native.sk_image_new_from_bitmap(@ptr)
  raise Error, 'Failed to create image from bitmap' if ptr.nil? || ptr.null?

  Image.new(ptr)
end

#widthObject



16
17
18
# File 'lib/skia/bitmap.rb', line 16

def width
  info.width
end