Module: ChunkyPNG::Canvas::Resampling
- Included in:
- ChunkyPNG::Canvas
- Defined in:
- lib/chunky_png/canvas/resampling.rb
Overview
The ChunkyPNG::Canvas::Resampling module defines methods to perform image resampling to a ChunkyPNG::Canvas.
Currently, only the nearest neighbor algorithm is implemented. Bilinear and cubic algorithms may be added later on.
Instance Method Summary collapse
- #resample_nearest_neighbor(new_width, new_height) ⇒ Object (also: #resample)
-
#resample_nearest_neighbor!(new_width, new_height) ⇒ ChunkyPNG::Canvas
Resamples the canvas.
Instance Method Details
#resample_nearest_neighbor(new_width, new_height) ⇒ Object Also known as: resample
40 41 42 |
# File 'lib/chunky_png/canvas/resampling.rb', line 40 def resample_nearest_neighbor(new_width, new_height) dup.resample_nearest_neighbor!(new_width, new_height) end |
#resample_nearest_neighbor!(new_width, new_height) ⇒ ChunkyPNG::Canvas
Resamples the canvas.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/chunky_png/canvas/resampling.rb', line 17 def resample_nearest_neighbor!(new_width, new_height) resampled_image = self.class.new(new_width.to_i, new_height.to_i) width_ratio = width.to_f / new_width.to_f height_ratio = height.to_f / new_height.to_f pixels = [] for y in 1..new_height do source_y = (y - 0.5) * height_ratio + 0.5 input_y = source_y.to_i for x in 1..new_width do source_x = (x - 0.5) * width_ratio + 0.5 input_x = source_x.to_i pixels << get_pixel([input_x - 1, 0].max, [input_y - 1, 0].max) end end replace_canvas!(new_width.to_i, new_height.to_i, pixels) end |