Skip to content

Commit

Permalink
Add support for arbitrary rotation angle via vips_rotate (#1385)
Browse files Browse the repository at this point in the history
  • Loading branch information
freezy authored and lovell committed Sep 27, 2018
1 parent 37d385f commit 796738d
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 36 deletions.
69 changes: 38 additions & 31 deletions docs/api-operation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
Rotate the output image by either an explicit angle
or auto-orient based on the EXIF `Orientation` tag.

If an angle is provided, it is converted to a valid 90/180/270deg rotation.
If an angle is provided, it is converted to a valid positive degree rotation.
For example, `-450` will produce a 270deg rotation.

If an angle that is not a multiple of 90 is provided, the color of the
background color can be provided with the `background` option.

If no angle is provided, it is determined from the EXIF data.
Mirroring is supported and may infer the use of a flip operation.

Expand All @@ -19,6 +22,8 @@ for example `rotate(x).extract(y)` will produce a different result to `extract(y
### Parameters

- `angle` **[Number][1]** angle of rotation, must be a multiple of 90. (optional, default `auto`)
- `options` **[Object][2]?** if present, is an Object with optional attributes.
- `options.background` **([String][3] \| [Object][2])** parsed by the [color][4] module to extract values for red, green, blue and alpha. (optional, default `"#000000"`)

### Examples

Expand All @@ -34,7 +39,7 @@ const pipeline = sharp()
readableStream.pipe(pipeline);
```

- Throws **[Error][2]** Invalid parameters
- Throws **[Error][5]** Invalid parameters

Returns **Sharp**

Expand All @@ -45,7 +50,7 @@ The use of `flip` implies the removal of the EXIF `Orientation` tag, if any.

### Parameters

- `flip` **[Boolean][3]** (optional, default `true`)
- `flip` **[Boolean][6]** (optional, default `true`)

Returns **Sharp**

Expand All @@ -56,7 +61,7 @@ The use of `flop` implies the removal of the EXIF `Orientation` tag, if any.

### Parameters

- `flop` **[Boolean][3]** (optional, default `true`)
- `flop` **[Boolean][6]** (optional, default `true`)

Returns **Sharp**

Expand All @@ -74,7 +79,7 @@ Separate control over the level of sharpening in "flat" and "jagged" areas is av
- `jagged` **[Number][1]** the level of sharpening to apply to "jagged" areas. (optional, default `2.0`)


- Throws **[Error][2]** Invalid parameters
- Throws **[Error][5]** Invalid parameters

Returns **Sharp**

Expand All @@ -88,7 +93,7 @@ When used without parameters the default window is 3x3.
- `size` **[Number][1]** square mask size: size x size (optional, default `3`)


- Throws **[Error][2]** Invalid parameters
- Throws **[Error][5]** Invalid parameters

Returns **Sharp**

Expand All @@ -103,7 +108,7 @@ When a `sigma` is provided, performs a slower, more accurate Gaussian blur.
- `sigma` **[Number][1]?** a value between 0.3 and 1000 representing the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`.


- Throws **[Error][2]** Invalid parameters
- Throws **[Error][5]** Invalid parameters

Returns **Sharp**

Expand All @@ -113,7 +118,7 @@ Merge alpha transparency channel, if any, with `background`.

### Parameters

- `flatten` **[Boolean][3]** (optional, default `true`)
- `flatten` **[Boolean][6]** (optional, default `true`)

Returns **Sharp**

Expand All @@ -130,7 +135,7 @@ when applying a gamma correction.
- `gamma` **[Number][1]** value between 1.0 and 3.0. (optional, default `2.2`)


- Throws **[Error][2]** Invalid parameters
- Throws **[Error][5]** Invalid parameters

Returns **Sharp**

Expand All @@ -140,7 +145,7 @@ Produce the "negative" of the image.

### Parameters

- `negate` **[Boolean][3]** (optional, default `true`)
- `negate` **[Boolean][6]** (optional, default `true`)

Returns **Sharp**

Expand All @@ -150,7 +155,7 @@ Enhance output image contrast by stretching its luminance to cover the full dyna

### Parameters

- `normalise` **[Boolean][3]** (optional, default `true`)
- `normalise` **[Boolean][6]** (optional, default `true`)

Returns **Sharp**

Expand All @@ -160,7 +165,7 @@ Alternative spelling of normalise.

### Parameters

- `normalize` **[Boolean][3]** (optional, default `true`)
- `normalize` **[Boolean][6]** (optional, default `true`)

Returns **Sharp**

Expand All @@ -170,10 +175,10 @@ Convolve the image with the specified kernel.

### Parameters

- `kernel` **[Object][4]**
- `kernel` **[Object][2]**
- `kernel.width` **[Number][1]** width of the kernel in pixels.
- `kernel.height` **[Number][1]** width of the kernel in pixels.
- `kernel.kernel` **[Array][5]<[Number][1]>** Array of length `width*height` containing the kernel values.
- `kernel.kernel` **[Array][7]<[Number][1]>** Array of length `width*height` containing the kernel values.
- `kernel.scale` **[Number][1]** the scale of the kernel in pixels. (optional, default `sum`)
- `kernel.offset` **[Number][1]** the offset of the kernel in pixels. (optional, default `0`)

Expand All @@ -193,7 +198,7 @@ sharp(input)
});
```

- Throws **[Error][2]** Invalid parameters
- Throws **[Error][5]** Invalid parameters

Returns **Sharp**

Expand All @@ -204,12 +209,12 @@ Any pixel value greather than or equal to the threshold value will be set to 255
### Parameters

- `threshold` **[Number][1]** a value in the range 0-255 representing the level at which the threshold will be applied. (optional, default `128`)
- `options` **[Object][4]?**
- `options.greyscale` **[Boolean][3]** convert to single channel greyscale. (optional, default `true`)
- `options.grayscale` **[Boolean][3]** alternative spelling for greyscale. (optional, default `true`)
- `options` **[Object][2]?**
- `options.greyscale` **[Boolean][6]** convert to single channel greyscale. (optional, default `true`)
- `options.grayscale` **[Boolean][6]** alternative spelling for greyscale. (optional, default `true`)


- Throws **[Error][2]** Invalid parameters
- Throws **[Error][5]** Invalid parameters

Returns **Sharp**

Expand All @@ -222,16 +227,16 @@ the selected bitwise boolean `operation` between the corresponding pixels of the

### Parameters

- `operand` **([Buffer][6] \| [String][7])** Buffer containing image data or String containing the path to an image file.
- `operator` **[String][7]** one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively.
- `options` **[Object][4]?**
- `options.raw` **[Object][4]?** describes operand when using raw pixel data.
- `operand` **([Buffer][8] \| [String][3])** Buffer containing image data or String containing the path to an image file.
- `operator` **[String][3]** one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively.
- `options` **[Object][2]?**
- `options.raw` **[Object][2]?** describes operand when using raw pixel data.
- `options.raw.width` **[Number][1]?**
- `options.raw.height` **[Number][1]?**
- `options.raw.channels` **[Number][1]?**


- Throws **[Error][2]** Invalid parameters
- Throws **[Error][5]** Invalid parameters

Returns **Sharp**

Expand All @@ -245,20 +250,22 @@ Apply the linear formula a \* input + b to the image (levels adjustment)
- `b` **[Number][1]** offset (optional, default `0.0`)


- Throws **[Error][2]** Invalid parameters
- Throws **[Error][5]** Invalid parameters

Returns **Sharp**

[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number

[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object

[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String

[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
[4]: https://www.npmjs.org/package/color

[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error

[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean

[6]: https://nodejs.org/api/buffer.html
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array

[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[8]: https://nodejs.org/api/buffer.html
2 changes: 2 additions & 0 deletions lib/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ const Sharp = function (input, options) {
embed: 0,
useExifOrientation: false,
angle: 0,
rotationAngle: 0,
rotationBackground: [0, 0, 0, 255],
rotateBeforePreExtract: false,
flip: false,
flop: false,
Expand Down
23 changes: 20 additions & 3 deletions lib/operation.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
'use strict';

const color = require('color');
const is = require('./is');

/**
* Rotate the output image by either an explicit angle
* or auto-orient based on the EXIF `Orientation` tag.
*
* If an angle is provided, it is converted to a valid 90/180/270deg rotation.
* If an angle is provided, it is converted to a valid positive degree rotation.
* For example, `-450` will produce a 270deg rotation.
*
* If an angle that is not a multiple of 90 is provided, the color of the
* background color can be provided with the `background` option.
*
* If no angle is provided, it is determined from the EXIF data.
* Mirroring is supported and may infer the use of a flip operation.
*
Expand All @@ -29,16 +33,29 @@ const is = require('./is');
* readableStream.pipe(pipeline);
*
* @param {Number} [angle=auto] angle of rotation, must be a multiple of 90.
* @param {Object} [options] - if present, is an Object with optional attributes.
* @param {String|Object} [options.background="#000000"] parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha.
* @returns {Sharp}
* @throws {Error} Invalid parameters
*/
function rotate (angle) {
function rotate (angle, options) {
if (!is.defined(angle)) {
this.options.useExifOrientation = true;
} else if (is.integer(angle) && !(angle % 90)) {
this.options.angle = angle;
} else if (is.number(angle)) {
this.options.rotationAngle = angle;
if (is.object(options) && options.background) {
const backgroundColour = color(options.background);
this.options.rotationBackground = [
backgroundColour.red(),
backgroundColour.green(),
backgroundColour.blue(),
Math.round(backgroundColour.alpha() * 255)
];
}
} else {
throw new Error('Unsupported angle: angle must be a positive/negative multiple of 90 ' + angle);
throw new Error('Unsupported angle: angle must be a number.');
}
return this;
}
Expand Down
13 changes: 13 additions & 0 deletions src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,13 @@ class PipelineWorker : public Nan::AsyncWorker {
}
}

// Rotate by degree
if (baton->rotationAngle != 0.0) {
std::vector<double> background;
std::tie(image, background) = sharp::ApplyAlpha(image, baton->rotationBackground);
image = image.rotate(baton->rotationAngle, VImage::option()->set("background", background));
}

// Post extraction
if (baton->topOffsetPost != -1) {
image = image.extract_area(
Expand Down Expand Up @@ -1187,6 +1194,12 @@ NAN_METHOD(pipeline) {
baton->normalise = AttrTo<bool>(options, "normalise");
baton->useExifOrientation = AttrTo<bool>(options, "useExifOrientation");
baton->angle = AttrTo<int32_t>(options, "angle");
baton->rotationAngle = AttrTo<double>(options, "rotationAngle");
// Rotation background colour
v8::Local<v8::Object> rotationBackground = AttrAs<v8::Object>(options, "rotationBackground");
for (unsigned int i = 0; i < 4; i++) {
baton->rotationBackground[i] = AttrTo<double>(rotationBackground, i);
}
baton->rotateBeforePreExtract = AttrTo<bool>(options, "rotateBeforePreExtract");
baton->flip = AttrTo<bool>(options, "flip");
baton->flop = AttrTo<bool>(options, "flop");
Expand Down
3 changes: 3 additions & 0 deletions src/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ struct PipelineBaton {
bool normalise;
bool useExifOrientation;
int angle;
double rotationAngle;
double rotationBackground[4];
bool rotateBeforePreExtract;
bool flip;
bool flop;
Expand Down Expand Up @@ -180,6 +182,7 @@ struct PipelineBaton {
normalise(false),
useExifOrientation(false),
angle(0),
rotationAngle(0.0),
flip(false),
flop(false),
extendTop(0),
Expand Down
Binary file added test/fixtures/expected/rotate-solid-bg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/expected/rotate-transparent-bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 796738d

Please sign in to comment.