{"record":{"id":"e7fb5b4b46ada27f","repo":"Yalantis/uCrop","slug":"index-instance-and-specified-colormap-u-u-u","errorCode":null,"errorMessage":"index(): Instance and specified colormap (%u,%u,%u,%u,%p) have incompatible dimensions.","messagePattern":"index\\(\\): Instance and specified colormap \\(%u,%u,%u,%u,%p\\) have incompatible dimensions\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":36176,"sourceCode":"       \\par Example\n       \\code\n       const CImg<float> img(\"reference.jpg\"), colormap(3,1,1,3, 0,128,255, 0,128,255, 0,128,255);\n       const CImg<float> res = img.get_index(colormap,1,true);\n       (img,res).display();\n       \\endcode\n       \\image html ref_index.jpg\n    **/\n    template<typename t>\n    CImg<T>& index(const CImg<t>& colormap, const float dithering=1, const bool map_colors=false) {\n      return get_index(colormap,dithering,map_colors).move_to(*this);\n    }\n\n    //! Index multi-valued pixels regarding to a specified colormap \\newinstance.\n    template<typename t>\n    CImg<typename CImg<t>::Tuint>\n    get_index(const CImg<t>& colormap, const float dithering=1, const bool map_colors=true) const {\n      if (colormap._spectrum!=_spectrum)\n        throw CImgArgumentException(_cimg_instance\n                                    \"index(): Instance and specified colormap (%u,%u,%u,%u,%p) \"\n                                    \"have incompatible dimensions.\",\n                                    cimg_instance,\n                                    colormap._width,colormap._height,colormap._depth,colormap._spectrum,colormap._data);\n\n      typedef typename CImg<t>::Tuint tuint;\n      if (is_empty()) return CImg<tuint>();\n      const ulongT\n        whd = (ulongT)_width*_height*_depth,\n        pwhd = (ulongT)colormap._width*colormap._height*colormap._depth;\n      CImg<tuint> res(_width,_height,_depth,map_colors?_spectrum:1);\n      if (dithering>0) { // Dithered versions\n        tuint *ptrd = res._data;\n        const float ndithering = cimg::cut(dithering,0.f,1.f)/16;\n        Tfloat valm = 0, valM = (Tfloat)max_min(valm);\n        if (valm==valM && valm>=0 && valM<=255) { valm = 0; valM = 255; }\n        CImg<Tfloat> cache = get_crop(-1,0,0,0,_width,1,0,_spectrum - 1);\n        Tfloat *cache_current = cache.data(1,0,0,0), *cache_next = cache.data(1,1,0,0);","sourceCodeStart":36158,"sourceCodeEnd":36194,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L36158-L36194","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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.","If indexing an RGB image, ensure the colormap was built from 3-channel samples (one row per color, spectrum=3).","Check colormap dimensions with assert(colormap.spectrum() == img.spectrum()) before calling.","When loading both files, verify channel counts at load time and normalize both to a common colorspace/channel count."],"exampleFix":"// before\nCImg<float> rgb = img.get_channels(0,2);\nCImg<unsigned char> pal = palette; // spectrum == 1\nauto idx = rgb.get_index(pal); // throws: 3 vs 1 spectrum\n// after\nCImg<unsigned char> pal3(pal.width(), 1, 1, 3);\ncimg_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); }\nauto idx = rgb.get_index(pal3);","handlingStrategy":"type-guard","validationCode":"bool colormapCompatible(const CImg<float>& img, const CImg<unsigned char>& colormap) {\n  return !img.is_empty() && !colormap.is_empty() && img.spectrum() == colormap.spectrum();\n}\nif (colormapCompatible(img, colormap)) auto idx = img.get_index(colormap, dithering);","typeGuard":"inline bool spectraMatch(const CImg<A>& a, const CImg<B>& b) { return a.spectrum() == b.spectrum(); }","tryCatchPattern":"try {\n  idx = img.get_index(colormap, dithering, mapColors);\n} catch (const CImgArgumentException& e) {\n  std::fprintf(stderr, \"colormap incompatible with image spectrum: %s\\n\", e.what());\n}","preventionTips":["Always compare img.spectrum() with colormap.spectrum() before indexing.","Build colormaps from samples with the same channel count as the target image.","Normalize loaded images to a fixed channel count at load time.","When converting images (e.g. gray->RGB), rebuild or convert the palette accordingly."],"tags":["cimg","dimension-mismatch","image-processing","colormap"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}