From e44a22e143520fcef46054b593c117440003a71d Mon Sep 17 00:00:00 2001 From: j433866 Date: Mon, 1 Apr 2019 10:54:46 +0100 Subject: [PATCH] Change ops to use ArrayBuffer instead of byteArray --- src/core/operations/AddTextToImage.mjs | 23 ++++++++++--------- src/core/operations/BlurImage.mjs | 21 +++++++++-------- src/core/operations/ContainImage.mjs | 21 +++++++++-------- src/core/operations/ConvertImageFormat.mjs | 23 ++++++++++--------- src/core/operations/CoverImage.mjs | 21 +++++++++-------- src/core/operations/CropImage.mjs | 21 +++++++++-------- src/core/operations/DitherImage.mjs | 21 +++++++++-------- src/core/operations/FlipImage.mjs | 21 +++++++++-------- .../operations/ImageBrightnessContrast.mjs | 21 +++++++++-------- src/core/operations/ImageFilter.mjs | 21 +++++++++-------- .../ImageHueSaturationLightness.mjs | 21 +++++++++-------- src/core/operations/ImageOpacity.mjs | 21 +++++++++-------- src/core/operations/InvertImage.mjs | 21 +++++++++-------- src/core/operations/NormaliseImage.mjs | 21 +++++++++-------- src/core/operations/ParseQRCode.mjs | 8 +++---- src/core/operations/ResizeImage.mjs | 21 +++++++++-------- src/core/operations/RotateImage.mjs | 21 +++++++++-------- src/core/operations/SharpenImage.mjs | 23 ++++++++++--------- 18 files changed, 194 insertions(+), 177 deletions(-) diff --git a/src/core/operations/AddTextToImage.mjs b/src/core/operations/AddTextToImage.mjs index 56c455b6..99ea7bd0 100644 --- a/src/core/operations/AddTextToImage.mjs +++ b/src/core/operations/AddTextToImage.mjs @@ -25,8 +25,8 @@ class AddTextToImage extends Operation { this.module = "Image"; this.description = "Adds text onto an image.

Text can be horizontally or vertically aligned, or the position can be manually specified.
Variants of the Roboto font face are available in any size or colour.

Note: This may cause a degradation in image quality, especially when using font sizes larger than 72."; this.infoURL = ""; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType = "html"; this.args = [ { @@ -102,7 +102,7 @@ class AddTextToImage extends Operation { } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ @@ -120,13 +120,13 @@ class AddTextToImage extends Operation { let xPos = args[3], yPos = args[4]; - if (!isImage(input)) { + if (!isImage(new Uint8Array(input))) { throw new OperationError("Invalid file type."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error loading image. (${err})`); } @@ -147,7 +147,7 @@ class AddTextToImage extends Operation { fontsMap.Roboto = fonts[0]; fontsMap["Roboto Black"] = fonts[1]; fontsMap["Roboto Mono"] = fonts[2]; - fontsMap["Roboto Slab"] = fonts[3] + fontsMap["Roboto Slab"] = fonts[3]; }); @@ -248,7 +248,7 @@ class AddTextToImage extends Operation { } else { imageBuffer = await image.getBufferAsync(jimp.AUTO); } - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error adding text to image. (${err})`); } @@ -257,18 +257,19 @@ class AddTextToImage extends Operation { /** * Displays the blurred image using HTML for web apps * - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { throw new OperationError("Invalid file type."); } - return ``; + return ``; } } diff --git a/src/core/operations/BlurImage.mjs b/src/core/operations/BlurImage.mjs index 68c8dbd6..d112d7c8 100644 --- a/src/core/operations/BlurImage.mjs +++ b/src/core/operations/BlurImage.mjs @@ -26,8 +26,8 @@ class BlurImage extends Operation { this.module = "Image"; this.description = "Applies a blur effect to the image.

Gaussian blur is much slower than fast blur, but produces better results."; this.infoURL = "https://wikipedia.org/wiki/Gaussian_blur"; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType = "html"; this.args = [ { @@ -45,20 +45,20 @@ class BlurImage extends Operation { } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ async run(input, args) { const [blurAmount, blurType] = args; - if (!isImage(input)) { + if (!isImage(new Uint8Array(input))) { throw new OperationError("Invalid file type."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error loading image. (${err})`); } @@ -82,7 +82,7 @@ class BlurImage extends Operation { } else { imageBuffer = await image.getBufferAsync(jimp.AUTO); } - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error blurring image. (${err})`); } @@ -91,18 +91,19 @@ class BlurImage extends Operation { /** * Displays the blurred image using HTML for web apps * - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { throw new OperationError("Invalid file type."); } - return ``; + return ``; } } diff --git a/src/core/operations/ContainImage.mjs b/src/core/operations/ContainImage.mjs index e16d3d58..e6edd9e1 100644 --- a/src/core/operations/ContainImage.mjs +++ b/src/core/operations/ContainImage.mjs @@ -25,8 +25,8 @@ class ContainImage extends Operation { this.module = "Image"; this.description = "Scales an image to the specified width and height, maintaining the aspect ratio. The image may be letterboxed."; this.infoURL = ""; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType = "html"; this.args = [ { @@ -82,7 +82,7 @@ class ContainImage extends Operation { } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ @@ -106,13 +106,13 @@ class ContainImage extends Operation { "Bottom": jimp.VERTICAL_ALIGN_BOTTOM }; - if (!isImage(input)) { + if (!isImage(new Uint8Array(input))) { throw new OperationError("Invalid file type."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error loading image. (${err})`); } @@ -133,7 +133,7 @@ class ContainImage extends Operation { } else { imageBuffer = await image.getBufferAsync(jimp.AUTO); } - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error containing image. (${err})`); } @@ -141,18 +141,19 @@ class ContainImage extends Operation { /** * Displays the contained image using HTML for web apps - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { throw new OperationError("Invalid file type."); } - return ``; + return ``; } } diff --git a/src/core/operations/ConvertImageFormat.mjs b/src/core/operations/ConvertImageFormat.mjs index 5900fbbc..cdcfc249 100644 --- a/src/core/operations/ConvertImageFormat.mjs +++ b/src/core/operations/ConvertImageFormat.mjs @@ -25,8 +25,8 @@ class ConvertImageFormat extends Operation { this.module = "Image"; this.description = "Converts an image between different formats. Supported formats:

Note: GIF files are supported for input, but cannot be outputted."; this.infoURL = "https://wikipedia.org/wiki/Image_file_formats"; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType = "html"; this.args = [ { @@ -69,7 +69,7 @@ class ConvertImageFormat extends Operation { } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ @@ -93,12 +93,12 @@ class ConvertImageFormat extends Operation { const mime = formatMap[format]; - if (!isImage(input)) { + if (!isImage(new Uint8Array(input))) { throw new OperationError("Invalid file format."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error opening image file. (${err})`); } @@ -114,7 +114,7 @@ class ConvertImageFormat extends Operation { } const imageBuffer = await image.getBufferAsync(mime); - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error converting image format. (${err})`); } @@ -123,18 +123,19 @@ class ConvertImageFormat extends Operation { /** * Displays the converted image using HTML for web apps * - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { - throw new OperationError("Invalid image type."); + throw new OperationError("Invalid file type."); } - return ``; + return ``; } } diff --git a/src/core/operations/CoverImage.mjs b/src/core/operations/CoverImage.mjs index 492938ff..e55b4da1 100644 --- a/src/core/operations/CoverImage.mjs +++ b/src/core/operations/CoverImage.mjs @@ -25,8 +25,8 @@ class CoverImage extends Operation { this.module = "Image"; this.description = "Scales the image to the given width and height, keeping the aspect ratio. The image may be clipped."; this.infoURL = ""; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType = "html"; this.args = [ { @@ -77,7 +77,7 @@ class CoverImage extends Operation { } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ @@ -101,13 +101,13 @@ class CoverImage extends Operation { "Bottom": jimp.VERTICAL_ALIGN_BOTTOM }; - if (!isImage(input)) { + if (!isImage(new Uint8Array(input))) { throw new OperationError("Invalid file type."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error loading image. (${err})`); } @@ -121,7 +121,7 @@ class CoverImage extends Operation { } else { imageBuffer = await image.getBufferAsync(jimp.AUTO); } - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error covering image. (${err})`); } @@ -129,18 +129,19 @@ class CoverImage extends Operation { /** * Displays the covered image using HTML for web apps - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { throw new OperationError("Invalid file type."); } - return ``; + return ``; } } diff --git a/src/core/operations/CropImage.mjs b/src/core/operations/CropImage.mjs index fd04a907..a1114bea 100644 --- a/src/core/operations/CropImage.mjs +++ b/src/core/operations/CropImage.mjs @@ -25,8 +25,8 @@ class CropImage extends Operation { this.module = "Image"; this.description = "Crops an image to the specified region, or automatically crops edges.

Autocrop
Automatically crops same-colour borders from the image.

Autocrop tolerance
A percentage value for the tolerance of colour difference between pixels.

Only autocrop frames
Only crop real frames (all sides must have the same border)

Symmetric autocrop
Force autocrop to be symmetric (top/bottom and left/right are cropped by the same amount)

Autocrop keep border
The number of pixels of border to leave around the image."; this.infoURL = "https://wikipedia.org/wiki/Cropping_(image)"; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType = "html"; this.args = [ { @@ -86,19 +86,19 @@ class CropImage extends Operation { } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ async run(input, args) { const [xPos, yPos, width, height, autocrop, autoTolerance, autoFrames, autoSymmetric, autoBorder] = args; - if (!isImage(input)) { + if (!isImage(new Uint8Array(input))) { throw new OperationError("Invalid file type."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error loading image. (${err})`); } @@ -122,7 +122,7 @@ class CropImage extends Operation { } else { imageBuffer = await image.getBufferAsync(jimp.AUTO); } - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error cropping image. (${err})`); } @@ -130,18 +130,19 @@ class CropImage extends Operation { /** * Displays the cropped image using HTML for web apps - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { throw new OperationError("Invalid file type."); } - return ``; + return ``; } } diff --git a/src/core/operations/DitherImage.mjs b/src/core/operations/DitherImage.mjs index 3e603016..aee70389 100644 --- a/src/core/operations/DitherImage.mjs +++ b/src/core/operations/DitherImage.mjs @@ -25,25 +25,25 @@ class DitherImage extends Operation { this.module = "Image"; this.description = "Apply a dither effect to an image."; this.infoURL = "https://wikipedia.org/wiki/Dither"; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType = "html"; this.args = []; } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ async run(input, args) { - if (!isImage(input)) { + if (!isImage(new Uint8Array(input))) { throw new OperationError("Invalid file type."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error loading image. (${err})`); } @@ -58,7 +58,7 @@ class DitherImage extends Operation { } else { imageBuffer = await image.getBufferAsync(jimp.AUTO); } - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error applying dither to image. (${err})`); } @@ -66,18 +66,19 @@ class DitherImage extends Operation { /** * Displays the dithered image using HTML for web apps - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { throw new OperationError("Invalid file type."); } - return ``; + return ``; } } diff --git a/src/core/operations/FlipImage.mjs b/src/core/operations/FlipImage.mjs index cebba0e0..16793441 100644 --- a/src/core/operations/FlipImage.mjs +++ b/src/core/operations/FlipImage.mjs @@ -25,8 +25,8 @@ class FlipImage extends Operation { this.module = "Image"; this.description = "Flips an image along its X or Y axis."; this.infoURL = ""; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType = "html"; this.args = [ { @@ -38,19 +38,19 @@ class FlipImage extends Operation { } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ async run(input, args) { const [flipAxis] = args; - if (!isImage(input)) { + if (!isImage(new Uint8Array(input))) { throw new OperationError("Invalid input file type."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error loading image. (${err})`); } @@ -72,7 +72,7 @@ class FlipImage extends Operation { } else { imageBuffer = await image.getBufferAsync(jimp.AUTO); } - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error flipping image. (${err})`); } @@ -80,18 +80,19 @@ class FlipImage extends Operation { /** * Displays the flipped image using HTML for web apps - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { throw new OperationError("Invalid file type."); } - return ``; + return ``; } } diff --git a/src/core/operations/ImageBrightnessContrast.mjs b/src/core/operations/ImageBrightnessContrast.mjs index f47134a2..f1c8f4e0 100644 --- a/src/core/operations/ImageBrightnessContrast.mjs +++ b/src/core/operations/ImageBrightnessContrast.mjs @@ -25,8 +25,8 @@ class ImageBrightnessContrast extends Operation { this.module = "Image"; this.description = "Adjust the brightness or contrast of an image."; this.infoURL = ""; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType = "html"; this.args = [ { @@ -47,19 +47,19 @@ class ImageBrightnessContrast extends Operation { } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ async run(input, args) { const [brightness, contrast] = args; - if (!isImage(input)) { + if (!isImage(new Uint8Array(input))) { throw new OperationError("Invalid file type."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error loading image. (${err})`); } @@ -81,7 +81,7 @@ class ImageBrightnessContrast extends Operation { } else { imageBuffer = await image.getBufferAsync(jimp.AUTO); } - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error adjusting image brightness or contrast. (${err})`); } @@ -89,18 +89,19 @@ class ImageBrightnessContrast extends Operation { /** * Displays the image using HTML for web apps - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { throw new OperationError("Invalid file type."); } - return ``; + return ``; } } diff --git a/src/core/operations/ImageFilter.mjs b/src/core/operations/ImageFilter.mjs index 6c1a5a65..be96c046 100644 --- a/src/core/operations/ImageFilter.mjs +++ b/src/core/operations/ImageFilter.mjs @@ -25,8 +25,8 @@ class ImageFilter extends Operation { this.module = "Image"; this.description = "Applies a greyscale or sepia filter to an image."; this.infoURL = ""; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType = "html"; this.args = [ { @@ -41,19 +41,19 @@ class ImageFilter extends Operation { } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ async run(input, args) { const [filterType] = args; - if (!isImage(input)){ + if (!isImage(new Uint8Array(input))){ throw new OperationError("Invalid file type."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error loading image. (${err})`); } @@ -72,7 +72,7 @@ class ImageFilter extends Operation { } else { imageBuffer = await image.getBufferAsync(jimp.AUTO); } - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error applying filter to image. (${err})`); } @@ -80,18 +80,19 @@ class ImageFilter extends Operation { /** * Displays the blurred image using HTML for web apps - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { throw new OperationError("Invalid file type."); } - return ``; + return ``; } } diff --git a/src/core/operations/ImageHueSaturationLightness.mjs b/src/core/operations/ImageHueSaturationLightness.mjs index 4bdd9fa7..d5b3718b 100644 --- a/src/core/operations/ImageHueSaturationLightness.mjs +++ b/src/core/operations/ImageHueSaturationLightness.mjs @@ -25,8 +25,8 @@ class ImageHueSaturationLightness extends Operation { this.module = "Image"; this.description = "Adjusts the hue / saturation / lightness (HSL) values of an image."; this.infoURL = ""; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType = "html"; this.args = [ { @@ -54,20 +54,20 @@ class ImageHueSaturationLightness extends Operation { } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ async run(input, args) { const [hue, saturation, lightness] = args; - if (!isImage(input)) { + if (!isImage(new Uint8Array(input))) { throw new OperationError("Invalid file type."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error loading image. (${err})`); } @@ -109,7 +109,7 @@ class ImageHueSaturationLightness extends Operation { } else { imageBuffer = await image.getBufferAsync(jimp.AUTO); } - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error adjusting image hue / saturation / lightness. (${err})`); } @@ -117,18 +117,19 @@ class ImageHueSaturationLightness extends Operation { /** * Displays the image using HTML for web apps - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { throw new OperationError("Invalid file type."); } - return ``; + return ``; } } diff --git a/src/core/operations/ImageOpacity.mjs b/src/core/operations/ImageOpacity.mjs index afa370ff..4303fcbf 100644 --- a/src/core/operations/ImageOpacity.mjs +++ b/src/core/operations/ImageOpacity.mjs @@ -25,8 +25,8 @@ class ImageOpacity extends Operation { this.module = "Image"; this.description = "Adjust the opacity of an image."; this.infoURL = ""; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType = "html"; this.args = [ { @@ -40,19 +40,19 @@ class ImageOpacity extends Operation { } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ async run(input, args) { const [opacity] = args; - if (!isImage(input)) { + if (!isImage(new Uint8Array(input))) { throw new OperationError("Invalid file type."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error loading image. (${err})`); } @@ -67,7 +67,7 @@ class ImageOpacity extends Operation { } else { imageBuffer = await image.getBufferAsync(jimp.AUTO); } - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error changing image opacity. (${err})`); } @@ -75,18 +75,19 @@ class ImageOpacity extends Operation { /** * Displays the image using HTML for web apps - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { throw new OperationError("Invalid file type."); } - return ``; + return ``; } } diff --git a/src/core/operations/InvertImage.mjs b/src/core/operations/InvertImage.mjs index 7c7a4d09..9c29f38a 100644 --- a/src/core/operations/InvertImage.mjs +++ b/src/core/operations/InvertImage.mjs @@ -25,25 +25,25 @@ class InvertImage extends Operation { this.module = "Image"; this.description = "Invert the colours of an image."; this.infoURL = ""; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType = "html"; this.args = []; } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ async run(input, args) { - if (!isImage(input)) { + if (!isImage(new Uint8Array(input))) { throw new OperationError("Invalid input file format."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error loading image. (${err})`); } @@ -58,7 +58,7 @@ class InvertImage extends Operation { } else { imageBuffer = await image.getBufferAsync(jimp.AUTO); } - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error inverting image. (${err})`); } @@ -66,18 +66,19 @@ class InvertImage extends Operation { /** * Displays the inverted image using HTML for web apps - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { throw new OperationError("Invalid file type."); } - return ``; + return ``; } } diff --git a/src/core/operations/NormaliseImage.mjs b/src/core/operations/NormaliseImage.mjs index c49cc977..befc8a92 100644 --- a/src/core/operations/NormaliseImage.mjs +++ b/src/core/operations/NormaliseImage.mjs @@ -25,25 +25,25 @@ class NormaliseImage extends Operation { this.module = "Image"; this.description = "Normalise the image colours."; this.infoURL = ""; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType= "html"; this.args = []; } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ async run(input, args) { - if (!isImage(input)) { + if (!isImage(new Uint8Array(input))) { throw new OperationError("Invalid file type."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error opening image file. (${err})`); } @@ -57,7 +57,7 @@ class NormaliseImage extends Operation { } else { imageBuffer = await image.getBufferAsync(jimp.AUTO); } - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error normalising image. (${err})`); } @@ -65,18 +65,19 @@ class NormaliseImage extends Operation { /** * Displays the normalised image using HTML for web apps - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { throw new OperationError("Invalid file type."); } - return ``; + return ``; } } diff --git a/src/core/operations/ParseQRCode.mjs b/src/core/operations/ParseQRCode.mjs index ef7af6d7..4c1e0fee 100644 --- a/src/core/operations/ParseQRCode.mjs +++ b/src/core/operations/ParseQRCode.mjs @@ -25,7 +25,7 @@ class ParseQRCode extends Operation { this.module = "Image"; this.description = "Reads an image file and attempts to detect and read a Quick Response (QR) code from the image.

Normalise Image
Attempts to normalise the image before parsing it to improve detection of a QR code."; this.infoURL = "https://wikipedia.org/wiki/QR_code"; - this.inputType = "byteArray"; + this.inputType = "ArrayBuffer"; this.outputType = "string"; this.args = [ { @@ -37,7 +37,7 @@ class ParseQRCode extends Operation { } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {string} */ @@ -45,7 +45,7 @@ class ParseQRCode extends Operation { const [normalise] = args; // Make sure that the input is an image - if (!isImage(input)) throw new OperationError("Invalid file type."); + if (!isImage(new Uint8Array(input))) throw new OperationError("Invalid file type."); let image = input; @@ -57,7 +57,7 @@ class ParseQRCode extends Operation { // Makes the image greyscale // Converts image to a JPEG image = await new Promise((resolve, reject) => { - jimp.read(Buffer.from(input)) + jimp.read(input) .then(image => { image .rgba(false) diff --git a/src/core/operations/ResizeImage.mjs b/src/core/operations/ResizeImage.mjs index 11266ba8..1a1521e6 100644 --- a/src/core/operations/ResizeImage.mjs +++ b/src/core/operations/ResizeImage.mjs @@ -25,8 +25,8 @@ class ResizeImage extends Operation { this.module = "Image"; this.description = "Resizes an image to the specified width and height values."; this.infoURL = "https://wikipedia.org/wiki/Image_scaling"; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType = "html"; this.args = [ { @@ -67,7 +67,7 @@ class ResizeImage extends Operation { } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ @@ -86,13 +86,13 @@ class ResizeImage extends Operation { "Bezier": jimp.RESIZE_BEZIER }; - if (!isImage(input)) { + if (!isImage(new Uint8Array(input))) { throw new OperationError("Invalid file type."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error loading image. (${err})`); } @@ -116,7 +116,7 @@ class ResizeImage extends Operation { } else { imageBuffer = await image.getBufferAsync(jimp.AUTO); } - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error resizing image. (${err})`); } @@ -124,18 +124,19 @@ class ResizeImage extends Operation { /** * Displays the resized image using HTML for web apps - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { throw new OperationError("Invalid file type."); } - return ``; + return ``; } } diff --git a/src/core/operations/RotateImage.mjs b/src/core/operations/RotateImage.mjs index 16330c44..08e345f0 100644 --- a/src/core/operations/RotateImage.mjs +++ b/src/core/operations/RotateImage.mjs @@ -25,8 +25,8 @@ class RotateImage extends Operation { this.module = "Image"; this.description = "Rotates an image by the specified number of degrees."; this.infoURL = ""; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType = "html"; this.args = [ { @@ -38,20 +38,20 @@ class RotateImage extends Operation { } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ async run(input, args) { const [degrees] = args; - if (!isImage(input)) { + if (!isImage(new Uint8Array(input))) { throw new OperationError("Invalid file type."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error loading image. (${err})`); } @@ -66,7 +66,7 @@ class RotateImage extends Operation { } else { imageBuffer = await image.getBufferAsync(jimp.AUTO); } - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error rotating image. (${err})`); } @@ -74,18 +74,19 @@ class RotateImage extends Operation { /** * Displays the rotated image using HTML for web apps - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { throw new OperationError("Invalid file type."); } - return ``; + return ``; } } diff --git a/src/core/operations/SharpenImage.mjs b/src/core/operations/SharpenImage.mjs index de2f3603..d7a17357 100644 --- a/src/core/operations/SharpenImage.mjs +++ b/src/core/operations/SharpenImage.mjs @@ -26,8 +26,8 @@ class SharpenImage extends Operation { this.module = "Image"; this.description = "Sharpens an image (Unsharp mask)"; this.infoURL = "https://wikipedia.org/wiki/Unsharp_masking"; - this.inputType = "byteArray"; - this.outputType = "byteArray"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; this.presentType = "html"; this.args = [ { @@ -54,20 +54,20 @@ class SharpenImage extends Operation { } /** - * @param {byteArray} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {byteArray} */ async run(input, args) { const [radius, amount, threshold] = args; - if (!isImage(input)){ + if (!isImage(new Uint8Array(input))){ throw new OperationError("Invalid file type."); } let image; try { - image = await jimp.read(Buffer.from(input)); + image = await jimp.read(input); } catch (err) { throw new OperationError(`Error loading image. (${err})`); } @@ -140,7 +140,7 @@ class SharpenImage extends Operation { } else { imageBuffer = await image.getBufferAsync(jimp.AUTO); } - return [...imageBuffer]; + return imageBuffer.buffer; } catch (err) { throw new OperationError(`Error sharpening image. (${err})`); } @@ -148,18 +148,19 @@ class SharpenImage extends Operation { /** * Displays the sharpened image using HTML for web apps - * @param {byteArray} data + * @param {ArrayBuffer} data * @returns {html} */ present(data) { - if (!data.length) return ""; + if (!data.byteLength) return ""; + const dataArray = new Uint8Array(data); - const type = isImage(data); + const type = isImage(dataArray); if (!type) { - throw new OperationError("Invalid image type."); + throw new OperationError("Invalid file type."); } - return ``; + return ``; } }