diff --git a/src/pixie/images.nim b/src/pixie/images.nim index 1ec038c..0c4da5b 100644 --- a/src/pixie/images.nim +++ b/src/pixie/images.nim @@ -263,6 +263,15 @@ proc toStraightAlpha*(image: Image) = c.g = ((c.g.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 = let minX = floor(x) @@ -272,16 +281,15 @@ proc getRgbaSmooth*(image: Image, x, y: float32): ColorRGBA = x = minX.int y = minY.int - x0y0 = image[x + 0, y + 0].toPremultipliedAlpha() - x1y0 = image[x + 1, y + 0].toPremultipliedAlpha() - x0y1 = image[x + 0, y + 1].toPremultipliedAlpha() - x1y1 = image[x + 1, y + 1].toPremultipliedAlpha() + x0y0 = image[x + 0, y + 0] + x1y0 = image[x + 1, y + 0] + x0y1 = image[x + 0, y + 1] + x1y1 = image[x + 1, y + 1] bottomMix = lerp(x0y0, x1y0, 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) = ## 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 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) = ## Sharpens the opacity to extreme. ## A = 0 stays 0. Anything else turns into 255. diff --git a/tests/benchmark_images.nim b/tests/benchmark_images.nim index 0d6fc2b..df46382 100644 --- a/tests/benchmark_images.nim +++ b/tests/benchmark_images.nim @@ -68,7 +68,7 @@ block: mask.fillPath(path) timeIt "mask": - image.mask(mask) + image.draw(mask) reset() diff --git a/tests/benchmark_masks.nim b/tests/benchmark_masks.nim index a359c4a..5289fdf 100644 --- a/tests/benchmark_masks.nim +++ b/tests/benchmark_masks.nim @@ -12,3 +12,6 @@ timeIt "minifyBy2": doAssert minified[0, 0] == 63 reset() + +timeIt "applyOpacity": + mask.applyOpacity(0.5)