{"record":{"id":"1b6fe09cb641ae31","repo":"Yalantis/uCrop","slug":"cimglist-s-fft-specified-real-and-imaginary","errorCode":null,"errorMessage":"CImgList<%s>::FFT(): Specified real and imaginary parts (%u,%u,%u,%u) have non 2^N dimension along the X-axis.","messagePattern":"CImgList<(.+?)>::FFT\\(\\): Specified real and imaginary parts \\(%u,%u,%u,%u\\) have non 2\\^N dimension along the X-axis\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":47780,"sourceCode":"              j = realc.offset(x,y,z);\n            realc[j] = (T)(a*ptrf[i]);\n            imagc[j] = (T)(a*ptrf[i + 1]);\n          }\n        }\n      }\n\n      fftw_destroy_plan(data_plan);\n      fftw_free(data_in);\n#ifndef cimg_use_fftw3_singlethread\n      fftw_cleanup_threads();\n#endif\n      cimg::mutex(12,0);\n#else\n      switch (_axis) {\n      case 'x' : { // Fourier along X, using built-in functions\n        const unsigned int N = real._width, N2 = N>>1;\n        if (((N - 1)&N) && N!=1)\n          throw CImgInstanceException(\"CImgList<%s>::FFT(): Specified real and imaginary parts (%u,%u,%u,%u) \"\n                                      \"have non 2^N dimension along the X-axis.\",\n                                      pixel_type(),\n                                      real._width,real._height,real._depth,real._spectrum);\n\n        for (unsigned int i = 0, j = 0; i<N2; ++i) {\n          if (j>i) cimg_forYZC(real,y,z,c) {\n              cimg::swap(real(i,y,z,c),real(j,y,z,c));\n              cimg::swap(imag(i,y,z,c),imag(j,y,z,c));\n              if (j<N2) {\n                const unsigned int ri = N - 1 - i, rj = N - 1 - j;\n                cimg::swap(real(ri,y,z,c),real(rj,y,z,c));\n                cimg::swap(imag(ri,y,z,c),imag(rj,y,z,c));\n              }\n            }\n          for (unsigned int m = N, n = N2; (j+=n)>=m; j-=m, m = n, n>>=1) {}\n        }\n        for (unsigned int delta = 2; delta<=N; delta<<=1) {\n          const unsigned int delta2 = delta>>1;","sourceCodeStart":47762,"sourceCodeEnd":47798,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L47762-L47798","documentation":"When FFTW3 is not available, CImg falls back to a built-in radix-2 FFT that only works when the dimension along the transform axis is a power of 2 (or exactly 1). For the X-axis it checks real._width via ((N-1)&N) && N!=1 and throws if width is not 2^N. This is a hard requirement of the built-in algorithm.","triggerScenarios":"Calling CImgList<T>::FFT()/CImg<T>::FFT() along the X-axis (axis='x') without cimg_use_fftw3 defined, on an image whose _width is not a power of two (e.g. 100, 333, 1000).","commonSituations":"Feeding camera frames or natural images of arbitrary width into FFT, forgetting to pad to a power of two, or a build that silently lacks FFTW3 so the fallback path is active.","solutions":["Pad the image width to the next power of 2 (e.g. with CImg::resize or zero-padding) before FFT.","Build CImg with cimg_use_fftw3 so arbitrary dimensions are supported by FFTW.","Crop the image to a power-of-two width if padding is unacceptable.","Verify image dimensions with code before calling FFT."],"exampleFix":"// before\nCImg<float> img(100, 100); img.FFT(); // width 100 is not 2^N\n// after\nCImg<float> img(100, 100);\nCImg<float> padded(img.get_resize(128, 128, -100, -100, 0));\npadded.FFT();","handlingStrategy":"validation","validationCode":"unsigned int w = img.width();\nbool isPow2 = w == 1 || ((w & (w - 1)) == 0);\nif (!isPow2) img.resize(nextPow2(w), img.height(), img.depth(), img.spectrum(), 0); // or reject","typeGuard":"bool isPowerOfTwo(unsigned int n) { return n == 1 || (n && !(n & (n - 1))); }","tryCatchPattern":"try { img.FFT(is_inverse, false, 'x'); }\ncatch (CImgInstanceException& e) { /* pad to power of two and retry */ }","preventionTips":["Pad width to a power of two before FFT when FFTW3 is not compiled in","Verify which build you ship: check cimg_use_fftw3 macro availability","Write a preprocessing step that resizes arbitrary images to 2^N dimensions"],"tags":["cimg","fft","power-of-two","image-dimensions"],"backgroundTag":"invalid-argument-value","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}