File: alpha_combine.c

package info (click to toggle)
wmaker 0.96.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,416 kB
  • sloc: ansic: 99,483; sh: 7,068; perl: 3,423; makefile: 1,647; lisp: 219; python: 34
file content (71 lines) | stat: -rw-r--r-- 1,869 bytes parent folder | download | duplicates (2)
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
/* alpha_combine.c - Alpha channel combination, based on Gimp 1.1.24
 * The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 *  MA 02110-1301, USA.
 */

#include "config.h"

#include "wraster.h"
#include "wr_i18n.h"


void RCombineAlpha(unsigned char *d, unsigned char *s, int s_has_alpha,
		   int width, int height, int dwi, int swi, int opacity) {
	int x, y;
	int t, sa;
	int alpha;
	float ratio, cratio;

	for (y=0; y<height; y++) {
		for (x=0; x<width; x++) {
			sa=s_has_alpha?*(s+3):255;

			if (opacity!=255) {
				t = sa * opacity + 0x80;
				sa = ((t>>8)+t)>>8;
			}

			t = *(d+3) * (255-sa) + 0x80;
			alpha = sa + (((t>>8)+t)>>8);

			if (sa==0 || alpha==0) {
				ratio = 0;
				cratio = 1.0;
			} else if(sa == alpha) {
				ratio = 1.0;
				cratio = 0;
			} else {
				ratio = (float)sa / alpha;
				cratio = 1.0F - ratio;
			}

			*d = (int)*d * cratio + (int)*s * ratio;
			s++; d++;
			*d = (int)*d * cratio + (int)*s * ratio;
			s++; d++;
			*d = (int)*d * cratio + (int)*s * ratio;
			s++; d++;
			*d = alpha;
			d++;

			if (s_has_alpha) s++;
		}
		d+=dwi;
		s+=swi;
	}
}