digiKam
dcolorblend.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date : 2006-03-01
7  * Description : DColor methods for blending
8  * Integer arithmetic inspired by DirectFB,
9  * src/gfx/generic/generic.c and src/display/idirectfbsurface.c
10  *
11  * Copyright (C) 2006-2009 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
12  * Copyright (C) 2000-2002 by convergence integrated media GmbH <curanz@convergence.de>
13  * Copyright (C) 2002-2005 by Denis Oliver Kropp <dok at directfb dot org>
14  * Copyright (C) 2002-2005 by Andreas Hundt <andi at fischlustig dot de>
15  * Copyright (C) 2002-2005 by Sven Neumann <neo at directfb dot org>
16  * Copyright (C) 2002-2005 by Ville Syrj <syrjala at sci dot fi>
17  * Copyright (C) 2002-2005 by Claudio Ciccani <klan at users dot sf dot net>
18  *
19  * This program is free software; you can redistribute it
20  * and/or modify it under the terms of the GNU General
21  * Public License as published by the Free Software Foundation;
22  * either version 2, or (at your option)
23  * any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU General Public License for more details.
29  *
30  * ============================================================ */
31 
32 #ifndef DIGIKAM_DCOLOR_BLEND_H
33 #define DIGIKAM_DCOLOR_BLEND_H
34 
35 namespace Digikam
36 {
37 
38 inline void DColor::premultiply()
39 {
40  if (sixteenBit())
41  {
43  }
44  else
45  {
47  }
48 }
49 
50 inline void DColor::demultiply()
51 {
52  if (sixteenBit())
53  {
55  blendClamp16();
56  }
57  else
58  {
59  demultiply8(alpha());
60  blendClamp8();
61  }
62 }
63 
64 inline void DColor::blendZero()
65 {
66  setAlpha(0);
67  setRed(0);
68  setGreen(0);
69  setBlue(0);
70 }
71 
72 inline void DColor::blendAlpha16(int alphaValue)
73 {
74  uint Sa = alphaValue + 1;
75 
76  setRed ((Sa * (uint)red()) >> 16);
77  setGreen((Sa * (uint)green()) >> 16);
78  setBlue ((Sa * (uint)blue()) >> 16);
79  setAlpha((Sa * (uint)alpha()) >> 16);
80 }
81 
82 inline void DColor::blendAlpha8(int alphaValue)
83 {
84  uint Sa = alphaValue + 1;
85 
86  setRed ((Sa * red()) >> 8);
87  setGreen((Sa * green()) >> 8);
88  setBlue ((Sa * blue()) >> 8);
89  setAlpha((Sa * alpha()) >> 8);
90 }
91 
92 inline void DColor::blendInvAlpha16(int alphaValue)
93 {
94  uint Sa = 65536 - alphaValue;
95 
96  setRed ((Sa * (uint)red()) >> 16);
97  setGreen((Sa * (uint)green()) >> 16);
98  setBlue ((Sa * (uint)blue()) >> 16);
99  setAlpha((Sa * (uint)alpha()) >> 16);
100 }
101 
102 inline void DColor::blendInvAlpha8(int alphaValue)
103 {
104  uint Sa = 256 - alphaValue;
105 
106  setRed ((Sa * red()) >> 8);
107  setGreen((Sa * green()) >> 8);
108  setBlue ((Sa * blue()) >> 8);
109  setAlpha((Sa * alpha()) >> 8);
110 }
111 
112 inline void DColor::premultiply16(int alphaValue)
113 {
114  uint Da = alphaValue + 1;
115 
116  setRed ((Da * (uint)red()) >> 16);
117  setGreen((Da * (uint)green()) >> 16);
118  setBlue ((Da * (uint)blue()) >> 16);
119 }
120 
121 inline void DColor::premultiply8(int alphaValue)
122 {
123  uint Da = alphaValue + 1;
124 
125  setRed ((Da * red()) >> 8);
126  setGreen((Da * green()) >> 8);
127  setBlue ((Da * blue()) >> 8);
128 }
129 
130 inline void DColor::demultiply16(int alphaValue)
131 {
132  uint Da = alphaValue + 1;
133 
134  setRed (((uint)red() << 16) / Da);
135  setGreen(((uint)green() << 16) / Da);
136  setBlue (((uint)blue() << 16) / Da);
137 }
138 
139 inline void DColor::demultiply8(int alphaValue)
140 {
141  uint Da = alphaValue + 1;
142 
143  setRed ((red() << 8) / Da);
144  setGreen((green() << 8) / Da);
145  setBlue ((blue() << 8) / Da);
146 }
147 
148 inline void DColor::blendAdd(const DColor& src)
149 {
150  setRed (red() + src.red());
151  setGreen(green() + src.green());
152  setBlue (blue() + src.blue());
153  setAlpha(alpha() + src.alpha());
154 }
155 
156 inline void DColor::blendClamp16()
157 {
158  if (0xFFFF0000 & red())
159  {
160  setRed(65535);
161  }
162 
163  if (0xFFFF0000 & green())
164  {
165  setGreen(65535);
166  }
167 
168  if (0xFFFF0000 & blue())
169  {
170  setBlue(65535);
171  }
172 
173  if (0xFFFF0000 & alpha())
174  {
175  setAlpha(65535);
176  }
177 }
178 
179 inline void DColor::blendClamp8()
180 {
181  if (0xFFFFFF00 & red())
182  {
183  setRed(255);
184  }
185 
186  if (0xFFFFFF00 & green())
187  {
188  setGreen(255);
189  }
190 
191  if (0xFFFFFF00 & blue())
192  {
193  setBlue(255);
194  }
195 
196  if (0xFFFFFF00 & alpha())
197  {
198  setAlpha(255);
199  }
200 }
201 
202 inline void DColor::multiply(float factor)
203 {
204  setRed (lround(red() * factor));
205  setGreen(lround(green() * factor));
206  setBlue (lround(blue() * factor));
207  setAlpha(lround(alpha() * factor));
208 }
209 
210 } // namespace Digikam
211 
212 #endif // DIGIKAM_DCOLOR_BLEND_H
Definition: dcolor.h:43
void demultiply16(int alpha)
Definition: dcolorblend.h:130
int green() const
Definition: dcolor.h:106
void setGreen(int green)
Definition: dcolor.h:131
void setAlpha(int alpha)
Definition: dcolor.h:141
void setRed(int red)
Definition: dcolor.h:126
void premultiply16(int alpha)
Definition: dcolorblend.h:112
void blendClamp8()
Definition: dcolorblend.h:179
bool sixteenBit() const
Definition: dcolor.h:121
void demultiply8(int alpha)
Definition: dcolorblend.h:139
void blendInvAlpha8(int alpha)
Definition: dcolorblend.h:102
void setBlue(int blue)
Definition: dcolor.h:136
void blendAdd(const DColor &src)
Definition: dcolorblend.h:148
void blendZero()
Definition: dcolorblend.h:64
void blendInvAlpha16(int alpha)
Definition: dcolorblend.h:92
void blendAlpha8(int alpha)
Definition: dcolorblend.h:82
void demultiply()
Definition: dcolorblend.h:50
int alpha() const
Definition: dcolor.h:116
void blendAlpha16(int alpha)
Definition: dcolorblend.h:72
void premultiply()
Definition: dcolorblend.h:38
void multiply(float factor)
Definition: dcolorblend.h:202
void blendClamp16()
Definition: dcolorblend.h:156
void premultiply8(int alpha)
Definition: dcolorblend.h:121
int blue() const
Definition: dcolor.h:111
int red() const
Definition: dcolor.h:101
Definition: datefolderview.cpp:43