Sha256: 165792432bccb096c238f9b73eb532a88659e90b0f0b18135c3f2c736d8786ef

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

require 'ruby-processing'

class PixelNeighbourDifferencesSketch < Processing::App

  def setup
    @img = load_image 'sunflower.jpg'
    @destination = create_image @img.width, @img.height, RGB
  end

  def draw
    # We are going to look at both image's pixels
    @img.load_pixels
    @destination.load_pixels

    # Since we are looking at left neighbors
    # We skip the first column
    (1...width).each do |x|
      (0...height).each do |y|
        # Pixel location and color
        loc = x + y * @img.width
        pix = @img.pixels[loc]

        # Pixel to the left location and color
        left_loc = (x - 1) + y * @img.width
        left_pix = @img.pixels[left_loc]

        # New color is difference between pixel and left neighbor
        diff = (brightness(pix) - brightness(left_pix)).abs
        @destination.pixels[loc] = color(diff) 
      end
    end

    # We changed the pixels in @destination
    @destination.update_pixels
    # Display the @destination
    image @destination, 0, 0
  end

end

PixelNeighbourDifferencesSketch.new :title => "Pixel Neighbour Differences", :width => 200, :height => 200


Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby-processing-1.0.1 samples/learning_processing/chapter_15/12_pixel_neighbour_differences.rb
ruby-processing-1.0.2 samples/learning_processing/chapter_15/12_pixel_neighbour_differences.rb
ruby-processing-1.0.3 samples/learning_processing/chapter_15/12_pixel_neighbour_differences.rb
ruby-processing-1.0.4 samples/learning_processing/chapter_15/12_pixel_neighbour_differences.rb