Module: Gsplat::Compression::Quantizer

Defined in:
lib/gsplat/compression/quantizer.rb

Overview

Per-channel linear quantization used by PNG parameter grids.

Class Method Summary collapse

Class Method Details

.decode(pixels, metadata) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gsplat/compression/quantizer.rb', line 22

def decode(pixels, )
  shape = .fetch("shape")
  feature_count = shape.drop(1).inject(1, :*)
  mins = .fetch("mins")
  maxs = .fetch("maxs")
  limit = (1 << .fetch("bits")) - 1
  values = pixels.reshape(pixels.shape[0] * pixels.shape[1], feature_count).to_a.flat_map do |row|
    row.each_with_index.map do |value, channel|
      mins[channel] + ((value.to_f / limit) * (maxs[channel] - mins[channel]))
    end
  end
  numeric_type(.fetch("dtype")).cast(values).reshape(*shape)
end

.dtype_name(type) ⇒ Object

Raises:



65
66
67
68
69
70
# File 'lib/gsplat/compression/quantizer.rb', line 65

def dtype_name(type)
  return "float32" if type == Numo::SFloat
  return "float64" if type == Numo::DFloat

  raise NotSupportedError, "compression requires float32 or float64 parameters"
end

.encode(array, side, bits:) ⇒ Object

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/gsplat/compression/quantizer.rb', line 9

def encode(array, side, bits:)
  feature_count = array.size / array.shape[0]
  raise NotSupportedError, "PNG parameter grids support at most four channels" if feature_count > 4

  flat = array.reshape(array.shape[0], feature_count)
  mins, maxs = channel_extrema(flat, feature_count)
  limit = (1 << bits) - 1
  values = quantized_values(flat, mins, maxs, limit)
  type = bits <= 8 ? Numo::UInt8 : Numo::UInt16
  pixels = type.cast(values).reshape(side, side, feature_count)
  [pixels, (array, mins, maxs, bits)]
end

.numeric_type(name) ⇒ Object



72
73
74
75
76
# File 'lib/gsplat/compression/quantizer.rb', line 72

def numeric_type(name)
  { "float32" => Numo::SFloat, "float64" => Numo::DFloat }.fetch(name) do
    raise NotSupportedError, "unsupported compressed dtype #{name.inspect}"
  end
end