Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
6.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. static inline void blurDataTriplets (uint8* d, int num, const int delta) noexcept
  18. {
  19. uint32 last = d[0];
  20. d[0] = (uint8) ((d[0] + d[delta] + 1) / 3);
  21. d += delta;
  22. num -= 2;
  23. do
  24. {
  25. const uint32 newLast = d[0];
  26. d[0] = (uint8) ((last + d[0] + d[delta] + 1) / 3);
  27. d += delta;
  28. last = newLast;
  29. }
  30. while (--num > 0);
  31. d[0] = (uint8) ((last + d[0] + 1) / 3);
  32. }
  33. static void blurSingleChannelImage (uint8* const data, const int width, const int height,
  34. const int lineStride, const int repetitions) noexcept
  35. {
  36. jassert (width > 2 && height > 2);
  37. for (int y = 0; y < height; ++y)
  38. for (int i = repetitions; --i >= 0;)
  39. blurDataTriplets (data + lineStride * y, width, 1);
  40. for (int x = 0; x < width; ++x)
  41. for (int i = repetitions; --i >= 0;)
  42. blurDataTriplets (data + x, height, lineStride);
  43. }
  44. static void blurSingleChannelImage (Image& image, int radius)
  45. {
  46. const Image::BitmapData bm (image, Image::BitmapData::readWrite);
  47. blurSingleChannelImage (bm.data, bm.width, bm.height, bm.lineStride, 2 * radius);
  48. }
  49. //==============================================================================
  50. DropShadow::DropShadow() noexcept
  51. : colour (0x90000000), radius (4)
  52. {
  53. }
  54. DropShadow::DropShadow (Colour shadowColour, const int r, Point<int> o) noexcept
  55. : colour (shadowColour), radius (r), offset (o)
  56. {
  57. jassert (radius > 0);
  58. }
  59. void DropShadow::drawForImage (Graphics& g, const Image& srcImage) const
  60. {
  61. jassert (radius > 0);
  62. if (srcImage.isValid())
  63. {
  64. Image shadowImage (srcImage.convertedToFormat (Image::SingleChannel));
  65. shadowImage.duplicateIfShared();
  66. blurSingleChannelImage (shadowImage, radius);
  67. g.setColour (colour);
  68. g.drawImageAt (shadowImage, offset.x, offset.y, true);
  69. }
  70. }
  71. void DropShadow::drawForPath (Graphics& g, const Path& path) const
  72. {
  73. jassert (radius > 0);
  74. const Rectangle<int> area ((path.getBounds().getSmallestIntegerContainer() + offset)
  75. .expanded (radius + 1)
  76. .getIntersection (g.getClipBounds().expanded (radius + 1)));
  77. if (area.getWidth() > 2 && area.getHeight() > 2)
  78. {
  79. Image renderedPath (Image::SingleChannel, area.getWidth(), area.getHeight(), true);
  80. {
  81. Graphics g2 (renderedPath);
  82. g2.setColour (Colours::white);
  83. g2.fillPath (path, AffineTransform::translation ((float) (offset.x - area.getX()),
  84. (float) (offset.y - area.getY())));
  85. }
  86. blurSingleChannelImage (renderedPath, radius);
  87. g.setColour (colour);
  88. g.drawImageAt (renderedPath, area.getX(), area.getY(), true);
  89. }
  90. }
  91. static void drawShadowSection (Graphics& g, ColourGradient& cg, Rectangle<float> area,
  92. bool isCorner, float centreX, float centreY, float edgeX, float edgeY)
  93. {
  94. cg.point1 = area.getRelativePoint (centreX, centreY);
  95. cg.point2 = area.getRelativePoint (edgeX, edgeY);
  96. cg.isRadial = isCorner;
  97. g.setGradientFill (cg);
  98. g.fillRect (area);
  99. }
  100. void DropShadow::drawForRectangle (Graphics& g, const Rectangle<int>& targetArea) const
  101. {
  102. ColourGradient cg (colour, 0, 0, colour.withAlpha (0.0f), 0, 0, false);
  103. for (float i = 0.05f; i < 1.0f; i += 0.1f)
  104. cg.addColour (1.0 - i, colour.withMultipliedAlpha (i * i));
  105. const float radiusInset = (radius + 1) / 2.0f;
  106. const float expandedRadius = radius + radiusInset;
  107. const Rectangle<float> area (targetArea.toFloat().reduced (radiusInset) + offset.toFloat());
  108. Rectangle<float> r (area.expanded (expandedRadius));
  109. Rectangle<float> top (r.removeFromTop (expandedRadius));
  110. Rectangle<float> bottom (r.removeFromBottom (expandedRadius));
  111. drawShadowSection (g, cg, top.removeFromLeft (expandedRadius), true, 1.0f, 1.0f, 0, 1.0f);
  112. drawShadowSection (g, cg, top.removeFromRight (expandedRadius), true, 0, 1.0f, 1.0f, 1.0f);
  113. drawShadowSection (g, cg, top, false, 0, 1.0f, 0, 0);
  114. drawShadowSection (g, cg, bottom.removeFromLeft (expandedRadius), true, 1.0f, 0, 0, 0);
  115. drawShadowSection (g, cg, bottom.removeFromRight (expandedRadius), true, 0, 0, 1.0f, 0);
  116. drawShadowSection (g, cg, bottom, false, 0, 0, 0, 1.0f);
  117. drawShadowSection (g, cg, r.removeFromLeft (expandedRadius), false, 1.0f, 0, 0, 0);
  118. drawShadowSection (g, cg, r.removeFromRight (expandedRadius), false, 0, 0, 1.0f, 0);
  119. g.setColour (colour);
  120. g.fillRect (area);
  121. }
  122. //==============================================================================
  123. DropShadowEffect::DropShadowEffect() {}
  124. DropShadowEffect::~DropShadowEffect() {}
  125. void DropShadowEffect::setShadowProperties (const DropShadow& newShadow)
  126. {
  127. shadow = newShadow;
  128. }
  129. void DropShadowEffect::applyEffect (Image& image, Graphics& g, float scaleFactor, float alpha)
  130. {
  131. DropShadow s (shadow);
  132. s.radius = roundToInt (s.radius * scaleFactor);
  133. s.colour = s.colour.withMultipliedAlpha (alpha);
  134. s.offset.x = roundToInt (s.offset.x * scaleFactor);
  135. s.offset.y = roundToInt (s.offset.y * scaleFactor);
  136. s.drawForImage (g, image);
  137. g.setOpacity (alpha);
  138. g.drawImageAt (image, 0, 0);
  139. }