| Bsoft 2.0.5 Bernard's software package

gal_ricoI.cpp
Go to the documentation of this file.
1
10 #include "gal_ricoI.h"
11 #include "fft_tool.h"
12
13 #include "Bimage.h"
14 #include "utilities.h"
15
16 int polcar_f (Bimage * p, float *pfr, float *pfi, int bordo, int n_radii, int n_theta, kernel * ker, int padd);
17 int polcart_fourier (float *pfri, float r, float phi, float *val, int bordo, int n_radii, int n_theta, kernel * ker);
18 int padding (Bimage * p, Bimage * ppiano, int z);
19 int copy180 (Bimage * p, float *pfr, float *pfi, int bordo);
20 int shift_phase (Bimage * p, float npix);
21
22
35 Bimage *
36 gal_ricoI (Bimage * p, int tipo, kernel * ker, int padd)
37 {
38 int k;
39 int n_radii, n_theta;
40 float t;
41 float *pr, *pfr, *pfi, *pr1, *pk1;
42
43 int bordo = ker->bordo;
44
45 Bimage *prad = p->copy_header (0);
46 prad->sizeY(prad->sizeX());
47
48 float *pdest = (float *) malloc (prad->sizeX() * prad->sizeY() * prad->sizeZ() * sizeof (float));
49 prad->data_assign((unsigned char *) pdest);
50
51 float *psource = (float *) p->data_pointer();
52
53 int pf_x = p->sizeX() / 2 + 2 * ker->bordo;
54 int pf_y = p->sizeY() + 2 * ker->bordo;
55 if (padd)
56 pf_x = p->sizeX() * 2 * padd + 2 * ker->bordo;
57
58 if ((pfr = (float *) malloc (2 * pf_x * pf_y * sizeof (float))) == 0)
59 {
60 perror ("sin_gal: pfr error calloc");
61 exit (0);
62 }
63 pfi = pfr + pf_x * pf_y;
64
65 Bimage *ppiano = new Bimage (Float, TSimple, p->sizeX() * (padd + 1), p->sizeY(), 1, 1);
66 float *piano;
67
68 Bimage *ppol = new Bimage (Float, TSimple, prad->sizeX(), prad->sizeY(), 1, 1);
69
70 char order[12] = "yxz";
71 for (k = 0; k < prad->sizeZ(); k++)
72 {
73 pr = psource + p->sizeX() * p->sizeY() * k;
74 piano = (float *) ppiano->data_pointer();
75
76 if (padd)
77 padding (p, ppiano, k);
78 else
79 for (pr1 = pr, pk1 = piano; pr1 < pr + p->sizeX() * p->sizeY(); pr1++, pk1++)
80 *pk1 = *pr1;
81
82 if (tipo != 4)
83 ppiano->sizeY(ppiano->sizeY() / 2);
84 ppiano->reslice(order);
85
86 fft_1D_forward (ppiano);
87
88 shift_phase (ppiano, (float) (ppiano->sizeY() / 2));
89
90 memset ((char *) pfr, 0, 2 * pf_x * pf_y * sizeof (float));
91
92 copy180 (ppiano, pfr, pfi, bordo);
93
94 n_theta = ppiano->sizeX();
95 n_radii = ppiano->sizeY() / 2;
96
97 // swap(ppiano->x, ppiano->y);
98 t = ppiano->sizeX();
99 ppiano->sizeX(ppiano->sizeY());
100 ppiano->sizeY(t);
101
102 memset (ppol->data_pointer(), 0, prad->sizeX() * prad->sizeY() * sizeof (float));
103 polcar_f (ppol, pfr, pfi, bordo, n_radii, n_theta, ker, padd);
104
105 rephase_orig (ppol);
106
107 ritoab (ppol);
108
109 zeroes (ppol);
110
111 fft_2D_backward (ppol);
112
113 pr = (float *) ppol->data_pointer();
114
115 if (padd)
116 for (pr1 = pr; pr1 < pr + prad->sizeX() * prad->sizeY(); pr1++)
117 *pr1 *= 2. * padd;
118
119 memcpy (pdest + prad->sizeX() * prad->sizeY() * k, pr, prad->sizeX() * prad->sizeY() * sizeof (float));
120
121 if (tipo != 4)
122 {
123 ppiano->sizeY(ppiano->sizeY() * 2);
124 ppiano->data_assign((unsigned char *)realloc (ppiano->data_pointer(), ppiano->sizeX() * ppiano->sizeY() * sizeof (float)));
125 }
126 }
127
128 delete ppol;
129 delete ppiano;
130 free ((float *) pfr);
131
132 return (prad);
133 }
134
147 int
148 polcar_f (Bimage * p, float *pfr, float *pfi, int bordo, int n_radii, int n_theta, kernel * ker, int padd)
149 {
150 int i, j, hx, hy;
151 float r, r2, r2max, phi, vr, vi, memo, round;
152 float vr1, vi1;
153 float *pr1;
154
155 float *pr = (float *) p->data_pointer();
156
157 hx = p->sizeX() / 2;
158 hy = p->sizeY() / 2;
159 round = 1. / (2 * ker->nkern);
160 r2max = (hx - 1) * (hx - 1);
161 for (i = -hy + 1; i < hy; i++)
162 for (j = 0; j < hx; j++)
163 {
164 r2 = i * i + j * j;
165 if (r2 <= r2max)
166 {
167 if (j != 0)
168 phi = atan2 ((double) i, (double) j);
169 else if (i >= 0)
170 phi = +M_PI / 2.0;
171 else
172 phi = -M_PI / 2.0;
173 r = sqrt (r2);
174 if (padd)
175 r *= 2 * padd;
176 r += round;
177 if (phi < 0)
178 phi += 2 * M_PI;
179 if (phi >= 2 * M_PI)
180 phi -= 2 * M_PI;
181 polcart_fourier (pfr, r, phi, &vr1, bordo, n_radii, n_theta, ker);
182 polcart_fourier (pfi, r, phi, &vi1, bordo, n_radii, n_theta, ker);
183 memo = 1;
184 if (phi >= M_PI)
185 memo = -1;
186 vr = vr1;
187 vi = vi1 * memo;
188 if (i >= 0)
189 {
190 pr1 = pr + 2 * j * p->sizeX() + 2 * i;
191 *pr1 = vr;
192 pr1 += p->sizeX();
193 *pr1 = vi;
194 if (i == 0)
195 {
196 *++pr1 = vr;
197 pr1 -= p->sizeX();
198 *pr1 = vi;
199 }
200 }
201 else
202 {
203 pr1 = pr + 2 * j * p->sizeX() - 2 * i + 1;
204 *pr1 = vi;
205 pr1 += p->sizeX();
206 *pr1 = vr;
207 }
208 }
209 }
210
211 return 0;
212 }
213
226 int
227 polcart_fourier (float *pfri, float r, float phi, float *val, int bordo, int n_radii, int n_theta, kernel * ker)
228 {
229 int ncol = n_theta + 2 * bordo;
230 float *itbx, *itby;
231 double x, y, toty, totx, xyt, f512 = ker->nkern;
232 int i, j, k, rnk = ker->nk, nkm1;
233 float *pf1, *pf2;
234
235 if (phi < 0)
236 phi += 2 * M_PI;
237 if (phi > 2 * M_PI)
238 phi -= 2 * M_PI;
239 if (phi >= M_PI)
240 phi -= M_PI;
241 x = phi * (float) n_theta / M_PI + bordo;
242 y = r + bordo;
243 x += 1. / (2. * ker->nkern);
244
245 if (bordo == 1)
246 {
247 i = (int) floor (x);
248 j = (int) floor (y);
249 totx = x - i;
250 toty = y - j;
251 pf1 = pfri + i + j * ncol;
252 *val = pf1[0] * (1 - totx) * (1 - toty) + pf1[1] * totx * (1 - toty) + pf1[ncol] * (1 - totx) * toty + pf1[ncol + 1] * totx * toty;
253 }
254 else
255 {
256 if (ker->nk % 2 == 0)
257 nkm1 = ker->nk / 2 - 1;
258 else
259 nkm1 = (ker->nk - 1) / 2;
260 xyt = floor (y);
261 j = (int) floor (f512 * (y - xyt));
262 j *= rnk;
263 itby = ker->kern + j;
264 i = (int) xyt;
265 i -= nkm1;
266
267 pf1 = pfri + i * ncol;
268
269 xyt = floor (x);
270 j = (int) floor (f512 * (x - xyt));
271 j *= rnk;
272 itbx = ker->kern + j;
273 i = (int) xyt;
274 i -= nkm1;
275 pf1 = pf1 + i;
276
277 pf2 = pf1;
278 toty = 0;
279 for (j = 0; j < rnk; j++)
280 {
281 totx = 0;
282 for (k = 0; k < rnk; k++)
283 {
284 totx += *itbx * *pf2++;
285 itbx++;
286 }
287 toty += *itby * totx;
288 itby++;
289 itbx -= rnk;
290 pf2 += ncol;
291 pf2 -= rnk;
292 }
293 *val = toty;
294 }
295 return 0;
296 }
297
298
306 int
307 padding (Bimage * p, Bimage * ppiano, int z)
308 {
309 float *pk1, *pr1, *pk2;
310 int i, j, offset;
311
312 float *pr = (float *) p->data_pointer();
313 float *piano = (float *) ppiano->data_pointer();
314
315 memset (piano, 0, ppiano->sizeX() * ppiano->sizeY() * sizeof (float));
316 offset = (ppiano->sizeX() - p->sizeX()) / 2;
317 pk1 = piano + offset;
318 pr1 = pr + z * p->sizeX() * p->sizeY();
319 for (i = 0; i < p->sizeY(); i++)
320 {
321 pk2 = pk1;
322 for (j = 0; j < p->sizeX(); j++)
323 *pk2++ = *pr1++;
324 pk1 += ppiano->sizeX();
325 }
326
327 return 0;
328 }
329
338 int
339 copy180 (Bimage * p, float *pfr, float *pfi, int bordo)
340 {
341 float *pr1, *pf1, *pr2, *pf2;
342 int i, j, ncol, nrow;
343
344 float *pr = (float *) p->data_pointer();
345
346 ncol = p->sizeX() + 2 * bordo;
347 nrow = p->sizeY() / 2 + 2 * bordo;
348
349 pf1 = pfr + bordo + bordo * ncol;
350 for (pr1 = pr; pr1 < pr + p->sizeX(); pr1 += 2 * p->sizeX())
351 {
352 pf2 = pf1;
353 for (pr2 = pr1; pr2 < pr1 + p->sizeX(); pr2++)
354 *pf2++ = *pr2;
355 pf1 += ncol;
356 }
357
358 for (pr1 = pr + 2 * p->sizeX(); pr1 < pr + p->sizeX() * p->sizeY(); pr1 += 2 * p->sizeX())
359 {
360 pf2 = pf1;
361 for (pr2 = pr1; pr2 < pr1 + p->sizeX(); pr2++)
362 *pf2++ = *pr2;
363 pf1 += ncol;
364 }
365
366 pf1 = pfi + bordo + bordo * ncol;
367 for (pr1 = pr + p->sizeX(); pr1 < pr + 2 * p->sizeX(); pr1 += 2 * p->sizeX())
368 {
369 pf2 = pf1;
370 for (pr2 = pr1; pr2 < pr1 + p->sizeX(); pr2++)
371 *pf2++ = 0.;
372 pf1 += ncol;
373 }
374
375 for (pr1 = pr + 3 * p->sizeX(); pr1 < pr + p->sizeX() * p->sizeY(); pr1 += 2 * p->sizeX())
376 {
377 pf2 = pf1;
378 for (pr2 = pr1; pr2 < pr1 + p->sizeX(); pr2++)
379 *pf2++ = *pr2;
380 pf1 += ncol;
381 }
382
383 for (i = 1; i <= bordo; i++)
384 {
385 pf1 = pfr + bordo + (bordo - i) * ncol;
386 pf2 = pfr + bordo + (bordo + i) * ncol;
387 for (j = 0; j < p->sizeX(); j++)
388 *pf1++ = *pf2++;
389 }
390 for (i = 0; i < bordo; i++)
391 {
392 pf1 = pfr + bordo + (bordo + p->sizeY() / 2 - 1 - i) * ncol;
393 pf2 = pfr + bordo + (bordo + p->sizeY() / 2 + i) * ncol;
394 for (j = 0; j < p->sizeX(); j++)
395 *pf2++ = 0.;
396 }
397 for (j = 0; j < nrow; j++)
398 {
399 pf1 = pfr + j * ncol;
400 pf2 = pfr + j * ncol + p->sizeX();
401 for (i = 0; i < bordo; i++)
402 *pf1++ = *pf2++;
403 }
404 for (j = 0; j < nrow; j++)
405 {
406 pf1 = pfr + j * ncol + p->sizeX() + bordo;
407 pf2 = pfr + j * ncol + bordo;
408 for (i = 0; i < bordo; i++)
409 *pf1++ = *pf2++;
410 }
411
412 for (i = 1; i <= bordo; i++)
413 {
414 pf1 = pfi + bordo + (bordo - i) * ncol;
415 pf2 = pfi + bordo + (bordo + i) * ncol;
416 for (j = 0; j < p->sizeX(); j++)
417 *pf1++ = -*pf2++;
418 }
419 for (i = 0; i < bordo; i++)
420 {
421 pf1 = pfi + bordo + (bordo + p->sizeY() / 2 - 1 - i) * ncol;
422 pf2 = pfi + bordo + (bordo + p->sizeY() / 2 + i) * ncol;
423 for (j = 0; j < p->sizeX(); j++)
424 *pf2++ = 0.;
425 }
426 for (j = 0; j < nrow; j++)
427 {
428 pf1 = pfi + j * ncol;
429 pf2 = pfi + j * ncol + p->sizeX();
430 for (i = 0; i < bordo; i++)
431 *pf1++ = -*pf2++;
432 }
433 for (j = 0; j < nrow; j++)
434 {
435 pf1 = pfi + j * ncol + p->sizeX() + bordo;
436 pf2 = pfi + j * ncol + bordo;
437 for (i = 0; i < bordo; i++)
438 *pf1++ = -*pf2++;
439 }
440 return 0;
441 }
442
449 int
450 shift_phase (Bimage * p, float npix)
451 {
452 float *pr0, *pr1, *pi1, *ptot, *pcol;
453 double a, cs, sn, cs1, sn1;
454 double ft, ang;
455
456 float *pr = (float *) p->data_pointer();
457
458 ft = npix;
459 ft *= M_PI / (p->sizeY() / 2);
460 ang = ft;
461
462 cs1 = cos (ang);
463 sn1 = sin (ang);
464 cs = cs1;
465 sn = sn1;
466 ptot = pr + p->sizeX() * p->sizeY();
467 for (pr0 = pr + 2 * p->sizeX(); pr0 < ptot; pr0 += 2 * p->sizeX())
468 {
469 pcol = pr0 + p->sizeX();
470 for (pr1 = pr0; pr1 < pcol; pr1++)
471 {
472 pi1 = pr1 + p->sizeX();
473 a = *pr1 * cs - *pi1 * sn;
474 *pi1 = *pr1 * sn + *pi1 * cs;
475 *pr1 = a;
476 }
477 a = cs * cs1 - sn * sn1;
478 sn = cs * sn1 + sn * cs1;
479 cs = a;
480 }
481 return 0;
482 }
kernel::nk
int nk
Definition: kernlut.h:15
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:650
fft_tool.h
Bimage.h
Header file for image class.
polcart_fourier
int polcart_fourier(float *pfri, float r, float phi, float *val, int bordo, int n_radii, int n_theta, kernel *ker)
Definition: gal_ricoI.cpp:227
shift_phase
int shift_phase(Bimage *p, float npix)
Definition: gal_ricoI.cpp:450
utilities.h
Header file for general utilities.
copy180
int copy180(Bimage *p, float *pfr, float *pfi, int bordo)
Definition: gal_ricoI.cpp:339
kernel::nkern
int nkern
Definition: kernlut.h:17
polcar_f
int polcar_f(Bimage *p, float *pfr, float *pfi, int bordo, int n_radii, int n_theta, kernel *ker, int padd)
Definition: gal_ricoI.cpp:148
zeroes
int zeroes(Bimage *p)
Definition: fft_tool.cpp:373
fft_2D_backward
int fft_2D_backward(Bimage *p)
Definition: fft_tool.cpp:55
TSimple
Definition: Bimage.h:58
Float
Definition: utilities.h:157
Bimage
General image parameter class.
Definition: Bimage.h:163
M_PI
#define M_PI
Definition: utilities.h:59
Bimage::data_assign
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
Definition: Bimage.cpp:852
gal_ricoI.h
padding
int padding(Bimage *p, Bimage *ppiano, int z)
Padds an image.
Definition: gal_ricoI.cpp:307
Bimage::reslice
void reslice(const char *order)
Definition: Bimage.h:709
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:261
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:377
fft_1D_forward
int fft_1D_forward(Bimage *p)
Definition: fft_tool.cpp:17
gal_ricoI
Bimage * gal_ricoI(Bimage *p, int tipo, kernel *ker, int padd)
Reconstruct an image from a sinogram.
Definition: gal_ricoI.cpp:36
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:379
kernel::bordo
int bordo
Definition: kernlut.h:19
kernel
Definition: kernlut.h:13
ritoab
int ritoab(Bimage *p)
Changes direct Fourier transform representation.
Definition: fft_tool.cpp:332
Bimage::sizeY
void sizeY(long ny)
Definition: Bimage.h:378
kernel::kern
float * kern
Definition: kernlut.h:20
rephase_orig
int rephase_orig(Bimage *p)
Shifting the origin by half the complex image size.
Definition: fft_tool.cpp:407