Skip to content

Commit

Permalink
#88 - image.cover
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalAr committed Dec 18, 2014
1 parent 115c3e9 commit e069b46
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 2 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
0. [Resize](#resize)
0. [Scale](#scale)
0. [Rotate](#rotate)
0. [Cover](#cover)
0. [Crop](#crop)
0. [Blur](#blur)
0. [Sharpen](#sharpen)
Expand Down Expand Up @@ -306,6 +307,26 @@ lwip.create(500, 500, 'yellow', function(err, image){
- `"lanczos"`
0. `callback {Function(err, image)}`

#### Cover

Cover a canvas with the image. The image will be resized to the smallest
possible size such that both its dimensions are bigger than the canvas's
dimensions. Margins of the image exceeding the canvas will be discarded.

`image.cover(width, height, inter, callback)`

0. `width {Integer}`: Canvas' width in pixels.
0. `height {Integer}`: Canvas' height in pixels.
0. `inter {String}`: **Optional** interpolation method. Defaults to `"lanczos"`.
Possible values:
- `"nearest-neighbor"`
- `"moving-average"`
- `"linear"`
- `"grid"`
- `"cubic"`
- `"lanczos"`
0. `callback {Function(err, image)}`

#### Rotate

`image.rotate(degs, color, callback)`
Expand Down
16 changes: 16 additions & 0 deletions examples/cover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Example for using LWIP to cover a canvas with an image.
*/

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

lwip.open('lena.jpg', function(err, image) {
if (err) return console.log(err);
image.cover(400,800,function(err, image){
image.writeFile('lena_cover.jpg', function(err){
if (err) return console.log(err);
console.log('done');
});
});
});
9 changes: 9 additions & 0 deletions lib/BatchPrototypeInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// batch mode.
scale: decree(defs.args.scale.slice(0, -1)),
resize: decree(defs.args.resize.slice(0, -1)),
cover: decree(defs.args.cover.slice(0, -1)),
rotate: decree(defs.args.rotate.slice(0, -1)),
blur: decree(defs.args.blur.slice(0, -1)),
hslaAdjust: decree(defs.args.hslaAdjust.slice(0, -1)),
Expand Down Expand Up @@ -84,6 +85,14 @@
return this;
};

Batch.prototype.cover = function() {
var that = this;
judges.cover(arguments, function(width, height, inter) {
that.__addOp(that.__image.cover, [width, height, inter].filter(undefinedFilter));
});
return this;
};

Batch.prototype.rotate = function() {
var that = this;
judges.rotate(arguments, function(degs, color) {
Expand Down
16 changes: 16 additions & 0 deletions lib/ImagePrototypeInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
var judges = {
scale: decree(defs.args.scale),
resize: decree(defs.args.resize),
cover: decree(defs.args.cover),
rotate: decree(defs.args.rotate),
blur: decree(defs.args.blur),
hslaAdjust: decree(defs.args.hslaAdjust),
Expand Down Expand Up @@ -118,6 +119,21 @@
);
};

Image.prototype.cover = function() {
var that = this;
judges.cover(
arguments,
function(width, height, inter, callback) {
var s = Math.max(width / that.width(), height / that.height());
that.scale(s, s, inter, function(err){
if (err) return callback(err);
that.crop(width, height, callback);
});
}
);
};


Image.prototype.rotate = function() {
this.__lock();
var that = this;
Expand Down
18 changes: 17 additions & 1 deletion lib/defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,23 @@
}, {
name: 'callback',
type: 'function'
}]
}],
cover: [{
name: 'width',
type: 'p-number'
}, {
name: 'height',
type: 'p-number'
}, {
name: 'interpolation',
type: 'interpolation',
optional: true,
default: defaults.DEF_INTERPOLATION
}, {
name: 'callback',
type: 'function'
}],

};

})();
61 changes: 61 additions & 0 deletions tests/02.operations/119.cover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var join = require('path').join,
should = require('should'),
assert = require('assert'),
mkdirp = require('mkdirp'),
lwip = require('../../'),
imgs = require('../imgs');

var tmpDir = join(__dirname, '../results'),
basename = 'cover',
current;

describe('lwip.cover', function() {

var image;

before(function(done) {
mkdirp(tmpDir, done);
});

beforeEach(function(done) {
lwip.open(imgs.png.rgb, function(err, img) {
image = img;
done(err);
});
});

afterEach(function(done) {
image.writeFile(join(tmpDir, current.join('_') + '.jpg'), 'jpg', {
quality: 90
}, done);
});

beforeEach(function(){
current = [ basename ];
});

describe('800X300, unspecified interpolation', function() {
it('image should have the correct size', function(done) {
current.push('800X300','unspecified_inter');
image.cover(800, 300, function(err, im) {
if (err) return done(err);
assert(im.width() === 800);
assert(im.height() === 300);
done();
});
});
});

describe('300X800, lanczos interpolation', function() {
it('image should have the correct size', function(done) {
current.push('300X800','lanczos');
image.cover(300, 800, 'lanczos', function(err, im) {
if (err) return done(err);
assert(im.width() === 300);
assert(im.height() === 800);
done();
});
});
});

});
7 changes: 7 additions & 0 deletions tests/03.safety/00.locks.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ describe('simultaneous operations locks', function() {
describe('image.paste lock', function() {
it('should lock image', function() {
image.paste.bind(image, 0, 0, tmpImage, function() {}).should.not.throwError();
image.cover.bind(image, 100, 100, function() {}).should.throwError();
});
});

describe('image.cover lock', function() {
it('should lock image', function() {
image.cover.bind(image, 200, 300, function() {}).should.not.throwError();
image.resize.bind(image, 100, 100, function() {}).should.throwError();
});
});
Expand Down
7 changes: 7 additions & 0 deletions tests/03.safety/01.releases.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ describe('failed ops lock release', function() {
describe('image.paste release', function() {
it('should release image lock', function() {
image.paste.bind(image, 'foo', 'foo', 'foo', function() {}).should.throwError();
image.cover.bind(image, 100, 100, function() {}).should.not.throwError();
});
});

describe('image.cover release', function() {
it('should release image lock', function() {
image.cover.bind(image, 'foo', 'foo', function() {}).should.throwError();
image.resize.bind(image, 100, 100, function() {}).should.not.throwError();
});
});
Expand Down
8 changes: 7 additions & 1 deletion tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
function generateRandomBatch(batch, n) {
var ops = [];
for (var i = 0; i < n; i++) {
var r = Math.floor(Math.random() * 14);
var r = Math.floor(Math.random() * 15);
switch (r) {
case 0:
var sd = Math.floor(Math.random() * 20);
Expand Down Expand Up @@ -81,6 +81,12 @@ function generateRandomBatch(batch, n) {
batch = batch.opacify();
ops.push('opc');
break;
case 14:
var w = Math.floor(Math.random() * 1000) + 10;
var h = Math.floor(Math.random() * 1000) + 10;
batch = batch.cover(w, h);
ops.push('cvr' + w + 'X' + h);
break;
}
}
return ops;
Expand Down

0 comments on commit e069b46

Please sign in to comment.