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