2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../../../core/juce_StandardHeader.h"
30 #include "juce_DropShadowEffect.h"
31 #include "../imaging/juce_Image.h"
32 #include "../colour/juce_PixelFormats.h"
34 #if JUCE_MSVC && JUCE_DEBUG
35 #pragma optimize ("t", on)
38 //==============================================================================
39 DropShadowEffect::DropShadowEffect()
47 DropShadowEffect::~DropShadowEffect()
51 void DropShadowEffect::setShadowProperties (const float newRadius
,
52 const float newOpacity
,
53 const int newShadowOffsetX
,
54 const int newShadowOffsetY
)
56 radius
= jmax (1.1f
, newRadius
);
57 offsetX
= newShadowOffsetX
;
58 offsetY
= newShadowOffsetY
;
62 void DropShadowEffect::applyEffect (Image
& image
, Graphics
& g
, float alpha
)
64 const int w
= image
.getWidth();
65 const int h
= image
.getHeight();
67 Image
shadowImage (Image::SingleChannel
, w
, h
, false);
70 const Image::BitmapData
srcData (image
, Image::BitmapData::readOnly
);
71 const Image::BitmapData
destData (shadowImage
, Image::BitmapData::readWrite
);
73 const int filter
= roundToInt (63.0f
/ radius
);
74 const int radiusMinus1
= roundToInt ((radius
- 1.0f
) * 63.0f
);
76 for (int x
= w
; --x
>= 0;)
80 const PixelARGB
* src
= ((const PixelARGB
*) srcData
.data
) + x
;
81 uint8
* shadowPix
= destData
.data
+ x
;
83 for (int y
= h
; --y
>= 0;)
85 shadowAlpha
= ((shadowAlpha
* radiusMinus1
+ (src
->getAlpha() << 6)) * filter
) >> 12;
87 *shadowPix
= (uint8
) shadowAlpha
;
88 src
= addBytesToPointer (src
, srcData
.lineStride
);
89 shadowPix
+= destData
.lineStride
;
93 for (int y
= h
; --y
>= 0;)
96 uint8
* shadowPix
= destData
.getLinePointer (y
);
98 for (int x
= w
; --x
>= 0;)
100 shadowAlpha
= ((shadowAlpha
* radiusMinus1
+ (*shadowPix
<< 6)) * filter
) >> 12;
101 *shadowPix
++ = (uint8
) shadowAlpha
;
106 g
.setColour (Colours::black
.withAlpha (opacity
* alpha
));
107 g
.drawImageAt (shadowImage
, offsetX
, offsetY
, true);
109 g
.setOpacity (alpha
);
110 g
.drawImageAt (image
, 0, 0);
113 #if JUCE_MSVC && JUCE_DEBUG
114 #pragma optimize ("", on) // resets optimisations to the project defaults