Skip to content

Commit

Permalink
Merge b545091 into fb92d3c
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjeffburke committed May 21, 2020
2 parents fb92d3c + b545091 commit b94d59b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/engines/pngquant.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
outputTypes: ['png'],
operations: [
'floyd',
'ncolors',
'nofs',
'ordered',
'speed',
Expand All @@ -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':
Expand All @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/queryString.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
Expand Down
46 changes: 45 additions & 1 deletion test/impro.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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');

Expand All @@ -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');

Expand Down Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions test/queryString.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
});

Expand All @@ -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'
);
});

Expand Down

0 comments on commit b94d59b

Please sign in to comment.