~ubuntu-branches/ubuntu/oneiric/koffice/oneiric-updates

« back to all changes in this revision

Viewing changes to plugins/colorengines/lcms2/compositeops/RgbCompositeOpSaturation.h

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (c) 2006 Boudewijn Rempt <boud@valdyas.org>
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 */
 
18
 
 
19
#ifndef RGBCOMPOSITEOPSATURATION_H
 
20
#define RGBCOMPOSITEOPSATURATION_H
 
21
 
 
22
#include "KoColorSpaceMaths.h"
 
23
#include "KoColorConversions.h"
 
24
#include <KoCompositeOp.h>
 
25
 
 
26
#define SCALE_TO_FLOAT( v ) KoColorSpaceMaths< channels_type, float>::scaleToA( v )
 
27
#define SCALE_FROM_FLOAT( v ) KoColorSpaceMaths< float, channels_type>::scaleToA( v )
 
28
 
 
29
template<class _CSTraits>
 
30
class RgbCompositeOpSaturation: public KoCompositeOp
 
31
{
 
32
    typedef typename _CSTraits::channels_type channels_type;
 
33
    typedef typename KoColorSpaceMathsTraits<typename _CSTraits::channels_type>::compositetype compositetype;
 
34
 
 
35
public:
 
36
 
 
37
    RgbCompositeOpSaturation(KoColorSpace * cs, const bool userVisible = true)
 
38
            : KoCompositeOp(cs, COMPOSITE_SATURATION, i18n("Saturation"), "", userVisible) {
 
39
    }
 
40
 
 
41
    using KoCompositeOp::composite;
 
42
 
 
43
    void composite(quint8 *dstRowStart, qint32 dstRowStride,
 
44
                   const quint8 *srcRowStart, qint32 srcRowStride,
 
45
                   const quint8 *maskRowStart, qint32 maskRowStride,
 
46
                   qint32 rows, qint32 numColumns,
 
47
                   quint8 opacity,
 
48
                   const QBitArray & channelFlags) const {
 
49
 
 
50
        while (rows > 0) {
 
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);
 
54
 
 
55
            for (int i = numColumns ; i > 0 ; --i) {
 
56
                channels_type srcAlpha = src[_CSTraits::alpha_pos];
 
57
                channels_type dstAlpha = dst[_CSTraits::alpha_pos];
 
58
 
 
59
                srcAlpha = qMin(srcAlpha, dstAlpha);
 
60
 
 
61
                // apply the alphamask
 
62
                if (mask != 0) {
 
63
                    if (*mask != OPACITY_OPAQUE_U8) {
 
64
                        channels_type tmpOpacity = KoColorSpaceMaths<quint8 , channels_type>::scaleToA(*mask);
 
65
                        srcAlpha =  KoColorSpaceMaths<channels_type>::multiply(srcAlpha, tmpOpacity);
 
66
                    }
 
67
                    mask++;
 
68
                }
 
69
 
 
70
                if (srcAlpha != NATIVE_OPACITY_TRANSPARENT) {
 
71
 
 
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);
 
75
                    }
 
76
 
 
77
                    channels_type srcBlend;
 
78
 
 
79
                    if (dstAlpha == NATIVE_OPACITY_OPAQUE) {
 
80
                        srcBlend = srcAlpha;
 
81
                    } else {
 
82
                        channels_type newAlpha = dstAlpha + KoColorSpaceMaths<channels_type>::multiply(NATIVE_OPACITY_OPAQUE - dstAlpha, srcAlpha);
 
83
                        dst[_CSTraits::alpha_pos] = newAlpha;
 
84
 
 
85
                        if (newAlpha != 0) {
 
86
                            srcBlend = KoColorSpaceMaths<channels_type>::divide(srcAlpha, newAlpha);
 
87
                        } else {
 
88
                            srcBlend = srcAlpha;
 
89
                        }
 
90
                    }
 
91
 
 
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]);
 
95
 
 
96
                    float srcHue;
 
97
                    float srcSaturation;
 
98
                    float srcValue;
 
99
                    float dstHue;
 
100
                    float dstSaturation;
 
101
                    float dstValue;
 
102
 
 
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]);
 
106
 
 
107
                    RGBToHSV(srcRed, srcGreen, srcBlue, &srcHue, &srcSaturation, &srcValue);
 
108
                    RGBToHSV(dstRed, dstGreen, dstBlue, &dstHue, &dstSaturation, &dstValue);
 
109
 
 
110
                    HSVToRGB(dstHue, srcSaturation, dstValue, &srcRed, &srcGreen, &srcBlue);
 
111
 
 
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);
 
118
                }
 
119
 
 
120
                src += _CSTraits::channels_nb;
 
121
                dst += _CSTraits::channels_nb;
 
122
            }
 
123
 
 
124
            rows--;
 
125
            srcRowStart += srcRowStride;
 
126
            dstRowStart += dstRowStride;
 
127
            if (maskRowStart)
 
128
                maskRowStart += maskRowStride;
 
129
        }
 
130
    }
 
131
};
 
132
 
 
133
#endif