{"record":{"id":"fec06d289e3851ac","repo":"Yalantis/uCrop","slug":"save-jxl-failed-to-set-color-encoding-when-savi","errorCode":null,"errorMessage":"save_jxl(): Failed to set color encoding when saving file '%s'.","messagePattern":"save_jxl\\(\\): Failed to set color encoding when saving file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":61877,"sourceCode":"        basicInfo.num_extra_channels = 1;\n      }\n      basicInfo.num_color_channels = nChannels;\n      if (JXL_ENC_SUCCESS!=JxlEncoderSetBasicInfo(encoder,&basicInfo)) {\n        cimg::fclose(file);\n        JxlEncoderDestroy(encoder);\n        throw CImgIOException(_cimg_instance\n                              \"save_jxl(): Failed to set basic info when saving file '%s'.\",\n                              cimg_instance,\n                              filename);\n      }\n\n      JxlColorEncoding colorEncoding = {};\n      JXL_BOOL isGray = pixelFormat.num_channels<3?JXL_TRUE:JXL_FALSE;\n      JxlColorEncodingSetToSRGB(&colorEncoding, isGray);\n      if (JXL_ENC_SUCCESS!=JxlEncoderSetColorEncoding(encoder,&colorEncoding)) {\n        cimg::fclose(file);\n        JxlEncoderDestroy(encoder);\n        throw CImgIOException(_cimg_instance\n                              \"save_jxl(): Failed to set color encoding when saving file '%s'.\",\n                              cimg_instance,\n                              filename);\n      }\n\n      JxlEncoderFrameSettings* frameSettings = JxlEncoderFrameSettingsCreate(encoder,NULL);\n      if (JXL_ENC_SUCCESS!=JxlEncoderSetFrameDistance(frameSettings,distance)) {\n        cimg::fclose(file);\n        JxlEncoderDestroy(encoder);\n        throw CImgIOException(_cimg_instance\n                              \"save_jxl(): Failed to set lossy compression level when saving file '%s'.\",\n                              cimg_instance,\n                              filename);\n      }\n      if (JXL_ENC_SUCCESS!=JxlEncoderAddImageFrame(frameSettings,&pixelFormat,(const void*)rgbBuffer._data,\n                                                   rgbBuffer.size())) {\n        cimg::fclose(file);\n        JxlEncoderDestroy(encoder);","sourceCodeStart":61859,"sourceCodeEnd":61895,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L61859-L61895","documentation":"After setting basic info, save_jxl() configures the color encoding to sRGB (gray or color) via JxlEncoderSetColorEncoding. If libjxl returns non-success, CImg closes the file, destroys the encoder, and throws CImgIOException, because a JXL stream cannot be produced without a valid color encoding.","triggerScenarios":"save_jxl() on an image where the pixel format's channel count implies gray but the encoder state disagrees (or vice versa), or a libjxl build/state where the color-encoding step fails after basic info was accepted.","commonSituations":"Mismatch between basic info (set earlier) and pixelFormat.num_channels — e.g. custom channel layouts; incompatible or very old libjxl versions lacking API/behaviour expected by CImg.h.","solutions":["Save with a standard channel count (1 for gray, 3/4 for RGB(A)); convert the image first: img.resize(-100,-100,-100,3).","Rebuild against a current libjxl so JxlEncoderSetColorEncoding behaves as CImg expects.","Ensure JxlEncoderSetBasicInfo succeeded with a channel layout consistent with the pixel format (isGray flag matches num_channels<3)."],"exampleFix":"// before\nimg.save_jxl(\"out.jxl\"); // img.spectrum()==2, inconsistent with gray/color setup\n// after\nimg.resize(-100, -100, -100, 3); // normalize to RGB\nimg.save_jxl(\"out.jxl\");","handlingStrategy":"try-catch","validationCode":"if (img.spectrum() != 1 && img.spectrum() != 3 && img.spectrum() != 4) {\n  img.resize(-100, -100, -100, 3); // normalize so color encoding setup is consistent\n}","typeGuard":null,"tryCatchPattern":"try {\n  img.save_jxl(\"out.jxl\");\n} catch (const cimg_library::CImgIOException &e) {\n  std::fprintf(stderr, \"jxl color encoding failed: %s\\n\", e.what());\n  img.save_png(\"out.png\");\n}","preventionTips":["Use channel counts that map cleanly to gray or sRGB color profiles.","Rebuild against a current libjxl.","Normalize images to RGB before JXL saves."],"tags":["cimg","jxl","libjxl","color-encoding"],"backgroundTag":"api-request-failed","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"}