Negative spread without inverts.

This commit is contained in:
treeform 2022-03-20 08:54:06 -07:00
parent 6068a92363
commit 87397c8bf8

View file

@ -255,11 +255,8 @@ proc spread*(mask: Mask, spread: float32) {.raises: [PixieError].} =
let spread = round(spread).int let spread = round(spread).int
if spread == 0: if spread == 0:
return return
elif spread < 0: elif spread > 0:
mask.invert()
spread(mask, -spread.float32)
mask.invert()
return
# Spread in the X direction. Store with dimensions swapped for reading later. # Spread in the X direction. Store with dimensions swapped for reading later.
let spreadX = newMask(mask.height, mask.width) let spreadX = newMask(mask.height, mask.width)
for y in 0 ..< mask.height: for y in 0 ..< mask.height:
@ -285,6 +282,34 @@ proc spread*(mask: Mask, spread: float32) {.raises: [PixieError].} =
break break
mask.unsafe[x, y] = maxValue mask.unsafe[x, y] = maxValue
elif spread < 0:
# Spread in the X direction. Store with dimensions swapped for reading later.
let spread = -spread
let spreadX = newMask(mask.height, mask.width)
for y in 0 ..< mask.height:
for x in 0 ..< mask.width:
var maxValue: uint8 = 255
for xx in max(x - spread, 0) .. min(x + spread, mask.width - 1):
let value = mask.unsafe[xx, y]
if value < maxValue:
maxValue = value
if maxValue == 0:
break
spreadX.unsafe[y, x] = maxValue
# Spread in the Y direction and modify mask.
for y in 0 ..< mask.height:
for x in 0 ..< mask.width:
var maxValue: uint8 = 255
for yy in max(y - spread, 0) .. min(y + spread, mask.height - 1):
let value = spreadX.unsafe[yy, x]
if value < maxValue:
maxValue = value
if maxValue == 0:
break
mask.unsafe[x, y] = maxValue
proc ceil*(mask: Mask) {.raises: [].} = proc ceil*(mask: Mask) {.raises: [].} =
## A value of 0 stays 0. Anything else turns into 255. ## A value of 0 stays 0. Anything else turns into 255.
var i: int var i: int