{"record":{"id":"47b15cdf537aca03","repo":"Yalantis/uCrop","slug":"yuvtorgb-instance-is-not-a-yuv-image","errorCode":null,"errorMessage":"YUVtoRGB(): Instance is not a YUV image.","messagePattern":"YUVtoRGB\\(\\): Instance is not a YUV image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37372,"sourceCode":"          G = (Tfloat)p2[N]/255,\n          B = (Tfloat)p3[N]/255,\n          Y = 0.299f*R + 0.587f*G + 0.114f*B;\n        p1[N] = (T)Y;\n        p2[N] = (T)(0.492f*(B - Y));\n        p3[N] = (T)(0.877*(R - Y));\n      }\n      return *this;\n    }\n\n    //! Convert pixel values from RGB to YUV color spaces \\newinstance.\n    CImg<Tfloat> get_RGBtoYUV() const {\n      return CImg<Tfloat>(*this,false).RGBtoYUV();\n    }\n\n    //! Convert pixel values from YUV to RGB color spaces.\n    CImg<T>& YUVtoRGB() {\n      if (_spectrum!=3)\n        throw CImgInstanceException(_cimg_instance\n                                    \"YUVtoRGB(): Instance is not a YUV 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,16384))\n      for (longT N = 0; N<whd; ++N) {\n        const Tfloat\n          Y = (Tfloat)p1[N],\n          U = (Tfloat)p2[N],\n          V = (Tfloat)p3[N],\n          R = (Y + 1.140f*V)*255,\n          G = (Y - 0.395f*U - 0.581f*V)*255,\n          B = (Y + 2.032f*U)*255;\n        p1[N] = (T)cimg::cut(R,0,255),\n        p2[N] = (T)cimg::cut(G,0,255),\n        p3[N] = (T)cimg::cut(B,0,255);\n      }","sourceCodeStart":37354,"sourceCodeEnd":37390,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37354-L37390","documentation":"YUVtoRGB() converts channels from YUV back to RGB in place and, despite the message saying 'not a YUV image', the actual check is the same _spectrum!=3 precondition shared by all 3-channel color conversions. Any image without exactly three channels throws.","triggerScenarios":"Calling img.YUVtoRGB() on grayscale, RGBA, 4-channel (e.g. leftover CMYK), or empty images.","commonSituations":"Decoders that output 4-channel buffers; pipelines where YUVtoRGB is called on already-RGB images that were later reduced to one channel; mis-constructed CImg buffers.","solutions":["Check spectrum()==3 before calling.","Reduce 4-channel inputs with channels(0,2); expand 1-channel with get_append.","Verify the preceding RGBtoYUV() actually succeeded on a 3-channel image.","Catch CImgInstanceException with a diagnostic including img.spectrum()."],"exampleFix":"// before\nimg.YUVtoRGB();\n// after\nassert(img.spectrum()==3);\nimg.YUVtoRGB();","handlingStrategy":"validation","validationCode":"assert(img.spectrum()==3);\nif (img.spectrum()!=3) throw std::runtime_error(\"YUV image must have 3 channels\");","typeGuard":"bool isYUV(const cimg_library::CImg<T>& img) { return img.spectrum()==3; }","tryCatchPattern":"try {\n  img.YUVtoRGB();\n} catch (const cimg_library::CImgInstanceException& e) {\n  std::cerr << \"YUVtoRGB failed: \" << e.what() << std::endl;\n}","preventionTips":["Verify the preceding RGBtoYUV step succeeded and image remained 3-channel","Track color space in a variable/enum alongside the image to avoid wrong-order conversions","Check spectrum() before every in-place conversion"],"tags":["cimg","image-processing","color-space","spectrum"],"backgroundTag":"type-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"}