Module ChunkyPNG::Canvas::Resampling
In: lib/chunky_png/canvas/resampling.rb

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.

@see ChunkyPNG::Canvas

Methods

Public Instance methods

resample(new_width, new_height)

[Source]

    # File lib/chunky_png/canvas/resampling.rb, line 38
38:       def resample_nearest_neighbor(new_width, new_height)
39:         dup.resample_nearest_neighbor!(new_width, new_height)
40:       end

Resamples the canvas. @param [Integer] new_width The width of the resampled canvas. @param [Integer] new_height The height of the resampled canvas. @return [ChunkyPNG::Canvas] A new canvas instance with the resampled pixels.

[Source]

    # File lib/chunky_png/canvas/resampling.rb, line 17
17:       def resample_nearest_neighbor!(new_width, new_height)
18:         
19:         width_ratio  = width.to_f / new_width.to_f
20:         height_ratio = height.to_f / new_height.to_f
21: 
22:         pixels = []
23:         for y in 1..new_height do
24:           source_y   = (y - 0.5) * height_ratio + 0.5
25:           input_y    = source_y.to_i
26: 
27:           for x in 1..new_width do
28:             source_x = (x - 0.5) * width_ratio + 0.5
29:             input_x  = source_x.to_i
30: 
31:             pixels << get_pixel([input_x - 1, 0].max, [input_y - 1, 0].max)
32:           end
33:         end
34:         
35:         replace_canvas!(new_width.to_i, new_height.to_i, pixels)
36:       end
resize(new_width, new_height)

Alias for resample

[Validate]