-
Notifications
You must be signed in to change notification settings - Fork 1
/
fourier_alpha.cpp
418 lines (361 loc) · 9.15 KB
/
fourier_alpha.cpp
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
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <jpeglib.h>
#include <typeinfo>
#include <string.h>
#include <unistd.h>
#include <bits/stdc++.h>
#include <limits>
#include <cfloat>
#include "image.hpp"
//Function to check if a number is a power of 2.
//Returns 1 if it is.
int ispow2(int num)
{
if((num & (num-1)) == 0)
return 1;
return 0;
}
/*
This computes an in-place complex-to-complex FFT
x and y are the real and imaginary arrays of 2^m points.
dir = 1 gives forward transform
dir = -1 gives reverse transform
*/
void FFT(int dir, int m, double *x, double *y)
{
long nn,i,i1,j,k,i2,l,l1,l2;
double c1,c2,tx,ty,t1,t2,u1,u2,z;
nn = 1;
for (i = 0; i < m; i++)
nn *= 2;
/* Do the bit reversal */
i2 = nn >> 1;
j = 0;
for (i = 0; i < nn-1; i++)
{
if (i < j)
{
tx = x[i];
ty = y[i];
x[i] = x[j];
y[i] = y[j];
x[j] = tx;
y[j] = ty;
}
k = i2;
while (k <= j)
{
j -= k;
k >>= 1;
}
j += k;
}
/* Compute the FFT */
c1 = -1.0;
c2 = 0.0;
l2 = 1;
for (l = 0; l < m; l++)
{
l1 = l2;
l2 <<= 1;
u1 = 1.0;
u2 = 0.0;
for (j = 0; j < l1; j++)
{
for (i = j; i < nn; i+=l2)
{
i1 = i + l1;
t1 = u1 * x[i1] - u2 * y[i1];
t2 = u1 * y[i1] + u2 * x[i1];
x[i1] = x[i] - t1;
y[i1] = y[i] - t2;
x[i] += t1;
y[i] += t2;
if(isnan(x[i1]))
x[i1] = 0.0;
if(isnan(y[i1]))
y[i1] = 0.0;
if(isnan(x[i]))
x[i] = 0.0;
if(isnan(y[i]))
y[i] = 0.0;
}
z = u1 * c1 - u2 * c2;
u2 = u1 * c2 + u2 * c1;
u1 = z;
}
c2 = sqrt((1.0 - c1) / 2.0);
if (dir == 1)
c2 = -c2;
c1 = sqrt((1.0 + c1) / 2.0);
}
/* Scaling for forward transform */
if (dir == 1)
{
for (i = 0; i<nn; i++)
{
x[i] /= (double)nn;
y[i] /= (double)nn;
}
}
}
//Function to do 2D FFT inplace given real and imaginary part arrays
//dir same as that for FFT above.
void doTransform(int dir, double *reals, double *imags, int width, int height, int m_row, int m_col)
{
double *realrow = new double[width];
double *imagrow = new double[width];
int i, j;
for(i=0; i<height; ++i)
{
for(j=0; j<width; ++j)
{
realrow[j] = reals[j+i*width];
imagrow[j] = imags[j+i*width];
}
FFT(dir, m_row, realrow, imagrow);
for(j=0; j<width; ++j)
{
reals[j+i*width] = realrow[j];
imags[j+i*width] = imagrow[j];
}
}
delete[] realrow;
delete[] imagrow;
// printf("Finished row transform\n");
double *realcol = new double[height];
double *imagcol = new double[height];
for(i=0; i<width; ++i)
{
for(j=0; j<height; ++j)
{
realcol[j] = reals[i+j*width];
imagcol[j] = imags[i+j*width];
}
FFT(dir, m_col, realrow, imagrow);
for(j=0; j<height; ++j)
{
reals[i+j*width] = realcol[j];
imags[i+j*width] = imagcol[j];
}
}
delete[] realcol;
delete[] imagcol;
// printf("Finished column transform\n");
}
//Function to make the double array after the forward transform to an Image object(unsigned char array) that can be saved as a jpeg
Image convertForwardTransform(double *mat, int size, int width, int height, double min, double max)
{
int i1=0,j1=0,i,j;
unsigned char *temp = new unsigned char[size];
double mid = (max + min)/2.0;
for(i=0; i<size; ++i)
{
temp[i] = floor((mat[i] - mid)*255);
if(temp[i]<0)
temp[i] = 0;
else if(temp[i]>255)
temp[i] = 255;
}
unsigned char *temp2 = (unsigned char*)calloc(size*2, sizeof(unsigned char));
for(i=0; i<height; ++i){
for(j=0; j<width;++j){
if(i<height/2)
i1= height/2 - i;
else
i1 = (3*height)/2 - i;
if(j<width/2)
j1 = width/2 - j;
else
j1 = (3*width)/2 - j;
temp2[i1*width+j1] = temp[i*width+j];
}
}
free(temp);
Image conv = Image(width, height, 1, temp2);
free(temp2);
return conv;
}
//Function to make the double array after inverse transform to an Image object(unsigned char array) that can be saved as a jpeg
Image convertInverseTransform(double *mat, int size, int width, int height, double min, double max, int orig_width, int orig_height)
{
int i,j;
unsigned char *temp = new unsigned char[orig_width*orig_height];
double maximum = (abs(max)>abs(min))?abs(max):abs(min);
for(i=0; i<orig_height; ++i)
{
for(j=0; j<orig_width; ++j)
{
temp[j+i*orig_width] = floor((abs(mat[j+i*width])*255)/maximum);
}
}
Image conv = Image(orig_width, orig_height, 1, temp);
free(temp);
return conv;
}
//Function to find the minimum and maximum values for the double arrays.
//Output used in the transform to Image functions above (convertForwardTransform and convertTransform)
void findMinMax(double *mat, int size, double *min, double *max)
{
int i;
for(i=0; i<size; ++i)
{
if(mat[i] > *max)
*max = mat[i];
else if(mat[i] < *min)
*min = mat[i];
}
}
//Function to perform the 2D transform and return real and imaginary
int FFTFromImage(Image img, int m_row, int m_col, int orig_width, int orig_height)
{
if(!ispow2(img.width) || !ispow2(img.height))
{
return -1;
}
if(img.components!=1)
{
return -2;
}
int width = img.width;
int height = img.height;
int size = width*height;
double *reals = new double[size];
double *imags = new double[size];
int i,j;
double c;
for(i=0; i<size; ++i)
{
c = (double)img.image[i];
if(c>255.0)
c=255.0;
else if(c<0.0)
c=0.0;
reals[i] = c;
}
char *path_forward_real = (char *)calloc(200, sizeof(char));
char *path_forward_imag = (char *)calloc(200, sizeof(char));
char *path_inverse = (char *)calloc(200, sizeof(char));
// strcat(path_forward_real, "uploads/");
// strcat(path_forward_imag, "uploads/");
// strcat(path_inverse, "uploads/");
// int l = strlen(path_forward_real);
double max_real = DBL_MIN;
double min_real = DBL_MAX;
double max_imag = DBL_MIN;
double min_imag = DBL_MAX;
try
{
for(i=0; !(img.filename[i] == '.' && img.filename[i+1] == 'j'); ++i)
{
path_forward_real[i] = img.filename[i];
path_forward_imag[i] = img.filename[i];
path_inverse[i] = img.filename[i];
}
path_forward_real[i] = '\0';
path_forward_imag[i] = '\0';
path_inverse[i] = '\0';
// printf("%s\n", path_forward_real);
doTransform(1, reals, imags, width, height, m_row, m_col);
findMinMax(reals, size, &min_real, &max_real);
findMinMax(imags, size, &min_imag, &max_imag);
Image realpart = convertForwardTransform(reals, size, width, height, min_real, max_real);
const char *savename_real_forward = strcat(path_forward_real, "_forward_real.jpg");
realpart.write_jpeg(savename_real_forward, GRAYSCALE);
printf("%s\n", savename_real_forward);
Image imagpart = convertForwardTransform(imags, size, width, height, min_imag, max_imag);
const char *savename_imag_forward = strcat(path_forward_imag, "_forward_imag.jpg");
imagpart.write_jpeg(savename_imag_forward, GRAYSCALE);
printf("%s\n", savename_imag_forward);
doTransform(-1, reals, imags, width, height, m_row, m_col);
max_real = DBL_MIN;
min_real = DBL_MAX;
findMinMax(reals, size, &min_real, &max_real);
Image realpart_inv = convertInverseTransform(reals, size, width, height, min_real, max_real, orig_width, orig_height);
const char *savename_real_inverse = strcat(path_inverse, "_inverse_real.jpg");
realpart_inv.write_jpeg(savename_real_inverse, GRAYSCALE);
printf("%s\n", savename_real_inverse);
}
catch(int x)
{
return x;
}
return 0;
}
void closest_m(Image img, int &width, int &height)
{
double val = img.width;
double logval = log2(val);
double intval;
double fracval = modf(logval, &intval);
if(intval-fracval == intval)
width = intval;
else
width = intval+1;
val = img.height;
logval = log2(val);
fracval = modf(logval, &intval);
if(intval-fracval == intval)
height = intval;
else
height = intval+1;
if(width>height)
height = width;
else if(height>width)
width = height;
}
Image getpow2img(Image orig, int width_exp, int height_exp)
{
// printf("%d\t%d\n", width_exp, height_exp);
int width = floor(pow(2,width_exp));
int height = floor(pow(2,height_exp));
// printf("%d\t%d\n", width, height);
if(width == orig.width && height == orig.height)
return orig;
Image pow2 = Image(width, height, orig.components);
if(orig.filename != NULL && strlen(orig.filename)>0)
{
pow2.filename = new char[strlen(orig.filename)];
strcpy(pow2.filename, orig.filename);
}
int i,j;
int size = width*height*orig.components, orig_width = orig.width, orig_height = orig.height;
for(i=0; i<size; ++i)
pow2.image[i] = 0;
for(i=0; i<orig_height; ++i)
for(j=0; j<orig_width; ++j)
pow2.image[j+i*width] = orig.image[j+i*orig_width];
return pow2;
}
int main(int argc, char *argv[])
{
if(argc!=2)
{
printf("Please enter input filename as argument.\n");
return -1;
}
const char *filepath = argv[1];
double *reals = NULL;
double *imags = NULL;
int dir = 1;
Image orig_img = Image();
try
{
int size = strlen(filepath);
orig_img.filename = new char[size];
strcpy(orig_img.filename, filepath);
orig_img.read_jpeg();
}
catch(int x)
{
return x;
}
int dimX=0, dimY=0;
closest_m(orig_img, dimX, dimY);
Image img = getpow2img(orig_img, dimX, dimY);
int status = FFTFromImage(img, dimX, dimY, orig_img.width, orig_img.height);
printf("%d\n", status);
return status;
}