2
* Copyright (c) 2006 Boudewijn Rempt <boud@valdyas.org>
4
* This program is free software const; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation const; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY const; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public License
15
* along with this program const; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
#ifndef RGBCOMPOSITEOPSATURATION_H
20
#define RGBCOMPOSITEOPSATURATION_H
22
#include "KoColorSpaceMaths.h"
23
#include "KoColorConversions.h"
24
#include <KoCompositeOp.h>
26
#define SCALE_TO_FLOAT( v ) KoColorSpaceMaths< channels_type, float>::scaleToA( v )
27
#define SCALE_FROM_FLOAT( v ) KoColorSpaceMaths< float, channels_type>::scaleToA( v )
29
template<class _CSTraits>
30
class RgbCompositeOpSaturation: public KoCompositeOp
32
typedef typename _CSTraits::channels_type channels_type;
33
typedef typename KoColorSpaceMathsTraits<typename _CSTraits::channels_type>::compositetype compositetype;
37
RgbCompositeOpSaturation(KoColorSpace * cs, const bool userVisible = true)
38
: KoCompositeOp(cs, COMPOSITE_SATURATION, i18n("Saturation"), "", userVisible) {
41
using KoCompositeOp::composite;
43
void composite(quint8 *dstRowStart, qint32 dstRowStride,
44
const quint8 *srcRowStart, qint32 srcRowStride,
45
const quint8 *maskRowStart, qint32 maskRowStride,
46
qint32 rows, qint32 numColumns,
48
const QBitArray & channelFlags) const {
51
const quint8 *mask = maskRowStart;
52
const channels_type *src = reinterpret_cast<const channels_type *>(srcRowStart);
53
channels_type *dst = reinterpret_cast<channels_type *>(dstRowStart);
55
for (int i = numColumns ; i > 0 ; --i) {
56
channels_type srcAlpha = src[_CSTraits::alpha_pos];
57
channels_type dstAlpha = dst[_CSTraits::alpha_pos];
59
srcAlpha = qMin(srcAlpha, dstAlpha);
61
// apply the alphamask
63
if (*mask != OPACITY_OPAQUE_U8) {
64
channels_type tmpOpacity = KoColorSpaceMaths<quint8 , channels_type>::scaleToA(*mask);
65
srcAlpha = KoColorSpaceMaths<channels_type>::multiply(srcAlpha, tmpOpacity);
70
if (srcAlpha != NATIVE_OPACITY_TRANSPARENT) {
72
if (opacity != OPACITY_OPAQUE_U8) {
73
channels_type tmpOpacity = KoColorSpaceMaths<quint8 , channels_type>::scaleToA(opacity);
74
srcAlpha = KoColorSpaceMaths<channels_type>::multiply(src[_CSTraits::alpha_pos], tmpOpacity);
77
channels_type srcBlend;
79
if (dstAlpha == NATIVE_OPACITY_OPAQUE) {
82
channels_type newAlpha = dstAlpha + KoColorSpaceMaths<channels_type>::multiply(NATIVE_OPACITY_OPAQUE - dstAlpha, srcAlpha);
83
dst[_CSTraits::alpha_pos] = newAlpha;
86
srcBlend = KoColorSpaceMaths<channels_type>::divide(srcAlpha, newAlpha);
92
float dstRed = SCALE_TO_FLOAT(dst[_CSTraits::red_pos]);
93
float dstGreen = SCALE_TO_FLOAT(dst[_CSTraits::green_pos]);
94
float dstBlue = SCALE_TO_FLOAT(dst[_CSTraits::blue_pos]);
103
float srcRed = SCALE_TO_FLOAT(src[_CSTraits::red_pos]);
104
float srcGreen = SCALE_TO_FLOAT(src[_CSTraits::green_pos]);
105
float srcBlue = SCALE_TO_FLOAT(src[_CSTraits::blue_pos]);
107
RGBToHSV(srcRed, srcGreen, srcBlue, &srcHue, &srcSaturation, &srcValue);
108
RGBToHSV(dstRed, dstGreen, dstBlue, &dstHue, &dstSaturation, &dstValue);
110
HSVToRGB(dstHue, srcSaturation, dstValue, &srcRed, &srcGreen, &srcBlue);
112
if (channelFlags.isEmpty() || channelFlags.testBit(_CSTraits::red_pos))
113
dst[_CSTraits::red_pos] = KoColorSpaceMaths<channels_type>::blend(SCALE_FROM_FLOAT(srcRed), SCALE_FROM_FLOAT(dstRed), srcBlend);
114
if (channelFlags.isEmpty() || channelFlags.testBit(_CSTraits::green_pos))
115
dst[_CSTraits::green_pos] = KoColorSpaceMaths<channels_type>::blend(SCALE_FROM_FLOAT(srcGreen), SCALE_FROM_FLOAT(dstGreen), srcBlend);
116
if (channelFlags.isEmpty() || channelFlags.testBit(_CSTraits::blue_pos))
117
dst[_CSTraits::blue_pos] = KoColorSpaceMaths<channels_type>::blend(SCALE_FROM_FLOAT(srcBlue), SCALE_FROM_FLOAT(dstBlue), srcBlend);
120
src += _CSTraits::channels_nb;
121
dst += _CSTraits::channels_nb;
125
srcRowStart += srcRowStride;
126
dstRowStart += dstRowStride;
128
maskRowStart += maskRowStride;