Skip to content

Commit

Permalink
Doc refresh, changelog and dead code removal for #977
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Oct 10, 2017
1 parent 18870a1 commit 300b0d5
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 155 deletions.
18 changes: 4 additions & 14 deletions docs/api-resize.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,20 @@
Resize image to `width` x `height`.
By default, the resized image is centre cropped to the exact size specified.

Possible reduction kernels are:
Possible kernels are:

- `nearest`: Use [nearest neighbour interpolation](http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation).
- `cubic`: Use a [Catmull-Rom spline](https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline).
- `lanczos2`: Use a [Lanczos kernel](https://en.wikipedia.org/wiki/Lanczos_resampling#Lanczos_kernel) with `a=2`.
- `lanczos3`: Use a Lanczos kernel with `a=3` (the default).

Possible enlargement interpolators are:

- `nearest`: Use [nearest neighbour interpolation](http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation).
- `bilinear`: Use [bilinear interpolation](http://en.wikipedia.org/wiki/Bilinear_interpolation), faster than bicubic but with less smooth results.
- `vertexSplitQuadraticBasisSpline`: Use the smoother [VSQBS interpolation](https://github.com/jcupitt/libvips/blob/master/libvips/resample/vsqbs.cpp#L48) to prevent "staircasing" when enlarging.
- `bicubic`: Use [bicubic interpolation](http://en.wikipedia.org/wiki/Bicubic_interpolation) (the default).
- `locallyBoundedBicubic`: Use [LBB interpolation](https://github.com/jcupitt/libvips/blob/master/libvips/resample/lbb.cpp#L100), which prevents some "[acutance](http://en.wikipedia.org/wiki/Acutance)" but typically reduces performance by a factor of 2.
- `nohalo`: Use [Nohalo interpolation](http://eprints.soton.ac.uk/268086/), which prevents acutance but typically reduces performance by a factor of 3.

**Parameters**

- `width` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** pixels wide the resultant image should be. Use `null` or `undefined` to auto-scale the width to match the height.
- `height` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** pixels high the resultant image should be. Use `null` or `undefined` to auto-scale the height to match the width.
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?**
- `options.kernel` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the kernel to use for image reduction. (optional, default `'lanczos3'`)
- `options.interpolator` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the interpolator to use for image enlargement. (optional, default `'bicubic'`)
- `options.fastShrinkOnLoad` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** take greater advantage of the JPEG and WebP shrink-on-load feature, which can lead to a slight moiré pattern on some images. (optional, default `true`)
- `options.centreSampling` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** use \*magick centre sampling convention instead of corner sampling. (optional, default `false`)
- `options.centerSampling` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** alternative spelling of centreSampling. (optional, default `false`)

Expand All @@ -46,15 +37,14 @@ Possible enlargement interpolators are:
```javascript
sharp(inputBuffer)
.resize(200, 300, {
kernel: sharp.kernel.lanczos2,
interpolator: sharp.interpolator.nohalo
kernel: sharp.kernel.nearest
})
.background('white')
.embed()
.toFile('output.tiff')
.then(function() {
// output.tiff is a 200 pixels wide and 300 pixels high image
// containing a lanczos2/nohalo scaled version, embedded on a white canvas,
// containing a nearest-neighbour scaled version, embedded on a white canvas,
// of the image data in inputBuffer
});
```
Expand Down
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Requires libvips v8.6.0.
[#976](https://github.com/lovell/sharp/pull/976)
[@mceachen](https://github.com/mceachen)

* Resize: switch to libvips' implementation, make fastShrinkOnLoad optional, remove interpolators.
[#977](https://github.com/lovell/sharp/pull/977)
[@jardakotesovec](https://github.com/jardakotesovec)

### v0.18 - "*ridge*"

Requires libvips v8.5.5.
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ the help and code contributions of the following people:
* [Nicolas Coden](https://github.com/ncoden)
* [Matt Parrish](https://github.com/pbomb)
* [Matthew McEachen](https://github.com/mceachen)
* [Jarda Kotěšovec](https://github.com/jardakotesovec)

Thank you!

Expand Down
43 changes: 4 additions & 39 deletions lib/resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,60 +42,35 @@ const kernel = {
lanczos3: 'lanczos3'
};

/**
* Enlargement interpolators.
* @member
* @private
*/
const interpolator = {
nearest: 'nearest',
bilinear: 'bilinear',
bicubic: 'bicubic',
nohalo: 'nohalo',
lbb: 'lbb',
locallyBoundedBicubic: 'lbb',
vsqbs: 'vsqbs',
vertexSplitQuadraticBasisSpline: 'vsqbs'
};

/**
* Resize image to `width` x `height`.
* By default, the resized image is centre cropped to the exact size specified.
*
* Possible reduction kernels are:
* Possible kernels are:
* - `nearest`: Use [nearest neighbour interpolation](http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation).
* - `cubic`: Use a [Catmull-Rom spline](https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline).
* - `lanczos2`: Use a [Lanczos kernel](https://en.wikipedia.org/wiki/Lanczos_resampling#Lanczos_kernel) with `a=2`.
* - `lanczos3`: Use a Lanczos kernel with `a=3` (the default).
*
* Possible enlargement interpolators are:
* - `nearest`: Use [nearest neighbour interpolation](http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation).
* - `bilinear`: Use [bilinear interpolation](http://en.wikipedia.org/wiki/Bilinear_interpolation), faster than bicubic but with less smooth results.
* - `vertexSplitQuadraticBasisSpline`: Use the smoother [VSQBS interpolation](https://github.com/jcupitt/libvips/blob/master/libvips/resample/vsqbs.cpp#L48) to prevent "staircasing" when enlarging.
* - `bicubic`: Use [bicubic interpolation](http://en.wikipedia.org/wiki/Bicubic_interpolation) (the default).
* - `locallyBoundedBicubic`: Use [LBB interpolation](https://github.com/jcupitt/libvips/blob/master/libvips/resample/lbb.cpp#L100), which prevents some "[acutance](http://en.wikipedia.org/wiki/Acutance)" but typically reduces performance by a factor of 2.
* - `nohalo`: Use [Nohalo interpolation](http://eprints.soton.ac.uk/268086/), which prevents acutance but typically reduces performance by a factor of 3.
*
* @example
* sharp(inputBuffer)
* .resize(200, 300, {
* kernel: sharp.kernel.lanczos2,
* interpolator: sharp.interpolator.nohalo
* kernel: sharp.kernel.nearest
* })
* .background('white')
* .embed()
* .toFile('output.tiff')
* .then(function() {
* // output.tiff is a 200 pixels wide and 300 pixels high image
* // containing a lanczos2/nohalo scaled version, embedded on a white canvas,
* // containing a nearest-neighbour scaled version, embedded on a white canvas,
* // of the image data in inputBuffer
* });
*
* @param {Number} [width] - pixels wide the resultant image should be. Use `null` or `undefined` to auto-scale the width to match the height.
* @param {Number} [height] - pixels high the resultant image should be. Use `null` or `undefined` to auto-scale the height to match the width.
* @param {Object} [options]
* @param {String} [options.kernel='lanczos3'] - the kernel to use for image reduction.
* @param {String} [options.interpolator='bicubic'] - the interpolator to use for image enlargement.
* @param {Boolean} [options.fastShrinkOnLoad=true] - take greater advantage of the JPEG and WebP shrink-on-load feature, which can lead to a slight moiré pattern on some images.
* @param {Boolean} [options.centreSampling=false] - use *magick centre sampling convention instead of corner sampling.
* @param {Boolean} [options.centerSampling=false] - alternative spelling of centreSampling.
* @returns {Sharp}
Expand Down Expand Up @@ -129,20 +104,11 @@ function resize (width, height, options) {
throw is.invalidParameterError('kernel', 'valid kernel name', options.kernel);
}
}
// Interpolator
if (is.defined(options.interpolator)) {
if (is.string(interpolator[options.interpolator])) {
this.options.interpolator = interpolator[options.interpolator];
} else {
throw is.invalidParameterError('interpolator', 'valid interpolator name', options.interpolator);
}
}
// Centre sampling
options.centreSampling = is.bool(options.centerSampling) ? options.centerSampling : options.centreSampling;
if (is.defined(options.centreSampling)) {
this._setBooleanOption('centreSampling', options.centreSampling);
}

// Shrink on load
if (is.defined(options.fastShrinkOnLoad)) {
this._setBooleanOption('fastShrinkOnLoad', options.fastShrinkOnLoad);
Expand Down Expand Up @@ -307,5 +273,4 @@ module.exports = function (Sharp) {
Sharp.gravity = gravity;
Sharp.strategy = strategy;
Sharp.kernel = kernel;
Sharp.interpolator = interpolator;
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"Guy Maliar <guy@tailorbrands.com>",
"Nicolas Coden <nicolas@ncoden.fr>",
"Matt Parrish <matt.r.parrish@gmail.com>",
"Matthew McEachen <matthew+github@mceachen.org>"
"Matthew McEachen <matthew+github@mceachen.org>",
"Jarda Kotěšovec <jarda.kotesovec@gmail.com>"
],
"scripts": {
"clean": "rm -rf node_modules/ build/ vendor/ coverage/ test/fixtures/output.*",
Expand Down
1 change: 0 additions & 1 deletion src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,6 @@ NAN_METHOD(pipeline) {
baton->withoutEnlargement = AttrTo<bool>(options, "withoutEnlargement");
baton->crop = AttrTo<int32_t>(options, "crop");
baton->kernel = AttrAsStr(options, "kernel");
baton->interpolator = AttrAsStr(options, "interpolator");
baton->centreSampling = AttrTo<bool>(options, "centreSampling");
baton->fastShrinkOnLoad = AttrTo<bool>(options, "fastShrinkOnLoad");
// Join Channel Options
Expand Down
1 change: 0 additions & 1 deletion src/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ struct PipelineBaton {
int cropOffsetTop;
bool premultiplied;
std::string kernel;
std::string interpolator;
bool centreSampling;
bool fastShrinkOnLoad;
double background[4];
Expand Down
99 changes: 0 additions & 99 deletions test/unit/interpolation.js

This file was deleted.

36 changes: 36 additions & 0 deletions test/unit/resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,40 @@ describe('Resize dimensions', function () {
fixtures.assertSimilar(fixtures.expected('fast-shrink-on-load-true.png'), data, done);
});
});

[
sharp.kernel.nearest,
sharp.kernel.cubic,
sharp.kernel.lanczos2,
sharp.kernel.lanczos3
].forEach(function (kernel) {
it(`kernel ${kernel}`, function (done) {
sharp(fixtures.inputJpg)
.resize(320, null, { kernel: kernel })
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(320, info.width);
fixtures.assertSimilar(fixtures.inputJpg, data, done);
});
});
});

it('nearest upsampling with integral factor', function (done) {
sharp(fixtures.inputTiff8BitDepth)
.resize(210, 210, { kernel: 'nearest' })
.png()
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(210, info.width);
assert.strictEqual(210, info.height);
done();
});
});

it('unknown kernel throws', function () {
assert.throws(function () {
sharp().resize(null, null, { kernel: 'unknown' });
});
});
});

0 comments on commit 300b0d5

Please sign in to comment.