summaryrefslogtreecommitdiffstats
path: root/digikam/libs/dimg/dcolorcomposer.cpp
blob: 653dbab330207681a752e7618b42f2c9d6fe7ea8 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2006-03-02
 * Description : DColor methods for composing
 *
 * 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.
 *
 * ============================================================ */

// Integer arithmetic inspired by DirectFB,
// src/gfx/generic/generic.c and src/display/idirectfbsurface.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>.

*/

// C++ includes.

#include <cmath>

// Local includes.

#include "dcolorcomposer.h"

namespace Digikam
{

class DColorComposerPorterDuffNone : public DColorComposer
{
public:
    virtual void compose(DColor &dest, DColor src);
};

class DColorComposerPorterDuffClear : public DColorComposer
{
public:
    virtual void compose(DColor &dest, DColor src);
    virtual void compose(DColor &dest, DColor src, MultiplicationFlags multiplicationFlags);
};

class DColorComposerPorterDuffSrc : public DColorComposer
{
public:
    virtual void compose(DColor &dest, DColor src);
    virtual void compose(DColor &dest, DColor src, MultiplicationFlags multiplicationFlags);
};

class DColorComposerPorterDuffSrcOver : public DColorComposer
{
public:
    virtual void compose(DColor &dest, DColor src);
};

class DColorComposerPorterDuffDstOver : public DColorComposer
{
public:
    virtual void compose(DColor &dest, DColor src);
};

class DColorComposerPorterDuffSrcIn : public DColorComposer
{
public:
    virtual void compose(DColor &dest, DColor src);
};

class DColorComposerPorterDuffDstIn : public DColorComposer
{
public:
    virtual void compose(DColor &dest, DColor src);
};

class DColorComposerPorterDuffSrcOut : public DColorComposer
{
public:
    virtual void compose(DColor &dest, DColor src);
};

class DColorComposerPorterDuffDstOut : public DColorComposer
{
public:
    virtual void compose(DColor &dest, DColor src);
};

class DColorComposerPorterDuffSrcAtop : public DColorComposer
{
    public:
        virtual void compose(DColor &dest, DColor src);
};

class DColorComposerPorterDuffDstAtop : public DColorComposer
{
    public:
        virtual void compose(DColor &dest, DColor src);
};

class DColorComposerPorterDuffXor : public DColorComposer
{
    public:
        virtual void compose(DColor &dest, DColor src);
};

// Porter-Duff None
// component = (source * sa + destination * (1-sa))
// Src blending function Src Alpha
// Dst blending function Inv Src Alpha
void DColorComposerPorterDuffNone::compose(DColor &dest, DColor src)
{
    // preserve src alpha value for dest blending,
    // src.alpha() will be changed after blending src
    int sa = src.alpha();
    if (dest.sixteenBit())
    {
        src.blendAlpha16(sa);
        dest.blendInvAlpha16(sa);
        dest.blendAdd(src);
        dest.blendClamp16();
    }
    else
    {
        src.blendAlpha8(sa);
        dest.blendInvAlpha8(sa);
        dest.blendAdd(src);
        dest.blendClamp8();
    }
}

// Porter-Duff Clear
// component = (source * 0 + destination * 0)
// Src blending function Zero
// Dst blending function Zero
void DColorComposerPorterDuffClear::compose(DColor &dest, DColor src)
{
    src.blendZero();
    dest.blendZero();
    dest.blendAdd(src);
}

void DColorComposerPorterDuffClear::compose(DColor &dest, DColor src, MultiplicationFlags)
{
    // skip pre- and demultiplication
    compose(dest, src);
}

// Porter-Duff Src
// Normal Painter's algorithm
// component = (source * 1 + destination * 0)
// Src blending function One
// Dst blending function Zero
void DColorComposerPorterDuffSrc::compose(DColor &dest, DColor src)
{
    // src: no-op
    dest.blendZero();
    dest.blendAdd(src);
}

void DColorComposerPorterDuffSrc::compose(DColor &dest, DColor src, MultiplicationFlags)
{
    // skip pre- and demultiplication
    compose(dest, src);
}

// Porter-Duff Src Over
// component = (source * 1 + destination * (1-sa))
// Src blending function One
// Dst blending function Inv Src Alpha
void DColorComposerPorterDuffSrcOver::compose(DColor &dest, DColor src)
{
    if (dest.sixteenBit())
    {
        // src: no-op
        dest.blendInvAlpha16(src.alpha());
        dest.blendAdd(src);
        dest.blendClamp16();
    }
    else
    {
        // src: no-op
        dest.blendInvAlpha8(src.alpha());
        dest.blendAdd(src);
        dest.blendClamp8();
    }
}

// Porter-Duff Dst over
// component = (source * (1.0-da) + destination * 1)
// Src blending function Inv Dst Alpha
// Dst blending function One
void DColorComposerPorterDuffDstOver::compose(DColor &dest, DColor src)
{
    if (dest.sixteenBit())
    {
        src.blendInvAlpha16(dest.alpha());
        // dest: no-op
        dest.blendAdd(src);
        dest.blendClamp16();
    }
    else
    {
        src.blendInvAlpha8(dest.alpha());
        // dest: no-op
        dest.blendAdd(src);
        dest.blendClamp8();
    }
}

// Porter-Duff Src In
// component = (source * da + destination * 0)
// Src blending function Dst Alpha
// Dst blending function Zero
void DColorComposerPorterDuffSrcIn::compose(DColor &dest, DColor src)
{
    if (dest.sixteenBit())
    {
        src.blendAlpha16(dest.alpha());
        dest.blendZero();
        dest.blendAdd(src);
        dest.blendClamp16();
    }
    else
    {
        src.blendAlpha8(dest.alpha());
        dest.blendZero();
        dest.blendAdd(src);
        dest.blendClamp8();
    }
}

// Porter-Duff Dst In
// component = (source * 0 + destination * sa)
// Src blending function Zero
// Dst blending function Src Alpha
void DColorComposerPorterDuffDstIn::compose(DColor &dest, DColor src)
{
    int sa = src.alpha();
    if (dest.sixteenBit())
    {
        src.blendZero();
        dest.blendAlpha16(sa);
        dest.blendAdd(src);
        dest.blendClamp16();
    }
    else
    {
        src.blendZero();
        dest.blendAlpha8(sa);
        dest.blendAdd(src);
        dest.blendClamp8();
    }
}

// Porter-Duff Src Out
// component = (source * (1-da) + destination * 0)
// Src blending function Inv Dst Alpha
// Dst blending function Zero
void DColorComposerPorterDuffSrcOut::compose(DColor &dest, DColor src)
{
    if (dest.sixteenBit())
    {
        src.blendInvAlpha16(dest.alpha());
        dest.blendZero();
        dest.blendAdd(src);
        dest.blendClamp16();
    }
    else
    {
        src.blendInvAlpha8(dest.alpha());
        dest.blendZero();
        dest.blendAdd(src);
        dest.blendClamp8();
    }
}

// Porter-Duff Dst Out
// component = (source * 0 + destination * (1-sa))
// Src blending function Zero
// Dst blending function Inv Src Alpha
void DColorComposerPorterDuffDstOut::compose(DColor &dest, DColor src)
{
    int sa = src.alpha();
    if (dest.sixteenBit())
    {
        src.blendZero();
        dest.blendInvAlpha16(sa);
        dest.blendAdd(src);
        dest.blendClamp16();
    }
    else
    {
        src.blendZero();
        dest.blendInvAlpha8(sa);
        dest.blendAdd(src);
        dest.blendClamp8();
    }
}

// Porter-Duff Src Atop
// component = (source * da + destination * (1-sa))
// Src blending function Dst Alpha
// Dst blending function Inv Src Alpha
void DColorComposerPorterDuffSrcAtop::compose(DColor &dest, DColor src)
{
    int sa = src.alpha();
    if (dest.sixteenBit())
    {
        src.blendAlpha16(dest.alpha());
        dest.blendInvAlpha16(sa);
        dest.blendAdd(src);
        dest.blendClamp16();
    }
    else
    {
        src.blendAlpha8(dest.alpha());
        dest.blendInvAlpha8(sa);
        dest.blendAdd(src);
        dest.blendClamp8();
    }
}

// Porter-Duff Dst Atop
// component = (source * (1-da) + destination * sa)
// Src blending function Inv Dest Alpha
// Dst blending function Src Alpha
void DColorComposerPorterDuffDstAtop::compose(DColor &dest, DColor src)
{
    int sa = src.alpha();
    if (dest.sixteenBit())
    {
        src.blendInvAlpha16(dest.alpha());
        dest.blendAlpha16(sa);
        dest.blendAdd(src);
        dest.blendClamp16();
    }
    else
    {
        src.blendInvAlpha8(dest.alpha());
        dest.blendInvAlpha8(sa);
        dest.blendAdd(src);
        dest.blendClamp8();
    }
}

// Porter-Duff Xor
// component = (source * (1-da) + destination * (1-sa))
// Src blending function Inv Dst Alpha
// Dst blending function Inv Src Alpha
void DColorComposerPorterDuffXor::compose(DColor &dest, DColor src)
{
    int sa = src.alpha();
    if (dest.sixteenBit())
    {
        src.blendInvAlpha16(dest.alpha());
        dest.blendInvAlpha16(sa);
        dest.blendAdd(src);
        dest.blendClamp16();
    }
    else
    {
        src.blendInvAlpha8(dest.alpha());
        dest.blendInvAlpha8(sa);
        dest.blendAdd(src);
        dest.blendClamp8();
    }
}

// -----------------------------------------------------------------------

void DColorComposer::compose(DColor &dest, DColor src, DColorComposer::MultiplicationFlags multiplicationFlags)
{
    if (multiplicationFlags & PremultiplySrc)
        src.premultiply();
    if (multiplicationFlags & PremultiplyDst)
        dest.premultiply();

    compose(dest, src);

    if (multiplicationFlags & DemultiplyDst)
        dest.demultiply();
}

DColorComposer *DColorComposer::getComposer(DColorComposer::CompositingOperation rule)
{
    switch(rule)
    {
        case PorterDuffNone:
            return new DColorComposerPorterDuffNone;
        case PorterDuffClear:
            return new DColorComposerPorterDuffClear;
        case PorterDuffSrc:
            return new DColorComposerPorterDuffSrc;
        case PorterDuffSrcOver:
            return new DColorComposerPorterDuffSrcOver;
        case PorterDuffDstOver:
            return new DColorComposerPorterDuffDstOver;
        case PorterDuffSrcIn:
            return new DColorComposerPorterDuffSrcIn;
        case PorterDuffDstIn:
            return new DColorComposerPorterDuffDstIn;
        case PorterDuffSrcOut:
            return new DColorComposerPorterDuffSrcOut;
        case PorterDuffDstOut:
            return new DColorComposerPorterDuffDstOut;
        case PorterDuffSrcAtop:
            return new DColorComposerPorterDuffDstOut;
        case PorterDuffDstAtop:
            return new DColorComposerPorterDuffDstOut;
        case PorterDuffXor:
            return new DColorComposerPorterDuffDstOut;
    }
    return 0;
}

}  // namespace DigiKam