Debian Codegraph @

plugins/actor/bumpscope/bump_scope.c

raw
1 /* Libvisual-plugins - Standard plugins for libvisual
2 *
3 * Copyright (C) 1999, 2001 Zinx Verituse <[email protected]>
4 *
5 * Authors: Zinx Verituse <[email protected]>
6 * Dennis Smit <[email protected]>
7 *
8 * Notes: Bumpscope plugin originally from XMMS.
9 *
10 * $Id: bump_scope.c,v 1.18 2006/02/05 18:47:26 synap Exp $
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <math.h>
30
31 #include "bump_scope.h"
32
33 #define min(x,y) ((x)<(y)?(x):(y))
34
35 #define PI 3.14159265358979323846
36
37 static void bumpscope_blur_8 (uint8_t *ptr, int w, int h, int bpl);
38 static void bumpscope_generate_intense (BumpscopePrivate *priv);
39 static void bumpscope_translate (BumpscopePrivate *priv, int x, int y, int *xo, int *yo, int *xd, int *yd, int *angle);
40 static void bumpscope_draw (BumpscopePrivate *priv);
41 static inline void draw_vert_line(uint8_t *buffer, int x, int y1, int y2, int pitch);
42 static void bumpscope_render_light (BumpscopePrivate *priv, int lx, int ly);
43
44 static void bumpscope_blur_8 (uint8_t *ptr, int w, int h, int bpl)
45 {
46 register unsigned int i,sum = 0;
47 register uint8_t *iptr;
48
49 iptr = ptr + bpl + 1;
50 i = bpl * h;
51
52 if (visual_cpu_get_mmx ()) {
53 #if defined(VISUAL_ARCH_X86) || defined(VISUAL_ARCH_X86_64)
54
55 __asm __volatile
56 ("pxor %%mm6, %%mm6"
57 ::);
58
59 while(i -= 4)
60 {
61 __asm __volatile
62 ("\n\t movd %[dest], %%mm0"
63 "\n\t movd %[pix2], %%mm1"
64 "\n\t punpcklbw %%mm6, %%mm0"
65 "\n\t movd %[pix3], %%mm2"
66 "\n\t punpcklbw %%mm6, %%mm1"
67 "\n\t movd %[pix4], %%mm3"
68 "\n\t punpcklbw %%mm6, %%mm2"
69 "\n\t movd %[pix5], %%mm4"
70 "\n\t punpcklbw %%mm6, %%mm3"
71 "\n\t paddw %%mm1, %%mm2"
72 "\n\t punpcklbw %%mm6, %%mm4"
73 "\n\t paddw %%mm3, %%mm2"
74 "\n\t paddw %%mm2, %%mm4"
75 "\n\t psrlw $2, %%mm4"
76 "\n\t packuswb %%mm6, %%mm4"
77 "\n\t movd %%mm4, %[dest]"
78 :: [dest] "m" (*(iptr))
79 , [pix2] "m" (*(iptr - bpl))
80 , [pix3] "m" (*(iptr - 1))
81 , [pix4] "m" (*(iptr + 1))
82 , [pix5] "m" (*(iptr + bpl)));
83
84 iptr += 4;
85 }
86
87 __asm __volatile
88 ("\n\t emms");
89 #endif
90 } else {
91 while(i--)
92 {
93 sum = (iptr[-bpl] + iptr[-1] + iptr[1] + iptr[bpl]) >> 2;
94 *(iptr++) = sum;
95 }
96 }
97 }
98
99 static void bumpscope_generate_intense (BumpscopePrivate *priv)
100 {
101 int32_t i;
102
103 for(i = 255; i > 0; i--) {
104 priv->intense1[i] = cos(((float)(255-i)*PI)/512.0);
105 priv->intense2[i] = pow(priv->intense1[i], 250)*150;
106 }
107
108 priv->intense1[0] = priv->intense1[1];
109 priv->intense2[0] = priv->intense2[1];
110 }
111
112 static void bumpscope_translate (BumpscopePrivate *priv, int x, int y, int *xo, int *yo, int *xd, int *yd, int *angle)
113 {
114 /* try setting y to both maxes */
115 *yo = priv->height/2;
116 *angle = asin((float)(y-(priv->height/2))/(float)*yo)/(PI/180.0);
117 *xo = (x-(priv->width/2))/cos(*angle*(PI/180.0));
118
119 if (*xo>=-(priv->width/2) && *xo<=(priv->width/2)) {
120 *xd = (*xo>0)?-1:1;
121 *yd = 0;
122 return;
123 }
124
125 *yo = -*yo;
126 *angle = asin((float)(y-(priv->height/2))/(float)*yo)/(PI/180.0);
127 *xo = (x-(priv->width/2))/cos(*angle*(PI/180.0));
128
129 if (*xo>=-(priv->width/2) && *xo<=(priv->width/2)) {
130 *xd = (*xo>0)?-1:1;
131 *yd = 0;
132 return;
133 }
134
135 /* try setting x to both maxes */
136 *xo = priv->width/2;
137 *angle = acos((float)(x-(priv->width/2))/(float)*xo)/(PI/180.0);
138 *yo = (y-(priv->height/2))/sin(*angle*(PI/180.0));
139
140 if (*yo>=-(priv->height/2) && *yo<=(priv->height/2)) {
141 *yd = (*yo>0)?-1:1;
142 *xd = 0;
143 return;
144 }
145
146 *xo = -*xo;
147 *angle = acos((float)(x-(priv->width/2))/(float)*xo)/(PI/180.0);
148 *yo = (y-(priv->height/2))/sin(*angle*(PI/180.0));
149
150 /* if this isn't right, it's out of our range and we don't care */
151 *yd = (*yo>0)?-1:1;
152 *xd = 0;
153 }
154
155 static void bumpscope_draw (BumpscopePrivate *priv)
156 {
157 /* FIXME remove the statics */
158 static int angle, xo, yo, xd, yd, lx, ly;
159 static int was_moving = 0, was_color = 0;
160 static float sd = 0;
161 static float h, s, v;
162 static int hd = 0;
163
164 lx = priv->light_x;
165 ly = priv->light_y;
166 if (priv->moving_light) {
167 if (!was_moving) {
168 bumpscope_translate (priv, lx, ly, &xo, &yo, &xd, &yd, &angle);
169 was_moving = 1;
170 }
171
172 lx = priv->width/2+cos(angle*(PI/180.0))*xo;
173 ly = priv->height/2+sin(angle*(PI/180.0))*yo;
174
175 angle += 2; if (angle >= 360) angle = 0;
176
177 xo += xd;
178 if ((int)xo > ((int)priv->width/(int)2) || (int)xo < -((int)priv->width/(int)2)) {
179 xo = (xo>0)?(priv->width/2):-(priv->width/2);
180 if (visual_random_context_int(priv->rcontext)&1) {
181 xd = (xd>0)?-1:1;
182 yd = 0;
183 } else {
184 yd = (yd>0)?-1:1;
185 xd = 0;
186 }
187 }
188
189 yo += yd;
190 if ((int)yo > ((int)priv->height/(int)2) || (int)yo < -((int)priv->height/(int)2)) {
191 yo = (yo>0)?(priv->height/2):-(priv->height/2);
192 if (visual_random_context_int(priv->rcontext)&1) {
193 xd = (xd>0)?-1:1;
194 yd = 0;
195 } else {
196 yd = (yd>0)?-1:1;
197 xd = 0;
198 }
199 }
200 } else {
201 if (was_moving) {
202 was_moving = 0;
203 }
204 }
205
206 if (priv->color_cycle) {
207 if (!was_color) {
208 visual_color_to_hsv (&priv->color, &h, &s, &v);
209
210 was_color = 1;
211
212 if (visual_random_context_int(priv->rcontext)&1) {
213 hd = (visual_random_context_int(priv->rcontext)&1)*2-1;
214 sd = 0;
215 } else {
216 sd = 0.01 * ((visual_random_context_int(priv->rcontext)&1)*2-1);
217 hd = 0;
218 }
219 }
220
221 visual_color_from_hsv (&priv->color, h, s, v);
222 __bumpscope_generate_palette (priv, &priv->color);
223
224 if (hd) {
225 h += hd;
226 if (h >= 360) h = 0;
227 if (h < 0) h = 359;
228 if ((visual_random_context_int(priv->rcontext)%150) == 0) {
229 if (visual_random_context_int(priv->rcontext)&1) {
230 hd = (visual_random_context_int(priv->rcontext)&1)*2-1;
231 sd = 0;
232 } else {
233 sd = 0.01 * ((visual_random_context_int(priv->rcontext)&1)*2-1);
234 hd = 0;
235 }
236 }
237 } else {
238 s += sd;
239
240 if (s <= 0 || s >= 0.5) {
241 if (s < 0) s = 0;
242
243 if (s > 0.52) {
244 sd = -0.01;
245 } else if (s == 0) {
246 h = visual_random_context_int(priv->rcontext)%360;
247 sd = 0.01;
248 } else {
249 if (visual_random_context_int(priv->rcontext)&1) {
250 hd = (visual_random_context_int(priv->rcontext)&1)*2-1;
251 sd = 0;
252 } else {
253 sd = 0.01 * ((visual_random_context_int(priv->rcontext)&1)*2-1);
254 hd = 0;
255 }
256 }
257 }
258 }
259 } else if (was_color) {
260 __bumpscope_generate_palette (priv, &priv->color);
261 was_color = 0;
262 }
263
264 if (visual_color_compare (&priv->old_color, &priv->color) == FALSE)
265 priv->colorchanged = TRUE;
266 else
267 priv->colorchanged = FALSE;
268
269 visual_color_copy (&priv->old_color, &priv->color);
270
271 bumpscope_render_light (priv, lx, ly);
272 }
273
274 static inline void draw_vert_line(uint8_t *buffer, int x, int y1, int y2, int pitch)
275 {
276 int y;
277 uint8_t *p;
278 if(y1 < y2)
279 {
280 p = buffer+((y1+1)*pitch)+x+1;
281 for(y = y1; y <= y2; y++) {
282 *p = 0xff;
283 p += pitch;
284 }
285 }
286 else if(y2 < y1)
287 {
288 p = buffer+((y2+1)*pitch)+x+1;
289 for(y = y2; y <= y1; y++) {
290 *p = 0xff;
291 p += pitch;
292 }
293 }
294 else {
295 buffer[((y1+1)*pitch)+x+1] = 0xff;
296 }
297 }
298
299 static void bumpscope_render_light (BumpscopePrivate *priv, int lx, int ly)
300 {
301 int i, j, prev_y, dy, dx, xq, yq;
302
303 prev_y = priv->video->pitch + 1;
304
305 for (dy = (-ly)+(priv->phongres/2), j = 0; j < priv->height; j++, dy++, prev_y+=priv->video->pitch-priv->width) {
306 for (dx = (-lx)+(priv->phongres/2), i = 0; i < priv->width; i++, dx++, prev_y++) {
307
308 xq = (priv->rgb_buf[prev_y-1]-priv->rgb_buf[prev_y+1])+dx;
309 yq = (priv->rgb_buf[prev_y-priv->video->pitch]-priv->rgb_buf[prev_y+priv->video->pitch])+dy;
310
311 if (yq<0 || yq>=priv->phongres ||
312 xq<0 || xq>=priv->phongres) {
313
314 priv->rgb_buf2[prev_y] = 0;
315
316 continue;
317 }
318
319 priv->rgb_buf2[prev_y] = priv->phongdat[(xq * priv->phongres) + yq];
320 }
321 }
322 }
323
324 void __bumpscope_generate_palette (BumpscopePrivate *priv, VisColor *col)
325 {
326 int32_t i,r,g,b;
327
328 for (i = 0; i < 256; i++) {
329 r = ((float)(100*col->r/255)*priv->intense1[i]+priv->intense2[i]);
330 g = ((float)(100*col->g/255)*priv->intense1[i]+priv->intense2[i]);
331 b = ((float)(100*col->b/255)*priv->intense1[i]+priv->intense2[i]);
332
333 priv->pal.colors[i].r = r;
334 priv->pal.colors[i].g = g;
335 priv->pal.colors[i].b = b;
336
337 }
338 }
339
340 void __bumpscope_generate_phongdat (BumpscopePrivate *priv)
341 {
342 int y, x;
343 double i, i2, cont;
344 int phresd2 = priv->phongres / 2;
345
346 for (y = phresd2; y < priv->phongres; y++) {
347 for (x = phresd2; x < priv->phongres; x++) {
348
349 i = (double)x/((double)priv->phongres)-1;
350 i2 = (double)y/((double)priv->phongres)-1;
351
352 if (priv->diamond)
353 i = 1 - pow(i*i2,.75) - i*i - i2*i2;
354 else
355 i = 1 - i*i - i2*i2;
356
357 if (i >= 0) {
358 if (priv->diamond)
359 i = i*i*i * 255.0;
360 else
361 i = i*i*i * 255.0;
362
363 if (i > 255) i = 255;
364
365 cont = i;
366
367 if (cont < 110)
368 cont = 0;
369
370 priv->phongdat[((y - phresd2) * priv->phongres) + x - phresd2] = cont;
371 priv->phongdat[(((priv->phongres-1)-y + phresd2) * priv->phongres) + x - phresd2] = cont;
372 priv->phongdat[(((y - phresd2) * priv->phongres) + ((priv->phongres-1) - x)) + phresd2] = cont;
373 priv->phongdat[(((priv->phongres-1)-y + phresd2) * priv->phongres) + ((priv->phongres-1)-x + phresd2)] = cont;
374 } else {
375 priv->phongdat[((y - phresd2) * priv->phongres) + x - phresd2] = cont;
376 priv->phongdat[(((priv->phongres-1)-y + phresd2) * priv->phongres) + x - phresd2] = cont;
377 priv->phongdat[(((y - phresd2) * priv->phongres) + ((priv->phongres-1) - x)) + phresd2] = cont;
378 priv->phongdat[(((priv->phongres-1)-y + phresd2) * priv->phongres) + ((priv->phongres-1)-x + phresd2)] = cont;
379 }
380 }
381 }
382 }
383
384 void __bumpscope_render_pcm (BumpscopePrivate *priv, float *data)
385 {
386 int i, y, prev_y;
387
388 prev_y = (int)priv->height/(int)2 + (data[0] * (priv->height / 2));
389
390 if (prev_y < 0) prev_y = 0;
391 if (prev_y >= priv->height) prev_y = priv->height-1;
392
393 for(i = 0; i < priv->width; i++)
394 {
395 y = (i*511)/(priv->width-1);
396
397 y = (int)priv->height/(int)2 + (data[y % 512] * priv->height);
398
399 /* This should _never_ happen (and doesn't), but I test anyway. */
400 if (y < 0) y = 0;
401 if(y >= priv->height) y = priv->height - 1;
402
403 draw_vert_line(priv->rgb_buf, i, prev_y, y, priv->video->pitch);
404 prev_y = y;
405 }
406
407 bumpscope_blur_8(priv->rgb_buf, priv->width, priv->height, priv->video->pitch);
408 bumpscope_draw (priv);
409 }
410
411 void __bumpscope_init (BumpscopePrivate *priv)
412 {
413 priv->phongdat = visual_mem_malloc0 (priv->phongres * priv->phongres * 2);
414 priv->rgb_buf = visual_mem_malloc0 (visual_video_get_size (priv->video) + (priv->video->pitch * 2) + 1);
415 priv->rgb_buf2 = visual_mem_malloc0 (visual_video_get_size (priv->video) + (priv->video->pitch * 2) + 1);
416
417 __bumpscope_generate_phongdat (priv);
418 bumpscope_generate_intense (priv);
419 __bumpscope_generate_palette (priv, &priv->color);
420 }
421
422 void __bumpscope_cleanup (BumpscopePrivate *priv)
423 {
424 if (priv->phongdat != NULL)
425 visual_mem_free (priv->phongdat);
426
427 if (priv->rgb_buf != NULL)
428 visual_mem_free (priv->rgb_buf);
429
430 if (priv->rgb_buf2 != NULL)
431 visual_mem_free (priv->rgb_buf2);
432 }
433