Skip to content

Commit

Permalink
Expose libvips centre option, mimics *magick +0.5px convention
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Nov 4, 2016
1 parent deb978b commit 2c7a846
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 164 deletions.
1 change: 1 addition & 0 deletions docs/api-output.md
Expand Up @@ -68,6 +68,7 @@ Use these JPEG options for output image.
- `trellisQuantisation` **\[[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** apply trellis quantisation, requires mozjpeg (optional, default `false`)
- `overshootDeringing` **\[[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** apply overshoot deringing, requires mozjpeg (optional, default `false`)
- `optimiseScans` **\[[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** optimise progressive scans, forces progressive, requires mozjpeg (optional, default `false`)
- `optimizeScans` **\[[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** alternative spelling of optimiseScans (optional, default `false`)


- Throws **[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid options
Expand Down
2 changes: 2 additions & 0 deletions docs/api-resize.md
Expand Up @@ -27,6 +27,8 @@ Possible enlargement interpolators are:
- `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.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`)

**Examples**

Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.md
Expand Up @@ -15,6 +15,9 @@ Requires libvips v8.4.2.
Access to these is now via output format functions, for example `quality(n)`
is now `jpeg({quality: n})` and/or `webp({quality: n})`.

* Expose libvips' "centre" resize option to mimic *magick's +0.5px convention.
[#568](https://github.com/lovell/sharp/issues/568)

* Ensure support for embedded base64 PNG and JPEG images within an SVG.
[#601](https://github.com/lovell/sharp/issues/601)
[@dynamite-ready](https://github.com/dynamite-ready)
Expand Down
1 change: 1 addition & 0 deletions lib/constructor.js
Expand Up @@ -102,6 +102,7 @@ const Sharp = function (input, options) {
withoutEnlargement: false,
kernel: 'lanczos3',
interpolator: 'bicubic',
centreSampling: false,
// operations
background: [0, 0, 0, 255],
flatten: false,
Expand Down
4 changes: 2 additions & 2 deletions lib/output.js
Expand Up @@ -115,14 +115,14 @@ const jpeg = function jpeg (options) {
throw new Error('Invalid chromaSubsampling (4:2:0, 4:4:4) ' + options.chromaSubsampling);
}
}
options.trellisQuantisation = options.trellisQuantisation || options.trellisQuantization;
options.trellisQuantisation = is.bool(options.trellisQuantization) ? options.trellisQuantization : options.trellisQuantisation;
if (is.defined(options.trellisQuantisation)) {
this._setBooleanOption('jpegTrellisQuantisation', options.trellisQuantisation);
}
if (is.defined(options.overshootDeringing)) {
this._setBooleanOption('jpegOvershootDeringing', options.overshootDeringing);
}
options.optimiseScans = is.bool(options.optimiseScans) ? options.optimiseScans : options.optimizeScans;
options.optimiseScans = is.bool(options.optimizeScans) ? options.optimizeScans : options.optimiseScans;
if (is.defined(options.optimiseScans)) {
this._setBooleanOption('jpegOptimiseScans', options.optimiseScans);
if (options.optimiseScans) {
Expand Down
7 changes: 7 additions & 0 deletions lib/resize.js
Expand Up @@ -94,6 +94,8 @@ const interpolator = {
* @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.centreSampling=false] - use *magick centre sampling convention instead of corner sampling.
* @param {Boolean} [options.centerSampling=false] - alternative spelling of centreSampling.
* @returns {Sharp}
* @throws {Error} Invalid parameters
*/
Expand Down Expand Up @@ -133,6 +135,11 @@ const resize = function resize (width, height, options) {
throw new Error('Invalid interpolator ' + options.interpolator);
}
}
// Centre sampling
options.centreSampling = is.bool(options.centerSampling) ? options.centerSampling : options.centreSampling;
if (is.defined(options.centreSampling)) {
this._setBooleanOption('centreSampling', options.centreSampling);
}
}
return this;
};
Expand Down
11 changes: 9 additions & 2 deletions src/pipeline.cc
Expand Up @@ -388,10 +388,16 @@ class PipelineWorker : public Nan::AsyncWorker {
throw vips::VError("Unknown kernel");
}
if (yresidual < 1.0) {
image = image.reducev(1.0 / yresidual, VImage::option()->set("kernel", kernel));
image = image.reducev(1.0 / yresidual, VImage::option()
->set("kernel", kernel)
->set("centre", baton->centreSampling)
);
}
if (xresidual < 1.0) {
image = image.reduceh(1.0 / xresidual, VImage::option()->set("kernel", kernel));
image = image.reduceh(1.0 / xresidual, VImage::option()
->set("kernel", kernel)
->set("centre", baton->centreSampling)
);
}
}
// Perform affine enlargement
Expand Down Expand Up @@ -1063,6 +1069,7 @@ NAN_METHOD(pipeline) {
baton->crop = AttrTo<int32_t>(options, "crop");
baton->kernel = AttrAsStr(options, "kernel");
baton->interpolator = AttrAsStr(options, "interpolator");
baton->centreSampling = AttrTo<bool>(options, "centreSampling");
// Join Channel Options
if(HasAttr(options, "joinChannelIn")) {
v8::Local<v8::Object> joinChannelObject = Nan::Get(options, Nan::New("joinChannelIn").ToLocalChecked())
Expand Down
2 changes: 2 additions & 0 deletions src/pipeline.h
Expand Up @@ -50,6 +50,7 @@ struct PipelineBaton {
int cropCalcTop;
std::string kernel;
std::string interpolator;
bool centreSampling;
double background[4];
bool flatten;
bool negate;
Expand Down Expand Up @@ -119,6 +120,7 @@ struct PipelineBaton {
crop(0),
cropCalcLeft(-1),
cropCalcTop(-1),
centreSampling(false),
flatten(false),
negate(false),
blurSigma(0.0),
Expand Down

0 comments on commit 2c7a846

Please sign in to comment.