Created
February 26, 2016 03:33
-
-
Save framallo/49abaa08749f94fa386d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
def initialize(image) | |
@image = image | |
end | |
#Iterate through @image input and identify which indices have a "1". | |
def identify | |
one_index = [] | |
@image.each_with_index do |row, row_index| | |
row.each_with_index do |pixel, col_index| | |
if pixel == 1 | |
one_index.push([row_index, col_index]) | |
end | |
end | |
end | |
return one_index | |
end | |
def transform(one_index) | |
# Iterate through array of "1" pixel locations and pass those values to the update_cell method. | |
one_index.each do |row_index, col_index| | |
blur_cell row_index, col_index | |
end | |
return Image.new(@image) | |
end | |
# The blur method take the row_index and col_index as arguments and passes them to the | |
# update_cell method along with a vaule to set the identified pixels to, causing the blur. | |
def blur(n) | |
one_index.each { |x,y| blur_cell(x,y,n) } | |
end | |
def blur_cell(x,y,n = 1) | |
return if n == 0 | |
select_square(x, y, n).each do |delta_x, delta_y| | |
update_cell x + delta_x, y + delta_y, 1 | |
end | |
blur_cell x, y, n - 1 | |
end | |
def select_square(x, y, n) | |
range = (-n..n).to_a | |
range.product(range).select { |a, b| a.abs + b.abs == n } | |
end | |
def blur_cell2(x,y,n = 1) | |
return if n == 0 | |
update_cell(x+n, y, 1) | |
update_cell(x, y+n, 1) | |
update_cell(x, y-n, 1) | |
update_cell(x-n,y,1) | |
if n == 2 | |
update_cell(x-1, y - 1, 1) | |
update_cell(x-1, y + 1, 1) | |
update_cell(x+1, y - 1, 1) | |
update_cell(x+1, y + 1, 1) | |
blur_cell(x, y, n - 1) | |
end | |
end | |
# update_cell sets the values passed into it to "1", but only after checking to see if the # selected indices are within the bounds of the array. | |
def update_cell(row_index, col_index, value) | |
return if !within?(@image, row_index) || !within?(@image[row_index],col_index) | |
@image[row_index][col_index] = value | |
end | |
# The within? method is used in conjunction with the update_cell method in order to make | |
# sure that the indices being updated are within the bounds of the image input. | |
def within?(array,index) | |
array.size > index && index >= 0 | |
end | |
def output_image | |
@image.each { |row| puts row.join } | |
end | |
end | |
#First "1" in @image [1][1] | |
# Indicies that need to be changed for blur to be complete | |
#@image[0][1] | |
#@image[1][0] | |
#@image[1][2] | |
#@image[2][1] | |
# image = Image.new([ | |
# [0, 0, 0, 0], | |
# [0, 1, 0, 0], | |
# [0, 0, 0, 1], | |
# [0, 0, 0, 0] | |
# ]) | |
# #image.output_image | |
# | |
# afterImage = image.identify | |
# afterImage.blur(2) | |
# afterImage = Image.new([ | |
# [0, 1, 0, 0], | |
# [1, 1, 1, 1], | |
# [0, 1, 1, 1], | |
# [0, 0, 0, 1] | |
# ]) |
This file contains hidden or 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
require 'rspec' | |
require 'pp' | |
require_relative 'blur3' | |
RSpec.describe Image do | |
describe '#blur_cell' do | |
it 'blurs a single cell with manhattan distance of 1' do | |
image = Image.new([ | |
[0, 0, 0, 0], | |
[0, 1, 0, 0], | |
[0, 0, 0, 0], | |
[0, 0, 0, 0] | |
]) | |
image.blur_cell(1,1) | |
puts | |
image.output_image | |
puts | |
expect { image.output_image }.to output( | |
"0100\n1110\n0100\n0000\n").to_stdout | |
end | |
it 'blurs a single cell with manhattan distance of 2' do | |
image = Image.new([ | |
[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] | |
]) | |
# image2 = Image.new([ | |
# [0, 0, 0, 0, 0], | |
# [0, 1, 1, 1, 0], | |
# [1, 1, 1, 1, 1], | |
# [0, 1, 1, 1, 0], | |
# [0, 0, 1, 0, 0] | |
# ]) | |
image.blur_cell(2,2,2) | |
puts | |
image.output_image | |
puts | |
expect { image.output_image }.to output( | |
"00100\n01110\n11111\n01110\n00100\n").to_stdout | |
end | |
it 'blurs a single cell with manhattan distance of 3' do | |
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, 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] | |
]) | |
# image2 = Image.new([ | |
# [0, 0, 0, 1, 0, 0, 0], | |
# [0, 0, 1, 1, 1, 0, 0], | |
# [0, 1, 1, 1, 1, 1, 0], | |
# [1, 1, 1, *, 1, 1, 1], | |
# [0, 1, 1, 1, 1, 1, 0], | |
# [0, 0, 1, 1, 1, 0, 0], | |
# [0, 0, 0, 1, 0, 0, 0] | |
# ]) | |
image.blur_cell(3,3,3) | |
puts | |
image.output_image | |
puts | |
expect { image.output_image }.to output( | |
"0001000\n0011100\n0111110\n1111111\n0111110\n0011100\n0001000\n").to_stdout | |
end | |
end | |
describe '#update_cell' do | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment