Compare commits
91 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3511723914 | ||
|
|
6a1c7b7588 | ||
|
|
18fd6ef119 | ||
|
|
0004f5d2ff | ||
|
|
5f29d1ba9c | ||
|
|
791fd35c35 | ||
|
|
e0d622d347 | ||
|
|
6b34e8a804 | ||
|
|
eb8773fe3e | ||
|
|
b40e3fa1f1 | ||
|
|
d25d761b55 | ||
|
|
d6051dd714 | ||
|
|
53ff061efa | ||
|
|
72b0efd393 | ||
|
|
df97ef23d9 | ||
|
|
f6373971bd | ||
|
|
ec617f2489 | ||
|
|
502ae78579 | ||
|
|
49297d6afb | ||
|
|
29354badd8 | ||
|
|
3c4de796c8 | ||
|
|
c7f4488e77 | ||
|
|
d8765f955d | ||
|
|
9f20037dad | ||
|
|
2ebb090df2 | ||
|
|
110fff3ab9 | ||
|
|
f42a1ceab7 | ||
|
|
9e39a7fa95 | ||
|
|
c879df3b31 | ||
|
|
361ed98353 | ||
|
|
d45f8ef2d3 | ||
|
|
d6a63d11d7 | ||
|
|
4c6804eadc | ||
|
|
99810c0311 | ||
|
|
d15fb1ab1b | ||
|
|
0a6d8b37ad | ||
|
|
f78ffdb9ce | ||
|
|
b7b6fdbdf5 | ||
|
|
e398b471e1 | ||
|
|
48f69f3d88 | ||
|
|
95850d75f6 | ||
|
|
c41d755441 | ||
|
|
39a21787b7 | ||
|
|
36078f9903 | ||
|
|
2f534dc01c | ||
|
|
c8e59f08ec | ||
|
|
19dd6a997f | ||
|
|
4d1a1694cd | ||
|
|
52bea15ad7 | ||
|
|
6592361c5a | ||
|
|
f3f83494f5 | ||
|
|
1169afbe90 | ||
|
|
301bfbd271 | ||
|
|
46aec7eabc | ||
|
|
4cd3b66761 | ||
|
|
567e3dd258 | ||
|
|
fcf853712c | ||
|
|
088d36b47b | ||
|
|
27fb864ac4 | ||
|
|
4001c4a48a | ||
|
|
f64c18ef15 | ||
|
|
f8e72f443d | ||
|
|
5e015cc3ca | ||
|
|
9707f8c5d2 | ||
|
|
6b1d698448 | ||
|
|
72f69dda30 | ||
|
|
8b5d8a0577 | ||
|
|
1aa053ce6f | ||
|
|
701b1c4216 | ||
|
|
f1c4cef781 | ||
|
|
6fe5b307b1 | ||
|
|
679ce08998 | ||
|
|
eeb923eb5b | ||
|
|
142c431745 | ||
|
|
81f5589411 | ||
|
|
04f5c884a4 | ||
|
|
d8df503404 | ||
|
|
d241efcdbe | ||
|
|
a1b8efe721 | ||
|
|
815d076b35 | ||
|
|
86b4816b3f | ||
|
|
473055468a | ||
|
|
c6a28db8b1 | ||
|
|
971f567571 | ||
|
|
7e2eca3d1e | ||
|
|
86b0053bf0 | ||
|
|
cfc4b282f0 | ||
|
|
b85d2aa565 | ||
|
|
70a3d4fb5e | ||
|
|
d9b667e346 | ||
|
|
3a1db53d5a |
40
.travis.yml
@@ -1,21 +1,27 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "4"
|
||||
- "6"
|
||||
- "7"
|
||||
os:
|
||||
- linux
|
||||
- osx
|
||||
sudo: false
|
||||
addons:
|
||||
apt:
|
||||
sources:
|
||||
- ubuntu-toolchain-r-test
|
||||
packages:
|
||||
- g++-4.8
|
||||
osx_image: xcode8
|
||||
before_install:
|
||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then export CXX=g++-4.8; fi
|
||||
matrix:
|
||||
include:
|
||||
- os: linux
|
||||
dist: trusty
|
||||
sudo: false
|
||||
node_js: "4"
|
||||
- os: linux
|
||||
dist: trusty
|
||||
sudo: false
|
||||
node_js: "6"
|
||||
- os: linux
|
||||
dist: trusty
|
||||
sudo: false
|
||||
node_js: "8"
|
||||
- os: osx
|
||||
osx_image: xcode8
|
||||
node_js: "4"
|
||||
- os: osx
|
||||
osx_image: xcode8
|
||||
node_js: "6"
|
||||
- os: osx
|
||||
osx_image: xcode8
|
||||
node_js: "8"
|
||||
after_success:
|
||||
- npm install coveralls
|
||||
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
|
||||
|
||||
@@ -41,7 +41,6 @@ Any change that modifies the existing public API should be added to the relevant
|
||||
|
||||
| Release | WIP branch |
|
||||
| ------: | :--------- |
|
||||
| v0.17.0 | quill |
|
||||
| v0.18.0 | ridge |
|
||||
| v0.19.0 | suit |
|
||||
|
||||
@@ -72,12 +71,7 @@ These can be converted to Markdown by running:
|
||||
npm run docs
|
||||
```
|
||||
|
||||
The `types.d.ts` TypeScript declaration can be generated by running:
|
||||
```sh
|
||||
npm run types
|
||||
```
|
||||
|
||||
Please include documentation and TypeScript declaration updates in any Pull Request that modifies the public API.
|
||||
Please include documentation updates in any Pull Request that modifies the public API.
|
||||
|
||||
## Run the tests
|
||||
|
||||
|
||||
@@ -30,8 +30,11 @@ import sharp from 'sharp';
|
||||
sharp(inputBuffer)
|
||||
.resize(320, 240)
|
||||
.toFile('output.webp', (err, info) => ... );
|
||||
// A Promises/A+ promise is returned when callback is not provided.
|
||||
```
|
||||
|
||||
|
||||
|
||||
```javascript
|
||||
sharp('input.jpg')
|
||||
.rotate()
|
||||
@@ -74,7 +77,7 @@ covers reporting bugs, requesting features and submitting code changes.
|
||||
|
||||
### Licence
|
||||
|
||||
Copyright 2013, 2014, 2015, 2016 Lovell Fuller and contributors.
|
||||
Copyright 2013, 2014, 2015, 2016, 2017 Lovell Fuller and contributors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -107,7 +107,7 @@
|
||||
['OS == "linux"', {
|
||||
'defines': [
|
||||
# Inspect libvips-cpp.so to determine which C++11 ABI version was used and set _GLIBCXX_USE_CXX11_ABI accordingly. This is quite horrible.
|
||||
'_GLIBCXX_USE_CXX11_ABI=<!(if readelf -Ws "$(PKG_CONFIG_PATH="<(pkg_config_path)" pkg-config --libs-only-L vips-cpp | cut -c 3- | sed -e "s/[[:space:]]*$//" | sed -e "s/^$/\/usr\/lib/")/libvips-cpp.so" | c++filt | grep -qF __cxx11;then echo "1";else echo "0";fi)'
|
||||
'_GLIBCXX_USE_CXX11_ABI=<!(if readelf -Ws "$(PKG_CONFIG_PATH="<(pkg_config_path)" pkg-config --variable libdir vips-cpp)/libvips-cpp.so" | c++filt | grep -qF __cxx11;then echo "1";else echo "0";fi)'
|
||||
]
|
||||
}]
|
||||
]
|
||||
@@ -144,7 +144,7 @@
|
||||
}],
|
||||
['OS == "linux"', {
|
||||
'variables': {
|
||||
'download_vips': '<!(LDD_VERSION="<!(ldd --version 2>&1 || true)" node -e "require(\'./binding\').download_vips()")'
|
||||
'download_vips': '<!(node -e "require(\'./binding\').download_vips()")'
|
||||
},
|
||||
'defines': [
|
||||
'_GLIBCXX_USE_CXX11_ABI=0'
|
||||
@@ -267,7 +267,7 @@
|
||||
'vendor/lib/libstdc++-6.dll',
|
||||
'vendor/lib/libtiff-5.dll',
|
||||
'vendor/lib/libvips-42.dll',
|
||||
'vendor/lib/libwebp-6.dll',
|
||||
'vendor/lib/libwebp-7.dll',
|
||||
'vendor/lib/libxml2-2.dll',
|
||||
'vendor/lib/zlib1.dll'
|
||||
]
|
||||
|
||||
70
binding.js
@@ -3,14 +3,14 @@
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const zlib = require('zlib');
|
||||
|
||||
const caw = require('caw');
|
||||
const got = require('got');
|
||||
const simpleGet = require('simple-get');
|
||||
const semver = require('semver');
|
||||
const tar = require('tar');
|
||||
const detectLibc = require('detect-libc');
|
||||
|
||||
const distBaseUrl = 'https://dl.bintray.com/lovell/sharp/';
|
||||
const distBaseUrl = process.env.SHARP_DIST_BASE_URL || 'https://dl.bintray.com/lovell/sharp/';
|
||||
|
||||
// Use NPM-provided environment variable where available, falling back to require-based method for Electron
|
||||
const minimumLibvipsVersion = process.env.npm_package_config_libvips || require('./package.json').config.libvips;
|
||||
@@ -29,21 +29,22 @@ const isFile = function (file) {
|
||||
};
|
||||
|
||||
const unpack = function (tarPath, done) {
|
||||
const extractor = tar.Extract({ path: path.join(__dirname, 'vendor') });
|
||||
if (done) {
|
||||
extractor.on('end', done);
|
||||
}
|
||||
extractor.on('error', error);
|
||||
fs.createReadStream(tarPath)
|
||||
.on('error', error)
|
||||
.pipe(zlib.Unzip())
|
||||
.pipe(extractor);
|
||||
const vendorPath = path.join(__dirname, 'vendor');
|
||||
fs.mkdirSync(vendorPath);
|
||||
tar
|
||||
.extract({
|
||||
file: tarPath,
|
||||
cwd: vendorPath,
|
||||
strict: true
|
||||
})
|
||||
.then(done)
|
||||
.catch(error);
|
||||
};
|
||||
|
||||
const platformId = function () {
|
||||
const platformId = [platform];
|
||||
if (arch === 'arm' || arch === 'armhf' || arch === 'arch64') {
|
||||
const armVersion = (arch === 'arch64') ? '8' : process.env.npm_config_armv || process.config.variables.arm_version || '6';
|
||||
if (arch === 'arm' || arch === 'armhf' || arch === 'arm64') {
|
||||
const armVersion = (arch === 'arm64') ? '8' : process.env.npm_config_armv || process.config.variables.arm_version || '6';
|
||||
platformId.push('armv' + armVersion);
|
||||
} else {
|
||||
platformId.push(arch);
|
||||
@@ -68,19 +69,15 @@ module.exports.download_vips = function () {
|
||||
if (!isFile(vipsHeaderPath)) {
|
||||
// Ensure Intel 64-bit or ARM
|
||||
if (arch === 'ia32') {
|
||||
error('Intel Architecture 32-bit systems require manual installation - please see http://sharp.dimens.io/en/stable/install/');
|
||||
error('Intel Architecture 32-bit systems require manual installation of libvips - please see http://sharp.dimens.io/page/install');
|
||||
}
|
||||
// Ensure glibc >= 2.15
|
||||
const lddVersion = process.env.LDD_VERSION;
|
||||
if (lddVersion) {
|
||||
if (/(glibc|gnu libc)/i.test(lddVersion)) {
|
||||
const glibcVersion = lddVersion ? lddVersion.split(/\n/)[0].split(' ').slice(-1)[0].trim() : '';
|
||||
if (glibcVersion && semver.lt(glibcVersion + '.0', '2.13.0')) {
|
||||
error('glibc version ' + glibcVersion + ' requires manual installation - please see http://sharp.dimens.io/en/stable/install/');
|
||||
}
|
||||
} else {
|
||||
error(lddVersion.split(/\n/)[0] + ' requires manual installation - please see http://sharp.dimens.io/en/stable/install/');
|
||||
}
|
||||
// Ensure glibc Linux
|
||||
if (detectLibc.isNonGlibcLinux) {
|
||||
error(`Use with ${detectLibc.family} libc requires manual installation of libvips - please see http://sharp.dimens.io/page/install`);
|
||||
}
|
||||
// Ensure glibc >= 2.13
|
||||
if (detectLibc.family === detectLibc.GLIBC && detectLibc.version && semver.lt(`${detectLibc.version}.0`, '2.13.0')) {
|
||||
error(`Use with glibc version ${detectLibc.version} requires manual installation of libvips - please see http://sharp.dimens.io/page/install`);
|
||||
}
|
||||
// Arch/platform-specific .tar.gz
|
||||
const tarFilename = ['libvips', minimumLibvipsVersion, platformId()].join('-') + '.tar.gz';
|
||||
@@ -98,19 +95,22 @@ module.exports.download_vips = function () {
|
||||
} catch (err) {}
|
||||
});
|
||||
});
|
||||
const gotOpt = {};
|
||||
if (process.env.npm_config_https_proxy) {
|
||||
// Use the NPM-configured HTTPS proxy
|
||||
gotOpt.agent = caw(process.env.npm_config_https_proxy);
|
||||
}
|
||||
const url = distBaseUrl + tarFilename;
|
||||
got.stream(url, gotOpt).on('response', function (response) {
|
||||
const simpleGetOpt = {
|
||||
url: url,
|
||||
agent: caw(null, {
|
||||
protocol: 'https'
|
||||
})
|
||||
};
|
||||
simpleGet(simpleGetOpt, function (err, response) {
|
||||
if (err) {
|
||||
error('Download of ' + url + ' failed: ' + err.message);
|
||||
}
|
||||
if (response.statusCode !== 200) {
|
||||
error(url + ' status code ' + response.statusCode);
|
||||
}
|
||||
}).on('error', function (err) {
|
||||
error('Download of ' + url + ' failed: ' + err.message);
|
||||
}).pipe(tmpFile);
|
||||
response.pipe(tmpFile);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
machine:
|
||||
node:
|
||||
version: v4.6.1
|
||||
version: v4.8.4
|
||||
services:
|
||||
- docker
|
||||
test:
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
# extractChannel
|
||||
### Table of Contents
|
||||
|
||||
- [extractChannel](#extractchannel)
|
||||
- [joinChannel](#joinchannel)
|
||||
- [bandbool](#bandbool)
|
||||
|
||||
## extractChannel
|
||||
|
||||
Extract a single channel from a multi-channel image.
|
||||
|
||||
@@ -23,7 +29,7 @@ sharp(input)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# joinChannel
|
||||
## joinChannel
|
||||
|
||||
Join one or more channels to the image.
|
||||
The meaning of the added channels depends on the output colourspace, set with `toColourspace()`.
|
||||
@@ -46,7 +52,7 @@ For raw pixel input, the `options` object should contain a `raw` attribute, whic
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# bandbool
|
||||
## bandbool
|
||||
|
||||
Perform a bitwise boolean operation on all input image channels (bands) to produce a single channel output image.
|
||||
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
# background
|
||||
### Table of Contents
|
||||
|
||||
- [background](#background)
|
||||
- [greyscale](#greyscale)
|
||||
- [grayscale](#grayscale)
|
||||
- [toColourspace](#tocolourspace)
|
||||
- [toColorspace](#tocolorspace)
|
||||
|
||||
## background
|
||||
|
||||
Set the background for the `embed`, `flatten` and `extend` operations.
|
||||
The default background is `{r: 0, g: 0, b: 0, alpha: 1}`, black without transparency.
|
||||
@@ -18,7 +26,7 @@ The alpha value is a float between `0` (transparent) and `1` (opaque).
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# greyscale
|
||||
## greyscale
|
||||
|
||||
Convert to 8-bit greyscale; 256 shades of grey.
|
||||
This is a linear operation. If the input image is in a non-linear colour space such as sRGB, use `gamma()` with `greyscale()` for the best results.
|
||||
@@ -29,21 +37,21 @@ An alpha channel may be present, and will be unchanged by the operation.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `greyscale` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** (optional, default `true`)
|
||||
- `greyscale` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# grayscale
|
||||
## grayscale
|
||||
|
||||
Alternative spelling of `greyscale`.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `grayscale` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** (optional, default `true`)
|
||||
- `grayscale` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# toColourspace
|
||||
## toColourspace
|
||||
|
||||
Set the output colourspace.
|
||||
By default output image will be web-friendly sRGB, with additional channels interpreted as alpha channels.
|
||||
@@ -57,7 +65,7 @@ By default output image will be web-friendly sRGB, with additional channels inte
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# toColorspace
|
||||
## toColorspace
|
||||
|
||||
Alternative spelling of `toColourspace`.
|
||||
|
||||
|
||||
@@ -1,25 +1,37 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
# overlayWith
|
||||
### Table of Contents
|
||||
|
||||
- [overlayWith](#overlaywith)
|
||||
|
||||
## overlayWith
|
||||
|
||||
Overlay (composite) an image over the processed (resized, extracted etc.) image.
|
||||
|
||||
The overlay image must be the same size or smaller than the processed image.
|
||||
If both `top` and `left` options are provided, they take precedence over `gravity`.
|
||||
|
||||
If the overlay image contains an alpha channel then composition with premultiplication will occur.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `overlay` **([Buffer](https://nodejs.org/api/buffer.html) \| [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String))** Buffer containing image data or String containing the path to an image file.
|
||||
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?**
|
||||
- `options.gravity` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** gravity at which to place the overlay. (optional, default `'centre'`)
|
||||
- `options.gravity` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** gravity at which to place the overlay. (optional, default `'centre'`)
|
||||
- `options.top` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** the pixel offset from the top edge.
|
||||
- `options.left` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** the pixel offset from the left edge.
|
||||
- `options.tile` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** set to true to repeat the overlay image across the entire image with the given `gravity`. (optional, default `false`)
|
||||
- `options.cutout` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** set to true to apply only the alpha channel of the overlay image to the input image, giving the appearance of one image being cut out of another. (optional, default `false`)
|
||||
- `options.tile` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** set to true to repeat the overlay image across the entire image with the given `gravity`. (optional, default `false`)
|
||||
- `options.cutout` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** set to true to apply only the alpha channel of the overlay image to the input image, giving the appearance of one image being cut out of another. (optional, default `false`)
|
||||
- `options.density` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** integral number representing the DPI for vector overlay image. (optional, default `72`)
|
||||
- `options.raw` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** describes overlay when using raw pixel data.
|
||||
- `options.raw.width` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.raw.height` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.raw.channels` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.create` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** describes a blank overlay to be created.
|
||||
- `options.create.width` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.create.height` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.create.channels` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** 3-4
|
||||
- `options.create.background` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object))?** parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha.
|
||||
|
||||
**Examples**
|
||||
|
||||
|
||||
@@ -1,19 +1,31 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
# Sharp
|
||||
### Table of Contents
|
||||
|
||||
- [Sharp](#sharp)
|
||||
- [format](#format)
|
||||
- [versions](#versions)
|
||||
- [queue](#queue)
|
||||
|
||||
## Sharp
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `input` **([Buffer](https://nodejs.org/api/buffer.html) \| [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String))?** if present, can be
|
||||
a Buffer containing JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data, or
|
||||
a String containing the path to an JPEG, PNG, WebP, GIF, SVG or TIFF image file.
|
||||
JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when null or undefined.
|
||||
JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when not present.
|
||||
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** if present, is an Object with optional attributes.
|
||||
- `options.density` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** integral number representing the DPI for vector images. (optional, default `72`)
|
||||
- `options.raw` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** describes raw pixel image data. See `raw()` for pixel ordering.
|
||||
- `options.density` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** integral number representing the DPI for vector images. (optional, default `72`)
|
||||
- `options.raw` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** describes raw pixel input image data. See `raw()` for pixel ordering.
|
||||
- `options.raw.width` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.raw.height` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.raw.channels` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.raw.channels` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** 1-4
|
||||
- `options.create` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** describes a new image to be created.
|
||||
- `options.create.width` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.create.height` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.create.channels` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** 3-4
|
||||
- `options.create.background` **([String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object))?** parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha.
|
||||
|
||||
**Examples**
|
||||
|
||||
@@ -39,23 +51,38 @@ var transformer = sharp()
|
||||
readableStream.pipe(transformer).pipe(writableStream);
|
||||
```
|
||||
|
||||
```javascript
|
||||
// Create a blank 300x200 PNG image of semi-transluent red pixels
|
||||
sharp({
|
||||
create: {
|
||||
width: 300,
|
||||
height: 200,
|
||||
channels: 4,
|
||||
background: { r: 255, g: 0, b: 0, alpha: 128 }
|
||||
}
|
||||
})
|
||||
.png()
|
||||
.toBuffer()
|
||||
.then( ... );
|
||||
```
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
|
||||
Returns **[Sharp](#sharp)**
|
||||
|
||||
## format
|
||||
### format
|
||||
|
||||
An Object containing nested boolean values representing the available input and output formats/methods.
|
||||
|
||||
**Examples**
|
||||
|
||||
```javascript
|
||||
console.log(sharp.format());
|
||||
console.log(sharp.format);
|
||||
```
|
||||
|
||||
Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
||||
|
||||
## versions
|
||||
### versions
|
||||
|
||||
An Object containing the version numbers of libvips and its dependencies.
|
||||
|
||||
@@ -65,7 +92,7 @@ An Object containing the version numbers of libvips and its dependencies.
|
||||
console.log(sharp.versions);
|
||||
```
|
||||
|
||||
# queue
|
||||
## queue
|
||||
|
||||
An EventEmitter that emits a `change` event when a task is either:
|
||||
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
# clone
|
||||
### Table of Contents
|
||||
|
||||
- [clone](#clone)
|
||||
- [metadata](#metadata)
|
||||
- [limitInputPixels](#limitinputpixels)
|
||||
- [sequentialRead](#sequentialread)
|
||||
|
||||
## clone
|
||||
|
||||
Take a "snapshot" of the Sharp instance, returning a new instance.
|
||||
Cloned instances inherit the input of their parent instance.
|
||||
@@ -19,16 +26,17 @@ readableStream.pipe(pipeline);
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# metadata
|
||||
## metadata
|
||||
|
||||
Fast access to image metadata without decoding any compressed image data.
|
||||
Fast access to (uncached) image metadata without decoding any compressed image data.
|
||||
A Promises/A+ promise is returned when `callback` is not provided.
|
||||
|
||||
- `format`: Name of decoder used to decompress image data e.g. `jpeg`, `png`, `webp`, `gif`, `svg`
|
||||
- `width`: Number of pixels wide
|
||||
- `height`: Number of pixels high
|
||||
- `space`: Name of colour space interpretation e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://github.com/jcupitt/libvips/blob/master/libvips/iofuncs/enumtypes.c#L568)
|
||||
- `space`: Name of colour space interpretation e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://github.com/jcupitt/libvips/blob/master/libvips/iofuncs/enumtypes.c#L636)
|
||||
- `channels`: Number of bands e.g. `3` for sRGB, `4` for CMYK
|
||||
- `depth`: Name of pixel depth format e.g. `uchar`, `char`, `ushort`, `float` [...](https://github.com/jcupitt/libvips/blob/master/libvips/iofuncs/enumtypes.c#L672)
|
||||
- `density`: Number of pixels per inch (DPI), if present
|
||||
- `hasProfile`: Boolean indicating the presence of an embedded ICC profile
|
||||
- `hasAlpha`: Boolean indicating the presence of an alpha transparency channel
|
||||
@@ -59,7 +67,7 @@ image
|
||||
|
||||
Returns **([Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)> | Sharp)**
|
||||
|
||||
# limitInputPixels
|
||||
## limitInputPixels
|
||||
|
||||
Do not process input images where the number of pixels (width _ height) exceeds this limit.
|
||||
Assumes image dimensions contained in the input metadata can be trusted.
|
||||
@@ -74,13 +82,13 @@ The default limit is 268402689 (0x3FFF _ 0x3FFF) pixels.
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# sequentialRead
|
||||
## sequentialRead
|
||||
|
||||
An advanced setting that switches the libvips access method to `VIPS_ACCESS_SEQUENTIAL`.
|
||||
This will reduce memory usage and can improve performance on some systems.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `sequentialRead` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** (optional, default `true`)
|
||||
- `sequentialRead` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -1,11 +1,33 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
# rotate
|
||||
### Table of Contents
|
||||
|
||||
- [rotate](#rotate)
|
||||
- [extract](#extract)
|
||||
- [flip](#flip)
|
||||
- [flop](#flop)
|
||||
- [sharpen](#sharpen)
|
||||
- [blur](#blur)
|
||||
- [extend](#extend)
|
||||
- [flatten](#flatten)
|
||||
- [trim](#trim)
|
||||
- [gamma](#gamma)
|
||||
- [negate](#negate)
|
||||
- [normalise](#normalise)
|
||||
- [normalize](#normalize)
|
||||
- [convolve](#convolve)
|
||||
- [threshold](#threshold)
|
||||
- [boolean](#boolean)
|
||||
|
||||
## rotate
|
||||
|
||||
Rotate the output image by either an explicit angle
|
||||
or auto-orient based on the EXIF `Orientation` tag.
|
||||
|
||||
Use this method without angle to determine the angle from EXIF data.
|
||||
If an angle is provided, it is converted to a valid 90/180/270deg rotation.
|
||||
For example, `-450` will produce a 270deg rotation.
|
||||
|
||||
If no angle is provided, it is determined from the EXIF data.
|
||||
Mirroring is supported and may infer the use of a flip operation.
|
||||
|
||||
The use of `rotate` implies the removal of the EXIF `Orientation` tag, if any.
|
||||
@@ -15,7 +37,7 @@ for example `rotate(x).extract(y)` will produce a different result to `extract(y
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `angle` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** 0, 90, 180 or 270. (optional, default `auto`)
|
||||
- `angle` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** angle of rotation, must be a multiple of 90. (optional, default `auto`)
|
||||
|
||||
**Examples**
|
||||
|
||||
@@ -35,7 +57,7 @@ readableStream.pipe(pipeline);
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# extract
|
||||
## extract
|
||||
|
||||
Extract a region of the image.
|
||||
|
||||
@@ -75,29 +97,29 @@ sharp(input)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# flip
|
||||
## flip
|
||||
|
||||
Flip the image about the vertical Y axis. This always occurs after rotation, if any.
|
||||
The use of `flip` implies the removal of the EXIF `Orientation` tag, if any.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `flip` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** (optional, default `true`)
|
||||
- `flip` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# flop
|
||||
## flop
|
||||
|
||||
Flop the image about the horizontal X axis. This always occurs after rotation, if any.
|
||||
The use of `flop` implies the removal of the EXIF `Orientation` tag, if any.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `flop` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** (optional, default `true`)
|
||||
- `flop` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# sharpen
|
||||
## sharpen
|
||||
|
||||
Sharpen the image.
|
||||
When used without parameters, performs a fast, mild sharpen of the output image.
|
||||
@@ -107,15 +129,15 @@ Separate control over the level of sharpening in "flat" and "jagged" areas is av
|
||||
**Parameters**
|
||||
|
||||
- `sigma` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`.
|
||||
- `flat` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** the level of sharpening to apply to "flat" areas. (optional, default `1.0`)
|
||||
- `jagged` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** the level of sharpening to apply to "jagged" areas. (optional, default `2.0`)
|
||||
- `flat` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** the level of sharpening to apply to "flat" areas. (optional, default `1.0`)
|
||||
- `jagged` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** the level of sharpening to apply to "jagged" areas. (optional, default `2.0`)
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# blur
|
||||
## blur
|
||||
|
||||
Blur the image.
|
||||
When used without parameters, performs a fast, mild blur of the output image.
|
||||
@@ -130,7 +152,7 @@ When a `sigma` is provided, performs a slower, more accurate Gaussian blur.
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# extend
|
||||
## 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.
|
||||
@@ -150,7 +172,7 @@ This operation will always occur after resizing and extraction, if any.
|
||||
// to the top, left and right edges and 20 to the bottom edge
|
||||
sharp(input)
|
||||
.resize(140)
|
||||
.background({r: 0, g: 0, b: 0, a: 0})
|
||||
.background({r: 0, g: 0, b: 0, alpha: 0})
|
||||
.extend({top: 10, bottom: 20, left: 10, right: 10})
|
||||
...
|
||||
```
|
||||
@@ -159,30 +181,30 @@ sharp(input)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# flatten
|
||||
## flatten
|
||||
|
||||
Merge alpha transparency channel, if any, with `background`.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `flatten` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** (optional, default `true`)
|
||||
- `flatten` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# trim
|
||||
## trim
|
||||
|
||||
Trim "boring" pixels from all edges that contain values within a percentage similarity of the top-left pixel.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `tolerance` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** value between 1 and 99 representing the percentage similarity. (optional, default `10`)
|
||||
- `tolerance` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** value between 1 and 99 representing the percentage similarity. (optional, default `10`)
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# gamma
|
||||
## gamma
|
||||
|
||||
Apply a gamma correction by reducing the encoding (darken) pre-resize at a factor of `1/gamma`
|
||||
then increasing the encoding (brighten) post-resize at a factor of `gamma`.
|
||||
@@ -192,44 +214,44 @@ when applying a gamma correction.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `gamma` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** value between 1.0 and 3.0. (optional, default `2.2`)
|
||||
- `gamma` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** value between 1.0 and 3.0. (optional, default `2.2`)
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# negate
|
||||
## negate
|
||||
|
||||
Produce the "negative" of the image.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `negate` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** (optional, default `true`)
|
||||
- `negate` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# normalise
|
||||
## normalise
|
||||
|
||||
Enhance output image contrast by stretching its luminance to cover the full dynamic range.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `normalise` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** (optional, default `true`)
|
||||
- `normalise` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# normalize
|
||||
## normalize
|
||||
|
||||
Alternative spelling of normalise.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `normalize` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** (optional, default `true`)
|
||||
- `normalize` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# convolve
|
||||
## convolve
|
||||
|
||||
Convolve the image with the specified kernel.
|
||||
|
||||
@@ -239,8 +261,8 @@ Convolve the image with the specified kernel.
|
||||
- `kernel.width` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** width of the kernel in pixels.
|
||||
- `kernel.height` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** width of the kernel in pixels.
|
||||
- `kernel.kernel` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** Array of length `width*height` containing the kernel values.
|
||||
- `kernel.scale` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** the scale of the kernel in pixels. (optional, default `sum`)
|
||||
- `kernel.offset` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** the offset of the kernel in pixels. (optional, default `0`)
|
||||
- `kernel.scale` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** the scale of the kernel in pixels. (optional, default `sum`)
|
||||
- `kernel.offset` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** the offset of the kernel in pixels. (optional, default `0`)
|
||||
|
||||
**Examples**
|
||||
|
||||
@@ -262,23 +284,23 @@ sharp(input)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# threshold
|
||||
## threshold
|
||||
|
||||
Any pixel value greather than or equal to the threshold value will be set to 255, otherwise it will be set to 0.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `threshold` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** a value in the range 0-255 representing the level at which the threshold will be applied. (optional, default `128`)
|
||||
- `threshold` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** a value in the range 0-255 representing the level at which the threshold will be applied. (optional, default `128`)
|
||||
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?**
|
||||
- `options.greyscale` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** convert to single channel greyscale. (optional, default `true`)
|
||||
- `options.grayscale` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** alternative spelling for greyscale. (optional, default `true`)
|
||||
- `options.greyscale` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** convert to single channel greyscale. (optional, default `true`)
|
||||
- `options.grayscale` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** alternative spelling for greyscale. (optional, default `true`)
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# boolean
|
||||
## boolean
|
||||
|
||||
Perform a bitwise boolean operation with operand image.
|
||||
|
||||
|
||||
@@ -1,6 +1,19 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
# toFile
|
||||
### Table of Contents
|
||||
|
||||
- [toFile](#tofile)
|
||||
- [toBuffer](#tobuffer)
|
||||
- [withMetadata](#withmetadata)
|
||||
- [jpeg](#jpeg)
|
||||
- [png](#png)
|
||||
- [webp](#webp)
|
||||
- [tiff](#tiff)
|
||||
- [raw](#raw)
|
||||
- [toFormat](#toformat)
|
||||
- [tile](#tile)
|
||||
|
||||
## toFile
|
||||
|
||||
Write output image data to a file.
|
||||
|
||||
@@ -14,33 +27,37 @@ A Promises/A+ promise is returned when `callback` is not provided.
|
||||
|
||||
- `fileOut` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the path to write the image data to.
|
||||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)?** called on completion with two arguments `(err, info)`.
|
||||
`info` contains the output image `format`, `size` (bytes), `width`, `height` and `channels`.
|
||||
`info` contains the output image `format`, `size` (bytes), `width`, `height`,
|
||||
`channels` and `premultiplied` (indicating if premultiplication was used).
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
|
||||
Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>** when no callback is provided
|
||||
|
||||
# toBuffer
|
||||
## toBuffer
|
||||
|
||||
Write output to a Buffer.
|
||||
JPEG, PNG, WebP, and RAW output are supported.
|
||||
JPEG, PNG, WebP, TIFF and RAW output are supported.
|
||||
By default, the format will match the input image, except GIF and SVG input which become PNG output.
|
||||
|
||||
`callback`, if present, gets three arguments `(err, buffer, info)` where:
|
||||
`callback`, if present, gets three arguments `(err, data, info)` where:
|
||||
|
||||
- `err` is an error message, if any.
|
||||
- `buffer` is the output image data.
|
||||
- `info` contains the output image `format`, `size` (bytes), `width`, `height` and `channels`.
|
||||
A Promises/A+ promise is returned when `callback` is not provided.
|
||||
- `err` is an error, if any.
|
||||
- `data` is the output image data.
|
||||
- `info` contains the output image `format`, `size` (bytes), `width`, `height`,
|
||||
`channels` and `premultiplied` (indicating if premultiplication was used).
|
||||
A Promise is returned when `callback` is not provided.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?**
|
||||
- `options.resolveWithObject` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** Resolve the Promise with an Object containing `data` and `info` properties instead of resolving only with `data`.
|
||||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)?**
|
||||
|
||||
Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Buffer](https://nodejs.org/api/buffer.html)>** when no callback is provided
|
||||
|
||||
# withMetadata
|
||||
## withMetadata
|
||||
|
||||
Include all metadata (EXIF, XMP, IPTC) from the input image in the output image.
|
||||
The default behaviour, when `withMetadata` is not used, is to strip all metadata and convert to the device-independent sRGB colour space.
|
||||
@@ -56,81 +73,89 @@ This will also convert to and add a web-friendly sRGB ICC profile.
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# jpeg
|
||||
## jpeg
|
||||
|
||||
Use these JPEG options for output image.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** output options
|
||||
- `options.quality` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** quality, integer 1-100 (optional, default `80`)
|
||||
- `options.progressive` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** use progressive (interlace) scan (optional, default `false`)
|
||||
- `options.chromaSubsampling` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** set to '4:4:4' to prevent chroma subsampling when quality <= 90 (optional, default `'4:2:0'`)
|
||||
- `options.trellisQuantisation` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** apply trellis quantisation, requires mozjpeg (optional, default `false`)
|
||||
- `options.overshootDeringing` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** apply overshoot deringing, requires mozjpeg (optional, default `false`)
|
||||
- `options.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`)
|
||||
- `options.optimizeScans` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** alternative spelling of optimiseScans (optional, default `false`)
|
||||
- `options.force` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** force JPEG output, otherwise attempt to use input format (optional, default `true`)
|
||||
- `options.quality` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** quality, integer 1-100 (optional, default `80`)
|
||||
- `options.progressive` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** use progressive (interlace) scan (optional, default `false`)
|
||||
- `options.chromaSubsampling` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** set to '4:4:4' to prevent chroma subsampling when quality <= 90 (optional, default `'4:2:0'`)
|
||||
- `options.trellisQuantisation` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** apply trellis quantisation, requires mozjpeg (optional, default `false`)
|
||||
- `options.overshootDeringing` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** apply overshoot deringing, requires mozjpeg (optional, default `false`)
|
||||
- `options.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`)
|
||||
- `options.optimizeScans` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** alternative spelling of optimiseScans (optional, default `false`)
|
||||
- `options.force` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** force JPEG output, otherwise attempt to use input format (optional, default `true`)
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid options
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# png
|
||||
## png
|
||||
|
||||
Use these PNG options for output image.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?**
|
||||
- `options.progressive` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** use progressive (interlace) scan (optional, default `false`)
|
||||
- `options.compressionLevel` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** zlib compression level (optional, default `6`)
|
||||
- `options.adaptiveFiltering` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** use adaptive row filtering (optional, default `true`)
|
||||
- `options.force` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** force PNG output, otherwise attempt to use input format (optional, default `true`)
|
||||
- `options.progressive` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** use progressive (interlace) scan (optional, default `false`)
|
||||
- `options.compressionLevel` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** zlib compression level (optional, default `6`)
|
||||
- `options.adaptiveFiltering` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** use adaptive row filtering (optional, default `true`)
|
||||
- `options.force` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** force PNG output, otherwise attempt to use input format (optional, default `true`)
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid options
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# webp
|
||||
## webp
|
||||
|
||||
Use these WebP options for output image.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** output options
|
||||
- `options.quality` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** quality, integer 1-100 (optional, default `80`)
|
||||
- `options.force` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** force WebP output, otherwise attempt to use input format (optional, default `true`)
|
||||
- `options.quality` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** quality, integer 1-100 (optional, default `80`)
|
||||
- `options.alphaQuality` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** quality of alpha layer, integer 0-100 (optional, default `100`)
|
||||
- `options.lossless` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** use lossless compression mode (optional, default `false`)
|
||||
- `options.nearLossless` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** use near_lossless compression mode (optional, default `false`)
|
||||
- `options.force` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** force WebP output, otherwise attempt to use input format (optional, default `true`)
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid options
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# tiff
|
||||
## tiff
|
||||
|
||||
Use these TIFF options for output image.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** output options
|
||||
- `options.quality` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** quality, integer 1-100 (optional, default `80`)
|
||||
- `options.force` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** force TIFF output, otherwise attempt to use input format (optional, default `true`)
|
||||
- `options.quality` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** quality, integer 1-100 (optional, default `80`)
|
||||
- `options.force` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** force TIFF output, otherwise attempt to use input format (optional, default `true`)
|
||||
- `options.compression` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** compression options: lzw, deflate, jpeg (optional, default `'jpeg'`)
|
||||
- `options.predictor` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** compression predictor options: none, horizontal, float (optional, default `'none'`)
|
||||
- `options.xres` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** horizontal resolution in pixels/mm (optional, default `1.0`)
|
||||
- `options.yres` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** vertical resolution in pixels/mm (optional, default `1.0`)
|
||||
- `options.squash` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** squash 8-bit images down to 1 bit (optional, default `false`)
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid options
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# raw
|
||||
## raw
|
||||
|
||||
Force output to be raw, uncompressed uint8 pixel data.
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# toFormat
|
||||
## toFormat
|
||||
|
||||
Force output to a given format.
|
||||
|
||||
@@ -144,7 +169,7 @@ Force output to a given format.
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# tile
|
||||
## tile
|
||||
|
||||
Use tile-based deep zoom (image pyramid) output.
|
||||
Set the format and options for tile images via the `toFormat`, `jpeg`, `png` or `webp` functions.
|
||||
@@ -153,10 +178,10 @@ Use a `.zip` or `.szi` file extension with `toFile` to write to a compressed arc
|
||||
**Parameters**
|
||||
|
||||
- `tile` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?**
|
||||
- `tile.size` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** tile size in pixels, a value between 1 and 8192. (optional, default `256`)
|
||||
- `tile.overlap` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** tile overlap in pixels, a value between 0 and 8192. (optional, default `0`)
|
||||
- `tile.container` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** tile container, with value `fs` (filesystem) or `zip` (compressed file). (optional, default `'fs'`)
|
||||
- `tile.layout` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** filesystem layout, possible values are `dz`, `zoomify` or `google`. (optional, default `'dz'`)
|
||||
- `tile.size` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** tile size in pixels, a value between 1 and 8192. (optional, default `256`)
|
||||
- `tile.overlap` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** tile overlap in pixels, a value between 0 and 8192. (optional, default `0`)
|
||||
- `tile.container` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** tile container, with value `fs` (filesystem) or `zip` (compressed file). (optional, default `'fs'`)
|
||||
- `tile.layout` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** filesystem layout, possible values are `dz`, `zoomify` or `google`. (optional, default `'dz'`)
|
||||
|
||||
**Examples**
|
||||
|
||||
|
||||
@@ -1,12 +1,23 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
# resize
|
||||
### Table of Contents
|
||||
|
||||
- [resize](#resize)
|
||||
- [crop](#crop)
|
||||
- [embed](#embed)
|
||||
- [max](#max)
|
||||
- [min](#min)
|
||||
- [ignoreAspectRatio](#ignoreaspectratio)
|
||||
- [withoutEnlargement](#withoutenlargement)
|
||||
|
||||
## resize
|
||||
|
||||
Resize image to `width` x `height`.
|
||||
By default, the resized image is centre cropped to the exact size specified.
|
||||
|
||||
Possible reduction 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).
|
||||
@@ -22,13 +33,13 @@ Possible enlargement interpolators are:
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `width` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** pixels wide the resultant image should be, between 1 and 16383 (0x3FFF). 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, between 1 and 16383. Use `null` or `undefined` to auto-scale the height to match the width.
|
||||
- `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.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`)
|
||||
- `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**
|
||||
|
||||
@@ -52,7 +63,7 @@ sharp(inputBuffer)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# crop
|
||||
## crop
|
||||
|
||||
Crop the resized image to the exact size specified, the default behaviour.
|
||||
|
||||
@@ -67,7 +78,7 @@ then repeatedly ranks edge regions, discarding the edge with the lowest score ba
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `crop` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?** A member of `sharp.gravity` to crop to an edge/corner or `sharp.strategy` to crop dynamically. (optional, default `'centre'`)
|
||||
- `crop` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** A member of `sharp.gravity` to crop to an edge/corner or `sharp.strategy` to crop dynamically. (optional, default `'centre'`)
|
||||
|
||||
**Examples**
|
||||
|
||||
@@ -87,7 +98,7 @@ readableStream.pipe(transformer).pipe(writableStream);
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# embed
|
||||
## embed
|
||||
|
||||
Preserving aspect ratio, resize the image to the maximum `width` or `height` specified
|
||||
then embed on a background of the exact `width` and `height` specified.
|
||||
@@ -100,7 +111,7 @@ contain an alpha channel, even when the input image does not.
|
||||
```javascript
|
||||
sharp('input.gif')
|
||||
.resize(200, 300)
|
||||
.background({r: 0, g: 0, b: 0, a: 0})
|
||||
.background({r: 0, g: 0, b: 0, alpha: 0})
|
||||
.embed()
|
||||
.toFormat(sharp.format.webp)
|
||||
.toBuffer(function(err, outputBuffer) {
|
||||
@@ -114,7 +125,7 @@ sharp('input.gif')
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# max
|
||||
## max
|
||||
|
||||
Preserving aspect ratio, resize the image to be as large as possible
|
||||
while ensuring its dimensions are less than or equal to the `width` and `height` specified.
|
||||
@@ -137,7 +148,7 @@ sharp(inputBuffer)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# min
|
||||
## min
|
||||
|
||||
Preserving aspect ratio, resize the image to be as small as possible
|
||||
while ensuring its dimensions are greater than or equal to the `width` and `height` specified.
|
||||
@@ -146,14 +157,14 @@ Both `width` and `height` must be provided via `resize` otherwise the behaviour
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# ignoreAspectRatio
|
||||
## ignoreAspectRatio
|
||||
|
||||
Ignoring the aspect ratio of the input, stretch the image to
|
||||
the exact `width` and/or `height` provided via `resize`.
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
# withoutEnlargement
|
||||
## withoutEnlargement
|
||||
|
||||
Do not enlarge the output image if the input image width _or_ height are already less than the required dimensions.
|
||||
This is equivalent to GraphicsMagick's `>` geometry option:
|
||||
@@ -161,6 +172,6 @@ This is equivalent to GraphicsMagick's `>` geometry option:
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `withoutEnlargement` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** (optional, default `true`)
|
||||
- `withoutEnlargement` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -1,18 +1,25 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
# cache
|
||||
### Table of Contents
|
||||
|
||||
Gets, or when options are provided sets, the limits of _libvips'_ operation cache.
|
||||
- [cache](#cache)
|
||||
- [concurrency](#concurrency)
|
||||
- [counters](#counters)
|
||||
- [simd](#simd)
|
||||
|
||||
## cache
|
||||
|
||||
Gets or, when options are provided, sets the limits of _libvips'_ operation cache.
|
||||
Existing entries in the cache will be trimmed after any change in limits.
|
||||
This method always returns cache statistics,
|
||||
useful for determining how much working memory is required for a particular task.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `options` **([Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) \| [Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean))** Object with the following attributes, or Boolean where true uses default cache settings and false removes all caching.
|
||||
- `options.memory` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** is the maximum memory in MB to use for this cache (optional, default `50`)
|
||||
- `options.files` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** is the maximum number of files to hold open (optional, default `20`)
|
||||
- `options.items` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)?** is the maximum number of operations to cache (optional, default `100`)
|
||||
- `options` **([Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) \| [Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean))** Object with the following attributes, or Boolean where true uses default cache settings and false removes all caching (optional, default `true`)
|
||||
- `options.memory` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** is the maximum memory in MB to use for this cache (optional, default `50`)
|
||||
- `options.files` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** is the maximum number of files to hold open (optional, default `20`)
|
||||
- `options.items` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** is the maximum number of operations to cache (optional, default `100`)
|
||||
|
||||
**Examples**
|
||||
|
||||
@@ -28,9 +35,9 @@ sharp.cache(false);
|
||||
|
||||
Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
||||
|
||||
# concurrency
|
||||
## concurrency
|
||||
|
||||
Gets, or when a concurrency is provided sets,
|
||||
Gets or, when a concurrency is provided, sets
|
||||
the number of threads _libvips'_ should create to process each image.
|
||||
The default value is the number of CPU cores.
|
||||
A value of `0` will reset to this default.
|
||||
@@ -54,7 +61,7 @@ sharp.concurrency(0); // 4
|
||||
|
||||
Returns **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** concurrency
|
||||
|
||||
# counters
|
||||
## counters
|
||||
|
||||
Provides access to internal task counters.
|
||||
|
||||
@@ -69,7 +76,7 @@ const counters = sharp.counters(); // { queue: 2, process: 4 }
|
||||
|
||||
Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
||||
|
||||
# simd
|
||||
## simd
|
||||
|
||||
Get and set use of SIMD vector unit instructions.
|
||||
Requires libvips to have been compiled with liborc support.
|
||||
@@ -82,7 +89,7 @@ Versions of liborc prior to 0.4.25 are known to segfault under heavy load.
|
||||
|
||||
**Parameters**
|
||||
|
||||
- `simd` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)?** (optional, default `false`)
|
||||
- `simd` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `false`)
|
||||
|
||||
**Examples**
|
||||
|
||||
|
||||
@@ -1,9 +1,151 @@
|
||||
# Changelog
|
||||
|
||||
### v0.18 - "*ridge*"
|
||||
|
||||
Requires libvips v8.5.5.
|
||||
|
||||
#### v0.18.4 - 18<sup>th</sup> September 2017
|
||||
|
||||
* Ensure input Buffer really is marked as Persistent, prevents mark-sweep GC.
|
||||
[#950](https://github.com/lovell/sharp/issues/950)
|
||||
[@lfdoherty](https://github.com/lfdoherty)
|
||||
|
||||
#### v0.18.3 - 13<sup>th</sup> September 2017
|
||||
|
||||
* Skip shrink-on-load when trimming.
|
||||
[#888](https://github.com/lovell/sharp/pull/888)
|
||||
[@kleisauke](https://github.com/kleisauke)
|
||||
|
||||
* Migrate from got to simple-get for basic auth support.
|
||||
[#945](https://github.com/lovell/sharp/pull/945)
|
||||
[@pbomb](https://github.com/pbomb)
|
||||
|
||||
#### v0.18.2 - 1<sup>st</sup> July 2017
|
||||
|
||||
* Expose libvips' xres and yres properties for TIFF output.
|
||||
[#828](https://github.com/lovell/sharp/pull/828)
|
||||
[@YvesBos](https://github.com/YvesBos)
|
||||
|
||||
* Ensure flip and flop operations work with auto-rotate.
|
||||
[#837](https://github.com/lovell/sharp/issues/837)
|
||||
[@rexxars](https://github.com/rexxars)
|
||||
|
||||
* Allow binary download URL override via SHARP_DIST_BASE_URL env variable.
|
||||
[#841](https://github.com/lovell/sharp/issues/841)
|
||||
|
||||
* Add support for Solus Linux.
|
||||
[#857](https://github.com/lovell/sharp/pull/857)
|
||||
[@ekremkaraca](https://github.com/ekremkaraca)
|
||||
|
||||
#### v0.18.1 - 30<sup>th</sup> May 2017
|
||||
|
||||
* Remove regression from #781 that could cause incorrect shrink calculation.
|
||||
[#831](https://github.com/lovell/sharp/issues/831)
|
||||
[@suprMax](https://github.com/suprMax)
|
||||
|
||||
#### v0.18.0 - 30<sup>th</sup> May 2017
|
||||
|
||||
* Remove the previously-deprecated output format "option" functions:
|
||||
quality, progressive, compressionLevel, withoutAdaptiveFiltering,
|
||||
withoutChromaSubsampling, trellisQuantisation, trellisQuantization,
|
||||
overshootDeringing, optimiseScans and optimizeScans.
|
||||
|
||||
* Ensure maximum output dimensions are based on the format to be used.
|
||||
[#176](https://github.com/lovell/sharp/issues/176)
|
||||
[@stephanebachelier](https://github.com/stephanebachelier)
|
||||
|
||||
* Avoid costly (un)premultiply when using overlayWith without alpha channel.
|
||||
[#573](https://github.com/lovell/sharp/issues/573)
|
||||
[@strarsis](https://github.com/strarsis)
|
||||
|
||||
* Include pixel depth (e.g. "uchar") when reading metadata.
|
||||
[#577](https://github.com/lovell/sharp/issues/577)
|
||||
[@moedusa](https://github.com/moedusa)
|
||||
|
||||
* Add support for Buffer and Stream-based TIFF output.
|
||||
[#587](https://github.com/lovell/sharp/issues/587)
|
||||
[@strarsis](https://github.com/strarsis)
|
||||
|
||||
* Expose warnings from libvips via NODE_DEBUG=sharp environment variable.
|
||||
[#607](https://github.com/lovell/sharp/issues/607)
|
||||
[@puzrin](https://github.com/puzrin)
|
||||
|
||||
* Switch to the libvips implementation of "attention" and "entropy" crop strategies.
|
||||
[#727](https://github.com/lovell/sharp/issues/727)
|
||||
|
||||
* Improve performance and accuracy of nearest neighbour integral upsampling.
|
||||
[#752](https://github.com/lovell/sharp/issues/752)
|
||||
[@MrIbby](https://github.com/MrIbby)
|
||||
|
||||
* Constructor single argument API: allow plain object, reject null/undefined.
|
||||
[#768](https://github.com/lovell/sharp/issues/768)
|
||||
[@kub1x](https://github.com/kub1x)
|
||||
|
||||
* Ensure ARM64 pre-built binaries use correct C++11 ABI version.
|
||||
[#772](https://github.com/lovell/sharp/issues/772)
|
||||
[@ajiratech2](https://github.com/ajiratech2)
|
||||
|
||||
* Prevent aliasing by using dynamic values for shrink(-on-load).
|
||||
[#781](https://github.com/lovell/sharp/issues/781)
|
||||
[@kleisauke](https://github.com/kleisauke)
|
||||
|
||||
* Expose libvips' "squash" parameter to enable 1-bit TIFF output.
|
||||
[#783](https://github.com/lovell/sharp/pull/783)
|
||||
[@YvesBos](https://github.com/YvesBos)
|
||||
|
||||
* Add support for rotation using any multiple of +/-90 degrees.
|
||||
[#791](https://github.com/lovell/sharp/pull/791)
|
||||
[@ncoden](https://github.com/ncoden)
|
||||
|
||||
* Add "jpg" alias to toFormat as shortened form of "jpeg".
|
||||
[#814](https://github.com/lovell/sharp/pull/814)
|
||||
[@jingsam](https://github.com/jingsam)
|
||||
|
||||
### v0.17 - "*quill*"
|
||||
|
||||
Requires libvips v8.4.2.
|
||||
|
||||
#### v0.17.3 - 1<sup>st</sup> April 2017
|
||||
|
||||
* Allow toBuffer to optionally resolve a Promise with both info and data.
|
||||
[#143](https://github.com/lovell/sharp/issues/143)
|
||||
[@salzhrani](https://github.com/salzhrani)
|
||||
|
||||
* Create blank image of given width, height, channels and background.
|
||||
[#470](https://github.com/lovell/sharp/issues/470)
|
||||
[@pjarts](https://github.com/pjarts)
|
||||
|
||||
* Add support for the "nearest" kernel for image reductions.
|
||||
[#732](https://github.com/lovell/sharp/pull/732)
|
||||
[@alice0meta](https://github.com/alice0meta)
|
||||
|
||||
* Add support for TIFF compression and predictor options.
|
||||
[#738](https://github.com/lovell/sharp/pull/738)
|
||||
[@kristojorg](https://github.com/kristojorg)
|
||||
|
||||
#### v0.17.2 - 11<sup>th</sup> February 2017
|
||||
|
||||
* Ensure Readable side of Stream can start flowing after Writable side has finished.
|
||||
[#671](https://github.com/lovell/sharp/issues/671)
|
||||
[@danhaller](https://github.com/danhaller)
|
||||
|
||||
* Expose WebP alpha quality, lossless and near-lossless output options.
|
||||
[#685](https://github.com/lovell/sharp/pull/685)
|
||||
[@rnanwani](https://github.com/rnanwani)
|
||||
|
||||
#### v0.17.1 - 15<sup>th</sup> January 2017
|
||||
|
||||
* Improve error messages for invalid parameters.
|
||||
[@spikeon](https://github.com/spikeon)
|
||||
[#644](https://github.com/lovell/sharp/pull/644)
|
||||
|
||||
* Simplify expression for finding vips-cpp libdir.
|
||||
[#656](https://github.com/lovell/sharp/pull/656)
|
||||
|
||||
* Allow HTTPS-over-HTTP proxy when downloading pre-compiled dependencies.
|
||||
[@wangzhiwei1888](https://github.com/wangzhiwei1888)
|
||||
[#679](https://github.com/lovell/sharp/issues/679)
|
||||
|
||||
#### v0.17.0 - 11<sup>th</sup> December 2016
|
||||
|
||||
* Drop support for versions of Node prior to v4.
|
||||
|
||||
@@ -22,7 +22,7 @@ the installation of any external runtime dependencies.
|
||||
|
||||
This module supports reading JPEG, PNG, WebP, TIFF, GIF and SVG images.
|
||||
|
||||
Output images can be in JPEG, PNG and WebP formats as well as uncompressed raw pixel data.
|
||||
Output images can be in JPEG, PNG, WebP and TIFF formats as well as uncompressed raw pixel data.
|
||||
|
||||
Streams, Buffer objects and the filesystem can be used for input and output.
|
||||
|
||||
@@ -94,12 +94,20 @@ the help and code contributions of the following people:
|
||||
* [Kleis Auke Wolthuizen](https://github.com/kleisauke)
|
||||
* [Matt Hirsch](https://github.com/mhirsch)
|
||||
* [Rahul Nanwani](https://github.com/rnanwani)
|
||||
* [Matthias Thoemmes](https://github.com/cmtt)
|
||||
* [Patrick Paskaris](https://github.com/ppaskaris)
|
||||
* [Jérémy Lal](https://github.com/kapouer)
|
||||
* [Alice Monday](https://github.com/alice0meta)
|
||||
* [Kristo Jorgenson](https://github.com/kristojorg)
|
||||
* [Yves Bos](https://github.com/YvesBos)
|
||||
* [Nicolas Coden](https://github.com/ncoden)
|
||||
* [Matt Parrish](https://github.com/pbomb)
|
||||
|
||||
Thank you!
|
||||
|
||||
### Licence
|
||||
|
||||
Copyright 2013, 2014, 2015, 2016 Lovell Fuller and contributors.
|
||||
Copyright 2013, 2014, 2015, 2016, 2017 Lovell Fuller and contributors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -10,9 +10,9 @@ yarn add sharp
|
||||
|
||||
### Prerequisites
|
||||
|
||||
* Node v4+
|
||||
* Node v4.5.0+
|
||||
* C++11 compatible compiler such as gcc 4.8+, clang 3.0+ or MSVC 2013+
|
||||
* [node-gyp](https://github.com/TooTallNate/node-gyp#installation) and its dependencies
|
||||
* [node-gyp](https://github.com/TooTallNate/node-gyp#installation) and its dependencies (includes Python)
|
||||
|
||||
### Linux
|
||||
|
||||
@@ -20,18 +20,19 @@ yarn add sharp
|
||||
[](https://circleci.com/gh/lovell/sharp)
|
||||
|
||||
libvips and its dependencies are fetched and stored within `node_modules/sharp/vendor` during `npm install`.
|
||||
This involves an automated HTTPS download of approximately 6.5MB.
|
||||
This involves an automated HTTPS download of approximately 7MB.
|
||||
|
||||
Most recent Linux-based operating systems with glibc running on x64 and ARMv6+ CPUs should "just work", e.g.:
|
||||
|
||||
* Debian 7, 8
|
||||
* Ubuntu 12.04, 14.04, 16.04
|
||||
* Centos 7
|
||||
* Fedora 23, 24
|
||||
* Fedora
|
||||
* openSUSE 13.2
|
||||
* Archlinux
|
||||
* Raspbian Jessie
|
||||
* Amazon Linux 2016.03, 2016.09
|
||||
* Solus
|
||||
|
||||
To use a globally-installed version of libvips instead of the provided binaries,
|
||||
make sure it is at least the version listed under `config.libvips` in the `package.json` file
|
||||
@@ -46,10 +47,19 @@ This allows the use of newer versions of libvips with older versions of sharp.
|
||||
For 32-bit Intel CPUs and older Linux-based operating systems such as Centos 6,
|
||||
it is recommended to install a system-wide installation of libvips from source:
|
||||
|
||||
https://github.com/jcupitt/libvips#building-libvips-from-a-source-tarball
|
||||
https://jcupitt.github.io/libvips/install.html#building-libvips-from-a-source-tarball
|
||||
|
||||
For Linux-based operating systems such as Alpine that use musl libc,
|
||||
the smaller stack size means libvips' cache should be disabled
|
||||
#### Alpine Linux
|
||||
|
||||
libvips is available in the
|
||||
[testing repository](https://pkgs.alpinelinux.org/packages?name=vips-dev):
|
||||
|
||||
```sh
|
||||
apk add vips-dev fftw-dev --update-cache --repository https://dl-3.alpinelinux.org/alpine/edge/testing/
|
||||
```
|
||||
|
||||
The smaller stack size of musl libc means
|
||||
libvips may need to be used without a cache
|
||||
via `sharp.cache(false)` to avoid a stack overflow.
|
||||
|
||||
### Mac OS
|
||||
@@ -57,7 +67,7 @@ via `sharp.cache(false)` to avoid a stack overflow.
|
||||
[](https://travis-ci.org/lovell/sharp)
|
||||
|
||||
libvips and its dependencies are fetched and stored within `node_modules/sharp/vendor` during `npm install`.
|
||||
This involves an automated HTTPS download of approximately 6.3MB.
|
||||
This involves an automated HTTPS download of approximately 7MB.
|
||||
|
||||
To use your own version of libvips instead of the provided binaries, make sure it is
|
||||
at least the version listed under `config.libvips` in the `package.json` file and
|
||||
@@ -68,7 +78,7 @@ that it can be located using `pkg-config --modversion vips-cpp`.
|
||||
[](https://ci.appveyor.com/project/lovell/sharp)
|
||||
|
||||
libvips and its dependencies are fetched and stored within `node_modules\sharp\vendor` during `npm install`.
|
||||
This involves an automated HTTPS download of approximately 9MB.
|
||||
This involves an automated HTTPS download of approximately 11MB.
|
||||
|
||||
Only 64-bit (x64) `node.exe` is supported.
|
||||
|
||||
@@ -81,11 +91,16 @@ This can be achieved via [FreshPorts](https://www.freshports.org/graphics/vips/)
|
||||
cd /usr/ports/graphics/vips/ && make install clean
|
||||
```
|
||||
|
||||
FreeBSD's gcc v4 and v5 need `CXXFLAGS=-D_GLIBCXX_USE_C99` set for C++11 support due to
|
||||
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=193528
|
||||
|
||||
### Heroku
|
||||
|
||||
[Alessandro Tagliapietra](https://github.com/alex88) maintains an
|
||||
[Heroku buildpack for libvips](https://github.com/alex88/heroku-buildpack-vips)
|
||||
and its dependencies.
|
||||
libvips and its dependencies are fetched and stored within `node_modules\sharp\vendor` during `npm install`.
|
||||
This involves an automated HTTPS download of approximately 7MB.
|
||||
|
||||
Set [NODE_MODULES_CACHE](https://devcenter.heroku.com/articles/nodejs-support#cache-behavior)
|
||||
to `false` when using the `yarn` package manager.
|
||||
|
||||
### Docker
|
||||
|
||||
@@ -103,6 +118,13 @@ docker pull marcbachmann/libvips
|
||||
docker pull wjordan/libvips
|
||||
```
|
||||
|
||||
[Tailor Brands](https://github.com/TailorBrands) maintain
|
||||
[Debian-based Dockerfiles for libvips and nodejs](https://github.com/TailorBrands/docker-libvips).
|
||||
|
||||
```sh
|
||||
docker pull tailor/docker-libvips
|
||||
```
|
||||
|
||||
### AWS Lambda
|
||||
|
||||
In order to use sharp on AWS Lambda, you need to [create a deployment package](http://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html). Because sharp
|
||||
@@ -135,6 +157,10 @@ You can now download your deployment ZIP using `scp` and upload it to Lambda. Be
|
||||
* [gulp-responsive](https://www.npmjs.com/package/gulp-responsive)
|
||||
* [grunt-sharp](https://www.npmjs.com/package/grunt-sharp)
|
||||
|
||||
### CLI tools
|
||||
|
||||
* [sharp-cli](https://www.npmjs.com/package/sharp-cli)
|
||||
|
||||
### Security
|
||||
|
||||
Many users of this module process untrusted, user-supplied images,
|
||||
@@ -170,16 +196,27 @@ configuration file to prevent the use of coders known to be vulnerable.
|
||||
Set the `MAGICK_CONFIGURE_PATH` environment variable
|
||||
to the directory containing the `policy.xml` file.
|
||||
|
||||
### Licences
|
||||
### Pre-compiled libvips binaries
|
||||
|
||||
If a global installation of libvips that meets the
|
||||
minimum version requirement cannot be found,
|
||||
this module will download a pre-compiled bundle of libvips
|
||||
this module will attempt to download a pre-compiled bundle of libvips
|
||||
and its dependencies on Linux and Windows machines.
|
||||
|
||||
Should you need to manually download and inspect these files,
|
||||
you can do so via https://dl.bintray.com/lovell/sharp/
|
||||
|
||||
Should you wish to install these from your own location,
|
||||
set the `SHARP_DIST_BASE_URL` environment variable, e.g.
|
||||
|
||||
```sh
|
||||
SHARP_DIST_BASE_URL="https://hostname/path/" npm install sharp
|
||||
```
|
||||
|
||||
to use `https://hostname/path/libvips-x.y.z-platform.tar.gz`.
|
||||
|
||||
### Licences
|
||||
|
||||
This module is licensed under the terms of the
|
||||
[Apache 2.0 Licence](https://github.com/lovell/sharp/blob/master/LICENSE).
|
||||
|
||||
@@ -193,6 +230,7 @@ Use of libraries under the terms of the LGPLv3 is via the
|
||||
| Library | Used under the terms of |
|
||||
|---------------|----------------------------------------------------------------------------------------------------------|
|
||||
| cairo | Mozilla Public License 2.0 |
|
||||
| expat | MIT Licence |
|
||||
| fontconfig | [fontconfig Licence](https://cgit.freedesktop.org/fontconfig/tree/COPYING) (BSD-like) |
|
||||
| freetype | [freetype Licence](http://git.savannah.gnu.org/cgit/freetype/freetype2.git/tree/docs/FTL.TXT) (BSD-like) |
|
||||
| giflib | MIT Licence |
|
||||
|
||||
@@ -27,7 +27,7 @@ const bool = {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid channel
|
||||
*/
|
||||
const extractChannel = function extractChannel (channel) {
|
||||
function extractChannel (channel) {
|
||||
if (channel === 'red') {
|
||||
channel = 0;
|
||||
} else if (channel === 'green') {
|
||||
@@ -41,7 +41,7 @@ const extractChannel = function extractChannel (channel) {
|
||||
throw new Error('Cannot extract invalid channel ' + channel);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Join one or more channels to the image.
|
||||
@@ -59,7 +59,7 @@ const extractChannel = function extractChannel (channel) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const joinChannel = function joinChannel (images, options) {
|
||||
function joinChannel (images, options) {
|
||||
if (Array.isArray(images)) {
|
||||
images.forEach(function (image) {
|
||||
this.options.joinChannelIn.push(this._createInputDescriptor(image, options));
|
||||
@@ -68,7 +68,7 @@ const joinChannel = function joinChannel (images, options) {
|
||||
this.options.joinChannelIn.push(this._createInputDescriptor(images, options));
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a bitwise boolean operation on all input image channels (bands) to produce a single channel output image.
|
||||
@@ -86,14 +86,14 @@ const joinChannel = function joinChannel (images, options) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const bandbool = function bandbool (boolOp) {
|
||||
function bandbool (boolOp) {
|
||||
if (is.string(boolOp) && is.inArray(boolOp, ['and', 'or', 'eor'])) {
|
||||
this.options.bandBoolOp = boolOp;
|
||||
} else {
|
||||
throw new Error('Invalid bandbool operation ' + boolOp);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorate the Sharp prototype with channel-related functions.
|
||||
|
||||
@@ -27,7 +27,7 @@ const colourspace = {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameter
|
||||
*/
|
||||
const background = function background (rgba) {
|
||||
function background (rgba) {
|
||||
const colour = color(rgba);
|
||||
this.options.background = [
|
||||
colour.red(),
|
||||
@@ -36,7 +36,7 @@ const background = function background (rgba) {
|
||||
Math.round(colour.alpha() * 255)
|
||||
];
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert to 8-bit greyscale; 256 shades of grey.
|
||||
@@ -48,19 +48,19 @@ const background = function background (rgba) {
|
||||
* @param {Boolean} [greyscale=true]
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const greyscale = function greyscale (greyscale) {
|
||||
function greyscale (greyscale) {
|
||||
this.options.greyscale = is.bool(greyscale) ? greyscale : true;
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Alternative spelling of `greyscale`.
|
||||
* @param {Boolean} [grayscale=true]
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const grayscale = function grayscale (grayscale) {
|
||||
function grayscale (grayscale) {
|
||||
return this.greyscale(grayscale);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the output colourspace.
|
||||
@@ -69,13 +69,13 @@ const grayscale = function grayscale (grayscale) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const toColourspace = function toColourspace (colourspace) {
|
||||
function toColourspace (colourspace) {
|
||||
if (!is.string(colourspace)) {
|
||||
throw new Error('Invalid output colourspace ' + colourspace);
|
||||
}
|
||||
this.options.colourspace = colourspace;
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Alternative spelling of `toColourspace`.
|
||||
@@ -83,9 +83,9 @@ const toColourspace = function toColourspace (colourspace) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const toColorspace = function toColorspace (colorspace) {
|
||||
function toColorspace (colorspace) {
|
||||
return this.toColourspace(colorspace);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorate the Sharp prototype with colour-related functions.
|
||||
|
||||
@@ -8,6 +8,8 @@ const is = require('./is');
|
||||
* The overlay image must be the same size or smaller than the processed image.
|
||||
* If both `top` and `left` options are provided, they take precedence over `gravity`.
|
||||
*
|
||||
* If the overlay image contains an alpha channel then composition with premultiplication will occur.
|
||||
*
|
||||
* @example
|
||||
* sharp('input.png')
|
||||
* .rotate(180)
|
||||
@@ -33,14 +35,20 @@ const is = require('./is');
|
||||
* @param {Number} [options.left] - the pixel offset from the left edge.
|
||||
* @param {Boolean} [options.tile=false] - set to true to repeat the overlay image across the entire image with the given `gravity`.
|
||||
* @param {Boolean} [options.cutout=false] - set to true to apply only the alpha channel of the overlay image to the input image, giving the appearance of one image being cut out of another.
|
||||
* @param {Number} [options.density=72] - integral number representing the DPI for vector overlay image.
|
||||
* @param {Object} [options.raw] - describes overlay when using raw pixel data.
|
||||
* @param {Number} [options.raw.width]
|
||||
* @param {Number} [options.raw.height]
|
||||
* @param {Number} [options.raw.channels]
|
||||
* @param {Object} [options.create] - describes a blank overlay to be created.
|
||||
* @param {Number} [options.create.width]
|
||||
* @param {Number} [options.create.height]
|
||||
* @param {Number} [options.create.channels] - 3-4
|
||||
* @param {String|Object} [options.create.background] - 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
|
||||
*/
|
||||
const overlayWith = function overlayWith (overlay, options) {
|
||||
function overlayWith (overlay, options) {
|
||||
this.options.overlay = this._createInputDescriptor(overlay, options, {
|
||||
allowStream: false
|
||||
});
|
||||
@@ -60,10 +68,7 @@ const overlayWith = function overlayWith (overlay, options) {
|
||||
}
|
||||
}
|
||||
if (is.defined(options.left) || is.defined(options.top)) {
|
||||
if (
|
||||
is.integer(options.left) && is.inRange(options.left, 0, this.constructor.maximum.width) &&
|
||||
is.integer(options.top) && is.inRange(options.top, 0, this.constructor.maximum.height)
|
||||
) {
|
||||
if (is.integer(options.left) && options.left >= 0 && is.integer(options.top) && options.top >= 0) {
|
||||
this.options.overlayXOffset = options.left;
|
||||
this.options.overlayYOffset = options.top;
|
||||
} else {
|
||||
@@ -81,7 +86,7 @@ const overlayWith = function overlayWith (overlay, options) {
|
||||
}
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorate the Sharp prototype with composite-related functions.
|
||||
|
||||
@@ -5,6 +5,7 @@ const util = require('util');
|
||||
const stream = require('stream');
|
||||
const events = require('events');
|
||||
const semver = require('semver');
|
||||
const is = require('./is');
|
||||
const sharp = require('../build/Release/sharp.node');
|
||||
|
||||
// Versioning
|
||||
@@ -24,12 +25,15 @@ let versions = {
|
||||
} catch (err) {}
|
||||
})();
|
||||
|
||||
// Use NODE_DEBUG=sharp to enable libvips warnings
|
||||
const debuglog = util.debuglog('sharp');
|
||||
|
||||
/**
|
||||
* @class Sharp
|
||||
*
|
||||
* Constructor factory to create an instance of `sharp`, to which further methods are chained.
|
||||
*
|
||||
* JPEG, PNG or WebP format image data can be streamed out from this object.
|
||||
* JPEG, PNG, WebP or TIFF format image data can be streamed out from this object.
|
||||
* When using Stream based output, derived attributes are available from the `info` event.
|
||||
*
|
||||
* Implements the [stream.Duplex](http://nodejs.org/api/stream.html#stream_class_stream_duplex) class.
|
||||
@@ -54,20 +58,42 @@ let versions = {
|
||||
* });
|
||||
* readableStream.pipe(transformer).pipe(writableStream);
|
||||
*
|
||||
* @example
|
||||
* // Create a blank 300x200 PNG image of semi-transluent red pixels
|
||||
* sharp({
|
||||
* create: {
|
||||
* width: 300,
|
||||
* height: 200,
|
||||
* channels: 4,
|
||||
* background: { r: 255, g: 0, b: 0, alpha: 128 }
|
||||
* }
|
||||
* })
|
||||
* .png()
|
||||
* .toBuffer()
|
||||
* .then( ... );
|
||||
*
|
||||
* @param {(Buffer|String)} [input] - if present, can be
|
||||
* a Buffer containing JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data, or
|
||||
* a String containing the path to an JPEG, PNG, WebP, GIF, SVG or TIFF image file.
|
||||
* JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when null or undefined.
|
||||
* JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when not present.
|
||||
* @param {Object} [options] - if present, is an Object with optional attributes.
|
||||
* @param {Number} [options.density=72] - integral number representing the DPI for vector images.
|
||||
* @param {Object} [options.raw] - describes raw pixel image data. See `raw()` for pixel ordering.
|
||||
* @param {Object} [options.raw] - describes raw pixel input image data. See `raw()` for pixel ordering.
|
||||
* @param {Number} [options.raw.width]
|
||||
* @param {Number} [options.raw.height]
|
||||
* @param {Number} [options.raw.channels]
|
||||
* @param {Number} [options.raw.channels] - 1-4
|
||||
* @param {Object} [options.create] - describes a new image to be created.
|
||||
* @param {Number} [options.create.width]
|
||||
* @param {Number} [options.create.height]
|
||||
* @param {Number} [options.create.channels] - 3-4
|
||||
* @param {String|Object} [options.create.background] - 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
|
||||
*/
|
||||
const Sharp = function (input, options) {
|
||||
if (arguments.length === 1 && !is.defined(input)) {
|
||||
throw new Error('Invalid input');
|
||||
}
|
||||
if (!(this instanceof Sharp)) {
|
||||
return new Sharp(input, options);
|
||||
}
|
||||
@@ -75,7 +101,7 @@ const Sharp = function (input, options) {
|
||||
this.options = {
|
||||
// input options
|
||||
sequentialRead: false,
|
||||
limitInputPixels: maximum.pixels,
|
||||
limitInputPixels: Math.pow(0x3FFF, 2),
|
||||
// ICC profiles
|
||||
iccProfilePath: path.join(__dirname, 'icc') + path.sep,
|
||||
// resize options
|
||||
@@ -91,6 +117,7 @@ const Sharp = function (input, options) {
|
||||
height: -1,
|
||||
canvas: 'crop',
|
||||
crop: 0,
|
||||
useExifOrientation: false,
|
||||
angle: 0,
|
||||
rotateBeforePreExtract: false,
|
||||
flip: false,
|
||||
@@ -134,6 +161,7 @@ const Sharp = function (input, options) {
|
||||
streamOut: false,
|
||||
withMetadata: false,
|
||||
withMetadataOrientation: -1,
|
||||
resolveWithObject: false,
|
||||
// output format
|
||||
jpegQuality: 80,
|
||||
jpegProgressive: false,
|
||||
@@ -145,9 +173,19 @@ const Sharp = function (input, options) {
|
||||
pngCompressionLevel: 6,
|
||||
pngAdaptiveFiltering: true,
|
||||
webpQuality: 80,
|
||||
webpAlphaQuality: 100,
|
||||
webpLossless: false,
|
||||
webpNearLossless: false,
|
||||
tiffQuality: 80,
|
||||
tiffCompression: 'jpeg',
|
||||
tiffPredictor: 'none',
|
||||
tiffSquash: false,
|
||||
tiffXres: 1.0,
|
||||
tiffYres: 1.0,
|
||||
tileSize: 256,
|
||||
tileOverlap: 0,
|
||||
// Function to notify of libvips warnings
|
||||
debuglog: debuglog,
|
||||
// Function to notify of queue length changes
|
||||
queueListener: function (queueLength) {
|
||||
queue.emit('change', queueLength);
|
||||
@@ -158,18 +196,6 @@ const Sharp = function (input, options) {
|
||||
};
|
||||
util.inherits(Sharp, stream.Duplex);
|
||||
|
||||
/**
|
||||
* Pixel limits.
|
||||
* @member
|
||||
* @private
|
||||
*/
|
||||
const maximum = {
|
||||
width: 0x3FFF,
|
||||
height: 0x3FFF,
|
||||
pixels: Math.pow(0x3FFF, 2)
|
||||
};
|
||||
Sharp.maximum = maximum;
|
||||
|
||||
/**
|
||||
* An EventEmitter that emits a `change` event when a task is either:
|
||||
* - queued, waiting for _libuv_ to provide a worker thread
|
||||
@@ -186,7 +212,7 @@ Sharp.queue = queue;
|
||||
/**
|
||||
* An Object containing nested boolean values representing the available input and output formats/methods.
|
||||
* @example
|
||||
* console.log(sharp.format());
|
||||
* console.log(sharp.format);
|
||||
* @returns {Object}
|
||||
*/
|
||||
Sharp.format = sharp.format();
|
||||
|
||||
82
lib/input.js
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const util = require('util');
|
||||
const color = require('color');
|
||||
const is = require('./is');
|
||||
const sharp = require('../build/Release/sharp.node');
|
||||
|
||||
@@ -8,7 +8,7 @@ const sharp = require('../build/Release/sharp.node');
|
||||
* Create Object containing input and input-related options.
|
||||
* @private
|
||||
*/
|
||||
const _createInputDescriptor = function _createInputDescriptor (input, inputOptions, containerOptions) {
|
||||
function _createInputDescriptor (input, inputOptions, containerOptions) {
|
||||
const inputDescriptor = {};
|
||||
if (is.string(input)) {
|
||||
// filesystem
|
||||
@@ -16,6 +16,9 @@ const _createInputDescriptor = function _createInputDescriptor (input, inputOpti
|
||||
} else if (is.buffer(input)) {
|
||||
// Buffer
|
||||
inputDescriptor.buffer = input;
|
||||
} else if (is.plainObject(input) && !is.defined(inputOptions)) {
|
||||
// Plain Object descriptor, e.g. create
|
||||
inputOptions = input;
|
||||
} else if (!is.defined(input) && is.object(containerOptions) && containerOptions.allowStream) {
|
||||
// Stream
|
||||
inputDescriptor.buffer = [];
|
||||
@@ -35,8 +38,8 @@ const _createInputDescriptor = function _createInputDescriptor (input, inputOpti
|
||||
if (is.defined(inputOptions.raw)) {
|
||||
if (
|
||||
is.object(inputOptions.raw) &&
|
||||
is.integer(inputOptions.raw.width) && is.inRange(inputOptions.raw.width, 1, this.constructor.maximum.width) &&
|
||||
is.integer(inputOptions.raw.height) && is.inRange(inputOptions.raw.height, 1, this.constructor.maximum.height) &&
|
||||
is.integer(inputOptions.raw.width) && inputOptions.raw.width > 0 &&
|
||||
is.integer(inputOptions.raw.height) && inputOptions.raw.height > 0 &&
|
||||
is.integer(inputOptions.raw.channels) && is.inRange(inputOptions.raw.channels, 1, 4)
|
||||
) {
|
||||
inputDescriptor.rawWidth = inputOptions.raw.width;
|
||||
@@ -46,11 +49,35 @@ const _createInputDescriptor = function _createInputDescriptor (input, inputOpti
|
||||
throw new Error('Expected width, height and channels for raw pixel input');
|
||||
}
|
||||
}
|
||||
// Create new image
|
||||
if (is.defined(inputOptions.create)) {
|
||||
if (
|
||||
is.object(inputOptions.create) &&
|
||||
is.integer(inputOptions.create.width) && inputOptions.create.width > 0 &&
|
||||
is.integer(inputOptions.create.height) && inputOptions.create.height > 0 &&
|
||||
is.integer(inputOptions.create.channels) && is.inRange(inputOptions.create.channels, 3, 4) &&
|
||||
is.defined(inputOptions.create.background)
|
||||
) {
|
||||
inputDescriptor.createWidth = inputOptions.create.width;
|
||||
inputDescriptor.createHeight = inputOptions.create.height;
|
||||
inputDescriptor.createChannels = inputOptions.create.channels;
|
||||
const background = color(inputOptions.create.background);
|
||||
inputDescriptor.createBackground = [
|
||||
background.red(),
|
||||
background.green(),
|
||||
background.blue(),
|
||||
Math.round(background.alpha() * 255)
|
||||
];
|
||||
delete inputDescriptor.buffer;
|
||||
} else {
|
||||
throw new Error('Expected width, height, channels and background to create a new input image');
|
||||
}
|
||||
}
|
||||
} else if (is.defined(inputOptions)) {
|
||||
throw new Error('Invalid input options ' + inputOptions);
|
||||
}
|
||||
return inputDescriptor;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle incoming Buffer chunk on Writable Stream.
|
||||
@@ -59,11 +86,17 @@ const _createInputDescriptor = function _createInputDescriptor (input, inputOpti
|
||||
* @param {String} encoding - unused
|
||||
* @param {Function} callback
|
||||
*/
|
||||
const _write = function _write (chunk, encoding, callback) {
|
||||
function _write (chunk, encoding, callback) {
|
||||
/* istanbul ignore else */
|
||||
if (Array.isArray(this.options.input.buffer)) {
|
||||
/* istanbul ignore else */
|
||||
if (is.buffer(chunk)) {
|
||||
if (this.options.input.buffer.length === 0) {
|
||||
const that = this;
|
||||
this.on('finish', function () {
|
||||
that.streamInFinished = true;
|
||||
});
|
||||
}
|
||||
this.options.input.buffer.push(chunk);
|
||||
callback();
|
||||
} else {
|
||||
@@ -72,26 +105,26 @@ const _write = function _write (chunk, encoding, callback) {
|
||||
} else {
|
||||
callback(new Error('Unexpected data on Writable Stream'));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Flattens the array of chunks accumulated in input.buffer.
|
||||
* @private
|
||||
*/
|
||||
const _flattenBufferIn = function _flattenBufferIn () {
|
||||
function _flattenBufferIn () {
|
||||
if (this._isStreamInput()) {
|
||||
this.options.input.buffer = Buffer.concat(this.options.input.buffer);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Are we expecting Stream-based input?
|
||||
* @private
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
const _isStreamInput = function _isStreamInput () {
|
||||
function _isStreamInput () {
|
||||
return Array.isArray(this.options.input.buffer);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Take a "snapshot" of the Sharp instance, returning a new instance.
|
||||
@@ -108,11 +141,11 @@ const _isStreamInput = function _isStreamInput () {
|
||||
*
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const clone = function clone () {
|
||||
function clone () {
|
||||
const that = this;
|
||||
// Clone existing options
|
||||
const clone = this.constructor.call();
|
||||
util._extend(clone.options, this.options);
|
||||
clone.options = Object.assign({}, this.options);
|
||||
// Pass 'finish' event to clone for Stream-based input
|
||||
this.on('finish', function () {
|
||||
// Clone inherits input data
|
||||
@@ -121,17 +154,18 @@ const clone = function clone () {
|
||||
clone.emit('finish');
|
||||
});
|
||||
return clone;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Fast access to image metadata without decoding any compressed image data.
|
||||
* Fast access to (uncached) image metadata without decoding any compressed image data.
|
||||
* A Promises/A+ promise is returned when `callback` is not provided.
|
||||
*
|
||||
* - `format`: Name of decoder used to decompress image data e.g. `jpeg`, `png`, `webp`, `gif`, `svg`
|
||||
* - `width`: Number of pixels wide
|
||||
* - `height`: Number of pixels high
|
||||
* - `space`: Name of colour space interpretation e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://github.com/jcupitt/libvips/blob/master/libvips/iofuncs/enumtypes.c#L568)
|
||||
* - `space`: Name of colour space interpretation e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://github.com/jcupitt/libvips/blob/master/libvips/iofuncs/enumtypes.c#L636)
|
||||
* - `channels`: Number of bands e.g. `3` for sRGB, `4` for CMYK
|
||||
* - `depth`: Name of pixel depth format e.g. `uchar`, `char`, `ushort`, `float` [...](https://github.com/jcupitt/libvips/blob/master/libvips/iofuncs/enumtypes.c#L672)
|
||||
* - `density`: Number of pixels per inch (DPI), if present
|
||||
* - `hasProfile`: Boolean indicating the presence of an embedded ICC profile
|
||||
* - `hasAlpha`: Boolean indicating the presence of an alpha transparency channel
|
||||
@@ -156,7 +190,7 @@ const clone = function clone () {
|
||||
* @param {Function} [callback] - called with the arguments `(err, metadata)`
|
||||
* @returns {Promise<Object>|Sharp}
|
||||
*/
|
||||
const metadata = function metadata (callback) {
|
||||
function metadata (callback) {
|
||||
const that = this;
|
||||
if (is.fn(callback)) {
|
||||
if (this._isStreamInput()) {
|
||||
@@ -194,7 +228,7 @@ const metadata = function metadata (callback) {
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Do not process input images where the number of pixels (width * height) exceeds this limit.
|
||||
@@ -204,20 +238,20 @@ const metadata = function metadata (callback) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid limit
|
||||
*/
|
||||
const limitInputPixels = function limitInputPixels (limit) {
|
||||
function limitInputPixels (limit) {
|
||||
// if we pass in false we represent the integer as 0 to disable
|
||||
if (limit === false) {
|
||||
limit = 0;
|
||||
} else if (limit === true) {
|
||||
limit = this.constructor.maximum.pixels;
|
||||
limit = Math.pow(0x3FFF, 2);
|
||||
}
|
||||
if (is.integer(limit) && limit >= 0) {
|
||||
this.options.limitInputPixels = limit;
|
||||
} else {
|
||||
throw new Error('Invalid pixel limit (0 to ' + this.constructor.maximum.pixels + ') ' + limit);
|
||||
throw is.invalidParameterError('limitInputPixels', 'integer', limit);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* An advanced setting that switches the libvips access method to `VIPS_ACCESS_SEQUENTIAL`.
|
||||
@@ -225,10 +259,10 @@ const limitInputPixels = function limitInputPixels (limit) {
|
||||
* @param {Boolean} [sequentialRead=true]
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const sequentialRead = function sequentialRead (sequentialRead) {
|
||||
function sequentialRead (sequentialRead) {
|
||||
this.options.sequentialRead = is.bool(sequentialRead) ? sequentialRead : true;
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorate the Sharp prototype with input-related functions.
|
||||
|
||||
27
lib/is.js
@@ -16,6 +16,14 @@ const object = function (val) {
|
||||
return typeof val === 'object';
|
||||
};
|
||||
|
||||
/**
|
||||
* Is this value a plain object?
|
||||
* @private
|
||||
*/
|
||||
const plainObject = function (val) {
|
||||
return object(val) && Object.prototype.toString.call(val) === '[object Object]';
|
||||
};
|
||||
|
||||
/**
|
||||
* Is this value a function?
|
||||
* @private
|
||||
@@ -80,9 +88,25 @@ const inArray = function (val, list) {
|
||||
return list.indexOf(val) !== -1;
|
||||
};
|
||||
|
||||
/**
|
||||
* Create an Error with a message relating to an invalid parameter.
|
||||
*
|
||||
* @param {String} name - parameter name.
|
||||
* @param {String} expected - description of the type/value/range expected.
|
||||
* @param {*} actual - the value received.
|
||||
* @returns {Error} Containing the formatted message.
|
||||
* @private
|
||||
*/
|
||||
const invalidParameterError = function (name, expected, actual) {
|
||||
return new Error(
|
||||
`Expected ${expected} for ${name} but received ${actual} of type ${typeof actual}`
|
||||
);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
defined: defined,
|
||||
object: object,
|
||||
plainObject: plainObject,
|
||||
fn: fn,
|
||||
bool: bool,
|
||||
buffer: buffer,
|
||||
@@ -90,5 +114,6 @@ module.exports = {
|
||||
number: number,
|
||||
integer: integer,
|
||||
inRange: inRange,
|
||||
inArray: inArray
|
||||
inArray: inArray,
|
||||
invalidParameterError: invalidParameterError
|
||||
};
|
||||
|
||||
@@ -6,7 +6,10 @@ const is = require('./is');
|
||||
* Rotate the output image by either an explicit angle
|
||||
* or auto-orient based on the EXIF `Orientation` tag.
|
||||
*
|
||||
* Use this method without angle to determine the angle from EXIF data.
|
||||
* If an angle is provided, it is converted to a valid 90/180/270deg rotation.
|
||||
* For example, `-450` will produce a 270deg rotation.
|
||||
*
|
||||
* If no angle is provided, it is determined from the EXIF data.
|
||||
* Mirroring is supported and may infer the use of a flip operation.
|
||||
*
|
||||
* The use of `rotate` implies the removal of the EXIF `Orientation` tag, if any.
|
||||
@@ -25,20 +28,20 @@ const is = require('./is');
|
||||
* });
|
||||
* readableStream.pipe(pipeline);
|
||||
*
|
||||
* @param {Number} [angle=auto] 0, 90, 180 or 270.
|
||||
* @param {Number} [angle=auto] angle of rotation, must be a multiple of 90.
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const rotate = function rotate (angle) {
|
||||
function rotate (angle) {
|
||||
if (!is.defined(angle)) {
|
||||
this.options.angle = -1;
|
||||
} else if (is.integer(angle) && is.inArray(angle, [0, 90, 180, 270])) {
|
||||
this.options.useExifOrientation = true;
|
||||
} else if (is.integer(angle) && !(angle % 90)) {
|
||||
this.options.angle = angle;
|
||||
} else {
|
||||
throw new Error('Unsupported angle (0, 90, 180, 270) ' + angle);
|
||||
throw new Error('Unsupported angle: angle must be a positive/negative multiple of 90 ' + angle);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a region of the image.
|
||||
@@ -70,7 +73,7 @@ const rotate = function rotate (angle) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const extract = function extract (options) {
|
||||
function extract (options) {
|
||||
const suffix = this.options.width === -1 && this.options.height === -1 ? 'Pre' : 'Post';
|
||||
['left', 'top', 'width', 'height'].forEach(function (name) {
|
||||
const value = options[name];
|
||||
@@ -81,11 +84,11 @@ const extract = function extract (options) {
|
||||
}
|
||||
}, this);
|
||||
// Ensure existing rotation occurs before pre-resize extraction
|
||||
if (suffix === 'Pre' && this.options.angle !== 0) {
|
||||
if (suffix === 'Pre' && ((this.options.angle % 360) !== 0 || this.options.useExifOrientation === true)) {
|
||||
this.options.rotateBeforePreExtract = true;
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Flip the image about the vertical Y axis. This always occurs after rotation, if any.
|
||||
@@ -93,10 +96,10 @@ const extract = function extract (options) {
|
||||
* @param {Boolean} [flip=true]
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const flip = function flip (flip) {
|
||||
function flip (flip) {
|
||||
this.options.flip = is.bool(flip) ? flip : true;
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Flop the image about the horizontal X axis. This always occurs after rotation, if any.
|
||||
@@ -104,10 +107,10 @@ const flip = function flip (flip) {
|
||||
* @param {Boolean} [flop=true]
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const flop = function flop (flop) {
|
||||
function flop (flop) {
|
||||
this.options.flop = is.bool(flop) ? flop : true;
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Sharpen the image.
|
||||
@@ -121,7 +124,7 @@ const flop = function flop (flop) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const sharpen = function sharpen (sigma, flat, jagged) {
|
||||
function sharpen (sigma, flat, jagged) {
|
||||
if (!is.defined(sigma)) {
|
||||
// No arguments: default to mild sharpen
|
||||
this.options.sharpenSigma = -1;
|
||||
@@ -151,7 +154,7 @@ const sharpen = function sharpen (sigma, flat, jagged) {
|
||||
throw new Error('Invalid sharpen sigma (0.01 - 10000) ' + sigma);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Blur the image.
|
||||
@@ -161,7 +164,7 @@ const sharpen = function sharpen (sigma, flat, jagged) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const blur = function blur (sigma) {
|
||||
function blur (sigma) {
|
||||
if (!is.defined(sigma)) {
|
||||
// No arguments: default to mild blur
|
||||
this.options.blurSigma = -1;
|
||||
@@ -175,7 +178,7 @@ const blur = function blur (sigma) {
|
||||
throw new Error('Invalid blur sigma (0.3 - 1000.0) ' + sigma);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Extends/pads the edges of the image with the colour provided to the `background` method.
|
||||
@@ -186,7 +189,7 @@ const blur = function blur (sigma) {
|
||||
* // to the top, left and right edges and 20 to the bottom edge
|
||||
* sharp(input)
|
||||
* .resize(140)
|
||||
* .background({r: 0, g: 0, b: 0, a: 0})
|
||||
* .background({r: 0, g: 0, b: 0, alpha: 0})
|
||||
* .extend({top: 10, bottom: 20, left: 10, right: 10})
|
||||
* ...
|
||||
*
|
||||
@@ -198,7 +201,7 @@ const blur = function blur (sigma) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const extend = function extend (extend) {
|
||||
function extend (extend) {
|
||||
if (is.integer(extend) && extend > 0) {
|
||||
this.options.extendTop = extend;
|
||||
this.options.extendBottom = extend;
|
||||
@@ -219,17 +222,17 @@ const extend = function extend (extend) {
|
||||
throw new Error('Invalid edge extension ' + extend);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge alpha transparency channel, if any, with `background`.
|
||||
* @param {Boolean} [flatten=true]
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const flatten = function flatten (flatten) {
|
||||
function flatten (flatten) {
|
||||
this.options.flatten = is.bool(flatten) ? flatten : true;
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Trim "boring" pixels from all edges that contain values within a percentage similarity of the top-left pixel.
|
||||
@@ -237,7 +240,7 @@ const flatten = function flatten (flatten) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const trim = function trim (tolerance) {
|
||||
function trim (tolerance) {
|
||||
if (!is.defined(tolerance)) {
|
||||
this.options.trimTolerance = 10;
|
||||
} else if (is.integer(tolerance) && is.inRange(tolerance, 1, 99)) {
|
||||
@@ -246,7 +249,7 @@ const trim = function trim (tolerance) {
|
||||
throw new Error('Invalid trim tolerance (1 to 99) ' + tolerance);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a gamma correction by reducing the encoding (darken) pre-resize at a factor of `1/gamma`
|
||||
@@ -258,7 +261,7 @@ const trim = function trim (tolerance) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const gamma = function gamma (gamma) {
|
||||
function gamma (gamma) {
|
||||
if (!is.defined(gamma)) {
|
||||
// Default gamma correction of 2.2 (sRGB)
|
||||
this.options.gamma = 2.2;
|
||||
@@ -268,36 +271,36 @@ const gamma = function gamma (gamma) {
|
||||
throw new Error('Invalid gamma correction (1.0 to 3.0) ' + gamma);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce the "negative" of the image.
|
||||
* @param {Boolean} [negate=true]
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const negate = function negate (negate) {
|
||||
function negate (negate) {
|
||||
this.options.negate = is.bool(negate) ? negate : true;
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Enhance output image contrast by stretching its luminance to cover the full dynamic range.
|
||||
* @param {Boolean} [normalise=true]
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const normalise = function normalise (normalise) {
|
||||
function normalise (normalise) {
|
||||
this.options.normalise = is.bool(normalise) ? normalise : true;
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Alternative spelling of normalise.
|
||||
* @param {Boolean} [normalize=true]
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const normalize = function normalize (normalize) {
|
||||
function normalize (normalize) {
|
||||
return this.normalise(normalize);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convolve the image with the specified kernel.
|
||||
@@ -324,7 +327,7 @@ const normalize = function normalize (normalize) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const convolve = function convolve (kernel) {
|
||||
function convolve (kernel) {
|
||||
if (!is.object(kernel) || !Array.isArray(kernel.kernel) ||
|
||||
!is.integer(kernel.width) || !is.integer(kernel.height) ||
|
||||
!is.inRange(kernel.width, 3, 1001) || !is.inRange(kernel.height, 3, 1001) ||
|
||||
@@ -348,7 +351,7 @@ const convolve = function convolve (kernel) {
|
||||
}
|
||||
this.options.convKernel = kernel;
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Any pixel value greather than or equal to the threshold value will be set to 255, otherwise it will be set to 0.
|
||||
@@ -359,7 +362,7 @@ const convolve = function convolve (kernel) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const threshold = function threshold (threshold, options) {
|
||||
function threshold (threshold, options) {
|
||||
if (!is.defined(threshold)) {
|
||||
this.options.threshold = 128;
|
||||
} else if (is.bool(threshold)) {
|
||||
@@ -375,7 +378,7 @@ const threshold = function threshold (threshold, options) {
|
||||
this.options.thresholdGrayscale = false;
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a bitwise boolean operation with operand image.
|
||||
@@ -393,7 +396,7 @@ const threshold = function threshold (threshold, options) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const boolean = function boolean (operand, operator, options) {
|
||||
function boolean (operand, operator, options) {
|
||||
this.options.boolean = this._createInputDescriptor(operand, options);
|
||||
if (is.string(operator) && is.inArray(operator, ['and', 'or', 'eor'])) {
|
||||
this.options.booleanOp = operator;
|
||||
@@ -401,7 +404,7 @@ const boolean = function boolean (operand, operator, options) {
|
||||
throw new Error('Invalid boolean operator ' + operator);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorate the Sharp prototype with operation-related functions.
|
||||
|
||||
252
lib/output.js
@@ -1,6 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
const util = require('util');
|
||||
const is = require('./is');
|
||||
const sharp = require('../build/Release/sharp.node');
|
||||
|
||||
@@ -15,11 +14,12 @@ const sharp = require('../build/Release/sharp.node');
|
||||
*
|
||||
* @param {String} fileOut - the path to write the image data to.
|
||||
* @param {Function} [callback] - called on completion with two arguments `(err, info)`.
|
||||
* `info` contains the output image `format`, `size` (bytes), `width`, `height` and `channels`.
|
||||
* `info` contains the output image `format`, `size` (bytes), `width`, `height`,
|
||||
* `channels` and `premultiplied` (indicating if premultiplication was used).
|
||||
* @returns {Promise<Object>} - when no callback is provided
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const toFile = function toFile (fileOut, callback) {
|
||||
function toFile (fileOut, callback) {
|
||||
if (!fileOut || fileOut.length === 0) {
|
||||
const errOutputInvalid = new Error('Invalid output');
|
||||
if (is.fn(callback)) {
|
||||
@@ -41,25 +41,33 @@ const toFile = function toFile (fileOut, callback) {
|
||||
}
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Write output to a Buffer.
|
||||
* JPEG, PNG, WebP, and RAW output are supported.
|
||||
* JPEG, PNG, WebP, TIFF and RAW output are supported.
|
||||
* By default, the format will match the input image, except GIF and SVG input which become PNG output.
|
||||
*
|
||||
* `callback`, if present, gets three arguments `(err, buffer, info)` where:
|
||||
* - `err` is an error message, if any.
|
||||
* - `buffer` is the output image data.
|
||||
* - `info` contains the output image `format`, `size` (bytes), `width`, `height` and `channels`.
|
||||
* A Promises/A+ promise is returned when `callback` is not provided.
|
||||
* `callback`, if present, gets three arguments `(err, data, info)` where:
|
||||
* - `err` is an error, if any.
|
||||
* - `data` is the output image data.
|
||||
* - `info` contains the output image `format`, `size` (bytes), `width`, `height`,
|
||||
* `channels` and `premultiplied` (indicating if premultiplication was used).
|
||||
* A Promise is returned when `callback` is not provided.
|
||||
*
|
||||
* @param {Object} [options]
|
||||
* @param {Boolean} [options.resolveWithObject] Resolve the Promise with an Object containing `data` and `info` properties instead of resolving only with `data`.
|
||||
* @param {Function} [callback]
|
||||
* @returns {Promise<Buffer>} - when no callback is provided
|
||||
*/
|
||||
const toBuffer = function toBuffer (callback) {
|
||||
return this._pipeline(callback);
|
||||
};
|
||||
function toBuffer (options, callback) {
|
||||
if (is.object(options)) {
|
||||
if (is.bool(options.resolveWithObject)) {
|
||||
this.options.resolveWithObject = options.resolveWithObject;
|
||||
}
|
||||
}
|
||||
return this._pipeline(is.fn(options) ? options : callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Include all metadata (EXIF, XMP, IPTC) from the input image in the output image.
|
||||
@@ -70,7 +78,7 @@ const toBuffer = function toBuffer (callback) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const withMetadata = function withMetadata (withMetadata) {
|
||||
function withMetadata (withMetadata) {
|
||||
this.options.withMetadata = is.bool(withMetadata) ? withMetadata : true;
|
||||
if (is.object(withMetadata)) {
|
||||
if (is.defined(withMetadata.orientation)) {
|
||||
@@ -82,7 +90,7 @@ const withMetadata = function withMetadata (withMetadata) {
|
||||
}
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Use these JPEG options for output image.
|
||||
@@ -98,7 +106,7 @@ const withMetadata = function withMetadata (withMetadata) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid options
|
||||
*/
|
||||
const jpeg = function jpeg (options) {
|
||||
function jpeg (options) {
|
||||
if (is.object(options)) {
|
||||
if (is.defined(options.quality)) {
|
||||
if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
|
||||
@@ -133,7 +141,7 @@ const jpeg = function jpeg (options) {
|
||||
}
|
||||
}
|
||||
return this._updateFormatOut('jpeg', options);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Use these PNG options for output image.
|
||||
@@ -145,7 +153,7 @@ const jpeg = function jpeg (options) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid options
|
||||
*/
|
||||
const png = function png (options) {
|
||||
function png (options) {
|
||||
if (is.object(options)) {
|
||||
if (is.defined(options.progressive)) {
|
||||
this._setBooleanOption('pngProgressive', options.progressive);
|
||||
@@ -162,17 +170,20 @@ const png = function png (options) {
|
||||
}
|
||||
}
|
||||
return this._updateFormatOut('png', options);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Use these WebP options for output image.
|
||||
* @param {Object} [options] - output options
|
||||
* @param {Number} [options.quality=80] - quality, integer 1-100
|
||||
* @param {Number} [options.alphaQuality=100] - quality of alpha layer, integer 0-100
|
||||
* @param {Boolean} [options.lossless=false] - use lossless compression mode
|
||||
* @param {Boolean} [options.nearLossless=false] - use near_lossless compression mode
|
||||
* @param {Boolean} [options.force=true] - force WebP output, otherwise attempt to use input format
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid options
|
||||
*/
|
||||
const webp = function webp (options) {
|
||||
function webp (options) {
|
||||
if (is.object(options) && is.defined(options.quality)) {
|
||||
if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
|
||||
this.options.webpQuality = options.quality;
|
||||
@@ -180,18 +191,36 @@ const webp = function webp (options) {
|
||||
throw new Error('Invalid quality (integer, 1-100) ' + options.quality);
|
||||
}
|
||||
}
|
||||
if (is.object(options) && is.defined(options.alphaQuality)) {
|
||||
if (is.integer(options.alphaQuality) && is.inRange(options.alphaQuality, 1, 100)) {
|
||||
this.options.webpAlphaQuality = options.alphaQuality;
|
||||
} else {
|
||||
throw new Error('Invalid webp alpha quality (integer, 1-100) ' + options.alphaQuality);
|
||||
}
|
||||
}
|
||||
if (is.object(options) && is.defined(options.lossless)) {
|
||||
this._setBooleanOption('webpLossless', options.lossless);
|
||||
}
|
||||
if (is.object(options) && is.defined(options.nearLossless)) {
|
||||
this._setBooleanOption('webpNearLossless', options.nearLossless);
|
||||
}
|
||||
return this._updateFormatOut('webp', options);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Use these TIFF options for output image.
|
||||
* @param {Object} [options] - output options
|
||||
* @param {Number} [options.quality=80] - quality, integer 1-100
|
||||
* @param {Boolean} [options.force=true] - force TIFF output, otherwise attempt to use input format
|
||||
* @param {Boolean} [options.compression='jpeg'] - compression options: lzw, deflate, jpeg
|
||||
* @param {Boolean} [options.predictor='none'] - compression predictor options: none, horizontal, float
|
||||
* @param {Number} [options.xres=1.0] - horizontal resolution in pixels/mm
|
||||
* @param {Number} [options.yres=1.0] - vertical resolution in pixels/mm
|
||||
* @param {Boolean} [options.squash=false] - squash 8-bit images down to 1 bit
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid options
|
||||
*/
|
||||
const tiff = function tiff (options) {
|
||||
function tiff (options) {
|
||||
if (is.object(options) && is.defined(options.quality)) {
|
||||
if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
|
||||
this.options.tiffQuality = options.quality;
|
||||
@@ -199,16 +228,56 @@ const tiff = function tiff (options) {
|
||||
throw new Error('Invalid quality (integer, 1-100) ' + options.quality);
|
||||
}
|
||||
}
|
||||
if (is.object(options) && is.defined(options.squash)) {
|
||||
if (is.bool(options.squash)) {
|
||||
this.options.tiffSquash = options.squash;
|
||||
} else {
|
||||
throw new Error('Invalid Value for squash ' + options.squash + ' Only Boolean Values allowed for options.squash.');
|
||||
}
|
||||
}
|
||||
// resolution
|
||||
if (is.object(options) && is.defined(options.xres)) {
|
||||
if (is.number(options.xres)) {
|
||||
this.options.tiffXres = options.xres;
|
||||
} else {
|
||||
throw new Error('Invalid Value for xres ' + options.xres + ' Only numeric values allowed for options.xres');
|
||||
}
|
||||
}
|
||||
if (is.object(options) && is.defined(options.yres)) {
|
||||
if (is.number(options.yres)) {
|
||||
this.options.tiffYres = options.yres;
|
||||
} else {
|
||||
throw new Error('Invalid Value for yres ' + options.yres + ' Only numeric values allowed for options.yres');
|
||||
}
|
||||
}
|
||||
// compression
|
||||
if (is.defined(options) && is.defined(options.compression)) {
|
||||
if (is.string(options.compression) && is.inArray(options.compression, ['lzw', 'deflate', 'jpeg', 'none'])) {
|
||||
this.options.tiffCompression = options.compression;
|
||||
} else {
|
||||
const message = `Invalid compression option "${options.compression}". Should be one of: lzw, deflate, jpeg, none`;
|
||||
throw new Error(message);
|
||||
}
|
||||
}
|
||||
// predictor
|
||||
if (is.defined(options) && is.defined(options.predictor)) {
|
||||
if (is.string(options.predictor) && is.inArray(options.predictor, ['none', 'horizontal', 'float'])) {
|
||||
this.options.tiffPredictor = options.predictor;
|
||||
} else {
|
||||
const message = `Invalid predictor option "${options.predictor}". Should be one of: none, horizontal, float`;
|
||||
throw new Error(message);
|
||||
}
|
||||
}
|
||||
return this._updateFormatOut('tiff', options);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Force output to be raw, uncompressed uint8 pixel data.
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const raw = function raw () {
|
||||
function raw () {
|
||||
return this._updateFormatOut('raw');
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Force output to a given format.
|
||||
@@ -217,15 +286,16 @@ const raw = function raw () {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} unsupported format or options
|
||||
*/
|
||||
const toFormat = function toFormat (format, options) {
|
||||
function toFormat (format, options) {
|
||||
if (is.object(format) && is.string(format.id)) {
|
||||
format = format.id;
|
||||
}
|
||||
if (format === 'jpg') format = 'jpeg';
|
||||
if (!is.inArray(format, ['jpeg', 'png', 'webp', 'tiff', 'raw'])) {
|
||||
throw new Error('Unsupported output format ' + format);
|
||||
}
|
||||
return this[format](options);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Use tile-based deep zoom (image pyramid) output.
|
||||
@@ -251,7 +321,7 @@ const toFormat = function toFormat (format, options) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const tile = function tile (tile) {
|
||||
function tile (tile) {
|
||||
if (is.object(tile)) {
|
||||
// Size of square tiles, in pixels
|
||||
if (is.defined(tile.size)) {
|
||||
@@ -296,7 +366,7 @@ const tile = function tile (tile) {
|
||||
throw new Error('Invalid tile format ' + this.options.formatOut);
|
||||
}
|
||||
return this._updateFormatOut('dz');
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the output format unless options.force is false,
|
||||
@@ -307,10 +377,10 @@ const tile = function tile (tile) {
|
||||
* @param {Boolean} [options.force=true] - force output format, otherwise attempt to use input format
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const _updateFormatOut = function _updateFormatOut (formatOut, options) {
|
||||
function _updateFormatOut (formatOut, options) {
|
||||
this.options.formatOut = (is.object(options) && options.force === false) ? 'input' : formatOut;
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a Boolean attribute of the this.options Object.
|
||||
@@ -319,31 +389,31 @@ const _updateFormatOut = function _updateFormatOut (formatOut, options) {
|
||||
* @param {Boolean} val
|
||||
* @throws {Error} Invalid key
|
||||
*/
|
||||
const _setBooleanOption = function _setBooleanOption (key, val) {
|
||||
function _setBooleanOption (key, val) {
|
||||
if (is.bool(val)) {
|
||||
this.options[key] = val;
|
||||
} else {
|
||||
throw new Error('Invalid ' + key + ' (boolean) ' + val);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by a WriteableStream to notify us it is ready for data.
|
||||
* @private
|
||||
*/
|
||||
const _read = function _read () {
|
||||
function _read () {
|
||||
if (!this.options.streamOut) {
|
||||
this.options.streamOut = true;
|
||||
this._pipeline();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke the C++ image processing pipeline
|
||||
* Supports callback, stream and promise variants
|
||||
* @private
|
||||
*/
|
||||
const _pipeline = function _pipeline (callback) {
|
||||
function _pipeline (callback) {
|
||||
const that = this;
|
||||
if (typeof callback === 'function') {
|
||||
// output=file/buffer
|
||||
@@ -362,9 +432,9 @@ const _pipeline = function _pipeline (callback) {
|
||||
// output=stream
|
||||
if (this._isStreamInput()) {
|
||||
// output=stream, input=stream
|
||||
this.on('finish', function () {
|
||||
that._flattenBufferIn();
|
||||
sharp.pipeline(that.options, function (err, data, info) {
|
||||
if (this.streamInFinished) {
|
||||
this._flattenBufferIn();
|
||||
sharp.pipeline(this.options, function (err, data, info) {
|
||||
if (err) {
|
||||
that.emit('error', err);
|
||||
} else {
|
||||
@@ -373,7 +443,20 @@ const _pipeline = function _pipeline (callback) {
|
||||
}
|
||||
that.push(null);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
this.on('finish', function () {
|
||||
that._flattenBufferIn();
|
||||
sharp.pipeline(that.options, function (err, data, info) {
|
||||
if (err) {
|
||||
that.emit('error', err);
|
||||
} else {
|
||||
that.emit('info', info);
|
||||
that.push(data);
|
||||
}
|
||||
that.push(null);
|
||||
});
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// output=stream, input=file/buffer
|
||||
sharp.pipeline(this.options, function (err, data, info) {
|
||||
@@ -394,11 +477,15 @@ const _pipeline = function _pipeline (callback) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
that.on('finish', function () {
|
||||
that._flattenBufferIn();
|
||||
sharp.pipeline(that.options, function (err, data) {
|
||||
sharp.pipeline(that.options, function (err, data, info) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(data);
|
||||
if (that.options.resolveWithObject) {
|
||||
resolve({ data: data, info: info });
|
||||
} else {
|
||||
resolve(data);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -406,77 +493,21 @@ const _pipeline = function _pipeline (callback) {
|
||||
} else {
|
||||
// output=promise, input=file/buffer
|
||||
return new Promise(function (resolve, reject) {
|
||||
sharp.pipeline(that.options, function (err, data) {
|
||||
sharp.pipeline(that.options, function (err, data, info) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(data);
|
||||
if (that.options.resolveWithObject) {
|
||||
resolve({ data: data, info: info });
|
||||
} else {
|
||||
resolve(data);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Deprecated output options
|
||||
/* istanbul ignore next */
|
||||
const quality = util.deprecate(function (quality) {
|
||||
const formatOut = this.options.formatOut;
|
||||
const options = { quality: quality };
|
||||
this.jpeg(options).webp(options).tiff(options);
|
||||
this.options.formatOut = formatOut;
|
||||
return this;
|
||||
}, 'quality: use jpeg({ quality: ... }), webp({ quality: ... }) and/or tiff({ quality: ... }) instead');
|
||||
/* istanbul ignore next */
|
||||
const progressive = util.deprecate(function (progressive) {
|
||||
const formatOut = this.options.formatOut;
|
||||
const options = { progressive: (progressive !== false) };
|
||||
this.jpeg(options).png(options);
|
||||
this.options.formatOut = formatOut;
|
||||
return this;
|
||||
}, 'progressive: use jpeg({ progressive: ... }) and/or png({ progressive: ... }) instead');
|
||||
/* istanbul ignore next */
|
||||
const compressionLevel = util.deprecate(function (compressionLevel) {
|
||||
const formatOut = this.options.formatOut;
|
||||
this.png({ compressionLevel: compressionLevel });
|
||||
this.options.formatOut = formatOut;
|
||||
return this;
|
||||
}, 'compressionLevel: use png({ compressionLevel: ... }) instead');
|
||||
/* istanbul ignore next */
|
||||
const withoutAdaptiveFiltering = util.deprecate(function (withoutAdaptiveFiltering) {
|
||||
const formatOut = this.options.formatOut;
|
||||
this.png({ adaptiveFiltering: (withoutAdaptiveFiltering === false) });
|
||||
this.options.formatOut = formatOut;
|
||||
return this;
|
||||
}, 'withoutAdaptiveFiltering: use png({ adaptiveFiltering: ... }) instead');
|
||||
/* istanbul ignore next */
|
||||
const withoutChromaSubsampling = util.deprecate(function (withoutChromaSubsampling) {
|
||||
const formatOut = this.options.formatOut;
|
||||
this.jpeg({ chromaSubsampling: (withoutChromaSubsampling === false) ? '4:2:0' : '4:4:4' });
|
||||
this.options.formatOut = formatOut;
|
||||
return this;
|
||||
}, 'withoutChromaSubsampling: use jpeg({ chromaSubsampling: "4:4:4" }) instead');
|
||||
/* istanbul ignore next */
|
||||
const trellisQuantisation = util.deprecate(function (trellisQuantisation) {
|
||||
const formatOut = this.options.formatOut;
|
||||
this.jpeg({ trellisQuantisation: (trellisQuantisation !== false) });
|
||||
this.options.formatOut = formatOut;
|
||||
return this;
|
||||
}, 'trellisQuantisation: use jpeg({ trellisQuantisation: ... }) instead');
|
||||
/* istanbul ignore next */
|
||||
const overshootDeringing = util.deprecate(function (overshootDeringing) {
|
||||
const formatOut = this.options.formatOut;
|
||||
this.jpeg({ overshootDeringing: (overshootDeringing !== false) });
|
||||
this.options.formatOut = formatOut;
|
||||
return this;
|
||||
}, 'overshootDeringing: use jpeg({ overshootDeringing: ... }) instead');
|
||||
/* istanbul ignore next */
|
||||
const optimiseScans = util.deprecate(function (optimiseScans) {
|
||||
const formatOut = this.options.formatOut;
|
||||
this.jpeg({ optimiseScans: (optimiseScans !== false) });
|
||||
this.options.formatOut = formatOut;
|
||||
return this;
|
||||
}, 'optimiseScans: use jpeg({ optimiseScans: ... }) instead');
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorate the Sharp prototype with output-related functions.
|
||||
@@ -503,15 +534,4 @@ module.exports = function (Sharp) {
|
||||
].forEach(function (f) {
|
||||
Sharp.prototype[f.name] = f;
|
||||
});
|
||||
// Deprecated
|
||||
Sharp.prototype.quality = quality;
|
||||
Sharp.prototype.progressive = progressive;
|
||||
Sharp.prototype.compressionLevel = compressionLevel;
|
||||
Sharp.prototype.withoutAdaptiveFiltering = withoutAdaptiveFiltering;
|
||||
Sharp.prototype.withoutChromaSubsampling = withoutChromaSubsampling;
|
||||
Sharp.prototype.trellisQuantisation = trellisQuantisation;
|
||||
Sharp.prototype.trellisQuantization = trellisQuantisation;
|
||||
Sharp.prototype.overshootDeringing = overshootDeringing;
|
||||
Sharp.prototype.optimiseScans = optimiseScans;
|
||||
Sharp.prototype.optimizeScans = optimiseScans;
|
||||
};
|
||||
|
||||
@@ -36,6 +36,7 @@ const strategy = {
|
||||
* @private
|
||||
*/
|
||||
const kernel = {
|
||||
nearest: 'nearest',
|
||||
cubic: 'cubic',
|
||||
lanczos2: 'lanczos2',
|
||||
lanczos3: 'lanczos3'
|
||||
@@ -62,6 +63,7 @@ const interpolator = {
|
||||
* By default, the resized image is centre cropped to the exact size specified.
|
||||
*
|
||||
* Possible reduction 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).
|
||||
@@ -89,8 +91,8 @@ const interpolator = {
|
||||
* // of the image data in inputBuffer
|
||||
* });
|
||||
*
|
||||
* @param {Number} [width] - pixels wide the resultant image should be, between 1 and 16383 (0x3FFF). Use `null` or `undefined` to auto-scale the width to match the height.
|
||||
* @param {Number} [height] - pixels high the resultant image should be, between 1 and 16383. Use `null` or `undefined` to auto-scale the height to match the width.
|
||||
* @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.
|
||||
@@ -99,21 +101,21 @@ const interpolator = {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const resize = function resize (width, height, options) {
|
||||
function resize (width, height, options) {
|
||||
if (is.defined(width)) {
|
||||
if (is.integer(width) && is.inRange(width, 1, this.constructor.maximum.width)) {
|
||||
if (is.integer(width) && width > 0) {
|
||||
this.options.width = width;
|
||||
} else {
|
||||
throw new Error('Invalid width (1 to ' + this.constructor.maximum.width + ') ' + width);
|
||||
throw is.invalidParameterError('width', 'positive integer', width);
|
||||
}
|
||||
} else {
|
||||
this.options.width = -1;
|
||||
}
|
||||
if (is.defined(height)) {
|
||||
if (is.integer(height) && is.inRange(height, 1, this.constructor.maximum.height)) {
|
||||
if (is.integer(height) && height > 0) {
|
||||
this.options.height = height;
|
||||
} else {
|
||||
throw new Error('Invalid height (1 to ' + this.constructor.maximum.height + ') ' + height);
|
||||
throw is.invalidParameterError('height', 'positive integer', height);
|
||||
}
|
||||
} else {
|
||||
this.options.height = -1;
|
||||
@@ -124,7 +126,7 @@ const resize = function resize (width, height, options) {
|
||||
if (is.string(kernel[options.kernel])) {
|
||||
this.options.kernel = kernel[options.kernel];
|
||||
} else {
|
||||
throw new Error('Invalid kernel ' + options.kernel);
|
||||
throw is.invalidParameterError('kernel', 'valid kernel name', options.kernel);
|
||||
}
|
||||
}
|
||||
// Interpolator
|
||||
@@ -132,7 +134,7 @@ const resize = function resize (width, height, options) {
|
||||
if (is.string(interpolator[options.interpolator])) {
|
||||
this.options.interpolator = interpolator[options.interpolator];
|
||||
} else {
|
||||
throw new Error('Invalid interpolator ' + options.interpolator);
|
||||
throw is.invalidParameterError('interpolator', 'valid interpolator name', options.interpolator);
|
||||
}
|
||||
}
|
||||
// Centre sampling
|
||||
@@ -142,7 +144,7 @@ const resize = function resize (width, height, options) {
|
||||
}
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Crop the resized image to the exact size specified, the default behaviour.
|
||||
@@ -170,7 +172,7 @@ const resize = function resize (width, height, options) {
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
const crop = function crop (crop) {
|
||||
function crop (crop) {
|
||||
this.options.canvas = 'crop';
|
||||
if (!is.defined(crop)) {
|
||||
// Default
|
||||
@@ -184,11 +186,14 @@ const crop = function crop (crop) {
|
||||
} else if (is.integer(crop) && crop >= strategy.entropy) {
|
||||
// Strategy
|
||||
this.options.crop = crop;
|
||||
} else if (is.string(crop) && is.integer(strategy[crop])) {
|
||||
// Strategy (string)
|
||||
this.options.crop = strategy[crop];
|
||||
} else {
|
||||
throw new Error('Unsupported crop ' + crop);
|
||||
throw is.invalidParameterError('crop', 'valid crop id/name/strategy', crop);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Preserving aspect ratio, resize the image to the maximum `width` or `height` specified
|
||||
@@ -200,7 +205,7 @@ const crop = function crop (crop) {
|
||||
* @example
|
||||
* sharp('input.gif')
|
||||
* .resize(200, 300)
|
||||
* .background({r: 0, g: 0, b: 0, a: 0})
|
||||
* .background({r: 0, g: 0, b: 0, alpha: 0})
|
||||
* .embed()
|
||||
* .toFormat(sharp.format.webp)
|
||||
* .toBuffer(function(err, outputBuffer) {
|
||||
@@ -213,10 +218,10 @@ const crop = function crop (crop) {
|
||||
*
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const embed = function embed () {
|
||||
function embed () {
|
||||
this.options.canvas = 'embed';
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Preserving aspect ratio, resize the image to be as large as possible
|
||||
@@ -237,10 +242,10 @@ const embed = function embed () {
|
||||
*
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const max = function max () {
|
||||
function max () {
|
||||
this.options.canvas = 'max';
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Preserving aspect ratio, resize the image to be as small as possible
|
||||
@@ -250,20 +255,20 @@ const max = function max () {
|
||||
*
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const min = function min () {
|
||||
function min () {
|
||||
this.options.canvas = 'min';
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Ignoring the aspect ratio of the input, stretch the image to
|
||||
* the exact `width` and/or `height` provided via `resize`.
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const ignoreAspectRatio = function ignoreAspectRatio () {
|
||||
function ignoreAspectRatio () {
|
||||
this.options.canvas = 'ignore_aspect';
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Do not enlarge the output image if the input image width *or* height are already less than the required dimensions.
|
||||
@@ -272,10 +277,10 @@ const ignoreAspectRatio = function ignoreAspectRatio () {
|
||||
* @param {Boolean} [withoutEnlargement=true]
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
const withoutEnlargement = function withoutEnlargement (withoutEnlargement) {
|
||||
function withoutEnlargement (withoutEnlargement) {
|
||||
this.options.withoutEnlargement = is.bool(withoutEnlargement) ? withoutEnlargement : true;
|
||||
return this;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorate the Sharp prototype with resize-related functions.
|
||||
|
||||
@@ -4,7 +4,7 @@ const is = require('./is');
|
||||
const sharp = require('../build/Release/sharp.node');
|
||||
|
||||
/**
|
||||
* Gets, or when options are provided sets, the limits of _libvips'_ operation cache.
|
||||
* Gets or, when options are provided, sets the limits of _libvips'_ operation cache.
|
||||
* Existing entries in the cache will be trimmed after any change in limits.
|
||||
* This method always returns cache statistics,
|
||||
* useful for determining how much working memory is required for a particular task.
|
||||
@@ -16,13 +16,13 @@ const sharp = require('../build/Release/sharp.node');
|
||||
* sharp.cache( { files: 0 } );
|
||||
* sharp.cache(false);
|
||||
*
|
||||
* @param {Object|Boolean} options - Object with the following attributes, or Boolean where true uses default cache settings and false removes all caching.
|
||||
* @param {Object|Boolean} [options=true] - Object with the following attributes, or Boolean where true uses default cache settings and false removes all caching
|
||||
* @param {Number} [options.memory=50] - is the maximum memory in MB to use for this cache
|
||||
* @param {Number} [options.files=20] - is the maximum number of files to hold open
|
||||
* @param {Number} [options.items=100] - is the maximum number of operations to cache
|
||||
* @returns {Object}
|
||||
*/
|
||||
const cache = function cache (options) {
|
||||
function cache (options) {
|
||||
if (is.bool(options)) {
|
||||
if (options) {
|
||||
// Default cache settings of 50MB, 20 files, 100 items
|
||||
@@ -35,11 +35,11 @@ const cache = function cache (options) {
|
||||
} else {
|
||||
return sharp.cache();
|
||||
}
|
||||
};
|
||||
}
|
||||
cache(true);
|
||||
|
||||
/**
|
||||
* Gets, or when a concurrency is provided sets,
|
||||
* Gets or, when a concurrency is provided, sets
|
||||
* the number of threads _libvips'_ should create to process each image.
|
||||
* The default value is the number of CPU cores.
|
||||
* A value of `0` will reset to this default.
|
||||
@@ -57,9 +57,9 @@ cache(true);
|
||||
* @param {Number} [concurrency]
|
||||
* @returns {Number} concurrency
|
||||
*/
|
||||
const concurrency = function concurrency (concurrency) {
|
||||
function concurrency (concurrency) {
|
||||
return sharp.concurrency(is.integer(concurrency) ? concurrency : null);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides access to internal task counters.
|
||||
@@ -71,9 +71,9 @@ const concurrency = function concurrency (concurrency) {
|
||||
*
|
||||
* @returns {Object}
|
||||
*/
|
||||
const counters = function counters () {
|
||||
function counters () {
|
||||
return sharp.counters();
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and set use of SIMD vector unit instructions.
|
||||
@@ -95,9 +95,9 @@ const counters = function counters () {
|
||||
* @param {Boolean} [simd=false]
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
const simd = function simd (simd) {
|
||||
function simd (simd) {
|
||||
return sharp.simd(is.bool(simd) ? simd : null);
|
||||
};
|
||||
}
|
||||
simd(false);
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
site_name: sharp
|
||||
site_url: http://sharp.dimens.io/
|
||||
site_url: http://sharp.pixelplumbing.com/
|
||||
repo_url: https://github.com/lovell/sharp
|
||||
site_description: High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP and TIFF images
|
||||
copyright: <a href="https://dimens.io/">dimens.io</a>
|
||||
google_analytics: ['UA-13034748-12', 'sharp.dimens.io']
|
||||
copyright: <a href="https://pixelplumbing.com/">pixelplumbing.com</a>
|
||||
google_analytics: ['UA-13034748-12', 'sharp.pixelplumbing.com']
|
||||
theme: readthedocs
|
||||
markdown_extensions:
|
||||
- toc:
|
||||
permalink: True
|
||||
dev_addr: 0.0.0.0:10101
|
||||
pages:
|
||||
- Home: index.md
|
||||
- Installation: install.md
|
||||
|
||||
57
package.json
@@ -1,8 +1,9 @@
|
||||
{
|
||||
"name": "sharp",
|
||||
"description": "High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP and TIFF images",
|
||||
"version": "0.17.0",
|
||||
"version": "0.18.4",
|
||||
"author": "Lovell Fuller <npm@lovell.info>",
|
||||
"homepage": "https://github.com/lovell/sharp",
|
||||
"contributors": [
|
||||
"Pierre Inglebert <pierre.inglebert@gmail.com>",
|
||||
"Jonathan Ong <jonathanrichardong@gmail.com>",
|
||||
@@ -29,11 +30,19 @@
|
||||
"Kleis Auke Wolthuizen <info@kleisauke.nl>",
|
||||
"Matt Hirsch <mhirsch@media.mit.edu>",
|
||||
"Matthias Thoemmes <thoemmes@gmail.com>",
|
||||
"Patrick Paskaris <patrick@paskaris.gr>"
|
||||
"Patrick Paskaris <patrick@paskaris.gr>",
|
||||
"Jérémy Lal <kapouer@melix.org>",
|
||||
"Rahul Nanwani <r.nanwani@gmail.com>",
|
||||
"Alice Monday <alice0meta@gmail.com>",
|
||||
"Kristo Jorgenson <kristo.jorgenson@gmail.com>",
|
||||
"YvesBos <yves_bos@outlook.com>",
|
||||
"Guy Maliar <guy@tailorbrands.com>",
|
||||
"Nicolas Coden <nicolas@ncoden.fr>",
|
||||
"Matt Parrish <matt.r.parrish@gmail.com>"
|
||||
],
|
||||
"scripts": {
|
||||
"clean": "rm -rf node_modules/ build/ vendor/ coverage/ test/fixtures/output.*",
|
||||
"test": "semistandard && cross-env VIPS_WARNING=0 nyc --reporter=lcov --branches=99 mocha --slow=5000 --timeout=60000 ./test/unit/*.js",
|
||||
"test": "semistandard && cc && nyc --reporter=lcov --branches=99 mocha --slow=5000 --timeout=60000 ./test/unit/*.js",
|
||||
"test-leak": "./test/leak/leak.sh",
|
||||
"test-packaging": "./packaging/test-linux-x64.sh",
|
||||
"docs": "for m in constructor input resize composite operation colour channel output utility; do documentation build --shallow --format=md lib/$m.js >docs/api-$m.md; done"
|
||||
@@ -60,36 +69,44 @@
|
||||
],
|
||||
"dependencies": {
|
||||
"caw": "^2.0.0",
|
||||
"color": "^1.0.2",
|
||||
"got": "^6.6.3",
|
||||
"nan": "^2.4.0",
|
||||
"color": "^2.0.0",
|
||||
"detect-libc": "^0.2.0",
|
||||
"nan": "^2.6.2",
|
||||
"semver": "^5.3.0",
|
||||
"tar": "^2.2.1"
|
||||
"simple-get": "^2.7.0",
|
||||
"tar": "^3.1.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"async": "^2.1.4",
|
||||
"bufferutil": "^1.3.0",
|
||||
"cross-env": "^3.1.3",
|
||||
"documentation": "^4.0.0-beta16",
|
||||
"exif-reader": "^1.0.1",
|
||||
"icc": "^0.0.2",
|
||||
"mocha": "^3.2.0",
|
||||
"node-cpplint": "^0.4.0",
|
||||
"nyc": "^10.0.0",
|
||||
"rimraf": "^2.5.4",
|
||||
"semistandard": "^9.2.1",
|
||||
"async": "^2.5.0",
|
||||
"cc": "^1.0.1",
|
||||
"documentation": "^4.0.0-rc.1",
|
||||
"exif-reader": "^1.0.2",
|
||||
"icc": "^1.0.0",
|
||||
"mocha": "^3.4.2",
|
||||
"nyc": "^11.0.3",
|
||||
"rimraf": "^2.6.1",
|
||||
"semistandard": "^11.0.0",
|
||||
"unzip": "^0.1.11"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"config": {
|
||||
"libvips": "8.4.2"
|
||||
"libvips": "8.5.5"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
"node": ">=4.5.0"
|
||||
},
|
||||
"semistandard": {
|
||||
"env": [
|
||||
"mocha"
|
||||
]
|
||||
},
|
||||
"cc": {
|
||||
"linelength": "120",
|
||||
"filter": [
|
||||
"build/c++11",
|
||||
"build/include",
|
||||
"runtime/indentation_namespace",
|
||||
"runtime/references"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,11 @@ if ! type docker >/dev/null; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Update base images
|
||||
for baseimage in debian:wheezy debian:jessie debian:stretch socialdefect/raspbian-jessie-core; do
|
||||
docker pull $baseimage
|
||||
done
|
||||
|
||||
# Windows (x64)
|
||||
if [ $PLATFORM = "all" ] || [ $PLATFORM = "win32-x64" ]; then
|
||||
echo "Building win32-x64..."
|
||||
|
||||
@@ -16,27 +16,28 @@ export CFLAGS="${FLAGS}"
|
||||
export CXXFLAGS="${FLAGS}"
|
||||
|
||||
# Dependency version numbers
|
||||
VERSION_ZLIB=1.2.8
|
||||
VERSION_ZLIB=1.2.11
|
||||
VERSION_FFI=3.2.1
|
||||
VERSION_GLIB=2.50.1
|
||||
VERSION_GLIB=2.53.1
|
||||
VERSION_XML2=2.9.4
|
||||
VERSION_GSF=1.14.40
|
||||
VERSION_GSF=1.14.41
|
||||
VERSION_EXIF=0.6.21
|
||||
VERSION_LCMS2=2.8
|
||||
VERSION_JPEG=1.5.1
|
||||
VERSION_PNG16=1.6.25
|
||||
VERSION_WEBP=0.5.1
|
||||
VERSION_TIFF=4.0.6
|
||||
VERSION_PNG16=1.6.29
|
||||
VERSION_WEBP=0.6.0
|
||||
VERSION_TIFF=4.0.7
|
||||
VERSION_ORC=0.4.26
|
||||
VERSION_GDKPIXBUF=2.36.0
|
||||
VERSION_FREETYPE=2.7
|
||||
VERSION_GDKPIXBUF=2.36.6
|
||||
VERSION_FREETYPE=2.8
|
||||
VERSION_EXPAT=2.2.0
|
||||
VERSION_FONTCONFIG=2.12.1
|
||||
VERSION_HARFBUZZ=1.3.2
|
||||
VERSION_HARFBUZZ=1.4.6
|
||||
VERSION_PIXMAN=0.34.0
|
||||
VERSION_CAIRO=1.14.6
|
||||
VERSION_PANGO=1.40.3
|
||||
VERSION_CROCO=0.6.11
|
||||
VERSION_SVG=2.40.16
|
||||
VERSION_CAIRO=1.14.8
|
||||
VERSION_PANGO=1.40.5
|
||||
VERSION_CROCO=0.6.12
|
||||
VERSION_SVG=2.40.17
|
||||
VERSION_GIF=5.1.4
|
||||
|
||||
# Least out-of-sync Sourceforge mirror
|
||||
@@ -56,11 +57,12 @@ cd ${DEPS}/ffi
|
||||
make install-strip
|
||||
|
||||
mkdir ${DEPS}/glib
|
||||
curl -Ls https://download.gnome.org/sources/glib/2.50/glib-${VERSION_GLIB}.tar.xz | tar xJC ${DEPS}/glib --strip-components=1
|
||||
curl -Ls https://download.gnome.org/sources/glib/2.53/glib-${VERSION_GLIB}.tar.xz | tar xJC ${DEPS}/glib --strip-components=1
|
||||
cd ${DEPS}/glib
|
||||
echo glib_cv_stack_grows=no >>glib.cache
|
||||
echo glib_cv_uscore=no >>glib.cache
|
||||
./configure --cache-file=glib.cache --host=${CHOST} --prefix=${TARGET} --enable-shared --disable-static --disable-dependency-tracking --with-pcre=internal --disable-libmount
|
||||
./configure --cache-file=glib.cache --host=${CHOST} --prefix=${TARGET} --enable-shared --disable-static --disable-dependency-tracking \
|
||||
--with-pcre=internal --disable-libmount
|
||||
make install-strip
|
||||
|
||||
mkdir ${DEPS}/xml2
|
||||
@@ -87,6 +89,9 @@ make install-strip
|
||||
mkdir ${DEPS}/lcms2
|
||||
curl -Ls http://${SOURCEFORGE_MIRROR}.dl.sourceforge.net/project/lcms/lcms/${VERSION_LCMS2}/lcms2-${VERSION_LCMS2}.tar.gz | tar xzC ${DEPS}/lcms2 --strip-components=1
|
||||
cd ${DEPS}/lcms2
|
||||
# Apply patches for lcms2 vulnerabilities reported since v2.8
|
||||
VERSION_LCMS2_GIT_MASTER_SHA=$(curl -Ls https://api.github.com/repos/mm2/Little-CMS/git/refs/heads/master | jq -r '.object.sha' | head -c7)
|
||||
curl -Ls https://github.com/mm2/Little-CMS/compare/lcms2.8...master.patch | patch -p1 -t || true
|
||||
./configure --host=${CHOST} --prefix=${TARGET} --enable-shared --disable-static --disable-dependency-tracking
|
||||
make install-strip
|
||||
|
||||
@@ -106,15 +111,16 @@ make install-strip
|
||||
mkdir ${DEPS}/webp
|
||||
curl -Ls http://downloads.webmproject.org/releases/webp/libwebp-${VERSION_WEBP}.tar.gz | tar xzC ${DEPS}/webp --strip-components=1
|
||||
cd ${DEPS}/webp
|
||||
./configure --host=${CHOST} --prefix=${TARGET} --enable-shared --disable-static --disable-dependency-tracking --disable-neon
|
||||
./configure --host=${CHOST} --prefix=${TARGET} --enable-shared --disable-static --disable-dependency-tracking \
|
||||
--disable-neon --enable-libwebpmux
|
||||
make install-strip
|
||||
|
||||
mkdir ${DEPS}/tiff
|
||||
curl -Ls http://download.osgeo.org/libtiff/tiff-${VERSION_TIFF}.tar.gz | tar xzC ${DEPS}/tiff --strip-components=1
|
||||
cd ${DEPS}/tiff
|
||||
# Apply patches for various libtiff security vulnerabilities reported since v4.0.6
|
||||
# Apply patches for libtiff vulnerabilities reported since v4.0.7
|
||||
VERSION_TIFF_GIT_MASTER_SHA=$(curl -Ls https://api.github.com/repos/vadz/libtiff/git/refs/heads/master | jq -r '.object.sha' | head -c7)
|
||||
curl -Ls https://github.com/vadz/libtiff/compare/Release-v4-0-6...master.patch | patch -p1 -t || true
|
||||
curl -Ls https://github.com/vadz/libtiff/compare/Release-v4-0-7...master.patch | patch -p1 -t || true
|
||||
if [ -n "${CHOST}" ]; then autoreconf -fiv; fi
|
||||
./configure --host=${CHOST} --prefix=${TARGET} --enable-shared --disable-static --disable-dependency-tracking --disable-mdi --disable-pixarlog --disable-cxx
|
||||
make install-strip
|
||||
@@ -130,8 +136,11 @@ rm -rf liborc-test-*
|
||||
mkdir ${DEPS}/gdkpixbuf
|
||||
curl -Ls https://download.gnome.org/sources/gdk-pixbuf/2.36/gdk-pixbuf-${VERSION_GDKPIXBUF}.tar.xz | tar xJC ${DEPS}/gdkpixbuf --strip-components=1
|
||||
cd ${DEPS}/gdkpixbuf
|
||||
touch gdk-pixbuf/loaders.cache
|
||||
LD_LIBRARY_PATH=${TARGET}/lib \
|
||||
./configure --cache-file=gdkpixbuf.cache --host=${CHOST} --prefix=${TARGET} --enable-shared --disable-static --disable-dependency-tracking --disable-introspection --disable-modules --disable-gio-sniffing --without-libtiff --without-gdiplus --with-included-loaders=png,jpeg
|
||||
./configure --host=${CHOST} --prefix=${TARGET} --enable-shared --disable-static --disable-dependency-tracking \
|
||||
--disable-introspection --disable-modules --disable-gio-sniffing \
|
||||
--without-libtiff --without-gdiplus --with-included-loaders=png,jpeg
|
||||
make install-strip
|
||||
|
||||
mkdir ${DEPS}/freetype
|
||||
@@ -140,10 +149,17 @@ cd ${DEPS}/freetype
|
||||
./configure --host=${CHOST} --prefix=${TARGET} --enable-shared --disable-static
|
||||
make install
|
||||
|
||||
mkdir ${DEPS}/expat
|
||||
curl -Ls http://${SOURCEFORGE_MIRROR}.dl.sourceforge.net/project/expat/expat/${VERSION_EXPAT}/expat-${VERSION_EXPAT}.tar.bz2 | tar xjC ${DEPS}/expat --strip-components=1
|
||||
cd ${DEPS}/expat
|
||||
./configure --host=${CHOST} --prefix=${TARGET} --enable-shared --disable-static
|
||||
make install
|
||||
|
||||
mkdir ${DEPS}/fontconfig
|
||||
curl -Ls https://www.freedesktop.org/software/fontconfig/release/fontconfig-${VERSION_FONTCONFIG}.tar.bz2 | tar xjC ${DEPS}/fontconfig --strip-components=1
|
||||
cd ${DEPS}/fontconfig
|
||||
./configure --host=${CHOST} --prefix=${TARGET} --enable-shared --disable-static --disable-dependency-tracking --enable-libxml2 --sysconfdir=/etc
|
||||
./configure --host=${CHOST} --prefix=${TARGET} --enable-shared --disable-static --disable-dependency-tracking \
|
||||
--with-expat-includes=${TARGET}/include --with-expat-lib=${TARGET}/lib --sysconfdir=/etc
|
||||
make install-strip
|
||||
|
||||
mkdir ${DEPS}/harfbuzz
|
||||
@@ -191,7 +207,7 @@ cd ${DEPS}/gif
|
||||
make install-strip
|
||||
|
||||
mkdir ${DEPS}/vips
|
||||
curl -Ls http://www.vips.ecs.soton.ac.uk/supported/8.4/vips-${VERSION_VIPS}.tar.gz | tar xzC ${DEPS}/vips --strip-components=1
|
||||
curl -Ls https://github.com/jcupitt/libvips/releases/download/v${VERSION_VIPS}/vips-${VERSION_VIPS}.tar.gz | tar xzC ${DEPS}/vips --strip-components=1
|
||||
cd ${DEPS}/vips
|
||||
./configure --host=${CHOST} --prefix=${TARGET} --enable-shared --disable-static --disable-dependency-tracking \
|
||||
--disable-debug --disable-introspection --without-python --without-fftw \
|
||||
@@ -212,6 +228,7 @@ echo "{\n\
|
||||
\"cairo\": \"${VERSION_CAIRO}\",\n\
|
||||
\"croco\": \"${VERSION_CROCO}\",\n\
|
||||
\"exif\": \"${VERSION_EXIF}\",\n\
|
||||
\"expat\": \"${VERSION_EXPAT}\",\n\
|
||||
\"ffi\": \"${VERSION_FFI}\",\n\
|
||||
\"fontconfig\": \"${VERSION_FONTCONFIG}\",\n\
|
||||
\"freetype\": \"${VERSION_FREETYPE}\",\n\
|
||||
@@ -221,7 +238,7 @@ echo "{\n\
|
||||
\"gsf\": \"${VERSION_GSF}\",\n\
|
||||
\"harfbuzz\": \"${VERSION_HARFBUZZ}\",\n\
|
||||
\"jpeg\": \"${VERSION_JPEG}\",\n\
|
||||
\"lcms\": \"${VERSION_LCMS2}\",\n\
|
||||
\"lcms\": \"${VERSION_LCMS2}-${VERSION_LCMS2_GIT_MASTER_SHA}\",\n\
|
||||
\"orc\": \"${VERSION_ORC}\",\n\
|
||||
\"pango\": \"${VERSION_PANGO}\",\n\
|
||||
\"pixman\": \"${VERSION_PIXMAN}\",\n\
|
||||
|
||||
@@ -8,14 +8,11 @@ curl -L -O https://github.com/lovell/build-win64/releases/download/v${VERSION_VI
|
||||
unzip vips-dev-w64-web-${VERSION_VIPS}.zip
|
||||
|
||||
# Clean and zip
|
||||
cd /vips/vips-dev-8.4
|
||||
cd /vips/vips-dev-8.5
|
||||
rm bin/libvipsCC-42.dll bin/libvips-cpp-42.dll bin/libgsf-win32-1-114.dll
|
||||
cp bin/*.dll lib/
|
||||
cp -r lib64/* lib/
|
||||
|
||||
# Temp patch for __declspec ordering
|
||||
curl -L -o include/vips/VImage8.h https://raw.githubusercontent.com/lovell/libvips/e1aef0445bf123d2de000bc7f2ef97b9f788eea0/cplusplus/include/vips/VImage8.h
|
||||
|
||||
echo "Creating tarball"
|
||||
tar czf /packaging/libvips-${VERSION_VIPS}-${PLATFORM}.tar.gz include lib/glib-2.0 lib/libvips.lib lib/libglib-2.0.lib lib/libgobject-2.0.lib lib/*.dll
|
||||
echo "Shrinking tarball"
|
||||
|
||||
@@ -9,10 +9,10 @@ RUN \
|
||||
apt-get install -y curl && \
|
||||
dpkg --add-architecture arm64 && \
|
||||
apt-get update && \
|
||||
apt-get install -y crossbuild-essential-arm64 autoconf libtool nasm gtk-doc-tools texinfo advancecomp libglib2.0-dev jq
|
||||
apt-get install -y crossbuild-essential-arm64 autoconf libtool nasm gtk-doc-tools texinfo advancecomp libglib2.0-dev jq gettext intltool autopoint
|
||||
|
||||
# Compiler settings
|
||||
ENV \
|
||||
PLATFORM=linux-armv8 \
|
||||
CHOST=aarch64-linux-gnu \
|
||||
FLAGS="-march=armv8-a -Os"
|
||||
FLAGS="-march=armv8-a -Os -D_GLIBCXX_USE_CXX11_ABI=0"
|
||||
|
||||
@@ -6,15 +6,14 @@ if ! type docker >/dev/null; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
version_node=6.3.0
|
||||
|
||||
test="npm run clean; npm install --unsafe-perm; npm test"
|
||||
|
||||
# Debian 7, 8
|
||||
# Ubuntu 14.04
|
||||
for dist in wheezy jessie trusty; do
|
||||
# Ubuntu 14.04, 16.04
|
||||
for dist in debian:jessie debian:stretch ubuntu:trusty ubuntu:xenial; do
|
||||
echo "Testing $dist..."
|
||||
if docker run -i -t --rm -v $PWD:/v -e "NODE_ENV=development" nodesource/$dist:$version_node >packaging/$dist.log 2>&1 sh -c "cd /v; ./packaging/test/debian.sh; $test";
|
||||
docker pull $dist
|
||||
if docker run -i -t --rm -v $PWD:/v $dist >packaging/$dist.log 2>&1 sh -c "cd /v; ./packaging/test/debian.sh; $test";
|
||||
then echo "$dist OK"
|
||||
else echo "$dist fail" && cat packaging/$dist.log
|
||||
fi
|
||||
@@ -22,35 +21,16 @@ done
|
||||
|
||||
# Centos 7
|
||||
echo "Testing centos7..."
|
||||
if docker run -i -t --rm -v $PWD:/v -e "NODE_ENV=development" nodesource/centos7:$version_node >packaging/$dist.log 2>&1 sh -c "cd /v; $test";
|
||||
docker pull centos:7
|
||||
if docker run -i -t --rm -v $PWD:/v centos:7 >packaging/centos7.log 2>&1 sh -c "cd /v; ./packaging/test/centos.sh; $test";
|
||||
then echo "centos7 OK"
|
||||
else echo "centos7 fail" && cat packaging/$dist.log
|
||||
else echo "centos7 fail" && cat packaging/centos7.log
|
||||
fi
|
||||
|
||||
# Fedora 22
|
||||
echo "Testing fedora22..."
|
||||
if docker run -i -t --rm -v $PWD:/v -e "NODE_ENV=development" nodesource/fedora22:$version_node >packaging/$dist.log 2>&1 sh -c "cd /v; $test";
|
||||
then echo "fedora22 OK"
|
||||
else echo "fedora22 fail" && cat packaging/$dist.log
|
||||
fi
|
||||
|
||||
# openSUSE 13.2
|
||||
echo "Testing opensuse..."
|
||||
if docker run -i -t --rm -v $PWD:/v opensuse:13.2 >packaging/opensuse.log 2>&1 /bin/sh -c "cd /v; ./packaging/test/opensuse.sh; $test";
|
||||
then echo "opensuse OK"
|
||||
else echo "opensuse fail" && cat packaging/opensuse.log
|
||||
fi
|
||||
|
||||
# Archlinux 2015.06.01
|
||||
# Archlinux latest
|
||||
echo "Testing archlinux..."
|
||||
docker pull pritunl/archlinux:latest
|
||||
if docker run -i -t --rm -v $PWD:/v pritunl/archlinux:latest >packaging/archlinux.log 2>&1 sh -c "cd /v; ./packaging/test/archlinux.sh; $test";
|
||||
then echo "archlinux OK"
|
||||
else echo "archlinux fail" && cat packaging/archlinux.log
|
||||
fi
|
||||
|
||||
# Alpine
|
||||
echo "Testing alpine..."
|
||||
if docker run -i -t --rm -v $PWD:/v -e "SHARP_TEST_WITHOUT_CACHE=0" alpine:edge >packaging/alpine.log 2>&1 sh -c "cd /v; ./packaging/test/alpine.sh; $test";
|
||||
then echo "alpine OK"
|
||||
else echo "alpine fail" && cat packaging/alpine.log
|
||||
fi
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Install build dependencies
|
||||
apk add --update make gcc g++ python nodejs
|
||||
|
||||
# Install libvips from aports/testing
|
||||
apk add --update --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing vips-dev
|
||||
4
packaging/test/centos.sh
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
|
||||
curl -sL https://rpm.nodesource.com/setup_6.x | bash -
|
||||
yum install -y gcc-c++ make nodejs
|
||||
@@ -1,5 +1,6 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Install pkg-config on Debian/Ubuntu
|
||||
apt-get update
|
||||
apt-get install -y pkg-config
|
||||
apt-get install -y build-essential python pkg-config curl
|
||||
curl -sL https://deb.nodesource.com/setup_6.x | bash -
|
||||
apt-get install -y nodejs
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Install Node.js on openSUSE 13.2
|
||||
zypper addrepo http://download.opensuse.org/repositories/devel:languages:nodejs/openSUSE_13.2/devel:languages:nodejs.repo
|
||||
zypper --gpg-auto-import-keys refresh
|
||||
zypper --non-interactive install gcc-c++ make nodejs-devel npm
|
||||
npm install -g npm
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM debian:jessie
|
||||
FROM debian:stretch
|
||||
MAINTAINER Lovell Fuller <npm@lovell.info>
|
||||
|
||||
# Create Debian-based container suitable for post-processing Windows x64 binaries
|
||||
|
||||
316
preinstall.sh
@@ -1,316 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Use of this script is deprecated
|
||||
|
||||
echo
|
||||
echo "WARNING: THIS SCRIPT WILL STOP WORKING AT THE END OF 2016"
|
||||
echo
|
||||
echo "WARNING: THIS SCRIPT IS NO LONGER REQUIRED ON MOST 64-BIT LINUX SYSTEMS WHEN USING SHARP V0.12.0+"
|
||||
echo
|
||||
echo "See http://sharp.dimens.io/page/install#linux"
|
||||
echo
|
||||
echo "If you really, really need this script, it will attempt"
|
||||
echo "to globally install libvips if not already available."
|
||||
echo
|
||||
sleep 5
|
||||
|
||||
vips_version_minimum=8.4.2
|
||||
vips_version_latest_major_minor=8.4
|
||||
vips_version_latest_patch=2
|
||||
|
||||
openslide_version_minimum=3.4.0
|
||||
openslide_version_latest_major_minor=3.4
|
||||
openslide_version_latest_patch=1
|
||||
|
||||
install_libvips_from_source() {
|
||||
echo "Compiling libvips $vips_version_latest_major_minor.$vips_version_latest_patch from source"
|
||||
curl -O http://www.vips.ecs.soton.ac.uk/supported/$vips_version_latest_major_minor/vips-$vips_version_latest_major_minor.$vips_version_latest_patch.tar.gz
|
||||
tar zvxf vips-$vips_version_latest_major_minor.$vips_version_latest_patch.tar.gz
|
||||
cd vips-$vips_version_latest_major_minor.$vips_version_latest_patch
|
||||
CXXFLAGS="-D_GLIBCXX_USE_CXX11_ABI=0" ./configure --disable-debug --disable-docs --disable-static --disable-introspection --disable-dependency-tracking --enable-cxx=yes --without-python --without-orc --without-fftw $1
|
||||
make
|
||||
make install
|
||||
cd ..
|
||||
rm -rf vips-$vips_version_latest_major_minor.$vips_version_latest_patch
|
||||
rm vips-$vips_version_latest_major_minor.$vips_version_latest_patch.tar.gz
|
||||
ldconfig
|
||||
echo "Installed libvips $(PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig pkg-config --modversion vips)"
|
||||
}
|
||||
|
||||
install_libopenslide_from_source() {
|
||||
echo "Compiling openslide $openslide_version_latest_major_minor.$openslide_version_latest_patch from source"
|
||||
curl -O -L https://github.com/openslide/openslide/releases/download/v$openslide_version_latest_major_minor.$openslide_version_latest_patch/openslide-$openslide_version_latest_major_minor.$openslide_version_latest_patch.tar.gz
|
||||
tar xzvf openslide-$openslide_version_latest_major_minor.$openslide_version_latest_patch.tar.gz
|
||||
cd openslide-$openslide_version_latest_major_minor.$openslide_version_latest_patch
|
||||
PKG_CONFIG_PATH=$pkg_config_path ./configure $1
|
||||
make
|
||||
make install
|
||||
cd ..
|
||||
rm -rf openslide-$openslide_version_latest_major_minor.$openslide_version_latest_patch
|
||||
rm openslide-$openslide_version_latest_major_minor.$openslide_version_latest_patch.tar.gz
|
||||
ldconfig
|
||||
echo "Installed libopenslide $openslide_version_latest_major_minor.$openslide_version_latest_patch"
|
||||
}
|
||||
|
||||
sorry() {
|
||||
echo "Sorry, I don't yet know how to install lib$1 on $2"
|
||||
exit 1
|
||||
}
|
||||
|
||||
pkg_config_path="$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig"
|
||||
|
||||
check_if_library_exists() {
|
||||
PKG_CONFIG_PATH=$pkg_config_path pkg-config --exists $1
|
||||
if [ $? -eq 0 ]; then
|
||||
version_found=$(PKG_CONFIG_PATH=$pkg_config_path pkg-config --modversion $1)
|
||||
PKG_CONFIG_PATH=$pkg_config_path pkg-config --atleast-version=$2 $1
|
||||
if [ $? -eq 0 ]; then
|
||||
# Found suitable version of libvips
|
||||
echo "Found lib$1 $version_found"
|
||||
return 1
|
||||
fi
|
||||
echo "Found lib$1 $version_found but require $2"
|
||||
else
|
||||
echo "Could not find lib$1 using a PKG_CONFIG_PATH of '$pkg_config_path'"
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
enable_openslide=0
|
||||
# Is libvips already installed, and is it at least the minimum required version?
|
||||
if [ $# -eq 1 ]; then
|
||||
if [ "$1" = "--with-openslide" ]; then
|
||||
echo "Installing vips with openslide support"
|
||||
enable_openslide=1
|
||||
else
|
||||
echo "Sorry, $1 is not supported. Did you mean --with-openslide?"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if ! type pkg-config >/dev/null; then
|
||||
sorry "vips" "a system without pkg-config"
|
||||
fi
|
||||
|
||||
openslide_exists=0
|
||||
if [ $enable_openslide -eq 1 ]; then
|
||||
check_if_library_exists "openslide" "$openslide_version_minimum"
|
||||
openslide_exists=$?
|
||||
fi
|
||||
|
||||
check_if_library_exists "vips" "$vips_version_minimum"
|
||||
vips_exists=$?
|
||||
if [ $vips_exists -eq 1 ] && [ $enable_openslide -eq 1 ]; then
|
||||
if [ $openslide_exists -eq 1 ]; then
|
||||
# Check if vips compiled with openslide support
|
||||
vips_with_openslide=`vips list classes | grep -i opensli`
|
||||
if [ -z $vips_with_openslide ]; then
|
||||
echo "Vips compiled without openslide support."
|
||||
else
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
elif [ $vips_exists -eq 1 ] && [ $enable_openslide -eq 0 ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Verify root/sudo access
|
||||
if [ "$(id -u)" -ne "0" ]; then
|
||||
echo "Sorry, I need root/sudo access to continue"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Deprecation warning
|
||||
if [ "$(arch)" == "x86_64" ]; then
|
||||
echo "This script is no longer required on most 64-bit Linux systems when using sharp v0.12.0+"
|
||||
fi
|
||||
|
||||
# OS-specific installations of libopenslide follows
|
||||
# Either openslide does not exist, or vips is installed without openslide support
|
||||
if [ $enable_openslide -eq 1 ] && [ -z $vips_with_openslide ] && [ $openslide_exists -eq 0 ]; then
|
||||
if [ -f /etc/debian_version ]; then
|
||||
# Debian Linux
|
||||
DISTRO=$(lsb_release -c -s)
|
||||
echo "Detected Debian Linux '$DISTRO'"
|
||||
case "$DISTRO" in
|
||||
jessie|vivid|wily|xenial)
|
||||
# Debian 8, Ubuntu 15
|
||||
echo "Installing libopenslide via apt-get"
|
||||
apt-get install -y libopenslide-dev
|
||||
;;
|
||||
trusty|utopic|qiana|rebecca|rafaela|freya)
|
||||
# Ubuntu 14, Mint 17
|
||||
echo "Installing libopenslide dependencies via apt-get"
|
||||
apt-get install -y automake build-essential curl zlib1g-dev libopenjpeg-dev libpng12-dev libjpeg-dev libtiff5-dev libgdk-pixbuf2.0-dev libxml2-dev libsqlite3-dev libcairo2-dev libglib2.0-dev sqlite3 libsqlite3-dev
|
||||
install_libopenslide_from_source
|
||||
;;
|
||||
precise|wheezy|maya)
|
||||
# Debian 7, Ubuntu 12.04, Mint 13
|
||||
echo "Installing libopenslide dependencies via apt-get"
|
||||
apt-get install -y automake build-essential curl zlib1g-dev libopenjpeg-dev libpng12-dev libjpeg-dev libtiff5-dev libgdk-pixbuf2.0-dev libxml2-dev libsqlite3-dev libcairo2-dev libglib2.0-dev sqlite3 libsqlite3-dev
|
||||
install_libopenslide_from_source
|
||||
;;
|
||||
*)
|
||||
# Unsupported Debian-based OS
|
||||
sorry "openslide" "Debian-based $DISTRO"
|
||||
;;
|
||||
esac
|
||||
elif [ -f /etc/redhat-release ]; then
|
||||
# Red Hat Linux
|
||||
RELEASE=$(cat /etc/redhat-release)
|
||||
echo "Detected Red Hat Linux '$RELEASE'"
|
||||
case $RELEASE in
|
||||
"Red Hat Enterprise Linux release 7."*|"CentOS Linux release 7."*|"Scientific Linux release 7."*)
|
||||
# RHEL/CentOS 7
|
||||
echo "Installing libopenslide dependencies via yum"
|
||||
yum groupinstall -y "Development Tools"
|
||||
yum install -y tar curl libpng-devel libjpeg-devel libxml2-devel zlib-devel openjpeg-devel libtiff-devel gdk-pixbuf2-devel sqlite-devel cairo-devel glib2-devel
|
||||
install_libopenslide_from_source "--prefix=/usr"
|
||||
;;
|
||||
"Red Hat Enterprise Linux release 6."*|"CentOS release 6."*|"Scientific Linux release 6."*)
|
||||
# RHEL/CentOS 6
|
||||
echo "Installing libopenslide dependencies via yum"
|
||||
yum groupinstall -y "Development Tools"
|
||||
yum install -y tar curl libpng-devel libjpeg-devel libxml2-devel zlib-devel openjpeg-devel libtiff-devel gdk-pixbuf2-devel sqlite-devel cairo-devel glib2-devel
|
||||
install_libopenslide_from_source "--prefix=/usr"
|
||||
;;
|
||||
"Fedora release 21 "*|"Fedora release 22 "*)
|
||||
# Fedora 21, 22
|
||||
echo "Installing libopenslide via yum"
|
||||
yum install -y openslide-devel
|
||||
;;
|
||||
*)
|
||||
# Unsupported RHEL-based OS
|
||||
sorry "openslide" "$RELEASE"
|
||||
;;
|
||||
esac
|
||||
elif [ -f /etc/os-release ]; then
|
||||
RELEASE=$(cat /etc/os-release | grep VERSION)
|
||||
echo "Detected OpenSuse Linux '$RELEASE'"
|
||||
case $RELEASE in
|
||||
*"13.2"*)
|
||||
echo "Installing libopenslide via zypper"
|
||||
zypper --gpg-auto-import-keys install -y libopenslide-devel
|
||||
;;
|
||||
esac
|
||||
elif [ -f /etc/SuSE-brand ]; then
|
||||
RELEASE=$(cat /etc/SuSE-brand | grep VERSION)
|
||||
echo "Detected OpenSuse Linux '$RELEASE'"
|
||||
case $RELEASE in
|
||||
*"13.1")
|
||||
echo "Installing libopenslide dependencies via zypper"
|
||||
zypper --gpg-auto-import-keys install -y --type pattern devel_basis
|
||||
zypper --gpg-auto-import-keys install -y tar curl libpng16-devel libjpeg-turbo libjpeg8-devel libxml2-devel zlib-devel openjpeg-devel libtiff-devel libgdk_pixbuf-2_0-0 sqlite3-devel cairo-devel glib2-devel
|
||||
install_libopenslide_from_source
|
||||
;;
|
||||
esac
|
||||
else
|
||||
# Unsupported OS
|
||||
sorry "openslide" "$(uname -a)"
|
||||
fi
|
||||
fi
|
||||
|
||||
# OS-specific installations of libvips follows
|
||||
|
||||
if [ -f /etc/debian_version ]; then
|
||||
# Debian Linux
|
||||
DISTRO=$(lsb_release -c -s)
|
||||
echo "Detected Debian Linux '$DISTRO'"
|
||||
case "$DISTRO" in
|
||||
jessie|trusty|utopic|vivid|wily|xenial|qiana|rebecca|rafaela|freya)
|
||||
# Debian 8, Ubuntu 14.04+, Mint 17
|
||||
echo "Installing libvips dependencies via apt-get"
|
||||
apt-get install -y automake build-essential gobject-introspection gtk-doc-tools libglib2.0-dev libjpeg-dev libpng12-dev libwebp-dev libtiff5-dev libexif-dev libgsf-1-dev liblcms2-dev libxml2-dev swig libmagickcore-dev curl
|
||||
install_libvips_from_source
|
||||
;;
|
||||
precise|wheezy|maya)
|
||||
# Debian 7, Ubuntu 12.04, Mint 13
|
||||
echo "Installing libvips dependencies via apt-get"
|
||||
add-apt-repository -y ppa:lyrasis/precise-backports
|
||||
apt-get update
|
||||
apt-get install -y automake build-essential gobject-introspection gtk-doc-tools libglib2.0-dev libjpeg-dev libpng12-dev libwebp-dev libtiff4-dev libexif-dev libgsf-1-dev liblcms2-dev libxml2-dev swig libmagickcore-dev curl
|
||||
install_libvips_from_source
|
||||
;;
|
||||
*)
|
||||
# Unsupported Debian-based OS
|
||||
sorry "vips" "Debian-based $DISTRO"
|
||||
;;
|
||||
esac
|
||||
elif [ -f /etc/redhat-release ]; then
|
||||
# Red Hat Linux
|
||||
RELEASE=$(cat /etc/redhat-release)
|
||||
echo "Detected Red Hat Linux '$RELEASE'"
|
||||
case $RELEASE in
|
||||
"Red Hat Enterprise Linux release 7."*|"CentOS Linux release 7."*|"Scientific Linux release 7."*)
|
||||
# RHEL/CentOS 7
|
||||
echo "Installing libvips dependencies via yum"
|
||||
yum groupinstall -y "Development Tools"
|
||||
yum install -y tar curl gtk-doc libxml2-devel libjpeg-turbo-devel libpng-devel libtiff-devel libexif-devel libgsf-devel lcms2-devel ImageMagick-devel gobject-introspection-devel libwebp-devel
|
||||
install_libvips_from_source "--prefix=/usr"
|
||||
;;
|
||||
"Red Hat Enterprise Linux release 6."*|"CentOS release 6."*|"Scientific Linux release 6."*)
|
||||
# RHEL/CentOS 6
|
||||
echo "Installing libvips dependencies via yum"
|
||||
yum groupinstall -y "Development Tools"
|
||||
yum install -y tar curl gtk-doc libxml2-devel libjpeg-turbo-devel libpng-devel libtiff-devel libexif-devel libgsf-devel lcms-devel ImageMagick-devel
|
||||
yum install -y http://li.nux.ro/download/nux/dextop/el6/x86_64/nux-dextop-release-0-2.el6.nux.noarch.rpm
|
||||
yum install -y --enablerepo=nux-dextop gobject-introspection-devel
|
||||
yum install -y http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
|
||||
yum install -y --enablerepo=remi libwebp-devel
|
||||
install_libvips_from_source "--prefix=/usr"
|
||||
;;
|
||||
"Fedora"*)
|
||||
# Fedora 21, 22, 23
|
||||
echo "Installing libvips dependencies via yum"
|
||||
yum groupinstall -y "Development Tools"
|
||||
yum install -y gcc-c++ gtk-doc libxml2-devel libjpeg-turbo-devel libpng-devel libtiff-devel libexif-devel lcms-devel ImageMagick-devel gobject-introspection-devel libwebp-devel curl
|
||||
install_libvips_from_source "--prefix=/usr"
|
||||
;;
|
||||
*)
|
||||
# Unsupported RHEL-based OS
|
||||
sorry "vips" "$RELEASE"
|
||||
;;
|
||||
esac
|
||||
elif [ -f /etc/system-release ]; then
|
||||
# Probably Amazon Linux
|
||||
RELEASE=$(cat /etc/system-release)
|
||||
case $RELEASE in
|
||||
"Amazon Linux AMI release 2015.03"|"Amazon Linux AMI release 2015.09")
|
||||
# Amazon Linux
|
||||
echo "Detected '$RELEASE'"
|
||||
echo "Installing libvips dependencies via yum"
|
||||
yum groupinstall -y "Development Tools"
|
||||
yum install -y gtk-doc libxml2-devel libjpeg-turbo-devel libpng-devel libtiff-devel libexif-devel libgsf-devel lcms2-devel ImageMagick-devel gobject-introspection-devel libwebp-devel curl
|
||||
install_libvips_from_source "--prefix=/usr"
|
||||
;;
|
||||
*)
|
||||
# Unsupported Amazon Linux version
|
||||
sorry "vips" "$RELEASE"
|
||||
;;
|
||||
esac
|
||||
elif [ -f /etc/os-release ]; then
|
||||
RELEASE=$(cat /etc/os-release | grep VERSION)
|
||||
echo "Detected OpenSuse Linux '$RELEASE'"
|
||||
case $RELEASE in
|
||||
*"13.2"*)
|
||||
echo "Installing libvips dependencies via zypper"
|
||||
zypper --gpg-auto-import-keys install -y --type pattern devel_basis
|
||||
zypper --gpg-auto-import-keys install -y tar curl gtk-doc libxml2-devel libjpeg-turbo libjpeg8-devel libpng16-devel libtiff-devel libexif-devel liblcms2-devel ImageMagick-devel gobject-introspection-devel libwebp-devel
|
||||
install_libvips_from_source
|
||||
;;
|
||||
esac
|
||||
elif [ -f /etc/SuSE-brand ]; then
|
||||
RELEASE=$(cat /etc/SuSE-brand | grep VERSION)
|
||||
echo "Detected OpenSuse Linux '$RELEASE'"
|
||||
case $RELEASE in
|
||||
*"13.1")
|
||||
echo "Installing libvips dependencies via zypper"
|
||||
zypper --gpg-auto-import-keys install -y --type pattern devel_basis
|
||||
zypper --gpg-auto-import-keys install -y tar curl gtk-doc libxml2-devel libjpeg-turbo libjpeg8-devel libpng16-devel libtiff-devel libexif-devel liblcms2-devel ImageMagick-devel gobject-introspection-devel libwebp-devel
|
||||
install_libvips_from_source
|
||||
;;
|
||||
esac
|
||||
else
|
||||
# Unsupported OS
|
||||
sorry "vips" "$(uname -a)"
|
||||
fi
|
||||
154
src/common.cc
@@ -1,12 +1,29 @@
|
||||
// Copyright 2013, 2014, 2015, 2016, 2017 Lovell Fuller and contributors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
#include <mutex>
|
||||
|
||||
#include <node.h>
|
||||
#include <node_buffer.h>
|
||||
#include <nan.h>
|
||||
#include <vips/vips8>
|
||||
|
||||
#include "nan.h"
|
||||
#include "common.h"
|
||||
|
||||
using vips::VImage;
|
||||
@@ -23,13 +40,13 @@ namespace sharp {
|
||||
|
||||
// Create an InputDescriptor instance from a v8::Object describing an input image
|
||||
InputDescriptor* CreateInputDescriptor(
|
||||
v8::Handle<v8::Object> input, std::vector<v8::Local<v8::Object>> buffersToPersist
|
||||
v8::Handle<v8::Object> input, std::vector<v8::Local<v8::Object>> &buffersToPersist
|
||||
) {
|
||||
Nan::HandleScope();
|
||||
InputDescriptor *descriptor = new InputDescriptor;
|
||||
if (HasAttr(input, "file")) {
|
||||
descriptor->file = AttrAsStr(input, "file");
|
||||
} else {
|
||||
} else if (HasAttr(input, "buffer")) {
|
||||
v8::Local<v8::Object> buffer = AttrAs<v8::Object>(input, "buffer");
|
||||
descriptor->bufferLength = node::Buffer::Length(buffer);
|
||||
descriptor->buffer = node::Buffer::Data(buffer);
|
||||
@@ -45,6 +62,16 @@ namespace sharp {
|
||||
descriptor->rawWidth = AttrTo<uint32_t>(input, "rawWidth");
|
||||
descriptor->rawHeight = AttrTo<uint32_t>(input, "rawHeight");
|
||||
}
|
||||
// Create new image
|
||||
if (HasAttr(input, "createChannels")) {
|
||||
descriptor->createChannels = AttrTo<uint32_t>(input, "createChannels");
|
||||
descriptor->createWidth = AttrTo<uint32_t>(input, "createWidth");
|
||||
descriptor->createHeight = AttrTo<uint32_t>(input, "createHeight");
|
||||
v8::Local<v8::Object> createBackground = AttrAs<v8::Object>(input, "createBackground");
|
||||
for (unsigned int i = 0; i < 4; i++) {
|
||||
descriptor->createBackground[i] = AttrTo<double>(createBackground, i);
|
||||
}
|
||||
}
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
@@ -177,7 +204,6 @@ namespace sharp {
|
||||
VImage image;
|
||||
ImageType imageType;
|
||||
if (descriptor->buffer != nullptr) {
|
||||
// From buffer
|
||||
if (descriptor->rawChannels > 0) {
|
||||
// Raw, uncompressed pixel data
|
||||
image = VImage::new_from_memory(descriptor->buffer, descriptor->bufferLength,
|
||||
@@ -212,26 +238,41 @@ namespace sharp {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// From filesystem
|
||||
imageType = DetermineImageType(descriptor->file.data());
|
||||
if (imageType != ImageType::UNKNOWN) {
|
||||
try {
|
||||
vips::VOption *option = VImage::option()->set("access", accessMethod);
|
||||
if (imageType == ImageType::SVG || imageType == ImageType::PDF) {
|
||||
option->set("dpi", static_cast<double>(descriptor->density));
|
||||
}
|
||||
if (imageType == ImageType::MAGICK) {
|
||||
option->set("density", std::to_string(descriptor->density).data());
|
||||
}
|
||||
image = VImage::new_from_file(descriptor->file.data(), option);
|
||||
if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) {
|
||||
SetDensity(image, descriptor->density);
|
||||
}
|
||||
} catch (...) {
|
||||
throw vips::VError("Input file has corrupt header");
|
||||
if (descriptor->createChannels > 0) {
|
||||
// Create new image
|
||||
std::vector<double> background = {
|
||||
descriptor->createBackground[0],
|
||||
descriptor->createBackground[1],
|
||||
descriptor->createBackground[2]
|
||||
};
|
||||
if (descriptor->createChannels == 4) {
|
||||
background.push_back(descriptor->createBackground[3]);
|
||||
}
|
||||
image = VImage::new_matrix(descriptor->createWidth, descriptor->createHeight).new_from_image(background);
|
||||
image.get_image()->Type = VIPS_INTERPRETATION_sRGB;
|
||||
imageType = ImageType::RAW;
|
||||
} else {
|
||||
throw vips::VError("Input file is missing or of an unsupported image format");
|
||||
// From filesystem
|
||||
imageType = DetermineImageType(descriptor->file.data());
|
||||
if (imageType != ImageType::UNKNOWN) {
|
||||
try {
|
||||
vips::VOption *option = VImage::option()->set("access", accessMethod);
|
||||
if (imageType == ImageType::SVG || imageType == ImageType::PDF) {
|
||||
option->set("dpi", static_cast<double>(descriptor->density));
|
||||
}
|
||||
if (imageType == ImageType::MAGICK) {
|
||||
option->set("density", std::to_string(descriptor->density).data());
|
||||
}
|
||||
image = VImage::new_from_file(descriptor->file.data(), option);
|
||||
if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) {
|
||||
SetDensity(image, descriptor->density);
|
||||
}
|
||||
} catch (...) {
|
||||
throw vips::VError("Input file has corrupt header");
|
||||
}
|
||||
} else {
|
||||
throw vips::VError("Input file is missing or of an unsupported image format");
|
||||
}
|
||||
}
|
||||
}
|
||||
return std::make_tuple(image, imageType);
|
||||
@@ -254,8 +295,7 @@ namespace sharp {
|
||||
return (
|
||||
(bands == 2 && interpretation == VIPS_INTERPRETATION_B_W) ||
|
||||
(bands == 4 && interpretation != VIPS_INTERPRETATION_CMYK) ||
|
||||
(bands == 5 && interpretation == VIPS_INTERPRETATION_CMYK)
|
||||
);
|
||||
(bands == 5 && interpretation == VIPS_INTERPRETATION_CMYK));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -307,6 +347,25 @@ namespace sharp {
|
||||
image.set(VIPS_META_RESOLUTION_UNIT, "in");
|
||||
}
|
||||
|
||||
/*
|
||||
Check the proposed format supports the current dimensions.
|
||||
*/
|
||||
void AssertImageTypeDimensions(VImage image, ImageType const imageType) {
|
||||
if (imageType == ImageType::JPEG) {
|
||||
if (image.width() > 65535 || image.height() > 65535) {
|
||||
throw vips::VError("Processed image is too large for the JPEG format");
|
||||
}
|
||||
} else if (imageType == ImageType::PNG) {
|
||||
if (image.width() > 2147483647 || image.height() > 2147483647) {
|
||||
throw vips::VError("Processed image is too large for the PNG format");
|
||||
}
|
||||
} else if (imageType == ImageType::WEBP) {
|
||||
if (image.width() > 16383 || image.height() > 16383) {
|
||||
throw vips::VError("Processed image is too large for the WebP format");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Called when a Buffer undergoes GC, required to support mixed runtime libraries in Windows
|
||||
*/
|
||||
@@ -316,6 +375,33 @@ namespace sharp {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Temporary buffer of warnings
|
||||
*/
|
||||
std::queue<std::string> vipsWarnings;
|
||||
std::mutex vipsWarningsMutex;
|
||||
|
||||
/*
|
||||
Called with warnings from the glib-registered "VIPS" domain
|
||||
*/
|
||||
void VipsWarningCallback(char const* log_domain, GLogLevelFlags log_level, char const* message, void* ignore) {
|
||||
std::lock_guard<std::mutex> lock(vipsWarningsMutex);
|
||||
vipsWarnings.emplace(message);
|
||||
}
|
||||
|
||||
/*
|
||||
Pop the oldest warning message from the queue
|
||||
*/
|
||||
std::string VipsWarningPop() {
|
||||
std::string warning;
|
||||
std::lock_guard<std::mutex> lock(vipsWarningsMutex);
|
||||
if (!vipsWarnings.empty()) {
|
||||
warning = vipsWarnings.front();
|
||||
vipsWarnings.pop();
|
||||
}
|
||||
return warning;
|
||||
}
|
||||
|
||||
/*
|
||||
Calculate the (left, top) coordinates of the output image
|
||||
within the input image, applying the given gravity.
|
||||
@@ -352,9 +438,11 @@ namespace sharp {
|
||||
// Southeast
|
||||
left = inWidth - outWidth;
|
||||
top = inHeight - outHeight;
|
||||
break;
|
||||
case 7:
|
||||
// Southwest
|
||||
top = inHeight - outHeight;
|
||||
break;
|
||||
case 8:
|
||||
// Northwest
|
||||
break;
|
||||
@@ -378,23 +466,23 @@ namespace sharp {
|
||||
int top = 0;
|
||||
|
||||
// assign only if valid
|
||||
if(x >= 0 && x < (inWidth - outWidth)) {
|
||||
if (x >= 0 && x < (inWidth - outWidth)) {
|
||||
left = x;
|
||||
} else if(x >= (inWidth - outWidth)) {
|
||||
} else if (x >= (inWidth - outWidth)) {
|
||||
left = inWidth - outWidth;
|
||||
}
|
||||
|
||||
if(y >= 0 && y < (inHeight - outHeight)) {
|
||||
if (y >= 0 && y < (inHeight - outHeight)) {
|
||||
top = y;
|
||||
} else if(y >= (inHeight - outHeight)) {
|
||||
} else if (y >= (inHeight - outHeight)) {
|
||||
top = inHeight - outHeight;
|
||||
}
|
||||
|
||||
// the resulting left and top could have been outside the image after calculation from bottom/right edges
|
||||
if(left < 0) {
|
||||
if (left < 0) {
|
||||
left = 0;
|
||||
}
|
||||
if(top < 0) {
|
||||
if (top < 0) {
|
||||
top = 0;
|
||||
}
|
||||
|
||||
@@ -421,8 +509,7 @@ namespace sharp {
|
||||
*/
|
||||
VipsOperationBoolean GetBooleanOperation(std::string const opStr) {
|
||||
return static_cast<VipsOperationBoolean>(
|
||||
vips_enum_from_nick(nullptr, VIPS_TYPE_OPERATION_BOOLEAN, opStr.data())
|
||||
);
|
||||
vips_enum_from_nick(nullptr, VIPS_TYPE_OPERATION_BOOLEAN, opStr.data()));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -430,8 +517,7 @@ namespace sharp {
|
||||
*/
|
||||
VipsInterpretation GetInterpretation(std::string const typeStr) {
|
||||
return static_cast<VipsInterpretation>(
|
||||
vips_enum_from_nick(nullptr, VIPS_TYPE_INTERPRETATION, typeStr.data())
|
||||
);
|
||||
vips_enum_from_nick(nullptr, VIPS_TYPE_INTERPRETATION, typeStr.data()));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
54
src/common.h
@@ -1,18 +1,32 @@
|
||||
// Copyright 2013, 2014, 2015, 2016, 2017 Lovell Fuller and contributors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef SRC_COMMON_H_
|
||||
#define SRC_COMMON_H_
|
||||
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#include <node.h>
|
||||
#include <nan.h>
|
||||
#include <vips/vips8>
|
||||
|
||||
#include "nan.h"
|
||||
|
||||
// Verify platform and compiler compatibility
|
||||
|
||||
#if (VIPS_MAJOR_VERSION < 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION < 4))
|
||||
#error libvips version 8.4.x required - see sharp.dimens.io/page/install
|
||||
#if (VIPS_MAJOR_VERSION < 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION < 5))
|
||||
#error libvips version 8.5.x required - see sharp.dimens.io/page/install
|
||||
#endif
|
||||
|
||||
#if ((!defined(__clang__)) && defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6)))
|
||||
@@ -38,6 +52,10 @@ namespace sharp {
|
||||
int rawChannels;
|
||||
int rawWidth;
|
||||
int rawHeight;
|
||||
int createChannels;
|
||||
int createWidth;
|
||||
int createHeight;
|
||||
double createBackground[4];
|
||||
|
||||
InputDescriptor():
|
||||
buffer(nullptr),
|
||||
@@ -45,7 +63,15 @@ namespace sharp {
|
||||
density(72),
|
||||
rawChannels(0),
|
||||
rawWidth(0),
|
||||
rawHeight(0) {}
|
||||
rawHeight(0),
|
||||
createChannels(0),
|
||||
createWidth(0),
|
||||
createHeight(0) {
|
||||
createBackground[0] = 0.0;
|
||||
createBackground[1] = 0.0;
|
||||
createBackground[2] = 0.0;
|
||||
createBackground[3] = 255.0;
|
||||
}
|
||||
};
|
||||
|
||||
// Convenience methods to access the attributes of a v8::Object
|
||||
@@ -63,8 +89,7 @@ namespace sharp {
|
||||
|
||||
// Create an InputDescriptor instance from a v8::Object describing an input image
|
||||
InputDescriptor* CreateInputDescriptor(
|
||||
v8::Handle<v8::Object> input, std::vector<v8::Local<v8::Object>> buffersToPersist
|
||||
);
|
||||
v8::Handle<v8::Object> input, std::vector<v8::Local<v8::Object>> &buffersToPersist);
|
||||
|
||||
enum class ImageType {
|
||||
JPEG,
|
||||
@@ -159,11 +184,26 @@ namespace sharp {
|
||||
*/
|
||||
void SetDensity(VImage image, const int density);
|
||||
|
||||
/*
|
||||
Check the proposed format supports the current dimensions.
|
||||
*/
|
||||
void AssertImageTypeDimensions(VImage image, ImageType const imageType);
|
||||
|
||||
/*
|
||||
Called when a Buffer undergoes GC, required to support mixed runtime libraries in Windows
|
||||
*/
|
||||
void FreeCallback(char* data, void* hint);
|
||||
|
||||
/*
|
||||
Called with warnings from the glib-registered "VIPS" domain
|
||||
*/
|
||||
void VipsWarningCallback(char const* log_domain, GLogLevelFlags log_level, char const* message, void* ignore);
|
||||
|
||||
/*
|
||||
Pop the oldest warning message from the queue
|
||||
*/
|
||||
std::string VipsWarningPop();
|
||||
|
||||
/*
|
||||
Calculate the (left, top) coordinates of the output image
|
||||
within the input image, applying the given gravity.
|
||||
|
||||
@@ -350,7 +350,7 @@ set_property( VipsObject *object, const char *name, const GValue *value )
|
||||
|
||||
if( vips_object_get_argument( object, name,
|
||||
&pspec, &argument_class, &argument_instance ) ) {
|
||||
vips_warn( NULL, "%s", vips_error_buffer() );
|
||||
g_warning( "%s", vips_error_buffer() );
|
||||
vips_error_clear();
|
||||
return;
|
||||
}
|
||||
@@ -364,7 +364,7 @@ set_property( VipsObject *object, const char *name, const GValue *value )
|
||||
|
||||
if( (enum_value = vips_enum_from_nick( object_class->nickname,
|
||||
pspec_type, g_value_get_string( value ) )) < 0 ) {
|
||||
vips_warn( NULL, "%s", vips_error_buffer() );
|
||||
g_warning( "%s", vips_error_buffer() );
|
||||
vips_error_clear();
|
||||
return;
|
||||
}
|
||||
@@ -770,19 +770,19 @@ operator+( VImage a, std::vector<double> b )
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator+=( VImage a, const VImage b )
|
||||
operator+=( VImage &a, const VImage b )
|
||||
{
|
||||
return( a = a + b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator+=( VImage a, const double b )
|
||||
operator+=( VImage &a, const double b )
|
||||
{
|
||||
return( a = a + b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator+=( VImage a, std::vector<double> b )
|
||||
operator+=( VImage &a, std::vector<double> b )
|
||||
{
|
||||
return( a = a + b );
|
||||
}
|
||||
@@ -818,19 +818,19 @@ operator-( VImage a, std::vector<double> b )
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator-=( VImage a, const VImage b )
|
||||
operator-=( VImage &a, const VImage b )
|
||||
{
|
||||
return( a = a - b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator-=( VImage a, const double b )
|
||||
operator-=( VImage &a, const double b )
|
||||
{
|
||||
return( a = a - b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator-=( VImage a, std::vector<double> b )
|
||||
operator-=( VImage &a, std::vector<double> b )
|
||||
{
|
||||
return( a = a - b );
|
||||
}
|
||||
@@ -872,19 +872,19 @@ operator*( VImage a, std::vector<double> b )
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator*=( VImage a, const VImage b )
|
||||
operator*=( VImage &a, const VImage b )
|
||||
{
|
||||
return( a = a * b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator*=( VImage a, const double b )
|
||||
operator*=( VImage &a, const double b )
|
||||
{
|
||||
return( a = a * b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator*=( VImage a, std::vector<double> b )
|
||||
operator*=( VImage &a, std::vector<double> b )
|
||||
{
|
||||
return( a = a * b );
|
||||
}
|
||||
@@ -920,19 +920,19 @@ operator/( VImage a, std::vector<double> b )
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator/=( VImage a, const VImage b )
|
||||
operator/=( VImage &a, const VImage b )
|
||||
{
|
||||
return( a = a / b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator/=( VImage a, const double b )
|
||||
operator/=( VImage &a, const double b )
|
||||
{
|
||||
return( a = a / b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator/=( VImage a, std::vector<double> b )
|
||||
operator/=( VImage &a, std::vector<double> b )
|
||||
{
|
||||
return( a = a / b );
|
||||
}
|
||||
@@ -956,19 +956,19 @@ operator%( VImage a, std::vector<double> b )
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator%=( VImage a, const VImage b )
|
||||
operator%=( VImage &a, const VImage b )
|
||||
{
|
||||
return( a = a % b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator%=( VImage a, const double b )
|
||||
operator%=( VImage &a, const double b )
|
||||
{
|
||||
return( a = a % b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator%=( VImage a, std::vector<double> b )
|
||||
operator%=( VImage &a, std::vector<double> b )
|
||||
{
|
||||
return( a = a % b );
|
||||
}
|
||||
@@ -982,29 +982,29 @@ operator<( VImage a, VImage b )
|
||||
VImage
|
||||
operator<( double a, VImage b )
|
||||
{
|
||||
return( b.relational_const( to_vector( a ),
|
||||
VIPS_OPERATION_RELATIONAL_MORE ) );
|
||||
return( b.relational_const( VIPS_OPERATION_RELATIONAL_MORE,
|
||||
to_vector( a ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator<( VImage a, double b )
|
||||
{
|
||||
return( a.relational_const( to_vector( b ),
|
||||
VIPS_OPERATION_RELATIONAL_LESS ) );
|
||||
return( a.relational_const( VIPS_OPERATION_RELATIONAL_LESS,
|
||||
to_vector( b ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator<( std::vector<double> a, VImage b )
|
||||
{
|
||||
return( b.relational_const( a,
|
||||
VIPS_OPERATION_RELATIONAL_MORE ) );
|
||||
return( b.relational_const( VIPS_OPERATION_RELATIONAL_MORE,
|
||||
a ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator<( VImage a, std::vector<double> b )
|
||||
{
|
||||
return( a.relational_const( b,
|
||||
VIPS_OPERATION_RELATIONAL_LESS ) );
|
||||
return( a.relational_const( VIPS_OPERATION_RELATIONAL_LESS,
|
||||
b ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
@@ -1016,29 +1016,29 @@ operator<=( VImage a, VImage b )
|
||||
VImage
|
||||
operator<=( double a, VImage b )
|
||||
{
|
||||
return( b.relational_const( to_vector( a ),
|
||||
VIPS_OPERATION_RELATIONAL_MOREEQ ) );
|
||||
return( b.relational_const( VIPS_OPERATION_RELATIONAL_MOREEQ,
|
||||
to_vector( a ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator<=( VImage a, double b )
|
||||
{
|
||||
return( a.relational_const( to_vector( b ),
|
||||
VIPS_OPERATION_RELATIONAL_LESSEQ ) );
|
||||
return( a.relational_const( VIPS_OPERATION_RELATIONAL_LESSEQ,
|
||||
to_vector( b ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator<=( std::vector<double> a, VImage b )
|
||||
{
|
||||
return( b.relational_const( a,
|
||||
VIPS_OPERATION_RELATIONAL_MOREEQ ) );
|
||||
return( b.relational_const( VIPS_OPERATION_RELATIONAL_MOREEQ,
|
||||
a ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator<=( VImage a, std::vector<double> b )
|
||||
{
|
||||
return( a.relational_const( b,
|
||||
VIPS_OPERATION_RELATIONAL_LESSEQ ) );
|
||||
return( a.relational_const( VIPS_OPERATION_RELATIONAL_LESSEQ,
|
||||
b ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
@@ -1050,29 +1050,29 @@ operator>( VImage a, VImage b )
|
||||
VImage
|
||||
operator>( double a, VImage b )
|
||||
{
|
||||
return( b.relational_const( to_vector( a ),
|
||||
VIPS_OPERATION_RELATIONAL_LESS ) );
|
||||
return( b.relational_const( VIPS_OPERATION_RELATIONAL_LESS,
|
||||
to_vector( a ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator>( VImage a, double b )
|
||||
{
|
||||
return( a.relational_const( to_vector( b ),
|
||||
VIPS_OPERATION_RELATIONAL_MORE ) );
|
||||
return( a.relational_const( VIPS_OPERATION_RELATIONAL_MORE,
|
||||
to_vector( b ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator>( std::vector<double> a, VImage b )
|
||||
{
|
||||
return( b.relational_const( a,
|
||||
VIPS_OPERATION_RELATIONAL_LESS ) );
|
||||
return( b.relational_const( VIPS_OPERATION_RELATIONAL_LESS,
|
||||
a ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator>( VImage a, std::vector<double> b )
|
||||
{
|
||||
return( a.relational_const( b,
|
||||
VIPS_OPERATION_RELATIONAL_MORE ) );
|
||||
return( a.relational_const( VIPS_OPERATION_RELATIONAL_MORE,
|
||||
b ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
@@ -1084,29 +1084,29 @@ operator>=( VImage a, VImage b )
|
||||
VImage
|
||||
operator>=( double a, VImage b )
|
||||
{
|
||||
return( b.relational_const( to_vector( a ),
|
||||
VIPS_OPERATION_RELATIONAL_LESSEQ ) );
|
||||
return( b.relational_const( VIPS_OPERATION_RELATIONAL_LESSEQ,
|
||||
to_vector( a ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator>=( VImage a, double b )
|
||||
{
|
||||
return( a.relational_const( to_vector( b ),
|
||||
VIPS_OPERATION_RELATIONAL_MOREEQ ) );
|
||||
return( a.relational_const( VIPS_OPERATION_RELATIONAL_MOREEQ,
|
||||
to_vector( b ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator>=( std::vector<double> a, VImage b )
|
||||
{
|
||||
return( b.relational_const( a,
|
||||
VIPS_OPERATION_RELATIONAL_LESSEQ ) );
|
||||
return( b.relational_const( VIPS_OPERATION_RELATIONAL_LESSEQ,
|
||||
a ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator>=( VImage a, std::vector<double> b )
|
||||
{
|
||||
return( a.relational_const( b,
|
||||
VIPS_OPERATION_RELATIONAL_MOREEQ ) );
|
||||
return( a.relational_const( VIPS_OPERATION_RELATIONAL_MOREEQ,
|
||||
b ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
@@ -1118,29 +1118,29 @@ operator==( VImage a, VImage b )
|
||||
VImage
|
||||
operator==( double a, VImage b )
|
||||
{
|
||||
return( b.relational_const( to_vector( a ),
|
||||
VIPS_OPERATION_RELATIONAL_EQUAL ) );
|
||||
return( b.relational_const( VIPS_OPERATION_RELATIONAL_EQUAL,
|
||||
to_vector( a ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator==( VImage a, double b )
|
||||
{
|
||||
return( a.relational_const( to_vector( b ),
|
||||
VIPS_OPERATION_RELATIONAL_EQUAL ) );
|
||||
return( a.relational_const( VIPS_OPERATION_RELATIONAL_EQUAL,
|
||||
to_vector( b ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator==( std::vector<double> a, VImage b )
|
||||
{
|
||||
return( b.relational_const( a,
|
||||
VIPS_OPERATION_RELATIONAL_EQUAL ) );
|
||||
return( b.relational_const( VIPS_OPERATION_RELATIONAL_EQUAL,
|
||||
a ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator==( VImage a, std::vector<double> b )
|
||||
{
|
||||
return( a.relational_const( b,
|
||||
VIPS_OPERATION_RELATIONAL_EQUAL ) );
|
||||
return( a.relational_const( VIPS_OPERATION_RELATIONAL_EQUAL,
|
||||
b ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
@@ -1152,29 +1152,29 @@ operator!=( VImage a, VImage b )
|
||||
VImage
|
||||
operator!=( double a, VImage b )
|
||||
{
|
||||
return( b.relational_const( to_vector( a ),
|
||||
VIPS_OPERATION_RELATIONAL_NOTEQ ) );
|
||||
return( b.relational_const( VIPS_OPERATION_RELATIONAL_NOTEQ,
|
||||
to_vector( a ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator!=( VImage a, double b )
|
||||
{
|
||||
return( a.relational_const( to_vector( b ),
|
||||
VIPS_OPERATION_RELATIONAL_NOTEQ ) );
|
||||
return( a.relational_const( VIPS_OPERATION_RELATIONAL_NOTEQ,
|
||||
to_vector( b ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator!=( std::vector<double> a, VImage b )
|
||||
{
|
||||
return( b.relational_const( a,
|
||||
VIPS_OPERATION_RELATIONAL_NOTEQ ) );
|
||||
return( b.relational_const( VIPS_OPERATION_RELATIONAL_NOTEQ,
|
||||
a ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator!=( VImage a, std::vector<double> b )
|
||||
{
|
||||
return( a.relational_const( b,
|
||||
VIPS_OPERATION_RELATIONAL_NOTEQ ) );
|
||||
return( a.relational_const( VIPS_OPERATION_RELATIONAL_NOTEQ,
|
||||
b ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
@@ -1186,43 +1186,43 @@ operator&( VImage a, VImage b )
|
||||
VImage
|
||||
operator&( double a, VImage b )
|
||||
{
|
||||
return( b.boolean_const( to_vector( a ),
|
||||
VIPS_OPERATION_BOOLEAN_AND ) );
|
||||
return( b.boolean_const( VIPS_OPERATION_BOOLEAN_AND,
|
||||
to_vector( a ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator&( VImage a, double b )
|
||||
{
|
||||
return( a.boolean_const( to_vector( b ),
|
||||
VIPS_OPERATION_BOOLEAN_AND ) );
|
||||
return( a.boolean_const( VIPS_OPERATION_BOOLEAN_AND,
|
||||
to_vector( b ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator&( std::vector<double> a, VImage b )
|
||||
{
|
||||
return( b.boolean_const( a, VIPS_OPERATION_BOOLEAN_AND ) );
|
||||
return( b.boolean_const( VIPS_OPERATION_BOOLEAN_AND, a ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator&( VImage a, std::vector<double> b )
|
||||
{
|
||||
return( a.boolean_const( b, VIPS_OPERATION_BOOLEAN_AND ) );
|
||||
return( a.boolean_const( VIPS_OPERATION_BOOLEAN_AND, b ) );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator&=( VImage a, const VImage b )
|
||||
operator&=( VImage &a, const VImage b )
|
||||
{
|
||||
return( a = a & b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator&=( VImage a, const double b )
|
||||
operator&=( VImage &a, const double b )
|
||||
{
|
||||
return( a = a & b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator&=( VImage a, std::vector<double> b )
|
||||
operator&=( VImage &a, std::vector<double> b )
|
||||
{
|
||||
return( a = a & b );
|
||||
}
|
||||
@@ -1236,43 +1236,45 @@ operator|( VImage a, VImage b )
|
||||
VImage
|
||||
operator|( double a, VImage b )
|
||||
{
|
||||
return( b.boolean_const( to_vector( a ),
|
||||
VIPS_OPERATION_BOOLEAN_OR ) );
|
||||
return( b.boolean_const( VIPS_OPERATION_BOOLEAN_OR,
|
||||
to_vector( a ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator|( VImage a, double b )
|
||||
{
|
||||
return( a.boolean_const( to_vector( b ),
|
||||
VIPS_OPERATION_BOOLEAN_OR ) );
|
||||
return( a.boolean_const( VIPS_OPERATION_BOOLEAN_OR,
|
||||
to_vector( b ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator|( std::vector<double> a, VImage b )
|
||||
{
|
||||
return( b.boolean_const( a, VIPS_OPERATION_BOOLEAN_OR ) );
|
||||
return( b.boolean_const( VIPS_OPERATION_BOOLEAN_OR,
|
||||
a ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator|( VImage a, std::vector<double> b )
|
||||
{
|
||||
return( a.boolean_const( b, VIPS_OPERATION_BOOLEAN_OR ) );
|
||||
return( a.boolean_const( VIPS_OPERATION_BOOLEAN_OR,
|
||||
b ) );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator|=( VImage a, const VImage b )
|
||||
operator|=( VImage &a, const VImage b )
|
||||
{
|
||||
return( a = a | b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator|=( VImage a, const double b )
|
||||
operator|=( VImage &a, const double b )
|
||||
{
|
||||
return( a = a | b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator|=( VImage a, std::vector<double> b )
|
||||
operator|=( VImage &a, std::vector<double> b )
|
||||
{
|
||||
return( a = a | b );
|
||||
}
|
||||
@@ -1286,43 +1288,45 @@ operator^( VImage a, VImage b )
|
||||
VImage
|
||||
operator^( double a, VImage b )
|
||||
{
|
||||
return( b.boolean_const( to_vector( a ),
|
||||
VIPS_OPERATION_BOOLEAN_EOR ) );
|
||||
return( b.boolean_const( VIPS_OPERATION_BOOLEAN_EOR,
|
||||
to_vector( a ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator^( VImage a, double b )
|
||||
{
|
||||
return( a.boolean_const( to_vector( b ),
|
||||
VIPS_OPERATION_BOOLEAN_EOR ) );
|
||||
return( a.boolean_const( VIPS_OPERATION_BOOLEAN_EOR,
|
||||
to_vector( b ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator^( std::vector<double> a, VImage b )
|
||||
{
|
||||
return( b.boolean_const( a, VIPS_OPERATION_BOOLEAN_EOR ) );
|
||||
return( b.boolean_const( VIPS_OPERATION_BOOLEAN_EOR,
|
||||
a ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator^( VImage a, std::vector<double> b )
|
||||
{
|
||||
return( a.boolean_const( b, VIPS_OPERATION_BOOLEAN_EOR ) );
|
||||
return( a.boolean_const( VIPS_OPERATION_BOOLEAN_EOR,
|
||||
b ) );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator^=( VImage a, const VImage b )
|
||||
operator^=( VImage &a, const VImage b )
|
||||
{
|
||||
return( a = a ^ b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator^=( VImage a, const double b )
|
||||
operator^=( VImage &a, const double b )
|
||||
{
|
||||
return( a = a ^ b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator^=( VImage a, std::vector<double> b )
|
||||
operator^=( VImage &a, std::vector<double> b )
|
||||
{
|
||||
return( a = a ^ b );
|
||||
}
|
||||
@@ -1336,30 +1340,31 @@ operator<<( VImage a, VImage b )
|
||||
VImage
|
||||
operator<<( VImage a, double b )
|
||||
{
|
||||
return( a.boolean_const( to_vector( b ),
|
||||
VIPS_OPERATION_BOOLEAN_LSHIFT ) );
|
||||
return( a.boolean_const( VIPS_OPERATION_BOOLEAN_LSHIFT,
|
||||
to_vector( b ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator<<( VImage a, std::vector<double> b )
|
||||
{
|
||||
return( a.boolean_const( b, VIPS_OPERATION_BOOLEAN_LSHIFT ) );
|
||||
return( a.boolean_const( VIPS_OPERATION_BOOLEAN_LSHIFT,
|
||||
b ) );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator<<=( VImage a, const VImage b )
|
||||
operator<<=( VImage &a, const VImage b )
|
||||
{
|
||||
return( a = a << b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator<<=( VImage a, const double b )
|
||||
operator<<=( VImage &a, const double b )
|
||||
{
|
||||
return( a = a << b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator<<=( VImage a, std::vector<double> b )
|
||||
operator<<=( VImage &a, std::vector<double> b )
|
||||
{
|
||||
return( a = a << b );
|
||||
}
|
||||
@@ -1373,30 +1378,31 @@ operator>>( VImage a, VImage b )
|
||||
VImage
|
||||
operator>>( VImage a, double b )
|
||||
{
|
||||
return( a.boolean_const( to_vector( b ),
|
||||
VIPS_OPERATION_BOOLEAN_RSHIFT ) );
|
||||
return( a.boolean_const( VIPS_OPERATION_BOOLEAN_RSHIFT,
|
||||
to_vector( b ) ) );
|
||||
}
|
||||
|
||||
VImage
|
||||
operator>>( VImage a, std::vector<double> b )
|
||||
{
|
||||
return( a.boolean_const( b, VIPS_OPERATION_BOOLEAN_RSHIFT ) );
|
||||
return( a.boolean_const( VIPS_OPERATION_BOOLEAN_RSHIFT,
|
||||
b ) );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator>>=( VImage a, const VImage b )
|
||||
operator>>=( VImage &a, const VImage b )
|
||||
{
|
||||
return( a = a << b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator>>=( VImage a, const double b )
|
||||
operator>>=( VImage &a, const double b )
|
||||
{
|
||||
return( a = a << b );
|
||||
}
|
||||
|
||||
VImage &
|
||||
operator>>=( VImage a, std::vector<double> b )
|
||||
operator>>=( VImage &a, std::vector<double> b )
|
||||
{
|
||||
return( a = a << b );
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// bodies for vips operations
|
||||
// Thu 18 Aug 16:01:57 BST 2016
|
||||
// Mon 13 Mar 13:22:17 GMT 2017
|
||||
// this file is generated automatically, do not edit!
|
||||
|
||||
void VImage::system( char * cmd_format , VOption *options )
|
||||
@@ -231,7 +231,7 @@ VImage VImage::round( VipsOperationRound round , VOption *options )
|
||||
return( out );
|
||||
}
|
||||
|
||||
VImage VImage::relational_const( std::vector<double> c , VipsOperationRelational relational , VOption *options )
|
||||
VImage VImage::relational_const( VipsOperationRelational relational , std::vector<double> c , VOption *options )
|
||||
{
|
||||
VImage out;
|
||||
|
||||
@@ -239,8 +239,8 @@ VImage VImage::relational_const( std::vector<double> c , VipsOperationRelational
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) ->
|
||||
set( "c", c ) ->
|
||||
set( "relational", relational ) );
|
||||
set( "relational", relational ) ->
|
||||
set( "c", c ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -258,7 +258,7 @@ VImage VImage::remainder_const( std::vector<double> c , VOption *options )
|
||||
return( out );
|
||||
}
|
||||
|
||||
VImage VImage::boolean_const( std::vector<double> c , VipsOperationBoolean boolean , VOption *options )
|
||||
VImage VImage::boolean_const( VipsOperationBoolean boolean , std::vector<double> c , VOption *options )
|
||||
{
|
||||
VImage out;
|
||||
|
||||
@@ -266,13 +266,13 @@ VImage VImage::boolean_const( std::vector<double> c , VipsOperationBoolean boole
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) ->
|
||||
set( "c", c ) ->
|
||||
set( "boolean", boolean ) );
|
||||
set( "boolean", boolean ) ->
|
||||
set( "c", c ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
|
||||
VImage VImage::math2_const( std::vector<double> c , VipsOperationMath2 math2 , VOption *options )
|
||||
VImage VImage::math2_const( VipsOperationMath2 math2 , std::vector<double> c , VOption *options )
|
||||
{
|
||||
VImage out;
|
||||
|
||||
@@ -280,8 +280,8 @@ VImage VImage::math2_const( std::vector<double> c , VipsOperationMath2 math2 , V
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) ->
|
||||
set( "c", c ) ->
|
||||
set( "math2", math2 ) );
|
||||
set( "math2", math2 ) ->
|
||||
set( "c", c ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -493,8 +493,8 @@ VImage VImage::copy( VOption *options )
|
||||
|
||||
call( "copy" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) );
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -505,8 +505,8 @@ VImage VImage::tilecache( VOption *options )
|
||||
|
||||
call( "tilecache" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) );
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -517,8 +517,8 @@ VImage VImage::linecache( VOption *options )
|
||||
|
||||
call( "linecache" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) );
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -529,8 +529,8 @@ VImage VImage::sequential( VOption *options )
|
||||
|
||||
call( "sequential" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) );
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -541,8 +541,8 @@ VImage VImage::cache( VOption *options )
|
||||
|
||||
call( "cache" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) );
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -569,8 +569,8 @@ VImage VImage::flip( VipsDirection direction , VOption *options )
|
||||
|
||||
call( "flip" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) ->
|
||||
set( "direction", direction ) );
|
||||
|
||||
return( out );
|
||||
@@ -633,6 +633,20 @@ VImage VImage::extract_area( int left , int top , int width , int height , VOpti
|
||||
return( out );
|
||||
}
|
||||
|
||||
VImage VImage::smartcrop( int width , int height , VOption *options )
|
||||
{
|
||||
VImage out;
|
||||
|
||||
call( "smartcrop" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "input", *this ) ->
|
||||
set( "out", &out ) ->
|
||||
set( "width", width ) ->
|
||||
set( "height", height ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
|
||||
VImage VImage::extract_band( int band , VOption *options )
|
||||
{
|
||||
VImage out;
|
||||
@@ -728,8 +742,8 @@ VImage VImage::cast( VipsBandFormat format , VOption *options )
|
||||
|
||||
call( "cast" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) ->
|
||||
set( "format", format ) );
|
||||
|
||||
return( out );
|
||||
@@ -741,8 +755,8 @@ VImage VImage::rot( VipsAngle angle , VOption *options )
|
||||
|
||||
call( "rot" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) ->
|
||||
set( "angle", angle ) );
|
||||
|
||||
return( out );
|
||||
@@ -754,8 +768,8 @@ VImage VImage::rot45( VOption *options )
|
||||
|
||||
call( "rot45" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) );
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -766,8 +780,8 @@ VImage VImage::autorot( VOption *options )
|
||||
|
||||
call( "autorot" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) );
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -805,8 +819,8 @@ VImage VImage::bandfold( VOption *options )
|
||||
|
||||
call( "bandfold" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) );
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -817,8 +831,8 @@ VImage VImage::bandunfold( VOption *options )
|
||||
|
||||
call( "bandunfold" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) );
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -829,8 +843,8 @@ VImage VImage::flatten( VOption *options )
|
||||
|
||||
call( "flatten" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) );
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -841,8 +855,8 @@ VImage VImage::premultiply( VOption *options )
|
||||
|
||||
call( "premultiply" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) );
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -853,8 +867,8 @@ VImage VImage::unpremultiply( VOption *options )
|
||||
|
||||
call( "unpremultiply" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) );
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -865,8 +879,8 @@ VImage VImage::grid( int tile_height , int across , int down , VOption *options
|
||||
|
||||
call( "grid" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) ->
|
||||
set( "tile-height", tile_height ) ->
|
||||
set( "across", across ) ->
|
||||
set( "down", down ) );
|
||||
@@ -880,8 +894,8 @@ VImage VImage::scale( VOption *options )
|
||||
|
||||
call( "scale" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) );
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -892,8 +906,8 @@ VImage VImage::wrap( VOption *options )
|
||||
|
||||
call( "wrap" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) );
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -944,8 +958,8 @@ VImage VImage::byteswap( VOption *options )
|
||||
|
||||
call( "byteswap" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "out", &out ) ->
|
||||
set( "in", *this ) );
|
||||
set( "in", *this ) ->
|
||||
set( "out", &out ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
@@ -1757,6 +1771,18 @@ void VImage::dzsave( char * filename , VOption *options )
|
||||
set( "filename", filename ) );
|
||||
}
|
||||
|
||||
VipsBlob * VImage::dzsave_buffer( VOption *options )
|
||||
{
|
||||
VipsBlob * buffer;
|
||||
|
||||
call( "dzsave_buffer" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "in", *this ) ->
|
||||
set( "buffer", &buffer ) );
|
||||
|
||||
return( buffer );
|
||||
}
|
||||
|
||||
void VImage::pngsave( char * filename , VOption *options )
|
||||
{
|
||||
call( "pngsave" ,
|
||||
@@ -1832,6 +1858,18 @@ void VImage::tiffsave( char * filename , VOption *options )
|
||||
set( "filename", filename ) );
|
||||
}
|
||||
|
||||
VipsBlob * VImage::tiffsave_buffer( VOption *options )
|
||||
{
|
||||
VipsBlob * buffer;
|
||||
|
||||
call( "tiffsave_buffer" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "in", *this ) ->
|
||||
set( "buffer", &buffer ) );
|
||||
|
||||
return( buffer );
|
||||
}
|
||||
|
||||
void VImage::fitssave( char * filename , VOption *options )
|
||||
{
|
||||
call( "fitssave" ,
|
||||
@@ -1840,6 +1878,32 @@ void VImage::fitssave( char * filename , VOption *options )
|
||||
set( "filename", filename ) );
|
||||
}
|
||||
|
||||
VImage VImage::thumbnail( char * filename , int width , VOption *options )
|
||||
{
|
||||
VImage out;
|
||||
|
||||
call( "thumbnail" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "filename", filename ) ->
|
||||
set( "out", &out ) ->
|
||||
set( "width", width ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
|
||||
VImage VImage::thumbnail_buffer( VipsBlob * buffer , int width , VOption *options )
|
||||
{
|
||||
VImage out;
|
||||
|
||||
call( "thumbnail_buffer" ,
|
||||
(options ? options : VImage::option()) ->
|
||||
set( "buffer", buffer ) ->
|
||||
set( "out", &out ) ->
|
||||
set( "width", width ) );
|
||||
|
||||
return( out );
|
||||
}
|
||||
|
||||
VImage VImage::mapim( VImage index , VOption *options )
|
||||
{
|
||||
VImage out;
|
||||
|
||||
@@ -1,25 +1,40 @@
|
||||
// Copyright 2013, 2014, 2015, 2016, 2017 Lovell Fuller and contributors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <numeric>
|
||||
#include <vector>
|
||||
|
||||
#include <node.h>
|
||||
#include <nan.h>
|
||||
#include <vips/vips8>
|
||||
|
||||
#include "nan.h"
|
||||
#include "common.h"
|
||||
#include "metadata.h"
|
||||
|
||||
class MetadataWorker : public Nan::AsyncWorker {
|
||||
public:
|
||||
MetadataWorker(
|
||||
Nan::Callback *callback, MetadataBaton *baton,
|
||||
std::vector<v8::Local<v8::Object>> const buffersToPersist
|
||||
) : Nan::AsyncWorker(callback), baton(baton), buffersToPersist(buffersToPersist) {
|
||||
Nan::Callback *callback, MetadataBaton *baton, Nan::Callback *debuglog,
|
||||
std::vector<v8::Local<v8::Object>> const buffersToPersist) :
|
||||
Nan::AsyncWorker(callback), baton(baton), debuglog(debuglog),
|
||||
buffersToPersist(buffersToPersist) {
|
||||
// Protect Buffer objects from GC, keyed on index
|
||||
std::accumulate(buffersToPersist.begin(), buffersToPersist.end(), 0,
|
||||
[this](uint32_t index, v8::Local<v8::Object> const buffer) -> uint32_t {
|
||||
SaveToPersistent(index, buffer);
|
||||
return index + 1;
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
~MetadataWorker() {}
|
||||
|
||||
@@ -42,6 +57,7 @@ class MetadataWorker : public Nan::AsyncWorker {
|
||||
baton->height = image.height();
|
||||
baton->space = vips_enum_nick(VIPS_TYPE_INTERPRETATION, image.interpretation());
|
||||
baton->channels = image.bands();
|
||||
baton->depth = vips_enum_nick(VIPS_TYPE_BAND_FORMAT, image.format());
|
||||
if (sharp::HasDensity(image)) {
|
||||
baton->density = sharp::GetDensity(image);
|
||||
}
|
||||
@@ -72,7 +88,7 @@ class MetadataWorker : public Nan::AsyncWorker {
|
||||
vips_thread_shutdown();
|
||||
}
|
||||
|
||||
void HandleOKCallback () {
|
||||
void HandleOKCallback() {
|
||||
using Nan::New;
|
||||
using Nan::Set;
|
||||
Nan::HandleScope();
|
||||
@@ -88,6 +104,7 @@ class MetadataWorker : public Nan::AsyncWorker {
|
||||
Set(info, New("height").ToLocalChecked(), New<v8::Uint32>(baton->height));
|
||||
Set(info, New("space").ToLocalChecked(), New<v8::String>(baton->space).ToLocalChecked());
|
||||
Set(info, New("channels").ToLocalChecked(), New<v8::Uint32>(baton->channels));
|
||||
Set(info, New("depth").ToLocalChecked(), New<v8::String>(baton->depth).ToLocalChecked());
|
||||
if (baton->density > 0) {
|
||||
Set(info, New("density").ToLocalChecked(), New<v8::Uint32>(baton->density));
|
||||
}
|
||||
@@ -99,14 +116,12 @@ class MetadataWorker : public Nan::AsyncWorker {
|
||||
if (baton->exifLength > 0) {
|
||||
Set(info,
|
||||
New("exif").ToLocalChecked(),
|
||||
Nan::NewBuffer(baton->exif, baton->exifLength, sharp::FreeCallback, nullptr).ToLocalChecked()
|
||||
);
|
||||
Nan::NewBuffer(baton->exif, baton->exifLength, sharp::FreeCallback, nullptr).ToLocalChecked());
|
||||
}
|
||||
if (baton->iccLength > 0) {
|
||||
Set(info,
|
||||
New("icc").ToLocalChecked(),
|
||||
Nan::NewBuffer(baton->icc, baton->iccLength, sharp::FreeCallback, nullptr).ToLocalChecked()
|
||||
);
|
||||
Nan::NewBuffer(baton->icc, baton->iccLength, sharp::FreeCallback, nullptr).ToLocalChecked());
|
||||
}
|
||||
argv[1] = info;
|
||||
}
|
||||
@@ -116,17 +131,25 @@ class MetadataWorker : public Nan::AsyncWorker {
|
||||
[this](uint32_t index, v8::Local<v8::Object> const buffer) -> uint32_t {
|
||||
GetFromPersistent(index);
|
||||
return index + 1;
|
||||
}
|
||||
);
|
||||
});
|
||||
delete baton->input;
|
||||
delete baton;
|
||||
|
||||
// Handle warnings
|
||||
std::string warning = sharp::VipsWarningPop();
|
||||
while (!warning.empty()) {
|
||||
v8::Local<v8::Value> message[1] = { New(warning).ToLocalChecked() };
|
||||
debuglog->Call(1, message);
|
||||
warning = sharp::VipsWarningPop();
|
||||
}
|
||||
|
||||
// Return to JavaScript
|
||||
callback->Call(2, argv);
|
||||
}
|
||||
|
||||
private:
|
||||
MetadataBaton* baton;
|
||||
Nan::Callback *debuglog;
|
||||
std::vector<v8::Local<v8::Object>> buffersToPersist;
|
||||
};
|
||||
|
||||
@@ -144,9 +167,12 @@ NAN_METHOD(metadata) {
|
||||
// Input
|
||||
baton->input = sharp::CreateInputDescriptor(sharp::AttrAs<v8::Object>(options, "input"), buffersToPersist);
|
||||
|
||||
// Function to notify of libvips warnings
|
||||
Nan::Callback *debuglog = new Nan::Callback(sharp::AttrAs<v8::Function>(options, "debuglog"));
|
||||
|
||||
// Join queue for worker thread
|
||||
Nan::Callback *callback = new Nan::Callback(info[1].As<v8::Function>());
|
||||
Nan::AsyncQueueWorker(new MetadataWorker(callback, baton, buffersToPersist));
|
||||
Nan::AsyncQueueWorker(new MetadataWorker(callback, baton, debuglog, buffersToPersist));
|
||||
|
||||
// Increment queued task counter
|
||||
g_atomic_int_inc(&sharp::counterQueue);
|
||||
|
||||
@@ -1,8 +1,24 @@
|
||||
// Copyright 2013, 2014, 2015, 2016, 2017 Lovell Fuller and contributors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef SRC_METADATA_H_
|
||||
#define SRC_METADATA_H_
|
||||
|
||||
#include "nan.h"
|
||||
#include "common.h"
|
||||
#include <string>
|
||||
#include <nan.h>
|
||||
|
||||
#include "./common.h"
|
||||
|
||||
struct MetadataBaton {
|
||||
// Input
|
||||
@@ -13,6 +29,7 @@ struct MetadataBaton {
|
||||
int height;
|
||||
std::string space;
|
||||
int channels;
|
||||
std::string depth;
|
||||
int density;
|
||||
bool hasProfile;
|
||||
bool hasAlpha;
|
||||
|
||||
@@ -1,7 +1,23 @@
|
||||
// Copyright 2013, 2014, 2015, 2016, 2017 Lovell Fuller and contributors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#include <vips/vips8>
|
||||
|
||||
#include "common.h"
|
||||
@@ -13,69 +29,32 @@ using vips::VError;
|
||||
namespace sharp {
|
||||
|
||||
/*
|
||||
Alpha composite src over dst with given gravity.
|
||||
Assumes alpha channels are already premultiplied and will be unpremultiplied after.
|
||||
Composite overlayImage over image at given position
|
||||
Assumes alpha channels are already premultiplied and will be unpremultiplied after
|
||||
*/
|
||||
VImage Composite(VImage src, VImage dst, const int gravity) {
|
||||
if(IsInputValidForComposition(src, dst)) {
|
||||
// Enlarge overlay src, if required
|
||||
if (src.width() < dst.width() || src.height() < dst.height()) {
|
||||
// Calculate the (left, top) coordinates of the output image within the input image, applying the given gravity.
|
||||
int left;
|
||||
int top;
|
||||
std::tie(left, top) = CalculateCrop(dst.width(), dst.height(), src.width(), src.height(), gravity);
|
||||
// Embed onto transparent background
|
||||
std::vector<double> background { 0.0, 0.0, 0.0, 0.0 };
|
||||
src = src.embed(left, top, dst.width(), dst.height(), VImage::option()
|
||||
VImage Composite(VImage image, VImage overlayImage, int const left, int const top) {
|
||||
if (HasAlpha(overlayImage)) {
|
||||
// Alpha composite
|
||||
if (overlayImage.width() < image.width() || overlayImage.height() < image.height()) {
|
||||
// Enlarge overlay
|
||||
std::vector<double> const background { 0.0, 0.0, 0.0, 0.0 };
|
||||
overlayImage = overlayImage.embed(left, top, image.width(), image.height(), VImage::option()
|
||||
->set("extend", VIPS_EXTEND_BACKGROUND)
|
||||
->set("background", background)
|
||||
);
|
||||
->set("background", background));
|
||||
}
|
||||
return CompositeImage(src, dst);
|
||||
}
|
||||
// If the input was not valid for composition the return the input image itself
|
||||
return dst;
|
||||
}
|
||||
|
||||
VImage Composite(VImage src, VImage dst, const int x, const int y) {
|
||||
if(IsInputValidForComposition(src, dst)) {
|
||||
// Enlarge overlay src, if required
|
||||
if (src.width() < dst.width() || src.height() < dst.height()) {
|
||||
// Calculate the (left, top) coordinates of the output image within the input image, applying the given gravity.
|
||||
int left;
|
||||
int top;
|
||||
std::tie(left, top) = CalculateCrop(dst.width(), dst.height(), src.width(), src.height(), x, y);
|
||||
// Embed onto transparent background
|
||||
std::vector<double> background { 0.0, 0.0, 0.0, 0.0 };
|
||||
src = src.embed(left, top, dst.width(), dst.height(), VImage::option()
|
||||
->set("extend", VIPS_EXTEND_BACKGROUND)
|
||||
->set("background", background)
|
||||
);
|
||||
return AlphaComposite(image, overlayImage);
|
||||
} else {
|
||||
if (HasAlpha(image)) {
|
||||
// Add alpha channel to overlayImage so channels match
|
||||
double const multiplier = sharp::Is16Bit(overlayImage.interpretation()) ? 256.0 : 1.0;
|
||||
overlayImage = overlayImage.bandjoin(
|
||||
VImage::new_matrix(overlayImage.width(), overlayImage.height()).new_from_image(255 * multiplier));
|
||||
}
|
||||
return CompositeImage(src, dst);
|
||||
return image.insert(overlayImage, left, top);
|
||||
}
|
||||
// If the input was not valid for composition the return the input image itself
|
||||
return dst;
|
||||
}
|
||||
|
||||
bool IsInputValidForComposition(VImage src, VImage dst) {
|
||||
using sharp::CalculateCrop;
|
||||
using sharp::HasAlpha;
|
||||
|
||||
if (!HasAlpha(src)) {
|
||||
throw VError("Overlay image must have an alpha channel");
|
||||
}
|
||||
if (!HasAlpha(dst)) {
|
||||
throw VError("Image to be overlaid must have an alpha channel");
|
||||
}
|
||||
if (src.width() > dst.width() || src.height() > dst.height()) {
|
||||
throw VError("Overlay image must have same dimensions or smaller");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
VImage CompositeImage(VImage src, VImage dst) {
|
||||
VImage AlphaComposite(VImage dst, VImage src) {
|
||||
// Split src into non-alpha and alpha channels
|
||||
VImage srcWithoutAlpha = src.extract_band(0, VImage::option()->set("n", src.bands() - 1));
|
||||
VImage srcAlpha = src[src.bands() - 1] * (1.0 / 255.0);
|
||||
@@ -145,12 +124,11 @@ namespace sharp {
|
||||
std::vector<double> background { 0.0, 0.0, 0.0, 0.0 };
|
||||
mask = mask.embed(left, top, dst.width(), dst.height(), VImage::option()
|
||||
->set("extend", VIPS_EXTEND_BACKGROUND)
|
||||
->set("background", background)
|
||||
);
|
||||
->set("background", background));
|
||||
}
|
||||
|
||||
// we use the mask alpha if it has alpha
|
||||
if(maskHasAlpha) {
|
||||
if (maskHasAlpha) {
|
||||
mask = mask.extract_band(mask.bands() - 1, VImage::option()->set("n", 1));;
|
||||
}
|
||||
|
||||
@@ -284,123 +262,11 @@ namespace sharp {
|
||||
colourspaceBeforeSharpen = VIPS_INTERPRETATION_sRGB;
|
||||
}
|
||||
return image.sharpen(
|
||||
VImage::option()->set("sigma", sigma)->set("m1", flat)->set("m2", jagged)
|
||||
).colourspace(colourspaceBeforeSharpen);
|
||||
VImage::option()->set("sigma", sigma)->set("m1", flat)->set("m2", jagged))
|
||||
.colourspace(colourspaceBeforeSharpen);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Calculate the Shannon entropy
|
||||
*/
|
||||
double EntropyStrategy::operator()(VImage image) {
|
||||
return image.hist_find().hist_entropy();
|
||||
}
|
||||
|
||||
/*
|
||||
Calculate the intensity of edges, skin tone and saturation
|
||||
*/
|
||||
double AttentionStrategy::operator()(VImage image) {
|
||||
// Flatten RGBA onto a mid-grey background
|
||||
if (image.bands() == 4 && HasAlpha(image)) {
|
||||
double const midgrey = sharp::Is16Bit(image.interpretation()) ? 32768.0 : 128.0;
|
||||
std::vector<double> background { midgrey, midgrey, midgrey };
|
||||
image = image.flatten(VImage::option()->set("background", background));
|
||||
}
|
||||
// Convert to LAB colourspace
|
||||
VImage lab = image.colourspace(VIPS_INTERPRETATION_LAB);
|
||||
VImage l = lab[0];
|
||||
VImage a = lab[1];
|
||||
VImage b = lab[2];
|
||||
// Edge detect luminosity with the Sobel operator
|
||||
VImage sobel = vips::VImage::new_matrixv(3, 3,
|
||||
-1.0, 0.0, 1.0,
|
||||
-2.0, 0.0, 2.0,
|
||||
-1.0, 0.0, 1.0);
|
||||
VImage edges = l.conv(sobel).abs() + l.conv(sobel.rot90()).abs();
|
||||
// Skin tone chroma thresholds trained with http://humanae.tumblr.com/
|
||||
VImage skin = (a >= 3) & (a <= 22) & (b >= 4) & (b <= 31);
|
||||
// Chroma >~50% saturation
|
||||
VImage lch = lab.colourspace(VIPS_INTERPRETATION_LCH);
|
||||
VImage c = lch[1];
|
||||
VImage saturation = c > 60;
|
||||
// Find maximum in combined saliency mask
|
||||
VImage mask = edges + skin + saturation;
|
||||
return mask.max();
|
||||
}
|
||||
|
||||
/*
|
||||
Calculate crop area based on image entropy
|
||||
*/
|
||||
std::tuple<int, int> Crop(
|
||||
VImage image, int const outWidth, int const outHeight, std::function<double(VImage)> strategy
|
||||
) {
|
||||
int left = 0;
|
||||
int top = 0;
|
||||
int const inWidth = image.width();
|
||||
int const inHeight = image.height();
|
||||
if (inWidth > outWidth) {
|
||||
// Reduce width by repeated removing slices from edge with lowest score
|
||||
int width = inWidth;
|
||||
double leftScore = 0.0;
|
||||
double rightScore = 0.0;
|
||||
// Max width of each slice
|
||||
int const maxSliceWidth = static_cast<int>(ceil((inWidth - outWidth) / 8.0));
|
||||
while (width > outWidth) {
|
||||
// Width of current slice
|
||||
int const slice = std::min(width - outWidth, maxSliceWidth);
|
||||
if (leftScore == 0.0) {
|
||||
// Update score of left slice
|
||||
leftScore = strategy(image.extract_area(left, 0, slice, inHeight));
|
||||
}
|
||||
if (rightScore == 0.0) {
|
||||
// Update score of right slice
|
||||
rightScore = strategy(image.extract_area(width - slice - 1, 0, slice, inHeight));
|
||||
}
|
||||
// Keep slice with highest score
|
||||
if (leftScore >= rightScore) {
|
||||
// Discard right slice
|
||||
rightScore = 0.0;
|
||||
} else {
|
||||
// Discard left slice
|
||||
leftScore = 0.0;
|
||||
left = left + slice;
|
||||
}
|
||||
width = width - slice;
|
||||
}
|
||||
}
|
||||
if (inHeight > outHeight) {
|
||||
// Reduce height by repeated removing slices from edge with lowest score
|
||||
int height = inHeight;
|
||||
double topScore = 0.0;
|
||||
double bottomScore = 0.0;
|
||||
// Max height of each slice
|
||||
int const maxSliceHeight = static_cast<int>(ceil((inHeight - outHeight) / 8.0));
|
||||
while (height > outHeight) {
|
||||
// Height of current slice
|
||||
int const slice = std::min(height - outHeight, maxSliceHeight);
|
||||
if (topScore == 0.0) {
|
||||
// Update score of top slice
|
||||
topScore = strategy(image.extract_area(0, top, inWidth, slice));
|
||||
}
|
||||
if (bottomScore == 0.0) {
|
||||
// Update score of bottom slice
|
||||
bottomScore = strategy(image.extract_area(0, height - slice - 1, inWidth, slice));
|
||||
}
|
||||
// Keep slice with highest score
|
||||
if (topScore >= bottomScore) {
|
||||
// Discard bottom slice
|
||||
bottomScore = 0.0;
|
||||
} else {
|
||||
// Discard top slice
|
||||
topScore = 0.0;
|
||||
top = top + slice;
|
||||
}
|
||||
height = height - slice;
|
||||
}
|
||||
}
|
||||
return std::make_tuple(left, top);
|
||||
}
|
||||
|
||||
/*
|
||||
Insert a tile cache to prevent over-computation of any previous operations in the pipeline
|
||||
*/
|
||||
@@ -415,12 +281,11 @@ namespace sharp {
|
||||
->set("tile_height", 10)
|
||||
->set("max_tiles", static_cast<int>(round(1.0 + need_lines / 10.0)))
|
||||
->set("access", VIPS_ACCESS_SEQUENTIAL)
|
||||
->set("threaded", TRUE)
|
||||
);
|
||||
->set("threaded", TRUE));
|
||||
}
|
||||
|
||||
VImage Threshold(VImage image, double const threshold, bool const thresholdGrayscale) {
|
||||
if(!thresholdGrayscale) {
|
||||
if (!thresholdGrayscale) {
|
||||
return image >= threshold;
|
||||
}
|
||||
return image.colourspace(VIPS_INTERPRETATION_B_W) >= threshold;
|
||||
@@ -485,7 +350,7 @@ namespace sharp {
|
||||
int width = right - left;
|
||||
int height = bottom - top;
|
||||
|
||||
if(width <= 0 || height <= 0) {
|
||||
if (width <= 0 || height <= 0) {
|
||||
throw VError("Unexpected error while trimming. Try to lower the tolerance");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,17 @@
|
||||
// Copyright 2013, 2014, 2015, 2016, 2017 Lovell Fuller and contributors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef SRC_OPERATIONS_H_
|
||||
#define SRC_OPERATIONS_H_
|
||||
|
||||
@@ -18,20 +32,14 @@ namespace sharp {
|
||||
VImage Composite(VImage src, VImage dst, const int gravity);
|
||||
|
||||
/*
|
||||
Alpha composite src over dst with given x and y offsets.
|
||||
Assumes alpha channels are already premultiplied and will be unpremultiplied after.
|
||||
Composite overlayImage over image at given position
|
||||
*/
|
||||
VImage Composite(VImage src, VImage dst, const int x, const int y);
|
||||
VImage Composite(VImage image, VImage overlayImage, int const x, int const y);
|
||||
|
||||
/*
|
||||
Check if the src and dst Images for composition operation are valid
|
||||
Alpha composite overlayImage over image, assumes matching dimensions
|
||||
*/
|
||||
bool IsInputValidForComposition(VImage src, VImage dst);
|
||||
|
||||
/*
|
||||
Given a valid src and dst, returns the composite of the two images
|
||||
*/
|
||||
VImage CompositeImage(VImage src, VImage dst);
|
||||
VImage AlphaComposite(VImage image, VImage overlayImage);
|
||||
|
||||
/*
|
||||
Cutout src over dst with given gravity.
|
||||
@@ -64,23 +72,6 @@ namespace sharp {
|
||||
*/
|
||||
VImage Sharpen(VImage image, double const sigma, double const flat, double const jagged);
|
||||
|
||||
/*
|
||||
Crop strategy functors
|
||||
*/
|
||||
struct EntropyStrategy {
|
||||
double operator()(VImage image);
|
||||
};
|
||||
struct AttentionStrategy {
|
||||
double operator()(VImage image);
|
||||
};
|
||||
|
||||
/*
|
||||
Calculate crop area based on given strategy (Entropy, Attention)
|
||||
*/
|
||||
std::tuple<int, int> Crop(
|
||||
VImage image, int const outWidth, int const outHeight, std::function<double(VImage)> strategy
|
||||
);
|
||||
|
||||
/*
|
||||
Insert a tile cache to prevent over-computation of any previous operations in the pipeline
|
||||
*/
|
||||
|
||||
461
src/pipeline.cc
@@ -1,15 +1,31 @@
|
||||
// Copyright 2013, 2014, 2015, 2016, 2017 Lovell Fuller and contributors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <numeric>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <vips/vips8>
|
||||
#include <node.h>
|
||||
#include <nan.h>
|
||||
|
||||
#include "nan.h"
|
||||
#include "common.h"
|
||||
#include "operations.h"
|
||||
#include "pipeline.h"
|
||||
@@ -17,16 +33,16 @@
|
||||
class PipelineWorker : public Nan::AsyncWorker {
|
||||
public:
|
||||
PipelineWorker(
|
||||
Nan::Callback *callback, PipelineBaton *baton, Nan::Callback *queueListener,
|
||||
std::vector<v8::Local<v8::Object>> const buffersToPersist
|
||||
) : Nan::AsyncWorker(callback), baton(baton), queueListener(queueListener), buffersToPersist(buffersToPersist) {
|
||||
Nan::Callback *callback, PipelineBaton *baton, Nan::Callback *debuglog, Nan::Callback *queueListener,
|
||||
std::vector<v8::Local<v8::Object>> const buffersToPersist) :
|
||||
Nan::AsyncWorker(callback), baton(baton), debuglog(debuglog), queueListener(queueListener),
|
||||
buffersToPersist(buffersToPersist) {
|
||||
// Protect Buffer objects from GC, keyed on index
|
||||
std::accumulate(buffersToPersist.begin(), buffersToPersist.end(), 0,
|
||||
[this](uint32_t index, v8::Local<v8::Object> const buffer) -> uint32_t {
|
||||
SaveToPersistent(index, buffer);
|
||||
return index + 1;
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
~PipelineWorker() {}
|
||||
|
||||
@@ -65,16 +81,15 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
|
||||
// Calculate angle of rotation
|
||||
VipsAngle rotation;
|
||||
bool flip;
|
||||
bool flop;
|
||||
std::tie(rotation, flip, flop) = CalculateRotationAndFlip(baton->angle, image);
|
||||
if (flip && !baton->flip) {
|
||||
// Add flip operation due to EXIF mirroring
|
||||
baton->flip = TRUE;
|
||||
}
|
||||
if (flop && !baton->flop) {
|
||||
// Add flip operation due to EXIF mirroring
|
||||
baton->flop = TRUE;
|
||||
if (baton->useExifOrientation) {
|
||||
// Rotate and flip image according to Exif orientation
|
||||
bool flip;
|
||||
bool flop;
|
||||
std::tie(rotation, flip, flop) = CalculateExifRotationAndFlip(sharp::ExifOrientation(image));
|
||||
baton->flip = baton->flip || flip;
|
||||
baton->flop = baton->flop || flop;
|
||||
} else {
|
||||
rotation = CalculateAngleRotation(baton->angle);
|
||||
}
|
||||
|
||||
// Rotate pre-extract
|
||||
@@ -84,7 +99,7 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
}
|
||||
|
||||
// Trim
|
||||
if(baton->trimTolerance != 0) {
|
||||
if (baton->trimTolerance != 0) {
|
||||
image = sharp::Trim(image, baton->trimTolerance);
|
||||
}
|
||||
|
||||
@@ -205,12 +220,12 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
}
|
||||
|
||||
// If integral x and y shrink are equal, try to use shrink-on-load for JPEG and WebP,
|
||||
// but not when applying gamma correction or pre-resize extract
|
||||
// but not when applying gamma correction, pre-resize extract or trim
|
||||
int shrink_on_load = 1;
|
||||
if (
|
||||
xshrink == yshrink && xshrink >= 2 &&
|
||||
(inputImageType == ImageType::JPEG || inputImageType == ImageType::WEBP) &&
|
||||
baton->gamma == 0 && baton->topOffsetPre == -1
|
||||
baton->gamma == 0 && baton->topOffsetPre == -1 && baton->trimTolerance == 0
|
||||
) {
|
||||
if (xshrink >= 8) {
|
||||
xfactor = xfactor / 8;
|
||||
@@ -226,6 +241,12 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
shrink_on_load = 2;
|
||||
}
|
||||
}
|
||||
// Help ensure a final kernel-based reduction to prevent shrink aliasing
|
||||
if (shrink_on_load > 1 && (xresidual == 1.0 || yresidual == 1.0)) {
|
||||
shrink_on_load = shrink_on_load / 2;
|
||||
xfactor = xfactor * 2;
|
||||
yfactor = yfactor * 2;
|
||||
}
|
||||
if (shrink_on_load > 1) {
|
||||
// Reload input using shrink-on-load
|
||||
vips::VOption *option = VImage::option()->set("shrink", shrink_on_load);
|
||||
@@ -269,6 +290,13 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
std::swap(xresidual, yresidual);
|
||||
}
|
||||
}
|
||||
// Help ensure a final kernel-based reduction to prevent shrink aliasing
|
||||
if (xshrink > 1 && yshrink > 1 && (xresidual == 1.0 || yresidual == 1.0)) {
|
||||
xshrink = xshrink / 2;
|
||||
yshrink = yshrink / 2;
|
||||
xresidual = static_cast<double>(xshrink) / xfactor;
|
||||
yresidual = static_cast<double>(yshrink) / yfactor;
|
||||
}
|
||||
|
||||
// Ensure we're using a device-independent colour space
|
||||
if (sharp::HasProfile(image)) {
|
||||
@@ -277,8 +305,7 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
image = image.icc_transform(
|
||||
const_cast<char*>(profileMap[VIPS_INTERPRETATION_sRGB].data()), VImage::option()
|
||||
->set("embedded", TRUE)
|
||||
->set("intent", VIPS_INTENT_PERCEPTUAL)
|
||||
);
|
||||
->set("intent", VIPS_INTENT_PERCEPTUAL));
|
||||
} catch(...) {
|
||||
// Ignore failure of embedded profile
|
||||
}
|
||||
@@ -286,8 +313,7 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
image = image.icc_transform(
|
||||
const_cast<char*>(profileMap[VIPS_INTERPRETATION_sRGB].data()), VImage::option()
|
||||
->set("input_profile", profileMap[VIPS_INTERPRETATION_CMYK].data())
|
||||
->set("intent", VIPS_INTENT_PERCEPTUAL)
|
||||
);
|
||||
->set("intent", VIPS_INTENT_PERCEPTUAL));
|
||||
}
|
||||
|
||||
// Flatten image to remove alpha channel
|
||||
@@ -301,8 +327,7 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
baton->background[2] * multiplier
|
||||
};
|
||||
image = image.flatten(VImage::option()
|
||||
->set("background", background)
|
||||
);
|
||||
->set("background", background));
|
||||
}
|
||||
|
||||
// Negate the colours in the image
|
||||
@@ -320,13 +345,20 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
image = image.colourspace(VIPS_INTERPRETATION_B_W);
|
||||
}
|
||||
|
||||
// Ensure image has an alpha channel when there is an overlay
|
||||
bool hasOverlay = baton->overlay != nullptr;
|
||||
if (hasOverlay && !HasAlpha(image)) {
|
||||
double const multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0;
|
||||
image = image.bandjoin(
|
||||
VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier)
|
||||
);
|
||||
// Ensure image has an alpha channel when there is an overlay with an alpha channel
|
||||
VImage overlayImage;
|
||||
ImageType overlayImageType = ImageType::UNKNOWN;
|
||||
bool shouldOverlayWithAlpha = FALSE;
|
||||
if (baton->overlay != nullptr) {
|
||||
std::tie(overlayImage, overlayImageType) = OpenInput(baton->overlay, baton->accessMethod);
|
||||
if (HasAlpha(overlayImage)) {
|
||||
shouldOverlayWithAlpha = !baton->overlayCutout;
|
||||
if (!HasAlpha(image)) {
|
||||
double const multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0;
|
||||
image = image.bandjoin(
|
||||
VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool const shouldShrink = xshrink > 1 || yshrink > 1;
|
||||
@@ -334,9 +366,8 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
bool const shouldBlur = baton->blurSigma != 0.0;
|
||||
bool const shouldConv = baton->convKernelWidth * baton->convKernelHeight > 0;
|
||||
bool const shouldSharpen = baton->sharpenSigma != 0.0;
|
||||
bool const shouldCutout = baton->overlayCutout;
|
||||
bool const shouldPremultiplyAlpha = HasAlpha(image) &&
|
||||
(shouldShrink || shouldReduce || shouldBlur || shouldConv || shouldSharpen || (hasOverlay && !shouldCutout));
|
||||
(shouldShrink || shouldReduce || shouldBlur || shouldConv || shouldSharpen || shouldOverlayWithAlpha);
|
||||
|
||||
// Premultiply image alpha channel before all transformations to avoid
|
||||
// dark fringing around bright pixels
|
||||
@@ -380,36 +411,42 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
// Perform kernel-based reduction
|
||||
if (yresidual < 1.0 || xresidual < 1.0) {
|
||||
VipsKernel kernel = static_cast<VipsKernel>(
|
||||
vips_enum_from_nick(nullptr, VIPS_TYPE_KERNEL, baton->kernel.data())
|
||||
);
|
||||
if (kernel != VIPS_KERNEL_CUBIC && kernel != VIPS_KERNEL_LANCZOS2 && kernel != VIPS_KERNEL_LANCZOS3) {
|
||||
vips_enum_from_nick(nullptr, VIPS_TYPE_KERNEL, baton->kernel.data()));
|
||||
if (
|
||||
kernel != VIPS_KERNEL_NEAREST && kernel != VIPS_KERNEL_CUBIC && kernel != VIPS_KERNEL_LANCZOS2 &&
|
||||
kernel != VIPS_KERNEL_LANCZOS3
|
||||
) {
|
||||
throw vips::VError("Unknown kernel");
|
||||
}
|
||||
if (yresidual < 1.0) {
|
||||
image = image.reducev(1.0 / yresidual, VImage::option()
|
||||
->set("kernel", kernel)
|
||||
->set("centre", baton->centreSampling)
|
||||
);
|
||||
->set("centre", baton->centreSampling));
|
||||
}
|
||||
if (xresidual < 1.0) {
|
||||
image = image.reduceh(1.0 / xresidual, VImage::option()
|
||||
->set("kernel", kernel)
|
||||
->set("centre", baton->centreSampling)
|
||||
);
|
||||
->set("centre", baton->centreSampling));
|
||||
}
|
||||
}
|
||||
// Perform affine enlargement
|
||||
// Perform enlargement
|
||||
if (yresidual > 1.0 || xresidual > 1.0) {
|
||||
vips::VInterpolate interpolator = vips::VInterpolate::new_from_name(baton->interpolator.data());
|
||||
if (yresidual > 1.0) {
|
||||
image = image.affine({1.0, 0.0, 0.0, yresidual}, VImage::option()
|
||||
->set("interpolate", interpolator)
|
||||
);
|
||||
}
|
||||
if (xresidual > 1.0) {
|
||||
image = image.affine({xresidual, 0.0, 0.0, 1.0}, VImage::option()
|
||||
->set("interpolate", interpolator)
|
||||
);
|
||||
if (trunc(xresidual) == xresidual && trunc(yresidual) == yresidual && baton->interpolator == "nearest") {
|
||||
// Fast, integral nearest neighbour enlargement
|
||||
image = image.zoom(static_cast<int>(xresidual), static_cast<int>(yresidual));
|
||||
} else {
|
||||
// Floating point affine transformation
|
||||
vips::VInterpolate interpolator = vips::VInterpolate::new_from_name(baton->interpolator.data());
|
||||
if (yresidual > 1.0 && xresidual > 1.0) {
|
||||
image = image.affine({xresidual, 0.0, 0.0, yresidual}, VImage::option()
|
||||
->set("interpolate", interpolator));
|
||||
} else if (yresidual > 1.0) {
|
||||
image = image.affine({1.0, 0.0, 0.0, yresidual}, VImage::option()
|
||||
->set("interpolate", interpolator));
|
||||
} else if (xresidual > 1.0) {
|
||||
image = image.affine({xresidual, 0.0, 0.0, 1.0}, VImage::option()
|
||||
->set("interpolate", interpolator));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -433,13 +470,12 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
}
|
||||
|
||||
// Join additional color channels to the image
|
||||
if(baton->joinChannelIn.size() > 0) {
|
||||
if (baton->joinChannelIn.size() > 0) {
|
||||
VImage joinImage;
|
||||
ImageType joinImageType = ImageType::UNKNOWN;
|
||||
|
||||
for(unsigned int i = 0; i < baton->joinChannelIn.size(); i++) {
|
||||
for (unsigned int i = 0; i < baton->joinChannelIn.size(); i++) {
|
||||
std::tie(joinImage, joinImageType) = sharp::OpenInput(baton->joinChannelIn[i], baton->accessMethod);
|
||||
|
||||
image = image.bandjoin(joinImage);
|
||||
}
|
||||
image = image.copy(VImage::option()->set("interpretation", baton->colourspace));
|
||||
@@ -463,8 +499,8 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
background = { multiplier * (
|
||||
0.2126 * baton->background[0] +
|
||||
0.7152 * baton->background[1] +
|
||||
0.0722 * baton->background[2]
|
||||
)};
|
||||
0.0722 * baton->background[2])
|
||||
};
|
||||
}
|
||||
// Add alpha channel to background colour
|
||||
if (baton->background[3] < 255.0 || HasAlpha(image)) {
|
||||
@@ -475,45 +511,37 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
// Add non-transparent alpha channel, if required
|
||||
if (baton->background[3] < 255.0 && !HasAlpha(image)) {
|
||||
image = image.bandjoin(
|
||||
VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier)
|
||||
);
|
||||
VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier));
|
||||
}
|
||||
// Embed
|
||||
int left = static_cast<int>(round((baton->width - image.width()) / 2));
|
||||
int top = static_cast<int>(round((baton->height - image.height()) / 2));
|
||||
image = image.embed(left, top, baton->width, baton->height, VImage::option()
|
||||
->set("extend", VIPS_EXTEND_BACKGROUND)
|
||||
->set("background", background)
|
||||
);
|
||||
->set("background", background));
|
||||
} else if (baton->canvas != Canvas::IGNORE_ASPECT) {
|
||||
// Crop/max/min
|
||||
int left;
|
||||
int top;
|
||||
if (baton->crop < 9) {
|
||||
// Gravity-based crop
|
||||
int left;
|
||||
int top;
|
||||
std::tie(left, top) = sharp::CalculateCrop(
|
||||
image.width(), image.height(), baton->width, baton->height, baton->crop
|
||||
);
|
||||
} else if (baton->crop == 16) {
|
||||
// Entropy-based crop
|
||||
std::tie(left, top) = sharp::Crop(image, baton->width, baton->height, sharp::EntropyStrategy());
|
||||
image.width(), image.height(), baton->width, baton->height, baton->crop);
|
||||
int width = std::min(image.width(), baton->width);
|
||||
int height = std::min(image.height(), baton->height);
|
||||
image = image.extract_area(left, top, width, height);
|
||||
} else {
|
||||
// Attention-based crop
|
||||
std::tie(left, top) = sharp::Crop(image, baton->width, baton->height, sharp::AttentionStrategy());
|
||||
// Attention-based or Entropy-based crop
|
||||
image = image.smartcrop(baton->width, baton->height, VImage::option()
|
||||
->set("interesting", baton->crop == 16 ? VIPS_INTERESTING_ENTROPY : VIPS_INTERESTING_ATTENTION));
|
||||
}
|
||||
int width = std::min(image.width(), baton->width);
|
||||
int height = std::min(image.height(), baton->height);
|
||||
image = image.extract_area(left, top, width, height);
|
||||
baton->cropCalcLeft = left;
|
||||
baton->cropCalcTop = top;
|
||||
}
|
||||
}
|
||||
|
||||
// Post extraction
|
||||
if (baton->topOffsetPost != -1) {
|
||||
image = image.extract_area(
|
||||
baton->leftOffsetPost, baton->topOffsetPost, baton->widthPost, baton->heightPost
|
||||
);
|
||||
baton->leftOffsetPost, baton->topOffsetPost, baton->widthPost, baton->heightPost);
|
||||
}
|
||||
|
||||
// Extend edges
|
||||
@@ -533,8 +561,8 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
background = { multiplier * (
|
||||
0.2126 * baton->background[0] +
|
||||
0.7152 * baton->background[1] +
|
||||
0.0722 * baton->background[2]
|
||||
)};
|
||||
0.0722 * baton->background[2])
|
||||
};
|
||||
}
|
||||
// Add alpha channel to background colour
|
||||
if (baton->background[3] < 255.0 || HasAlpha(image)) {
|
||||
@@ -545,8 +573,7 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
// Add non-transparent alpha channel, if required
|
||||
if (baton->background[3] < 255.0 && !HasAlpha(image)) {
|
||||
image = image.bandjoin(
|
||||
VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier)
|
||||
);
|
||||
VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier));
|
||||
}
|
||||
// Embed
|
||||
baton->width = image.width() + baton->extendLeft + baton->extendRight;
|
||||
@@ -571,8 +598,7 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
image = sharp::Convolve(image,
|
||||
baton->convKernelWidth, baton->convKernelHeight,
|
||||
baton->convKernelScale, baton->convKernelOffset,
|
||||
baton->convKernel
|
||||
);
|
||||
baton->convKernel);
|
||||
}
|
||||
|
||||
// Sharpen
|
||||
@@ -581,10 +607,11 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
}
|
||||
|
||||
// Composite with overlay, if present
|
||||
if (hasOverlay) {
|
||||
VImage overlayImage;
|
||||
ImageType overlayImageType = ImageType::UNKNOWN;
|
||||
std::tie(overlayImage, overlayImageType) = OpenInput(baton->overlay, baton->accessMethod);
|
||||
if (baton->overlay != nullptr) {
|
||||
// Verify overlay image is within current dimensions
|
||||
if (overlayImage.width() > image.width() || overlayImage.height() > image.height()) {
|
||||
throw vips::VError("Overlay image must have same dimensions or smaller");
|
||||
}
|
||||
// Check if overlay is tiled
|
||||
if (baton->overlayTile) {
|
||||
int const overlayImageWidth = overlayImage.width();
|
||||
@@ -606,48 +633,45 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
// the overlayX/YOffsets will now be used to CalculateCrop for extract_area
|
||||
std::tie(left, top) = sharp::CalculateCrop(
|
||||
overlayImage.width(), overlayImage.height(), image.width(), image.height(),
|
||||
baton->overlayXOffset, baton->overlayYOffset
|
||||
);
|
||||
baton->overlayXOffset, baton->overlayYOffset);
|
||||
} else {
|
||||
// the overlayGravity will now be used to CalculateCrop for extract_area
|
||||
std::tie(left, top) = sharp::CalculateCrop(
|
||||
overlayImage.width(), overlayImage.height(), image.width(), image.height(), baton->overlayGravity
|
||||
);
|
||||
overlayImage.width(), overlayImage.height(), image.width(), image.height(), baton->overlayGravity);
|
||||
}
|
||||
overlayImage = overlayImage.extract_area(
|
||||
left, top, image.width(), image.height()
|
||||
);
|
||||
overlayImage = overlayImage.extract_area(left, top, image.width(), image.height());
|
||||
}
|
||||
// the overlayGravity was used for extract_area, therefore set it back to its default value of 0
|
||||
baton->overlayGravity = 0;
|
||||
}
|
||||
if (shouldCutout) {
|
||||
if (baton->overlayCutout) {
|
||||
// 'cut out' the image, premultiplication is not required
|
||||
image = sharp::Cutout(overlayImage, image, baton->overlayGravity);
|
||||
} else {
|
||||
// Ensure overlay has alpha channel
|
||||
if (!HasAlpha(overlayImage)) {
|
||||
double const multiplier = sharp::Is16Bit(overlayImage.interpretation()) ? 256.0 : 1.0;
|
||||
overlayImage = overlayImage.bandjoin(
|
||||
VImage::new_matrix(overlayImage.width(), overlayImage.height()).new_from_image(255 * multiplier)
|
||||
);
|
||||
// Ensure overlay is sRGB
|
||||
overlayImage = overlayImage.colourspace(VIPS_INTERPRETATION_sRGB);
|
||||
// Ensure overlay matches premultiplication state
|
||||
if (shouldPremultiplyAlpha) {
|
||||
// Ensure overlay has alpha channel
|
||||
if (!HasAlpha(overlayImage)) {
|
||||
double const multiplier = sharp::Is16Bit(overlayImage.interpretation()) ? 256.0 : 1.0;
|
||||
overlayImage = overlayImage.bandjoin(
|
||||
VImage::new_matrix(overlayImage.width(), overlayImage.height()).new_from_image(255 * multiplier));
|
||||
}
|
||||
overlayImage = overlayImage.premultiply();
|
||||
}
|
||||
// Ensure image has alpha channel
|
||||
if (!HasAlpha(image)) {
|
||||
double const multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0;
|
||||
image = image.bandjoin(
|
||||
VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier)
|
||||
);
|
||||
}
|
||||
// Ensure overlay is premultiplied sRGB
|
||||
overlayImage = overlayImage.colourspace(VIPS_INTERPRETATION_sRGB).premultiply();
|
||||
int left;
|
||||
int top;
|
||||
if (baton->overlayXOffset >= 0 && baton->overlayYOffset >= 0) {
|
||||
// Composite images with given offsets
|
||||
image = sharp::Composite(overlayImage, image, baton->overlayXOffset, baton->overlayYOffset);
|
||||
// Composite images at given offsets
|
||||
std::tie(left, top) = sharp::CalculateCrop(image.width(), image.height(),
|
||||
overlayImage.width(), overlayImage.height(), baton->overlayXOffset, baton->overlayYOffset);
|
||||
} else {
|
||||
// Composite images with given gravity
|
||||
image = sharp::Composite(overlayImage, image, baton->overlayGravity);
|
||||
std::tie(left, top) = sharp::CalculateCrop(image.width(), image.height(),
|
||||
overlayImage.width(), overlayImage.height(), baton->overlayGravity);
|
||||
}
|
||||
image = sharp::Composite(image, overlayImage, left, top);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -661,6 +685,7 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
image = image.cast(VIPS_FORMAT_UCHAR);
|
||||
}
|
||||
}
|
||||
baton->premultiplied = shouldPremultiplyAlpha;
|
||||
|
||||
// Gamma decoding (brighten)
|
||||
if (baton->gamma >= 1 && baton->gamma <= 3) {
|
||||
@@ -686,8 +711,8 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
}
|
||||
|
||||
// Extract an image channel (aka vips band)
|
||||
if(baton->extractChannel > -1) {
|
||||
if(baton->extractChannel >= image.bands()) {
|
||||
if (baton->extractChannel > -1) {
|
||||
if (baton->extractChannel >= image.bands()) {
|
||||
(baton->err).append("Cannot extract channel from image. Too few channels in image.");
|
||||
return Error();
|
||||
}
|
||||
@@ -701,12 +726,9 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
// Convert colourspace, pass the current known interpretation so libvips doesn't have to guess
|
||||
image = image.colourspace(baton->colourspace, VImage::option()->set("source_space", image.interpretation()));
|
||||
// Transform colours from embedded profile to output profile
|
||||
if (baton->withMetadata &&
|
||||
sharp::HasProfile(image) &&
|
||||
profileMap[baton->colourspace] != std::string()) {
|
||||
if (baton->withMetadata && sharp::HasProfile(image) && profileMap[baton->colourspace] != std::string()) {
|
||||
image = image.icc_transform(const_cast<char*>(profileMap[baton->colourspace].data()),
|
||||
VImage::option()->set("embedded", TRUE)
|
||||
);
|
||||
VImage::option()->set("embedded", TRUE));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -720,10 +742,11 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
baton->width = image.width();
|
||||
baton->height = image.height();
|
||||
// Output
|
||||
if (baton->fileOut == "") {
|
||||
if (baton->fileOut.empty()) {
|
||||
// Buffer output
|
||||
if (baton->formatOut == "jpeg" || (baton->formatOut == "input" && inputImageType == ImageType::JPEG)) {
|
||||
// Write JPEG to buffer
|
||||
sharp::AssertImageTypeDimensions(image, ImageType::JPEG);
|
||||
VipsArea *area = VIPS_AREA(image.jpegsave_buffer(VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("Q", baton->jpegQuality)
|
||||
@@ -732,31 +755,29 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
->set("trellis_quant", baton->jpegTrellisQuantisation)
|
||||
->set("overshoot_deringing", baton->jpegOvershootDeringing)
|
||||
->set("optimize_scans", baton->jpegOptimiseScans)
|
||||
->set("optimize_coding", TRUE)
|
||||
));
|
||||
->set("optimize_coding", TRUE)));
|
||||
baton->bufferOut = static_cast<char*>(area->data);
|
||||
baton->bufferOutLength = area->length;
|
||||
area->free_fn = nullptr;
|
||||
vips_area_unref(area);
|
||||
baton->formatOut = "jpeg";
|
||||
if(baton->colourspace == VIPS_INTERPRETATION_CMYK) {
|
||||
if (baton->colourspace == VIPS_INTERPRETATION_CMYK) {
|
||||
baton->channels = std::min(baton->channels, 4);
|
||||
} else {
|
||||
baton->channels = std::min(baton->channels, 3);
|
||||
}
|
||||
} else if (baton->formatOut == "png" || (baton->formatOut == "input" &&
|
||||
(inputImageType == ImageType::PNG || inputImageType == ImageType::GIF || inputImageType == ImageType::SVG))) {
|
||||
// Write PNG to buffer
|
||||
sharp::AssertImageTypeDimensions(image, ImageType::PNG);
|
||||
// Strip profile
|
||||
if (!baton->withMetadata) {
|
||||
vips_image_remove(image.get_image(), VIPS_META_ICC_NAME);
|
||||
}
|
||||
// Write PNG to buffer
|
||||
VipsArea *area = VIPS_AREA(image.pngsave_buffer(VImage::option()
|
||||
->set("interlace", baton->pngProgressive)
|
||||
->set("compression", baton->pngCompressionLevel)
|
||||
->set("filter", baton->pngAdaptiveFiltering ?
|
||||
VIPS_FOREIGN_PNG_FILTER_ALL : VIPS_FOREIGN_PNG_FILTER_NONE )
|
||||
));
|
||||
->set("filter", baton->pngAdaptiveFiltering ? VIPS_FOREIGN_PNG_FILTER_ALL : VIPS_FOREIGN_PNG_FILTER_NONE)));
|
||||
baton->bufferOut = static_cast<char*>(area->data);
|
||||
baton->bufferOutLength = area->length;
|
||||
area->free_fn = nullptr;
|
||||
@@ -764,15 +785,41 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
baton->formatOut = "png";
|
||||
} else if (baton->formatOut == "webp" || (baton->formatOut == "input" && inputImageType == ImageType::WEBP)) {
|
||||
// Write WEBP to buffer
|
||||
sharp::AssertImageTypeDimensions(image, ImageType::WEBP);
|
||||
VipsArea *area = VIPS_AREA(image.webpsave_buffer(VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("Q", baton->webpQuality)
|
||||
));
|
||||
->set("lossless", baton->webpLossless)
|
||||
->set("near_lossless", baton->webpNearLossless)
|
||||
->set("alpha_q", baton->webpAlphaQuality)));
|
||||
baton->bufferOut = static_cast<char*>(area->data);
|
||||
baton->bufferOutLength = area->length;
|
||||
area->free_fn = nullptr;
|
||||
vips_area_unref(area);
|
||||
baton->formatOut = "webp";
|
||||
} else if (baton->formatOut == "tiff" || (baton->formatOut == "input" && inputImageType == ImageType::TIFF)) {
|
||||
// Write TIFF to buffer
|
||||
if (baton->tiffCompression == VIPS_FOREIGN_TIFF_COMPRESSION_JPEG) {
|
||||
sharp::AssertImageTypeDimensions(image, ImageType::JPEG);
|
||||
}
|
||||
// Cast pixel values to float, if required
|
||||
if (baton->tiffPredictor == VIPS_FOREIGN_TIFF_PREDICTOR_FLOAT) {
|
||||
image = image.cast(VIPS_FORMAT_FLOAT);
|
||||
}
|
||||
VipsArea *area = VIPS_AREA(image.tiffsave_buffer(VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("Q", baton->tiffQuality)
|
||||
->set("squash", baton->tiffSquash)
|
||||
->set("compression", baton->tiffCompression)
|
||||
->set("predictor", baton->tiffPredictor)
|
||||
->set("xres", baton->tiffXres)
|
||||
->set("yres", baton->tiffYres)));
|
||||
baton->bufferOut = static_cast<char*>(area->data);
|
||||
baton->bufferOutLength = area->length;
|
||||
area->free_fn = nullptr;
|
||||
vips_area_unref(area);
|
||||
baton->formatOut = "tiff";
|
||||
baton->channels = std::min(baton->channels, 3);
|
||||
} else if (baton->formatOut == "raw" || (baton->formatOut == "input" && inputImageType == ImageType::RAW)) {
|
||||
// Write raw, uncompressed image data to buffer
|
||||
if (baton->greyscale || image.interpretation() == VIPS_INTERPRETATION_B_W) {
|
||||
@@ -813,6 +860,7 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
!(isJpeg || isPng || isWebp || isTiff || isDz || isDzZip || isV);
|
||||
if (baton->formatOut == "jpeg" || isJpeg || (matchInput && inputImageType == ImageType::JPEG)) {
|
||||
// Write JPEG to file
|
||||
sharp::AssertImageTypeDimensions(image, ImageType::JPEG);
|
||||
image.jpegsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("Q", baton->jpegQuality)
|
||||
@@ -821,38 +869,49 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
->set("trellis_quant", baton->jpegTrellisQuantisation)
|
||||
->set("overshoot_deringing", baton->jpegOvershootDeringing)
|
||||
->set("optimize_scans", baton->jpegOptimiseScans)
|
||||
->set("optimize_coding", TRUE)
|
||||
);
|
||||
->set("optimize_coding", TRUE));
|
||||
baton->formatOut = "jpeg";
|
||||
baton->channels = std::min(baton->channels, 3);
|
||||
} else if (baton->formatOut == "png" || isPng || (matchInput &&
|
||||
(inputImageType == ImageType::PNG || inputImageType == ImageType::GIF || inputImageType == ImageType::SVG))) {
|
||||
// Write PNG to file
|
||||
sharp::AssertImageTypeDimensions(image, ImageType::PNG);
|
||||
// Strip profile
|
||||
if (!baton->withMetadata) {
|
||||
vips_image_remove(image.get_image(), VIPS_META_ICC_NAME);
|
||||
}
|
||||
// Write PNG to file
|
||||
image.pngsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
|
||||
->set("interlace", baton->pngProgressive)
|
||||
->set("compression", baton->pngCompressionLevel)
|
||||
->set("filter", baton->pngAdaptiveFiltering ?
|
||||
VIPS_FOREIGN_PNG_FILTER_ALL : VIPS_FOREIGN_PNG_FILTER_NONE )
|
||||
);
|
||||
->set("filter", baton->pngAdaptiveFiltering ? VIPS_FOREIGN_PNG_FILTER_ALL : VIPS_FOREIGN_PNG_FILTER_NONE));
|
||||
baton->formatOut = "png";
|
||||
} else if (baton->formatOut == "webp" || isWebp || (matchInput && inputImageType == ImageType::WEBP)) {
|
||||
// Write WEBP to file
|
||||
AssertImageTypeDimensions(image, ImageType::WEBP);
|
||||
image.webpsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("Q", baton->webpQuality)
|
||||
);
|
||||
->set("lossless", baton->webpLossless)
|
||||
->set("near_lossless", baton->webpNearLossless)
|
||||
->set("alpha_q", baton->webpAlphaQuality));
|
||||
baton->formatOut = "webp";
|
||||
} else if (baton->formatOut == "tiff" || isTiff || (matchInput && inputImageType == ImageType::TIFF)) {
|
||||
// Write TIFF to file
|
||||
if (baton->tiffCompression == VIPS_FOREIGN_TIFF_COMPRESSION_JPEG) {
|
||||
sharp::AssertImageTypeDimensions(image, ImageType::JPEG);
|
||||
}
|
||||
// Cast pixel values to float, if required
|
||||
if (baton->tiffPredictor == VIPS_FOREIGN_TIFF_PREDICTOR_FLOAT) {
|
||||
image = image.cast(VIPS_FORMAT_FLOAT);
|
||||
}
|
||||
image.tiffsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("Q", baton->tiffQuality)
|
||||
->set("compression", VIPS_FOREIGN_TIFF_COMPRESSION_JPEG)
|
||||
);
|
||||
->set("squash", baton->tiffSquash)
|
||||
->set("compression", baton->tiffCompression)
|
||||
->set("predictor", baton->tiffPredictor)
|
||||
->set("xres", baton->tiffXres)
|
||||
->set("yres", baton->tiffYres));
|
||||
baton->formatOut = "tiff";
|
||||
baton->channels = std::min(baton->channels, 3);
|
||||
} else if (baton->formatOut == "dz" || isDz || isDzZip) {
|
||||
@@ -870,7 +929,10 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
suffix = AssembleSuffixString(".png", options);
|
||||
} else if (baton->tileFormat == "webp") {
|
||||
std::vector<std::pair<std::string, std::string>> options {
|
||||
{"Q", std::to_string(baton->webpQuality)}
|
||||
{"Q", std::to_string(baton->webpQuality)},
|
||||
{"alpha_q", std::to_string(baton->webpAlphaQuality)},
|
||||
{"lossless", baton->webpLossless ? "TRUE" : "FALSE"},
|
||||
{"near_lossless", baton->webpNearLossless ? "TRUE" : "FALSE"}
|
||||
};
|
||||
suffix = AssembleSuffixString(".webp", options);
|
||||
} else {
|
||||
@@ -895,14 +957,12 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
->set("overlap", baton->tileOverlap)
|
||||
->set("container", baton->tileContainer)
|
||||
->set("layout", baton->tileLayout)
|
||||
->set("suffix", const_cast<char*>(suffix.data()))
|
||||
);
|
||||
->set("suffix", const_cast<char*>(suffix.data())));
|
||||
baton->formatOut = "dz";
|
||||
} else if (baton->formatOut == "v" || isV || (matchInput && inputImageType == ImageType::VIPS)) {
|
||||
// Write V to file
|
||||
image.vipssave(const_cast<char*>(baton->fileOut.data()), VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
);
|
||||
->set("strip", !baton->withMetadata));
|
||||
baton->formatOut = "v";
|
||||
} else {
|
||||
// Unsupported output format
|
||||
@@ -918,7 +978,7 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
vips_thread_shutdown();
|
||||
}
|
||||
|
||||
void HandleOKCallback () {
|
||||
void HandleOKCallback() {
|
||||
using Nan::New;
|
||||
using Nan::Set;
|
||||
Nan::HandleScope();
|
||||
@@ -944,6 +1004,7 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
Set(info, New("width").ToLocalChecked(), New<v8::Uint32>(static_cast<uint32_t>(width)));
|
||||
Set(info, New("height").ToLocalChecked(), New<v8::Uint32>(static_cast<uint32_t>(height)));
|
||||
Set(info, New("channels").ToLocalChecked(), New<v8::Uint32>(static_cast<uint32_t>(baton->channels)));
|
||||
Set(info, New("premultiplied").ToLocalChecked(), New<v8::Boolean>(baton->premultiplied));
|
||||
if (baton->cropCalcLeft != -1 && baton->cropCalcLeft != -1) {
|
||||
Set(info, New("cropCalcLeft").ToLocalChecked(), New<v8::Uint32>(static_cast<uint32_t>(baton->cropCalcLeft)));
|
||||
Set(info, New("cropCalcTop").ToLocalChecked(), New<v8::Uint32>(static_cast<uint32_t>(baton->cropCalcTop)));
|
||||
@@ -952,8 +1013,8 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
if (baton->bufferOutLength > 0) {
|
||||
// Pass ownership of output data to Buffer instance
|
||||
argv[1] = Nan::NewBuffer(
|
||||
static_cast<char*>(baton->bufferOut), baton->bufferOutLength, sharp::FreeCallback, nullptr
|
||||
).ToLocalChecked();
|
||||
static_cast<char*>(baton->bufferOut), baton->bufferOutLength, sharp::FreeCallback, nullptr)
|
||||
.ToLocalChecked();
|
||||
// Add buffer size to info
|
||||
Set(info, New("size").ToLocalChecked(), New<v8::Uint32>(static_cast<uint32_t>(baton->bufferOutLength)));
|
||||
argv[2] = info;
|
||||
@@ -972,18 +1033,24 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
[this](uint32_t index, v8::Local<v8::Object> const buffer) -> uint32_t {
|
||||
GetFromPersistent(index);
|
||||
return index + 1;
|
||||
}
|
||||
);
|
||||
});
|
||||
delete baton->input;
|
||||
delete baton->overlay;
|
||||
delete baton->boolean;
|
||||
for_each(baton->joinChannelIn.begin(), baton->joinChannelIn.end(),
|
||||
[this](sharp::InputDescriptor *joinChannelIn) {
|
||||
delete joinChannelIn;
|
||||
}
|
||||
);
|
||||
});
|
||||
delete baton;
|
||||
|
||||
// Handle warnings
|
||||
std::string warning = sharp::VipsWarningPop();
|
||||
while (!warning.empty()) {
|
||||
v8::Local<v8::Value> message[1] = { New(warning).ToLocalChecked() };
|
||||
debuglog->Call(1, message);
|
||||
warning = sharp::VipsWarningPop();
|
||||
}
|
||||
|
||||
// Decrement processing task counter
|
||||
g_atomic_int_dec_and_test(&sharp::counterProcess);
|
||||
v8::Local<v8::Value> queueLength[1] = { New<v8::Uint32>(sharp::counterQueue) };
|
||||
@@ -996,43 +1063,48 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
|
||||
private:
|
||||
PipelineBaton *baton;
|
||||
Nan::Callback *debuglog;
|
||||
Nan::Callback *queueListener;
|
||||
std::vector<v8::Local<v8::Object>> buffersToPersist;
|
||||
|
||||
/*
|
||||
Calculate the angle of rotation and need-to-flip for the output image.
|
||||
In order of priority:
|
||||
1. Use explicitly requested angle (supports 90, 180, 270)
|
||||
2. Use input image EXIF Orientation header - supports mirroring
|
||||
3. Otherwise default to zero, i.e. no rotation
|
||||
Calculate the angle of rotation and need-to-flip for the given Exif orientation
|
||||
By default, returns zero, i.e. no rotation.
|
||||
*/
|
||||
std::tuple<VipsAngle, bool, bool>
|
||||
CalculateRotationAndFlip(int const angle, vips::VImage image) {
|
||||
CalculateExifRotationAndFlip(int const exifOrientation) {
|
||||
VipsAngle rotate = VIPS_ANGLE_D0;
|
||||
bool flip = FALSE;
|
||||
bool flop = FALSE;
|
||||
if (angle == -1) {
|
||||
switch(sharp::ExifOrientation(image)) {
|
||||
case 6: rotate = VIPS_ANGLE_D90; break;
|
||||
case 3: rotate = VIPS_ANGLE_D180; break;
|
||||
case 8: rotate = VIPS_ANGLE_D270; break;
|
||||
case 2: flop = TRUE; break; // flop 1
|
||||
case 7: flip = TRUE; rotate = VIPS_ANGLE_D90; break; // flip 6
|
||||
case 4: flop = TRUE; rotate = VIPS_ANGLE_D180; break; // flop 3
|
||||
case 5: flip = TRUE; rotate = VIPS_ANGLE_D270; break; // flip 8
|
||||
}
|
||||
} else {
|
||||
if (angle == 90) {
|
||||
rotate = VIPS_ANGLE_D90;
|
||||
} else if (angle == 180) {
|
||||
rotate = VIPS_ANGLE_D180;
|
||||
} else if (angle == 270) {
|
||||
rotate = VIPS_ANGLE_D270;
|
||||
}
|
||||
switch (exifOrientation) {
|
||||
case 6: rotate = VIPS_ANGLE_D90; break;
|
||||
case 3: rotate = VIPS_ANGLE_D180; break;
|
||||
case 8: rotate = VIPS_ANGLE_D270; break;
|
||||
case 2: flop = TRUE; break; // flop 1
|
||||
case 7: flip = TRUE; rotate = VIPS_ANGLE_D90; break; // flip 6
|
||||
case 4: flop = TRUE; rotate = VIPS_ANGLE_D180; break; // flop 3
|
||||
case 5: flip = TRUE; rotate = VIPS_ANGLE_D270; break; // flip 8
|
||||
}
|
||||
return std::make_tuple(rotate, flip, flop);
|
||||
}
|
||||
|
||||
/*
|
||||
Calculate the rotation for the given angle.
|
||||
Supports any positive or negative angle that is a multiple of 90.
|
||||
*/
|
||||
VipsAngle
|
||||
CalculateAngleRotation(int angle) {
|
||||
angle = angle % 360;
|
||||
if (angle < 0)
|
||||
angle = 360 + angle;
|
||||
switch (angle) {
|
||||
case 90: return VIPS_ANGLE_D90;
|
||||
case 180: return VIPS_ANGLE_D180;
|
||||
case 270: return VIPS_ANGLE_D270;
|
||||
}
|
||||
return VIPS_ANGLE_D0;
|
||||
}
|
||||
|
||||
/*
|
||||
Assemble the suffix argument to dzsave, which is the format (by extname)
|
||||
alongisde comma-separated arguments to the corresponding `formatsave` vips
|
||||
@@ -1114,7 +1186,7 @@ NAN_METHOD(pipeline) {
|
||||
// Background colour
|
||||
v8::Local<v8::Object> background = AttrAs<v8::Object>(options, "background");
|
||||
for (unsigned int i = 0; i < 4; i++) {
|
||||
baton->background[i] = AttrTo<uint32_t>(background, i);
|
||||
baton->background[i] = AttrTo<double>(background, i);
|
||||
}
|
||||
// Overlay options
|
||||
if (HasAttr(options, "overlay")) {
|
||||
@@ -1132,12 +1204,12 @@ NAN_METHOD(pipeline) {
|
||||
baton->interpolator = AttrAsStr(options, "interpolator");
|
||||
baton->centreSampling = AttrTo<bool>(options, "centreSampling");
|
||||
// Join Channel Options
|
||||
if(HasAttr(options, "joinChannelIn")) {
|
||||
if (HasAttr(options, "joinChannelIn")) {
|
||||
v8::Local<v8::Object> joinChannelObject = Nan::Get(options, Nan::New("joinChannelIn").ToLocalChecked())
|
||||
.ToLocalChecked().As<v8::Object>();
|
||||
v8::Local<v8::Array> joinChannelArray = joinChannelObject.As<v8::Array>();
|
||||
int joinChannelArrayLength = AttrTo<int32_t>(joinChannelObject, "length");
|
||||
for(int i = 0; i < joinChannelArrayLength; i++) {
|
||||
for (int i = 0; i < joinChannelArrayLength; i++) {
|
||||
baton->joinChannelIn.push_back(
|
||||
CreateInputDescriptor(
|
||||
Nan::Get(joinChannelArray, i).ToLocalChecked().As<v8::Object>(),
|
||||
@@ -1154,12 +1226,10 @@ NAN_METHOD(pipeline) {
|
||||
baton->threshold = AttrTo<int32_t>(options, "threshold");
|
||||
baton->thresholdGrayscale = AttrTo<bool>(options, "thresholdGrayscale");
|
||||
baton->trimTolerance = AttrTo<int32_t>(options, "trimTolerance");
|
||||
if(baton->accessMethod == VIPS_ACCESS_SEQUENTIAL && baton->trimTolerance != 0) {
|
||||
baton->accessMethod = VIPS_ACCESS_RANDOM;
|
||||
}
|
||||
baton->gamma = AttrTo<double>(options, "gamma");
|
||||
baton->greyscale = AttrTo<bool>(options, "greyscale");
|
||||
baton->normalise = AttrTo<bool>(options, "normalise");
|
||||
baton->useExifOrientation = AttrTo<bool>(options, "useExifOrientation");
|
||||
baton->angle = AttrTo<int32_t>(options, "angle");
|
||||
baton->rotateBeforePreExtract = AttrTo<bool>(options, "rotateBeforePreExtract");
|
||||
baton->flip = AttrTo<bool>(options, "flip");
|
||||
@@ -1209,7 +1279,21 @@ NAN_METHOD(pipeline) {
|
||||
baton->pngCompressionLevel = AttrTo<uint32_t>(options, "pngCompressionLevel");
|
||||
baton->pngAdaptiveFiltering = AttrTo<bool>(options, "pngAdaptiveFiltering");
|
||||
baton->webpQuality = AttrTo<uint32_t>(options, "webpQuality");
|
||||
baton->webpAlphaQuality = AttrTo<uint32_t>(options, "webpAlphaQuality");
|
||||
baton->webpLossless = AttrTo<bool>(options, "webpLossless");
|
||||
baton->webpNearLossless = AttrTo<bool>(options, "webpNearLossless");
|
||||
baton->tiffQuality = AttrTo<uint32_t>(options, "tiffQuality");
|
||||
baton->tiffSquash = AttrTo<bool>(options, "tiffSquash");
|
||||
baton->tiffXres = AttrTo<double>(options, "tiffXres");
|
||||
baton->tiffYres = AttrTo<double>(options, "tiffYres");
|
||||
// tiff compression options
|
||||
baton->tiffCompression = static_cast<VipsForeignTiffCompression>(
|
||||
vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_TIFF_COMPRESSION,
|
||||
AttrAsStr(options, "tiffCompression").data()));
|
||||
baton->tiffPredictor = static_cast<VipsForeignTiffPredictor>(
|
||||
vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_TIFF_PREDICTOR,
|
||||
AttrAsStr(options, "tiffPredictor").data()));
|
||||
|
||||
// Tile output
|
||||
baton->tileSize = AttrTo<uint32_t>(options, "tileSize");
|
||||
baton->tileOverlap = AttrTo<uint32_t>(options, "tileOverlap");
|
||||
@@ -1228,13 +1312,22 @@ NAN_METHOD(pipeline) {
|
||||
baton->tileLayout = VIPS_FOREIGN_DZ_LAYOUT_DZ;
|
||||
}
|
||||
baton->tileFormat = AttrAsStr(options, "tileFormat");
|
||||
// Force random access for certain operations
|
||||
if (baton->accessMethod == VIPS_ACCESS_SEQUENTIAL && (
|
||||
baton->trimTolerance != 0 || baton->normalise ||
|
||||
baton->crop == 16 || baton->crop == 17)) {
|
||||
baton->accessMethod = VIPS_ACCESS_RANDOM;
|
||||
}
|
||||
|
||||
// Function to notify of libvips warnings
|
||||
Nan::Callback *debuglog = new Nan::Callback(AttrAs<v8::Function>(options, "debuglog"));
|
||||
|
||||
// Function to notify of queue length changes
|
||||
Nan::Callback *queueListener = new Nan::Callback(AttrAs<v8::Function>(options, "queueListener"));
|
||||
|
||||
// Join queue for worker thread
|
||||
Nan::Callback *callback = new Nan::Callback(info[1].As<v8::Function>());
|
||||
Nan::AsyncQueueWorker(new PipelineWorker(callback, baton, queueListener, buffersToPersist));
|
||||
Nan::AsyncQueueWorker(new PipelineWorker(callback, baton, debuglog, queueListener, buffersToPersist));
|
||||
|
||||
// Increment queued task counter
|
||||
g_atomic_int_inc(&sharp::counterQueue);
|
||||
|
||||
@@ -1,12 +1,28 @@
|
||||
// Copyright 2013, 2014, 2015, 2016, 2017 Lovell Fuller and contributors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef SRC_PIPELINE_H_
|
||||
#define SRC_PIPELINE_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <nan.h>
|
||||
#include <vips/vips8>
|
||||
|
||||
#include "nan.h"
|
||||
#include "common.h"
|
||||
#include "./common.h"
|
||||
|
||||
NAN_METHOD(pipeline);
|
||||
|
||||
@@ -48,6 +64,7 @@ struct PipelineBaton {
|
||||
int crop;
|
||||
int cropCalcLeft;
|
||||
int cropCalcTop;
|
||||
bool premultiplied;
|
||||
std::string kernel;
|
||||
std::string interpolator;
|
||||
bool centreSampling;
|
||||
@@ -64,6 +81,7 @@ struct PipelineBaton {
|
||||
double gamma;
|
||||
bool greyscale;
|
||||
bool normalise;
|
||||
bool useExifOrientation;
|
||||
int angle;
|
||||
bool rotateBeforePreExtract;
|
||||
bool flip;
|
||||
@@ -84,7 +102,15 @@ struct PipelineBaton {
|
||||
int pngCompressionLevel;
|
||||
bool pngAdaptiveFiltering;
|
||||
int webpQuality;
|
||||
int webpAlphaQuality;
|
||||
bool webpNearLossless;
|
||||
bool webpLossless;
|
||||
int tiffQuality;
|
||||
VipsForeignTiffCompression tiffCompression;
|
||||
VipsForeignTiffPredictor tiffPredictor;
|
||||
bool tiffSquash;
|
||||
double tiffXres;
|
||||
double tiffYres;
|
||||
std::string err;
|
||||
bool withMetadata;
|
||||
int withMetadataOrientation;
|
||||
@@ -121,6 +147,7 @@ struct PipelineBaton {
|
||||
crop(0),
|
||||
cropCalcLeft(-1),
|
||||
cropCalcTop(-1),
|
||||
premultiplied(false),
|
||||
centreSampling(false),
|
||||
flatten(false),
|
||||
negate(false),
|
||||
@@ -134,6 +161,7 @@ struct PipelineBaton {
|
||||
gamma(0.0),
|
||||
greyscale(false),
|
||||
normalise(false),
|
||||
useExifOrientation(false),
|
||||
angle(0),
|
||||
flip(false),
|
||||
flop(false),
|
||||
@@ -153,6 +181,11 @@ struct PipelineBaton {
|
||||
pngAdaptiveFiltering(true),
|
||||
webpQuality(80),
|
||||
tiffQuality(80),
|
||||
tiffCompression(VIPS_FOREIGN_TIFF_COMPRESSION_JPEG),
|
||||
tiffPredictor(VIPS_FOREIGN_TIFF_PREDICTOR_NONE),
|
||||
tiffSquash(false),
|
||||
tiffXres(1.0),
|
||||
tiffYres(1.0),
|
||||
withMetadata(false),
|
||||
withMetadataOrientation(-1),
|
||||
convKernelWidth(0),
|
||||
|
||||
22
src/sharp.cc
@@ -1,7 +1,20 @@
|
||||
#include <node.h>
|
||||
#include <vips/vips8>
|
||||
// Copyright 2013, 2014, 2015, 2016, 2017 Lovell Fuller and contributors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "nan.h"
|
||||
#include <node.h>
|
||||
#include <nan.h>
|
||||
#include <vips/vips8>
|
||||
|
||||
#include "common.h"
|
||||
#include "metadata.h"
|
||||
@@ -11,6 +24,9 @@
|
||||
NAN_MODULE_INIT(init) {
|
||||
vips_init("sharp");
|
||||
|
||||
g_log_set_handler("VIPS", static_cast<GLogLevelFlags>(G_LOG_LEVEL_WARNING),
|
||||
static_cast<GLogFunc>(sharp::VipsWarningCallback), nullptr);
|
||||
|
||||
// Methods available to JavaScript
|
||||
Nan::Set(target, Nan::New("metadata").ToLocalChecked(),
|
||||
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(metadata)).ToLocalChecked());
|
||||
|
||||
@@ -1,10 +1,25 @@
|
||||
// Copyright 2013, 2014, 2015, 2016, 2017 Lovell Fuller and contributors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
|
||||
#include <node.h>
|
||||
#include <nan.h>
|
||||
#include <vips/vips8>
|
||||
#include <vips/vector.h>
|
||||
|
||||
#include "nan.h"
|
||||
|
||||
#include "common.h"
|
||||
#include "operations.h"
|
||||
#include "utilities.h"
|
||||
@@ -45,14 +60,11 @@ NAN_METHOD(cache) {
|
||||
// Get memory stats
|
||||
Local<Object> memory = New<Object>();
|
||||
Set(memory, New("current").ToLocalChecked(),
|
||||
New<Integer>(static_cast<int>(round(vips_tracked_get_mem() / 1048576)))
|
||||
);
|
||||
New<Integer>(static_cast<int>(round(vips_tracked_get_mem() / 1048576))));
|
||||
Set(memory, New("high").ToLocalChecked(),
|
||||
New<Integer>(static_cast<int>(round(vips_tracked_get_mem_highwater() / 1048576)))
|
||||
);
|
||||
New<Integer>(static_cast<int>(round(vips_tracked_get_mem_highwater() / 1048576))));
|
||||
Set(memory, New("max").ToLocalChecked(),
|
||||
New<Integer>(static_cast<int>(round(vips_cache_get_max_mem() / 1048576)))
|
||||
);
|
||||
New<Integer>(static_cast<int>(round(vips_cache_get_max_mem() / 1048576))));
|
||||
// Get file stats
|
||||
Local<Object> files = New<Object>();
|
||||
Set(files, New("current").ToLocalChecked(), New<Integer>(vips_tracked_get_files()));
|
||||
|
||||
@@ -1,7 +1,21 @@
|
||||
// Copyright 2013, 2014, 2015, 2016, 2017 Lovell Fuller and contributors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef SRC_UTILITIES_H_
|
||||
#define SRC_UTILITIES_H_
|
||||
|
||||
#include "nan.h"
|
||||
#include <nan.h>
|
||||
|
||||
NAN_METHOD(cache);
|
||||
NAN_METHOD(concurrency);
|
||||
|
||||
@@ -5,17 +5,18 @@
|
||||
"author": "Lovell Fuller <npm@lovell.info>",
|
||||
"description": "Benchmark and performance tests for sharp",
|
||||
"scripts": {
|
||||
"test": "VIPS_WARNING=0 node perf && node random && node parallel"
|
||||
"test": "node perf && node random && node parallel"
|
||||
},
|
||||
"devDependencies": {
|
||||
"async": "^2.1.4",
|
||||
"benchmark": "^2.1.2",
|
||||
"async": "^2.5.0",
|
||||
"benchmark": "^2.1.4",
|
||||
"gm": "^1.23.0",
|
||||
"imagemagick": "^0.1.3",
|
||||
"imagemagick-native": "^1.9.3",
|
||||
"jimp": "^0.2.27",
|
||||
"lwip": "^0.0.9",
|
||||
"mapnik": "^3.5.14",
|
||||
"images": "^3.0.0",
|
||||
"jimp": "^0.2.28",
|
||||
"mapnik": "^3.6.2",
|
||||
"pajk-lwip": "^0.2.0",
|
||||
"semver": "^5.3.0"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
|
||||
@@ -7,11 +7,17 @@ const assert = require('assert');
|
||||
const Benchmark = require('benchmark');
|
||||
|
||||
// Contenders
|
||||
const sharp = require('../../');
|
||||
const gm = require('gm');
|
||||
const imagemagick = require('imagemagick');
|
||||
const mapnik = require('mapnik');
|
||||
const jimp = require('jimp');
|
||||
const sharp = require('../../');
|
||||
let images;
|
||||
try {
|
||||
images = require('images');
|
||||
} catch (err) {
|
||||
console.log('Excluding node-images');
|
||||
}
|
||||
let imagemagickNative;
|
||||
try {
|
||||
imagemagickNative = require('imagemagick-native');
|
||||
@@ -20,7 +26,7 @@ try {
|
||||
}
|
||||
let lwip;
|
||||
try {
|
||||
lwip = require('lwip');
|
||||
lwip = require('pajk-lwip');
|
||||
} catch (err) {
|
||||
console.log('Excluding lwip');
|
||||
}
|
||||
@@ -145,7 +151,7 @@ async.series({
|
||||
}).add('mapnik-buffer-buffer', {
|
||||
defer: true,
|
||||
fn: function (deferred) {
|
||||
mapnik.Image.fromBytes(inputJpgBuffer, function (err, img) {
|
||||
mapnik.Image.fromBytes(inputJpgBuffer, { max_size: 3000 }, function (err, img) {
|
||||
if (err) throw err;
|
||||
img
|
||||
.resize(width, height, {
|
||||
@@ -266,6 +272,14 @@ async.series({
|
||||
});
|
||||
}
|
||||
});
|
||||
// images
|
||||
if (typeof images !== 'undefined') {
|
||||
jpegSuite.add('images-file-file', function () {
|
||||
images(fixtures.inputJpg)
|
||||
.resize(width, height)
|
||||
.save(fixtures.outputJpg, { quality: 80 });
|
||||
});
|
||||
}
|
||||
// sharp
|
||||
jpegSuite.add('sharp-buffer-file', {
|
||||
defer: true,
|
||||
@@ -733,7 +747,7 @@ async.series({
|
||||
}).add('mapnik-buffer-buffer', {
|
||||
defer: true,
|
||||
fn: function (deferred) {
|
||||
mapnik.Image.fromBytes(inputPngBuffer, function (err, img) {
|
||||
mapnik.Image.fromBytes(inputPngBuffer, { max_size: 3000 }, function (err, img) {
|
||||
if (err) throw err;
|
||||
img.premultiply(function (err, img) {
|
||||
if (err) throw err;
|
||||
@@ -819,6 +833,14 @@ async.series({
|
||||
});
|
||||
}
|
||||
});
|
||||
// images
|
||||
if (typeof images !== 'undefined') {
|
||||
pngSuite.add('images-file-file', function () {
|
||||
images(fixtures.inputPng)
|
||||
.resize(width, height)
|
||||
.save(fixtures.outputPng);
|
||||
});
|
||||
}
|
||||
// sharp
|
||||
pngSuite.add('sharp-buffer-file', {
|
||||
defer: true,
|
||||
@@ -957,7 +979,7 @@ async.series({
|
||||
}).add('sharp-file-buffer', {
|
||||
defer: true,
|
||||
fn: function (deferred) {
|
||||
sharp(fixtures.inputWebp)
|
||||
sharp(fixtures.inputWebP)
|
||||
.resize(width, height)
|
||||
.toBuffer(function (err, buffer) {
|
||||
if (err) {
|
||||
|
||||
@@ -15,7 +15,7 @@ const min = 320;
|
||||
const max = 960;
|
||||
|
||||
const randomDimension = function () {
|
||||
return Math.ceil(Math.random() * (max - min) + min);
|
||||
return Math.ceil((Math.random() * (max - min)) + min);
|
||||
};
|
||||
|
||||
new Benchmark.Suite('random').add('imagemagick', {
|
||||
|
||||
BIN
test/fixtures/320x240.jpg
vendored
Normal file
|
After Width: | Height: | Size: 79 KiB |
BIN
test/fixtures/8bit_depth.tiff
vendored
Normal file
BIN
test/fixtures/alpha-layer-2-ink.jpg
vendored
Normal file
|
After Width: | Height: | Size: 36 KiB |
BIN
test/fixtures/expected/alpha-layer-2-trim-resize.jpg
vendored
Normal file
|
After Width: | Height: | Size: 6.2 KiB |
BIN
test/fixtures/expected/create-rgb.jpg
vendored
Normal file
|
After Width: | Height: | Size: 271 B |
BIN
test/fixtures/expected/create-rgba.png
vendored
Normal file
|
After Width: | Height: | Size: 105 B |
BIN
test/fixtures/expected/crop-strategy-attention.jpg
vendored
Normal file
|
After Width: | Height: | Size: 4.4 KiB |
BIN
test/fixtures/expected/crop-strategy-entropy.jpg
vendored
Normal file
|
After Width: | Height: | Size: 5.9 KiB |
BIN
test/fixtures/expected/crop-strategy.jpg
vendored
|
Before Width: | Height: | Size: 8.5 KiB |
BIN
test/fixtures/expected/embed-16bit-rgba.png
vendored
|
Before Width: | Height: | Size: 762 B After Width: | Height: | Size: 987 B |
BIN
test/fixtures/expected/embed-2channel.png
vendored
|
Before Width: | Height: | Size: 732 B After Width: | Height: | Size: 762 B |
BIN
test/fixtures/expected/extend-2channel.png
vendored
|
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 32 KiB |
BIN
test/fixtures/expected/gamma-0.0.jpg
vendored
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 614 B |
BIN
test/fixtures/expected/overlay-jpeg-with-jpeg.jpg
vendored
Normal file
|
After Width: | Height: | Size: 17 KiB |
BIN
test/fixtures/expected/resize-diff-shrink-even.jpg
vendored
Normal file
|
After Width: | Height: | Size: 4.0 KiB |
BIN
test/fixtures/expected/resize-diff-shrink-odd.jpg
vendored
Normal file
|
After Width: | Height: | Size: 6.3 KiB |
BIN
test/fixtures/expected/rotate-and-flip.jpg
vendored
Normal file
|
After Width: | Height: | Size: 24 KiB |
BIN
test/fixtures/expected/rotate-and-flop.jpg
vendored
Normal file
|
After Width: | Height: | Size: 24 KiB |
BIN
test/fixtures/expected/webp-alpha-80.webp
vendored
Normal file
|
After Width: | Height: | Size: 116 KiB |
BIN
test/fixtures/expected/webp-lossless.webp
vendored
Normal file
|
After Width: | Height: | Size: 170 KiB |
BIN
test/fixtures/expected/webp-near-lossless-50.webp
vendored
Normal file
|
After Width: | Height: | Size: 94 KiB |
9
test/fixtures/index.js
vendored
@@ -25,8 +25,8 @@ const fingerprint = function (image, callback) {
|
||||
let fingerprint = '';
|
||||
for (let col = 0; col < 8; col++) {
|
||||
for (let row = 0; row < 8; row++) {
|
||||
const left = data[row * 8 + col];
|
||||
const right = data[row * 8 + col + 1];
|
||||
const left = data[(row * 8) + col];
|
||||
const right = data[(row * 8) + col + 1];
|
||||
fingerprint = fingerprint + (left < right ? '1' : '0');
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,8 @@ module.exports = {
|
||||
inputJpgWithCorruptHeader: getPath('corrupt-header.jpg'),
|
||||
inputJpgWithLowContrast: getPath('low-contrast.jpg'), // http://www.flickr.com/photos/grizdave/2569067123/
|
||||
inputJpgLarge: getPath('giant-image.jpg'),
|
||||
inputJpg320x240: getPath('320x240.jpg'), // http://www.andrewault.net/2010/01/26/create-a-test-pattern-video-with-perl/
|
||||
inputJpgOverlayLayer2: getPath('alpha-layer-2-ink.jpg'),
|
||||
|
||||
inputPng: getPath('50020484-00001.png'), // http://c.searspartsdirect.com/lis_png/PLDM/50020484-00001.png
|
||||
inputPngWithTransparency: getPath('blackbug.png'), // public domain
|
||||
@@ -84,6 +86,8 @@ module.exports = {
|
||||
inputWebPWithTransparency: getPath('5_webp_a.webp'), // http://www.gstatic.com/webp/gallery3/5_webp_a.webp
|
||||
inputTiff: getPath('G31D.TIF'), // http://www.fileformat.info/format/tiff/sample/e6c9a6e5253348f4aef6d17b534360ab/index.htm
|
||||
inputTiffCielab: getPath('cielab-dagams.tiff'), // https://github.com/lovell/sharp/issues/646
|
||||
inputTiffUncompressed: getPath('uncompressed_tiff.tiff'), // https://code.google.com/archive/p/imagetestsuite/wikis/TIFFTestSuite.wiki file: 0c84d07e1b22b76f24cccc70d8788e4a.tif
|
||||
inputTiff8BitDepth: getPath('8bit_depth.tiff'),
|
||||
inputGif: getPath('Crash_test.gif'), // http://upload.wikimedia.org/wikipedia/commons/e/e3/Crash_test.gif
|
||||
inputGifGreyPlusAlpha: getPath('grey-plus-alpha.gif'), // http://i.imgur.com/gZ5jlmE.gif
|
||||
inputSvg: getPath('check.svg'), // http://dev.w3.org/SVG/tools/svgweb/samples/svg-files/check.svg
|
||||
@@ -102,6 +106,7 @@ module.exports = {
|
||||
outputPng: getPath('output.png'),
|
||||
outputWebP: getPath('output.webp'),
|
||||
outputV: getPath('output.v'),
|
||||
outputTiff: getPath('output.tiff'),
|
||||
outputZoinks: getPath('output.zoinks'), // an 'unknown' file extension
|
||||
|
||||
// Path for tests requiring human inspection
|
||||
|
||||
BIN
test/fixtures/uncompressed_tiff.tiff
vendored
Normal file
@@ -1,48 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const assert = require('assert');
|
||||
|
||||
const cpplint = require('node-cpplint/lib/');
|
||||
|
||||
describe('cpplint', function () {
|
||||
// Ignore cpplint failures, possibly newline-related, on Windows
|
||||
if (process.platform !== 'win32') {
|
||||
// List C++ source files
|
||||
fs.readdirSync(path.join(__dirname, '..', '..', 'src')).filter(function (source) {
|
||||
return source !== 'libvips';
|
||||
}).forEach(function (source) {
|
||||
const file = path.join('src', source);
|
||||
it(file, function (done) {
|
||||
// Lint each source file
|
||||
cpplint({
|
||||
files: [file],
|
||||
linelength: 120,
|
||||
filters: {
|
||||
legal: {
|
||||
copyright: false
|
||||
},
|
||||
build: {
|
||||
include: false
|
||||
},
|
||||
whitespace: {
|
||||
parens: false
|
||||
},
|
||||
runtime: {
|
||||
indentation_namespace: false
|
||||
}
|
||||
}
|
||||
}, function (err, report) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
const expected = {};
|
||||
expected[file] = [];
|
||||
assert.deepEqual(expected, report);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -141,16 +141,16 @@ describe('Crop', function () {
|
||||
it('Invalid values fail', function () {
|
||||
assert.throws(function () {
|
||||
sharp().crop(9);
|
||||
});
|
||||
}, /Expected valid crop id\/name\/strategy for crop but received 9 of type number/);
|
||||
assert.throws(function () {
|
||||
sharp().crop(1.1);
|
||||
});
|
||||
}, /Expected valid crop id\/name\/strategy for crop but received 1.1 of type number/);
|
||||
assert.throws(function () {
|
||||
sharp().crop(-1);
|
||||
});
|
||||
}, /Expected valid crop id\/name\/strategy for crop but received -1 of type number/);
|
||||
assert.throws(function () {
|
||||
sharp().crop('zoinks');
|
||||
});
|
||||
}, /Expected valid crop id\/name\/strategy for crop but received zoinks of type string/);
|
||||
});
|
||||
|
||||
it('Uses default value when none specified', function () {
|
||||
@@ -161,7 +161,7 @@ describe('Crop', function () {
|
||||
|
||||
describe('Entropy-based strategy', function () {
|
||||
it('JPEG', function (done) {
|
||||
sharp(fixtures.inputJpgWithCmykProfile)
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(80, 320)
|
||||
.crop(sharp.strategy.entropy)
|
||||
.toBuffer(function (err, data, info) {
|
||||
@@ -170,9 +170,7 @@ describe('Crop', function () {
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual(80, info.width);
|
||||
assert.strictEqual(320, info.height);
|
||||
assert.strictEqual(250, info.cropCalcLeft);
|
||||
assert.strictEqual(0, info.cropCalcTop);
|
||||
fixtures.assertSimilar(fixtures.expected('crop-strategy.jpg'), data, done);
|
||||
fixtures.assertSimilar(fixtures.expected('crop-strategy-entropy.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -186,8 +184,20 @@ describe('Crop', function () {
|
||||
assert.strictEqual(4, info.channels);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(80, info.height);
|
||||
assert.strictEqual(0, info.cropCalcLeft);
|
||||
assert.strictEqual(80, info.cropCalcTop);
|
||||
fixtures.assertSimilar(fixtures.expected('crop-strategy.png'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('supports the strategy passed as a string', function (done) {
|
||||
sharp(fixtures.inputPngWithTransparency)
|
||||
.resize(320, 80)
|
||||
.crop('entropy')
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('png', info.format);
|
||||
assert.strictEqual(4, info.channels);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(80, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected('crop-strategy.png'), data, done);
|
||||
});
|
||||
});
|
||||
@@ -195,7 +205,7 @@ describe('Crop', function () {
|
||||
|
||||
describe('Attention strategy', function () {
|
||||
it('JPEG', function (done) {
|
||||
sharp(fixtures.inputJpgWithCmykProfile)
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(80, 320)
|
||||
.crop(sharp.strategy.attention)
|
||||
.toBuffer(function (err, data, info) {
|
||||
@@ -204,9 +214,7 @@ describe('Crop', function () {
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual(80, info.width);
|
||||
assert.strictEqual(320, info.height);
|
||||
assert.strictEqual(250, info.cropCalcLeft);
|
||||
assert.strictEqual(0, info.cropCalcTop);
|
||||
fixtures.assertSimilar(fixtures.expected('crop-strategy.jpg'), data, done);
|
||||
fixtures.assertSimilar(fixtures.expected('crop-strategy-attention.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -220,8 +228,20 @@ describe('Crop', function () {
|
||||
assert.strictEqual(4, info.channels);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(80, info.height);
|
||||
assert.strictEqual(0, info.cropCalcLeft);
|
||||
assert.strictEqual(80, info.cropCalcTop);
|
||||
fixtures.assertSimilar(fixtures.expected('crop-strategy.png'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('supports the strategy passed as a string', function (done) {
|
||||
sharp(fixtures.inputPngWithTransparency)
|
||||
.resize(320, 80)
|
||||
.crop('attention')
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('png', info.format);
|
||||
assert.strictEqual(4, info.channels);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(80, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected('crop-strategy.png'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,6 +8,7 @@ const fixtures = require('../fixtures');
|
||||
describe('Interpolators and kernels', function () {
|
||||
describe('Reducers', function () {
|
||||
[
|
||||
sharp.kernel.nearest,
|
||||
sharp.kernel.cubic,
|
||||
sharp.kernel.lanczos2,
|
||||
sharp.kernel.lanczos3
|
||||
@@ -34,17 +35,54 @@ describe('Interpolators and kernels', function () {
|
||||
sharp.interpolator.locallyBoundedBicubic,
|
||||
sharp.interpolator.vertexSplitQuadraticBasisSpline
|
||||
].forEach(function (interpolator) {
|
||||
it(interpolator, function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, null, { interpolator: interpolator })
|
||||
.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);
|
||||
});
|
||||
describe(interpolator, function () {
|
||||
it('x and y', function (done) {
|
||||
sharp(fixtures.inputTiff8BitDepth)
|
||||
.resize(200, 200, { interpolator: interpolator })
|
||||
.png()
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(200, info.width);
|
||||
assert.strictEqual(200, info.height);
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('x only', function (done) {
|
||||
sharp(fixtures.inputTiff8BitDepth)
|
||||
.resize(200, 21, { interpolator: interpolator })
|
||||
.png()
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(200, info.width);
|
||||
assert.strictEqual(21, info.height);
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('y only', function (done) {
|
||||
sharp(fixtures.inputTiff8BitDepth)
|
||||
.resize(21, 200, { interpolator: interpolator })
|
||||
.png()
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(21, info.width);
|
||||
assert.strictEqual(200, info.height);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('nearest with integral factor', function (done) {
|
||||
sharp(fixtures.inputTiff8BitDepth)
|
||||
.resize(210, 210, { interpolator: '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 () {
|
||||
|
||||
467
test/unit/io.js
@@ -77,16 +77,58 @@ describe('Input/output', function () {
|
||||
readable.pipe(pipeline);
|
||||
});
|
||||
|
||||
it('Read from Stream and write to Buffer via Promise', function (done) {
|
||||
const readable = fs.createReadStream(fixtures.inputJpg);
|
||||
it('Read from Stream and write to Buffer via Promise resolved with Buffer', function () {
|
||||
const pipeline = sharp().resize(1, 1);
|
||||
pipeline.toBuffer().then(function (data) {
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
done();
|
||||
}).catch(function (err) {
|
||||
throw err;
|
||||
});
|
||||
readable.pipe(pipeline);
|
||||
fs.createReadStream(fixtures.inputJpg).pipe(pipeline);
|
||||
return pipeline
|
||||
.toBuffer({resolveWithObject: false})
|
||||
.then(function (data) {
|
||||
assert.strictEqual(true, data instanceof Buffer);
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
});
|
||||
});
|
||||
|
||||
it('Read from Stream and write to Buffer via Promise resolved with Object', function () {
|
||||
const pipeline = sharp().resize(1, 1);
|
||||
fs.createReadStream(fixtures.inputJpg).pipe(pipeline);
|
||||
return pipeline
|
||||
.toBuffer({resolveWithObject: true})
|
||||
.then(function (object) {
|
||||
assert.strictEqual('object', typeof object);
|
||||
assert.strictEqual('object', typeof object.info);
|
||||
assert.strictEqual('jpeg', object.info.format);
|
||||
assert.strictEqual(1, object.info.width);
|
||||
assert.strictEqual(1, object.info.height);
|
||||
assert.strictEqual(3, object.info.channels);
|
||||
assert.strictEqual(true, object.data instanceof Buffer);
|
||||
assert.strictEqual(true, object.data.length > 0);
|
||||
});
|
||||
});
|
||||
|
||||
it('Read from File and write to Buffer via Promise resolved with Buffer', function () {
|
||||
return sharp(fixtures.inputJpg)
|
||||
.resize(1, 1)
|
||||
.toBuffer({resolveWithObject: false})
|
||||
.then(function (data) {
|
||||
assert.strictEqual(true, data instanceof Buffer);
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
});
|
||||
});
|
||||
|
||||
it('Read from File and write to Buffer via Promise resolved with Object', function () {
|
||||
return sharp(fixtures.inputJpg)
|
||||
.resize(1, 1)
|
||||
.toBuffer({resolveWithObject: true})
|
||||
.then(function (object) {
|
||||
assert.strictEqual('object', typeof object);
|
||||
assert.strictEqual('object', typeof object.info);
|
||||
assert.strictEqual('jpeg', object.info.format);
|
||||
assert.strictEqual(1, object.info.width);
|
||||
assert.strictEqual(1, object.info.height);
|
||||
assert.strictEqual(3, object.info.channels);
|
||||
assert.strictEqual(true, object.data instanceof Buffer);
|
||||
assert.strictEqual(true, object.data.length > 0);
|
||||
});
|
||||
});
|
||||
|
||||
it('Read from Stream and write to Stream', function (done) {
|
||||
@@ -157,6 +199,28 @@ describe('Input/output', function () {
|
||||
readableButNotAnImage.pipe(writable);
|
||||
});
|
||||
|
||||
it('Readable side of Stream can start flowing after Writable side has finished', function (done) {
|
||||
const readable = fs.createReadStream(fixtures.inputJpg);
|
||||
const writable = fs.createWriteStream(fixtures.outputJpg);
|
||||
writable.on('finish', function () {
|
||||
sharp(fixtures.outputJpg).toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
assert.strictEqual(data.length, info.size);
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
fs.unlinkSync(fixtures.outputJpg);
|
||||
done();
|
||||
});
|
||||
});
|
||||
const pipeline = sharp().resize(320, 240);
|
||||
readable.pipe(pipeline);
|
||||
pipeline.on('finish', function () {
|
||||
pipeline.pipe(writable);
|
||||
});
|
||||
});
|
||||
|
||||
it('Sequential read, force JPEG', function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.sequentialRead()
|
||||
@@ -189,6 +253,21 @@ describe('Input/output', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Support output to jpg format', function (done) {
|
||||
sharp(fixtures.inputPng)
|
||||
.resize(320, 240)
|
||||
.toFormat('jpg')
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
assert.strictEqual(data.length, info.size);
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('Fail when output File is input File', function (done) {
|
||||
sharp(fixtures.inputJpg).toFile(fixtures.inputJpg, function (err) {
|
||||
assert(!!err);
|
||||
@@ -224,7 +303,7 @@ describe('Input/output', function () {
|
||||
});
|
||||
|
||||
it('Fail when input is empty Buffer', function (done) {
|
||||
sharp(new Buffer(0)).toBuffer().then(function () {
|
||||
sharp(Buffer.alloc(0)).toBuffer().then(function () {
|
||||
assert(false);
|
||||
done();
|
||||
}).catch(function (err) {
|
||||
@@ -234,7 +313,7 @@ describe('Input/output', function () {
|
||||
});
|
||||
|
||||
it('Fail when input is invalid Buffer', function (done) {
|
||||
sharp(new Buffer([0x1, 0x2, 0x3, 0x4])).toBuffer().then(function () {
|
||||
sharp(Buffer.from([0x1, 0x2, 0x3, 0x4])).toBuffer().then(function () {
|
||||
assert(false);
|
||||
done();
|
||||
}).catch(function (err) {
|
||||
@@ -244,6 +323,16 @@ describe('Input/output', function () {
|
||||
});
|
||||
|
||||
describe('Fail for unsupported input', function () {
|
||||
it('Undefined', function () {
|
||||
assert.throws(function () {
|
||||
sharp(undefined);
|
||||
});
|
||||
});
|
||||
it('Null', function () {
|
||||
assert.throws(function () {
|
||||
sharp(null);
|
||||
});
|
||||
});
|
||||
it('Numeric', function () {
|
||||
assert.throws(function () {
|
||||
sharp(1);
|
||||
@@ -254,11 +343,6 @@ describe('Input/output', function () {
|
||||
sharp(true);
|
||||
});
|
||||
});
|
||||
it('Empty Object', function () {
|
||||
assert.throws(function () {
|
||||
sharp({});
|
||||
});
|
||||
});
|
||||
it('Error Object', function () {
|
||||
assert.throws(function () {
|
||||
sharp(new Error());
|
||||
@@ -372,6 +456,50 @@ describe('Input/output', function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should work for webp alpha quality', function (done) {
|
||||
sharp(fixtures.inputPngAlphaPremultiplicationSmall)
|
||||
.webp({alphaQuality: 80})
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
assert.strictEqual('webp', info.format);
|
||||
fixtures.assertSimilar(fixtures.expected('webp-alpha-80.webp'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('should work for webp lossless', function (done) {
|
||||
sharp(fixtures.inputPngAlphaPremultiplicationSmall)
|
||||
.webp({lossless: true})
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
assert.strictEqual('webp', info.format);
|
||||
fixtures.assertSimilar(fixtures.expected('webp-lossless.webp'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('should work for webp near-lossless', function (done) {
|
||||
sharp(fixtures.inputPngAlphaPremultiplicationSmall)
|
||||
.webp({nearLossless: true, quality: 50})
|
||||
.toBuffer(function (err50, data50, info50) {
|
||||
if (err50) throw err50;
|
||||
assert.strictEqual(true, data50.length > 0);
|
||||
assert.strictEqual('webp', info50.format);
|
||||
fixtures.assertSimilar(fixtures.expected('webp-near-lossless-50.webp'), data50, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('should use near-lossless when both lossless and nearLossless are specified', function (done) {
|
||||
sharp(fixtures.inputPngAlphaPremultiplicationSmall)
|
||||
.webp({nearLossless: true, quality: 50, lossless: true})
|
||||
.toBuffer(function (err50, data50, info50) {
|
||||
if (err50) throw err50;
|
||||
assert.strictEqual(true, data50.length > 0);
|
||||
assert.strictEqual('webp', info50.format);
|
||||
fixtures.assertSimilar(fixtures.expected('webp-near-lossless-50.webp'), data50, done);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
it('Invalid output format', function (done) {
|
||||
@@ -729,12 +857,32 @@ describe('Input/output', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Save TIFF to Buffer', function (done) {
|
||||
sharp(fixtures.inputTiff)
|
||||
.resize(320, 240)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, data.length > 0);
|
||||
assert.strictEqual(data.length, info.size);
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('Invalid WebP quality throws error', function () {
|
||||
assert.throws(function () {
|
||||
sharp().webp({ quality: 101 });
|
||||
});
|
||||
});
|
||||
|
||||
it('Invalid WebP alpha quality throws error', function () {
|
||||
assert.throws(function () {
|
||||
sharp().webp({ alphaQuality: 101 });
|
||||
});
|
||||
});
|
||||
|
||||
it('Invalid TIFF quality throws error', function () {
|
||||
assert.throws(function () {
|
||||
sharp().tiff({ quality: 101 });
|
||||
@@ -747,6 +895,219 @@ describe('Input/output', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Not squashing TIFF to a bit depth of 1 should not change the file size', function (done) {
|
||||
const startSize = fs.statSync(fixtures.inputTiff8BitDepth).size;
|
||||
sharp(fixtures.inputTiff8BitDepth)
|
||||
.toColourspace('b-w') // can only squash 1 band uchar images
|
||||
.tiff({
|
||||
squash: false,
|
||||
compression: 'none'
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert(info.size === startSize);
|
||||
fs.unlink(fixtures.outputTiff, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('Squashing TIFF to a bit depth of 1 should significantly reduce file size', function (done) {
|
||||
const startSize = fs.statSync(fixtures.inputTiff8BitDepth).size;
|
||||
sharp(fixtures.inputTiff8BitDepth)
|
||||
.toColourspace('b-w') // can only squash 1 band uchar images
|
||||
.tiff({
|
||||
squash: true,
|
||||
compression: 'none'
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert(info.size < (startSize / 2));
|
||||
fs.unlink(fixtures.outputTiff, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('Invalid TIFF squash value throws error', function () {
|
||||
assert.throws(function () {
|
||||
sharp().tiff({ squash: 'true' });
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF setting xres and yres on file', function (done) {
|
||||
const res = 1000.0; // inputTiff has a dpi of 300 (res*2.54)
|
||||
sharp(fixtures.inputTiff)
|
||||
.tiff({
|
||||
xres: (res),
|
||||
yres: (res)
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
sharp(fixtures.outputTiff).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(metadata.density, res * 2.54); // convert to dpi
|
||||
fs.unlink(fixtures.outputTiff, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF setting xres and yres on buffer', function (done) {
|
||||
const res = 1000.0; // inputTiff has a dpi of 300 (res*2.54)
|
||||
sharp(fixtures.inputTiff)
|
||||
.tiff({
|
||||
xres: (res),
|
||||
yres: (res)
|
||||
})
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
sharp(data).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(metadata.density, res * 2.54); // convert to dpi
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF invalid xres value should throw an error', function () {
|
||||
assert.throws(function () {
|
||||
sharp().tiff({ xres: '1000.0' });
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF invalid yres value should throw an error', function () {
|
||||
assert.throws(function () {
|
||||
sharp().tiff({ yres: '1000.0' });
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF lzw compression with horizontal predictor shrinks test file', function (done) {
|
||||
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
|
||||
sharp(fixtures.inputTiffUncompressed)
|
||||
.tiff({
|
||||
compression: 'lzw',
|
||||
predictor: 'horizontal'
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert(info.size < startSize);
|
||||
fs.unlink(fixtures.outputTiff, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF deflate compression with horizontal predictor shrinks test file', function (done) {
|
||||
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
|
||||
sharp(fixtures.inputTiffUncompressed)
|
||||
.tiff({
|
||||
compression: 'deflate',
|
||||
predictor: 'horizontal'
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert(info.size < startSize);
|
||||
fs.unlink(fixtures.outputTiff, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF deflate compression with float predictor shrinks test file', function (done) {
|
||||
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
|
||||
sharp(fixtures.inputTiffUncompressed)
|
||||
.tiff({
|
||||
compression: 'deflate',
|
||||
predictor: 'float'
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert(info.size < startSize);
|
||||
fs.unlink(fixtures.outputTiff, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF deflate compression without predictor shrinks test file', function (done) {
|
||||
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
|
||||
sharp(fixtures.inputTiffUncompressed)
|
||||
.tiff({
|
||||
compression: 'deflate',
|
||||
predictor: 'none'
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert(info.size < startSize);
|
||||
fs.unlink(fixtures.outputTiff, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF jpeg compression shrinks test file', function (done) {
|
||||
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
|
||||
sharp(fixtures.inputTiffUncompressed)
|
||||
.tiff({
|
||||
compression: 'jpeg'
|
||||
})
|
||||
.toFile(fixtures.outputTiff, (err, info) => {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', info.format);
|
||||
assert(info.size < startSize);
|
||||
fs.unlink(fixtures.outputTiff, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF none compression does not throw error', function () {
|
||||
assert.doesNotThrow(function () {
|
||||
sharp().tiff({ compression: 'none' });
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF lzw compression does not throw error', function () {
|
||||
assert.doesNotThrow(function () {
|
||||
sharp().tiff({ compression: 'lzw' });
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF deflate compression does not throw error', function () {
|
||||
assert.doesNotThrow(function () {
|
||||
sharp().tiff({ compression: 'deflate' });
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF invalid compression option throws', function () {
|
||||
assert.throws(function () {
|
||||
sharp().tiff({ compression: 0 });
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF invalid compression option throws', function () {
|
||||
assert.throws(function () {
|
||||
sharp().tiff({ compression: 'a' });
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF invalid predictor option throws', function () {
|
||||
assert.throws(function () {
|
||||
sharp().tiff({ predictor: 'a' });
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF horizontal predictor does not throw error', function () {
|
||||
assert.doesNotThrow(function () {
|
||||
sharp().tiff({ predictor: 'horizontal' });
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF float predictor does not throw error', function () {
|
||||
assert.doesNotThrow(function () {
|
||||
sharp().tiff({ predictor: 'float' });
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF none predictor does not throw error', function () {
|
||||
assert.doesNotThrow(function () {
|
||||
sharp().tiff({ predictor: 'none' });
|
||||
});
|
||||
});
|
||||
|
||||
it('Input and output formats match when not forcing', function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 240)
|
||||
@@ -930,7 +1291,7 @@ describe('Input/output', function () {
|
||||
sharp(fixtures.inputJpg).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
sharp(fixtures.inputJpg)
|
||||
.limitInputPixels(metadata.width * metadata.height - 1)
|
||||
.limitInputPixels((metadata.width * metadata.height) - 1)
|
||||
.toBuffer(function (err) {
|
||||
assert.strictEqual(true, !!err);
|
||||
done();
|
||||
@@ -963,27 +1324,27 @@ describe('Input/output', function () {
|
||||
describe('Raw pixel input', function () {
|
||||
it('Missing options', function () {
|
||||
assert.throws(function () {
|
||||
sharp(null, { raw: {} });
|
||||
sharp({ raw: {} });
|
||||
});
|
||||
});
|
||||
it('Incomplete options', function () {
|
||||
assert.throws(function () {
|
||||
sharp(null, { raw: { width: 1, height: 1 } });
|
||||
sharp({ raw: { width: 1, height: 1 } });
|
||||
});
|
||||
});
|
||||
it('Invalid channels', function () {
|
||||
assert.throws(function () {
|
||||
sharp(null, { raw: { width: 1, height: 1, channels: 5 } });
|
||||
sharp({ raw: { width: 1, height: 1, channels: 5 } });
|
||||
});
|
||||
});
|
||||
it('Invalid height', function () {
|
||||
assert.throws(function () {
|
||||
sharp(null, { raw: { width: 1, height: 0, channels: 4 } });
|
||||
sharp({ raw: { width: 1, height: 0, channels: 4 } });
|
||||
});
|
||||
});
|
||||
it('Invalid width', function () {
|
||||
assert.throws(function () {
|
||||
sharp(null, { raw: { width: 'zoinks', height: 1, channels: 4 } });
|
||||
sharp({ raw: { width: 'zoinks', height: 1, channels: 4 } });
|
||||
});
|
||||
});
|
||||
it('RGB', function (done) {
|
||||
@@ -1036,12 +1397,72 @@ describe('Input/output', function () {
|
||||
assert.strictEqual(256, info.width);
|
||||
assert.strictEqual(192, info.height);
|
||||
assert.strictEqual(4, info.channels);
|
||||
fixtures.assertSimilar(fixtures.inputPngOverlayLayer1, data, done);
|
||||
fixtures.assertSimilar(fixtures.inputPngOverlayLayer1, data, { threshold: 7 }, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('create new image', function () {
|
||||
it('RGB', function (done) {
|
||||
const create = {
|
||||
width: 10,
|
||||
height: 20,
|
||||
channels: 3,
|
||||
background: { r: 0, g: 255, b: 0 }
|
||||
};
|
||||
sharp({ create: create })
|
||||
.jpeg()
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(create.width, info.width);
|
||||
assert.strictEqual(create.height, info.height);
|
||||
assert.strictEqual(create.channels, info.channels);
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
fixtures.assertSimilar(fixtures.expected('create-rgb.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
it('RGBA', function (done) {
|
||||
const create = {
|
||||
width: 20,
|
||||
height: 10,
|
||||
channels: 4,
|
||||
background: { r: 255, g: 0, b: 0, alpha: 128 }
|
||||
};
|
||||
sharp({ create: create })
|
||||
.png()
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(create.width, info.width);
|
||||
assert.strictEqual(create.height, info.height);
|
||||
assert.strictEqual(create.channels, info.channels);
|
||||
assert.strictEqual('png', info.format);
|
||||
fixtures.assertSimilar(fixtures.expected('create-rgba.png'), data, done);
|
||||
});
|
||||
});
|
||||
it('Invalid channels', function () {
|
||||
const create = {
|
||||
width: 10,
|
||||
height: 20,
|
||||
channels: 2,
|
||||
background: { r: 0, g: 0, b: 0 }
|
||||
};
|
||||
assert.throws(function () {
|
||||
sharp({ create: create });
|
||||
});
|
||||
});
|
||||
it('Missing background', function () {
|
||||
const create = {
|
||||
width: 10,
|
||||
height: 20,
|
||||
channels: 3
|
||||
};
|
||||
assert.throws(function () {
|
||||
sharp({ create: create });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Queue length change events', function (done) {
|
||||
let eventCounter = 0;
|
||||
const queueListener = function (queueLength) {
|
||||
|
||||
@@ -17,6 +17,7 @@ describe('Image metadata', function () {
|
||||
assert.strictEqual(2225, metadata.height);
|
||||
assert.strictEqual('srgb', metadata.space);
|
||||
assert.strictEqual(3, metadata.channels);
|
||||
assert.strictEqual('uchar', metadata.depth);
|
||||
assert.strictEqual('undefined', typeof metadata.density);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(false, metadata.hasAlpha);
|
||||
@@ -35,6 +36,7 @@ describe('Image metadata', function () {
|
||||
assert.strictEqual(600, metadata.height);
|
||||
assert.strictEqual('srgb', metadata.space);
|
||||
assert.strictEqual(3, metadata.channels);
|
||||
assert.strictEqual('uchar', metadata.depth);
|
||||
assert.strictEqual(72, metadata.density);
|
||||
assert.strictEqual(true, metadata.hasProfile);
|
||||
assert.strictEqual(false, metadata.hasAlpha);
|
||||
@@ -56,25 +58,24 @@ describe('Image metadata', function () {
|
||||
});
|
||||
});
|
||||
|
||||
if (sharp.format.tiff.input.file) {
|
||||
it('TIFF', function (done) {
|
||||
sharp(fixtures.inputTiff).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', metadata.format);
|
||||
assert.strictEqual(2464, metadata.width);
|
||||
assert.strictEqual(3248, metadata.height);
|
||||
assert.strictEqual('b-w', metadata.space);
|
||||
assert.strictEqual(1, metadata.channels);
|
||||
assert.strictEqual(300, metadata.density);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(false, metadata.hasAlpha);
|
||||
assert.strictEqual(1, metadata.orientation);
|
||||
assert.strictEqual('undefined', typeof metadata.exif);
|
||||
assert.strictEqual('undefined', typeof metadata.icc);
|
||||
done();
|
||||
});
|
||||
it('TIFF', function (done) {
|
||||
sharp(fixtures.inputTiff).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('tiff', metadata.format);
|
||||
assert.strictEqual(2464, metadata.width);
|
||||
assert.strictEqual(3248, metadata.height);
|
||||
assert.strictEqual('b-w', metadata.space);
|
||||
assert.strictEqual(1, metadata.channels);
|
||||
assert.strictEqual('uchar', metadata.depth);
|
||||
assert.strictEqual(300, metadata.density);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(false, metadata.hasAlpha);
|
||||
assert.strictEqual(1, metadata.orientation);
|
||||
assert.strictEqual('undefined', typeof metadata.exif);
|
||||
assert.strictEqual('undefined', typeof metadata.icc);
|
||||
done();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it('PNG', function (done) {
|
||||
sharp(fixtures.inputPng).metadata(function (err, metadata) {
|
||||
@@ -84,6 +85,7 @@ describe('Image metadata', function () {
|
||||
assert.strictEqual(2074, metadata.height);
|
||||
assert.strictEqual('b-w', metadata.space);
|
||||
assert.strictEqual(1, metadata.channels);
|
||||
assert.strictEqual('uchar', metadata.depth);
|
||||
assert.strictEqual(300, metadata.density);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(false, metadata.hasAlpha);
|
||||
@@ -102,6 +104,7 @@ describe('Image metadata', function () {
|
||||
assert.strictEqual(1536, metadata.height);
|
||||
assert.strictEqual('srgb', metadata.space);
|
||||
assert.strictEqual(4, metadata.channels);
|
||||
assert.strictEqual('uchar', metadata.depth);
|
||||
assert.strictEqual(72, metadata.density);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(true, metadata.hasAlpha);
|
||||
@@ -112,80 +115,59 @@ describe('Image metadata', function () {
|
||||
});
|
||||
});
|
||||
|
||||
if (sharp.format.webp.input.file) {
|
||||
it('WebP', function (done) {
|
||||
sharp(fixtures.inputWebP).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('webp', metadata.format);
|
||||
assert.strictEqual(1024, metadata.width);
|
||||
assert.strictEqual(772, metadata.height);
|
||||
assert.strictEqual('srgb', metadata.space);
|
||||
assert.strictEqual(3, metadata.channels);
|
||||
assert.strictEqual('undefined', typeof metadata.density);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(false, metadata.hasAlpha);
|
||||
assert.strictEqual('undefined', typeof metadata.orientation);
|
||||
assert.strictEqual('undefined', typeof metadata.exif);
|
||||
assert.strictEqual('undefined', typeof metadata.icc);
|
||||
done();
|
||||
});
|
||||
it('WebP', function (done) {
|
||||
sharp(fixtures.inputWebP).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('webp', metadata.format);
|
||||
assert.strictEqual(1024, metadata.width);
|
||||
assert.strictEqual(772, metadata.height);
|
||||
assert.strictEqual('srgb', metadata.space);
|
||||
assert.strictEqual(3, metadata.channels);
|
||||
assert.strictEqual('uchar', metadata.depth);
|
||||
assert.strictEqual('undefined', typeof metadata.density);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(false, metadata.hasAlpha);
|
||||
assert.strictEqual('undefined', typeof metadata.orientation);
|
||||
assert.strictEqual('undefined', typeof metadata.exif);
|
||||
assert.strictEqual('undefined', typeof metadata.icc);
|
||||
done();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if (sharp.format.gif.input.file) {
|
||||
it('GIF via giflib', function (done) {
|
||||
sharp(fixtures.inputGif).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('gif', metadata.format);
|
||||
assert.strictEqual(800, metadata.width);
|
||||
assert.strictEqual(533, metadata.height);
|
||||
assert.strictEqual(3, metadata.channels);
|
||||
assert.strictEqual('undefined', typeof metadata.density);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(false, metadata.hasAlpha);
|
||||
assert.strictEqual('undefined', typeof metadata.orientation);
|
||||
assert.strictEqual('undefined', typeof metadata.exif);
|
||||
assert.strictEqual('undefined', typeof metadata.icc);
|
||||
done();
|
||||
});
|
||||
it('GIF via giflib', function (done) {
|
||||
sharp(fixtures.inputGif).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('gif', metadata.format);
|
||||
assert.strictEqual(800, metadata.width);
|
||||
assert.strictEqual(533, metadata.height);
|
||||
assert.strictEqual(3, metadata.channels);
|
||||
assert.strictEqual('uchar', metadata.depth);
|
||||
assert.strictEqual('undefined', typeof metadata.density);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(false, metadata.hasAlpha);
|
||||
assert.strictEqual('undefined', typeof metadata.orientation);
|
||||
assert.strictEqual('undefined', typeof metadata.exif);
|
||||
assert.strictEqual('undefined', typeof metadata.icc);
|
||||
done();
|
||||
});
|
||||
it('GIF grey+alpha via giflib', function (done) {
|
||||
sharp(fixtures.inputGifGreyPlusAlpha).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('gif', metadata.format);
|
||||
assert.strictEqual(2, metadata.width);
|
||||
assert.strictEqual(1, metadata.height);
|
||||
assert.strictEqual(2, metadata.channels);
|
||||
assert.strictEqual('undefined', typeof metadata.density);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(true, metadata.hasAlpha);
|
||||
assert.strictEqual('undefined', typeof metadata.orientation);
|
||||
assert.strictEqual('undefined', typeof metadata.exif);
|
||||
assert.strictEqual('undefined', typeof metadata.icc);
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('GIF grey+alpha via giflib', function (done) {
|
||||
sharp(fixtures.inputGifGreyPlusAlpha).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('gif', metadata.format);
|
||||
assert.strictEqual(2, metadata.width);
|
||||
assert.strictEqual(1, metadata.height);
|
||||
assert.strictEqual(2, metadata.channels);
|
||||
assert.strictEqual('uchar', metadata.depth);
|
||||
assert.strictEqual('undefined', typeof metadata.density);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(true, metadata.hasAlpha);
|
||||
assert.strictEqual('undefined', typeof metadata.orientation);
|
||||
assert.strictEqual('undefined', typeof metadata.exif);
|
||||
assert.strictEqual('undefined', typeof metadata.icc);
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
if (sharp.format.openslide.input.file) {
|
||||
it('Aperio SVS via openslide', function (done) {
|
||||
sharp(fixtures.inputSvs).metadata(function (err, metadata) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('openslide', metadata.format);
|
||||
assert.strictEqual(2220, metadata.width);
|
||||
assert.strictEqual(2967, metadata.height);
|
||||
assert.strictEqual(4, metadata.channels);
|
||||
assert.strictEqual('undefined', typeof metadata.density);
|
||||
assert.strictEqual('rgb', metadata.space);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(true, metadata.hasAlpha);
|
||||
assert.strictEqual('undefined', typeof metadata.orientation);
|
||||
assert.strictEqual('undefined', typeof metadata.exif);
|
||||
assert.strictEqual('undefined', typeof metadata.icc);
|
||||
done();
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it('File in, Promise out', function (done) {
|
||||
sharp(fixtures.inputJpg).metadata().then(function (metadata) {
|
||||
@@ -194,6 +176,7 @@ describe('Image metadata', function () {
|
||||
assert.strictEqual(2225, metadata.height);
|
||||
assert.strictEqual('srgb', metadata.space);
|
||||
assert.strictEqual(3, metadata.channels);
|
||||
assert.strictEqual('uchar', metadata.depth);
|
||||
assert.strictEqual('undefined', typeof metadata.density);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(false, metadata.hasAlpha);
|
||||
@@ -222,6 +205,7 @@ describe('Image metadata', function () {
|
||||
assert.strictEqual(2225, metadata.height);
|
||||
assert.strictEqual('srgb', metadata.space);
|
||||
assert.strictEqual(3, metadata.channels);
|
||||
assert.strictEqual('uchar', metadata.depth);
|
||||
assert.strictEqual('undefined', typeof metadata.density);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(false, metadata.hasAlpha);
|
||||
@@ -244,6 +228,7 @@ describe('Image metadata', function () {
|
||||
assert.strictEqual(2225, metadata.height);
|
||||
assert.strictEqual('srgb', metadata.space);
|
||||
assert.strictEqual(3, metadata.channels);
|
||||
assert.strictEqual('uchar', metadata.depth);
|
||||
assert.strictEqual('undefined', typeof metadata.density);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(false, metadata.hasAlpha);
|
||||
@@ -264,6 +249,7 @@ describe('Image metadata', function () {
|
||||
assert.strictEqual(2225, metadata.height);
|
||||
assert.strictEqual('srgb', metadata.space);
|
||||
assert.strictEqual(3, metadata.channels);
|
||||
assert.strictEqual('uchar', metadata.depth);
|
||||
assert.strictEqual('undefined', typeof metadata.density);
|
||||
assert.strictEqual(false, metadata.hasProfile);
|
||||
assert.strictEqual(false, metadata.hasAlpha);
|
||||
|
||||
@@ -155,20 +155,22 @@ describe('Overlays', function () {
|
||||
});
|
||||
}
|
||||
|
||||
it('Composite JPEG onto PNG', function (done) {
|
||||
it('Composite JPEG onto PNG, no premultiply', function (done) {
|
||||
sharp(fixtures.inputPngOverlayLayer1)
|
||||
.overlayWith(fixtures.inputJpgWithLandscapeExif1)
|
||||
.toBuffer(function (error) {
|
||||
if (error) return done(error);
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(false, info.premultiplied);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('Composite opaque JPEG onto JPEG', function (done) {
|
||||
it('Composite opaque JPEG onto JPEG, no premultiply', function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.overlayWith(fixtures.inputJpgWithLandscapeExif1)
|
||||
.toBuffer(function (error) {
|
||||
if (error) return done(error);
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(false, info.premultiplied);
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -561,14 +563,15 @@ describe('Overlays', function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(2048, 1536)
|
||||
.overlayWith(data, { raw: info })
|
||||
.toBuffer(function (err, data) {
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, info.premultiplied);
|
||||
fixtures.assertSimilar(fixtures.expected('overlay-jpeg-with-rgb.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Throws an error when called with an invalid file', function (done) {
|
||||
it('Returns an error when called with an invalid file', function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.overlayWith('notfound.png')
|
||||
.toBuffer(function (err) {
|
||||
@@ -576,4 +579,20 @@ describe('Overlays', function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('Composite JPEG onto JPEG, no premultiply', function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(480, 320)
|
||||
.overlayWith(fixtures.inputJpgBooleanTest)
|
||||
.png()
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('png', info.format);
|
||||
assert.strictEqual(480, info.width);
|
||||
assert.strictEqual(320, info.height);
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual(false, info.premultiplied);
|
||||
fixtures.assertSimilar(fixtures.expected('overlay-jpeg-with-jpeg.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "(bufferutil|sharp)" }] */
|
||||
|
||||
describe('Require-time checks', function () {
|
||||
/**
|
||||
Including sharp alongside another C++ module that does not require
|
||||
-stdlib=libc++ (for its C++11 features) has caused clang/llvm to
|
||||
segfault due to the use of static function variables.
|
||||
*/
|
||||
it('Require alongside C++ module that does not use libc++', function () {
|
||||
const bufferutil = require('bufferutil');
|
||||
const sharp = require('../../');
|
||||
});
|
||||
});
|
||||
@@ -66,37 +66,47 @@ describe('Resize dimensions', function () {
|
||||
it('Invalid width - NaN', function () {
|
||||
assert.throws(function () {
|
||||
sharp().resize('spoons', 240);
|
||||
});
|
||||
}, /Expected positive integer for width but received spoons of type string/);
|
||||
});
|
||||
|
||||
it('Invalid height - NaN', function () {
|
||||
assert.throws(function () {
|
||||
sharp().resize(320, 'spoons');
|
||||
});
|
||||
}, /Expected positive integer for height but received spoons of type string/);
|
||||
});
|
||||
|
||||
it('Invalid width - float', function () {
|
||||
assert.throws(function () {
|
||||
sharp().resize(1.5, 240);
|
||||
});
|
||||
}, /Expected positive integer for width but received 1.5 of type number/);
|
||||
});
|
||||
|
||||
it('Invalid height - float', function () {
|
||||
assert.throws(function () {
|
||||
sharp().resize(320, 1.5);
|
||||
});
|
||||
}, /Expected positive integer for height but received 1.5 of type number/);
|
||||
});
|
||||
|
||||
it('Invalid width - too large', function () {
|
||||
assert.throws(function () {
|
||||
sharp().resize(0x4000, 240);
|
||||
});
|
||||
it('Invalid width - too large', function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(0x4000, 1)
|
||||
.webp()
|
||||
.toBuffer(function (err) {
|
||||
assert.strictEqual(true, err instanceof Error);
|
||||
assert.strictEqual('Processed image is too large for the WebP format', err.message);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('Invalid height - too large', function () {
|
||||
assert.throws(function () {
|
||||
sharp().resize(320, 0x4000);
|
||||
});
|
||||
it('Invalid height - too large', function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(1, 0x4000)
|
||||
.webp()
|
||||
.toBuffer(function (err) {
|
||||
assert.strictEqual(true, err instanceof Error);
|
||||
assert.strictEqual('Processed image is too large for the WebP format', err.message);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('WebP shrink-on-load rounds to zero, ensure recalculation is correct', function (done) {
|
||||
@@ -402,4 +412,40 @@ describe('Resize dimensions', function () {
|
||||
sharp().resize(32, 24, { centreSampling: 1 });
|
||||
});
|
||||
});
|
||||
|
||||
it('Dimensions that result in differing even shrinks on each axis', function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(645, 399)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(645, info.width);
|
||||
assert.strictEqual(399, info.height);
|
||||
sharp(data)
|
||||
.resize(150, 100)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(150, info.width);
|
||||
assert.strictEqual(100, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected('resize-diff-shrink-even.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Dimensions that result in differing odd shrinks on each axis', function (done) {
|
||||
return sharp(fixtures.inputJpg)
|
||||
.resize(600, 399)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(600, info.width);
|
||||
assert.strictEqual(399, info.height);
|
||||
sharp(data)
|
||||
.resize(200)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(200, info.width);
|
||||
assert.strictEqual(133, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected('resize-diff-shrink-odd.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -34,6 +34,28 @@ describe('Rotation', function () {
|
||||
});
|
||||
});
|
||||
|
||||
[-3690, -450, -90, 90, 450, 3690].forEach(function (angle) {
|
||||
it('Rotate by any 90-multiple angle (' + angle + 'deg)', function (done) {
|
||||
sharp(fixtures.inputJpg320x240).rotate(angle).toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(240, info.width);
|
||||
assert.strictEqual(320, info.height);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
[-3780, -540, 0, 180, 540, 3780].forEach(function (angle) {
|
||||
it('Rotate by any 180-multiple angle (' + angle + 'deg)', function (done) {
|
||||
sharp(fixtures.inputJpg320x240).rotate(angle).toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Rotate by 270 degrees, square output ignoring aspect ratio', function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(240, 240)
|
||||
@@ -231,4 +253,32 @@ describe('Rotation', function () {
|
||||
fixtures.assertSimilar(fixtures.inputJpg, data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('Auto-rotate and flip', function (done) {
|
||||
sharp(fixtures.inputJpgWithExif)
|
||||
.rotate()
|
||||
.flip()
|
||||
.resize(320)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected('rotate-and-flip.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('Auto-rotate and flop', function (done) {
|
||||
sharp(fixtures.inputJpgWithExif)
|
||||
.rotate()
|
||||
.flop()
|
||||
.resize(320)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected('rotate-and-flop.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -187,7 +187,7 @@ describe('Tile', function () {
|
||||
assert.strictEqual(2225, info.height);
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual('undefined', typeof info.size);
|
||||
assertDeepZoomTiles(directory, 512 + 2 * 16, 13, done);
|
||||
assertDeepZoomTiles(directory, 512 + (2 * 16), 13, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -20,6 +20,20 @@ describe('Trim borders', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Skip shrink-on-load', function (done) {
|
||||
const expected = fixtures.expected('alpha-layer-2-trim-resize.jpg');
|
||||
sharp(fixtures.inputJpgOverlayLayer2)
|
||||
.trim()
|
||||
.resize(300)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(300, info.width);
|
||||
assert.strictEqual(300, info.height);
|
||||
fixtures.assertSimilar(expected, data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('16-bit PNG with alpha channel', function (done) {
|
||||
sharp(fixtures.inputPngWithTransparency16bit)
|
||||
.resize(32, 32)
|
||||
|
||||