diff --git a/src/core/operations/ViewBitPlane.mjs b/src/core/operations/ViewBitPlane.mjs index 3740c10d..920e5bce 100644 --- a/src/core/operations/ViewBitPlane.mjs +++ b/src/core/operations/ViewBitPlane.mjs @@ -52,9 +52,15 @@ class ViewBitPlane extends Operation { if (!isImage(input)) throw new OperationError("Please enter a valid image file."); - const [colour, bit] = args, - parsedImage = await Jimp.read(input), - width = parsedImage.bitmap.width, + const [colour, bit] = args; + let parsedImage; + try { + parsedImage = await Jimp.read(input); + } catch (err) { + throw new OperationError(`Error loading image. (${err})`); + } + + const width = parsedImage.bitmap.width, height = parsedImage.bitmap.height, colourIndex = COLOUR_OPTIONS.indexOf(colour), bitIndex = 7 - bit; diff --git a/tests/operations/tests/Image.mjs b/tests/operations/tests/Image.mjs index fe6cab10..7da6c65d 100644 --- a/tests/operations/tests/Image.mjs +++ b/tests/operations/tests/Image.mjs @@ -241,6 +241,21 @@ TestRegister.addTests([ } ] }, + { + name: "View Bit Plane: malformed PNG", + input: PNG_HEX.replace("49484452", "49424452"), + expectedOutput: "Error loading image. (Error: unrecognised content at end of stream)", + recipeConfig: [ + { + op: "From Hex", + args: ["None"] + }, + { + op: "View Bit Plane", + args: ["Red", 0] + } + ] + }, { name: "Randomize Colour Palette", "input": PNG_HEX,