~ubuntu-branches/ubuntu/saucy/python-imaging/saucy-proposed

« back to all changes in this revision

Viewing changes to libImaging/AlphaComposite.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-31 20:49:20 UTC
  • mfrom: (27.1.1 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20130131204920-b5zshy6vgfvdionl
Tags: 1.1.7+1.7.8-1ubuntu1
Rewrite build dependencies to allow cross builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The Python Imaging Library
 
3
 * $Id$
 
4
 *
 
5
 * Alpha composite imSrc over imDst.
 
6
 * http://en.wikipedia.org/wiki/Alpha_compositing
 
7
 *
 
8
 * See the README file for details on usage and redistribution.
 
9
 */
 
10
 
 
11
 
 
12
#include "Imaging.h"
 
13
 
 
14
 
 
15
Imaging
 
16
ImagingAlphaComposite(Imaging imDst, Imaging imSrc)
 
17
{
 
18
    Imaging imOut;
 
19
    int x, y;
 
20
    float dstR, dstG, dstB, dstA;
 
21
    float srcR, srcG, srcB, srcA;
 
22
    float outR, outG, outB, outA;
 
23
 
 
24
    /* Check arguments */
 
25
    if (!imDst || !imSrc ||
 
26
        strcmp(imDst->mode, "RGBA") ||
 
27
        imDst->type != IMAGING_TYPE_UINT8 ||
 
28
        imDst->bands != 4)
 
29
        return ImagingError_ModeError();
 
30
    if (strcmp(imDst->mode, imSrc->mode) ||
 
31
        imDst->type  != imSrc->type  ||
 
32
        imDst->bands != imSrc->bands ||
 
33
        imDst->xsize != imSrc->xsize ||
 
34
        imDst->ysize != imSrc->ysize)
 
35
        return ImagingError_Mismatch();
 
36
 
 
37
    imOut = ImagingNew(imDst->mode, imDst->xsize, imDst->ysize);
 
38
    if (!imOut)
 
39
        return NULL;
 
40
 
 
41
    ImagingCopyInfo(imOut, imDst);
 
42
 
 
43
    for (y = 0; y < imDst->ysize; y++) {
 
44
 
 
45
        UINT8* dst = (UINT8*) imDst->image[y];
 
46
        UINT8* src = (UINT8*) imSrc->image[y];
 
47
        UINT8* out = (UINT8*) imOut->image[y];
 
48
 
 
49
        for (x = 0; x < imDst->linesize; x += 4) {
 
50
 
 
51
            dstR = dst[x + 0] / 255.0;
 
52
            dstG = dst[x + 1] / 255.0;
 
53
            dstB = dst[x + 2] / 255.0;
 
54
            dstA = dst[x + 3] / 255.0;
 
55
 
 
56
            srcR = src[x + 0] / 255.0;
 
57
            srcG = src[x + 1] / 255.0;
 
58
            srcB = src[x + 2] / 255.0;
 
59
            srcA = src[x + 3] / 255.0;
 
60
 
 
61
            if (dstA == 1.0) {
 
62
                outR = srcR * srcA + dstR * (1.0 - srcA);
 
63
                outG = srcG * srcA + dstG * (1.0 - srcA);
 
64
                outB = srcB * srcA + dstB * (1.0 - srcA);
 
65
                outA = 1.0;
 
66
            } else if (srcA == 0.0) {
 
67
                outR = dstR;
 
68
                outG = dstG;
 
69
                outB = dstB;
 
70
                outA = dstA;
 
71
            } else {
 
72
                outA = srcA + dstA * (1.0 - srcA);
 
73
                if (outA == 0.0) {
 
74
                    outR = 0.0;
 
75
                    outG = 0.0;
 
76
                    outB = 0.0;
 
77
                } else {
 
78
                    outR = (srcR * srcA + dstR * dstA * (1.0 - srcA)) / outA;
 
79
                    outG = (srcG * srcA + dstG * dstA * (1.0 - srcA)) / outA;
 
80
                    outB = (srcB * srcA + dstB * dstA * (1.0 - srcA)) / outA;
 
81
                }
 
82
            }
 
83
 
 
84
            out[x + 0] = (UINT8) (255.0 * outR + 0.5);
 
85
            out[x + 1] = (UINT8) (255.0 * outG + 0.5);
 
86
            out[x + 2] = (UINT8) (255.0 * outB + 0.5);
 
87
            out[x + 3] = (UINT8) (255.0 * outA + 0.5);
 
88
 
 
89
        }
 
90
 
 
91
    }
 
92
 
 
93
    return imOut;
 
94
}