Normand Briere
2019-09-06 9cc83b97378c48bae3792064f2d01b2f954c0e01
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
 
import java.lang.Math.*;
import java.awt.*;
 
/**
 * The FFT class contains methods to apply the 2D FFT to a
 * TwoDArray.
 *
 * @author Simon Horne
 */
public class FFT
{
 
    /**
     * Data structure to hold the input to the algorithm.
     */
    public TwoDArray input;
    /**
     * Data structure to hold the intermediate results of the algorithm.
     * After applying the 1D FFT to the columns but before the rows.
     */
    public TwoDArray intermediate;
    /**
     * Data structure to hold the ouput of the algorithm.
     */
    public TwoDArray output;
    public int progress;
 
    /**
     * Default no argument constructor.
     */
    public FFT()
    {
    }
 
    /** 
     * Constructor to set up an FFT object and then automatically 
     * apply the FFT algorithm.
     *
     * @param pixels  int array containing the image data.
     * @param w  The width of the image in pixels.
     * @param h  The height of the image in pixels.
     */
    public FFT(double[] pixels, int w, int h)
    {
        progress = 0;
        input = new TwoDArray(pixels, w, h);
        intermediate = new TwoDArray(pixels, w, h);
        output = new TwoDArray(pixels, w, h);
        transform();
    }
 
    static ComplexNumber cTwo = new ComplexNumber(2, 0);
    /**
     * Method to recursively apply the 1D FFT to a ComplexNumber array.
     *
     * @param  x  A ComplexNumber array containing a row or a column of
     * image data.
     * @return A ComplexNumber array containing the result of the 1D FFT.
     */
    static ComplexNumber[] recursiveFFT(ComplexNumber[] x)
    {
        ComplexNumber z1, z2, z3, z4, tmp;
        int n = x.length;
        int m = n / 2;
        ComplexNumber[] result = new ComplexNumber[n];
        ComplexNumber[] even = new ComplexNumber[m];
        ComplexNumber[] odd = new ComplexNumber[m];
        ComplexNumber[] sum = new ComplexNumber[m];
        ComplexNumber[] diff = new ComplexNumber[m];
        if (n == 1)
        {
            result[0] = x[0];
        } else
        {
            z1 = new ComplexNumber(0.0, -2 * (Math.PI) / n);
            tmp = ComplexNumber.cExp(z1);
            z1 = new ComplexNumber(1.0, 0.0);
            for (int i = 0; i < m; ++i)
            {
                z3 = ComplexNumber.cSum(x[i], x[i + m]);
                sum[i] = ComplexNumber.cDiv(z3, cTwo);
 
                z3 = ComplexNumber.cDif(x[i], x[i + m]);
                z4 = ComplexNumber.cMult(z3, z1);
                diff[i] = ComplexNumber.cDiv(z4, cTwo);
 
                z2 = ComplexNumber.cMult(z1, tmp);
                z1 = new ComplexNumber(z2);
            }
            even = recursiveFFT(sum);
            odd = recursiveFFT(diff);
 
            for (int i = 0; i < m; ++i)
            {
                result[i * 2] = new ComplexNumber(even[i]);
                result[i * 2 + 1] = new ComplexNumber(odd[i]);
            }
        }
        return result;
    }
 
    /**
     * Method to apply the 2D FFT by applying the recursive 1D FFT to the
     * columns and then the rows of image data.
     */
    void transform()
    {
 
        for (int i = 0; i < input.size; ++i)
        {
            progress++;
            intermediate.putColumn(i, recursiveFFT(input.getColumn(i)));
        }
        for (int i = 0; i < intermediate.size; ++i)
        {
            //System.out.println("i = " + i);
            progress++;
            output.putRow(i, recursiveFFT(intermediate.getRow(i)));
        }
    /*
    for(int j=0;j<output.values.length;++j){
    for(int i=0;i<output.values[0].length;++i){
    intermediate.values[i][j] = output.values[i][j];
    input.values[i][j] = output.values[i][j];
    }
    }
     */
    }
 
    public int[] getPixels()
    {
//    return toPixels(logs(intermediate.DCToCentre(intermediate.getMagnitude())));
        return toPixels(logs(output.DCToCentre(output.getMagnitude())));
    }
 
    /**
     * A method to convert an array of doubles to an array of their log values.
     *
     * @param values An array of doubles (all positive).
     * @return An array of doubles.
     */
    private double[] logs(double[] values)
    {
        double[] output = new double[values.length];
        /*
        for(int i=0;i<values.length;++i){
        values[i] = values[i]*1024*1024;
        }
        double c = 255/Math.log(1+maxValue(values));
         * */
        for (int i = 0; i < values.length; ++i)
        {
//      output[i] = c * Math.log(1+values[i]);
            output[i] = 127 * Math.log(1 + values[i]);
        }
        return output;
    }
 
    /** 
     * A method to convert an array of grey values to an array of pixel values.
     *
     * @param values An array of grey values (all positive).
     * @return An array of pixel values.
     */
    static int[] toPixels(double[] values)
    {
        int grey;
//    double [] greys = new double [values.length];
        int[] pixels = new int[values.length];
//    for(int i=0;i<greys.length;++i){
//      greys[i] = values[i];
//    }
//    double max = maxValue(greys);
//    double max = maxValue(values);
        double scale;
//    if (max == 0)
        scale = 1; // 1024.0*1024;
//    else scale = 255.0/max;
 
        //System.out.println(max);
//    for(int i=0;i<greys.length;++i){
        for (int i = 0; i < values.length; ++i)
        {
//      greys[i] = greys[i] * 255/max;
//      grey = (int) Math.round(greys[i]);
            grey = (int) /*Math.round*/ (values[i] * scale);
            if (grey < 0)
            {
                grey = 0;
            }
            pixels[i] = grey; // new Color(grey,grey,grey).getRGB(); //recombine greys
        }
        return pixels;
    }
 
    /**
     * Method to find the maximum value from an array of doubles.
     *
     * @param values an array of doubles.
     * @return The maximum value.
     */
    static private double maxValue(double[] values)
    {
        double max = values[0];
        for (int i = 1; i < values.length; ++i)
        {
            if (values[i] > max)
            {
                max = values[i];
            }
        }
        return max;
    }
 
    /**
     * Method to find the minimum value from an array of doubles.
     *
     * @param values an array of doubles.
     * @return The minimum value.
     */
    private double minValue(double[] values)
    {
        double min = values[0];
        for (int i = 1; i < values.length; ++i)
        {
            if (values[i] < min)
            {
                min = values[i];
            }
        }
        return min;
    }
 
    public int getProgress()
    {
        return progress;
    }
}