aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Pillow/py3/libImaging/Blend.c
blob: a53ae0fad53ead3dab73bb9640707897ce37d8c9 (plain) (blame)
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
/*
 * The Python Imaging Library
 * $Id$
 *
 * interpolate between two existing images
 *
 * history:
 * 96-03-20 fl Created
 * 96-05-18 fl Simplified blend expression
 * 96-10-05 fl Fixed expression bug, special case for interpolation
 *
 * Copyright (c) Fredrik Lundh 1996.
 * Copyright (c) Secret Labs AB 1997.
 *
 * See the README file for details on usage and redistribution.
 */

#include "Imaging.h"

Imaging
ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha) {
    Imaging imOut;
    int x, y;

    /* Check arguments */
    if (!imIn1 || !imIn2 || imIn1->type != IMAGING_TYPE_UINT8 || imIn1->palette ||
        strcmp(imIn1->mode, "1") == 0 || imIn2->palette ||
        strcmp(imIn2->mode, "1") == 0) {
        return ImagingError_ModeError();
    }

    if (imIn1->type != imIn2->type || imIn1->bands != imIn2->bands ||
        imIn1->xsize != imIn2->xsize || imIn1->ysize != imIn2->ysize) {
        return ImagingError_Mismatch();
    }

    /* Shortcuts */
    if (alpha == 0.0) {
        return ImagingCopy(imIn1);
    } else if (alpha == 1.0) {
        return ImagingCopy(imIn2);
    }

    imOut = ImagingNewDirty(imIn1->mode, imIn1->xsize, imIn1->ysize);
    if (!imOut) {
        return NULL;
    }

    if (alpha >= 0 && alpha <= 1.0) {
        /* Interpolate between bands */
        for (y = 0; y < imIn1->ysize; y++) {
            UINT8 *in1 = (UINT8 *)imIn1->image[y];
            UINT8 *in2 = (UINT8 *)imIn2->image[y];
            UINT8 *out = (UINT8 *)imOut->image[y];
            for (x = 0; x < imIn1->linesize; x++) {
                out[x] = (UINT8)((int)in1[x] + alpha * ((int)in2[x] - (int)in1[x]));
            }
        }
    } else {
        /* Extrapolation; must make sure to clip resulting values */
        for (y = 0; y < imIn1->ysize; y++) {
            UINT8 *in1 = (UINT8 *)imIn1->image[y];
            UINT8 *in2 = (UINT8 *)imIn2->image[y];
            UINT8 *out = (UINT8 *)imOut->image[y];
            for (x = 0; x < imIn1->linesize; x++) {
                float temp = (float)((int)in1[x] + alpha * ((int)in2[x] - (int)in1[x]));
                if (temp <= 0.0) {
                    out[x] = 0;
                } else if (temp >= 255.0) {
                    out[x] = 255;
                } else {
                    out[x] = (UINT8)temp;
                }
            }
        }
    }

    return imOut;
}