Yalantis/uCrop · error · CImgArgumentException

resize(): Specified centering arguments (%g,%g,%g,%g) are…

Error message

resize(): Specified centering arguments (%g,%g,%g,%g) are outside range [0,1].

What it means

resize() accepts centering_x/y/z/c parameters (range [0,1]) that control where the source window is anchored inside the target when the resized image is smaller than the original. The library throws CImgArgumentException when any centering value is negative or greater than 1, validating the arguments up front before doing any work.

Solutions

  1. Clamp each centering value into [0,1]: cimg::cut(value, 0.0f, 1.0f) before calling resize().
  2. Check your units — centering is a fraction (0 = left/top edge, 0.5 = centered, 1 = right/bottom), not a percentage; divide 0-100 values by 100.
  3. Verify computed centering values (e.g. crop offsets) cannot be negative; clamp or reject them upstream.
  4. Add a debug assert or early validation on user-supplied crop/centering settings before they reach resize().

Example fix

// before
img.resize(w, h, -100, -100, 1, 0, cx, cy); // cx computed from offset, can be 1.4 -> throws
// after
float cx2 = cimg::cut(cx, 0.0f, 1.0f), cy2 = cimg::cut(cy, 0.0f, 1.0f);
img.resize(w, h, -100, -100, 1, 0, cx2, cy2);
Defensive patterns

Strategy: validation

Validate before calling

if (!inUnitRange(cx)||!inUnitRange(cy)||!inUnitRange(cz)||!inUnitRange(cc)) { /* clamp or reject */ }

Type guard

bool inUnitRange(float v) { return !(v < 0.0f) && !(v > 1.0f); }

Try / catch

try { img.resize(w,h,-100,-100,1,0,cx,cy); } catch (cimg_library::CImgArgumentException& e) { /* clamp and retry */ }

Prevention

When it happens

Trigger: Calling img.resize(...) with any of centering_x, centering_y, centering_z, centering_c set to a float outside [0,1] — e.g. -0.5, 1.5, 2, or NaN propagation from a computed value. Applies to both resize() and get_resize() overloads that take centering parameters.

Common situations: Computing centering from a normalized ratio that can go negative (e.g. offset/size with a negative offset); swapping the centering argument with an offset or percentage expressed as 0-100 instead of 0-1; copying code that used a different library's 0-100 convention.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/ebc5d8798e6963bc. Report an issue: GitHub.

Appendix: source

Thrown at ucrop/src/main/jni/CImg.h:37845

      if (sx==_width && sy==_height && sz==_depth && sc==_spectrum) return *this;
      if (is_empty()) return assign(sx,sy,sz,sc,(T)0);
      if (interpolation_type==-1 && sx*sy*sz*sc==size()) {
        _width = sx; _height = sy; _depth = sz; _spectrum = sc;
        return *this;
      }
      return get_resize(sx,sy,sz,sc,interpolation_type,boundary_conditions,
                        centering_x,centering_y,centering_z,centering_c).move_to(*this);
    }

    //! Resize image to new dimensions \newinstance.
    CImg<T> get_resize(const int size_x, const int size_y = -100,
                       const int size_z = -100, const int size_c = -100,
                       const int interpolation_type=1, const unsigned int boundary_conditions=0,
                       const float centering_x = 0, const float centering_y = 0,
                       const float centering_z = 0, const float centering_c = 0) const {
      if (centering_x<0 || centering_x>1 || centering_y<0 || centering_y>1 ||
          centering_z<0 || centering_z>1 || centering_c<0 || centering_c>1)
        throw CImgArgumentException(_cimg_instance
                                    "resize(): Specified centering arguments (%g,%g,%g,%g) are outside range [0,1].",
                                    cimg_instance,
                                    centering_x,centering_y,centering_z,centering_c);

      if (!size_x || !size_y || !size_z || !size_c) return CImg<T>();
      const unsigned int
        sx = std::max(1U,(unsigned int)(size_x>=0?size_x:-size_x*width()/100)),
        sy = std::max(1U,(unsigned int)(size_y>=0?size_y:-size_y*height()/100)),
        sz = std::max(1U,(unsigned int)(size_z>=0?size_z:-size_z*depth()/100)),
        sc = std::max(1U,(unsigned int)(size_c>=0?size_c:-size_c*spectrum()/100));
      if (sx==_width && sy==_height && sz==_depth && sc==_spectrum) return +*this;
      if (is_empty()) return CImg<T>(sx,sy,sz,sc,(T)0);
      CImg<T> res;
      switch (interpolation_type) {

        // Raw resizing.
        //
      case -1 :

View on GitHub (pinned to f788b534b4)