Skip to content

Commit

Permalink
fixes #42, fixes #43, adds more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalAr committed Sep 17, 2014
1 parent 92736b1 commit 870e2a1
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 74 deletions.
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 + '\'');
Expand Down
2 changes: 2 additions & 0 deletions tests/02.operations/01.writeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
228 changes: 203 additions & 25 deletions tests/03.batch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

});

});

});

});
52 changes: 9 additions & 43 deletions tests/04.stress/index.js
Original file line number Diff line number Diff line change
@@ -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'),
Expand Down Expand Up @@ -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);
});
Expand Down
Loading

0 comments on commit 870e2a1

Please sign in to comment.