Codebase list frei0r / ceed3fc src / filter / 3dflippo / 3dflippo.c
ceed3fc

Tree @ceed3fc (Download .tar.gz)

3dflippo.c @ceed3fcraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* 3dflippo.c */

/*
 * 25/01/2006 c.e. prelz
 *
 * My second frei0r effect - more complex flipping
 *
 * Copyright (C) 2006 BEK - Bergen Senter for Elektronisk Kunst <bek@bek.no>
 * This file is a Frei0r plugin.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "frei0r.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_MSC_VER)
#define _USE_MATH_DEFINES
#endif /* _MSC_VER */
#include <math.h>

#define MSIZE 4
#define TWO_PI (M_PI*2.0)

enum axis
{
  AXIS_X,
  AXIS_Y,
  AXIS_Z
};

#include <assert.h>

typedef struct tdflippo_instance
{
  unsigned int width,height,fsize;
  int *mask;
  float flip[3],rate[3],center[2];
  unsigned char invertrot,dontblank,fillblack,mustrecompute;
} tdflippo_instance_t;

static float **newmat(unsigned char unit_flg);
static void matfree(float **mat);
static float **mat_translate(float tx,float ty,float tz);
static float **mat_rotate(enum axis ax,float angle);
static float **matmult(float **mat1,float **mat2);
static void vetmat(float **mat,float *x,float *y,float *z);
static void recompute_mask(tdflippo_instance_t* inst);

int f0r_init()
{
  return 1;
}

void f0r_deinit()
{
}

void f0r_get_plugin_info(f0r_plugin_info_t* flippoInfo)
{
  flippoInfo->name="3dflippo";
  flippoInfo->author="c.e. prelz AS FLUIDO <fluido@fluido.as>";
  flippoInfo->plugin_type=F0R_PLUGIN_TYPE_FILTER;
  flippoInfo->color_model=F0R_COLOR_MODEL_PACKED32;
  flippoInfo->frei0r_version=FREI0R_MAJOR_VERSION;
  flippoInfo->major_version=0;
  flippoInfo->minor_version=1;
  flippoInfo->num_params=11;
  flippoInfo->explanation="Frame rotation in 3d-space";
}

void f0r_get_param_info(f0r_param_info_t* info, int param_index)
{
  switch(param_index)
  {
  case 0:
    info->name="X axis rotation";
    info->type=F0R_PARAM_DOUBLE;
    info->explanation="Rotation on the X axis";
    break;
  case 1:
    info->name="Y axis rotation";
    info->type=F0R_PARAM_DOUBLE;
    info->explanation="Rotation on the Y axis";
    break;
  case 2:
    info->name="Z axis rotation";
    info->type=F0R_PARAM_DOUBLE;
    info->explanation="Rotation on the Z axis";
    break;
  case 3:
    info->name="X axis rotation rate";
    info->type=F0R_PARAM_DOUBLE;
    info->explanation="Rotation rate on the X axis";
    break;
  case 4:
    info->name="Y axis rotation rate";
    info->type=F0R_PARAM_DOUBLE;
    info->explanation="Rotation rate on the Y axis";
    break;
  case 5:
    info->name="Z axis rotation rate";
    info->type=F0R_PARAM_DOUBLE;
    info->explanation="Rotation rate on the Z axis";
    break;
  case 6:
    info->name="Center position (X)";
    info->type=F0R_PARAM_DOUBLE;
    info->explanation="Position of the center of rotation on the X axis";
    break;
  case 7:
    info->name="Center position (Y)";
    info->type=F0R_PARAM_DOUBLE;
    info->explanation="Position of the center of rotation on the Y axis";
    break;
  case 8:
    info->name="Invert rotation assignment";
    info->type=F0R_PARAM_BOOL;
    info->explanation="If true, when mapping rotation, make inverted (wrong) assignment";
    break;
  case 9:
    info->name="Don't blank mask";
    info->type=F0R_PARAM_BOOL;
    info->explanation="Mask for frame transposition is not blanked, so a trace of old transpositions is maintained";
    break;
  case 10:
    info->name="Fill with image or black";
    info->type=F0R_PARAM_BOOL;
    info->explanation="If true, pixels that are not transposed are black, otherwise, they are copied with the original";
    break;
  }
}

f0r_instance_t f0r_construct(unsigned int width,unsigned int height)
{
  tdflippo_instance_t *inst=(tdflippo_instance_t*)calloc(1, sizeof(*inst));
  inst->width=width;
  inst->height=height;
  inst->fsize=width*height;

  inst->flip[0]=inst->flip[1]=inst->flip[2]=inst->rate[0]=inst->rate[1]=inst->rate[2]=0.5;
  
  inst->mask=(int*)malloc(sizeof(int)*inst->fsize);

  return (f0r_instance_t)inst;
}

void f0r_destruct(f0r_instance_t instance)
{
  tdflippo_instance_t* inst=(tdflippo_instance_t*)instance;

  free(inst->mask);
  free(inst);
}

void f0r_set_param_value(f0r_instance_t instance,
                         f0r_param_t param,int param_index)
{
  assert(instance);

  tdflippo_instance_t *inst=(tdflippo_instance_t*)instance;

  switch(param_index)
  {
  case 0:
    inst->flip[0]=(float)(*((double*)param));
    break;
  case 1:
    inst->flip[1]=(float)(*((double*)param));
    break;
  case 2:
    inst->flip[2]=(float)(*((double*)param));
    break;
  case 3:
    inst->rate[0]=(float)(*((double*)param));
    break;
  case 4:
    inst->rate[1]=(float)(*((double*)param));
    break;
  case 5:
    inst->rate[2]=(float)(*((double*)param));
    break;
  case 6:
    inst->center[0]=(float)(*((double*)param));
    break;
  case 7:
    inst->center[1]=(float)(*((double*)param));
    break;
  case 8:
    inst->invertrot=(*((double*)param)>=0.5);
    break;
  case 9:
    inst->dontblank=(*((double*)param)>=0.5);
    break;
  case 10:
    inst->fillblack=(*((double*)param)>=0.5);
    break;
  }

  if((param_index>=0 && param_index<=2) || (param_index>=6 && param_index<=9))
    inst->mustrecompute=1;
}

void f0r_get_param_value(f0r_instance_t instance,
                         f0r_param_t param,int param_index)
{
  assert(instance);

  tdflippo_instance_t *inst=(tdflippo_instance_t*)instance;

  switch(param_index)
  {
  case 0:
    *((double*)param)=inst->flip[0];
    break;
  case 1:
    *((double*)param)=inst->flip[1];
    break;
  case 2:
    *((double*)param)=inst->flip[2];
    break;
  case 3:
    *((double*)param)=inst->rate[0];
    break;
  case 4:
    *((double*)param)=inst->rate[1];
    break;
  case 5:
    *((double*)param)=inst->rate[2];
    break;
  case 6:
    *((double*)param)=inst->center[0];
    break;
  case 7:
    *((double*)param)=inst->center[1];
    break;
  case 8:
    *((double*)param)=(inst->invertrot ? 1.0 : 0.0);
    break;
  case 9:
    *((double*)param)=(inst->dontblank ? 1.0 : 0.0);
    break;
  case 10:
    *((double*)param)=(inst->fillblack ? 1.0 : 0.0);
    break;    
  }
}

void f0r_update(f0r_instance_t instance,double time,
                const uint32_t *inframe, uint32_t *outframe)
{
  assert(instance);

  tdflippo_instance_t* inst=(tdflippo_instance_t*)instance;  
  int i;

  if(inst->rate[0]!=0.5 || inst->rate[1]!=0.5 || inst->rate[2]!=0.5 || inst->mustrecompute)
  {
    inst->mustrecompute=0;
    
/*
 * We are changing: apply change and recompute mask
 */

    for(i=0;i<3;i++)
    {
      inst->flip[i]+=inst->rate[i]-0.5;
      if(inst->flip[i]<0.0)
	inst->flip[i]+=1.0;
      else if(inst->flip[i]>=1.0)
	inst->flip[i]-=1.0;
    }
    recompute_mask(inst);
  }

  for(i=0;i<inst->fsize;i++)
  {
    if(inst->mask[i]>=0)
      outframe[i]=inframe[inst->mask[i]];
    else if(!inst->fillblack)
      outframe[i]=inframe[i];
    else
      outframe[i]=0;
  }
}

static float **newmat(unsigned char unit_flg)
{
  int i;
  float **to_ret=(float**)malloc(sizeof(float *)*MSIZE);

  for(i=0;i<MSIZE;i++)
  {
    to_ret[i]=(float*)calloc(MSIZE,sizeof(float));
    if(unit_flg)
      to_ret[i][i]=1.0;
  }

  return to_ret;
}

static void matfree(float **mat)
{
  int i;

  for(i=0;i<MSIZE;i++)
    free(mat[i]);
  
  free(mat);
}

static float **mat_translate(float tx,float ty,float tz)
{
  float **mat=newmat(1);

  mat[0][3]=tx;
  mat[1][3]=ty;
  mat[2][3]=tz;

  return mat;
}

static float **mat_rotate(enum axis ax,float angle)
{
  float **mat=newmat(1);
  float sf=sinf(angle);
  float cf=cosf(angle);

  switch(ax)
  {
    case AXIS_X:
      mat[1][1]=cf;
      mat[1][2]=-sf;
      mat[2][1]=sf;
      mat[2][2]=cf;
      break;
    case AXIS_Y:
      mat[0][0]=cf;
      mat[0][2]=sf;
      mat[2][0]=-sf;
      mat[2][2]=cf;
      break;
    case AXIS_Z:
      mat[0][0]=cf;
      mat[0][1]=-sf;
      mat[1][0]=sf;
      mat[1][1]=cf;
      break;
  }
  return mat;
}  

static float **matmult(float **mat1,float **mat2)
{
  float **mat=newmat(0);
  int i,j,k;

  for(i=0;i<MSIZE;i++)
    for(j=0;j<MSIZE;j++)
      for(k=0;k<MSIZE;k++)
	mat[i][j]+=mat1[i][k]*mat2[k][j];
  
  matfree(mat1);
  matfree(mat2);

  return mat;
}

static void vetmat(float **mat,float *x,float *y,float *z)
{
  float v;
  float vet[]={*x,*y,*z,1.0};
  float *vetp[]={x,y,z,&v};
  int i,j;

  for(i=0;i<MSIZE;i++)
  {
    *(vetp[i])=0.0;

    for(j=0;j<MSIZE;j++)
      *(vetp[i])+=mat[i][j]*vet[j];
  }
}
  
static void recompute_mask(tdflippo_instance_t* inst)
{
  float xpos=(float)inst->width*inst->center[0];
  float ypos=(float)inst->height*inst->center[1];
  float **mat=mat_translate(xpos,ypos,0.0);
  
  if(inst->flip[0]!=0.5)
    mat=matmult(mat,mat_rotate(AXIS_X,(inst->flip[0]-0.5)*TWO_PI));
  if(inst->flip[1]!=0.5)
    mat=matmult(mat,mat_rotate(AXIS_Y,(inst->flip[1]-0.5)*TWO_PI));
  if(inst->flip[2]!=0.5)
    mat=matmult(mat,mat_rotate(AXIS_Z,(inst->flip[2]-0.5)*TWO_PI));
  
  mat=matmult(mat,mat_translate(-xpos,-ypos,0.0));
  
#if 0
  fprintf(stderr,"Resarra %.2f %.2f %.2f %.2f | %.2f %.2f %.2f %.2f | %.2f %.2f %.2f %.2f | %.2f %.2f %.2f %.2f\n",
	  mat[0][0],mat[0][1],mat[0][2],mat[0][3],
	  mat[1][0],mat[1][1],mat[1][2],mat[1][3],
	  mat[2][0],mat[2][1],mat[2][2],mat[2][3],
	  mat[3][0],mat[3][1],mat[3][2],mat[3][3]);
#endif
  
  int x,y,nx,ny,pos;
  float xf,yf,zf;

  if(!inst->dontblank)
    memset(inst->mask,0xff,sizeof(int)*inst->fsize);

  for(y=0,pos=0;y<inst->height;y++)
    for(x=0;x<inst->width;x++,pos++)
    {
      xf=x;
      yf=y;
      zf=0.0;
      vetmat(mat,&xf,&yf,&zf);
      nx=(int)(xf+0.5);
      ny=(int)(yf+0.5);
      
      if(nx>=0 && nx<inst->width && ny>=0 && ny<inst->height)
      {
	if(!inst->invertrot) 
	  inst->mask[ny*inst->width+nx]=pos;
	else
	  inst->mask[pos]=ny*inst->width+nx;
      }
    }
  matfree(mat);
}