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.

113 lines
3.8KB

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