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
|