]> git.sesse.net Git - vlc/blob - modules/visualization/galaktos/per_pixel_eqn.c
1062b2248156c95b585d2e9f2d60e6a9afde9e18
[vlc] / modules / visualization / galaktos / per_pixel_eqn.c
1 /*****************************************************************************
2  * per_pixel_eqn.c:
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet <asmax@videolan.org>
8  *          code from projectM http://xmms-projectm.sourceforge.net
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <stdlib.h>
26
27 #include "fatal.h"
28 #include "common.h"
29
30 #include "expr_types.h"
31 #include "eval.h"
32
33 #include "splaytree_types.h"
34 #include "splaytree.h"
35
36 #include "param_types.h"
37 #include "param.h"
38
39 #include "per_pixel_eqn.h"
40 #include "per_pixel_eqn_types.h"
41
42 #include "engine_vars.h"
43
44
45 extern preset_t * active_preset;
46
47 extern int mesh_i;
48 extern int mesh_j;
49
50
51
52 /* Evaluates a per pixel equation */
53 inline void evalPerPixelEqn(per_pixel_eqn_t * per_pixel_eqn) {
54
55   double ** param_matrix = NULL;
56   gen_expr_t * eqn_ptr = NULL;
57   int x,y;
58
59   eqn_ptr = per_pixel_eqn->gen_expr; 
60  if (per_pixel_eqn->param->matrix == NULL) {
61     if (PER_PIXEL_EQN_DEBUG) printf("evalPerPixelEqn: [begin initializing matrix] (index = %d) (name = %s)\n", 
62                           per_pixel_eqn->index, per_pixel_eqn->param->name);
63     
64     param_matrix = per_pixel_eqn->param->matrix = (double**)malloc(gx*sizeof(double*));
65     
66     for(x = 0; x < gx; x++)
67       param_matrix[x] = (double *)malloc(gy * sizeof(double));
68
69     for (x = 0; x < gx; x++)
70       for (y = 0; y < gy; y++)
71         param_matrix[x][y] = 0.0;
72
73     if (per_pixel_eqn->param->name == NULL)
74       printf("null parameter?\n");
75
76     //    printf("PARAM MATRIX: \"%s\" initialized.\n", per_pixel_eqn->param->name);
77   }
78   else 
79     param_matrix = (double**)per_pixel_eqn->param->matrix;
80  
81   if (eqn_ptr == NULL)
82     printf("something is seriously wrong...\n");
83   for (mesh_i = 0; mesh_i < gx; mesh_i++) {    
84     for (mesh_j = 0; mesh_j < gy; mesh_j++) {     
85       param_matrix[mesh_i][mesh_j] = eval_gen_expr(eqn_ptr);
86     }
87   }
88   
89   /* Now that this parameter has been referenced with a per
90      pixel equation, we let the evaluator know by setting
91      this flag */
92   per_pixel_eqn->param->matrix_flag = 1; 
93 }
94
95 inline void evalPerPixelEqns() {
96
97   /* Evaluate all per pixel equations using splay traversal */
98   splay_traverse(evalPerPixelEqn, active_preset->per_pixel_eqn_tree);
99
100   /* Set mesh i / j values to -1 so engine vars are used by default again */
101   mesh_i = mesh_j = -1;
102
103 }
104 /* Adds a per pixel equation according to its string name. This
105    will be used only by the parser */
106
107 int add_per_pixel_eqn(char * name, gen_expr_t * gen_expr, preset_t * preset) {
108
109   per_pixel_eqn_t * per_pixel_eqn;
110   int index;
111   param_t * param = NULL;
112
113   /* Argument checks */
114   if (preset == NULL)
115           return FAILURE;
116   if (gen_expr == NULL)
117           return FAILURE;
118   if (name == NULL)
119           return FAILURE;
120   
121  if (PER_PIXEL_EQN_DEBUG) printf("add_per_pixel_eqn: per pixel equation (name = \"%s\")\n", name);
122  
123  if (!strncmp(name, "dx", strlen("dx"))) 
124    preset->per_pixel_flag[DX_OP] = TRUE;
125  else if (!strncmp(name, "dy", strlen("dy"))) 
126    preset->per_pixel_flag[DY_OP] = TRUE;
127  else if (!strncmp(name, "cx", strlen("cx"))) 
128    preset->per_pixel_flag[CX_OP] = TRUE;
129  else if (!strncmp(name, "cy", strlen("cy"))) 
130    preset->per_pixel_flag[CX_OP] = TRUE;
131  else if (!strncmp(name, "zoom", strlen("zoom"))) 
132    preset->per_pixel_flag[ZOOM_OP] = TRUE;
133  else if (!strncmp(name, "zoomexp", strlen("zoomexp"))) 
134    preset->per_pixel_flag[ZOOMEXP_OP] = TRUE;
135  else if (!strncmp(name, "rot", strlen("rot")))
136    preset->per_pixel_flag[ROT_OP] = TRUE;
137  else if (!strncmp(name, "sx", strlen("sx")))
138    preset->per_pixel_flag[SX_OP] = TRUE;
139  else if (!strncmp(name, "sy", strlen("sy")))
140    preset->per_pixel_flag[SY_OP] = TRUE;
141  
142
143  /* Search for the parameter so we know what matrix the per pixel equation is referencing */
144
145  if ((param = find_param(name, preset, TRUE)) == NULL) {
146    if (PER_PIXEL_EQN_DEBUG) printf("add_per_pixel_eqn: failed to allocate a new parameter!\n");
147    return FAILURE;
148  
149  }       
150
151  /* Find most largest index in the splaytree */
152  // if ((per_pixel_eqn = splay_find_max(active_preset->per_pixel_eqn_tree)) == NULL)
153  // index = 0;
154  // else
155  index = splay_size(preset->per_pixel_eqn_tree);
156    
157  /* Create the per pixel equation given the index, parameter, and general expression */
158  if ((per_pixel_eqn = new_per_pixel_eqn(index, param, gen_expr)) == NULL) {
159    if (PER_PIXEL_EQN_DEBUG) printf("add_per_pixel_eqn: failed to create new per pixel equation!\n");
160    return FAILURE;
161
162  }
163
164  if (PER_PIXEL_EQN_DEBUG) printf("add_per_pixel_eqn: new equation (index = %d) (param = \"%s\")\n", 
165                                  per_pixel_eqn->index, per_pixel_eqn->param->name);
166  /* Insert the per pixel equation into the preset per pixel database */
167  if (splay_insert(per_pixel_eqn, &per_pixel_eqn->index, preset->per_pixel_eqn_tree) < 0) {
168    free_per_pixel_eqn(per_pixel_eqn);
169    printf("failed to add per pixel eqn!\n");
170    return FAILURE;       
171  }
172
173  /* Done */ 
174  return SUCCESS;
175 }
176
177 per_pixel_eqn_t * new_per_pixel_eqn(int index, param_t * param, gen_expr_t * gen_expr) {
178
179         per_pixel_eqn_t * per_pixel_eqn;
180         
181         if (index < 0)
182           return NULL;
183         if (param == NULL)
184           return NULL;
185         if (gen_expr == NULL)
186           return NULL;
187         
188         if ((per_pixel_eqn = (per_pixel_eqn_t*)malloc(sizeof(per_pixel_eqn_t))) == NULL)
189           return NULL;
190
191         
192         per_pixel_eqn->index = index;
193         per_pixel_eqn->param = param;
194         per_pixel_eqn->gen_expr = gen_expr;
195         
196         return per_pixel_eqn;   
197 }
198
199
200 void free_per_pixel_eqn(per_pixel_eqn_t * per_pixel_eqn) {
201
202         if (per_pixel_eqn == NULL)
203                 return;
204         
205         free_gen_expr(per_pixel_eqn->gen_expr);
206         
207         free(per_pixel_eqn);
208         
209         return;
210 }
211
212 inline int isPerPixelEqn(int op) {
213     
214   return active_preset->per_pixel_flag[op];
215
216 }
217
218 inline int resetPerPixelEqnFlags(preset_t * preset) {
219   int i;
220
221   for (i = 0; i < NUM_OPS;i++)
222     preset->per_pixel_flag[i] = FALSE;
223
224   return SUCCESS;
225 }