wmaker: Replaced local 'extern' definition of wPreferences by proper header usage
[wmaker-crm.git] / wrlib / alpha_combine.c
blobb41018630bb3c0dd83559c6778239919b97fcb3b
1 /* alpha_combine.c - Alpha channel combination, based on Gimp 1.1.24
2 * The GIMP -- an image manipulation program
3 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18 * MA 02110-1301, USA.
21 #include "wraster.h"
23 void RCombineAlpha(unsigned char *d, unsigned char *s, int s_has_alpha,
24 int width, int height, int dwi, int swi, int opacity) {
25 int x, y;
26 int t, sa;
27 int alpha;
28 float ratio, cratio;
30 for (y=0; y<height; y++) {
31 for (x=0; x<width; x++) {
32 sa=s_has_alpha?*(s+3):255;
34 if (opacity!=255) {
35 t = sa * opacity + 0x80;
36 sa = ((t>>8)+t)>>8;
39 t = *(d+3) * (255-sa) + 0x80;
40 alpha = sa + (((t>>8)+t)>>8);
42 if (sa==0 || alpha==0) {
43 ratio = 0;
44 cratio = 1.0;
45 } else if(sa == alpha) {
46 ratio = 1.0;
47 cratio = 0;
48 } else {
49 ratio = (float)sa / alpha;
50 cratio = 1.0 - ratio;
53 *d = (int)*d * cratio + (int)*s * ratio;
54 s++; d++;
55 *d = (int)*d * cratio + (int)*s * ratio;
56 s++; d++;
57 *d = (int)*d * cratio + (int)*s * ratio;
58 s++; d++;
59 *d = alpha;
60 d++;
62 if (s_has_alpha) s++;
64 d+=dwi;
65 s+=swi;