j433866 6 rokov pred
rodič
commit
1031429550

+ 21 - 13
src/core/operations/BlurImage.mjs

@@ -53,21 +53,29 @@ class BlurImage extends Operation {
         const type = Magic.magicFileType(input);
 
         if (type && type.mime.indexOf("image") === 0){
-            const image = await jimp.read(Buffer.from(input));
-
-            switch (blurType){
-                case "Fast":
-                    image.blur(blurAmount);
-                    break;
-                case "Gaussian":
-                    if (ENVIRONMENT_IS_WORKER())
-                        self.sendStatusMessage("Gaussian blurring image. This will take a while...");
-                    image.gaussian(blurAmount);
-                    break;
+            let image;
+            try {
+                image = await jimp.read(Buffer.from(input));
+            } catch (err) {
+                throw new OperationError(`Error loading image. (${err})`);
             }
+            try {
+                switch (blurType){
+                    case "Fast":
+                        image.blur(blurAmount);
+                        break;
+                    case "Gaussian":
+                        if (ENVIRONMENT_IS_WORKER())
+                            self.sendStatusMessage("Gaussian blurring image. This will take a while...");
+                        image.gaussian(blurAmount);
+                        break;
+                }
 
-            const imageBuffer = await image.getBufferAsync(jimp.AUTO);
-            return [...imageBuffer];
+                const imageBuffer = await image.getBufferAsync(jimp.AUTO);
+                return [...imageBuffer];
+            } catch (err) {
+                throw new OperationError(`Error blurring image. (${err})`);
+            }
         } else {
             throw new OperationError("Invalid file type.");
         }

+ 15 - 7
src/core/operations/ContainImage.mjs

@@ -106,13 +106,21 @@ class ContainImage extends Operation {
             throw new OperationError("Invalid file type.");
         }
 
-        const image = await jimp.read(Buffer.from(input));
-
-        if (ENVIRONMENT_IS_WORKER())
-            self.sendStatusMessage("Containing image...");
-        image.contain(width, height, alignMap[hAlign] | alignMap[vAlign], resizeMap[alg]);
-        const imageBuffer = await image.getBufferAsync(jimp.AUTO);
-        return [...imageBuffer];
+        let image;
+        try {
+            image = await jimp.read(Buffer.from(input));
+        } catch (err) {
+            throw new OperationError(`Error loading image. (${err})`);
+        }
+        try {
+            if (ENVIRONMENT_IS_WORKER())
+                self.sendStatusMessage("Containing image...");
+            image.contain(width, height, alignMap[hAlign] | alignMap[vAlign], resizeMap[alg]);
+            const imageBuffer = await image.getBufferAsync(jimp.AUTO);
+            return [...imageBuffer];
+        } catch (err) {
+            throw new OperationError(`Error containing image. (${err})`);
+        }
     }
 
     /**

+ 15 - 6
src/core/operations/CoverImage.mjs

@@ -106,12 +106,21 @@ class CoverImage extends Operation {
             throw new OperationError("Invalid file type.");
         }
 
-        const image = await jimp.read(Buffer.from(input));
-        if (ENVIRONMENT_IS_WORKER())
-            self.sendStatusMessage("Covering image...");
-        image.cover(width, height, alignMap[hAlign] | alignMap[vAlign], resizeMap[alg]);
-        const imageBuffer = await image.getBufferAsync(jimp.AUTO);
-        return [...imageBuffer];
+        let image;
+        try {
+            image = await jimp.read(Buffer.from(input));
+        } catch (err) {
+            throw new OperationError(`Error loading image. (${err})`);
+        }
+        try {
+            if (ENVIRONMENT_IS_WORKER())
+                self.sendStatusMessage("Covering image...");
+            image.cover(width, height, alignMap[hAlign] | alignMap[vAlign], resizeMap[alg]);
+            const imageBuffer = await image.getBufferAsync(jimp.AUTO);
+            return [...imageBuffer];
+        } catch (err) {
+            throw new OperationError(`Error covering image. (${err})`);
+        }
     }
 
     /**

+ 23 - 14
src/core/operations/CropImage.mjs

@@ -98,22 +98,31 @@ class CropImage extends Operation {
             throw new OperationError("Invalid file type.");
         }
 
-        const image = await jimp.read(Buffer.from(input));
-        if (ENVIRONMENT_IS_WORKER())
-            self.sendStatusMessage("Cropping image...");
-        if (autocrop) {
-            image.autocrop({
-                tolerance: (autoTolerance / 100),
-                cropOnlyFrames: autoFrames,
-                cropSymmetric: autoSymmetric,
-                leaveBorder: autoBorder
-            });
-        } else {
-            image.crop(xPos, yPos, width, height);
+        let image;
+        try {
+            image = await jimp.read(Buffer.from(input));
+        } catch (err) {
+            throw new OperationError(`Error loading image. (${err})`);
         }
+        try {
+            if (ENVIRONMENT_IS_WORKER())
+                self.sendStatusMessage("Cropping image...");
+            if (autocrop) {
+                image.autocrop({
+                    tolerance: (autoTolerance / 100),
+                    cropOnlyFrames: autoFrames,
+                    cropSymmetric: autoSymmetric,
+                    leaveBorder: autoBorder
+                });
+            } else {
+                image.crop(xPos, yPos, width, height);
+            }
 
-        const imageBuffer = await image.getBufferAsync(jimp.AUTO);
-        return [...imageBuffer];
+            const imageBuffer = await image.getBufferAsync(jimp.AUTO);
+            return [...imageBuffer];
+        } catch (err) {
+            throw new OperationError(`Error cropping image. (${err})`);
+        }
     }
 
     /**

+ 15 - 6
src/core/operations/DitherImage.mjs

@@ -40,12 +40,21 @@ class DitherImage extends Operation {
         const type = Magic.magicFileType(input);
 
         if (type && type.mime.indexOf("image") === 0){
-            const image = await jimp.read(Buffer.from(input));
-            if (ENVIRONMENT_IS_WORKER())
-                self.sendStatusMessage("Applying dither to image...");
-            image.dither565();
-            const imageBuffer = await image.getBufferAsync(jimp.AUTO);
-            return [...imageBuffer];
+            let image;
+            try {
+                image = await jimp.read(Buffer.from(input));
+            } catch (err) {
+                throw new OperationError(`Error loading image. (${err})`);
+            }
+            try {
+                if (ENVIRONMENT_IS_WORKER())
+                    self.sendStatusMessage("Applying dither to image...");
+                image.dither565();
+                const imageBuffer = await image.getBufferAsync(jimp.AUTO);
+                return [...imageBuffer];
+            } catch (err) {
+                throw new OperationError(`Error applying dither to image. (${err})`);
+            }
         } else {
             throw new OperationError("Invalid file type.");
         }

+ 21 - 13
src/core/operations/FlipImage.mjs

@@ -49,21 +49,29 @@ class FlipImage extends Operation {
             throw new OperationError("Invalid input file type.");
         }
 
-        const image = await jimp.read(Buffer.from(input));
-
-        if (ENVIRONMENT_IS_WORKER())
-            self.sendStatusMessage("Flipping image...");
-        switch (flipAxis){
-            case "Horizontal":
-                image.flip(true, false);
-                break;
-            case "Vertical":
-                image.flip(false, true);
-                break;
+        let image;
+        try {
+            image = await jimp.read(Buffer.from(input));
+        } catch (err) {
+            throw new OperationError(`Error loading image. (${err})`);
         }
+        try {
+            if (ENVIRONMENT_IS_WORKER())
+                self.sendStatusMessage("Flipping image...");
+            switch (flipAxis){
+                case "Horizontal":
+                    image.flip(true, false);
+                    break;
+                case "Vertical":
+                    image.flip(false, true);
+                    break;
+            }
 
-        const imageBuffer = await image.getBufferAsync(jimp.AUTO);
-        return [...imageBuffer];
+            const imageBuffer = await image.getBufferAsync(jimp.AUTO);
+            return [...imageBuffer];
+        } catch (err) {
+            throw new OperationError(`Error flipping image. (${err})`);
+        }
     }
 
     /**

+ 21 - 12
src/core/operations/ImageBrightnessContrast.mjs

@@ -58,20 +58,29 @@ class ImageBrightnessContrast extends Operation {
             throw new OperationError("Invalid file type.");
         }
 
-        const image = await jimp.read(Buffer.from(input));
-        if (brightness !== 0) {
-            if (ENVIRONMENT_IS_WORKER())
-                self.sendStatusMessage("Changing image brightness...");
-            image.brightness(brightness / 100);
-        }
-        if (contrast !== 0) {
-            if (ENVIRONMENT_IS_WORKER())
-                self.sendStatusMessage("Changing image contrast...");
-            image.contrast(contrast / 100);
+        let image;
+        try {
+            image = await jimp.read(Buffer.from(input));
+        } catch (err) {
+            throw new OperationError(`Error loading image. (${err})`);
         }
+        try {
+            if (brightness !== 0) {
+                if (ENVIRONMENT_IS_WORKER())
+                    self.sendStatusMessage("Changing image brightness...");
+                image.brightness(brightness / 100);
+            }
+            if (contrast !== 0) {
+                if (ENVIRONMENT_IS_WORKER())
+                    self.sendStatusMessage("Changing image contrast...");
+                image.contrast(contrast / 100);
+            }
 
-        const imageBuffer = await image.getBufferAsync(jimp.AUTO);
-        return [...imageBuffer];
+            const imageBuffer = await image.getBufferAsync(jimp.AUTO);
+            return [...imageBuffer];
+        } catch (err) {
+            throw new OperationError(`Error adjusting image brightness / contrast. (${err})`);
+        }
     }
 
     /**

+ 18 - 9
src/core/operations/ImageFilter.mjs

@@ -52,17 +52,26 @@ class ImageFilter extends Operation {
             throw new OperationError("Invalid file type.");
         }
 
-        const image = await jimp.read(Buffer.from(input));
-        if (ENVIRONMENT_IS_WORKER())
-            self.sendStatusMessage("Applying " + filterType.toLowerCase() + " filter to image...");
-        if (filterType === "Greyscale") {
-            image.greyscale();
-        } else {
-            image.sepia();
+        let image;
+        try {
+            image = await jimp.read(Buffer.from(input));
+        } catch (err) {
+            throw new OperationError(`Error loading image. (${err})`);
         }
+        try {
+            if (ENVIRONMENT_IS_WORKER())
+                self.sendStatusMessage("Applying " + filterType.toLowerCase() + " filter to image...");
+            if (filterType === "Greyscale") {
+                image.greyscale();
+            } else {
+                image.sepia();
+            }
 
-        const imageBuffer = await image.getBufferAsync(jimp.AUTO);
-        return [...imageBuffer];
+            const imageBuffer = await image.getBufferAsync(jimp.AUTO);
+            return [...imageBuffer];
+        } catch (err) {
+            throw new OperationError(`Error applying filter to image. (${err})`);
+        }
     }
 
     /**

+ 40 - 32
src/core/operations/ImageHueSaturationLightness.mjs

@@ -66,40 +66,48 @@ class ImageHueSaturationLightness extends Operation {
             throw new OperationError("Invalid file type.");
         }
 
-        const image = await jimp.read(Buffer.from(input));
-
-        if (hue !== 0) {
-            if (ENVIRONMENT_IS_WORKER())
-                self.sendStatusMessage("Changing image hue...");
-            image.colour([
-                {
-                    apply: "hue",
-                    params: [hue]
-                }
-            ]);
-        }
-        if (saturation !== 0) {
-            if (ENVIRONMENT_IS_WORKER())
-                self.sendStatusMessage("Changing image saturation...");
-            image.colour([
-                {
-                    apply: "saturate",
-                    params: [saturation]
-                }
-            ]);
+        let image;
+        try {
+            image = await jimp.read(Buffer.from(input));
+        } catch (err) {
+            throw new OperationError(`Error loading image. (${err})`);
         }
-        if (lightness !== 0) {
-            if (ENVIRONMENT_IS_WORKER())
-                self.sendStatusMessage("Changing image lightness...");
-            image.colour([
-                {
-                    apply: "lighten",
-                    params: [lightness]
-                }
-            ]);
+        try {
+            if (hue !== 0) {
+                if (ENVIRONMENT_IS_WORKER())
+                    self.sendStatusMessage("Changing image hue...");
+                image.colour([
+                    {
+                        apply: "hue",
+                        params: [hue]
+                    }
+                ]);
+            }
+            if (saturation !== 0) {
+                if (ENVIRONMENT_IS_WORKER())
+                    self.sendStatusMessage("Changing image saturation...");
+                image.colour([
+                    {
+                        apply: "saturate",
+                        params: [saturation]
+                    }
+                ]);
+            }
+            if (lightness !== 0) {
+                if (ENVIRONMENT_IS_WORKER())
+                    self.sendStatusMessage("Changing image lightness...");
+                image.colour([
+                    {
+                        apply: "lighten",
+                        params: [lightness]
+                    }
+                ]);
+            }
+            const imageBuffer = await image.getBufferAsync(jimp.AUTO);
+            return [...imageBuffer];
+        } catch (err) {
+            throw new OperationError(`Error adjusting image hue / saturation / lightness. (${err})`);
         }
-        const imageBuffer = await image.getBufferAsync(jimp.AUTO);
-        return [...imageBuffer];
     }
 
     /**

+ 15 - 6
src/core/operations/ImageOpacity.mjs

@@ -51,13 +51,22 @@ class ImageOpacity extends Operation {
             throw new OperationError("Invalid file type.");
         }
 
-        const image = await jimp.read(Buffer.from(input));
-        if (ENVIRONMENT_IS_WORKER())
-            self.sendStatusMessage("Changing image opacity...");
-        image.opacity(opacity / 100);
+        let image;
+        try {
+            image = await jimp.read(Buffer.from(input));
+        } catch (err) {
+            throw new OperationError(`Error loading image. (${err})`);
+        }
+        try {
+            if (ENVIRONMENT_IS_WORKER())
+                self.sendStatusMessage("Changing image opacity...");
+            image.opacity(opacity / 100);
 
-        const imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
-        return [...imageBuffer];
+            const imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
+            return [...imageBuffer];
+        } catch (err) {
+            throw new OperateionError(`Error changing image opacity. (${err})`);
+        }
     }
 
     /**

+ 16 - 6
src/core/operations/InvertImage.mjs

@@ -41,12 +41,22 @@ class InvertImage extends Operation {
         if (!type || type.mime.indexOf("image") !== 0) {
             throw new OperationError("Invalid input file format.");
         }
-        const image = await jimp.read(Buffer.from(input));
-        if (ENVIRONMENT_IS_WORKER())
-            self.sendStatusMessage("Inverting image...");
-        image.invert();
-        const imageBuffer = await image.getBufferAsync(jimp.AUTO);
-        return [...imageBuffer];
+
+        let image;
+        try {
+            image = await jimp.read(Buffer.from(input));
+        } catch (err) {
+            throw new OperationError(`Error loading image. (${err})`);
+        }
+        try {
+            if (ENVIRONMENT_IS_WORKER())
+                self.sendStatusMessage("Inverting image...");
+            image.invert();
+            const imageBuffer = await image.getBufferAsync(jimp.AUTO);
+            return [...imageBuffer];
+        } catch (err) {
+            throw new OperationError(`Error inverting image. (${err})`);
+        }
     }
 
     /**

+ 91 - 0
src/core/operations/NormaliseImage.mjs

@@ -0,0 +1,91 @@
+/**
+ * @author j433866 [j433866@gmail.com]
+ * @copyright Crown Copyright 2019
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+import OperationError from "../errors/OperationError";
+import Magic from "../lib/Magic";
+import { toBase64 } from "../lib/Base64";
+import jimp from "jimp";
+
+/**
+ * Normalise Image operation
+ */
+class NormaliseImage extends Operation {
+
+    /**
+     * NormaliseImage constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Normalise Image";
+        this.module = "Image";
+        this.description = "Normalise the image colours.";
+        this.infoURL = "";
+        this.inputType = "byteArray";
+        this.outputType = "byteArray";
+        this.presentType=  "html";
+        this.args = [
+            /* Example arguments. See the project wiki for full details.
+            {
+                name: "First arg",
+                type: "string",
+                value: "Don't Panic"
+            },
+            {
+                name: "Second arg",
+                type: "number",
+                value: 42
+            }
+            */
+        ];
+    }
+
+    /**
+     * @param {byteArray} input
+     * @param {Object[]} args
+     * @returns {byteArray}
+     */
+    async run(input, args) {
+        // const [firstArg, secondArg] = args;
+        const type = Magic.magicFileType(input);
+
+        if (!type || type.mime.indexOf("image") !== 0){
+            throw new OperationError("Invalid file type.");
+        }
+
+        const image = await jimp.read(Buffer.from(input));
+
+        image.normalize();
+
+        const imageBuffer = await image.getBufferAsync(jimp.AUTO);
+        return [...imageBuffer];
+    }
+
+    /**
+     * Displays the normalised image using HTML for web apps
+     * @param {byteArray} data
+     * @returns {html}
+     */
+    present(data) {
+        if (!data.length) return "";
+
+        let dataURI = "data:";
+        const type = Magic.magicFileType(data);
+        if (type && type.mime.indexOf("image") === 0){
+            dataURI += type.mime + ";";
+        } else {
+            throw new OperationError("Invalid file type.");
+        }
+        dataURI += "base64," + toBase64(data);
+
+        return "<img src='" + dataURI + "'>";
+
+    }
+
+}
+
+export default NormaliseImage;

+ 22 - 14
src/core/operations/ResizeImage.mjs

@@ -91,23 +91,31 @@ class ResizeImage extends Operation {
             throw new OperationError("Invalid file type.");
         }
 
-        const image = await jimp.read(Buffer.from(input));
-
-        if (unit === "Percent") {
-            width = image.getWidth() * (width / 100);
-            height = image.getHeight() * (height / 100);
+        let image;
+        try {
+            image = await jimp.read(Buffer.from(input));
+        } catch (err) {
+            throw new OperationError(`Error loading image. (${err})`);
         }
+        try {
+            if (unit === "Percent") {
+                width = image.getWidth() * (width / 100);
+                height = image.getHeight() * (height / 100);
+            }
 
-        if (ENVIRONMENT_IS_WORKER())
-            self.sendStatusMessage("Resizing image...");
-        if (aspect) {
-            image.scaleToFit(width, height, resizeMap[resizeAlg]);
-        } else {
-            image.resize(width, height, resizeMap[resizeAlg]);
-        }
+            if (ENVIRONMENT_IS_WORKER())
+                self.sendStatusMessage("Resizing image...");
+            if (aspect) {
+                image.scaleToFit(width, height, resizeMap[resizeAlg]);
+            } else {
+                image.resize(width, height, resizeMap[resizeAlg]);
+            }
 
-        const imageBuffer = await image.getBufferAsync(jimp.AUTO);
-        return [...imageBuffer];
+            const imageBuffer = await image.getBufferAsync(jimp.AUTO);
+            return [...imageBuffer];
+        } catch (err) {
+            throw new OperationError(`Error resizing image. (${err})`);
+        }
     }
 
     /**

+ 15 - 6
src/core/operations/RotateImage.mjs

@@ -47,12 +47,21 @@ class RotateImage extends Operation {
         const type = Magic.magicFileType(input);
 
         if (type && type.mime.indexOf("image") === 0){
-            const image = await jimp.read(Buffer.from(input));
-            if (ENVIRONMENT_IS_WORKER())
-                self.sendStatusMessage("Rotating image...");
-            image.rotate(degrees);
-            const imageBuffer = await image.getBufferAsync(jimp.AUTO);
-            return [...imageBuffer];
+            let image;
+            try {
+                image = await jimp.read(Buffer.from(input));
+            } catch (err) {
+                throw new OperationError(`Error loading image. (${err})`);
+            }
+            try {
+                if (ENVIRONMENT_IS_WORKER())
+                    self.sendStatusMessage("Rotating image...");
+                image.rotate(degrees);
+                const imageBuffer = await image.getBufferAsync(jimp.AUTO);
+                return [...imageBuffer];
+            } catch (err) {
+                throw new OperationError(`Error rotating image. (${err})`);
+            }
         } else {
             throw new OperationError("Invalid file type.");
         }