Last active
September 16, 2015 18:24
-
-
Save bstolte/65c071420c13ec43ecd6 to your computer and use it in GitHub Desktop.
Image Blur #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Image | |
attr_accessor :image | |
def initialize(image) | |
self.image = image | |
end | |
def output_image | |
self.image.each do |row| | |
puts row.join(", ") | |
end | |
end | |
def blur | |
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 | |
self.image = buffer | |
output_image | |
end | |
end | |
image = Image.new([ | |
[0, 0, 0, 0], | |
[0, 0, 0, 0], | |
[0, 1, 0, 0], | |
[0, 0, 0, 0], | |
[0, 0, 0, 0], | |
[0, 0, 0, 1] | |
]) | |
image.blur |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment