28#include "irplib_fft.h"
54irplib_image_find_shift(
const cpl_image *self,
55 const cpl_image *other,
59 const cpl_size nx = cpl_image_get_size_x(self);
60 const cpl_size ny = cpl_image_get_size_y(self);
61 const cpl_size type = cpl_image_get_type(self);
63 (size_t)(nx * ny) * cpl_type_get_sizeof(type | CPL_TYPE_COMPLEX);
69 cpl_error_code code = CPL_ERROR_NONE;
71 cpl_ensure_code(px != NULL, CPL_ERROR_NULL_INPUT);
72 cpl_ensure_code(py != NULL, CPL_ERROR_NULL_INPUT);
74 iml = cpl_imagelist_new();
76 CPL_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual);
77 cpl_imagelist_set(iml, (cpl_image *)self, 0);
78 cpl_imagelist_set(iml, (cpl_image *)other, 1);
81 fdata = cpl_calloc(2, bufsz);
83 fml = cpl_imagelist_new();
84 fself = cpl_image_wrap(nx, ny, type | CPL_TYPE_COMPLEX, fdata);
86 cpl_image_wrap(nx, ny, type | CPL_TYPE_COMPLEX, (
char *)fdata + bufsz);
88 cpl_imagelist_set(fml, fself, 0);
89 cpl_imagelist_set(fml, fother, 1);
91 if (cpl_fft_imagelist(fml, iml, CPL_FFT_FORWARD)) {
92 code = cpl_error_set_where(cpl_func);
96 cpl_size xmax = 1, ymax = 1;
99 cpl_image_wrap(nx, ny, type, cpl_image_get_data(fself));
102 cpl_image_conjugate(fother, fother);
103 cpl_image_multiply(fother, fself);
105 cpl_fft_image(imgpos, fother, CPL_FFT_BACKWARD | CPL_FFT_NOSCALE);
107 cpl_image_get_maxpos(imgpos, &xmax, &ymax);
109 (void)cpl_image_unwrap(imgpos);
116 *px = 2 * xmax >= nx ? xmax - nx : xmax;
117 *py = 2 * ymax >= ny ? ymax - ny : ymax;
120 cpl_imagelist_unwrap(iml);
121 cpl_image_unwrap(cpl_imagelist_unset(fml, 1));
122 cpl_imagelist_delete(fml);