{"record":{"id":"fbbd743181d890c6","repo":"Yalantis/uCrop","slug":"cmyktocmy-instance-is-not-a-cmyk-image","errorCode":null,"errorMessage":"CMYKtoCMY(): Instance is not a CMYK image.","messagePattern":"CMYKtoCMY\\(\\): Instance is not a CMYK image\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":37500,"sourceCode":"        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),\n        pd3[N] = (Tfloat)cimg::cut(Y,0,255),\n        pd4[N] = (Tfloat)cimg::cut(K,0,255);\n      }\n      return res;\n    }\n\n    //! Convert pixel values from CMYK to CMY color spaces.\n    CImg<T>& CMYKtoCMY() {\n      return get_CMYKtoCMY().move_to(*this);\n    }\n\n    //! Convert pixel values from CMYK to CMY color spaces \\newinstance.\n    CImg<Tfloat> get_CMYKtoCMY() const {\n      if (_spectrum!=4)\n        throw CImgInstanceException(_cimg_instance\n                                    \"CMYKtoCMY(): Instance is not a CMYK image.\",\n                                    cimg_instance);\n\n      CImg<Tfloat> res(_width,_height,_depth,3);\n      const T *ps1 = data(0,0,0,0), *ps2 = data(0,0,0,1), *ps3 = data(0,0,0,2), *ps4 = data(0,0,0,3);\n      Tfloat *pd1 = res.data(0,0,0,0), *pd2 = res.data(0,0,0,1), *pd3 = res.data(0,0,0,2);\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        const Tfloat\n          C = (Tfloat)ps1[N],\n          M = (Tfloat)ps2[N],\n          Y = (Tfloat)ps3[N],\n          K = (Tfloat)ps4[N],\n          K1 = 1 - K/255,\n          nC = C*K1 + K,\n          nM = M*K1 + K,\n          nY = Y*K1 + K;","sourceCodeStart":37482,"sourceCodeEnd":37518,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L37482-L37518","documentation":"get_CMYKtoCMY() converts a 4-channel CMYK image to a new 3-channel CMY image; it requires _spectrum==4. CImg throws CImgInstanceException when the image does not have four channels, since it reads four source planes including the K (black) plane.","triggerScenarios":"Calling img.CMYKtoCMY() or img.get_CMYKtoCMY() on RGB (3-channel), grayscale, or empty images.","commonSituations":"Mixed print pipelines where RGB files reach a CMYK-only code path; images whose alpha or extra channel made the count 3 or 5; incorrect loading order.","solutions":["Guard with if (img.spectrum()==4) before calling.","For RGB input, call RGBtoCMY() instead of CMYKtoCMY().","Verify the source really is CMYK from the loader/ICC profile, not just channel count.","Catch CImgInstanceException and route by spectrum."],"exampleFix":"// before\nimg.CMYKtoCMY(); // img is RGB, spectrum=3\n// after\nif (img.spectrum()==4) img.CMYKtoCMY();\nelse if (img.spectrum()==3) img.RGBtoCMY();","handlingStrategy":"validation","validationCode":"if (img.spectrum()!=4) throw std::runtime_error(\"CMYKtoCMY requires 4-channel CMYK image\");\nCImg<float> cmy = img.get_CMYKtoCMY();","typeGuard":"bool isCMYK(const cimg_library::CImg<T>& img) { return img.spectrum()==4; }","tryCatchPattern":"try {\n  img.CMYKtoCMY();\n} catch (const cimg_library::CImgInstanceException& e) {\n  std::cerr << \"Need CMYK (4 channels), got \" << img.spectrum() << std::endl;\n}","preventionTips":["Confirm the loader actually produced CMYK (spectrum==4) before this path","Use RGBtoCMY() for RGB sources instead of forcing the CMYK path","Dispatch conversions by img.spectrum() rather than assumed file format"],"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"}