]> git.sesse.net Git - vlc/blob - modules/visualization/galaktos/per_pixel_eqn.c
Remove stdlib.h
[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
26
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "fatal.h"
31 #include "common.h"
32
33 #include "expr_types.h"
34 #include "eval.h"
35
36 #include "splaytree_types.h"
37 #include "splaytree.h"
38
39 #include "param_types.h"
40 #include "param.h"
41
42 #include "per_pixel_eqn.h"
43 #include "per_pixel_eqn_types.h"
44
45 #include "engine_vars.h"
46
47
48 extern preset_t * active_preset;
49
50 extern int mesh_i;
51 extern int mesh_j;
52
53
54
55 /* Evaluates a per pixel equation */
56 inline void evalPerPixelEqn(per_pixel_eqn_t * per_pixel_eqn) {
57
58   double ** param_matrix = NULL;
59   gen_expr_t * eqn_ptr = NULL;
60   int x,y;
61
62   eqn_ptr = per_pixel_eqn->gen_expr; 
63  if (per_pixel_eqn->param->matrix == NULL) {
64     if (PER_PIXEL_EQN_DEBUG) printf("evalPerPixelEqn: [begin initializing matrix] (index = %d) (name = %s)\n", 
65                           per_pixel_eqn->index, per_pixel_eqn->param->name);
66     
67     param_matrix = per_pixel_eqn->param->matrix = (double**)malloc(gx*sizeof(double*));
68     
69     for(x = 0; x < gx; x++)
70       param_matrix[x] = (double *)malloc(gy * sizeof(double));
71
72     for (x = 0; x < gx; x++)
73       for (y = 0; y < gy; y++)
74         param_matrix[x][y] = 0.0;
75
76     if (per_pixel_eqn->param->name == NULL)
77       printf("null parameter?\n");
78
79     //    printf("PARAM MATRIX: \"%s\" initialized.\n", per_pixel_eqn->param->name);
80   }
81   else 
82     param_matrix = (double**)per_pixel_eqn->param->matrix;
83  
84   if (eqn_ptr == NULL)
85     printf("something is seriously wrong...\n");
86   for (mesh_i = 0; mesh_i < gx; mesh_i++) {    
87     for (mesh_j = 0; mesh_j < gy; mesh_j++) {     
88       param_matrix[mesh_i][mesh_j] = eval_gen_expr(eqn_ptr);
89     }
90   }
91   
92   /* Now that this parameter has been referenced with a per
93      pixel equation, we let the evaluator know by setting
94      this flag */
95   per_pixel_eqn->param->matrix_flag = 1; 
96 }
97
98 inline void evalPerPixelEqns() {
99
100   /* Evaluate all per pixel equations using splay traversal */
101   splay_traverse(evalPerPixelEqn, active_preset->per_pixel_eqn_tree);
102
103   /* Set mesh i / j values to -1 so engine vars are used by default again */
104   mesh_i = mesh_j = -1;
105
106 }
107 /* Adds a per pixel equation according to its string name. This
108    will be used only by the parser */
109
110 int add_per_pixel_eqn(char * name, gen_expr_t * gen_expr, preset_t * preset) {
111
112   per_pixel_eqn_t * per_pixel_eqn;
113   int index;
114   param_t * param = NULL;
115
116   /* Argument checks */
117   if (preset == NULL)
118           return FAILURE;
119   if (gen_expr == NULL)
120           return FAILURE;
121   if (name == NULL)
122           return FAILURE;
123   
124  if (PER_PIXEL_EQN_DEBUG) printf("add_per_pixel_eqn: per pixel equation (name = \"%s\")\n", name);
125  
126  if (!strncmp(name, "dx", strlen("dx"))) 
127    preset->per_pixel_flag[DX_OP] = TRUE;
128  else if (!strncmp(name, "dy", strlen("dy"))) 
129    preset->per_pixel_flag[DY_OP] = TRUE;
130  else if (!strncmp(name, "cx", strlen("cx"))) 
131    preset->per_pixel_flag[CX_OP] = TRUE;
132  else if (!strncmp(name, "cy", strlen("cy"))) 
133    preset->per_pixel_flag[CX_OP] = TRUE;
134  else if (!strncmp(name, "zoom", strlen("zoom"))) 
135    preset->per_pixel_flag[ZOOM_OP] = TRUE;
136  else if (!strncmp(name, "zoomexp", strlen("zoomexp"))) 
137    preset->per_pixel_flag[ZOOMEXP_OP] = TRUE;
138  else if (!strncmp(name, "rot", strlen("rot")))
139    preset->per_pixel_flag[ROT_OP] = TRUE;
140  else if (!strncmp(name, "sx", strlen("sx")))
141    preset->per_pixel_flag[SX_OP] = TRUE;
142  else if (!strncmp(name, "sy", strlen("sy")))
143    preset->per_pixel_flag[SY_OP] = TRUE;
144  
145
146  /* Search for the parameter so we know what matrix the per pixel equation is referencing */
147
148  if ((param = find_param(name, preset, TRUE)) == NULL) {
149    if (PER_PIXEL_EQN_DEBUG) printf("add_per_pixel_eqn: failed to allocate a new parameter!\n");
150    return FAILURE;
151  
152  }       
153
154  /* Find most largest index in the splaytree */
155  // if ((per_pixel_eqn = splay_find_max(active_preset->per_pixel_eqn_tree)) == NULL)
156  // index = 0;
157  // else
158  index = splay_size(preset->per_pixel_eqn_tree);
159    
160  /* Create the per pixel equation given the index, parameter, and general expression */
161  if ((per_pixel_eqn = new_per_pixel_eqn(index, param, gen_expr)) == NULL) {
162    if (PER_PIXEL_EQN_DEBUG) printf("add_per_pixel_eqn: failed to create new per pixel equation!\n");
163    return FAILURE;
164
165  }
166
167  if (PER_PIXEL_EQN_DEBUG) printf("add_per_pixel_eqn: new equation (index = %d) (param = \"%s\")\n", 
168                                  per_pixel_eqn->index, per_pixel_eqn->param->name);
169  /* Insert the per pixel equation into the preset per pixel database */
170  if (splay_insert(per_pixel_eqn, &per_pixel_eqn->index, preset->per_pixel_eqn_tree) < 0) {
171    free_per_pixel_eqn(per_pixel_eqn);
172    printf("failed to add per pixel eqn!\n");
173    return FAILURE;       
174  }
175
176  /* Done */ 
177  return SUCCESS;
178 }
179
180 per_pixel_eqn_t * new_per_pixel_eqn(int index, param_t * param, gen_expr_t * gen_expr) {
181
182         per_pixel_eqn_t * per_pixel_eqn;
183         
184         if (index < 0)
185           return NULL;
186         if (param == NULL)
187           return NULL;
188         if (gen_expr == NULL)
189           return NULL;
190         
191         if ((per_pixel_eqn = (per_pixel_eqn_t*)malloc(sizeof(per_pixel_eqn_t))) == NULL)
192           return NULL;
193
194         
195         per_pixel_eqn->index = index;
196         per_pixel_eqn->param = param;
197         per_pixel_eqn->gen_expr = gen_expr;
198         
199         return per_pixel_eqn;   
200 }
201
202
203 void free_per_pixel_eqn(per_pixel_eqn_t * per_pixel_eqn) {
204
205         if (per_pixel_eqn == NULL)
206                 return;
207         
208         free_gen_expr(per_pixel_eqn->gen_expr);
209         
210         free(per_pixel_eqn);
211         
212         return;
213 }
214
215 inline int isPerPixelEqn(int op) {
216     
217   return active_preset->per_pixel_flag[op];
218
219 }
220
221 inline int resetPerPixelEqnFlags(preset_t * preset) {
222   int i;
223
224   for (i = 0; i < NUM_OPS;i++)
225     preset->per_pixel_flag[i] = FALSE;
226
227   return SUCCESS;
228 }