summaryrefslogtreecommitdiff
path: root/src/filters.c
blob: 1af5d4a8f555efda27f9c22b86e2ee9538d2575f (plain)
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
    FLAM3 - cosmic recursive fractal flames
    Copyright (C) 1992-2009 Spotworks LLC

    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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include "filters.h"


/*
 * filter function definitions
 * from Graphics Gems III code
 * and ImageMagick resize.c
 */


double flam3_spatial_support[flam3_num_spatialfilters] = {

   1.5, /* gaussian */
   1.0, /* hermite */
   0.5, /* box */
   1.0, /* triangle */
   1.5, /* bell */
   2.0, /* b spline */
   2.0, /* mitchell */
   1.0, /* blackman */
   2.0, /* catrom */
   1.0, /* hanning */
   1.0, /* hamming */
   3.0, /* lanczos3 */
   2.0, /* lanczos2 */
   1.5  /* quadratic */
};

double flam3_hermite_filter(double t) {
   /* f(t) = 2|t|^3 - 3|t|^2 + 1, -1 <= t <= 1 */
   if(t < 0.0) t = -t;
   if(t < 1.0) return((2.0 * t - 3.0) * t * t + 1.0);
   return(0.0);
}

double flam3_box_filter(double t) {
   if((t > -0.5) && (t <= 0.5)) return(1.0);
   return(0.0);
}

double flam3_triangle_filter(double t) {
   if(t < 0.0) t = -t;
   if(t < 1.0) return(1.0 - t);
   return(0.0);
}

double flam3_bell_filter(double t) {
   /* box (*) box (*) box */
   if(t < 0) t = -t;
   if(t < .5) return(.75 - (t * t));
   if(t < 1.5) {
      t = (t - 1.5);
      return(.5 * (t * t));
   }
   return(0.0);
}

double flam3_b_spline_filter(double t) {

   /* box (*) box (*) box (*) box */
   double tt;

   if(t < 0) t = -t;
   if(t < 1) {
      tt = t * t;
      return((.5 * tt * t) - tt + (2.0 / 3.0));
   } else if(t < 2) {
      t = 2 - t;
      return((1.0 / 6.0) * (t * t * t));
   }
   return(0.0);
}

double flam3_sinc(double x) {
   x *= M_PI;
   if(x != 0) return(sin(x) / x);
   return(1.0);
}

double flam3_blackman_filter(double x) {
  return(0.42+0.5*cos(M_PI*x)+0.08*cos(2*M_PI*x));
}

double flam3_catrom_filter(double x) {
  if (x < -2.0)
    return(0.0);
  if (x < -1.0)
    return(0.5*(4.0+x*(8.0+x*(5.0+x))));
  if (x < 0.0)
    return(0.5*(2.0+x*x*(-5.0-3.0*x)));
  if (x < 1.0)
    return(0.5*(2.0+x*x*(-5.0+3.0*x)));
  if (x < 2.0)
    return(0.5*(4.0+x*(-8.0+x*(5.0-x))));
  return(0.0);
}

double flam3_mitchell_filter(double t) {
   double tt;

   tt = t * t;
   if(t < 0) t = -t;
   if(t < 1.0) {
      t = (((12.0 - 9.0 * flam3_mitchell_b - 6.0 * flam3_mitchell_c) * (t * tt))
         + ((-18.0 + 12.0 * flam3_mitchell_b + 6.0 * flam3_mitchell_c) * tt)
         + (6.0 - 2 * flam3_mitchell_b));
      return(t / 6.0);
   } else if(t < 2.0) {
      t = (((-1.0 * flam3_mitchell_b - 6.0 * flam3_mitchell_c) * (t * tt))
         + ((6.0 * flam3_mitchell_b + 30.0 * flam3_mitchell_c) * tt)
         + ((-12.0 * flam3_mitchell_b - 48.0 * flam3_mitchell_c) * t)
         + (8.0 * flam3_mitchell_b + 24 * flam3_mitchell_c));
      return(t / 6.0);
   }
   return(0.0);
}

double flam3_hanning_filter(double x) {
  return(0.5+0.5*cos(M_PI*x));
}

double flam3_hamming_filter(double x) {
  return(0.54+0.46*cos(M_PI*x));
}

double flam3_lanczos3_filter(double t) {
   if(t < 0) t = -t;
   if(t < 3.0) return(flam3_sinc(t) * flam3_sinc(t/3.0));
   return(0.0);
}

double flam3_lanczos2_filter(double t) {
   if(t < 0) t = -t;
   if(t < 2.0) return(flam3_sinc(t) * flam3_sinc(t/2.0));
   return(0.0);
}

double flam3_gaussian_filter(double x) {
  return(exp((-2.0*x*x))*sqrt(2.0/M_PI));
}

double flam3_quadratic_filter(double x) {
  if (x < -1.5)
    return(0.0);
  if (x < -0.5)
    return(0.5*(x+1.5)*(x+1.5));
  if (x < 0.5)
    return(0.75-x*x);
  if (x < 1.5)
    return(0.5*(x-1.5)*(x-1.5));
  return(0.0);
}

double flam3_spatial_filter(int knum, double x) {

   if (knum==0)
      return flam3_gaussian_filter(x);
   else if (knum==1)
      return flam3_hermite_filter(x);
   else if (knum==2)
      return flam3_box_filter(x);
   else if (knum==3)
      return flam3_triangle_filter(x);
   else if (knum==4)
      return flam3_bell_filter(x);
   else if (knum==5)
      return flam3_b_spline_filter(x);
   else if (knum==6)
      return flam3_mitchell_filter(x);
   else if (knum==7)
      return flam3_sinc(x)*flam3_blackman_filter(x);
   else if (knum==8)
      return flam3_catrom_filter(x);
   else if (knum==9)
      return flam3_sinc(x)*flam3_hanning_filter(x);
   else if (knum==10)
      return flam3_sinc(x)*flam3_hamming_filter(x);
   else if (knum==11)
      return flam3_lanczos3_filter(x)*flam3_sinc(x/3.0);   
   else if (knum==12)
      return flam3_lanczos2_filter(x)*flam3_sinc(x/2.0);
   else if (knum==13)
      return flam3_quadratic_filter(x);
}

int normalize_vector(double *v, int n) {
   double t = 0.0;
   int i;
   for (i = 0; i < n; i++)
      t += v[i];
   if (0.0 == t) return 1;
   t = 1.0 / t;
   for (i = 0; i < n; i++)
      v[i] *= t;
   return 0;
}


int flam3_create_spatial_filter(flam3_frame *spec, int field, double **filter) {

   int sf_kernel = spec->genomes[0].spatial_filter_select;
   int supersample = spec->genomes[0].spatial_oversample;
   double sf_radius = spec->genomes[0].spatial_filter_radius;
   double aspect_ratio = spec->pixel_aspect_ratio;   
   double sf_supp = flam3_spatial_support[sf_kernel];
   
   double fw = 2.0 * sf_supp * supersample * sf_radius / aspect_ratio;
   double adjust, ii, jj;
   
   int fwidth = ((int) fw) + 1;
   int i,j;
   
   
   /* Make sure the filter kernel has same parity as oversample */
   if ((fwidth ^ supersample) & 1)
      fwidth++;

   /* Calculate the coordinate scaling factor for the kernel values */
   if (fw > 0.0)
      adjust = sf_supp * fwidth / fw;
   else
      adjust = 1.0;

   /* Calling function MUST FREE THE RETURNED KERNEL, lest ye leak memory */
   (*filter) = (double *)calloc(fwidth * fwidth,sizeof(double));

   /* fill in the coefs */
   for (i = 0; i < fwidth; i++)
      for (j = 0; j < fwidth; j++) {
      
         /* Calculate the function inputs for the kernel function */
         ii = ((2.0 * i + 1.0) / (double)fwidth - 1.0)*adjust;
         jj = ((2.0 * j + 1.0) / (double)fwidth - 1.0)*adjust;

         /* Scale for scanlines */
         if (field) jj *= 2.0;

         /* Adjust for aspect ratio */
         jj /= aspect_ratio;

         (*filter)[i + j * fwidth] = 
               flam3_spatial_filter(sf_kernel,ii) * flam3_spatial_filter(sf_kernel,jj);
      }


   if (normalize_vector((*filter), fwidth * fwidth)) {
      fprintf(stderr, "Spatial filter value is too small: %g.  Terminating.\n",sf_radius);
      return(-1);
   }   
   
   return (fwidth);
}

flam3_de_helper flam3_create_de_filters(double max_rad, double min_rad, double curve, int ss) {

   flam3_de_helper de;
   double comp_max_radius, comp_min_radius;
   double num_de_filters_d;
   int num_de_filters,de_max_ind;
   int de_row_size, de_half_size;
   int filtloop;
   int keep_thresh=100;

   de.kernel_size=-1;

   if (curve <= 0.0) {
      fprintf(stderr,"estimator curve must be > 0\n");
      return(de);
   }

   if (max_rad < min_rad) {
      fprintf(stderr,"estimator must be larger than estimator_minimum.\n");
      fprintf(stderr,"(%f > %f) ? \n",max_rad,min_rad);
      return(de);
   }

   /* We should scale the filter width by the oversample          */
   /* The '+1' comes from the assumed distance to the first pixel */
   comp_max_radius = max_rad*ss + 1;
   comp_min_radius = min_rad*ss + 1;

   /* Calculate how many filter kernels we need based on the decay function */
   /*                                                                       */
   /*    num filters = (de_max_width / de_min_width)^(1/estimator_curve)    */
   /*                                                                       */
   num_de_filters_d = pow( comp_max_radius/comp_min_radius, 1.0/curve );
   if (num_de_filters_d>1e7) {
      fprintf(stderr,"too many filters required in this configuration (%g)\n",num_de_filters_d);
      return(de);
   }
   num_de_filters = (int)ceil(num_de_filters_d);
         
   /* Condense the smaller kernels to save space */
   if (num_de_filters>keep_thresh) { 
      de_max_ind = (int)ceil(DE_THRESH + pow(num_de_filters-DE_THRESH,curve))+1;
      de.max_filtered_counts = (int)pow( (double)(de_max_ind-DE_THRESH), 1.0/curve) + DE_THRESH;
   } else {
      de_max_ind = num_de_filters;
      de.max_filtered_counts = de_max_ind;
   }

   /* Allocate the memory for these filters */
   /* and the hit/width lookup vector       */
   de_row_size = (int)(2*ceil(comp_max_radius)-1);
   de_half_size = (de_row_size-1)/2;
   de.kernel_size = (de_half_size+1)*(2+de_half_size)/2;

   de.filter_coefs = (double *) calloc (de_max_ind * de.kernel_size,sizeof(double));
   de.filter_widths = (double *) calloc (de_max_ind,sizeof(double));

   /* Generate the filter coefficients */
   de.max_filter_index = 0;
   for (filtloop=0;filtloop<de_max_ind;filtloop++) {

      double de_filt_sum=0.0, de_filt_d;
      double de_filt_h;
      int dej,dek;
      double adjloop;
      int filter_coef_idx;

      /* Calculate the filter width for this number of hits in a bin */
      if (filtloop<keep_thresh)
         de_filt_h = (comp_max_radius / pow(filtloop+1,curve));
      else {
         adjloop = pow(filtloop-keep_thresh,(1.0/curve)) + keep_thresh;
         de_filt_h = (comp_max_radius / pow(adjloop+1,curve));
      }

      /* Once we've reached the min radius, don't populate any more */
      if (de_filt_h <= comp_min_radius) {
         de_filt_h = comp_min_radius;
         de.max_filter_index = filtloop;
      }

      de.filter_widths[filtloop] = de_filt_h;

      /* Calculate norm of kernel separately (easier) */
      for (dej=-de_half_size; dej<=de_half_size; dej++) {
         for (dek=-de_half_size; dek<=de_half_size; dek++) {
            
            de_filt_d = sqrt( (double)(dej*dej+dek*dek) ) / de_filt_h;

            /* Only populate the coefs within this radius */
            if (de_filt_d <= 1.0) {

               /* Gaussian */
               de_filt_sum += flam3_spatial_filter(flam3_gaussian_kernel,
                        flam3_spatial_support[flam3_gaussian_kernel]*de_filt_d);

               /* Epanichnikov */
//             de_filt_sum += (1.0 - (de_filt_d * de_filt_d));
            }
         }
      }

      filter_coef_idx = filtloop*de.kernel_size;

      /* Calculate the unique entries of the kernel */
      for (dej=0; dej<=de_half_size; dej++) {
         for (dek=0; dek<=dej; dek++) {
            de_filt_d = sqrt( (double)(dej*dej+dek*dek) ) / de_filt_h;

            /* Only populate the coefs within this radius */
            if (de_filt_d>1.0)
               de.filter_coefs[filter_coef_idx] = 0.0;
            else {

               /* Gaussian */
               de.filter_coefs[filter_coef_idx] = flam3_spatial_filter(flam3_gaussian_kernel,
                        flam3_spatial_support[flam3_gaussian_kernel]*de_filt_d)/de_filt_sum; 
                            
               /* Epanichnikov */
//             de_filter_coefs[filter_coef_idx] = (1.0 - (de_filt_d * de_filt_d))/de_filt_sum;
            }
                  
            filter_coef_idx ++;
         }
      }

      if (de.max_filter_index>0)
         break;
   }

   if (de.max_filter_index==0)
      de.max_filter_index = de_max_ind-1;

   
   return(de);
}                       

double flam3_create_temporal_filter(int numsteps, int filter_type, double filter_exp, double filter_width,
                                    double **temporal_filter, double **temporal_deltas) {

   double maxfilt = 0.0;
   double sumfilt = 0.0;
   double slpx,halfsteps;
   double *deltas, *filter;
   
   int i;

   /* Allocate memory - this must be freed in the calling routine! */   
   deltas = (double *)malloc(numsteps*sizeof(double));
   filter = (double *)malloc(numsteps*sizeof(double));
   
   /* Deal with only one step */
   if (numsteps==1) {
      deltas[0] = 0;
      filter[0] = 1.0;
      *temporal_deltas = deltas;
      *temporal_filter = filter;
      return(1.0);
   }
      
   /* Define the temporal deltas */   
   for (i = 0; i < numsteps; i++)
      deltas[i] = ((double)i /(double)(numsteps - 1) - 0.5)*filter_width;
      
   /* Define the filter coefs */
   if (flam3_temporal_exp == filter_type) {

      for (i=0; i < numsteps; i++) {

         if (filter_exp>=0)
            slpx = ((double)i+1.0)/numsteps;
         else
            slpx = (double)(numsteps - i)/numsteps;

         /* Scale the color based on these values */
         filter[i] = pow(slpx,fabs(filter_exp));
         
         /* Keep the max */
         if (filter[i]>maxfilt)
            maxfilt = filter[i];
      }

   } else if (flam3_temporal_gaussian == filter_type) {

      halfsteps = numsteps/2.0;
      for (i=0; i < numsteps; i++) {
      
         /* Gaussian */
         filter[i] = flam3_spatial_filter(flam3_gaussian_kernel,
                           flam3_spatial_support[flam3_gaussian_kernel]*fabs(i - halfsteps)/halfsteps);
         /* Keep the max */
         if (filter[i]>maxfilt)
            maxfilt = filter[i];
      }
      
   } else { // (flam3_temporal_box)

      for (i=0; i < numsteps; i++)
         filter[i] = 1.0;
         
	   maxfilt = 1.0;
	   
   }

   /* Adjust the filter so that the max is 1.0, and */
   /* calculate the K2 scaling factor  */
   for (i=0;i<numsteps;i++) {
      filter[i] /= maxfilt;
      sumfilt += filter[i];
   }
         
   sumfilt /= numsteps;
   
   *temporal_deltas = deltas;
   *temporal_filter = filter;
   
   return(sumfilt);
}