2012-12-04 19:44:26 +04:00
|
|
|
/*
|
|
|
|
* The Python Imaging Library
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Alpha composite imSrc over imDst.
|
2016-02-10 11:37:16 +03:00
|
|
|
* https://en.wikipedia.org/wiki/Alpha_compositing
|
2012-12-04 19:44:26 +04:00
|
|
|
*
|
|
|
|
* See the README file for details on usage and redistribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Imaging.h"
|
|
|
|
|
2017-09-01 08:37:05 +03:00
|
|
|
#define PRECISION_BITS 7
|
2012-12-04 19:44:26 +04:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
typedef struct {
|
2013-03-23 05:44:36 +04:00
|
|
|
UINT8 r;
|
|
|
|
UINT8 g;
|
|
|
|
UINT8 b;
|
|
|
|
UINT8 a;
|
|
|
|
} rgba8;
|
|
|
|
|
2012-12-04 19:44:26 +04:00
|
|
|
Imaging
|
2021-01-03 06:17:51 +03:00
|
|
|
ImagingAlphaComposite(Imaging imDst, Imaging imSrc) {
|
2012-12-04 19:44:26 +04:00
|
|
|
Imaging imOut;
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
/* Check arguments */
|
2021-01-03 06:17:51 +03:00
|
|
|
if (!imDst || !imSrc || strcmp(imDst->mode, "RGBA") ||
|
|
|
|
imDst->type != IMAGING_TYPE_UINT8 || imDst->bands != 4) {
|
2013-03-23 05:44:36 +04:00
|
|
|
return ImagingError_ModeError();
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2013-03-23 05:44:36 +04:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
if (strcmp(imDst->mode, imSrc->mode) || imDst->type != imSrc->type ||
|
|
|
|
imDst->bands != imSrc->bands || imDst->xsize != imSrc->xsize ||
|
2020-05-10 12:56:36 +03:00
|
|
|
imDst->ysize != imSrc->ysize) {
|
2013-03-23 05:44:36 +04:00
|
|
|
return ImagingError_Mismatch();
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2012-12-04 19:44:26 +04:00
|
|
|
|
2017-08-06 13:32:46 +03:00
|
|
|
imOut = ImagingNewDirty(imDst->mode, imDst->xsize, imDst->ysize);
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!imOut) {
|
2013-03-23 05:44:36 +04:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2012-12-04 19:44:26 +04:00
|
|
|
|
|
|
|
for (y = 0; y < imDst->ysize; y++) {
|
2021-01-03 06:17:51 +03:00
|
|
|
rgba8 *dst = (rgba8 *)imDst->image[y];
|
|
|
|
rgba8 *src = (rgba8 *)imSrc->image[y];
|
|
|
|
rgba8 *out = (rgba8 *)imOut->image[y];
|
2013-03-23 05:44:36 +04:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
for (x = 0; x < imDst->xsize; x++) {
|
2013-03-23 14:26:14 +04:00
|
|
|
if (src->a == 0) {
|
2013-03-23 05:44:36 +04:00
|
|
|
// Copy 4 bytes at once.
|
2013-03-23 14:26:14 +04:00
|
|
|
*out = *dst;
|
2013-03-23 05:44:36 +04:00
|
|
|
} else {
|
|
|
|
// Integer implementation with increased precision.
|
|
|
|
// Each variable has extra meaningful bits.
|
|
|
|
// Divisions are rounded.
|
|
|
|
|
2013-03-27 23:18:10 +04:00
|
|
|
UINT32 tmpr, tmpg, tmpb;
|
2017-09-01 08:37:05 +03:00
|
|
|
UINT32 blend = dst->a * (255 - src->a);
|
|
|
|
UINT32 outa255 = src->a * 255 + blend;
|
2013-03-25 00:06:02 +04:00
|
|
|
// There we use 7 bits for precision.
|
|
|
|
// We could use more, but we go beyond 32 bits.
|
2021-01-03 06:17:51 +03:00
|
|
|
UINT32 coef1 = src->a * 255 * 255 * (1 << PRECISION_BITS) / outa255;
|
|
|
|
UINT32 coef2 = 255 * (1 << PRECISION_BITS) - coef1;
|
2017-09-01 08:37:05 +03:00
|
|
|
|
|
|
|
tmpr = src->r * coef1 + dst->r * coef2;
|
|
|
|
tmpg = src->g * coef1 + dst->g * coef2;
|
|
|
|
tmpb = src->b * coef1 + dst->b * coef2;
|
2021-01-03 06:17:51 +03:00
|
|
|
out->r =
|
|
|
|
SHIFTFORDIV255(tmpr + (0x80 << PRECISION_BITS)) >> PRECISION_BITS;
|
|
|
|
out->g =
|
|
|
|
SHIFTFORDIV255(tmpg + (0x80 << PRECISION_BITS)) >> PRECISION_BITS;
|
|
|
|
out->b =
|
|
|
|
SHIFTFORDIV255(tmpb + (0x80 << PRECISION_BITS)) >> PRECISION_BITS;
|
2013-03-25 00:06:02 +04:00
|
|
|
out->a = SHIFTFORDIV255(outa255 + 0x80);
|
2013-03-23 05:44:36 +04:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
dst++;
|
|
|
|
src++;
|
|
|
|
out++;
|
2013-03-23 05:44:36 +04:00
|
|
|
}
|
2012-12-04 19:44:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return imOut;
|
|
|
|
}
|