{"record":{"id":"73719b675f221561","repo":"Yalantis/uCrop","slug":"cmytocmyk-instance-is-not-a-cmy-image","errorCode":null,"errorMessage":"CMYtoCMYK(): Instance is not a CMY image.","messagePattern":"CMYtoCMYK\\(\\): Instance is not a CMY image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37467,"sourceCode":"        p3[N] = (T)cimg::cut(B,0,255);\n      }\n      return *this;\n    }\n\n    //! Convert pixel values from CMY to RGB color spaces \\newinstance.\n    CImg<Tuchar> get_CMYtoRGB() const {\n      return CImg<Tuchar>(*this,false).CMYtoRGB();\n    }\n\n    //! Convert pixel values from CMY to CMYK color spaces.\n    CImg<T>& CMYtoCMYK() {\n      return get_CMYtoCMYK().move_to(*this);\n    }\n\n    //! Convert pixel values from CMY to CMYK color spaces \\newinstance.\n    CImg<Tuchar> get_CMYtoCMYK() const {\n      if (_spectrum!=3)\n        throw CImgInstanceException(_cimg_instance\n                                    \"CMYtoCMYK(): Instance is not a CMY image.\",\n                                    cimg_instance);\n\n      CImg<Tfloat> res(_width,_height,_depth,4);\n      const T *ps1 = data(0,0,0,0), *ps2 = data(0,0,0,1), *ps3 = data(0,0,0,2);\n      Tfloat *pd1 = res.data(0,0,0,0), *pd2 = res.data(0,0,0,1), *pd3 = res.data(0,0,0,2), *pd4 = res.data(0,0,0,3);\n      const longT whd = (longT)width()*height()*depth();\n      cimg_pragma_openmp(parallel for cimg_openmp_if_size(whd,1024))\n      for (longT N = 0; N<whd; ++N) {\n        Tfloat\n          C = (Tfloat)ps1[N],\n          M = (Tfloat)ps2[N],\n          Y = (Tfloat)ps3[N],\n          K = cimg::min(C,M,Y);\n        if (K>=255) C = M = Y = 0;\n        else { const Tfloat K1 = 255 - K; C = 255*(C - K)/K1; M = 255*(M - K)/K1; Y = 255*(Y - K)/K1; }\n        pd1[N] = (Tfloat)cimg::cut(C,0,255),\n        pd2[N] = (Tfloat)cimg::cut(M,0,255),","sourceCodeStart":37449,"sourceCodeEnd":37485,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37449-L37485","documentation":"get_CMYtoCMYK() produces a new 4-channel CMYK image from a CMY image; it requires the source to have exactly 3 channels (_spectrum==3). CImg throws CImgInstanceException otherwise, since it reads three source planes and writes four destination planes.","triggerScenarios":"Calling img.CMYtoCMYK() or img.get_CMYtoCMYK() on grayscale, RGBA, or empty images.","commonSituations":"Print pipelines receiving RGB or grayscale scans that must be converted to CMYK; forgetting the RGBtoCMY() step; images loaded with alpha channels.","solutions":["Guard with if (img.spectrum()==3) before conversion.","Convert RGB input first: img.RGBtoCMY() (after ensuring 3 channels).","Expand grayscale with get_append(img,img,'c').","Catch CImgInstanceException for non-CMY inputs."],"exampleFix":"// before\nCImg<float> img(\"photo.png\"); // RGB\nimg.CMYtoCMYK();\n// after\nCImg<float> img(\"photo.png\");\nif (img.spectrum()==3) img.RGBtoCMY().CMYtoCMYK();","handlingStrategy":"validation","validationCode":"if (img.spectrum()!=3) throw std::runtime_error(\"CMYtoCMYK requires 3-channel CMY image\");\nCImg<float> cmyk = img.get_CMYtoCMYK();","typeGuard":"bool isCMY(const cimg_library::CImg<T>& img) { return img.spectrum()==3; }","tryCatchPattern":"try {\n  img.CMYtoCMYK();\n} catch (const cimg_library::CImgInstanceException& e) {\n  std::cerr << \"Need CMY (3 channels), got \" << img.spectrum() << std::endl;\n}","preventionTips":["Always run RGBtoCMY() before CMYtoCMYK() on RGB sources","Expand grayscale with get_append(img,img,'c') before conversion","Prefer the get_ variant so the source remains untouched on failure"],"tags":["cimg","image-processing","color-space","cmyk","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-17T15:17:12.973Z"}