Module: Gsplat::IO::ChunkyPngImageBackend

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

Overview

Portable chunky_png implementation.

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'lib/gsplat/io/image_backends.rb', line 48

def available?
  require "chunky_png"
  true
rescue LoadError
  false
end

.read(path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/gsplat/io/image_backends.rb', line 55

def read(path)
  image = ChunkyPNG::Image.from_file(path.to_s)
  values = image.height.times.flat_map do |y_coord|
    image.width.times.flat_map do |x_coord|
      pixel = image[x_coord, y_coord]
      [ChunkyPNG::Color.r(pixel), ChunkyPNG::Color.g(pixel), ChunkyPNG::Color.b(pixel)]
    end
  end
  Numo::SFloat.cast(values).reshape(image.height, image.width, 3) / 255.0
end

.write(path, array) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gsplat/io/image_backends.rb', line 66

def write(path, array)
  image = ChunkyPNG::Image.new(array.shape[1], array.shape[0])
  array.shape[0].times do |y_coord|
    array.shape[1].times do |x_coord|
      rgb = array[y_coord, x_coord, true].to_a.map do |value|
        (value.to_f.clamp(0.0, 1.0) * 255).round
      end
      image[x_coord, y_coord] = ChunkyPNG::Color.rgb(*rgb)
    end
  end
  image.save(path.to_s)
  path
end