{"record":{"id":"c53beeb195783205","repo":"gchq/CyberChef","slug":"error-adjusting-image-brightness-or-contrast-e","errorCode":null,"errorMessage":"Error adjusting image brightness or contrast. (${err})","messagePattern":"Error adjusting image brightness or contrast\\. \\((.+?)\\)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"warning","filePath":"src/core/operations/ImageBrightnessContrast.mjs","lineNumber":86,"sourceCode":"                if (isWorkerEnvironment())\n                    self.sendStatusMessage(\"Changing image brightness...\");\n                image.brightness(brightness / 100);\n            }\n            if (contrast !== 0) {\n                if (isWorkerEnvironment())\n                    self.sendStatusMessage(\"Changing image contrast...\");\n                image.contrast(contrast / 100);\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 adjusting image brightness or contrast. (${err})`,\n            );\n        }\n    }\n\n    /**\n     * Displays the 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":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ImageBrightnessContrast.mjs#L68-L104","documentation":"Thrown by the inner try/catch of Image Brightness/Contrast when either the brightness()/contrast() mutation or the final getBuffer() fails. Wraps the underlying Jimp/runtime error so the operation surfaces a readable message rather than an unhandled rejection.","triggerScenarios":"brightness/contrast values that drive pixels out of representable range in a Jimp version that throws on overflow; getBuffer rejecting for an unsupported output mime; a worker environment memory error during processing of a very large image.","commonSituations":"Extreme argument values (brightness/contrast far outside [-100,100]); processing a huge image in a memory-constrained web worker; a Jimp regression where gif->png conversion fails.","solutions":["Read the interpolated error to identify mutation vs buffer stage.","Use moderate brightness/contrast values within [-100, 100].","Downscale very large images before invoking, or run outside a worker.","Update/roll back the jimp dependency if a codec bug is suspected."],"exampleFix":"// before: extreme contrast drives a Jimp throw\nop.run(buf, [0, 500]);\n// after: stay within the documented range\nop.run(buf, [0, 50]);","handlingStrategy":"validation","validationCode":"function assertBrightnessContrastRange(b, c) {\n  if (!Number.isFinite(b) || Math.abs(b) > 100) throw new RangeError(`brightness out of range: ${b}`);\n  if (!Number.isFinite(c) || Math.abs(c) > 100) throw new RangeError(`contrast out of range: ${c}`);\n}","typeGuard":"function isValidBrightnessContrast(b, c) {\n  return [b, c].every(v => Number.isFinite(v) && Math.abs(v) <= 100);\n}","tryCatchPattern":"try {\n  result = await brightness.run(buf, [b, c]);\n} catch (e) {\n  if (e instanceof OperationError && /brightness or contrast/i.test(e.message)) {\n    // clamp to a safe range and retry\n    result = await brightness.run(buf, [Math.clamp(b,-100,100), Math.clamp(c,-100,100)]);\n  } else throw e;\n}","preventionTips":["Keep brightness/contrast within [-100, 100].","Downscale very large images before processing.","Update Jimp if a codec regression is suspected."],"tags":["image","jimp","processing-error","brightness","contrast"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}