{"record":{"id":"5f676f6380606e8a","repo":"Yalantis/uCrop","slug":"haar-sub-image-height-u-is-not-even","errorCode":null,"errorMessage":"haar(): Sub-image height %u is not even.","messagePattern":"haar\\(\\): Sub-image height %u is not even\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":47415,"sourceCode":"              for (unsigned int x = 0, xw = w, x2 = 0; x<w; ++x, ++xw) {\n                const Tfloat val0 = (Tfloat)(*this)(x,y,z,c), val1 = (Tfloat)(*this)(xw,y,z,c);\n                res(x2++,y,z,c) = (val0 - val1)/sqrt2;\n                res(x2++,y,z,c) = (val0 + val1)/sqrt2;\n              }\n            } else cimg_forYZC(*this,y,z,c) { // Direct transform along X\n              for (unsigned int x = 0, xw = w, x2 = 0; x<w; ++x, ++xw) {\n                const Tfloat val0 = (Tfloat)(*this)(x2++,y,z,c), val1 = (Tfloat)(*this)(x2++,y,z,c);\n                res(x,y,z,c) = (val0 + val1)/sqrt2;\n                res(xw,y,z,c) = (val1 - val0)/sqrt2;\n              }\n            }\n          } else return *this;\n        } break;\n        case 'y' : {\n          const unsigned int h = _height/2;\n          if (h) {\n            if ((h%2) && h!=1)\n              throw CImgInstanceException(_cimg_instance\n                                          \"haar(): Sub-image height %u is not even.\",\n                                          cimg_instance,\n                                          h);\n\n            res.assign(_width,_height,_depth,_spectrum);\n            if (invert) cimg_forXZC(*this,x,z,c) { // Inverse transform along Y\n              for (unsigned int y = 0, yh = h, y2 = 0; y<h; ++y, ++yh) {\n                const Tfloat val0 = (Tfloat)(*this)(x,y,z,c), val1 = (Tfloat)(*this)(x,yh,z,c);\n                res(x,y2++,z,c) = (val0 - val1)/sqrt2;\n                res(x,y2++,z,c) = (val0 + val1)/sqrt2;\n              }\n            } else cimg_forXZC(*this,x,z,c) {\n              for (unsigned int y = 0, yh = h, y2 = 0; y<h; ++y, ++yh) { // Direct transform along Y\n                const Tfloat val0 = (Tfloat)(*this)(x,y2++,z,c), val1 = (Tfloat)(*this)(x,y2++,z,c);\n                res(x,y,z,c)  = (val0 + val1)/sqrt2;\n                res(x,yh,z,c) = (val1 - val0)/sqrt2;\n              }\n            }","sourceCodeStart":47397,"sourceCodeEnd":47433,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L47397-L47433","documentation":"CImg::haar() performing the single-scale Haar transform along the Y axis requires the sub-image height (h = _height/2) to be even or equal to 1; otherwise the pairwise splitting of rows cannot proceed and it throws CImgInstanceException.","triggerScenarios":"Calling img.get_haar('y', invert, 1) when height/2 is odd and >1 — e.g. height of 6, 10, 14, 22.","commonSituations":"Images with non-power-of-two heights (cropped photos, sensor frames), multichannel volume processing where depth/slice count is odd, mixing transforms across axes where only one dimension is a power of two.","solutions":["Pad or resize the height to a power of two before the transform.","Choose an axis ('x' or 'z') whose dimension satisfies the evenness constraint if the data allows.","Use the multi-scale form (nb_scales>1) if appropriate for your sizes.","Crop to a size where height/2 is even or 1."],"exampleFix":"// before\nCImg<> out = img.get_haar('y', false, 1); // height 6 -> h=3, odd\n// after\nCImg<> padded = img.get_resize(img.width(), 8);\nCImg<> out = padded.get_haar('y', false, 1);","handlingStrategy":"validation","validationCode":"if (unsigned h = img.height()/2; (h % 2) && h != 1) img = img.get_resize(img.width(), nextPow2(img.height()));","typeGuard":"bool haarYOk(const CImg<T>& img) {\n  unsigned h = img.height()/2;\n  return h == 0 || !(h % 2) || h == 1;\n}","tryCatchPattern":"try {\n  res = img.get_haar('y', invert, nb_scales);\n} catch (const CImgInstanceException& e) {\n  res = img.get_resize(img.width(), pow2ceil(img.height())).get_haar('y', invert, nb_scales);\n}","preventionTips":["Keep heights at powers of two for wavelet pipelines.","Crop consistently across all dimensions, not just one axis.","Validate height/2 evenness in unit tests for your image sizes."],"tags":["cimg","wavelet","haar","dimension-mismatch"],"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"}