aboutsummaryrefslogtreecommitdiff
path: root/Plugins/rene.c
blob: 2885f47dd1194955961915fea774128beddaddc9 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// 242.rene -- does 3-source chroma keying with transparency.
//		by r. luke dubois (luke@music.columbia.edu),
//			computer music center, columbia university, 2001.
//  for rene beekman.
//
//  Pd / Framestein port by Olaf Matthes <olaf.matthes@gmx.de>, June 2002
//
//	objects and source are provided without warranty of any kind, express or implied.
//
//  usage: rene <red> <green> <blue> <fuzzi red> <fuzzi green> <fuzzi blue> <redfloor> <greenfloor> <bluefloor>
//
//         f1 is the key & target, f2 is the mask
//

#include <stdio.h>
#include <string.h>
#include "plugin.h"

INFO("242.rene -- does 3-source chroma keying with transparency")

void perform_effect(struct frame f, struct args a)
{
	printf("Using keyscreen as effect does nothing!\n");
}

void perform_copy(struct frame f1, struct frame f2, struct args a)
{
	short x, y, w, h;
	pixel16 *pix1_16, *pix2_16;
	pixel24 *pix1_24, *pix2_24;
	pixel32 *pix1_32, *pix2_32;
	byte redpix, greenpix, bluepix, rkeyed, gkeyed, bkeyed;
	byte red, green, blue, check, rf, gf, bf, rfl, gfl, bfl;
	float lum, keylum, lowfuzz, highfuzz, lowfloor, highfloor, coeff;
	char *t;

	w = f1.width<f2.width ? f1.width : f2.width;
	h = f1.height<f2.height ? f1.height : f2.height;

	// get params
	if(!a.s) return;
	red = atoi(a.s);
	if(!(t = strstr(a.s, " "))) return;
	green = atoi(t+1);
	if(!(t = strstr(t+1, " "))) return;
	blue = atoi(t+1);
	if(!(t = strstr(t+1, " "))) return;
	rf = atoi(t+1);
	if(!(t = strstr(t+1, " "))) return;
	gf = atoi(t+1);
	if(!(t = strstr(t+1, " "))) return;
	bf = atoi(t+1);
	if(!(t = strstr(t+1, " "))) return;
	rfl = atoi(t+1);
	if(!(t = strstr(t+1, " "))) return;
	gfl = atoi(t+1);
	if(!(t = strstr(t+1, " "))) return;
	bfl = atoi(t+1);

	printf("rene: r%d g%d b%d rg%d gf%d bf%d rfl%d gfl%d bfl%d\n", 
		    red, green, blue, rf, gf, bf, rfl, gfl, bfl);

	// key all channels simultaneously
	switch(f1.pixelformat)
	{
		case 16:
			for(y = 0; y < h; y++) 
			{
				pix1_16 = scanline16(f1, y);
				pix2_16 = scanline16(f2, y);
				for(x = 0; x < w; x++)
				{
					check = 0; // start with each pixel anew

					redpix = r16(pix2_16[x]);
					greenpix = g16(pix2_16[x]);
					bluepix = b16(pix2_16[x]);
					if ((redpix>=(red-rf))&&(redpix<=(red+rf))&&(greenpix>=(green-gf))&&(greenpix<=(green+gf))&&(bluepix>=(blue-bf))&&(bluepix<=(blue+bf)))
					{
                			check = 1; // mask it
					}        
					if ((redpix<(red-rfl))||(redpix>(red+rfl))&&(greenpix<(green-gfl))||(greenpix>(green+gfl))&&(bluepix<(blue-bfl))||(bluepix>(blue+bfl)))
					{
                			check = 2; // target it
					}        

					if (!check)
					{
						lum = (redpix + greenpix + bluepix) / 3.0;
						keylum = (red + blue + green) / 3.0;
						lowfuzz = (rf + gf + bf) / 3.0;
						if (lowfuzz < 0.0) lowfuzz = 0.0;
						highfuzz = (rf + gf + bf) / 3.0;
						if (highfuzz > 1.0) highfuzz = 1.0;
						lowfloor = keylum - ((rfl + gfl + bfl) / 3.0);
						if (lowfloor < 0.0) lowfloor = 0.;
						highfloor = keylum + (rfl + gfl + bfl) / 3.0;
						if (highfloor > 1.0) highfloor = 1.0;
						if (lum < keylum)
						{
							coeff = ((lum - lowfloor) / (keylum - lowfuzz));
						}
						if (lum >= keylum)
						{
							coeff = ((255 - lum - highfloor) / (255 - keylum - highfuzz));
						}

						rkeyed = ((float)r16(pix2_16[x]) * coeff) + ((float)r16(pix1_16[x]) * (1.0 - coeff));
						gkeyed = ((float)g16(pix2_16[x]) * coeff) + ((float)g16(pix1_16[x]) * (1.0 - coeff));
						bkeyed = ((float)b16(pix2_16[x]) * coeff) + ((float)b16(pix1_16[x]) * (1.0 - coeff));	
								
						pix2_16[x] = rgbtocolor16(rkeyed, gkeyed, bkeyed);
					}

					if (check==1)
					{	// was pix3
						// pix2_16[x] = rgbtocolor16(r16(pix2_16[x]), g16(pix2_16[x]), b16(pix2_16[x]));
					}
					if (check==2)
					{	// was pix2
						pix2_16[x] = rgbtocolor16(r16(pix1_16[x]), g16(pix1_16[x]), b16(pix1_16[x]));
					}
				}
			}
			break;
		case 24:
			for(y = 0; y < h; y++) 
			{
				pix1_24 = scanline24(f1, y);
				pix2_24 = scanline24(f2, y);
				for(x = 0; x < w; x++)
				{
					check = 0; // start with each pixel anew

					redpix = r24(pix2_24[x]);
					greenpix = g24(pix2_24[x]);
					bluepix = b24(pix2_24[x]);
					if ((redpix>=(red-rf))&&(redpix<=(red+rf))&&(greenpix>=(green-gf))&&(greenpix<=(green+gf))&&(bluepix>=(blue-bf))&&(bluepix<=(blue+bf)))
					{
                			check = 1; // mask it
					}        
					if ((redpix<(red-rfl))||(redpix>(red+rfl))&&(greenpix<(green-gfl))||(greenpix>(green+gfl))&&(bluepix<(blue-bfl))||(bluepix>(blue+bfl)))
					{
                			check = 2; // target it
					}        

					if (!check)
					{
						lum = (redpix + greenpix + bluepix) / 3.0;
						keylum = (red + blue + green) / 3.0;
						lowfuzz = (rf + gf + bf) / 3.0;
						if (lowfuzz < 0.0) lowfuzz = 0.0;
						highfuzz = (rf + gf + bf) / 3.0;
						if (highfuzz > 1.0) highfuzz = 1.0;
						lowfloor = keylum - ((rfl + gfl + bfl) / 3.0);
						if (lowfloor < 0.0) lowfloor = 0.;
						highfloor = keylum + (rfl + gfl + bfl) / 3.0;
						if (highfloor > 1.0) highfloor = 1.0;
						if (lum < keylum)
						{
							coeff = ((lum - lowfloor) / (keylum - lowfuzz));
						}
						if (lum >= keylum)
						{
							coeff = ((255 - lum - highfloor) / (255 - keylum - highfuzz));
						}

						rkeyed = ((float)r24(pix2_24[x]) * coeff) + ((float)r24(pix1_24[x]) * (1.0 - coeff));
						gkeyed = ((float)g24(pix2_24[x]) * coeff) + ((float)g24(pix1_24[x]) * (1.0 - coeff));
						bkeyed = ((float)b24(pix2_24[x]) * coeff) + ((float)b24(pix1_24[x]) * (1.0 - coeff));	
								
						pix2_24[x] = rgbtocolor24(rkeyed, gkeyed, bkeyed);
					}

					if (check==1)
					{	// was pix3
						// pix2_24[x] = rgbtocolor24(r24(pix2_24[x]), g24(pix2_24[x]), b24(pix2_24[x]));
					}
					if (check==2)
					{	// was pix2
						pix2_24[x] = rgbtocolor24(r24(pix1_24[x]), g24(pix1_24[x]), b24(pix1_24[x]));
					}
				}
			}
			break;
		case 32:
			for(y = 0; y < h; y++) 
			{
				pix1_32 = scanline32(f1, y);
				pix2_32 = scanline32(f2, y);
				for(x = 0; x < w; x++)
				{
					check = 0; // start with each pixel anew

					redpix = r32(pix2_32[x]);
					greenpix = g32(pix2_32[x]);
					bluepix = b32(pix2_32[x]);
					if ((redpix>=(red-rf))&&(redpix<=(red+rf))&&(greenpix>=(green-gf))&&(greenpix<=(green+gf))&&(bluepix>=(blue-bf))&&(bluepix<=(blue+bf)))
					{
                			check = 1; // mask it
					}        
					if ((redpix<(red-rfl))||(redpix>(red+rfl))&&(greenpix<(green-gfl))||(greenpix>(green+gfl))&&(bluepix<(blue-bfl))||(bluepix>(blue+bfl)))
					{
                			check = 2; // target it
					}        

					if (!check)
					{
						lum = (redpix + greenpix + bluepix) / 3.0;
						keylum = (red + blue + green) / 3.0;
						lowfuzz = (rf + gf + bf) / 3.0;
						if (lowfuzz < 0.0) lowfuzz = 0.0;
						highfuzz = (rf + gf + bf) / 3.0;
						if (highfuzz > 1.0) highfuzz = 1.0;
						lowfloor = keylum - ((rfl + gfl + bfl) / 3.0);
						if (lowfloor < 0.0) lowfloor = 0.;
						highfloor = keylum + (rfl + gfl + bfl) / 3.0;
						if (highfloor > 1.0) highfloor = 1.0;
						if (lum < keylum)
						{
							coeff = ((lum - lowfloor) / (keylum - lowfuzz));
						}
						if (lum >= keylum)
						{
							coeff = ((255 - lum - highfloor) / (255 - keylum - highfuzz));
						}

						rkeyed = ((float)r32(pix2_32[x]) * coeff) + ((float)r32(pix1_32[x]) * (1.0 - coeff));
						gkeyed = ((float)g32(pix2_32[x]) * coeff) + ((float)g32(pix1_32[x]) * (1.0 - coeff));
						bkeyed = ((float)b32(pix2_32[x]) * coeff) + ((float)b32(pix1_32[x]) * (1.0 - coeff));	
								
						pix2_32[x] = rgbtocolor32(rkeyed, gkeyed, bkeyed);
					}

					if (check==1)
					{	// was pix3
						// pix2_32[x] = rgbtocolor32(r32(pix2_32[x]), g32(pix2_32[x]), b32(pix2_32[x]));
					}
					if (check==2)
					{	// was pix2
						pix2_32[x] = rgbtocolor32(r32(pix1_32[x]), g32(pix1_32[x]), b32(pix1_32[x]));
					}
				}
			}
			break;
	}
}