module ChunkyPNG::Canvas::Resampling

def steps_residues(width, new_width)

Returns:
  • (Array, Array) - Two arrays of indicies and residues

Parameters:
  • new_width (Integer) -- The width of the destination
  • width (Integer) -- The width of the source
def steps_residues(width, new_width)
  indicies = Array.new(size=new_width, obj=nil)
  residues = Array.new(size=new_width, obj=nil)
  
  # This works by accumulating the fractional error and
  # overflowing when necessary.
  # We use mixed number arithmetic with a denominator of
  # 2 * new_width
  base_step = width / new_width
  err_step = (width % new_width) << 1
  denominator = (new_width) << 1
          
  # Initial pixel
  index = (width - new_width) / denominator
  err = (width - new_width) % denominator
  for i in 1..new_width
    indicies[i-1] = index
    residues[i-1] = (255.0 * err.to_f / denominator.to_f).round
    index += base_step
    err += err_step
    if err >= denominator
      index += 1
      err -= denominator
    end
  end
  return indicies, residues
end