The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

142 lines
4.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. static inline void blurDataTriplets (uint8* d, int num, const int delta) noexcept
  19. {
  20. uint32 last = d[0];
  21. d[0] = (uint8) ((d[0] + d[delta] + 1) / 3);
  22. d += delta;
  23. num -= 2;
  24. do
  25. {
  26. const uint32 newLast = d[0];
  27. d[0] = (uint8) ((last + d[0] + d[delta] + 1) / 3);
  28. d += delta;
  29. last = newLast;
  30. }
  31. while (--num > 0);
  32. d[0] = (uint8) ((last + d[0] + 1) / 3);
  33. }
  34. static void blurSingleChannelImage (uint8* const data, const int width, const int height,
  35. const int lineStride, const int repetitions) noexcept
  36. {
  37. jassert (width > 2 && height > 2);
  38. for (int y = 0; y < height; ++y)
  39. for (int i = repetitions; --i >= 0;)
  40. blurDataTriplets (data + lineStride * y, width, 1);
  41. for (int x = 0; x < width; ++x)
  42. for (int i = repetitions; --i >= 0;)
  43. blurDataTriplets (data + x, height, lineStride);
  44. }
  45. static void blurSingleChannelImage (Image& image, int radius)
  46. {
  47. const Image::BitmapData bm (image, Image::BitmapData::readWrite);
  48. blurSingleChannelImage (bm.data, bm.width, bm.height, bm.lineStride, 2 * radius);
  49. }
  50. //==============================================================================
  51. DropShadow::DropShadow() noexcept
  52. : colour (0x90000000), radius (4)
  53. {
  54. }
  55. DropShadow::DropShadow (const Colour& shadowColour, const int r, const Point<int>& o) noexcept
  56. : colour (shadowColour), radius (r), offset (o)
  57. {
  58. jassert (radius > 0);
  59. }
  60. void DropShadow::drawForImage (Graphics& g, const Image& srcImage) const
  61. {
  62. jassert (radius > 0);
  63. if (srcImage.isValid())
  64. {
  65. Image shadowImage (srcImage.convertedToFormat (Image::SingleChannel));
  66. shadowImage.duplicateIfShared();
  67. blurSingleChannelImage (shadowImage, radius);
  68. g.setColour (colour);
  69. g.drawImageAt (shadowImage, offset.x, offset.y, true);
  70. }
  71. }
  72. void DropShadow::drawForPath (Graphics& g, const Path& path) const
  73. {
  74. jassert (radius > 0);
  75. const Rectangle<int> area ((path.getBounds().getSmallestIntegerContainer() + offset)
  76. .getIntersection (g.getClipBounds())
  77. .expanded (radius + 1, radius + 1));
  78. if (area.getWidth() > 2 && area.getHeight() > 2)
  79. {
  80. Image renderedPath (Image::SingleChannel, area.getWidth(), area.getHeight(), true);
  81. {
  82. Graphics g2 (renderedPath);
  83. g2.setColour (Colours::white);
  84. g2.fillPath (path, AffineTransform::translation ((float) (offset.x - area.getX()),
  85. (float) (offset.y - area.getY())));
  86. }
  87. blurSingleChannelImage (renderedPath, radius);
  88. g.setColour (colour);
  89. g.drawImageAt (renderedPath, area.getX(), area.getY(), true);
  90. }
  91. }
  92. //==============================================================================
  93. DropShadowEffect::DropShadowEffect() {}
  94. DropShadowEffect::~DropShadowEffect() {}
  95. void DropShadowEffect::setShadowProperties (const DropShadow& newShadow)
  96. {
  97. shadow = newShadow;
  98. }
  99. void DropShadowEffect::applyEffect (Image& image, Graphics& g, float scaleFactor, float alpha)
  100. {
  101. DropShadow s (shadow);
  102. s.radius = roundToInt (s.radius * scaleFactor);
  103. s.colour = s.colour.withMultipliedAlpha (alpha);
  104. s.offset.x = roundToInt (s.offset.x * scaleFactor);
  105. s.offset.y = roundToInt (s.offset.y * scaleFactor);
  106. s.drawForImage (g, image);
  107. g.setOpacity (alpha);
  108. g.drawImageAt (image, 0, 0);
  109. }