{"record":{"id":"890a540ab1ee8f00","repo":"Yalantis/uCrop","slug":"labtoxyz-instance-is-not-a-lab-image","errorCode":null,"errorMessage":"LabtoXYZ(): Instance is not a Lab image.","messagePattern":"LabtoXYZ\\(\\): Instance is not a Lab image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37633,"sourceCode":"          fX = (Tfloat)_cimg_Labf(X),\n          fY = (Tfloat)_cimg_Labf(Y),\n          fZ = (Tfloat)_cimg_Labf(Z);\n        p1[N] = (T)cimg::cut(116*fY - 16,0,100);\n        p2[N] = (T)(500*(fX - fY));\n        p3[N] = (T)(200*(fY - fZ));\n      }\n      return *this;\n    }\n\n    //! Convert pixel values from XYZ to Lab color spaces \\newinstance.\n    CImg<Tfloat> get_XYZtoLab(const bool use_D65=true) const {\n      return CImg<Tfloat>(*this,false).XYZtoLab(use_D65);\n    }\n\n    //! Convert pixel values from Lab to XYZ color spaces.\n    CImg<T>& LabtoXYZ(const bool use_D65=true) {\n      if (_spectrum!=3)\n        throw CImgInstanceException(_cimg_instance\n                                    \"LabtoXYZ(): Instance is not a Lab image.\",\n                                    cimg_instance);\n      const CImg<Tfloat> white = CImg<Tfloat>(1,1,1,3,255).RGBtoXYZ(use_D65);\n      T *p1 = data(0,0,0,0), *p2 = data(0,0,0,1), *p3 = data(0,0,0,2);\n      const longT whd = (longT)width()*height()*depth();\n      cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,128))\n      for (longT N = 0; N<whd; ++N) {\n        const Tfloat\n          L = (Tfloat)p1[N],\n          a = (Tfloat)p2[N],\n          b = (Tfloat)p3[N],\n          cY = (L + 16)/116,\n          cZ = cY - b/200,\n          cX = a/500 + cY,\n          X = (Tfloat)(24389*cX>216?cX*cX*cX:(116*cX - 16)*27/24389),\n          Y = (Tfloat)(27*L>216?cY*cY*cY:27*L/24389),\n          Z = (Tfloat)(24389*cZ>216?cZ*cZ*cZ:(116*cZ - 16)*27/24389);\n        p1[N] = (T)(X*white[0]);","sourceCodeStart":37615,"sourceCodeEnd":37651,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37615-L37651","documentation":"LabtoXYZ() converts pixel values from CIE Lab back to CIE XYZ and requires the image to hold exactly 3 Lab channels (L, a, b). It throws CImgInstanceException when _spectrum != 3, since it accesses three channel pointers directly. This guards against running the inverse transform on images not produced by XYZtoLab (or hand-built with the right layout).","triggerScenarios":"Calling img.LabtoXYZ() on an image with spectrum != 3 — typically a grayscale image, an RGBA image, or an image that was never converted to Lab. Also from get_LabtoXYZ() with the same invalid instance.","commonSituations":"Undoing a Lab conversion on a copy whose alpha channel was re-added; loading a Lab-looking TIFF with 1 or 4 channels; applying LabtoXYZ to an image still in RGB (forgot RGBtoXYZ first).","solutions":["Verify img.spectrum()==3 immediately before the call and fix the pipeline so Lab data is actually 3-channel.","If the image is still RGB, call RGBtoXYZ() then XYZtoLab() before LabtoXYZ() — LabtoXYZ only reverses XYZtoLab.","Drop or split off alpha channels (spectrum==4) before conversion.","Guard with try/catch on CImgInstanceException for defensive native code."],"exampleFix":"// before\ntexture.LabtoXYZ(); // texture is RGBA, spectrum==4 -> throws\n// after\nCImg<float> lab = texture.get_channels(0,2); // drop alpha\nlab.LabtoXYZ();","handlingStrategy":"validation","validationCode":"if (img.spectrum()!=3) { /* repair before LabtoXYZ */ }","typeGuard":"template<class T> bool has3Channels(const cimg_library::CImg<T>& i){ return i.spectrum()==3; }","tryCatchPattern":"try { img.LabtoXYZ(); } catch (cimg_library::CImgInstanceException& e) { /* recover */ }","preventionTips":["Track color state; only invert on Lab images","Assert spectrum()==3 in debug builds"],"tags":["cimg","color-space","image-processing","instance-check"],"backgroundTag":"incompatible-source-type","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"}