3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #define DEBUG_KP_PIXMAP_FX 0
32 #include <kpPixmapFX.h>
36 #include <qapplication.h>
38 #include <qdatetime.h>
41 #include <qpainterpath.h>
51 #include <kmessagebox.h>
53 #include <kpAbstractSelection.h>
57 #include <kconfiggroup.h>
61 QColor
kpPixmapFX::draw_ToQColor (const kpColor
&color
, bool drawingOnRGBLayer
)
63 if (drawingOnRGBLayer
)
65 if (color
.isOpaque ())
66 return color
.toQColor ();
67 else // if (color.isTransparent())
69 // (arbitrary as image will be transparent here)
74 return color
.maskColor ();
81 void (*userDrawFunc
) (QPainter
* /*p*/,
82 bool /*drawingOnRGBLayer*/,
87 static QRect
DrawHelper (QPainter
*rgbPainter
, QPainter
*maskPainter
, void *data
)
89 DrawPack
*pack
= static_cast <DrawPack
*> (data
);
92 pack
->userDrawFunc (rgbPainter
, true/*drawing on RGB*/, pack
->userData
);
95 pack
->userDrawFunc (maskPainter
, false/*drawing on mask*/, pack
->userData
);
97 // Assume the whole image was clobbered.
98 return QRect (0, 0, pack
->image
->width (), pack
->image
->height ());
102 void kpPixmapFX::draw (QPixmap
*image
,
103 void (*drawFunc
) (QPainter
* /*p*/,
104 bool /*drawingOnRGBLayer*/,
106 bool anyColorOpaque
, bool anyColorTransparent
,
111 pack
.userDrawFunc
= drawFunc
;
112 pack
.userData
= data
;
114 // Call below method.
115 kpPixmapFX::draw (image
,
117 anyColorOpaque
, anyColorTransparent
,
122 QRect
kpPixmapFX::draw (QPixmap
*image
,
123 QRect (*drawFunc
) (QPainter
* /*rgbPainter*/, QPainter
* /*maskPainter*/,
125 bool anyColorOpaque
, bool anyColorTransparent
,
128 #if DEBUG_KP_PIXMAP_FX
129 kDebug () << "kppixmapfx.cpp:Draw(image: rect=" << image
->rect ()
130 << ",drawFunc=" << drawFunc
131 << ",anyColorOpaque=" << anyColorOpaque
132 << ",anyColorTransparent=" << anyColorTransparent
136 KP_PFX_CHECK_NO_ALPHA_CHANNEL (*image
);
138 // Interesting note (in case we support depth 1 later):
139 // QBitmap's do not have masks but QBitmap::mask() returns itself.
140 Q_ASSERT (image
->depth () > 1);
143 QBitmap mask
= image
->mask ();
145 #if DEBUG_KP_PIXMAP_FX
146 kDebug () << "\tDraw(): hasMask=" << !mask
.isNull ();
149 QPainter rgbPainter
, maskPainter
;
151 // Draw on RGB layer?
154 #if DEBUG_KP_PIXMAP_FX
155 kDebug () << "\tDraw(): drawing on RGB";
157 // RGB draw is not allowed to touch mask.
158 image
->setMask (QBitmap ());
160 rgbPainter
.begin (image
);
163 // Draw on mask layer?
164 if (anyColorTransparent
||
167 #if DEBUG_KP_PIXMAP_FX
168 kDebug () << "\tDraw(): drawing on transparent";
171 mask
= kpPixmapFX::getNonNullMask (*image
);
173 maskPainter
.begin (&mask
);
177 if (!rgbPainter
.isActive () && !maskPainter
.isActive ())
184 const QRect dirtyRect
= (*drawFunc
) (
185 rgbPainter
.isActive () ? &rgbPainter
: 0,
186 maskPainter
.isActive () ? &maskPainter
: 0,
190 if (rgbPainter
.isActive ())
193 if (maskPainter
.isActive ())
198 // A mask should not have been created - that's the job of the next step.
199 Q_ASSERT (!kpPixmapFX::hasMask (*image
));
203 #if DEBUG_KP_PIXMAP_FX
204 kDebug () << "\tDraw(): setting mask " << !mask
.isNull ();
208 image
->setMask (mask
);
211 KP_PFX_CHECK_NO_ALPHA_CHANNEL (*image
);