spread >10x faster using same strategy as blur

This commit is contained in:
Ryan Oldenburg 2021-06-19 15:26:41 -05:00
parent a33fcd9132
commit 2deaa3988a

View file

@ -178,25 +178,35 @@ proc getValueSmooth*(mask: Mask, x, y: float32): uint8 =
proc spread*(mask: Mask, spread: float32) = proc spread*(mask: Mask, spread: float32) =
## Grows the mask by spread. ## Grows the mask by spread.
let spread = round(spread).int
if spread == 0: if spread == 0:
return return
if spread < 0: if spread < 0:
raise newException(PixieError, "Cannot apply negative spread") raise newException(PixieError, "Cannot apply negative spread")
let # Spread in the X direction. Store with dimensions swapped for reading later.
copy = mask.copy() let spreadX = newMask(mask.height, mask.width)
spread = round(spread).int
for y in 0 ..< mask.height: for y in 0 ..< mask.height:
for x in 0 ..< mask.width: for x in 0 ..< mask.width:
var maxValue: uint8 var maxValue: uint8
block blurBox: for xx in max(x - spread, 0) .. min(x + spread, mask.width - 1):
for by in max(y - spread, 0) .. min(y + spread, mask.height - 1): let value = mask.getValueUnsafe(xx, y)
for bx in max(x - spread, 0) .. min(x + spread, mask.width - 1): if value > maxValue:
let value = copy.getValueUnsafe(bx, by) maxValue = value
if value > maxValue: if maxValue == 255:
maxValue = value break
if maxValue == 255: spreadX.setValueUnsafe(y, x, maxValue)
break blurBox
# Spread in the Y direction and modify mask.
for y in 0 ..< mask.height:
for x in 0 ..< mask.width:
var maxValue: uint8
for yy in max(y - spread, 0) .. min(y + spread, mask.height - 1):
let value = spreadX.getValueUnsafe(yy, x)
if value > maxValue:
maxValue = value
if maxValue == 255:
break
mask.setValueUnsafe(x, y, maxValue) mask.setValueUnsafe(x, y, maxValue)
proc ceil*(mask: Mask) = proc ceil*(mask: Mask) =