28#include "irplib_fft.h"
53cpl_error_code irplib_image_find_shift(
const cpl_image * self,
54 const cpl_image * other,
58 const cpl_size nx = cpl_image_get_size_x(self);
59 const cpl_size ny = cpl_image_get_size_y(self);
60 const cpl_size type = cpl_image_get_type(self);
61 const size_t bufsz = (size_t)(nx * ny)
62 * cpl_type_get_sizeof(type | CPL_TYPE_COMPLEX);
68 cpl_error_code code = CPL_ERROR_NONE;
70 cpl_ensure_code(px != NULL, CPL_ERROR_NULL_INPUT);
71 cpl_ensure_code(py != NULL, CPL_ERROR_NULL_INPUT);
73 iml = cpl_imagelist_new();
75 CPL_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual);
76 cpl_imagelist_set(iml, (cpl_image *)self, 0);
77 cpl_imagelist_set(iml, (cpl_image *)other, 1);
80 fdata = cpl_calloc(2, bufsz);
82 fml = cpl_imagelist_new();
83 fself = cpl_image_wrap(nx, ny, type | CPL_TYPE_COMPLEX, fdata);
84 fother = cpl_image_wrap(nx, ny, type | CPL_TYPE_COMPLEX, (
char*)fdata
87 cpl_imagelist_set(fml, fself, 0);
88 cpl_imagelist_set(fml, fother, 1);
90 if (cpl_fft_imagelist(fml, iml, CPL_FFT_FORWARD)) {
91 code = cpl_error_set_where(cpl_func);
94 cpl_size xmax = 1, ymax = 1;
96 cpl_image * imgpos = cpl_image_wrap(nx, ny, type,
97 cpl_image_get_data(fself));
100 cpl_image_conjugate(fother, fother);
101 cpl_image_multiply(fother, fself);
103 cpl_fft_image(imgpos, fother, CPL_FFT_BACKWARD | CPL_FFT_NOSCALE);
105 cpl_image_get_maxpos(imgpos, &xmax, &ymax);
107 (void)cpl_image_unwrap(imgpos);
114 *px = 2 * xmax >= nx ? xmax - nx : xmax;
115 *py = 2 * ymax >= ny ? ymax - ny : ymax;
118 cpl_imagelist_unwrap(iml);
119 cpl_image_unwrap(cpl_imagelist_unset(fml, 1));
120 cpl_imagelist_delete(fml);