1
0
Fork 0

Refactor to avoid stack overflow

This commit is contained in:
TEC 2022-08-10 22:59:19 +08:00
parent 58fda5eab8
commit b8eea0e7b4
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
1 changed files with 12 additions and 13 deletions

View File

@ -95,21 +95,20 @@ end
function growcolour!(cmat::Matrix{Union{Colorant, Missing}}, metric::Colors.DifferenceMetric,
refmat::Matrix{<:Colorant}, cnearest::Colorant, safethreshold::Float64,
pos::CartesianIndex{2})
# @info "Growing $(pos.I)"
surrounding = Ref(pos) .+ CartesianIndex{2}.([(0, 1), (0, -1), (1, 0), (-1, 0)])
filter!(s -> all((1,1) .<= s.I .<= size(refmat)), surrounding)
reseed = Vector{CartesianIndex{2}}()
for spos in surrounding
# @info " @ $(spos.I)"
if ismissing(cmat[spos]) && colordiff(cnearest, refmat[spos]; metric) < safethreshold
cmat[spos] = cnearest
push!(reseed, spos)
initalpos::CartesianIndex{2})
seeds = [initalpos]
while !isempty(seeds)
pos = pop!(seeds)
surrounding = Ref(pos) .+ CartesianIndex{2}.([(0, 1), (0, -1), (1, 0), (-1, 0)])
filter!(s -> all((1,1) .<= s.I .<= size(refmat)), surrounding)
for spos in surrounding
if ismissing(cmat[spos]) &&
colordiff(cnearest, refmat[spos]; metric) < safethreshold
cmat[spos] = cnearest
push!(seeds, spos)
end
end
end
for spos in reseed
growcolour!(cmat, metric, refmat, cnearest, safethreshold, spos)
end
end
function colour_grid_hsl(xs, ys, saturation=1)