Yalantis/uCrop · error · CImgArgumentException

insert(): Invalid insertion request of specified shared imag

Error message

insert(): Invalid insertion request of specified shared image CImg<%s>(%u,%u,%u,%u,%p) at position %u (pixel types are different).

What it means

CImgList::insert() rejects inserting a shared CImg whose pixel type differs from the list's element type T. A shared image must share its buffer with the list, so mixing pixel types (e.g. inserting a shared CImg<unsigned char> into a CImgList<float>) is invalid. The library throws CImgArgumentException naming both the image dimensions and the mismatched pixel_type().

Source

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

    //---------------------------

    //! Insert a copy of the image \c img into the current image list, at position \c pos.
    /**
        \param img Image to insert a copy to the list.
        \param pos Index of the insertion.
        \param is_shared Tells if the inserted image is a shared copy of \c img or not.
    **/
    template<typename t>
    CImgList<T>& insert(const CImg<t>& img, const unsigned int pos=~0U, const bool is_shared=false) {
      const unsigned int npos = pos==~0U?_width:pos;
      if (npos>_width)
        throw CImgArgumentException(_cimglist_instance
                                    "insert(): Invalid insertion request of specified image (%u,%u,%u,%u,%p) "
                                    "at position %u.",
                                    cimglist_instance,
                                    img._width,img._height,img._depth,img._spectrum,img._data,npos);
      if (is_shared)
        throw CImgArgumentException(_cimglist_instance
                                    "insert(): Invalid insertion request of specified shared image "
                                    "CImg<%s>(%u,%u,%u,%u,%p) at position %u (pixel types are different).",
                                    cimglist_instance,
                                    img.pixel_type(),img._width,img._height,img._depth,img._spectrum,img._data,npos);

      CImg<T> *const new_data = (++_width>_allocated_width)?new CImg<T>[_allocated_width?(_allocated_width<<=1):
                                                                        (_allocated_width=16)]:0;
      if (!_data) { // Insert new element into empty list
        _data = new_data;
        *_data = img;
      } else {
        if (new_data) { // Insert with re-allocation
          if (npos) std::memcpy((void*)new_data,(void*)_data,sizeof(CImg<T>)*npos);
          if (npos!=_width - 1)
            std::memcpy((void*)(new_data + npos + 1),(void*)(_data + npos),sizeof(CImg<T>)*(_width - 1 - npos));
          std::memset((void*)_data,0,sizeof(CImg<T>)*(_width - 1));
          delete[] _data;
          _data = new_data;

View on GitHub (pinned to f788b534b4)

Solutions

  1. Convert the image to the list's pixel type before inserting, or insert a non-shared copy (is_shared=false)
  2. Change the CImgList template parameter so it matches the shared image's pixel type
  3. Deep-copy via CImg<T>(img) to retype the data before sharing

Example fix

// before
CImgList<float> list;
CImg<unsigned char> img("in.png");
list.insert(img, 0, true); // throws: pixel types are different
// after
list.insert(img.get_resize(...).get_shared(), 0, true); // still invalid; correct:
list.insert(CImg<float>(img), 0, false); // or load as float
list.insert(CImg<float>(img), 0, true); // shared float image
Defensive patterns

Strategy: validation

Validate before calling

if (img.is_shared() && img.pixel_type() != typeid(T).name()) { /* retype or copy */ }

Type guard

template<typename S, typename T> bool pixel_type_matches(const CImg<S>& img) { return std::is_same<S,T>::value; }

Try / catch

try { list.insert(img, pos, true); } catch (const CImgArgumentException& e) { list.insert(CImg<T>(img), pos, false); }

Prevention

When it happens

Trigger: Calling list.insert(img, pos, true) (is_shared=true) where img.pixel_type() != typeid of the list's T, e.g. inserting a shared unsigned-char image into a CImgList<float>.

Common situations: Mixing 8-bit images loaded from disk with float processing lists; reusing a shared-image wrapper from another list of a different template instantiation; template type changed in refactoring so the list element type no longer matches the shared image.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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