Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

per_pixel_eqn.c

00001 /*****************************************************************************
00002  * per_pixel_eqn.c:
00003  *****************************************************************************
00004  * Copyright (C) 2004 the VideoLAN team
00005  * $Id: per_pixel_eqn.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Cyril Deguet <[email protected]>
00008  *          code from projectM http://xmms-projectm.sourceforge.net
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 
00026 
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030 
00031 #include "fatal.h"
00032 #include "common.h"
00033 
00034 #include "expr_types.h"
00035 #include "eval.h"
00036 
00037 #include "splaytree_types.h"
00038 #include "splaytree.h"
00039 
00040 #include "param_types.h"
00041 #include "param.h"
00042 
00043 #include "per_pixel_eqn.h"
00044 #include "per_pixel_eqn_types.h"
00045 
00046 #include "engine_vars.h"
00047 
00048 
00049 extern preset_t * active_preset;
00050 
00051 extern int mesh_i;
00052 extern int mesh_j;
00053 
00054 
00055 
00056 /* Evaluates a per pixel equation */
00057 inline void evalPerPixelEqn(per_pixel_eqn_t * per_pixel_eqn) {
00058 
00059   double ** param_matrix = NULL;
00060   gen_expr_t * eqn_ptr = NULL;
00061   int x,y;
00062 
00063   eqn_ptr = per_pixel_eqn->gen_expr; 
00064  if (per_pixel_eqn->param->matrix == NULL) {
00065     if (PER_PIXEL_EQN_DEBUG) printf("evalPerPixelEqn: [begin initializing matrix] (index = %d) (name = %s)\n", 
00066                           per_pixel_eqn->index, per_pixel_eqn->param->name);
00067     
00068     param_matrix = per_pixel_eqn->param->matrix = (double**)malloc(gx*sizeof(double*));
00069     
00070     for(x = 0; x < gx; x++)
00071       param_matrix[x] = (double *)malloc(gy * sizeof(double));
00072 
00073     for (x = 0; x < gx; x++)
00074       for (y = 0; y < gy; y++)
00075         param_matrix[x][y] = 0.0;
00076 
00077     if (per_pixel_eqn->param->name == NULL)
00078       printf("null parameter?\n");
00079 
00080     //    printf("PARAM MATRIX: \"%s\" initialized.\n", per_pixel_eqn->param->name);
00081   }
00082   else 
00083     param_matrix = (double**)per_pixel_eqn->param->matrix;
00084  
00085   if (eqn_ptr == NULL)
00086     printf("something is seriously wrong...\n");
00087   for (mesh_i = 0; mesh_i < gx; mesh_i++) {    
00088     for (mesh_j = 0; mesh_j < gy; mesh_j++) {     
00089       param_matrix[mesh_i][mesh_j] = eval_gen_expr(eqn_ptr);
00090     }
00091   }
00092   
00093   /* Now that this parameter has been referenced with a per
00094      pixel equation, we let the evaluator know by setting
00095      this flag */
00096   per_pixel_eqn->param->matrix_flag = 1; 
00097 }
00098 
00099 inline void evalPerPixelEqns() {
00100 
00101   /* Evaluate all per pixel equations using splay traversal */
00102   splay_traverse(evalPerPixelEqn, active_preset->per_pixel_eqn_tree);
00103 
00104   /* Set mesh i / j values to -1 so engine vars are used by default again */
00105   mesh_i = mesh_j = -1;
00106 
00107 }
00108 /* Adds a per pixel equation according to its string name. This
00109    will be used only by the parser */
00110 
00111 int add_per_pixel_eqn(char * name, gen_expr_t * gen_expr, preset_t * preset) {
00112 
00113   per_pixel_eqn_t * per_pixel_eqn;
00114   int index;
00115   param_t * param = NULL;
00116 
00117   /* Argument checks */
00118   if (preset == NULL)
00119           return FAILURE;
00120   if (gen_expr == NULL)
00121           return FAILURE;
00122   if (name == NULL)
00123           return FAILURE;
00124   
00125  if (PER_PIXEL_EQN_DEBUG) printf("add_per_pixel_eqn: per pixel equation (name = \"%s\")\n", name);
00126  
00127  if (!strncmp(name, "dx", strlen("dx"))) 
00128    preset->per_pixel_flag[DX_OP] = TRUE;
00129  else if (!strncmp(name, "dy", strlen("dy"))) 
00130    preset->per_pixel_flag[DY_OP] = TRUE;
00131  else if (!strncmp(name, "cx", strlen("cx"))) 
00132    preset->per_pixel_flag[CX_OP] = TRUE;
00133  else if (!strncmp(name, "cy", strlen("cy"))) 
00134    preset->per_pixel_flag[CX_OP] = TRUE;
00135  else if (!strncmp(name, "zoom", strlen("zoom"))) 
00136    preset->per_pixel_flag[ZOOM_OP] = TRUE;
00137  else if (!strncmp(name, "zoomexp", strlen("zoomexp"))) 
00138    preset->per_pixel_flag[ZOOMEXP_OP] = TRUE;
00139  else if (!strncmp(name, "rot", strlen("rot")))
00140    preset->per_pixel_flag[ROT_OP] = TRUE;
00141  else if (!strncmp(name, "sx", strlen("sx")))
00142    preset->per_pixel_flag[SX_OP] = TRUE;
00143  else if (!strncmp(name, "sy", strlen("sy")))
00144    preset->per_pixel_flag[SY_OP] = TRUE;
00145  
00146 
00147  /* Search for the parameter so we know what matrix the per pixel equation is referencing */
00148 
00149  if ((param = find_param(name, preset, TRUE)) == NULL) {
00150    if (PER_PIXEL_EQN_DEBUG) printf("add_per_pixel_eqn: failed to allocate a new parameter!\n");
00151    return FAILURE;
00152  
00153  }       
00154 
00155  /* Find most largest index in the splaytree */
00156  // if ((per_pixel_eqn = splay_find_max(active_preset->per_pixel_eqn_tree)) == NULL)
00157  // index = 0;
00158  // else
00159  index = splay_size(preset->per_pixel_eqn_tree);
00160    
00161  /* Create the per pixel equation given the index, parameter, and general expression */
00162  if ((per_pixel_eqn = new_per_pixel_eqn(index, param, gen_expr)) == NULL) {
00163    if (PER_PIXEL_EQN_DEBUG) printf("add_per_pixel_eqn: failed to create new per pixel equation!\n");
00164    return FAILURE;
00165 
00166  }
00167 
00168  if (PER_PIXEL_EQN_DEBUG) printf("add_per_pixel_eqn: new equation (index = %d) (param = \"%s\")\n", 
00169                                  per_pixel_eqn->index, per_pixel_eqn->param->name);
00170  /* Insert the per pixel equation into the preset per pixel database */
00171  if (splay_insert(per_pixel_eqn, &per_pixel_eqn->index, preset->per_pixel_eqn_tree) < 0) {
00172    free_per_pixel_eqn(per_pixel_eqn);
00173    printf("failed to add per pixel eqn!\n");
00174    return FAILURE;       
00175  }
00176 
00177  /* Done */ 
00178  return SUCCESS;
00179 }
00180 
00181 per_pixel_eqn_t * new_per_pixel_eqn(int index, param_t * param, gen_expr_t * gen_expr) {
00182 
00183         per_pixel_eqn_t * per_pixel_eqn;
00184         
00185         if (index < 0)
00186           return NULL;
00187         if (param == NULL)
00188           return NULL;
00189         if (gen_expr == NULL)
00190           return NULL;
00191         
00192         if ((per_pixel_eqn = (per_pixel_eqn_t*)malloc(sizeof(per_pixel_eqn_t))) == NULL)
00193           return NULL;
00194 
00195         
00196         per_pixel_eqn->index = index;
00197         per_pixel_eqn->param = param;
00198         per_pixel_eqn->gen_expr = gen_expr;
00199         
00200         return per_pixel_eqn;   
00201 }
00202 
00203 
00204 void free_per_pixel_eqn(per_pixel_eqn_t * per_pixel_eqn) {
00205 
00206         if (per_pixel_eqn == NULL)
00207                 return;
00208         
00209         free_gen_expr(per_pixel_eqn->gen_expr);
00210         
00211         free(per_pixel_eqn);
00212         
00213         return;
00214 }
00215 
00216 inline int isPerPixelEqn(int op) {
00217     
00218   return active_preset->per_pixel_flag[op];
00219 
00220 }
00221 
00222 inline int resetPerPixelEqnFlags(preset_t * preset) {
00223   int i;
00224 
00225   for (i = 0; i < NUM_OPS;i++)
00226     preset->per_pixel_flag[i] = FALSE;
00227 
00228   return SUCCESS;
00229 }

Generated on Tue Dec 20 10:14:59 2005 for vlc-0.8.4a by  doxygen 1.4.2