3 Copyright (c) 2007 Mike Gashler <gashlerm@yahoo.com>
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.
28 // TODO: Clarence's code review
30 #include <kpEffectHSV.h>
39 #include <kpPixmapFX.h>
42 static void ColorToHSV(unsigned int c
, float* pHue
, float* pSaturation
, float* pValue
)
54 *pHue
= (float)(r
- g
) / ((b
- min
) * 6) + (float)2 / 3;
55 *pSaturation
= (float)1 - (float)min
/ (float)b
;
62 *pValue
= (float)b
/ 255;
70 *pHue
= (float)(b
- r
) / ((g
- min
) * 6) + (float)1 / 3;
71 *pSaturation
= (float)1 - (float)min
/ (float)g
;
78 *pValue
= (float)g
/ 255;
86 *pHue
= (float)(g
- b
) / ((r
- min
) * 6);
89 *pSaturation
= (float)1 - (float)min
/ (float)r
;
96 *pValue
= (float)r
/ 255;
100 static unsigned int HSVToColor(int alpha
, float hue
, float saturation
, float value
)
102 //Q_ASSERT (hue >= 0 && hue <= 1 && saturation >= 0 && saturation <= 1 && value >= 0 && value <= 1);
104 hue
*= (float)5.999999;
107 float p
= value
* ((float)1 - saturation
);
108 float q
= value
* ((float)1 - ((h
& 1) == 0 ? (float)1 - f
: f
) * saturation
);
111 case 0: return qRgba((int)(value
* 255.999999), (int)(q
* 255.999999), (int)(p
* 255.999999), alpha
);
112 case 1: return qRgba((int)(q
* 255.999999), (int)(value
* 255.999999), (int)(p
* 255.999999), alpha
);
113 case 2: return qRgba((int)(p
* 255.999999), (int)(value
* 255.999999), (int)(q
* 255.999999), alpha
);
114 case 3: return qRgba((int)(p
* 255.999999), (int)(q
* 255.999999), (int)(value
* 255.999999), alpha
);
115 case 4: return qRgba((int)(q
* 255.999999), (int)(p
* 255.999999), (int)(value
* 255.999999), alpha
);
116 case 5: return qRgba((int)(value
* 255.999999), (int)(p
* 255.999999), (int)(q
* 255.999999), alpha
);
118 return qRgba(0, 0, 0, alpha
);
121 static QRgb
AdjustHSVInternal (QRgb pix
, double hueDiv360
, double saturation
, double value
)
124 ::ColorToHSV(pix
, &h
, &s
, &v
);
126 const int alpha
= qAlpha(pix
);
128 h
+= (float)hueDiv360
;
131 s
= qMax((float)0, qMin((float)1, s
+ (float)saturation
));
133 v
= qMax((float)0, qMin((float)1, v
+ (float)value
));
135 return ::HSVToColor(alpha
, h
, s
, v
);
138 static void AdjustHSV (QImage
* pImage
, double hue
, double saturation
, double value
)
142 if (pImage
->depth () > 8)
144 for (int y
= 0; y
< pImage
->height (); y
++)
146 for (int x
= 0; x
< pImage
->width (); x
++)
148 QRgb pix
= pImage
->pixel (x
, y
);
149 pix
= ::AdjustHSVInternal (pix
, hue
, saturation
, value
);
150 pImage
->setPixel (x
, y
, pix
);
156 for (int i
= 0; i
< pImage
->numColors (); i
++)
158 QRgb pix
= pImage
->color (i
);
159 pix
= ::AdjustHSVInternal (pix
, hue
, saturation
, value
);
160 pImage
->setColor (i
, pix
);
166 kpImage
kpEffectHSV::applyEffect (const kpImage
&image
,
167 double hue
, double saturation
, double value
)
169 QImage qimage
= kpPixmapFX::convertToQImage (image
);
171 ::AdjustHSV (&qimage
, hue
, saturation
, value
);
173 QPixmap retPixmap
= kpPixmapFX::convertToPixmap (qimage
);