starting transition of draw / images.nim to premul alpha
This commit is contained in:
parent
c5c89a766b
commit
5e536c5d67
3 changed files with 18 additions and 13 deletions
|
@ -263,6 +263,15 @@ proc toStraightAlpha*(image: Image) =
|
||||||
c.g = ((c.g.uint32 * multiplier) div 255).uint8
|
c.g = ((c.g.uint32 * multiplier) div 255).uint8
|
||||||
c.b = ((c.b.uint32 * multiplier) div 255).uint8
|
c.b = ((c.b.uint32 * multiplier) div 255).uint8
|
||||||
|
|
||||||
|
proc applyOpacity*(image: Image, opacity: float32) =
|
||||||
|
## Multiplies alpha of the image by opacity.
|
||||||
|
let opacity = round(255 * opacity).uint32
|
||||||
|
for rgba in image.data.mitems:
|
||||||
|
rgba.r = ((rgba.r * opacity) div 255).uint8
|
||||||
|
rgba.g = ((rgba.g * opacity) div 255).uint8
|
||||||
|
rgba.b = ((rgba.b * opacity) div 255).uint8
|
||||||
|
rgba.a = ((rgba.a * opacity) div 255).uint8
|
||||||
|
|
||||||
proc getRgbaSmooth*(image: Image, x, y: float32): ColorRGBA =
|
proc getRgbaSmooth*(image: Image, x, y: float32): ColorRGBA =
|
||||||
let
|
let
|
||||||
minX = floor(x)
|
minX = floor(x)
|
||||||
|
@ -272,16 +281,15 @@ proc getRgbaSmooth*(image: Image, x, y: float32): ColorRGBA =
|
||||||
x = minX.int
|
x = minX.int
|
||||||
y = minY.int
|
y = minY.int
|
||||||
|
|
||||||
x0y0 = image[x + 0, y + 0].toPremultipliedAlpha()
|
x0y0 = image[x + 0, y + 0]
|
||||||
x1y0 = image[x + 1, y + 0].toPremultipliedAlpha()
|
x1y0 = image[x + 1, y + 0]
|
||||||
x0y1 = image[x + 0, y + 1].toPremultipliedAlpha()
|
x0y1 = image[x + 0, y + 1]
|
||||||
x1y1 = image[x + 1, y + 1].toPremultipliedAlpha()
|
x1y1 = image[x + 1, y + 1]
|
||||||
|
|
||||||
bottomMix = lerp(x0y0, x1y0, diffX)
|
bottomMix = lerp(x0y0, x1y0, diffX)
|
||||||
topMix = lerp(x0y1, x1y1, diffX)
|
topMix = lerp(x0y1, x1y1, diffX)
|
||||||
finalMix = lerp(bottomMix, topMix, diffY)
|
|
||||||
|
|
||||||
finalMix.toStraightAlpha()
|
lerp(bottomMix, topMix, diffY)
|
||||||
|
|
||||||
proc drawCorrect*(a: Image, b: Image | Mask, mat = mat3(), blendMode = bmMask) =
|
proc drawCorrect*(a: Image, b: Image | Mask, mat = mat3(), blendMode = bmMask) =
|
||||||
## Draws one image onto another using matrix with color blending.
|
## Draws one image onto another using matrix with color blending.
|
||||||
|
@ -447,12 +455,6 @@ proc blurAlpha*(image: Image, radius: float32) =
|
||||||
alpha += c2.a.float32 * a
|
alpha += c2.a.float32 * a
|
||||||
image.setRgbaUnsafe(x, y, rgba(0, 0, 0, alpha.uint8))
|
image.setRgbaUnsafe(x, y, rgba(0, 0, 0, alpha.uint8))
|
||||||
|
|
||||||
proc applyOpacity*(image: Image, opacity: float32) =
|
|
||||||
## Multiplies alpha of the image by opacity.
|
|
||||||
let op = (255 * opacity).uint32
|
|
||||||
for rgba in image.data.mitems:
|
|
||||||
rgba.a = ((rgba.a.uint32 * op) div 255).clamp(0, 255).uint8
|
|
||||||
|
|
||||||
proc sharpOpacity*(image: Image) =
|
proc sharpOpacity*(image: Image) =
|
||||||
## Sharpens the opacity to extreme.
|
## Sharpens the opacity to extreme.
|
||||||
## A = 0 stays 0. Anything else turns into 255.
|
## A = 0 stays 0. Anything else turns into 255.
|
||||||
|
|
|
@ -68,7 +68,7 @@ block:
|
||||||
mask.fillPath(path)
|
mask.fillPath(path)
|
||||||
|
|
||||||
timeIt "mask":
|
timeIt "mask":
|
||||||
image.mask(mask)
|
image.draw(mask)
|
||||||
|
|
||||||
reset()
|
reset()
|
||||||
|
|
||||||
|
|
|
@ -12,3 +12,6 @@ timeIt "minifyBy2":
|
||||||
doAssert minified[0, 0] == 63
|
doAssert minified[0, 0] == 63
|
||||||
|
|
||||||
reset()
|
reset()
|
||||||
|
|
||||||
|
timeIt "applyOpacity":
|
||||||
|
mask.applyOpacity(0.5)
|
||||||
|
|
Loading…
Reference in a new issue