Module: ChunkyPNG::Canvas::Resampling

### Home Classes Methods

### In Files

- lib/chunky_png/canvas/resampling.rb

### Methods

- #resample
- #resample_bilinear
- #resample_bilinear!
- #resample_nearest_neighbor
- #resample_nearest_neighbor!
- #resize
- #steps
- #steps_residues

### Files

- BENCHMARKING.rdoc
- CHANGELOG.rdoc
- CONTRIBUTING.rdoc
- README.rdoc

### Class/Module Index

Quicksearch

- ChunkyPNG
- ChunkyPNG::CRCMismatch
- ChunkyPNG::Canvas
- ChunkyPNG::Canvas::Adam7Interlacing
- ChunkyPNG::Canvas::DataUrlExporting
- ChunkyPNG::Canvas::DataUrlImporting
- ChunkyPNG::Canvas::Drawing
- ChunkyPNG::Canvas::Masking
- ChunkyPNG::Canvas::Operations
- ChunkyPNG::Canvas::PNGDecoding
- ChunkyPNG::Canvas::PNGEncoding
- ChunkyPNG::Canvas::Resampling
- ChunkyPNG::Canvas::StreamExporting
- ChunkyPNG::Canvas::StreamImporting
- ChunkyPNG::Chunk
- ChunkyPNG::Chunk::Base
- ChunkyPNG::Chunk::CompressedText
- ChunkyPNG::Chunk::End
- ChunkyPNG::Chunk::Generic
- ChunkyPNG::Chunk::Header
- ChunkyPNG::Chunk::ImageData
- ChunkyPNG::Chunk::InternationalText
- ChunkyPNG::Chunk::Palette
- ChunkyPNG::Chunk::Text
- ChunkyPNG::Chunk::Transparency
- ChunkyPNG::Color
- ChunkyPNG::Datastream
- ChunkyPNG::Dimension
- ChunkyPNG::Exception
- ChunkyPNG::ExpectationFailed
- ChunkyPNG::Image
- ChunkyPNG::NotSupported
- ChunkyPNG::OutOfBounds
- ChunkyPNG::Palette
- ChunkyPNG::Point
- ChunkyPNG::RMagick
- ChunkyPNG::SignatureMismatch
- ChunkyPNG::Vector
- Enumerable
- String

No matching classes.

# 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) click to toggle source
Also aliased as: resize
Alias for: resample_nearest_neighbor
resample_bilinear(new_width, new_height) click to toggle source

    # File lib/chunky_png/canvas/resampling.rb, line 139
    def resample_bilinear(new_width, new_height)
      dup.resample_bilinear!(new_width, new_height)
    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, line 103
    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.new(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

resample_nearest_neighbor(new_width, new_height) click to toggle source

    # File lib/chunky_png/canvas/resampling.rb, line 95
    def resample_nearest_neighbor(new_width, new_height)
      dup.resample_nearest_neighbor!(new_width, new_height)
    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, line 78
    def resample_nearest_neighbor!(new_width, new_height)
      steps_x = steps(width, new_width)
      steps_y = steps(height, new_height)

      pixels = Array.new(new_width*new_height)
      i = 0
      for y in steps_y
        for x in steps_x
          pixels[i] = get_pixel(x, y)
          i += 1
        end
      end

      replace_canvas!(new_width.to_i, new_height.to_i, pixels)
    end

resize(new_width, new_height) click to toggle source
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, line 24
    def steps(width, new_width)
      indicies, residues = steps_residues(width, new_width)

      for i in 1..new_width
        indicies[i-1] = (indicies[i-1] + (residues[i-1] + 127)/255)
      end
      return indicies
    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, line 41
    def steps_residues(width, new_width)
      indicies = Array.new(new_width, obj=nil)
      residues = Array.new(new_width, obj=nil)

      # This works by accumulating the fractional error and
      # overflowing when necessary.

      # We use mixed number arithmetic with a denominator of
      # 2 * new_width
      base_step = width / new_width
      err_step = (width % new_width) << 1
      denominator = (new_width) << 1

      # Initial pixel
      index = (width - new_width) / denominator
      err = (width - new_width) % denominator

      for i in 1..new_width
        indicies[i-1] = index
        residues[i-1] = (255.0 * err.to_f / denominator.to_f).round

        index += base_step
        err += err_step
        if err >= denominator
          index += 1
          err -= denominator
        end
      end

      return indicies, residues
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.