{"record":{"id":"e2bb430f74b75000","repo":"Yalantis/uCrop","slug":"save-jxl-failed-to-set-lossy-compression-level","errorCode":null,"errorMessage":"save_jxl(): Failed to set lossy compression level when saving file '%s'.","messagePattern":"save_jxl\\(\\): Failed to set lossy compression level when saving file '(.+?)'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":61887,"sourceCode":"      }\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);\n        throw CImgIOException(_cimg_instance\n                              \"save_jxl(): Failed to set image data when saving file '%s'.\",\n                              cimg_instance,\n                              filename);\n      }\n\n      CImg<ucharT> alphaBuffer;\n      if (hasAlpha) {\n        ucharT *alphaPtr = (ucharT*)ptr_a;\n        if (sizeof(T)!=bytesPerPixel) {","sourceCodeStart":61869,"sourceCodeEnd":61905,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L61869-L61905","documentation":"For lossy JXL, CImg sets the compression distance via JxlEncoderSetFrameDistance. A non-success return makes save_jxl() tear down the encoder and throw CImgIOException, meaning libjxl rejected the requested distance value (e.g. out of its accepted range) or the encoder is in a bad state.","triggerScenarios":"Calling save_jxl(path, distance) with a distance value libjxl rejects — JXL distance is typically 0 (lossless) up to ~25; passing a negative value, NaN, or an out-of-range number triggers this.","commonSituations":"Users porting JPEG quality (1–100) directly into the JXL distance parameter, or config files supplying quality as a 0–100 scale that is passed unconverted to distance.","solutions":["Use a valid distance in [0, 25]: 0 for lossless, ~1.0 for visually lossless.","Convert a JPEG-style quality q to distance, e.g. distance = q >= 100 ? 0 : (100 - q) / 10.0f, before calling save_jxl.","Clamp/validate the distance parameter at the call site."],"exampleFix":"// before\nint quality = 85;\nimg.save_jxl(\"out.jxl\", (float)quality); // 85 is out of JXL distance range\n// after\nfloat distance = quality >= 100 ? 0.0f : (100.0f - quality) / 10.0f;\nimg.save_jxl(\"out.jxl\", distance);","handlingStrategy":"validation","validationCode":"float dist = quality >= 100 ? 0.0f : (100.0f - quality) / 10.0f;\nif (dist < 0.0f || dist > 25.0f) {\n  throw std::invalid_argument(\"jxl distance must be within [0, 25]\");\n}\nimg.save_jxl(\"out.jxl\", dist);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never pass JPEG quality (1-100) directly as JXL distance.","Clamp distance to [0, 25] at call sites.","Use 0.0 for lossless and ~1.0 for visually lossless defaults."],"tags":["cimg","jxl","compression","invalid-parameter"],"backgroundTag":"invalid-argument-value","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"}