From b8eea0e7b4908a6f8df5500d3914821bb8247e61 Mon Sep 17 00:00:00 2001 From: TEC Date: Wed, 10 Aug 2022 22:59:19 +0800 Subject: [PATCH] Refactor to avoid stack overflow --- pairsurrounding.jl | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/pairsurrounding.jl b/pairsurrounding.jl index f2aa425..f900846 100644 --- a/pairsurrounding.jl +++ b/pairsurrounding.jl @@ -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)