Merge pull request #20 from guzba/master

inplace blur
This commit is contained in:
treeform 2020-12-03 22:27:54 -08:00 committed by GitHub
commit 73e0ff25e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 10 deletions

View file

@ -207,15 +207,16 @@ proc resize*(srcImage: Image, width, height: int): Image =
)) ))
) )
proc blur*(image: Image, radius: float32): Image = proc blur*(image: Image, radius: float32) =
## 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:
return image.copy() return
# Compute lookup table for 1d Gaussian kernel. # Compute lookup table for 1d Gaussian kernel.
var lookup = newSeq[float](radius * 2 + 1) var
var total = 0.0 lookup = newSeq[float](radius * 2 + 1)
total = 0.0
for xb in -radius .. radius: for xb in -radius .. radius:
let s = radius.float32 / 2.2 # 2.2 matches Figma. let s = radius.float32 / 2.2 # 2.2 matches Figma.
let x = xb.float32 let x = xb.float32
@ -246,7 +247,6 @@ proc blur*(image: Image, radius: float32): Image =
blurX.setRgbaUnsafe(x, y, c.rgba) blurX.setRgbaUnsafe(x, y, c.rgba)
# Blur in the Y direction. # Blur in the Y direction.
var blurY = newImage(image.width, image.height)
for y in 0 ..< image.height: for y in 0 ..< image.height:
for x in 0 ..< image.width: for x in 0 ..< image.width:
var c: Color var c: Color
@ -263,9 +263,7 @@ proc blur*(image: Image, radius: float32): Image =
c.r = c.r / totalA c.r = c.r / totalA
c.g = c.g / totalA c.g = c.g / totalA
c.b = c.b / totalA c.b = c.b / totalA
blurY.setRgbaUnsafe(x, y, c.rgba) image.setRgbaUnsafe(x, y, c.rgba)
return blurY
proc blurAlpha*(image: Image, radius: float32) = proc blurAlpha*(image: Image, radius: float32) =
## Applies Gaussian blur to the image given a radius. ## Applies Gaussian blur to the image given a radius.

View file

@ -1,3 +1,4 @@
import system/memory
type type
Mask* = ref object Mask* = ref object
@ -51,8 +52,7 @@ proc `[]=`*(mask: Mask, x, y: int, value: uint8) {.inline.} =
proc fill*(mask: Mask, value: uint8) = proc fill*(mask: Mask, value: uint8) =
## Fills the mask with the parameter value. ## Fills the mask with the parameter value.
for i in 0 ..< mask.data.len: nimSetMem(mask.data[0].addr, value.cint, mask.data.len)
mask.data[i] = value
proc invert*(mask: Mask) = proc invert*(mask: Mask) =
## Inverts the entire mask value. ## Inverts the entire mask value.