Class: JXL::Modular::Weighted

Inherits:
Object
  • Object
show all
Defined in:
lib/jxl/modular/weighted.rb

Constant Summary collapse

DIV_LOOKUP =
Array.new(64) { |i| (1 << 24) / (i + 1) }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, width) ⇒ Weighted

Returns a new instance of Weighted.



10
11
12
13
14
15
16
# File 'lib/jxl/modular/weighted.rb', line 10

def initialize(header, width)
  @header = header
  @width = width
  @stride = width + 2
  @pred_errors = Array.new(4) { Array.new(@stride * 2, 0) }
  @errors = Array.new(@stride * 2, 0)
end

Instance Attribute Details

#propertyObject (readonly)

Returns the value of attribute property.



8
9
10
# File 'lib/jxl/modular/weighted.rb', line 8

def property
  @property
end

Instance Method Details

#predict(plane, x, y) ⇒ Object

rubocop:disable Metrics/AbcSize



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
# File 'lib/jxl/modular/weighted.rb', line 18

def predict(plane, x, y) # rubocop:disable Metrics/AbcSize
  n = Predictor.neighbors(plane, x, y)
  positions(x, y)
  weights = @pred_errors.each_with_index.map do |errors, i|
    sum = Num.u32(errors[@pos_n] + errors[@pos_ne] + errors[@pos_nw])
    error_weight(sum, @header.weights[i])
  end

  north, west, northeast, northwest, northnorth =
    %i[top left topright topleft toptop].map { n.fetch(_1) << 3 }
  error_west = x.zero? ? 0 : @errors[@current_row + x - 1]
  error_north = @errors[@pos_n]
  error_northwest = @errors[@pos_nw]
  error_northeast = @errors[@pos_ne]
  @property = [error_west, error_north, error_northwest, error_northeast].max_by(&:abs)

  sum = error_north + error_west
  p = @header.p
  @predictions = [
    west + northeast - north,
    north - (((sum + error_northeast) * p[0]) >> 5),
    west - (((sum + error_northwest) * p[1]) >> 5),
    north - (((error_northwest * p[2]) + (error_north * p[3]) + (error_northeast * p[4]) +
              ((northnorth - north) * p[5]) + ((northwest - west) * p[6])) >> 5)
  ]
  @prediction = weighted_average(weights)
  same_sign = ((error_north ^ error_west) | (error_north ^ error_northwest)).positive?
  return (@prediction + 3) >> 3 if same_sign

  @prediction = @prediction.clamp([west, northeast, north].min, [west, northeast, north].max)
  (@prediction + 3) >> 3
end

#update(value, x) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/jxl/modular/weighted.rb', line 51

def update(value, x)
  scaled = value << 3
  @errors[@current_row + x] = Num.i32(@prediction - scaled)
  @pred_errors.each_with_index do |errors, i|
    error = Num.u32(((@predictions[i] - scaled).abs + 3) >> 3)
    errors[@current_row + x] = error
    errors[@previous_row + x + 1] = Num.u32(errors[@previous_row + x + 1] + error)
  end
end