module ChunkyPNG::Canvas::Resampling

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

Public Instance Methods

resample(new_width, new_height)
Also aliased as: resize
resample_bilinear(new_width, new_height) click to toggle source
    # File lib/chunky_png/canvas/resampling.rb
134 def resample_bilinear(new_width, new_height)
135   dup.resample_bilinear!(new_width, new_height)
136 end
resample_bilinear!(new_width, new_height) click to toggle source

Resamples the canvas with bilinear interpolation. @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.

    # File lib/chunky_png/canvas/resampling.rb
 98 def resample_bilinear!(new_width, new_height)
 99   index_x, interp_x = steps_residues(width, new_width)
100   index_y, interp_y = steps_residues(height, new_height)
101 
102   pixels = Array.new(new_width * new_height)
103   i = 0
104   for y in 1..new_height
105     # Clamp the indicies to the edges of the image
106     y1 = [index_y[y - 1], 0].max
107     y2 = [index_y[y - 1] + 1, height - 1].min
108     y_residue = interp_y[y - 1]
109 
110     for x in 1..new_width
111       # Clamp the indicies to the edges of the image
112       x1 = [index_x[x - 1], 0].max
113       x2 = [index_x[x - 1] + 1, width - 1].min
114       x_residue = interp_x[x - 1]
115 
116       pixel_11 = get_pixel(x1, y1)
117       pixel_21 = get_pixel(x2, y1)
118       pixel_12 = get_pixel(x1, y2)
119       pixel_22 = get_pixel(x2, y2)
120 
121       # Interpolate by Row
122       pixel_top = ChunkyPNG::Color.interpolate_quick(pixel_21, pixel_11, x_residue)
123       pixel_bot = ChunkyPNG::Color.interpolate_quick(pixel_22, pixel_12, x_residue)
124 
125       # Interpolate by Column
126 
127       pixels[i] = ChunkyPNG::Color.interpolate_quick(pixel_bot, pixel_top, y_residue)
128       i += 1
129     end
130   end
131   replace_canvas!(new_width.to_i, new_height.to_i, pixels)
132 end
resample_nearest_neighbor(new_width, new_height) click to toggle source
   # File lib/chunky_png/canvas/resampling.rb
90 def resample_nearest_neighbor(new_width, new_height)
91   dup.resample_nearest_neighbor!(new_width, new_height)
92 end
Also aliased as: resample
resample_nearest_neighbor!(new_width, new_height) click to toggle source

Resamples the canvas using nearest neighbor interpolation. @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.

   # File lib/chunky_png/canvas/resampling.rb
74 def resample_nearest_neighbor!(new_width, new_height)
75   steps_x = steps(width, new_width)
76   steps_y = steps(height, new_height)
77 
78   pixels = Array.new(new_width * new_height)
79   i = 0
80   for y in steps_y
81     for x in steps_x
82       pixels[i] = get_pixel(x, y)
83       i += 1
84     end
85   end
86 
87   replace_canvas!(new_width.to_i, new_height.to_i, pixels)
88 end
resize(new_width, new_height)
Alias for: resample
steps(width, new_width) click to toggle source

Integer Interpolation between two values

Used for generating indicies for interpolation (eg, nearest neighbour).

@param [Integer] width The width of the source @param [Integer] new_width The width of the destination @return [Array<Integer>] An Array of Integer indicies

   # File lib/chunky_png/canvas/resampling.rb
21 def steps(width, new_width)
22   indicies, residues = steps_residues(width, new_width)
23 
24   for i in 1..new_width
25     indicies[i - 1] = (indicies[i - 1] + (residues[i - 1] + 127) / 255)
26   end
27   indicies
28 end
steps_residues(width, new_width) click to toggle source

Fractional Interpolation between two values

Used for generating values for interpolation (eg, bilinear). Produces both the indices and the interpolation factors (residues).

@param [Integer] width The width of the source @param [Integer] new_width The width of the destination @return [Array<Integer>, Array<Integer>] Two arrays of indicies and residues

   # File lib/chunky_png/canvas/resampling.rb
38 def steps_residues(width, new_width)
39   indicies = Array.new(new_width, nil)
40   residues = Array.new(new_width, nil)
41 
42   # This works by accumulating the fractional error and
43   # overflowing when necessary.
44 
45   # We use mixed number arithmetic with a denominator of
46   # 2 * new_width
47   base_step = width / new_width
48   err_step = (width % new_width) << 1
49   denominator = new_width << 1
50 
51   # Initial pixel
52   index = (width - new_width) / denominator
53   err = (width - new_width) % denominator
54 
55   for i in 1..new_width
56     indicies[i - 1] = index
57     residues[i - 1] = (255.0 * err.to_f / denominator.to_f).round
58 
59     index += base_step
60     err += err_step
61     if err >= denominator
62       index += 1
63       err -= denominator
64     end
65   end
66 
67   [indicies, residues]
68 end