{"record":{"id":"cf20ec04b1ea2edf","repo":"gchq/CyberChef","slug":"error-inverting-image-err","errorCode":null,"errorMessage":"Error inverting image. (${err})","messagePattern":"Error inverting image\\. \\((.+?)\\)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/InvertImage.mjs","lineNumber":63,"sourceCode":"        try {\n            image = await Jimp.read(input);\n        } catch (err) {\n            throw new OperationError(`Error loading image. (${err})`);\n        }\n        try {\n            if (isWorkerEnvironment())\n                self.sendStatusMessage(\"Inverting image...\");\n            image.invert();\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(`Error inverting image. (${err})`);\n        }\n    }\n\n    /**\n     * Displays the inverted 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        }\n\n        return `<img src=\"data:${type};base64,${toBase64(dataArray)}\">`;","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/InvertImage.mjs#L45-L81","documentation":"Catches any exception during image.invert() or getBuffer and re-wraps it. invert() is robust for most images, so this usually means a Jimp internal failure, a corrupted pixel buffer, or a getBuffer encode error (GIF outputs are re-encoded as PNG). The interpolated err carries the Jimp cause.","triggerScenarios":"Jimp internal failure on an unusual pixel layout, getBuffer failing during PNG re-encode, or memory exhaustion on a very large image during the invert pass.","commonSituations":"Oversized image, corrupted pixels surviving read, or a jimp version bug on a specific codec.","solutions":["Downscale the image and retry to rule out memory/timeout.","Re-encode the source to PNG so the encode path is clean.","If err names a Jimp method, pin/upgrade jimp.","Validate the image opens in an image editor before processing."],"exampleFix":"// before\ninvertOp.run(hugeCorruptPng, []); // -> Error inverting image.\n// after\nconst safe = await reencodeToPng(hugeCorruptPng, { maxWidth: 4000 });\ninvertOp.run(safe, []);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await invertOp.run(buf, []);\n} catch (e) {\n  if (e instanceof OperationError && /Error inverting image/.test(e.message)) {\n    // Likely oversized/corrupt pixels: downscale or re-encode, then retry.\n    await retryWithReencodedPng(buf);\n  } else throw e;\n}","preventionTips":["Downscale very large images before processing.","Re-encode to PNG so the encode path is clean.","Pin a compatible jimp version.","Validate the image opens in an editor first."],"tags":["image","jimp","error-handling","cyberchef"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}