Skip to content

Instantly share code, notes, and snippets.

@maleadt
Created February 21, 2014 15:37
Show Gist options
  • Save maleadt/9136414 to your computer and use it in GitHub Desktop.
Save maleadt/9136414 to your computer and use it in GitHub Desktop.
using Images
data = rand(10, 10)
image = Image(data)
immutable Point{T}
i::T
j::T
end
function foobar_image(input::Image{Float64}, i::Int, j::Int)
@inbounds return input.data[i, j]
end
function interpolate_image(input::Image{Float64}, p::Point{Float64})
# Get fractional and integral part of the coordinates
p_int::Point{Int} = Point(ifloor(p.i), ifloor(p.j))
p_fract::Point{Float64} = Point(p.i-p_int.i, p.j-p_int.j)
# Bilinear interpolation
@inbounds return (input[p_int.i, p_int.j] * (1-p_fract.j) * (1-p_fract.i) +
input[p_int.i, p_int.j+1] * p_fract.j * (1-p_fract.i) +
input[p_int.i+1, p_int.j] * (1-p_fract.j) * p_fract.i +
input[p_int.i+1, p_int.j+1] * p_fract.j * p_fract.i)
end
function foobar_array(input::Array{Float64, 2}, i::Int, j::Int)
@inbounds return input[i, j]
end
function interpolate_array(input::Array{Float64, 2}, p::Point{Float64})
# Get fractional and integral part of the coordinates
p_int::Point{Int} = Point(ifloor(p.i), ifloor(p.j))
p_fract::Point{Float64} = Point(p.i-p_int.i, p.j-p_int.j)
# Bilinear interpolation
@inbounds return (input[p_int.i, p_int.j] * (1-p_fract.j) * (1-p_fract.i) +
input[p_int.i, p_int.j+1] * p_fract.j * (1-p_fract.i) +
input[p_int.i+1, p_int.j] * (1-p_fract.j) * p_fract.i +
input[p_int.i+1, p_int.j+1] * p_fract.j * p_fract.i)
end
code_native(interpolate_image, (Image{Float64}, Point{Float64}))
interpolate_image(image, Point(0.0, 0.0))
gc_disable()
@time [interpolate_image(image, Point(0.0, 0.0)) for i in 1:100000];
gc_enable()
println()
println()
println()
code_native(interpolate_array, (Array{Float64, 2}, Point{Float64}))
interpolate_array(image.data, Point(0.0, 0.0))
gc_disable()
@time [interpolate_array(image.data, Point(0.0, 0.0)) for i in 1:100000];
gc_enable()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment