Yalantis/uCrop · error · CImgArgumentException

index(): Instance and specified colormap (%u,%u,%u,%u,%p) ha

Error message

index(): Instance and specified colormap (%u,%u,%u,%u,%p) have incompatible dimensions.

What it means

CImg's index() (and get_index()) maps each multi-valued pixel of the image to an entry of a supplied colormap. This only works when the colormap's channel count (spectrum) matches the image's spectrum; otherwise the mapping is undefined, so get_index throws this CImgArgumentException, reporting the colormap's full dimensions and data pointer.

Source

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

       \par Example
       \code
       const CImg<float> img("reference.jpg"), colormap(3,1,1,3, 0,128,255, 0,128,255, 0,128,255);
       const CImg<float> res = img.get_index(colormap,1,true);
       (img,res).display();
       \endcode
       \image html ref_index.jpg
    **/
    template<typename t>
    CImg<T>& index(const CImg<t>& colormap, const float dithering=1, const bool map_colors=false) {
      return get_index(colormap,dithering,map_colors).move_to(*this);
    }

    //! Index multi-valued pixels regarding to a specified colormap \newinstance.
    template<typename t>
    CImg<typename CImg<t>::Tuint>
    get_index(const CImg<t>& colormap, const float dithering=1, const bool map_colors=true) const {
      if (colormap._spectrum!=_spectrum)
        throw CImgArgumentException(_cimg_instance
                                    "index(): Instance and specified colormap (%u,%u,%u,%u,%p) "
                                    "have incompatible dimensions.",
                                    cimg_instance,
                                    colormap._width,colormap._height,colormap._depth,colormap._spectrum,colormap._data);

      typedef typename CImg<t>::Tuint tuint;
      if (is_empty()) return CImg<tuint>();
      const ulongT
        whd = (ulongT)_width*_height*_depth,
        pwhd = (ulongT)colormap._width*colormap._height*colormap._depth;
      CImg<tuint> res(_width,_height,_depth,map_colors?_spectrum:1);
      if (dithering>0) { // Dithered versions
        tuint *ptrd = res._data;
        const float ndithering = cimg::cut(dithering,0.f,1.f)/16;
        Tfloat valm = 0, valM = (Tfloat)max_min(valm);
        if (valm==valM && valm>=0 && valM<=255) { valm = 0; valM = 255; }
        CImg<Tfloat> cache = get_crop(-1,0,0,0,_width,1,0,_spectrum - 1);
        Tfloat *cache_current = cache.data(1,0,0,0), *cache_next = cache.data(1,1,0,0);

View on GitHub (pinned to f788b534b4)

Solutions

  1. Match spectra: convert the image to the colormap's spectrum (e.g. img.get_RGBtoLUT-style conversion, or channels 0..2 selection) or rebuild the colormap with the same spectrum.
  2. If indexing an RGB image, ensure the colormap was built from 3-channel samples (one row per color, spectrum=3).
  3. Check colormap dimensions with assert(colormap.spectrum() == img.spectrum()) before calling.
  4. When loading both files, verify channel counts at load time and normalize both to a common colorspace/channel count.

Example fix

// before
CImg<float> rgb = img.get_channels(0,2);
CImg<unsigned char> pal = palette; // spectrum == 1
auto idx = rgb.get_index(pal); // throws: 3 vs 1 spectrum
// after
CImg<unsigned char> pal3(pal.width(), 1, 1, 3);
cimg_forX(pal,x) { pal3(x,0,0,0)=pal(x,0,0,0); pal3(x,0,0,1)=pal(x,0,0,1); pal3(x,0,0,2)=pal(x,0,0,2); }
auto idx = rgb.get_index(pal3);
Defensive patterns

Strategy: type-guard

Validate before calling

bool colormapCompatible(const CImg<float>& img, const CImg<unsigned char>& colormap) {
  return !img.is_empty() && !colormap.is_empty() && img.spectrum() == colormap.spectrum();
}
if (colormapCompatible(img, colormap)) auto idx = img.get_index(colormap, dithering);

Type guard

inline bool spectraMatch(const CImg<A>& a, const CImg<B>& b) { return a.spectrum() == b.spectrum(); }

Try / catch

try {
  idx = img.get_index(colormap, dithering, mapColors);
} catch (const CImgArgumentException& e) {
  std::fprintf(stderr, "colormap incompatible with image spectrum: %s\n", e.what());
}

Prevention

When it happens

Trigger: Calling img.get_index(colormap, dithering, map_colors) where colormap._spectrum != img._spectrum — e.g. indexing an RGB image (spectrum=3) with a grayscale/paletted colormap (spectrum=1), or a 4-channel image with an RGB colormap.

Common situations: Loading a palette image, converting it (e.g. to RGB) but keeping the original single-channel palette for indexing; building a colormap from k-means on flattened single-channel data while the target image is RGB; mixing images of different channel counts from different files.

Understand the failure class

Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.

Related errors


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