Skip to content

Instantly share code, notes, and snippets.

@bstolte
Created September 16, 2015 18:23
Show Gist options
  • Save bstolte/02078daafb89d1fc71b9 to your computer and use it in GitHub Desktop.
Save bstolte/02078daafb89d1fc71b9 to your computer and use it in GitHub Desktop.
Image Blur #3
class Image
attr_accessor :image, :blur
def initialize(image)
self.image = image
end
def output_image
self.image.each do |row|
puts row.join(", ")
end
end
def blur(n)
return output_image if n.zero?
height = image.length
width = image[0].length
buffer = []
# create a duplicate of image called buffer
image.length.times do |y|
buffer[y] = []
image[y].length.times do |x|
if image[y][x] == 1
buffer[y][x] = 1
else
buffer[y][x] = 0
end
end
end
# run through image and change buffer accordingly
image.each.with_index do |row, i|
row.each.with_index do |cell, j|
if cell == 1
# RIGHT
if j+1 < width
buffer[i][j+1] = 1
end
# LEFT
if (j-1) >= 0 && (j-1) < width
buffer[i][j-1] = 1
end
# BELOW
if i+1 < height
buffer[i+1][j] = 1
end
# ABOVE
if (i-1) >= 0 && (i-1) < height
buffer[i-1][j] = 1
end
end
end
end
# update image to be buffer, repeat 'n' times
self.image = buffer
blur(n - 1)
end
end
image = Image.new([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
])
image.blur(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment