Sha256: 801f0b4ae8f6fd1bc9fb245a5b5b1142c5ad8c01b50e0680e803c3b8a61cba06

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

module ChunkyPNG
  class Canvas
    
    # 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
    module Resampling
      
      # Resamples the canvas.
      # @param [Integer] new_width The width of the resamples canvas.
      # @param [Integer] new_height The height of the resamples canvas.
      # @param [ChunkyPNG::Canvas] A new canvas instance with the resamples pixels.
      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

        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

            resampled_image.set_pixel(x - 1, y - 1, get_pixel([input_x - 1, 0].max, [input_y - 1, 0].max))
          end
        end
        
        return resampled_image
      end
      
      alias_method :resample, :resample_nearest_neighbor
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chunky_png-1.0.0.rc1 lib/chunky_png/canvas/resampling.rb