Skip to content

Commit

Permalink
Add support for vips_rotate.
Browse files Browse the repository at this point in the history
  • Loading branch information
freezy committed Sep 24, 2018
1 parent 37d385f commit d9ba5cf
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 36 deletions.
151 changes: 120 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 a non-rectangular angle 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,47 @@ const pipeline = sharp()
readableStream.pipe(pipeline);
```

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

Returns **Sharp**

## extract

Extract a region of the image.

- Use `extract` before `resize` for pre-resize extraction.
- Use `extract` after `resize` for post-resize extraction.
- Use `extract` before and after for both.

### Parameters

- `options` **[Object][2]**
- `options.left` **[Number][1]** zero-indexed offset from left edge
- `options.top` **[Number][1]** zero-indexed offset from top edge
- `options.width` **[Number][1]** dimension of extracted image
- `options.height` **[Number][1]** dimension of extracted image

### Examples

```javascript
sharp(input)
.extract({ left: left, top: top, width: width, height: height })
.toFile(output, function(err) {
// Extract a region of the input image, saving in the same format.
});
```

```javascript
sharp(input)
.extract({ left: leftOffsetPre, top: topOffsetPre, width: widthPre, height: heightPre })
.resize(width, height)
.extract({ left: leftOffsetPost, top: topOffsetPost, width: widthPost, height: heightPost })
.toFile(output, function(err) {
// Extract a region, resize, then extract from the resized image
});
```

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

Returns **Sharp**

Expand All @@ -45,7 +90,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 +101,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 +119,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 +133,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 +148,36 @@ 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**

## extend

Extends/pads the edges of the image with the colour provided to the `background` method.
This operation will always occur after resizing and extraction, if any.

### Parameters

- `extend` **([Number][1] \| [Object][2])** single pixel count to add to all edges or an Object with per-edge counts
- `extend.top` **[Number][1]?**
- `extend.left` **[Number][1]?**
- `extend.bottom` **[Number][1]?**
- `extend.right` **[Number][1]?**

### Examples

```javascript
// Resize to 140 pixels wide, then add 10 transparent pixels
// to the top, left and right edges and 20 to the bottom edge
sharp(input)
.resize(140)
.background({r: 0, g: 0, b: 0, alpha: 0})
.extend({top: 10, bottom: 20, left: 10, right: 10})
...
```

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

Returns **Sharp**

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

### Parameters

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

Returns **Sharp**

## trim

Trim "boring" pixels from all edges that contain values within a percentage similarity of the top-left pixel.

### Parameters

- `tolerance` **[Number][1]** value between 1 and 99 representing the percentage similarity. (optional, default `10`)


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

Returns **Sharp**

Expand All @@ -130,7 +217,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 +227,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 +237,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 +247,7 @@ Alternative spelling of normalise.

### Parameters

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

Returns **Sharp**

Expand All @@ -170,10 +257,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 +280,7 @@ sharp(input)
});
```

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

Returns **Sharp**

Expand All @@ -204,12 +291,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 +309,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 +332,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 a non-rectangular angle 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 (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 positive or negative number.');
}
return this;
}
Expand Down
15 changes: 15 additions & 0 deletions src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,15 @@ 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 +1196,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
Loading

0 comments on commit d9ba5cf

Please sign in to comment.