-
Notifications
You must be signed in to change notification settings - Fork 0
/
finterp.c
302 lines (262 loc) · 9.86 KB
/
finterp.c
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
/**
* @file finterp.c
* @brief Fourier zero-padding interpolation
* @author Pascal Getreuer <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <fftw3.h>
#include "basic.h"
#include "finterp.h"
/**
* @brief Boundary handling function for constant extension
* @param N is the data length
* @param i is an index into the data
* @return an index that is always between 0 and N - 1
*/
static int ConstExtension(int N, int i)
{
if(i < 0)
return 0;
else if(i >= N)
return N - 1;
else
return i;
}
/**
* @brief Boundary handling function for half-sample symmetric extension
* @param N is the data length
* @param i is an index into the data
* @return an index that is always between 0 and N - 1
*/
static int HSymExtension(int N, int i)
{
while(1)
{
if(i < 0)
i = -1 - i;
else if(i >= N)
i = (2*N - 1) - i;
else
return i;
}
}
/**
* @brief Boundary handling function for whole-sample symmetric extension
* @param N is the data length
* @param i is an index into the data
* @return an index that is always between 0 and N - 1
*/
static int WSymExtension(int N, int i)
{
while(1)
{
if(i < 0)
i = -i;
else if(i >= N)
i = (2*N - 2) - i;
else
return i;
}
}
int (*ExtensionMethod[3])(int, int) =
{ConstExtension, HSymExtension, WSymExtension};
static int FourierScaleScan(float *Dest,
int DestStride, int DestScanStride, int DestChannelStride, int DestScanSize,
const float *Src,
int SrcStride, int SrcScanStride, int SrcChannelStride, int SrcScanSize,
int NumScans, int NumChannels, float XStart, double PsfSigma,
boundaryhandling Boundary)
{
const int SrcPadScanSize = 2*(SrcScanSize
- ((Boundary == BOUNDARY_WSYMMETRIC) ? 1:0));
const int DestPadScanSize = (Boundary == BOUNDARY_HSYMMETRIC) ?
(2*DestScanSize)
: (2*DestScanSize - (int)floor((2.0f*DestScanSize)/SrcScanSize + 0.5f));
const int ReflectOffset = SrcPadScanSize
- ((Boundary == BOUNDARY_HSYMMETRIC) ? 1:0);
const int SrcDftSize = SrcPadScanSize/2 + 1;
const int DestDftSize = DestPadScanSize/2 + 1;
const int BufSpatialNumEl = DestPadScanSize*NumScans*NumChannels;
const int BufDftNumEl = 2*DestDftSize*NumScans*NumChannels;
float *BufSpatial = NULL, *BufDft = NULL, *Modulation = NULL, *Ptr;
fftwf_plan Plan = 0;
fftwf_iodim Dims[1], HowManyDims[1];
float Temp, Denom;
int i, Scan, Channel, Success = 0;
if((Boundary != BOUNDARY_HSYMMETRIC && Boundary != BOUNDARY_WSYMMETRIC)
|| !(BufSpatial = (float *)fftwf_malloc(sizeof(float)*BufSpatialNumEl))
|| !(BufDft = (float *)fftwf_malloc(sizeof(float)*BufDftNumEl)))
goto Catch;
if(XStart != 0)
{
if(!(Modulation = (float *)Malloc(sizeof(float)*2*DestDftSize)))
goto Catch;
for(i = 0; i < DestDftSize; i++)
{
Temp = (float)(M_2PI*XStart*i/SrcPadScanSize);
Modulation[2*i + 0] = (float)cos(Temp);
Modulation[2*i + 1] = (float)sin(Temp);
}
}
/* Fill BufSpatial with the input and symmetrize */
for(Channel = 0; Channel < NumChannels; Channel++)
{
for(Scan = 0; Scan < NumScans; Scan++)
{
for(i = 0; i < SrcScanSize; i++)
BufSpatial[i + SrcPadScanSize*(Scan + NumScans*Channel)]
= Src[SrcStride*i + SrcScanStride*Scan + SrcChannelStride*Channel];
for(; i < SrcPadScanSize; i++)
BufSpatial[i + SrcPadScanSize*(Scan + NumScans*Channel)]
= Src[SrcStride*(ReflectOffset - i)
+ SrcScanStride*Scan + SrcChannelStride*Channel];
}
}
/* Initialize DFT buffer to zeros (there is no "fftwf_calloc"). Note that
it is not safely portable to use memset for this purpose.
http://c-faq.com/malloc/calloc.html */
for(i = 0; i < BufDftNumEl; i++)
BufDft[i] = 0.0f;
/* Perform DFT real-to-complex transform */
Dims[0].n = SrcPadScanSize;
Dims[0].is = 1;
Dims[0].os = 1;
HowManyDims[0].n = NumScans*NumChannels;
HowManyDims[0].is = SrcPadScanSize;
HowManyDims[0].os = DestDftSize;
if(!(Plan = fftwf_plan_guru_dft_r2c(1, Dims, 1, HowManyDims, BufSpatial,
(fftwf_complex *)BufDft, FFTW_ESTIMATE | FFTW_DESTROY_INPUT)))
goto Catch;
fftwf_execute(Plan);
fftwf_destroy_plan(Plan);
if(PsfSigma == 0)
for(Channel = 0, Ptr = BufDft; Channel < NumChannels; Channel++)
for(Scan = 0; Scan < NumScans; Scan++, Ptr += 2*DestDftSize)
for(i = 0; i < SrcDftSize; i++)
{
Ptr[2*i + 0] /= SrcPadScanSize;
Ptr[2*i + 1] /= SrcPadScanSize;
}
else
{
/* Also divide by the Gaussian point spread function in this case */
Temp = (float)(SrcPadScanSize / (M_2PI * PsfSigma));
Temp = 2*Temp*Temp;
for(i = 0; i < SrcDftSize; i++)
{
if(i <= DestScanSize)
Denom = (float)exp(-(i*i)/Temp);
else
Denom = (float)exp(
-((DestPadScanSize - i)*(DestPadScanSize - i))/Temp);
Denom *= SrcPadScanSize;
for(Channel = 0; Channel < NumChannels; Channel++)
for(Scan = 0; Scan < NumScans; Scan++)
{
BufDft[2*(i + DestDftSize*(Scan + NumScans*Channel)) + 0] /= Denom;
BufDft[2*(i + DestDftSize*(Scan + NumScans*Channel)) + 1] /= Denom;
}
}
}
/* If XStart is nonzero, modulate the DFT to translate the result */
if(XStart != 0)
for(Channel = 0, Ptr = BufDft; Channel < NumChannels; Channel++)
for(Scan = 0; Scan < NumScans; Scan++, Ptr += 2*DestDftSize)
for(i = 0; i < SrcDftSize; i++)
{
/* Complex multiply */
Temp = Ptr[2*i + 0]*Modulation[2*i + 0]
- Ptr[2*i + 1]*Modulation[2*i + 1];
Ptr[2*i + 1] = Ptr[2*i + 0]*Modulation[2*i + 1]
+ Ptr[2*i + 1]*Modulation[2*i + 0];
Ptr[2*i + 0] = Temp;
}
/* Perform inverse DFT complex-to-real transform */
Dims[0].n = DestPadScanSize;
Dims[0].is = 1;
Dims[0].os = 1;
HowManyDims[0].n = NumScans*NumChannels;
HowManyDims[0].is = DestDftSize;
HowManyDims[0].os = DestPadScanSize;
if(!(Plan = fftwf_plan_guru_dft_c2r(1, Dims, 1, HowManyDims,
(fftwf_complex *)BufDft, BufSpatial,
FFTW_ESTIMATE | FFTW_DESTROY_INPUT)))
goto Catch;
fftwf_execute(Plan);
fftwf_destroy_plan(Plan);
/* Fill Dest with the result (and trim padding) */
for(Channel = 0; Channel < NumChannels; Channel++)
{
for(Scan = 0; Scan < NumScans; Scan++)
{
for(i = 0; i < DestScanSize; i++)
Dest[DestStride*i + DestScanStride*Scan + DestChannelStride*Channel]
= BufSpatial[i + DestPadScanSize*(Scan + NumScans*Channel)];
}
}
Success = 1;
Catch:
Free(Modulation);
if(BufDft)
fftwf_free(BufDft);
if(BufSpatial)
fftwf_free(BufSpatial);
fftwf_cleanup();
return Success;
}
/**
* @brief Scale image with Fourier zero padding
*
* @param Dest pointer to memory for holding the interpolated image
* @param DestWidth output image width
* @param XStart leftmost sample location (in input coordinates)
* @param DestHeight output image height
* @param YStart uppermost sample location (in input coordinates)
* @param Src the input image
* @param SrcWidth, SrcHeight, NumChannels input image dimensions
* @param PsfSigma Gaussian PSF standard deviation
* @param Boundary boundary handling
* @return 1 on success, 0 on failure.
*
* The image is first mirror folded with half-sample even symmetry to avoid
* boundary artifacts, then transformed with a real-to-complex DFT.
*
* The interpolation is computed so that Dest[m + DestWidth*n] is the
* interpolation of Input at sampling location
* (XStart + m*SrcWidth/DestWidth, YStart + n*SrcHeight/DestHeight)
* for m = 0, ..., DestWidth - 1, n = 0, ..., DestHeight - 1, where the
* pixels of Src are located at the integers.
*/
int FourierScale2d(float *Dest, int DestWidth, float XStart,
int DestHeight, float YStart,
const float *Src, int SrcWidth, int SrcHeight, int NumChannels,
double PsfSigma, boundaryhandling Boundary)
{
float *Buf = NULL;
unsigned long StartTime, StopTime;
int Success = 0;
if(!Dest || DestWidth < SrcWidth || DestHeight < SrcHeight || !Src
|| SrcWidth <= 0 || SrcHeight <= 0 || NumChannels <= 0 || PsfSigma < 0
|| !(Buf = (float *)Malloc(sizeof(float)*SrcWidth*DestHeight*3)))
return 0;
StartTime = Clock();
/* Scale the image vertically */
if(!FourierScaleScan(Buf, SrcWidth, 1, SrcWidth*DestHeight, DestHeight,
Src, SrcWidth, 1, SrcWidth*SrcHeight, SrcHeight,
SrcWidth, 3, YStart, PsfSigma, Boundary))
goto Catch;
/* Scale the image horizontally */
if(!FourierScaleScan(Dest, 1, DestWidth, DestWidth*DestHeight, DestWidth,
Buf, 1, SrcWidth, SrcWidth*DestHeight, SrcWidth,
DestHeight, 3, XStart, PsfSigma, Boundary))
goto Catch;
StopTime = Clock();
printf("CPU Time: %.3f s\n\n", 0.001*(StopTime - StartTime));
Success = 1;
Catch:
Free(Buf);
return Success;
}