Module: Gsplat::IO::VipsImageBackend

Defined in:
lib/gsplat/io/image_backends.rb

Overview

Optional ruby-vips implementation.

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
# File 'lib/gsplat/io/image_backends.rb', line 9

def available?
  require "vips"
  true
rescue LoadError, StandardError
  false
end

.read(path) ⇒ Object



16
17
18
19
20
21
# File 'lib/gsplat/io/image_backends.rb', line 16

def read(path)
  image = Vips::Image.new_from_file(path.to_s, access: :sequential)
  scale = { uchar: 255.0, ushort: 65_535.0 }.fetch(image.format.to_sym, 1.0)
  rgb = rgb_image(image).cast(:float)
  Numo::SFloat.from_binary(rgb.write_to_memory).reshape(rgb.height, rgb.width, 3) / scale
end

.write(path, array) ⇒ Object



23
24
25
26
27
28
# File 'lib/gsplat/io/image_backends.rb', line 23

def write(path, array)
  bytes = quantized_bytes(array)
  image = Vips::Image.new_from_memory(bytes, array.shape[1], array.shape[0], 3, :uchar)
  image.write_to_file(path.to_s)
  path
end