Skip to content

Instantly share code, notes, and snippets.

@pdbradley
Created September 22, 2016 15:59
Show Gist options
  • Save pdbradley/9cb778d2229ed66724d4dfa0d1c6cd81 to your computer and use it in GitHub Desktop.
Save pdbradley/9cb778d2229ed66724d4dfa0d1c6cd81 to your computer and use it in GitHub Desktop.
refactored image blur
class Image
def initialize(image)
@image = image
end
#output_image to output the image
def output_image
@image.each do |line|
line.each do |cell|
end
puts line.join
end
end
def copy_image
copy = []
@image.each_index do |x|
copy << []
@image[x].each_index do |y|
copy[x] << @image[x][y]
end
end
copy
end
def blur_above(x, y)
if upper_blur_inside_image?(x,y)
@copy[x][y+1] = 1
end
end
def blur_below(x,y)
if lower_blur_inside_image?(x,y)
@copy[x][y-1] = 1
end
end
def upper_blur_inside_image?(x,y)
(y+1) < @image[x].length
end
def lower_blur_inside_image?(x,y)
(y-1) >= 0
end
def left_blur_inside_image?(x)
(x-1) >= 0
end
def right_blur_inside_image?(x)
(x+1) < @image.length
end
def blur_left(x,y)
if left_blur_inside_image?(x)
@copy[x-1][y] = 1
end
end
def blur_right(x,y)
if right_blur_inside_image?(x)
@copy[x+1][y] = 1
end
end
#the blur method
def blur
@copy = copy_image
@image.each_index do |x|
@image[x].each_index do |y|
#if pixel == 1
if @image[x][y] == 1
blur_above(x,y)
blur_below(x,y)
blur_left(x,y)
blur_right(x,y)
end
end
end
#array switches to copy
@image = @copy
end
end
image = Image.new([
[0, 0, 0, 1],
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 0]
])
#Output the image
image.blur
image.output_image
#had a problem with 1's outside the "pixel box", had to end up having to use conditionals
#finally finished imageblur2, time to go get some food!
#this took a long time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment