module ChunkyPNG::Canvas::Resampling

def resample_bilinear!(new_width, new_height)

Returns:
  • (ChunkyPNG::Canvas) - A new canvas instance with the resampled pixels.

Parameters:
  • new_height (Integer) -- The height of the resampled canvas.
  • new_width (Integer) -- The width of the resampled canvas.
def resample_bilinear!(new_width, new_height)
  index_x, interp_x = steps_residues(width, new_width)
  index_y, interp_y = steps_residues(height, new_height)
  pixels = Array(size=new_width*new_height)
  i = 0
  for y in 1..new_height
    # Clamp the indicies to the edges of the image
    y1 = [index_y[y-1], 0].max
    y2 = [index_y[y-1] + 1, height - 1].min
    y_residue = interp_y[y-1]
    for x in 1..new_width
      # Clamp the indicies to the edges of the image
      x1 = [index_x[x-1], 0].max
      x2 = [index_x[x-1] + 1, width - 1].min
      x_residue = interp_x[x-1]
      pixel_11 = get_pixel(x1, y1)
      pixel_21 = get_pixel(x2, y1)
      pixel_12 = get_pixel(x1, y2)
      pixel_22 = get_pixel(x2, y2)
      # Interpolate by Row
      pixel_top = ChunkyPNG::Color.interpolate_quick(pixel_21, pixel_11, x_residue)
      pixel_bot = ChunkyPNG::Color.interpolate_quick(pixel_22, pixel_12, x_residue)
      # Interpolate by Column
      pixels[i] = ChunkyPNG::Color.interpolate_quick(pixel_bot, pixel_top, y_residue)
      i += 1
    end
  end
  replace_canvas!(new_width.to_i, new_height.to_i, pixels)
end