Skip to content

Commit

Permalink
adds tests to png encoding (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalAr committed Sep 3, 2014
1 parent 94a121b commit 5992d59
Show file tree
Hide file tree
Showing 13 changed files with 410 additions and 192 deletions.
346 changes: 175 additions & 171 deletions defs.js
Original file line number Diff line number Diff line change
@@ -1,175 +1,179 @@
var defaults = exports.defaults = {
DEF_INTERPOLATION: 'lanczos',
DEF_ROTATE_COLOR: 'gray',
DEF_JPEG_QUALITY: 100,
PNG_DEF_COMPRESSION: "fast",
PNG_DEF_INTERLACED: false
};
(function() {

exports.interpolations = {
'nearest-neighbor': 1,
'moving-average': 2,
'linear': 3,
'grid': 4,
'cubic': 5,
'lanczos': 6
};
var defaults = exports.defaults = {
DEF_INTERPOLATION: 'lanczos',
DEF_ROTATE_COLOR: 'gray',
DEF_JPEG_QUALITY: 100,
PNG_DEF_COMPRESSION: 'fast',
PNG_DEF_INTERLACED: false
};

exports.colors = {
'black': {
r: 0,
g: 0,
b: 0
},
'white': {
r: 255,
g: 255,
b: 255
},
'red': {
r: 255,
g: 0,
b: 0
},
'blue': {
r: 0,
g: 0,
b: 255
},
'green': {
r: 0,
g: 255,
b: 0
},
'cyan': {
r: 0,
g: 255,
b: 255
},
'yellow': {
r: 255,
g: 255,
b: 0
},
'gray': {
r: 128,
g: 128,
b: 128
},
'magenta': {
r: 255,
g: 0,
b: 255
exports.interpolations = {
'nearest-neighbor': 1,
'moving-average': 2,
'linear': 3,
'grid': 4,
'cubic': 5,
'lanczos': 6
};

exports.colors = {
'black': {
r: 0,
g: 0,
b: 0
},
'white': {
r: 255,
g: 255,
b: 255
},
'red': {
r: 255,
g: 0,
b: 0
},
'blue': {
r: 0,
g: 0,
b: 255
},
'green': {
r: 0,
g: 255,
b: 0
},
'cyan': {
r: 0,
g: 255,
b: 255
},
'yellow': {
r: 255,
g: 255,
b: 0
},
'gray': {
r: 128,
g: 128,
b: 128
},
'magenta': {
r: 255,
g: 0,
b: 255
}
};

exports.args = {
open: [{
name: 'impath',
type: 'string'
}, {
name: 'type',
type: 'string',
optional: true
}, {
name: 'callback',
type: 'function'
}],
scale: [{
name: 'wRation',
type: 'p-number'
}, {
name: 'hRatio',
type: 'p-number',
optional: true
}, {
name: 'inter',
type: 'string',
optional: true,
default: defaults.DEF_INTERPOLATION
}, {
name: 'callback',
type: 'function'
}],
resize: [{
name: 'width',
type: 'p-number'
}, {
name: 'height',
type: 'p-number',
optional: true
}, {
name: 'inter',
type: 'string',
optional: true,
default: defaults.DEF_INTERPOLATION
}, {
name: 'callback',
type: 'function'
}],
rotate: [{
name: 'degs',
type: 'number'
}, {
name: 'color',
types: ['string', 'array', 'hash'],
optional: true,
default: defaults.DEF_ROTATE_COLOR
}, {
name: 'callback',
type: 'function'
}],
blur: [{
name: 'sigma',
type: 'nn-number'
}, {
name: 'callback',
type: 'function'
}],
crop: [{
name: 'left',
type: 'nn-number'
}, {
name: 'top',
type: 'nn-number'
}, {
name: 'right',
type: 'nn-number',
optional: true
}, {
name: 'bottom',
type: 'nn-number',
optional: true
}, {
name: 'callback',
type: 'function'
}],
toBuffer: [{
name: 'type',
type: 'string'
}, {
name: 'params',
type: 'hash',
optional: true,
default: {}
}, {
name: 'callback',
type: 'function'
}],
writeFile: [{
name: 'path',
type: 'string'
}, {
name: 'type',
type: 'string',
optional: true,
}, {
name: 'params',
type: 'hash',
optional: true,
default: {}
}, {
name: 'callback',
type: 'function'
}]
}
};

exports.args = {
open: [{
name: 'impath',
type: 'string'
}, {
name: 'type',
type: 'string',
optional: true
}, {
name: 'callback',
type: 'function'
}],
scale: [{
name: 'wRation',
type: 'p-number'
}, {
name: 'hRatio',
type: 'p-number',
optional: true
}, {
name: 'inter',
type: 'string',
optional: true,
default: defaults.DEF_INTERPOLATION
}, {
name: 'callback',
type: 'function'
}],
resize: [{
name: 'width',
type: 'p-number'
}, {
name: 'height',
type: 'p-number',
optional: true
}, {
name: 'inter',
type: 'string',
optional: true,
default: defaults.DEF_INTERPOLATION
}, {
name: 'callback',
type: 'function'
}],
rotate: [{
name: 'degs',
type: 'number'
}, {
name: 'color',
types: ['string', 'array', 'hash'],
optional: true,
default: defaults.DEF_ROTATE_COLOR
}, {
name: 'callback',
type: 'function'
}],
blur: [{
name: 'sigma',
type: 'nn-number'
}, {
name: 'callback',
type: 'function'
}],
crop: [{
name: 'left',
type: 'nn-number'
}, {
name: 'top',
type: 'nn-number'
}, {
name: 'right',
type: 'nn-number',
optional: true
}, {
name: 'bottom',
type: 'nn-number',
optional: true
}, {
name: 'callback',
type: 'function'
}],
toBuffer: [{
name: 'type',
type: 'string'
}, {
name: 'params',
type: 'hash',
optional: true,
default: {}
}, {
name: 'callback',
type: 'function'
}],
writeFile: [{
name: 'path',
type: 'string'
}, {
name: 'type',
type: 'string',
optional: true,
}, {
name: 'params',
type: 'hash',
optional: true,
default: {}
}, {
name: 'callback',
type: 'function'
}]
}
})();
17 changes: 17 additions & 0 deletions examples/png_interlaced.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Example for using LWIP to create a highly compressed interlaced png file.
*/

var path = require('path'),
lwip = require('../');

lwip.open('lena.jpg', function(err, image) {
if (err) return console.log(err);
image.writeFile('lena_interlaced.png', {
compression: 'high',
interlaced: true
}, function(err) {
if (err) return console.log(err);
console.log('done');
});
});
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
else if (params.compression === 'high') params.compression = 2;
else throw Error('Invalid PNG compression');
params.interlaced = params.interlaced || defs.defaults.PNG_DEF_INTERLACED;
params.interlaced = !!params.interlaced;
if (typeof params.interlaced !== 'boolean') throw Error('PNG \'interlaced\' must be boolean');
return that.__lwip.toPngBuffer(params.compression, params.interlaced, function(err, buffer) {
that.__release();
callback(err, buffer);
Expand Down Expand Up @@ -309,6 +309,14 @@
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');
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 + '\'');
that.exec(function(err, image) {
if (err) return callback(err);
Expand Down
Loading

0 comments on commit 5992d59

Please sign in to comment.