{"record":{"id":"d67b5f1c4160690c","repo":"gchq/CyberChef","slug":"error-applying-filter-to-image-err","errorCode":null,"errorMessage":"Error applying filter to image. (${err})","messagePattern":"Error applying filter to image\\. \\((.+?)\\)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"warning","filePath":"src/core/operations/ImageFilter.mjs","lineNumber":78,"sourceCode":"                    \"Applying \" +\n                        filterType.toLowerCase() +\n                        \" filter to image...\",\n                );\n            if (filterType === \"Greyscale\") {\n                image.greyscale();\n            } else {\n                image.sepia();\n            }\n\n            let imageBuffer;\n            if (image.mime === \"image/gif\") {\n                imageBuffer = await image.getBuffer(JimpMime.png);\n            } else {\n                imageBuffer = await image.getBuffer(image.mime);\n            }\n            return imageBuffer.buffer;\n        } catch (err) {\n            throw new OperationError(\n                `Error applying filter to image. (${err})`,\n            );\n        }\n    }\n\n    /**\n     * Displays the blurred image using HTML for web apps\n     * @param {ArrayBuffer} data\n     * @returns {html}\n     */\n    present(data) {\n        if (!data.byteLength) return \"\";\n        const dataArray = new Uint8Array(data);\n\n        const type = isImage(dataArray);\n        if (!type) {\n            throw new OperationError(\"Invalid file type.\");\n        }","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ImageFilter.mjs#L60-L96","documentation":"Wraps any error from the greyscale()/sepia() filter call or the subsequent getBuffer() inside Image Filter's inner try/catch. Surfaces the underlying Jimp error in the message so the user can tell whether filtering or encoding failed.","triggerScenarios":"Jimp filter rejecting on an unusual colour space or bit depth; getBuffer failing for an unsupported output mime; memory exhaustion on a large image in a worker; a Jimp regression in sepia/greyscale.","commonSituations":"16-bit or CMYK JPEG that Jimp mishandles; animated GIF where frame handling throws; oversized image; Jimp version bug.","solutions":["Read the embedded error to distinguish filter vs buffer failure.","Pre-convert exotic colour spaces/bit depths to 8-bit RGB PNG/JPEG.","Downscale large images or process outside a worker.","Try the other filter type to isolate a codec-specific bug."],"exampleFix":"// before: 16-bit CMYK JPEG trips the filter\nop.run(cmykJpegBuf, [\"Greyscale\"]);\n// after: convert to 8-bit sRGB first\nop.run(srgbPngBuf, [\"Greyscale\"]);","handlingStrategy":"fallback","validationCode":"function assertFilterType(t) {\n  if (!['Greyscale', 'Sepia'].includes(t)) {\n    throw new RangeError(`Unsupported filter type: ${t}`);\n  }\n}","typeGuard":"function isSupportedFilterType(t) {\n  return t === 'Greyscale' || t === 'Sepia';\n}","tryCatchPattern":"try {\n  result = await filter.run(buf, [filterType]);\n} catch (e) {\n  if (e instanceof OperationError && /applying filter/i.test(e.message)) {\n    // try the other filter, or pre-convert colour space\n    result = await filter.run(await toSrgbPng(buf), [filterType]);\n  } else throw e;\n}","preventionTips":["Pre-convert 16-bit/CMYK images to 8-bit sRGB.","Downscale large images or process outside a worker.","Try the alternate filter to isolate codec-specific bugs."],"tags":["image","jimp","processing-error","greyscale","sepia"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}