Class: Img2png::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/imsg-grep/images/img2png.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, data: nil) ⇒ Image

Returns a new instance of Image.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/imsg-grep/images/img2png.rb', line 14

def initialize(path:nil, data:nil)
  [path, data].compact.size == 1 or raise ArgumentError, "One of path: or data: must be specified"
  out_w = FFI::MemoryPointer.new(:int)
  out_h = FFI::MemoryPointer.new(:int)

  @handle = case
  when data then Img2png.img2png_load(data, data.bytesize, out_w, out_h)
  when path then Img2png.img2png_load_path(path, out_w, out_h)
  end
  raise "Failed to load image" if @handle.null?

  @width  = out_w.read_int
  @height = out_h.read_int
end

Class Method Details

.finalize(handle) ⇒ Object



56
57
58
# File 'lib/imsg-grep/images/img2png.rb', line 56

def self.finalize(handle)
  proc { Img2png.img2png_release(handle) unless handle.null? }
end

Instance Method Details

#convert(fit_w: nil, fit_h: nil, box_w: nil, box_h: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/imsg-grep/images/img2png.rb', line 31

def convert(fit_w: nil, fit_h: nil, box_w: nil, box_h: nil)
  out_data = FFI::MemoryPointer.new(:pointer)
  out_len  = FFI::MemoryPointer.new(:int)

  success = Img2png.img2png_convert(
    @handle,
    fit_w || 0, fit_h || 0,
    box_w || 0, box_h || 0,
    out_data, out_len
  )

  return nil unless success

  ptr = out_data.read_pointer
  len = out_len.read_int
  result = ptr.read_bytes(len)
  Img2png.img2png_free(ptr)
  result
end

#dimensionsObject



29
# File 'lib/imsg-grep/images/img2png.rb', line 29

def dimensions = [@width, @height]

#releaseObject



51
52
53
54
# File 'lib/imsg-grep/images/img2png.rb', line 51

def release
  Img2png.img2png_release(@handle) unless @handle.null?
  @handle = FFI::Pointer::NULL
end