{"record":{"id":"b2aeedfe0cf52233","repo":"d2phap/ImageGlass","slug":"can-t-configure-lossless-preset","errorCode":null,"errorMessage":"Can't configure lossless preset","messagePattern":"Can't configure lossless preset","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"v9/Components/ImageGlass.WebP/WebPWrapper.cs","lineNumber":546,"sourceCode":"\n    /// <summary>Lossless encoding image in bitmap (Advanced encoding API)</summary>\n    /// <param name=\"bmp\">Bitmap with the image</param>\n    /// <param name=\"speed\">Between 0 (fastest, lowest compression) and 9 (slower, best compression)</param>\n    /// <returns>Compressed data</returns>\n    public byte[] EncodeLossless(Bitmap bmp, int speed)\n    {\n        //Initialize configuration structure\n        WebPConfig config = new WebPConfig();\n\n        //Set compression parameters\n        if (LibWebp.WebPConfigInit(ref config, WebPPreset.WEBP_PRESET_DEFAULT, (speed + 1) * 10) == 0)\n            throw new Exception(\"Can't config preset\");\n\n        //Old version of DLL does not support info and WebPConfigLosslessPreset\n        if (LibWebp.WebPGetDecoderVersion() > 1082)\n        {\n            if (LibWebp.WebPConfigLosslessPreset(ref config, speed) == 0)\n                throw new Exception(\"Can't configure lossless preset\");\n        }\n        else\n        {\n            config.lossless = 1;\n            config.method = speed;\n            if (config.method > 6)\n                config.method = 6;\n            config.quality = (speed + 1) * 10;\n        }\n        config.pass = speed + 1;\n        config.thread_level = 1;\n        config.alpha_filtering = 2;\n        config.use_sharp_yuv = 1;\n        config.exact = 0;\n\n        return AdvancedEncode(bmp, config, false);\n    }\n","sourceCodeStart":528,"sourceCodeEnd":564,"githubUrl":"https://github.com/d2phap/ImageGlass/blob/4a3c4feceffc5a8bb5e56ba836509634aaae47a9/v9/Components/ImageGlass.WebP/WebPWrapper.cs#L528-L564","documentation":"Thrown by EncodeLossless(Bitmap, int) when LibWebp.WebPConfigLosslessPreset returns 0 for the requested speed level. The wrapper only takes this branch on DLL versions > 1082 (i.e. libwebp that exposes WebPConfigLosslessPreset). Returning 0 means the preset could not be applied, usually because speed is out of the valid 0-9 range or the preset table is unavailable in this build.","triggerScenarios":"Calling EncodeLossless(bmp, speed) on a DLL version > 1082 with a speed value outside 0-9, or with a libwebp build whose lossless preset support is broken/limited.","commonSituations":"Passing a negative speed or speed > 9; a custom libwebp build compiled without the lossless preset module; mismatch between the wrapper's assumed API surface and the actual DLL.","solutions":["Clamp speed to the documented 0-9 range before calling EncodeLossless(bmp, speed).","Verify the libwebp build exposes lossless presets (use a stock build rather than a stripped one).","If the DLL reports <= 1082 the code already falls back to manual config; consider downgrading or pinning a known-good libwebp."],"exampleFix":"// before\nvar bytes = webp.EncodeLossless(bmp, speed);\n\n// after\nint s = Math.Clamp(speed, 0, 9);\nvar bytes = webp.EncodeLossless(bmp, s);","handlingStrategy":"validation","validationCode":"int s = Math.Clamp(speed, 0, 9);\nvar bytes = webp.EncodeLossless(bmp, s);","typeGuard":"static bool IsValidLosslessSpeed(int speed) => speed >= 0 && speed <= 9;","tryCatchPattern":"try { var bytes = webp.EncodeLossless(bmp, speed); }\ncatch (Exception ex) when (ex.Message.Contains(\"lossless preset\"))\n{ var bytes = webp.EncodeLossless(bmp, Math.Clamp(speed, 0, 9)); }","preventionTips":["Always clamp the lossless speed argument to 0-9.","Use a stock libwebp build that exposes the preset table.","Treat a preset-init failure as a signal to verify the native build."],"tags":["webp","encoding","lossless","preset","validation","native-abi"],"backgroundTag":null,"analyzedSha":"4a3c4feceffc5a8bb5e56ba836509634aaae47a9","analyzedAt":"2026-08-13T16:58:15.523Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}