From a8518c8218c57ef33d15bc8dfc176560a0a1f7f8 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Tue, 23 Nov 2021 03:15:58 -0600 Subject: [PATCH] save work if no shadow offset --- src/pixie/images.nim | 12 +++++++++--- tests/benchmark_images_draw.nim | 19 ++++++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/pixie/images.nim b/src/pixie/images.nim index 1b2919f..60977a2 100644 --- a/src/pixie/images.nim +++ b/src/pixie/images.nim @@ -976,12 +976,18 @@ proc shadow*( image: Image, offset: Vec2, spread, blur: float32, color: SomeColor ): Image {.raises: [PixieError].} = ## Create a shadow of the image with the offset, spread and blur. - let - mask = image.newMask() + let mask = image.newMask() + + var shifted: Mask + if offset == vec2(0, 0): + shifted = mask + else: shifted = newMask(mask.width, mask.height) - shifted.draw(mask, translate(offset), bmOverwrite) + shifted.draw(mask, translate(offset), bmOverwrite) + shifted.spread(spread) shifted.blur(blur) + result = newImage(shifted.width, shifted.height) result.fill(color) result.draw(shifted, blendMode = bmMask) diff --git a/tests/benchmark_images_draw.nim b/tests/benchmark_images_draw.nim index 34930be..3083c05 100644 --- a/tests/benchmark_images_draw.nim +++ b/tests/benchmark_images_draw.nim @@ -115,7 +115,7 @@ block: a = newImage(100, 100) b = newImage(50, 50) - timeIt "shadow": + timeIt "shadow (no offset)": b.fill(rgba(0, 0, 0, 255)) a.draw(b, translate(vec2(25, 25))) @@ -126,3 +126,20 @@ block: color = rgba(0, 0, 0, 255) ) keep(shadow) + +block: + let + a = newImage(100, 100) + b = newImage(50, 50) + + timeIt "shadow (with offset)": + b.fill(rgba(0, 0, 0, 255)) + a.draw(b, translate(vec2(25, 25))) + + let shadow = a.shadow( + offset = vec2(10, 10), + spread = 10, + blur = 10, + color = rgba(0, 0, 0, 255) + ) + keep(shadow)