summaryrefslogtreecommitdiffstats
path: root/digikam/libs/dimg/dcolorblend.h
blob: b68322bae762b091b1b596497ffa07586bcf309f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2006-03-01
 * Description : DColor methods for blending
 *
 * Copyright (C) 2006-2007 by Marcel Wiesweg <marcel.wiesweg@gmx.de>
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * ============================================================ */

// Inspired by DirectFB, src/gfx/generic/generic.c:

/*
   (c) Copyright 2000-2002  convergence integrated media GmbH <curanz@convergence.de>
   (c) Copyright 2002-2005  convergence GmbH.

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>,
              Andreas Hundt <andi@fischlustig.de>,
              Sven Neumann <neo@directfb.org>,
              Ville Syrjälä <syrjala@sci.fi> and
              Claudio Ciccani <klan@users.sf.net>.

*/

#ifndef DCOLORBLEND_H
#define DCOLORBLEND_H

namespace Digikam
{

inline void DColor::premultiply()
{
    if (sixteenBit())
        premultiply16(alpha());
    else
        premultiply8(alpha());
}

inline void DColor::demultiply()
{
    if (sixteenBit())
    {
        demultiply16(alpha());
        blendClamp16();
    }
    else
    {
        demultiply8(alpha());
        blendClamp8();
    }
}

inline void DColor::blendZero()
{
    setAlpha(0);
    setRed(0);
    setGreen(0);
    setBlue(0);
}

inline void DColor::blendAlpha16(int alphaValue)
{
    uint Sa = alphaValue + 1;

    setRed  ((Sa * (uint)red()) >> 16);
    setGreen((Sa * (uint)green()) >> 16);
    setBlue ((Sa * (uint)blue()) >> 16);
    setAlpha((Sa * (uint)alpha()) >> 16);
}

inline void DColor::blendAlpha8(int alphaValue)
{
    uint Sa = alphaValue + 1;

    setRed  ((Sa * red()) >> 8);
    setGreen((Sa * green()) >> 8);
    setBlue ((Sa * blue()) >> 8);
    setAlpha((Sa * alpha()) >> 8);
}

inline void DColor::blendInvAlpha16(int alphaValue)
{
    uint Sa = 65536 - alphaValue;

    setRed  ((Sa * (uint)red()) >> 16);
    setGreen((Sa * (uint)green()) >> 16);
    setBlue ((Sa * (uint)blue()) >> 16);
    setAlpha((Sa * (uint)alpha()) >> 16);
}

inline void DColor::blendInvAlpha8(int alphaValue)
{
    uint Sa = 256 - alphaValue;

    setRed  ((Sa * red()) >> 8);
    setGreen((Sa * green()) >> 8);
    setBlue ((Sa * blue()) >> 8);
    setAlpha((Sa * alpha()) >> 8);
}

inline void DColor::premultiply16(int alphaValue)
{
    uint Da = alphaValue + 1;

    setRed  ((Da * (uint)red()) >> 16);
    setGreen((Da * (uint)green()) >> 16);
    setBlue ((Da * (uint)blue()) >> 16);
}

inline void DColor::premultiply8(int alphaValue)
{
    uint Da = alphaValue + 1;

    setRed  ((Da * red()) >> 8);
    setGreen((Da * green()) >> 8);
    setBlue ((Da * blue()) >> 8);
}

inline void DColor::demultiply16(int alphaValue)
{
    uint Da = alphaValue + 1;

    setRed  (((uint)red()   << 16) / Da);
    setGreen(((uint)green() << 16) / Da);
    setBlue (((uint)blue()  << 16) / Da);
}

inline void DColor::demultiply8(int alphaValue)
{
    uint Da = alphaValue + 1;

    setRed  ((red()   << 8) / Da);
    setGreen((green() << 8) / Da);
    setBlue ((blue()  << 8) / Da);
}

inline void DColor::blendAdd(const DColor &src)
{
    setRed  (red()   + src.red());
    setGreen(green() + src.green());
    setBlue (blue()  + src.blue());
    setAlpha(alpha() + src.alpha());
}

inline void DColor::blendClamp16()
{
    if (0xFFFF0000 & red())   setRed(65535);
    if (0xFFFF0000 & green()) setGreen(65535);
    if (0xFFFF0000 & blue())  setBlue(65535);
    if (0xFFFF0000 & alpha()) setAlpha(65535);
}

inline void DColor::blendClamp8()
{
    if (0xFF00 & red())   setRed(255);
    if (0xFF00 & green()) setGreen(255);
    if (0xFF00 & blue())  setBlue(255);
    if (0xFF00 & alpha()) setAlpha(255);
}

} // namespace Digikam

#endif  // DCOLORBLEND_H