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.

112 lines
3.6KB

  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. BEGIN_JUCE_NAMESPACE
  19. #if JUCE_MSVC && JUCE_DEBUG
  20. #pragma optimize ("t", on)
  21. #endif
  22. //==============================================================================
  23. DropShadowEffect::DropShadowEffect()
  24. : offsetX (0),
  25. offsetY (0),
  26. radius (4),
  27. opacity (0.6f)
  28. {
  29. }
  30. DropShadowEffect::~DropShadowEffect()
  31. {
  32. }
  33. void DropShadowEffect::setShadowProperties (const float newRadius,
  34. const float newOpacity,
  35. const int newShadowOffsetX,
  36. const int newShadowOffsetY)
  37. {
  38. radius = jmax (1.1f, newRadius);
  39. offsetX = newShadowOffsetX;
  40. offsetY = newShadowOffsetY;
  41. opacity = newOpacity;
  42. }
  43. void DropShadowEffect::applyEffect (Image& image, Graphics& g, float alpha)
  44. {
  45. const int w = image.getWidth();
  46. const int h = image.getHeight();
  47. Image shadowImage (Image::SingleChannel, w, h, false);
  48. {
  49. const Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  50. const Image::BitmapData destData (shadowImage, Image::BitmapData::readWrite);
  51. const int filter = roundToInt (63.0f / radius);
  52. const int radiusMinus1 = roundToInt ((radius - 1.0f) * 63.0f);
  53. for (int x = w; --x >= 0;)
  54. {
  55. int shadowAlpha = 0;
  56. const PixelARGB* src = ((const PixelARGB*) srcData.data) + x;
  57. uint8* shadowPix = destData.data + x;
  58. for (int y = h; --y >= 0;)
  59. {
  60. shadowAlpha = ((shadowAlpha * radiusMinus1 + (src->getAlpha() << 6)) * filter) >> 12;
  61. *shadowPix = (uint8) shadowAlpha;
  62. src = addBytesToPointer (src, srcData.lineStride);
  63. shadowPix += destData.lineStride;
  64. }
  65. }
  66. for (int y = h; --y >= 0;)
  67. {
  68. int shadowAlpha = 0;
  69. uint8* shadowPix = destData.getLinePointer (y);
  70. for (int x = w; --x >= 0;)
  71. {
  72. shadowAlpha = ((shadowAlpha * radiusMinus1 + (*shadowPix << 6)) * filter) >> 12;
  73. *shadowPix++ = (uint8) shadowAlpha;
  74. }
  75. }
  76. }
  77. g.setColour (Colours::black.withAlpha (opacity * alpha));
  78. g.drawImageAt (shadowImage, offsetX, offsetY, true);
  79. g.setOpacity (alpha);
  80. g.drawImageAt (image, 0, 0);
  81. }
  82. #if JUCE_MSVC && JUCE_DEBUG
  83. #pragma optimize ("", on) // resets optimisations to the project defaults
  84. #endif
  85. END_JUCE_NAMESPACE