From 21a8d0320190644148072e45ed46610fbb14824e Mon Sep 17 00:00:00 2001 From: j433866 Date: Thu, 7 Mar 2019 13:21:26 +0000 Subject: [PATCH 1/5] Move parsing and generation of QR codes to lib folder. Also rewrote QR code parsing to be more readable and actually error out properly. --- src/core/lib/QRCode.mjs | 90 ++++++++++++++++++++++++++ src/core/operations/GenerateQRCode.mjs | 26 +------- src/core/operations/ParseQRCode.mjs | 60 ++--------------- 3 files changed, 96 insertions(+), 80 deletions(-) create mode 100644 src/core/lib/QRCode.mjs diff --git a/src/core/lib/QRCode.mjs b/src/core/lib/QRCode.mjs new file mode 100644 index 00000000..bf134367 --- /dev/null +++ b/src/core/lib/QRCode.mjs @@ -0,0 +1,90 @@ +/** + * QR code resources + * + * @author j433866 [j433866@gmail.com] + * @copyright Crown Copyright 2019 + * @license Apache-2.0 + */ + +import OperationError from "../errors/OperationError"; +import jsQR from "jsqr"; +import qr from "qr-image"; +import jimp from "jimp"; + +/** + * Parses a QR code image from an image + * + * @param {byteArray} input + * @param {boolean} normalise + * @returns {string} + */ +export async function parseQrCode(input, normalise) { + let image; + try { + image = await jimp.read(Buffer.from(input)); + } catch (err) { + throw new OperationError(`Error opening image. (${err})`); + } + + try { + if (normalise) { + image.rgba(false); + image.background(0xFFFFFFFF); + image.normalize(); + image.greyscale(); + } + } catch (err) { + throw new OperationError(`Error normalising iamge. (${err})`); + } + + const qrData = jsQR(image.bitmap.data, image.getWidth(), image.getHeight()); + if (qrData) { + return qrData.data; + } else { + throw new OperationError("Could not read a QR code from the image."); + } +} + +/** + * Generates a QR code from the input string + * + * @param {string} input + * @param {string} format + * @param {number} moduleSize + * @param {number} margin + * @param {string} errorCorrection + * @returns {byteArray} + */ +export function generateQrCode(input, format, moduleSize, margin, errorCorrection) { + const formats = ["SVG", "EPS", "PDF", "PNG"]; + if (!formats.includes(format.toUpperCase())) { + throw new OperationError("Unsupported QR code format."); + } + + let qrImage; + try { + qrImage = qr.imageSync(input, { + type: format, + size: moduleSize, + margin: margin, + "ec_level": errorCorrection.charAt(0).toUpperCase() + }); + } catch (err) { + throw new OperationError(`Error generating QR code. (${err})`); + } + + if (!qrImage) { + throw new OperationError("Error generating QR code."); + } + + switch (format) { + case "SVG": + case "EPS": + case "PDF": + return [...Buffer.from(qrImage)]; + case "PNG": + return [...qrImage]; + default: + throw new OperationError("Unsupported QR code format."); + } +} diff --git a/src/core/operations/GenerateQRCode.mjs b/src/core/operations/GenerateQRCode.mjs index edab6d40..4e1983e5 100644 --- a/src/core/operations/GenerateQRCode.mjs +++ b/src/core/operations/GenerateQRCode.mjs @@ -6,7 +6,7 @@ import Operation from "../Operation"; import OperationError from "../errors/OperationError"; -import qr from "qr-image"; +import { generateQrCode } from "../lib/QRCode"; import { toBase64 } from "../lib/Base64"; import Magic from "../lib/Magic"; import Utils from "../Utils"; @@ -62,29 +62,7 @@ class GenerateQRCode extends Operation { run(input, args) { const [format, size, margin, errorCorrection] = args; - // Create new QR image from the input data, and convert it to a buffer - const qrImage = qr.imageSync(input, { - type: format, - size: size, - margin: margin, - "ec_level": errorCorrection.charAt(0).toUpperCase() - }); - - if (qrImage == null) { - throw new OperationError("Error generating QR code."); - } - - switch (format) { - case "SVG": - case "EPS": - case "PDF": - return [...Buffer.from(qrImage)]; - case "PNG": - // Return the QR image buffer as a byte array - return [...qrImage]; - default: - throw new OperationError("Unsupported QR code format."); - } + return generateQrCode(input, format, size, margin, errorCorrection); } /** diff --git a/src/core/operations/ParseQRCode.mjs b/src/core/operations/ParseQRCode.mjs index 75a24d55..816b6e75 100644 --- a/src/core/operations/ParseQRCode.mjs +++ b/src/core/operations/ParseQRCode.mjs @@ -7,8 +7,7 @@ import Operation from "../Operation"; import OperationError from "../errors/OperationError"; import Magic from "../lib/Magic"; -import jsqr from "jsqr"; -import jimp from "jimp"; +import { parseQrCode } from "../lib/QRCode"; /** * Parse QR Code operation @@ -42,64 +41,13 @@ class ParseQRCode extends Operation { * @returns {string} */ async run(input, args) { - const type = Magic.magicFileType(input); const [normalise] = args; + const type = Magic.magicFileType(input); - // Make sure that the input is an image - if (type && type.mime.indexOf("image") === 0) { - let image = input; - - if (normalise) { - // Process the image to be easier to read by jsqr - // Disables the alpha channel - // Sets the image default background to white - // Normalises the image colours - // Makes the image greyscale - // Converts image to a JPEG - image = await new Promise((resolve, reject) => { - jimp.read(Buffer.from(input)) - .then(image => { - image - .rgba(false) - .background(0xFFFFFFFF) - .normalize() - .greyscale() - .getBuffer(jimp.MIME_JPEG, (error, result) => { - resolve(result); - }); - }) - .catch(err => { - reject(new OperationError("Error reading the image file.")); - }); - }); - } - - if (image instanceof OperationError) { - throw image; - } - - return new Promise((resolve, reject) => { - jimp.read(Buffer.from(image)) - .then(image => { - if (image.bitmap != null) { - const qrData = jsqr(image.bitmap.data, image.getWidth(), image.getHeight()); - if (qrData != null) { - resolve(qrData.data); - } else { - reject(new OperationError("Couldn't read a QR code from the image.")); - } - } else { - reject(new OperationError("Error reading the image file.")); - } - }) - .catch(err => { - reject(new OperationError("Error reading the image file.")); - }); - }); - } else { + if (!type || type.mime.indexOf("image") !== 0) { throw new OperationError("Invalid file type."); } - + return await parseQrCode(input, normalise); } } From 11451ac6b9f42e22a446d6c5e5e95259895dbed3 Mon Sep 17 00:00:00 2001 From: j433866 Date: Thu, 7 Mar 2019 13:35:37 +0000 Subject: [PATCH 2/5] Add image format pattern. ("borrowed" from RenderImage) --- src/core/operations/ParseQRCode.mjs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/core/operations/ParseQRCode.mjs b/src/core/operations/ParseQRCode.mjs index 816b6e75..d929500b 100644 --- a/src/core/operations/ParseQRCode.mjs +++ b/src/core/operations/ParseQRCode.mjs @@ -33,6 +33,14 @@ class ParseQRCode extends Operation { "value": false } ]; + this.patterns = [ + { + "match": "^(?:\\xff\\xd8\\xff|\\x89\\x50\\x4e\\x47|\\x47\\x49\\x46|.{8}\\x57\\x45\\x42\\x50|\\x42\\x4d)", + "flags": "", + "args": [false], + "useful": true + } + ]; } /** From 3e428c044ac6e6bc13b232d8fdb91f0c36875a78 Mon Sep 17 00:00:00 2001 From: j433866 Date: Fri, 8 Mar 2019 13:38:59 +0000 Subject: [PATCH 3/5] Add min values to operation args --- src/core/operations/GenerateQRCode.mjs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/core/operations/GenerateQRCode.mjs b/src/core/operations/GenerateQRCode.mjs index 4e1983e5..d88eee15 100644 --- a/src/core/operations/GenerateQRCode.mjs +++ b/src/core/operations/GenerateQRCode.mjs @@ -38,12 +38,14 @@ class GenerateQRCode extends Operation { { "name": "Module size (px)", "type": "number", - "value": 5 + "value": 5, + "min": 1 }, { "name": "Margin (num modules)", "type": "number", - "value": 2 + "value": 2, + "min": 0 }, { "name": "Error correction", From bb7487c4763ea10cb015d00a5089ab8a6c0727b4 Mon Sep 17 00:00:00 2001 From: j433866 Date: Wed, 13 Mar 2019 09:20:13 +0000 Subject: [PATCH 4/5] Change to use new FileType library --- src/core/operations/ParseQRCode.mjs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/core/operations/ParseQRCode.mjs b/src/core/operations/ParseQRCode.mjs index d929500b..63646b79 100644 --- a/src/core/operations/ParseQRCode.mjs +++ b/src/core/operations/ParseQRCode.mjs @@ -6,7 +6,7 @@ import Operation from "../Operation"; import OperationError from "../errors/OperationError"; -import Magic from "../lib/Magic"; +import { isImage } from "../lib/FileType.mjs"; import { parseQrCode } from "../lib/QRCode"; /** @@ -50,9 +50,8 @@ class ParseQRCode extends Operation { */ async run(input, args) { const [normalise] = args; - const type = Magic.magicFileType(input); - if (!type || type.mime.indexOf("image") !== 0) { + if (!isImage(input)) { throw new OperationError("Invalid file type."); } return await parseQrCode(input, normalise); From 4fafa39e54ce8c1fd4c78b01339a16fc8da29a7a Mon Sep 17 00:00:00 2001 From: j433866 Date: Wed, 13 Mar 2019 13:09:02 +0000 Subject: [PATCH 5/5] Fix magic library to better handle operation error --- src/core/lib/Magic.mjs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/core/lib/Magic.mjs b/src/core/lib/Magic.mjs index f0b55857..d66b6f93 100644 --- a/src/core/lib/Magic.mjs +++ b/src/core/lib/Magic.mjs @@ -312,6 +312,11 @@ class Magic { return; } + // If the recipe returned an empty buffer, do not continue + if (_buffersEqual(output, new ArrayBuffer())) { + return; + } + const magic = new Magic(output, this.opPatterns), speculativeResults = await magic.speculativeExecution( depth-1, extLang, intensive, [...recipeConfig, opConfig], op.useful, crib); @@ -395,7 +400,12 @@ class Magic { const recipe = new Recipe(recipeConfig); try { await recipe.execute(dish); - return dish.get(Dish.ARRAY_BUFFER); + // Return an empty buffer if the recipe did not run to completion + if (recipe.lastRunOp === recipe.opList[recipe.opList.length - 1]) { + return dish.get(Dish.ARRAY_BUFFER); + } else { + return new ArrayBuffer(); + } } catch (err) { // If there are errors, return an empty buffer return new ArrayBuffer();