diff --git a/index.js b/index.js index 82f47b02..f13eb08c 100644 --- a/index.js +++ b/index.js @@ -363,7 +363,8 @@ var that = this; decree(defs.args.toBuffer)(arguments, function(type, params, callback) { if (type === 'jpg' || type === 'jpeg') { - params.quality = params.quality || defs.defaults.DEF_JPEG_QUALITY; + if (params.quality != 0) + params.quality = params.quality || defs.defaults.DEF_JPEG_QUALITY; if (params.quality != parseInt(params.quality) || params.quality < 0 || params.quality > 100) throw Error('Invalid JPEG quality'); return encoder.jpeg( @@ -637,15 +638,14 @@ var that = this; decree(defs.args.toBuffer)(arguments, function(type, params, callback) { if (type === 'jpg' || type === 'jpeg') { - params.quality = params.quality || defs.defaults.DEF_JPEG_QUALITY; + if (params.quality != 0) + params.quality = params.quality || defs.defaults.DEF_JPEG_QUALITY; if (params.quality != parseInt(params.quality) || params.quality < 0 || params.quality > 100) throw Error('Invalid JPEG quality'); } else if (type === 'png') { params.compression = params.compression || defs.defaults.PNG_DEF_COMPRESSION; - if (params.compression === 'none') params.compression = 0; - else if (params.compression === 'fast') params.compression = 1; - else if (params.compression === 'high') params.compression = 2; - else throw Error('Invalid PNG compression'); + if (['none', 'fast', 'high'].indexOf(params.compression) === -1) + throw Error('Invalid PNG compression'); params.interlaced = params.interlaced || defs.defaults.PNG_DEF_INTERLACED; if (typeof params.interlaced !== 'boolean') throw Error('PNG \'interlaced\' must be boolean'); } else throw Error('Unknown type \'' + type + '\''); diff --git a/tests/02.operations/01.writeFile.js b/tests/02.operations/01.writeFile.js index 8475c70c..8541f10a 100644 --- a/tests/02.operations/01.writeFile.js +++ b/tests/02.operations/01.writeFile.js @@ -111,6 +111,7 @@ describe('lwip.writeFile', function() { }); describe('params specified - high compression, not interlaced', function() { + this.timeout(4000); // 4 seconds. high compression can take more time it('should succeed', function(done) { image.writeFile(outpathPng, { compression: 'high', @@ -120,6 +121,7 @@ describe('lwip.writeFile', function() { }); describe('params specified - high compression, interlaced', function() { + this.timeout(4000); // 4 seconds. high compression can take more time it('should succeed', function(done) { image.writeFile(outpathPng, { compression: 'high', diff --git a/tests/03.batch/index.js b/tests/03.batch/index.js index c6b28bd6..0b6fa0e3 100644 --- a/tests/03.batch/index.js +++ b/tests/03.batch/index.js @@ -2,48 +2,226 @@ var join = require('path').join, assert = require('assert'), mkdirp = require('mkdirp'), lwip = require('../../'), + utils = require('../utils'), imgs = require('../imgs'); -var tmpDir = join(__dirname, '../results'), - basename = 'batch', - current; - describe('image.batch', function() { - before(function(done) { - mkdirp(tmpDir, done); - }); + var ops, batch; - var batch; beforeEach(function(done) { lwip.open(imgs.jpg.rgb, function(err, img) { + if (err) return done(err); batch = img.batch(); - done(err); + ops = utils.generateRandomBatch(batch, 3); + done(); }); }); - beforeEach(function() { - current = [basename]; - }); + describe('toBuffer', function() { - afterEach(function(done) { - batch.writeFile(join(tmpDir, current.join('_') + '.jpg'), 'jpeg', { - quality: 100 - }, done); - }); + describe('jpeg', function() { + + describe('quality 0', function() { + it('should succeed', function(done) { + batch.toBuffer('jpg', { + quality: 0 + }, function(err, buffer) { + done(err); + }); + }); + }); + + describe('quality 100', function() { + it('should succeed', function(done) { + batch.toBuffer('jpg', { + quality: 100 + }, function(err, buffer) { + done(err); + }); + }); + }); - describe('rotate(45,yellow)->crop(200,200)->blur(5)', function() { - it('should succeed', function() { - current.push('rotate45yellow', 'crop200X200', 'blur5'); - batch.rotate(45, 'yellow').crop(200, 200).blur(5); }); + + describe('png', function() { + + describe('non interlaced', function() { + + describe('no compression', function() { + it('should succeed', function(done) { + batch.toBuffer('png', { + interlaced: false, + compression: 'none', + }, function(err, buffer) { + done(err); + }); + }); + }); + + describe('fast compression', function() { + it('should succeed', function(done) { + batch.toBuffer('png', { + interlaced: false, + compression: 'fast', + }, function(err, buffer) { + done(err); + }); + }); + }); + + describe('high compression', function() { + this.timeout(4000); // 4 seconds. high compression can take more time + it('should succeed', function(done) { + batch.toBuffer('png', { + interlaced: false, + compression: 'high', + }, function(err, buffer) { + done(err); + }); + }); + }); + + }); + + describe('interlaced', function() { + + describe('no compression', function() { + it('should succeed', function(done) { + batch.toBuffer('png', { + interlaced: true, + compression: 'none', + }, function(err, buffer) { + done(err); + }); + }); + }); + + describe('fast compression', function() { + it('should succeed', function(done) { + batch.toBuffer('png', { + interlaced: true, + compression: 'fast', + }, function(err, buffer) { + done(err); + }); + }); + }); + + describe('high compression', function() { + this.timeout(4000); // 4 seconds. high compression can take more time + it('should succeed', function(done) { + batch.toBuffer('png', { + interlaced: true, + compression: 'high', + }, function(err, buffer) { + done(err); + }); + }); + }); + + }); + + }); + }); - describe('rotate(-20,green)->scale(2)->crop(500,500)', function() { - it('should succeed', function() { - current.push('rotate-20green', 'scale2', 'crop500X500'); - batch.rotate(-20, 'green').scale(2).crop(500, 500); + describe('writeFile', function() { + + var tmpDir = join(__dirname, '../results'); + + before(function(done) { + mkdirp(tmpDir, done); + }); + + describe('jpeg', function() { + + describe('quality 0', function() { + it('should succeed', function(done) { + batch.writeFile(join(tmpDir, 'btch-q0-' + ops.join('#') + '.jpg'), 'jpg', { + quality: 0 + }, done); + }); + }); + + describe('quality 100', function() { + it('should succeed', function(done) { + batch.writeFile(join(tmpDir, 'btch-q100-' + ops.join('#') + '.jpg'), 'jpg', { + quality: 100 + }, done); + }); + }); + + }); + + describe('png', function() { + + describe('non interlaced', function() { + + describe('no compression', function() { + it('should succeed', function(done) { + batch.writeFile(join(tmpDir, 'btch--noint#nocomp--' + ops.join('#') + '.png'), 'png', { + interlaced: false, + compression: 'none', + }, done); + }); + }); + + describe('fast compression', function() { + it('should succeed', function(done) { + batch.writeFile(join(tmpDir, 'btch--noint#fstcomp--' + ops.join('#') + '.png'), 'png', { + interlaced: false, + compression: 'fast', + }, done); + }); + }); + + describe('high compression', function() { + this.timeout(4000); // 4 seconds. high compression can take more time + it('should succeed', function(done) { + batch.writeFile(join(tmpDir, 'btch-noint#hicomp-' + ops.join('#') + '.png'), 'png', { + interlaced: false, + compression: 'high', + }, done); + }); + }); + + }); + + describe('interlaced', function() { + + describe('no compression', function() { + it('should succeed', function(done) { + batch.writeFile(join(tmpDir, 'btch-intr#nocomp-' + ops.join('#') + '.png'), 'png', { + interlaced: true, + compression: 'none', + }, done); + }); + }); + + describe('fast compression', function() { + it('should succeed', function(done) { + batch.writeFile(join(tmpDir, 'btch-intr#fstcomp-' + ops.join('#') + '.png'), 'png', { + interlaced: true, + compression: 'fast', + }, done); + }); + }); + + describe('high compression', function() { + this.timeout(4000); // 4 seconds. high compression can take more time + it('should succeed', function(done) { + batch.writeFile(join(tmpDir, 'btch-intr#hicomp-' + ops.join('#') + '.png'), 'png', { + interlaced: true, + compression: 'high', + }, done); + }); + }); + + }); + }); + }); }); diff --git a/tests/04.stress/index.js b/tests/04.stress/index.js index d1b73459..39ddda1d 100644 --- a/tests/04.stress/index.js +++ b/tests/04.stress/index.js @@ -1,8 +1,10 @@ var join = require('path').join, + fs = require('fs'), assert = require('assert'), async = require('async'), mkdirp = require('mkdirp'), lwip = require('../../'), + utils = require('../utils'), imgs = require('../imgs'); var tmpDir = join(__dirname, '../results'), @@ -64,50 +66,14 @@ describe('stress tests', function() { lwip.open(imgs.jpg.rgb, 'jpeg', function(err, image) { if (err) return done(err); var batch = image.batch(); - for (var i = 0; i < 10; i++) { - var r = Math.floor(Math.random() * 12); - switch (r) { - case 0: - batch = batch.blur(5); - break; - case 1: - batch = batch.rotate(45); - break; - case 2: - batch = batch.resize(600, 200); - break; - case 3: - batch = batch.crop(100, 150); - break; - case 4: - batch = batch.scale(1.1, 0.66); - break; - case 5: - batch = batch.mirror('xy'); - break; - case 6: - batch = batch.border(10); - break; - case 7: - batch = batch.sharpen(300); - break; - case 8: - batch = batch.saturate(1.2); - break; - case 9: - batch = batch.lighten(0.5); - break; - case 10: - batch = batch.darken(0.5); - break; - case 11: - batch = batch.hue(-50); - break; - } - } - batch.writeFile(outpathJpeg, 'jpeg', { + var ops = utils.generateRandomBatch(batch, 10); + batch.writeFile(join(tmpDir, 'stress-rnd-' + i + '.jpg'), 'jpeg', { quality: 50 - }, done); + }, function(err) { + if (err) return done(err); + var data = ops.join('\n'); + fs.writeFile(join(tmpDir, 'stress-rnd-' + i + '.txt'), data, done); + }); }); }, done); }); diff --git a/tests/utils.js b/tests/utils.js new file mode 100644 index 00000000..fc5a2a04 --- /dev/null +++ b/tests/utils.js @@ -0,0 +1,77 @@ +module.exports = { + generateRandomBatch: generateRandomBatch +}; + +function generateRandomBatch(batch, n) { + var ops = []; + for (var i = 0; i < n; i++) { + var r = Math.floor(Math.random() * 12); + switch (r) { + case 0: + var sd = Math.floor(Math.random() * 20); + batch = batch.blur(sd); + ops.push('blr' + sd); + break; + case 1: + var a = Math.floor(Math.random() * 360); + batch = batch.rotate(a); + ops.push('rtt' + a); + break; + case 2: + var w = Math.floor(Math.random() * 1000) + 1; + var h = Math.floor(Math.random() * 1000) + 1; + batch = batch.resize(w, h); + ops.push('rsz' + w + 'X' + h); + break; + case 3: + var w = Math.floor(Math.random() * 1000); + var h = Math.floor(Math.random() * 1000); + batch = batch.crop(w, h); + ops.push('crp' + w + 'X' + h); + break; + case 4: + var wr = Math.random() * 3 + 0.1; + var hr = Math.random() * 3 + 0.1; + batch = batch.scale(wr, hr); + ops.push('scl' + wr.toFixed(2) + 'X' + hr.toFixed(2)); + break; + case 5: + var i = Math.floor(Math.random() * 3); + var axes = ['x', 'y', 'xy'][i]; + batch = batch.mirror(axes); + ops.push('mrr' + axes); + break; + case 6: + var b = Math.floor(Math.random() * 30); + batch = batch.border(b); + ops.push('brdr' + b); + break; + case 7: + var amp = Math.floor(Math.random() * 300); + batch = batch.sharpen(amp); + ops.push('shrp' + amp); + break; + case 8: + var d = Math.random() * 2; + batch = batch.saturate(d); + ops.push('sat' + d.toFixed(2)); + break; + case 9: + var d = Math.random() * 2; + batch = batch.lighten(d); + ops.push('lit' + d.toFixed(2)); + break; + case 10: + var d = Math.random() * 0.5; + batch = batch.darken(d); + ops.push('drk' + d.toFixed(2)); + break; + case 11: + var s = Math.floor(Math.random() * 360); + batch = batch.hue(s); + ops.push('hue' + s); + break; + } + } + return ops; +}