Yalantis/uCrop · error · CImgArgumentException

get_shared_channels(): Invalid request of a shared-memory…

Error message

get_shared_channels(): Invalid request of a shared-memory subset (0->%u,0->%u,0->%u,%u->%u).

What it means

CImg<T>::get_shared_channels() returns a shared-memory image referencing channels c0..c1 of the instance. It throws CImgArgumentException when the channel offsets are inverted (beg > end) or outside the instance memory (beg >= size() or end >= size()), i.e. the requested channel range does not exist in the image.

Solutions

  1. Ensure 0 <= c0 <= c1 < img.spectrum(); clamp c1 to spectrum-1 before the call.
  2. Verify the image actually has the expected channel count (e.g. img.spectrum() >= 4 before extracting RGBA).
  3. If the source lacks channels, append dummy channels first (img.append(CImg<T>(img.width(),img.height(),img.depth(),k),'c')) or handle the lower channel count.
  4. For grayscale images, decide explicitly whether to duplicate the channel instead of extracting a range.

Example fix

// before
CImg<float> rgba = img.get_shared_channels(0, 3);   // img is RGB (spectrum 3)
// after
if (img.spectrum() < 4) img.append(CImg<float>(img.width(),img.height(),img.depth(),1,1),'c');
CImg<float> rgba = img.get_shared_channels(0, 3);
Defensive patterns

Strategy: validation

Validate before calling

bool ok = c0 <= c1 && c1 < img.spectrum();

Try / catch

try { auto ch = img.get_shared_channels(c0, c1); }
catch (CImgArgumentException& e) { /* clamp c1 to spectrum-1 and retry */ }

Prevention

When it happens

Trigger: Calling img.get_shared_channels(c0,c1) with c0 > c1, c1 >= img.spectrum() (e.g. requesting channels 0-3 of an RGB image with spectrum 3), or on an image with spectrum < 2.

Common situations: Requesting RGBA (4) channels from an RGB (3) image; assuming an alpha channel exists; extracting channels from a grayscale image (spectrum 1); palette-indexed images where spectrum is 1.

Related errors


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

Appendix: source

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

      return get_shared_slices(z0,z0,c0);
    }

    //! Return a shared-memory image referencing one slice of the image instance \const.
    const CImg<T> get_shared_slice(const unsigned int z0, const unsigned int c0=0) const {
      return get_shared_slices(z0,z0,c0);
    }

    //! Return a shared-memory image referencing a range of channels of the image instance.
    /**
       \param c0 C-coordinate of the starting channel.
       \param c1 C-coordinate of the ending channel.
    **/
    CImg<T> get_shared_channels(const unsigned int c0, const unsigned int c1) {
      const ulongT
        beg = (ulongT)offset(0,0,0,c0),
        end = (ulongT)offset(0,0,0,c1);
      if (beg>end || beg>=size() || end>=size())
        throw CImgArgumentException(_cimg_instance
                                    "get_shared_channels(): Invalid request of a shared-memory subset "
                                    "(0->%u,0->%u,0->%u,%u->%u).",
                                    cimg_instance,
                                    _width - 1,_height - 1,_depth - 1,c0,c1);
      return CImg<T>(_data + beg,_width,_height,_depth,c1 - c0 + 1,true);
    }

    //! Return a shared-memory image referencing a range of channels of the image instance \const.
    const CImg<T> get_shared_channels(const unsigned int c0, const unsigned int c1) const {
      const ulongT
        beg = (ulongT)offset(0,0,0,c0),
        end = (ulongT)offset(0,0,0,c1);
      if (beg>end || beg>=size() || end>=size())
        throw CImgArgumentException(_cimg_instance
                                    "get_shared_channels(): Invalid request of a shared-memory subset "
                                    "(0->%u,0->%u,0->%u,%u->%u).",
                                    cimg_instance,
                                    _width - 1,_height - 1,_depth - 1,c0,c1);

View on GitHub (pinned to f788b534b4)