Module: JXL::Features::Splines

Defined in:
lib/jxl/features/splines.rb

Overview

rubocop:disable Metrics/ModuleLength

Constant Summary collapse

CHANNEL_WEIGHTS =
[0.0042, 0.075, 0.07, 0.3333].freeze
SQRT_HALF =
Math.sqrt(0.5)
SQRT_TWO =
Math.sqrt(2.0)

Class Method Summary collapse

Class Method Details

.apply(channels, set, correlation) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/jxl/features/splines.rb', line 52

def apply(channels, set, correlation)
  return channels unless set

  set.splines.each_with_index do |quantized, index|
    spline = dequantize(quantized, set.starting_points[index], set.quantization_adjustment, correlation)
    draw!(channels, spline)
  end
  channels
end

.read(reader, width, height) ⇒ Object

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jxl/features/splines.rb', line 15

def read(reader, width, height)
  decoder = Entropy::Decoder.read(reader, 6)
  count = decoder.read_uint(2) + 1
  limit = [1 << 20, width * height / 2].min
  raise ResourceLimitError, "too many splines" if count > limit

  starting_points = []
  last_x = last_y = 0
  count.times do |index|
    dx = decoder.read_uint(1)
    dy = decoder.read_uint(1)
    x = index.zero? ? dx : last_x + Num.unpack_signed(dx)
    y = index.zero? ? dy : last_y + Num.unpack_signed(dy)
    validate_position!(x, y)
    starting_points << [x.to_f, y.to_f]
    last_x = x
    last_y = y
  end
  adjustment = Num.unpack_signed(decoder.read_uint(0))
  total_points = count
  splines = Array.new(count) do
    points = decoder.read_uint(3)
    total_points += points
    raise ResourceLimitError, "too many spline control points" if total_points > limit

    deltas = Array.new(points) do
      [Num.unpack_signed(decoder.read_uint(4)),
       Num.unpack_signed(decoder.read_uint(4))]
    end
    colour = Array.new(3) { Array.new(32) { Num.unpack_signed(decoder.read_uint(5)) } }
    sigma = Array.new(32) { Num.unpack_signed(decoder.read_uint(5)) }
    QuantizedSpline.new(deltas:, colour_dct: colour, sigma_dct: sigma)
  end
  decoder.final_state!
  SplineSet.new(quantization_adjustment: adjustment, starting_points:, splines:)
end