From d28fe1da4f0dd5731049e9f777fc0f8ce61f9f8b Mon Sep 17 00:00:00 2001 From: Luke Maycock Date: Mon, 16 Aug 2021 14:29:24 +0100 Subject: [PATCH 1/2] Adding a way to draw outer- and inner-shadows of paths using the StackBlur algorithm --- .../effects/juce_StackShadowEffect.cpp | 329 ++++++++++++++++++ .../effects/juce_StackShadowEffect.h | 58 +++ modules/juce_graphics/juce_graphics.cpp | 1 + modules/juce_graphics/juce_graphics.h | 1 + 4 files changed, 389 insertions(+) create mode 100644 modules/juce_graphics/effects/juce_StackShadowEffect.cpp create mode 100644 modules/juce_graphics/effects/juce_StackShadowEffect.h diff --git a/modules/juce_graphics/effects/juce_StackShadowEffect.cpp b/modules/juce_graphics/effects/juce_StackShadowEffect.cpp new file mode 100644 index 0000000000..bc5322c35e --- /dev/null +++ b/modules/juce_graphics/effects/juce_StackShadowEffect.cpp @@ -0,0 +1,329 @@ +/* + ============================================================================== + + This file is part of the JUCE library. + Copyright (c) 2020 - Raw Material Software Limited + + JUCE is an open source library subject to commercial or open-source + licensing. + + By using JUCE, you agree to the terms of both the JUCE 6 End-User License + Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020). + + End User License Agreement: www.juce.com/juce-6-licence + Privacy Policy: www.juce.com/juce-privacy-policy + + Or: You may also use this code under the terms of the GPL v3 (see + www.gnu.org/licenses). + + JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER + EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE + DISCLAIMED. + + ============================================================================== +*/ + +namespace juce +{ + +//================================================================================= +// Code below has been borrowed from the Gin (https://github.com/FigBug/Gin) project. +// Copyright(c) 2018, Roland Rabien + +static unsigned short const stackblur_mul[255] = +{ + 512,512,456,512,328,456,335,512,405,328,271,456,388,335,292,512, + 454,405,364,328,298,271,496,456,420,388,360,335,312,292,273,512, + 482,454,428,405,383,364,345,328,312,298,284,271,259,496,475,456, + 437,420,404,388,374,360,347,335,323,312,302,292,282,273,265,512, + 497,482,468,454,441,428,417,405,394,383,373,364,354,345,337,328, + 320,312,305,298,291,284,278,271,265,259,507,496,485,475,465,456, + 446,437,428,420,412,404,396,388,381,374,367,360,354,347,341,335, + 329,323,318,312,307,302,297,292,287,282,278,273,269,265,261,512, + 505,497,489,482,475,468,461,454,447,441,435,428,422,417,411,405, + 399,394,389,383,378,373,368,364,359,354,350,345,341,337,332,328, + 324,320,316,312,309,305,301,298,294,291,287,284,281,278,274,271, + 268,265,262,259,257,507,501,496,491,485,480,475,470,465,460,456, + 451,446,442,437,433,428,424,420,416,412,408,404,400,396,392,388, + 385,381,377,374,370,367,363,360,357,354,350,347,344,341,338,335, + 332,329,326,323,320,318,315,312,310,307,304,302,299,297,294,292, + 289,287,285,282,280,278,275,273,271,269,267,265,263,261,259 +}; + +static unsigned char const stackblur_shr[255] = +{ + 9, 11, 12, 13, 13, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, + 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, + 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, + 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, + 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, + 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, + 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, + 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, + 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, + 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, + 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, + 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, + 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, + 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, + 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, + 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24 +}; + +// The Stack Blur Algorithm was invented by Mario Klingemann, +// mario@quasimondo.com and described here: +// http://incubator.quasimondo.com/processing/fast_blur_deluxe.php + +// Stackblur algorithm by Mario Klingemann +// Details here: +// http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html +// C++ implemenation base from: +// https://gist.github.com/benjamin9999/3809142 +// http://www.antigrain.com/__code/include/agg_blur.h.html +static void applyStackBlur (Image& img, unsigned int radius) +{ + const unsigned int w = (unsigned int)img.getWidth(); + const unsigned int h = (unsigned int)img.getHeight(); + + Image::BitmapData data (img, Image::BitmapData::readWrite); + + radius = jlimit (2u, 254u, radius); + + unsigned char stack[(254 * 2 + 1) * 1]; + + unsigned int x, y, xp, yp, i, sp, stack_start; + + unsigned char* stack_ptr = nullptr; + unsigned char* src_ptr = nullptr; + unsigned char* dst_ptr = nullptr; + + unsigned long sum, sum_in, sum_out; + + unsigned int wm = w - 1; + unsigned int hm = h - 1; + unsigned int w1 = (unsigned int) data.lineStride; + unsigned int div = (unsigned int) (radius * 2) + 1; + unsigned int mul_sum = stackblur_mul[radius]; + unsigned char shr_sum = stackblur_shr[radius]; + + for (y = 0; y < h; ++y) + { + sum = sum_in = sum_out = 0; + + src_ptr = data.getLinePointer (int (y)); + + for (i = 0; i <= radius; ++i) + { + stack_ptr = &stack[i]; + stack_ptr[0] = src_ptr[0]; + sum += src_ptr[0] * (i + 1); + sum_out += src_ptr[0]; + } + + for (i = 1; i <= radius; ++i) + { + if (i <= wm) + src_ptr += 1; + + stack_ptr = &stack[1 * (i + radius)]; + stack_ptr[0] = src_ptr[0]; + sum += src_ptr[0] * (radius + 1 - i); + sum_in += src_ptr[0]; + } + + sp = radius; + xp = radius; + if (xp > wm) + xp = wm; + + src_ptr = data.getLinePointer (int (y)) + (unsigned int)data.pixelStride * xp; + dst_ptr = data.getLinePointer (int (y)); + + for (x = 0; x < w; ++x) + { + dst_ptr[0] = (unsigned char)((sum * mul_sum) >> shr_sum); + dst_ptr += 1; + + sum -= sum_out; + + stack_start = sp + div - radius; + + if (stack_start >= div) + stack_start -= div; + + stack_ptr = &stack[1 * stack_start]; + + sum_out -= stack_ptr[0]; + + if (xp < wm) + { + src_ptr += 1; + ++xp; + } + + stack_ptr[0] = src_ptr[0]; + + sum_in += src_ptr[0]; + sum += sum_in; + + ++sp; + if (sp >= div) + sp = 0; + + stack_ptr = &stack[sp*1]; + + sum_out += stack_ptr[0]; + sum_in -= stack_ptr[0]; + } + } + + for (x = 0; x < w; ++x) + { + sum = sum_in = sum_out = 0; + + src_ptr = data.getLinePointer (0) + (unsigned int)data.pixelStride * x; + + for (i = 0; i <= radius; ++i) + { + stack_ptr = &stack[i * 1]; + stack_ptr[0] = src_ptr[0]; + sum += src_ptr[0] * (i + 1); + sum_out += src_ptr[0]; + } + + for (i = 1; i <= radius; ++i) + { + if (i <= hm) + src_ptr += w1; + + stack_ptr = &stack[1 * (i + radius)]; + stack_ptr[0] = src_ptr[0]; + sum += src_ptr[0] * (radius + 1 - i); + sum_in += src_ptr[0]; + } + + sp = radius; + yp = radius; + if (yp > hm) + yp = hm; + + src_ptr = data.getLinePointer (int (yp)) + (unsigned int)data.pixelStride * x; + dst_ptr = data.getLinePointer (0) + (unsigned int)data.pixelStride * x; + + for (y = 0; y < h; ++y) + { + dst_ptr[0] = (unsigned char)((sum * mul_sum) >> shr_sum); + dst_ptr += w1; + + sum -= sum_out; + + stack_start = sp + div - radius; + if (stack_start >= div) + stack_start -= div; + + stack_ptr = &stack[1 * stack_start]; + + sum_out -= stack_ptr[0]; + + if (yp < hm) + { + src_ptr += w1; + ++yp; + } + + stack_ptr[0] = src_ptr[0]; + + sum_in += src_ptr[0]; + sum += sum_in; + + ++sp; + if (sp >= div) + sp = 0; + + stack_ptr = &stack[sp * 1]; + + sum_out += stack_ptr[0]; + sum_in -= stack_ptr[0]; + } + } +} + +//================================================================================= + +StackShadow::StackShadow (Colour shadowColour, Point o, const int b, const int s) noexcept + : colour (shadowColour), offset (o), blur (b), spread (s) +{ + jassert (blur > 0); +} + +void StackShadow::drawOuterShadowForPath (Graphics& g, const Path& path) const +{ + jassert (blur > 0); + + const auto pathArea = path.getBounds().getSmallestIntegerContainer(); + auto area = (pathArea + offset) + .expanded (blur + 1) + .getIntersection (g.getClipBounds().expanded (blur + 1)); + + if (area.getWidth() > 2 && area.getHeight() > 2) + { + Image renderedPath (Image::SingleChannel, area.getWidth(), area.getHeight(), true); + + { + Graphics g2 (renderedPath); + g2.setColour (Colours::white); + auto t = AffineTransform::translation (static_cast (offset.x - area.getX()), + static_cast (offset.y - area.getY())); + auto s = AffineTransform::scale (1.f + (static_cast (spread) / static_cast (pathArea.getWidth())), + 1.f + (static_cast (spread) / static_cast (pathArea.getHeight())), + area.getCentreX(), + area.getCentreY()); + + g2.fillPath (path, t.followedBy (s)); + } + + applyStackBlur (renderedPath, blur); + + g.setColour (colour); + g.drawImageAt (renderedPath, area.getX(), area.getY(), true); + } +} + +void StackShadow::drawInnerShadowForPath (Graphics& g, const Path& path) const +{ + jassert (blur > 0); + jassert (spread >= 0); + + const auto pathArea = path.getBounds().getSmallestIntegerContainer(); + auto area = (pathArea + offset) + .expanded (blur + 1) + .getIntersection (g.getClipBounds().expanded (blur + 1)); + + if (area.getWidth() > 2 && area.getHeight() > 2) + { + Image renderedPath (Image::SingleChannel, area.getWidth(), area.getHeight(), true); + + { + Graphics g2 (renderedPath); + g2.setColour (Colours::white); + auto t = AffineTransform::translation (static_cast (offset.x - area.getX()), + static_cast (offset.y - area.getY())); + auto s = AffineTransform::scale (1.f - (static_cast (spread) / static_cast (pathArea.getWidth())), + 1.f - (static_cast (spread) / static_cast (pathArea.getHeight())), + area.getCentreX(), + area.getCentreY()); + + g2.strokePath (path, PathStrokeType (static_cast (2 * spread + 1)), t.followedBy(s)); + } + + applyStackBlur (renderedPath, blur); + + Graphics::ScopedSaveState save (g); + g.reduceClipRegion (path); + + g.setColour (colour); + g.drawImageAt (renderedPath, area.getX(), area.getY(), true); + } +} + +} // namespace juce diff --git a/modules/juce_graphics/effects/juce_StackShadowEffect.h b/modules/juce_graphics/effects/juce_StackShadowEffect.h new file mode 100644 index 0000000000..1d64849fee --- /dev/null +++ b/modules/juce_graphics/effects/juce_StackShadowEffect.h @@ -0,0 +1,58 @@ +/* + ============================================================================== + + This file is part of the JUCE library. + Copyright (c) 2020 - Raw Material Software Limited + + JUCE is an open source library subject to commercial or open-source + licensing. + + By using JUCE, you agree to the terms of both the JUCE 6 End-User License + Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020). + + End User License Agreement: www.juce.com/juce-6-licence + Privacy Policy: www.juce.com/juce-privacy-policy + + Or: You may also use this code under the terms of the GPL v3 (see + www.gnu.org/licenses). + + JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER + EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE + DISCLAIMED. + + ============================================================================== +*/ + +#pragma once + +namespace juce +{ + +struct JUCE_API StackShadow +{ + /** Creates a default stack-shadow effect. */ + StackShadow() = default; + + /** Creates a stack-shadow object with the given parameters. */ + StackShadow (Colour shadowColour, Point offset, int blur, int spread) noexcept; + + /** Renders a stack-shadow-based on the shape of a path. */ + void drawOuterShadowForPath (Graphics& g, const Path& path) const; + + /** Renders a stack-shadow-based inner-shadow on the shape of a path. */ + void drawInnerShadowForPath (Graphics& g, const Path& path) const; + + /** The colour with which to render the shadow. */ + Colour colour { 0x90000000 }; + + /** The offset of the shadow. */ + Point offset { 0, 0 }; + + /** The ammount of blur of the shadow. */ + int blur { 4 }; + + /** The spread of the shadow. */ + int spread { 0 }; +}; + +} // namespace juce diff --git a/modules/juce_graphics/juce_graphics.cpp b/modules/juce_graphics/juce_graphics.cpp index 4b2bd32170..53dd481a7a 100644 --- a/modules/juce_graphics/juce_graphics.cpp +++ b/modules/juce_graphics/juce_graphics.cpp @@ -126,6 +126,7 @@ #include "fonts/juce_GlyphArrangement.cpp" #include "fonts/juce_TextLayout.cpp" #include "effects/juce_DropShadowEffect.cpp" +#include "effects/juce_StackShadowEffect.cpp" #include "effects/juce_GlowEffect.cpp" #if JUCE_USE_FREETYPE diff --git a/modules/juce_graphics/juce_graphics.h b/modules/juce_graphics/juce_graphics.h index 9c2bdf44da..34a48f6b03 100644 --- a/modules/juce_graphics/juce_graphics.h +++ b/modules/juce_graphics/juce_graphics.h @@ -145,6 +145,7 @@ namespace juce #include "contexts/juce_LowLevelGraphicsPostScriptRenderer.h" #include "effects/juce_ImageEffectFilter.h" #include "effects/juce_DropShadowEffect.h" +#include "effects/juce_StackShadowEffect.h" #include "effects/juce_GlowEffect.h" #if JUCE_GRAPHICS_INCLUDE_COREGRAPHICS_HELPERS && (JUCE_MAC || JUCE_IOS) From 0945558925879c63a96a8ccec5d8674a022e434f Mon Sep 17 00:00:00 2001 From: LukeSonnox <66415193+LukeSonnox@users.noreply.github.com> Date: Fri, 21 Oct 2022 11:40:16 +0100 Subject: [PATCH 2/2] Update juce_StackShadowEffect.cpp Fixing order of transforms applied when drawing shadow --- modules/juce_graphics/effects/juce_StackShadowEffect.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/juce_graphics/effects/juce_StackShadowEffect.cpp b/modules/juce_graphics/effects/juce_StackShadowEffect.cpp index bc5322c35e..fab633c00b 100644 --- a/modules/juce_graphics/effects/juce_StackShadowEffect.cpp +++ b/modules/juce_graphics/effects/juce_StackShadowEffect.cpp @@ -279,7 +279,7 @@ void StackShadow::drawOuterShadowForPath (Graphics& g, const Path& path) const area.getCentreX(), area.getCentreY()); - g2.fillPath (path, t.followedBy (s)); + g2.fillPath (path, s.followedBy (t)); } applyStackBlur (renderedPath, blur); @@ -313,7 +313,7 @@ void StackShadow::drawInnerShadowForPath (Graphics& g, const Path& path) const area.getCentreX(), area.getCentreY()); - g2.strokePath (path, PathStrokeType (static_cast (2 * spread + 1)), t.followedBy(s)); + g2.strokePath (path, PathStrokeType (static_cast (2 * spread + 1)), s.followedBy(t)); } applyStackBlur (renderedPath, blur);