{"record":{"id":"18da5c4799e2b7bf","repo":"Yalantis/uCrop","slug":"xyztoxyy-instance-is-not-a-xyz-image","errorCode":null,"errorMessage":"XYZtoxyY(): Instance is not a XYZ image.","messagePattern":"XYZtoxyY\\(\\): Instance is not a XYZ image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37666,"sourceCode":"          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]);\n        p2[N] = (T)(Y*white[1]);\n        p3[N] = (T)(Z*white[2]);\n      }\n      return *this;\n    }\n\n    //! Convert pixel values from Lab to XYZ color spaces \\newinstance.\n    CImg<Tfloat> get_LabtoXYZ(const bool use_D65=true) const {\n      return CImg<Tfloat>(*this,false).LabtoXYZ(use_D65);\n    }\n\n    //! Convert pixel values from XYZ to xyY color spaces.\n    CImg<T>& XYZtoxyY() {\n      if (_spectrum!=3)\n        throw CImgInstanceException(_cimg_instance\n                                    \"XYZtoxyY(): Instance is not a XYZ image.\",\n                                    cimg_instance);\n\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,4096))\n      for (longT N = 0; N<whd; ++N) {\n        const Tfloat\n          X = (Tfloat)p1[N],\n          Y = (Tfloat)p2[N],\n          Z = (Tfloat)p3[N],\n          sum = X + Y + Z,\n          nsum = sum>0?sum:1;\n        p1[N] = (T)(X/nsum);\n        p2[N] = (T)(Y/nsum);\n        p3[N] = (T)Y;\n      }\n      return *this;","sourceCodeStart":37648,"sourceCodeEnd":37684,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37648-L37684","documentation":"XYZtoxyY() converts CIE XYZ channels to xyY chromaticity coordinates and requires exactly 3 channels (X, Y, Z). It throws CImgInstanceException when _spectrum != 3 because it operates on the three channel planes directly. Like the other color conversions, it is a precondition on the whole image instance.","triggerScenarios":"Calling img.XYZtoxyY() (or get_XYZtoxyY()) on any image whose spectrum is not 3: grayscale (_spectrum==1), RGB without XYZ conversion, RGBA (_spectrum==4), or video/sequence data with more channels.","commonSituations":"Chaining XYZtoxyY after RGBtoXYZ on an RGBA image where the XYZ conversion was applied to only part of the data; applying the transform to a single-channel luminance map; mis-ordered conversion pipeline on multi-channel scientific imagery.","solutions":["Assert spectrum()==3 before calling and correct the upstream pipeline (RGB -> RGBtoXYZ -> XYZtoxyY).","If the image is RGB, convert with RGBtoXYZ() first so the channels genuinely hold XYZ.","If the image has 1 or 4+ channels, project to 3 channels (get_channels(0,2) for RGBA) or rebuild the image as 3-channel before converting.","Catch CImgInstanceException around the call in native/JNI layers."],"exampleFix":"// before\nframe.XYZtoxyY(); // frame is RGBA, spectrum==4 -> throws\n// after\nCImg<float> xyz = frame.get_channels(0,2).RGBtoXYZ(); // ensure real XYZ, 3 channels\nCImg<float> xyy = xyz.get_XYZtoxyY();","handlingStrategy":"validation","validationCode":"if (img.spectrum()!=3) { /* repair before XYZtoxyY */ }","typeGuard":"template<class T> bool has3Channels(const cimg_library::CImg<T>& i){ return i.spectrum()==3; }","tryCatchPattern":"try { img.XYZtoxyY(); } catch (cimg_library::CImgInstanceException& e) { /* fallback */ }","preventionTips":["Run RGBtoXYZ before XYZtoxyY when starting from RGB","Validate channels at pipeline entry"],"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"}