File: image.c

package info (click to toggle)
effectv 0.3.11-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 748 kB
  • ctags: 1,316
  • sloc: ansic: 12,575; makefile: 216; perl: 38
file content (407 lines) | stat: -rw-r--r-- 7,437 bytes parent folder | download
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 * EffecTV - Realtime Digital Video Effector
 * Copyright (C) 2001-2006 FUKUCHI Kentaro
 *
 * image.c: utilities for image processing.
 *
 */

#include <string.h>
#include <stdlib.h>
#include "EffecTV.h"
#include "utils.h"

RGB32 *stretching_buffer;

static RGB32 *background;
static unsigned char *diff;
static unsigned char *diff2;

/* Initializer is called from utils_init(). */
int image_init(void)
{
	stretching_buffer = (RGB32 *)malloc(video_area * sizeof(RGB32));
	background = (RGB32 *)malloc(video_area * sizeof(RGB32));
	diff = (unsigned char *)malloc(video_area * sizeof(unsigned char));
	diff2 = (unsigned char *)malloc(video_area * sizeof(unsigned char));
	if(stretching_buffer == NULL || background == NULL || diff == NULL || diff2 == NULL) {
		return -1;
	}
	memset(diff2, 0, video_area * sizeof(unsigned char));

	return 0;
}

void image_end(void)
{
	free(stretching_buffer);
	free(background);
	free(diff);
	free(diff2);
}

void image_stretching_buffer_clear(RGB32 color)
{
	int i;
	RGB32 *p;

	p = stretching_buffer;
	for(i=0; i<video_area; i++) {
		*p++ = color;
	}
}

void image_stretch(RGB32 *src, int src_width, int src_height,
                    RGB32 *dest, int dest_width, int dest_height)
{
	int x, y;
	int sx, sy;
	int tx, ty;
	RGB32 *p;

	tx = src_width * 65536 / dest_width;
	ty = src_height * 65536 / dest_height;
	sy = 0;
	for(y=0; y<dest_height; y++) {
		p = src + (sy>>16) * src_width;
		sx = 0;
		for(x=0; x<dest_width; x++) {
			*dest++ = p[(sx>>16)];
			sx += tx;
		}
		sy += ty;
	}
}

static void image_stretch_to_screen_double(void)
{
	int x, y;
	RGB32 *src, *dest1, *dest2;
	int width = video_width;
	int height = video_height;
	int swidth = screen_width;

	src = stretching_buffer;
	dest1 = (RGB32 *)screen_getaddress();
	dest2 = dest1 + swidth;

	for(y=0; y<height; y++) {
		for(x=0; x<width; x++) {
			dest1[x*2] = *src;
			dest1[x*2+1] = *src;
			dest2[x*2] = *src;
			dest2[x*2+1] = *src;
			src++;
		}
		dest1 += swidth*2;
		dest2 += swidth*2;
	}
}

void image_stretch_to_screen(void)
{
	if(screen_scale == 2) {
		image_stretch_to_screen_double();
	} else {
		image_stretch(stretching_buffer, video_width, video_height,
		  (RGB32 *)screen_getaddress(), screen_width, screen_height);
	}
}

/*
 * Collection of background subtraction functions
 */

/* checks only fake-Y value */
/* In these function Y value is treated as R*2+G*4+B. */

static int y_threshold;
void image_set_threshold_y(int threshold)
{
	y_threshold = threshold * 7; /* fake-Y value is timed by 7 */
}

void image_bgset_y(RGB32 *src)
{
	int i;
	int R, G, B;
	RGB32 *p;
	short *q;

	p = src;
	q = (short *)background;
	for(i=0; i<video_area; i++) {
		R = ((*p)&0xff0000)>>(16-1);
		G = ((*p)&0xff00)>>(8-2);
		B = (*p)&0xff;
		*q = (short)(R + G + B);
		p++;
		q++;
	}
}

unsigned char *image_bgsubtract_y(RGB32 *src)
{
	int i;
	int R, G, B;
	RGB32 *p;
	short *q;
	unsigned char *r;
	int v;

	p = src;
	q = (short *)background;
	r = diff;
	for(i=0; i<video_area; i++) {
		R = ((*p)&0xff0000)>>(16-1);
		G = ((*p)&0xff00)>>(8-2);
		B = (*p)&0xff;
		v = (R + G + B) - (int)(*q);
		*r = ((v + y_threshold)>>24) | ((y_threshold - v)>>24);

		p++;
		q++;
		r++;
	}

	return diff;
/* The origin of subtraction function is;
 * diff(src, dest) = (abs(src - dest) > threshold) ? 0xff : 0;
 *
 * This functions is transformed to;
 * (threshold > (src - dest) > -threshold) ? 0 : 0xff;
 *
 * (v + threshold)>>24 is 0xff when v is less than -threshold.
 * (v - threshold)>>24 is 0xff when v is less than threshold.
 * So, ((v + threshold)>>24) | ((threshold - v)>>24) will become 0xff when
 * abs(src - dest) > threshold.
 */
}

/* Background image is refreshed every frame */
unsigned char *image_bgsubtract_update_y(RGB32 *src)
{
	int i;
	int R, G, B;
	RGB32 *p;
	short *q;
	unsigned char *r;
	int v;

	p = src;
	q = (short *)background;
	r = diff;
	for(i=0; i<video_area; i++) {
		R = ((*p)&0xff0000)>>(16-1);
		G = ((*p)&0xff00)>>(8-2);
		B = (*p)&0xff;
		v = (R + G + B) - (int)(*q);
		*q = (short)(R + G + B);
		*r = ((v + y_threshold)>>24) | ((y_threshold - v)>>24);

		p++;
		q++;
		r++;
	}

	return diff;
}

/* checks each RGB value */
static RGB32 rgb_threshold;

/* The range of r, g, b are [0..7] */
void image_set_threshold_RGB(int r, int g, int b)
{
	unsigned char R, G, B;

	R = G = B = 0xff;
	R = R<<r;
	G = G<<g;
	B = B<<b;
	rgb_threshold = (RGB32)(R<<16 | G<<8 | B);
}

void image_bgset_RGB(RGB32 *src)
{
	int i;
	RGB32 *p;

	p = background;
	for(i=0; i<video_area; i++) {
		*p++ = (*src++) & 0xfefefe;
	}
}

unsigned char *image_bgsubtract_RGB(RGB32 *src)
{
	int i;
	RGB32 *p, *q;
	unsigned a, b;
	unsigned char *r;

	p = src;
	q = background;
	r = diff;
	for(i=0; i<video_area; i++) {
		a = (*p++)|0x1010100;
		b = *q++;
		a = a - b;
		b = a & 0x1010100;
		b = b - (b>>8);
		b = b ^ 0xffffff;
		a = a ^ b;
		a = a & rgb_threshold;
		*r++ = (0 - a)>>24;
	}
	return diff;
}

unsigned char *image_bgsubtract_update_RGB(RGB32 *src)
{
	int i;
	RGB32 *p, *q;
	unsigned a, b;
	unsigned char *r;

	p = src;
	q = background;
	r = diff;
	for(i=0; i<video_area; i++) {
		a = *p|0x1010100;
		b = *q&0xfefefe;
		*q++ = *p++;
		a = a - b;
		b = a & 0x1010100;
		b = b - (b>>8);
		b = b ^ 0xffffff;
		a = a ^ b;
		a = a & rgb_threshold;
		*r++ = (0 - a)>>24;
	}
	return diff;
}

/* noise filter for subtracted image. */
unsigned char *image_diff_filter(unsigned char *diff)
{
	int x, y;
	unsigned char *src, *dest;
	unsigned int count;
	unsigned int sum1, sum2, sum3;
	const int width = video_width;

	src = diff;
	dest = diff2 + width +1;
	for(y=1; y<video_height-1; y++) {
		sum1 = src[0] + src[width] + src[width*2];
		sum2 = src[1] + src[width+1] + src[width*2+1];
		src += 2;
		for(x=1; x<width-1; x++) {
			sum3 = src[0] + src[width] + src[width*2];
			count = sum1 + sum2 + sum3;
			sum1 = sum2;
			sum2 = sum3;
			*dest++ = (0xff*3 - count)>>24;
			src++;
		}
		dest += 2;
	}
	
	return diff2;
}

/* Y value filters */
unsigned char *image_y_over(RGB32 *src)
{
	int i;
	int R, G, B, v;
	unsigned char *p = diff;

	for(i = video_area; i>0; i--) {
		R = ((*src)&0xff0000)>>(16-1);
		G = ((*src)&0xff00)>>(8-2);
		B = (*src)&0xff;
		v = y_threshold - (R + G + B);
		*p = (unsigned char)(v>>24);
		src++;
		p++;
	}

	return diff;
}

unsigned char *image_y_under(RGB32 *src)
{
	int i;
	int R, G, B, v;
	unsigned char *p = diff;

	for(i = video_area; i>0; i--) {
		R = ((*src)&0xff0000)>>(16-1);
		G = ((*src)&0xff00)>>(8-2);
		B = (*src)&0xff;
		v = (R + G + B) - y_threshold;
		*p = (unsigned char)(v>>24);
		src++;
		p++;
	}

	return diff;
}

/* tiny edge detection */
unsigned char *image_edge(RGB32 *src)
{
	int x, y;
	unsigned char *p, *q;
	int r, g, b;
	int ar, ag, ab;
	int w;

	p = (unsigned char *)src;
	q = diff2;
	w = video_width * sizeof(RGB32);

	for(y=0; y<video_height - 1; y++) {
		for(x=0; x<video_width - 1; x++) {
			b = p[0];
			g = p[1];
			r = p[2];
			ab = abs(b - p[4]);
			ag = abs(g - p[5]);
			ar = abs(r - p[6]);
			ab += abs(b - p[w]);
			ag += abs(g - p[w+1]);
			ar += abs(r - p[w+2]);
			b = ab+ag+ar;
			if(b > y_threshold) {
				*q = 255;
			} else {
				*q = 0;
			}
			q++;
			p += 4;
		}
		p += 4;
		*q++ = 0;
	}
	memset(q, 0, video_width);

	return diff2;
}

/* horizontal flipping */
void image_hflip(RGB32 *src, RGB32 *dest, int width, int height)
{
	int x, y;

	src += width - 1;
	for(y=0; y<height; y++) {
		for(x=0; x<width; x++) {
			*dest++ = *src--;
		}
		src += width * 2;
	}
}