From 8384cff3b1f2946685c991b76a1b5ed1cd01b1a5 Mon Sep 17 00:00:00 2001 From: Alex J Burke Date: Thu, 21 May 2020 12:47:22 +0200 Subject: [PATCH 1/2] Add ncolors argument and use it for a legacy query string engine arg. --- src/engines/pngquant.js | 11 ++++++++++ src/queryString.js | 2 +- test/impro.js | 46 ++++++++++++++++++++++++++++++++++++++++- test/queryString.js | 4 ++-- 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/engines/pngquant.js b/src/engines/pngquant.js index 0808e26..81f466b 100644 --- a/src/engines/pngquant.js +++ b/src/engines/pngquant.js @@ -8,6 +8,7 @@ module.exports = { outputTypes: ['png'], operations: [ 'floyd', + 'ncolors', 'nofs', 'ordered', 'speed', @@ -23,6 +24,8 @@ module.exports = { (args.length === 1 && /^(?:0(?:\.\d+)?|1(?:\.0+)?)$/.test(String(args[0]))) ); + case 'ncolors': + return args.length === 1 && args[0] >= 2 && args[0] <= 256; case 'speed': return args.length === 1 && args[0] >= 1 && args[0] <= 11; case 'quality': @@ -37,9 +40,17 @@ module.exports = { }, execute: function(pipeline, operations, options) { var commandLineArgs = []; + var nColors; operations.forEach(operation => { + if (operation.name === 'ncolors') { + nColors = operation.args[0]; + return; + } commandLineArgs.push('--' + operation.name, ...operation.args); }); + if (nColors) { + commandLineArgs.push(nColors); + } commandLineArgs.push('-'); pipeline._attach(new PngQuant(commandLineArgs)); diff --git a/src/queryString.js b/src/queryString.js index 544947e..0711969 100644 --- a/src/queryString.js +++ b/src/queryString.js @@ -120,7 +120,7 @@ function prepareLegacyQueryString(queryString, improInstance) { result.push(bit.slice(1)); lastSeenOptionIndex = index + 1; // account for the engine entry } else if (engineName === 'pngquant') { - result.push(`speed=${bit}`); + result.push(`ncolors=${bit}`); } else if (lastSeenOptionIndex > -1) { result[lastSeenOptionIndex] += `=${bit}`; } diff --git a/test/impro.js b/test/impro.js index 69b68ce..2cbd135 100644 --- a/test/impro.js +++ b/test/impro.js @@ -1759,7 +1759,7 @@ describe('impro', function() { return expect( 'purplealpha24bit.png', 'when piped through', - impro.pngquant().speed(8), + impro.pngquant().ncolors(256), 'to yield output satisfying', expect.it('to have metadata satisfying', { format: 'PNG', @@ -1771,6 +1771,16 @@ describe('impro', function() { ); }); + it('should reject with invalid argument', () => { + return expect( + () => { + impro.jpegtran().ncolors(1); + }, + 'to throw', + 'invalid operation or arguments: ncolors=[1]' + ); + }); + it('should support floyd', () => { const executeSpy = sinon.spy(impro.engineByName.pngquant, 'execute'); @@ -1788,6 +1798,21 @@ describe('impro', function() { }); }); + it('should support ncolors', () => { + const executeSpy = sinon.spy(impro.engineByName.pngquant, 'execute'); + + impro + .pngquant() + .ncolors(2) + .flush(); + + return expect(executeSpy.returnValues[0], 'to equal', [2, '-']).finally( + () => { + executeSpy.restore(); + } + ); + }); + it('should support posterize', () => { const executeSpy = sinon.spy(impro.engineByName.pngquant, 'execute'); @@ -1838,6 +1863,25 @@ describe('impro', function() { executeSpy.restore(); }); }); + + it('should support multiple operations', () => { + const executeSpy = sinon.spy(impro.engineByName.pngquant, 'execute'); + + impro + .pngquant() + .speed(1) + .ncolors(256) + .flush(); + + return expect(executeSpy.returnValues[0], 'to equal', [ + '--speed', + 1, + 256, + '-' + ]).finally(() => { + executeSpy.restore(); + }); + }); }); describe('with the pngcrush engine', function() { diff --git a/test/queryString.js b/test/queryString.js index a3027dc..b4e8072 100644 --- a/test/queryString.js +++ b/test/queryString.js @@ -232,9 +232,9 @@ describe('queryString', function() { it('should parse pngquant with integer argument correctly', () => { localExpect( - 'resize=800,800&pngquant=8', + 'resize=800,800&pngquant=256', 'when prepared to equal', - 'resize=800,800&pngquant&speed=8' + 'resize=800,800&pngquant&ncolors=256' ); }); From b545091135d222d6cf81462dba866cc5d0c89184 Mon Sep 17 00:00:00 2001 From: Alex J Burke Date: Thu, 21 May 2020 12:47:22 +0200 Subject: [PATCH 2/2] Catch up another test. --- test/queryString.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/queryString.js b/test/queryString.js index b4e8072..b69b6a6 100644 --- a/test/queryString.js +++ b/test/queryString.js @@ -258,7 +258,7 @@ describe('queryString', function() { localExpect( 'resize=800,800&pngquant=8&pngcrush=-rem,gAMA', 'when prepared to equal', - 'resize=800,800&pngquant&speed=8&pngcrush&rem=gAMA' + 'resize=800,800&pngquant&ncolors=8&pngcrush&rem=gAMA' ); });