From 345c169ac747eb06fa688b8808da444473e09454 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Thu, 2 Apr 2020 01:37:38 +0100 Subject: [PATCH] separate output and presentation --- .../operations/ExtractQuantisationTables.mjs | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/core/operations/ExtractQuantisationTables.mjs b/src/core/operations/ExtractQuantisationTables.mjs index f0972879..cc284a00 100644 --- a/src/core/operations/ExtractQuantisationTables.mjs +++ b/src/core/operations/ExtractQuantisationTables.mjs @@ -24,23 +24,23 @@ class ExtractQuantisationTables extends Operation { this.description = "Extracts quantisation tables embedded in a JPEG image."; this.infoURL = "https://en.wikipedia.org/wiki/Quantization_(image_processing)"; this.inputType = "ArrayBuffer"; - this.outputType = "string"; + this.outputType = "json"; + this.presentType = "string"; this.args = []; } /** * @param {ArrayBuffer} input * @param {Object[]} args - * @returns {string} + * @returns {json} */ run(input, args) { const image = new Uint8Array(input); - // maximally 4 tables are allowed in a JPEG file let ptr = indexOfMarker(image, 0xD8); if (ptr === -1) throw new OperationError("Malformed image file: Start of Image not found"); - let ret = ""; + const ret = {}; do { const dqtIndex = indexOfMarker(image, 0xDB, ptr); @@ -57,9 +57,13 @@ class ExtractQuantisationTables extends Operation { if (tableId >= 4) throw new OperationError(`Invalid table identifier ${tableId} at table definition beginning at ${dqtIndex}`); const table = parseQTable(image.slice(dqtIndex + 5, dqtIndex + 5 + tableLength), doublePrecision); - ret += `Quantisation table at position ${dqtIndex}. ID ${tableId}, ${(doublePrecision? "16":"8")}-bit precision:\n`; - ret += this.prettifyMatrix(table); - ret += "\n"; + // there may be two sets of quantisation tables, one in the embedded thumbnail and one for the main image + // we extract both sets so there may be tables with duplicate identifiers + ret[dqtIndex] = { + id: tableId, + precision: doublePrecision? 16:8, + table: table + }; ptr = dqtIndex + 5 + tableLength + 1; } while (ptr < image.length); @@ -67,6 +71,26 @@ class ExtractQuantisationTables extends Operation { return ret; } + /** + * Pretty print the tables with description + * + * @param {json} tables + * @param {Object[]} args + * @returns {string} + */ + present(tables, args) { + let ret = ""; + + for (const dqtIndex in tables) { + const tableInfo = tables[dqtIndex]; + ret += `Quantisation table at position ${dqtIndex}. ID ${tableInfo.id}, ${tableInfo.precision}-bit precision:\n`; + ret += this.prettifyMatrix(tableInfo.table); + ret += "\n"; + } + return ret; + } + + /** * @param {number[][]} mat * @returns {string}