Add offBounds to blur so that we can shadow from the edge.

This commit is contained in:
treeform 2021-02-15 18:24:53 -08:00
parent 219dc23bf9
commit 955d813a27

View file

@ -369,7 +369,7 @@ proc invert*(target: Image | Mask) =
for j in i ..< target.data.len: for j in i ..< target.data.len:
target.data[j] = (255 - target.data[j]).uint8 target.data[j] = (255 - target.data[j]).uint8
proc blur*(target: Image | Mask, radius: float32) = proc blur*(target: Image | Mask, radius: float32, offBounds: uint32 = 0) =
## Applies Gaussian blur to the image given a radius. ## Applies Gaussian blur to the image given a radius.
let radius = round(radius).int let radius = round(radius).int
if radius == 0: if radius == 0:
@ -399,6 +399,8 @@ proc blur*(target: Image | Mask, radius: float32) =
let lookup = gaussianLookup(radius) let lookup = gaussianLookup(radius)
when type(target) is Image: when type(target) is Image:
# TODO support offBounds for images.
doAssert offBounds == 0
template `*`(sample: ColorRGBA, a: uint32): array[4, uint32] = template `*`(sample: ColorRGBA, a: uint32): array[4, uint32] =
[ [
@ -453,9 +455,12 @@ proc blur*(target: Image | Mask, radius: float32) =
for x in 0 ..< target.width: for x in 0 ..< target.width:
var value: uint32 var value: uint32
for xb in -radius .. radius: for xb in -radius .. radius:
let var sample: uint32
sample = target[x + xb, y] if target.inside(x + xb, y):
a = lookup[xb + radius].uint32 sample = target.getValueUnsafe(x + xb, y)
else:
sample = offBounds
let a = lookup[xb + radius].uint32
value += sample * a value += sample * a
blurX.setValueUnsafe(x, y, (value div 1024 div 255).uint8) blurX.setValueUnsafe(x, y, (value div 1024 div 255).uint8)
@ -464,9 +469,12 @@ proc blur*(target: Image | Mask, radius: float32) =
for x in 0 ..< target.width: for x in 0 ..< target.width:
var value: uint32 var value: uint32
for yb in -radius .. radius: for yb in -radius .. radius:
let var sample: uint32
sample = blurX[x, y + yb] if blurX.inside(x, y + yb):
a = lookup[yb + radius].uint32 sample = blurX.getValueUnsafe(x, y + yb)
else:
sample = offBounds
let a = lookup[yb + radius].uint32
value += sample * a value += sample * a
target.setValueUnsafe(x, y, (value div 1024 div 255).uint8) target.setValueUnsafe(x, y, (value div 1024 div 255).uint8)