{"record":{"id":"788b796d26f07fe2","repo":"Yalantis/uCrop","slug":"rgbtoyuv-instance-is-not-a-rgb-image","errorCode":null,"errorMessage":"RGBtoYUV(): Instance is not a RGB image.","messagePattern":"RGBtoYUV\\(\\): Instance is not a RGB image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37344,"sourceCode":"          R = (298*Y + 409*Cr + 128)/256,\n          G = (298*Y - 100*Cb - 208*Cr + 128)/256,\n          B = (298*Y + 516*Cb + 128)/256;\n        p1[N] = (T)cimg::cut(R,(Tfloat)0,(Tfloat)255),\n        p2[N] = (T)cimg::cut(G,(Tfloat)0,(Tfloat)255),\n        p3[N] = (T)cimg::cut(B,(Tfloat)0,(Tfloat)255);\n      }\n      return *this;\n    }\n\n    //! Convert pixel values from RGB to YCbCr color spaces \\newinstance.\n    CImg<Tuchar> get_YCbCrtoRGB() const {\n      return CImg<Tuchar>(*this,false).YCbCrtoRGB();\n    }\n\n    //! Convert pixel values from RGB to YUV color spaces.\n    CImg<T>& RGBtoYUV() {\n      if (_spectrum!=3)\n        throw CImgInstanceException(_cimg_instance\n                                    \"RGBtoYUV(): Instance is not a RGB 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          R = (Tfloat)p1[N]/255,\n          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    }","sourceCodeStart":37326,"sourceCodeEnd":37362,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37326-L37362","documentation":"RGBtoYUV() performs an in-place RGB to YUV channel conversion and requires a 3-channel image. CImg throws this CImgInstanceException when _spectrum!=3 because the transform reads and writes exactly the three channel planes p1/p2/p3.","triggerScenarios":"Calling img.RGBtoYUV() on a grayscale (spectrum=1), RGBA (spectrum=4), or empty image.","commonSituations":"Video-frame pipelines fed 4-channel frames; grayscale webcam frames; images built with wrong channel count via CImg(w,h,d,s) constructor.","solutions":["Guard with if (img.spectrum()==3) before the call.","For grayscale, expand: img = img.get_append(img,img,'c');","For RGBA, strip alpha: img.channels(0,2).","Catch CImgInstanceException and convert or reject the frame."],"exampleFix":"// before\nCImg<float> frame = nextFrame(); // may be RGBA\nframe.RGBtoYUV();\n// after\nCImg<float> frame = nextFrame();\nif (frame.spectrum()==4) frame.channels(0,2);\nif (frame.spectrum()!=3) throw std::runtime_error(\"RGB required\");\nframe.RGBtoYUV();","handlingStrategy":"validation","validationCode":"if (img.spectrum()!=3) {\n  if (img.spectrum()==4) img.channels(0,2);\n  else if (img.spectrum()==1) img.append(img,img,img,'c');\n}","typeGuard":"bool isRGB(const cimg_library::CImg<T>& img) { return img.spectrum()==3; }","tryCatchPattern":"try {\n  img.RGBtoYUV();\n} catch (const cimg_library::CImgInstanceException& e) {\n  std::cerr << \"RGB required; spectrum=\" << img.spectrum() << std::endl;\n}","preventionTips":["Normalize frame channel count at ingestion (video frames are often 4-channel)","Check spectrum() after every crop/append operation","Use explicit spectrum=3 in CImg constructors when allocating color buffers"],"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"}