Compare commits
70 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5bed3a7d52 | ||
|
|
ece111280b | ||
|
|
a15a9b956b | ||
|
|
42860c2f83 | ||
|
|
b5b95e5ae1 | ||
|
|
d705cffdd6 | ||
|
|
23a4bc103e | ||
|
|
c14434f9e7 | ||
|
|
25bd2cea3e | ||
|
|
532de4ecab | ||
|
|
bfdd27eeef | ||
|
|
bd9f238ab4 | ||
|
|
75556bb57c | ||
|
|
2de062a34a | ||
|
|
4589b15dea | ||
|
|
8b75ce6786 | ||
|
|
7bbc5176a1 | ||
|
|
5cb35485f1 | ||
|
|
80189ed689 | ||
|
|
3d7e8ef432 | ||
|
|
1999c7103c | ||
|
|
9c20ae383e | ||
|
|
76c41eaf05 | ||
|
|
873aa6700f | ||
|
|
0d9590a9a0 | ||
|
|
94607b585a | ||
|
|
da0b0348a2 | ||
|
|
09263455b5 | ||
|
|
ddc23493d4 | ||
|
|
54a71fc142 | ||
|
|
b1a9bf10a2 | ||
|
|
97cfbe1b63 | ||
|
|
0ee8c63551 | ||
|
|
0ac5a9ad82 | ||
|
|
6e51f2d608 | ||
|
|
f23a8dc9dc | ||
|
|
d09fe6178c | ||
|
|
ae2cfcc4f3 | ||
|
|
853cc65e32 | ||
|
|
5d140d949f | ||
|
|
6d2da2b3ba | ||
|
|
165e337e44 | ||
|
|
b154cd0418 | ||
|
|
8ef1532691 | ||
|
|
771e44f2a7 | ||
|
|
8933f1128d | ||
|
|
dbac4b9a63 | ||
|
|
bdac5b5807 | ||
|
|
b0961b5213 | ||
|
|
0d7c3fc4d8 | ||
|
|
8dac256096 | ||
|
|
8320da39c3 | ||
|
|
875937e3d8 | ||
|
|
f880adbaac | ||
|
|
48c5f86adb | ||
|
|
f60f7dab12 | ||
|
|
8f690236ed | ||
|
|
430a4297aa | ||
|
|
69e3ab02c4 | ||
|
|
b87a20b881 | ||
|
|
0e6f2d16ab | ||
|
|
498b061819 | ||
|
|
5ab6f599fb | ||
|
|
8b80626035 | ||
|
|
f86ae79fdb | ||
|
|
1a4e68096f | ||
|
|
d599d1f29e | ||
|
|
73edfb3d2c | ||
|
|
ebae68d579 | ||
|
|
573836e2b8 |
1
.gitignore
vendored
@@ -12,3 +12,4 @@ vendor
|
||||
.gitattributes
|
||||
.DS_Store
|
||||
.nyc_output
|
||||
package-lock.json
|
||||
|
||||
@@ -2,13 +2,14 @@ build
|
||||
node_modules
|
||||
coverage
|
||||
.editorconfig
|
||||
.gitattributes
|
||||
.gitignore
|
||||
test
|
||||
.travis.yml
|
||||
appveyor.yml
|
||||
circle.yml
|
||||
mkdocs.yml
|
||||
docs/css/
|
||||
vendor
|
||||
packaging
|
||||
preinstall.sh
|
||||
.prebuildrc
|
||||
.nyc_output
|
||||
CONTRIBUTING.md
|
||||
|
||||
4
.prebuildrc
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"include-regex": "(sharp\\.node|libvips-cpp\\.dll)",
|
||||
"strip": true
|
||||
}
|
||||
@@ -16,7 +16,7 @@ matrix:
|
||||
- os: linux
|
||||
dist: trusty
|
||||
sudo: false
|
||||
node_js: "9"
|
||||
node_js: "10"
|
||||
- os: osx
|
||||
osx_image: xcode8.3
|
||||
node_js: "4"
|
||||
@@ -28,7 +28,7 @@ matrix:
|
||||
node_js: "8"
|
||||
- os: osx
|
||||
osx_image: xcode8.3
|
||||
node_js: "9"
|
||||
node_js: "10"
|
||||
after_success:
|
||||
- npm install coveralls
|
||||
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
|
||||
|
||||
@@ -41,8 +41,8 @@ Any change that modifies the existing public API should be added to the relevant
|
||||
|
||||
| Release | WIP branch |
|
||||
| ------: | :--------- |
|
||||
| v0.19.0 | suit |
|
||||
| v0.20.0 | teeth |
|
||||
| v0.21.0 | teeth |
|
||||
| v0.22.0 | uptake |
|
||||
|
||||
Please squash your changes into a single commit using a command like `git rebase -i upstream/<wip-branch>`.
|
||||
|
||||
@@ -89,15 +89,6 @@ Requires [Valgrind](http://valgrind.org/).
|
||||
npm run test-leak
|
||||
```
|
||||
|
||||
### Packaging tests
|
||||
|
||||
Tests the installation on a number of Linux-based operating systems.
|
||||
Requires docker.
|
||||
|
||||
```sh
|
||||
npm run test-packaging
|
||||
```
|
||||
|
||||
## Finally
|
||||
|
||||
Please feel free to ask any questions via a
|
||||
|
||||
11
README.md
@@ -4,6 +4,10 @@
|
||||
npm install sharp
|
||||
```
|
||||
|
||||
```sh
|
||||
yarn add sharp
|
||||
```
|
||||
|
||||
The typical use case for this high speed Node.js module
|
||||
is to convert large images in common formats to
|
||||
smaller, web-friendly JPEG, PNG and WebP images of varying dimensions.
|
||||
@@ -17,8 +21,9 @@ Lanczos resampling ensures quality is not sacrificed for speed.
|
||||
As well as image resizing, operations such as
|
||||
rotation, extraction, compositing and gamma correction are available.
|
||||
|
||||
OS X, Windows (x64), Linux (x64, ARM) systems do not require
|
||||
the installation of any external runtime dependencies.
|
||||
Most modern 64-bit OS X, Windows and Linux (glibc) systems running
|
||||
Node versions 4, 6, 8 and 10
|
||||
do not require any additional install or runtime dependencies.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -43,7 +48,7 @@ sharp('input.jpg')
|
||||
```
|
||||
|
||||
```javascript
|
||||
const roundedCorners = new Buffer(
|
||||
const roundedCorners = Buffer.from(
|
||||
'<svg><rect x="0" y="0" width="200" height="200" rx="50" ry="50"/></svg>'
|
||||
);
|
||||
|
||||
|
||||
@@ -7,10 +7,10 @@ environment:
|
||||
- nodejs_version: "4"
|
||||
- nodejs_version: "6"
|
||||
- nodejs_version: "8"
|
||||
- nodejs_version: "9"
|
||||
- nodejs_version: "10"
|
||||
install:
|
||||
- ps: Install-Product node $env:nodejs_version x64
|
||||
- npm install -g npm@5.3.x
|
||||
- npm install -g npm@5
|
||||
- npm install
|
||||
test_script:
|
||||
- npm test
|
||||
|
||||
103
binding.gyp
@@ -5,9 +5,6 @@
|
||||
['OS == "win"', {
|
||||
# Build libvips C++ binding for Windows due to MSVC std library ABI changes
|
||||
'type': 'shared_library',
|
||||
'variables': {
|
||||
'download_vips': '<!(node -e "require(\'./binding\').download_vips()")'
|
||||
},
|
||||
'defines': [
|
||||
'VIPS_CPLUSPLUS_EXPORTS',
|
||||
'_ALLOW_KEYWORD_MACROS'
|
||||
@@ -49,37 +46,15 @@
|
||||
'dependencies': [
|
||||
'libvips-cpp'
|
||||
],
|
||||
# Nested variables "pattern" borrowed from http://src.chromium.org/viewvc/chrome/trunk/src/build/common.gypi
|
||||
'variables': {
|
||||
'variables': {
|
||||
'variables': {
|
||||
'conditions': [
|
||||
['OS != "win"', {
|
||||
# Build the PKG_CONFIG_PATH environment variable with all possible combinations
|
||||
'pkg_config_path': '<!(which brew >/dev/null 2>&1 && eval $(brew --env) && echo $PKG_CONFIG_LIBDIR || true):$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig'
|
||||
}, {
|
||||
'pkg_config_path': ''
|
||||
}]
|
||||
],
|
||||
},
|
||||
'conditions': [
|
||||
['OS != "win"', {
|
||||
# Which version, if any, of libvips is available globally via pkg-config?
|
||||
'global_vips_version': '<!(PKG_CONFIG_PATH="<(pkg_config_path)" pkg-config --modversion vips-cpp 2>/dev/null || true)'
|
||||
}, {
|
||||
'global_vips_version': ''
|
||||
}]
|
||||
],
|
||||
'pkg_config_path%': '<(pkg_config_path)'
|
||||
},
|
||||
'pkg_config_path%': '<(pkg_config_path)',
|
||||
'runtime_link%': 'shared',
|
||||
'conditions': [
|
||||
['OS != "win"', {
|
||||
# Does the globally available version of libvips, if any, meet the minimum version requirement?
|
||||
'use_global_vips': '<!(GLOBAL_VIPS_VERSION="<(global_vips_version)" node -e "require(\'./binding\').use_global_vips()")'
|
||||
'pkg_config_path': '<!(node -e "console.log(require(\'./lib/libvips\').pkgConfigPath())")',
|
||||
'use_global_libvips': '<!(node -e "console.log(Boolean(require(\'./lib/libvips\').useGlobalLibvips()).toString())")'
|
||||
}, {
|
||||
'use_global_vips': ''
|
||||
'pkg_config_path': '',
|
||||
'use_global_libvips': ''
|
||||
}]
|
||||
]
|
||||
},
|
||||
@@ -96,7 +71,7 @@
|
||||
'<!(node -e "require(\'nan\')")'
|
||||
],
|
||||
'conditions': [
|
||||
['use_global_vips == "true"', {
|
||||
['use_global_libvips == "true"', {
|
||||
# Use pkg-config for include and lib
|
||||
'include_dirs': ['<!@(PKG_CONFIG_PATH="<(pkg_config_path)" pkg-config --cflags-only-I vips-cpp vips glib-2.0 | sed s\/-I//g)'],
|
||||
'conditions': [
|
||||
@@ -113,7 +88,7 @@
|
||||
}]
|
||||
]
|
||||
}, {
|
||||
# Attempt to download pre-built libvips and install locally within node_modules
|
||||
# Use pre-built libvips stored locally within node_modules
|
||||
'include_dirs': [
|
||||
'vendor/include',
|
||||
'vendor/include/glib-2.0',
|
||||
@@ -131,9 +106,6 @@
|
||||
]
|
||||
}],
|
||||
['OS == "mac"', {
|
||||
'variables': {
|
||||
'download_vips': '<!(node -e "require(\'./binding\').download_vips()")'
|
||||
},
|
||||
'libraries': [
|
||||
'../vendor/lib/libvips-cpp.42.dylib',
|
||||
'../vendor/lib/libvips.42.dylib',
|
||||
@@ -144,9 +116,6 @@
|
||||
]
|
||||
}],
|
||||
['OS == "linux"', {
|
||||
'variables': {
|
||||
'download_vips': '<!(node -e "require(\'./binding\').download_vips()")'
|
||||
},
|
||||
'defines': [
|
||||
'_GLIBCXX_USE_CXX11_ABI=0'
|
||||
],
|
||||
@@ -219,65 +188,5 @@
|
||||
]
|
||||
}
|
||||
},
|
||||
}, {
|
||||
'target_name': 'win_copy_dlls',
|
||||
'type': 'none',
|
||||
'dependencies': [
|
||||
'sharp'
|
||||
],
|
||||
'conditions': [
|
||||
['OS == "win"', {
|
||||
# Windows lacks support for rpath
|
||||
'copies': [{
|
||||
'destination': 'build/Release',
|
||||
'files': [
|
||||
'vendor/lib/libasprintf-0.dll',
|
||||
'vendor/lib/libcairo-2.dll',
|
||||
'vendor/lib/libcairo-gobject-2.dll',
|
||||
'vendor/lib/libcairo-script-interpreter-2.dll',
|
||||
'vendor/lib/libcharset-1.dll',
|
||||
'vendor/lib/libcroco-0.6-3.dll',
|
||||
'vendor/lib/libexif-12.dll',
|
||||
'vendor/lib/libexpat-1.dll',
|
||||
'vendor/lib/libffi-6.dll',
|
||||
'vendor/lib/libfftw3-3.dll',
|
||||
'vendor/lib/libfontconfig-1.dll',
|
||||
'vendor/lib/libfreetype-6.dll',
|
||||
'vendor/lib/libgcc_s_seh-1.dll',
|
||||
'vendor/lib/libgdk_pixbuf-2.0-0.dll',
|
||||
'vendor/lib/libgettextlib-0-19-8.dll',
|
||||
'vendor/lib/libgettextpo-1.dll',
|
||||
'vendor/lib/libgettextsrc-0-19-8.dll',
|
||||
'vendor/lib/libgif-7.dll',
|
||||
'vendor/lib/libgio-2.0-0.dll',
|
||||
'vendor/lib/libglib-2.0-0.dll',
|
||||
'vendor/lib/libgmodule-2.0-0.dll',
|
||||
'vendor/lib/libgobject-2.0-0.dll',
|
||||
'vendor/lib/libgsf-1-114.dll',
|
||||
'vendor/lib/libgthread-2.0-0.dll',
|
||||
'vendor/lib/libharfbuzz-0.dll',
|
||||
'vendor/lib/libiconv-2.dll',
|
||||
'vendor/lib/libintl-9.dll',
|
||||
'vendor/lib/libjpeg-62.dll',
|
||||
'vendor/lib/liblcms2-2.dll',
|
||||
'vendor/lib/libpango-1.0-0.dll',
|
||||
'vendor/lib/libpangocairo-1.0-0.dll',
|
||||
'vendor/lib/libpangoft2-1.0-0.dll',
|
||||
'vendor/lib/libpangowin32-1.0-0.dll',
|
||||
'vendor/lib/libpixman-1-0.dll',
|
||||
'vendor/lib/libpng16-16.dll',
|
||||
'vendor/lib/libquadmath-0.dll',
|
||||
'vendor/lib/librsvg-2-2.dll',
|
||||
'vendor/lib/libssp-0.dll',
|
||||
'vendor/lib/libstdc++-6.dll',
|
||||
'vendor/lib/libtiff-5.dll',
|
||||
'vendor/lib/libvips-42.dll',
|
||||
'vendor/lib/libwebp-7.dll',
|
||||
'vendor/lib/libxml2-2.dll',
|
||||
'vendor/lib/zlib1.dll'
|
||||
]
|
||||
}]
|
||||
}]
|
||||
]
|
||||
}]
|
||||
}
|
||||
|
||||
108
binding.js
@@ -1,108 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
|
||||
const simpleGet = require('simple-get');
|
||||
const semver = require('semver');
|
||||
const tar = require('tar');
|
||||
const detectLibc = require('detect-libc');
|
||||
|
||||
const agent = require('./lib/agent');
|
||||
const platform = require('./lib/platform');
|
||||
|
||||
// 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;
|
||||
|
||||
const distBaseUrl = process.env.SHARP_DIST_BASE_URL || `https://github.com/lovell/sharp-libvips/releases/download/v${minimumLibvipsVersion}/`;
|
||||
|
||||
// -- Helpers
|
||||
|
||||
const unpack = function (tarPath, done) {
|
||||
const vendorPath = path.join(__dirname, 'vendor');
|
||||
fs.mkdirSync(vendorPath);
|
||||
tar
|
||||
.extract({
|
||||
file: tarPath,
|
||||
cwd: vendorPath,
|
||||
strict: true
|
||||
})
|
||||
.then(done)
|
||||
.catch(error);
|
||||
};
|
||||
|
||||
const error = function (msg) {
|
||||
if (msg instanceof Error) {
|
||||
msg = msg.message;
|
||||
}
|
||||
process.stderr.write(`sharp: ${msg}\n`);
|
||||
process.exit(1);
|
||||
};
|
||||
|
||||
// -- Binary downloaders
|
||||
|
||||
module.exports.download_vips = function () {
|
||||
// Check for existing vendored binaries and verify platform matches
|
||||
const currentPlatformId = platform();
|
||||
try {
|
||||
const vendorPlatformId = require(path.join(__dirname, 'vendor', 'platform.json'));
|
||||
if (currentPlatformId === vendorPlatformId) {
|
||||
return;
|
||||
} else {
|
||||
error(`'${vendorPlatformId}' binaries cannot be used on the '${currentPlatformId}' platform. Please remove the 'node_modules/sharp/vendor' directory and run 'npm install'.`);
|
||||
}
|
||||
} catch (err) {}
|
||||
// Ensure Intel 64-bit or ARM
|
||||
const arch = process.env.npm_config_arch || process.arch;
|
||||
if (arch === 'ia32') {
|
||||
error(`Intel Architecture 32-bit systems require manual installation of libvips >= ${minimumLibvipsVersion} - please see http://sharp.pixelplumbing.com/page/install`);
|
||||
}
|
||||
// Ensure glibc Linux
|
||||
if (detectLibc.isNonGlibcLinux) {
|
||||
error(`Use with ${detectLibc.family} libc requires manual installation of libvips >= ${minimumLibvipsVersion} - please see http://sharp.pixelplumbing.com/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 >= ${minimumLibvipsVersion} - please see http://sharp.pixelplumbing.com/page/install`);
|
||||
}
|
||||
// Arch/platform-specific .tar.gz
|
||||
const tarFilename = ['libvips', minimumLibvipsVersion, currentPlatformId].join('-') + '.tar.gz';
|
||||
// Download to per-process temporary file
|
||||
const tarPathTemp = path.join(os.tmpdir(), `${process.pid}-${tarFilename}`);
|
||||
const tmpFile = fs.createWriteStream(tarPathTemp).on('close', function () {
|
||||
unpack(tarPathTemp, function () {
|
||||
// Attempt to remove temporary file
|
||||
try {
|
||||
fs.unlinkSync(tarPathTemp);
|
||||
} catch (err) {}
|
||||
});
|
||||
});
|
||||
const url = distBaseUrl + tarFilename;
|
||||
const simpleGetOpt = {
|
||||
url: url,
|
||||
agent: agent()
|
||||
};
|
||||
simpleGet(simpleGetOpt, function (err, response) {
|
||||
if (err) {
|
||||
error(`${url} download failed: ${err.message}`);
|
||||
}
|
||||
if (response.statusCode !== 200) {
|
||||
error(`${url} download failed: status ${response.statusCode}`);
|
||||
}
|
||||
response.pipe(tmpFile);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.use_global_vips = function () {
|
||||
const globalVipsVersion = process.env.GLOBAL_VIPS_VERSION;
|
||||
if (globalVipsVersion) {
|
||||
const useGlobalVips = semver.gte(
|
||||
globalVipsVersion,
|
||||
minimumLibvipsVersion
|
||||
);
|
||||
process.stdout.write(useGlobalVips ? 'true' : 'false');
|
||||
} else {
|
||||
process.stdout.write('false');
|
||||
}
|
||||
};
|
||||
@@ -1,20 +1,30 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
### Table of Contents
|
||||
## removeAlpha
|
||||
|
||||
- [extractChannel](#extractchannel)
|
||||
- [joinChannel](#joinchannel)
|
||||
- [bandbool](#bandbool)
|
||||
Remove alpha channel, if any. This is a no-op if the image does not have an alpha channel.
|
||||
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
sharp('rgba.png')
|
||||
.removeAlpha()
|
||||
.toFile('rgb.png', function(err, info) {
|
||||
// rgb.png is a 3 channel image without an alpha channel
|
||||
});
|
||||
```
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
## extractChannel
|
||||
|
||||
Extract a single channel from a multi-channel image.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `channel` **([Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) \| [String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String))** zero-indexed band number to extract, or `red`, `green` or `blue` as alternative to `0`, `1` or `2` respectively.
|
||||
- `channel` **([Number][1] \| [String][2])** zero-indexed band number to extract, or `red`, `green` or `blue` as alternative to `0`, `1` or `2` respectively.
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
sharp(input)
|
||||
@@ -25,7 +35,7 @@ sharp(input)
|
||||
});
|
||||
```
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid channel
|
||||
- Throws **[Error][3]** Invalid channel
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -42,13 +52,13 @@ Channel ordering follows vips convention:
|
||||
Buffers may be any of the image formats supported by sharp: JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data.
|
||||
For raw pixel input, the `options` object should contain a `raw` attribute, which follows the format of the attribute of the same name in the `sharp()` constructor.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `images` **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<([String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Buffer](https://nodejs.org/api/buffer.html))> | [String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Buffer](https://nodejs.org/api/buffer.html))** one or more images (file paths, Buffers).
|
||||
- `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** image options, see `sharp()` constructor.
|
||||
- `images` **([Array][4]<([String][2] \| [Buffer][5])> | [String][2] \| [Buffer][5])** one or more images (file paths, Buffers).
|
||||
- `options` **[Object][6]** image options, see `sharp()` constructor.
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][3]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -56,11 +66,11 @@ Returns **Sharp**
|
||||
|
||||
Perform a bitwise boolean operation on all input image channels (bands) to produce a single channel output image.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `boolOp` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively.
|
||||
- `boolOp` **[String][2]** one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively.
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
sharp('3-channel-rgb-input.png')
|
||||
@@ -72,6 +82,18 @@ sharp('3-channel-rgb-input.png')
|
||||
});
|
||||
```
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][3]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
||||
|
||||
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
||||
|
||||
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||
|
||||
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
|
||||
|
||||
[5]: https://nodejs.org/api/buffer.html
|
||||
|
||||
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
||||
|
||||
@@ -1,13 +1,5 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
### Table of Contents
|
||||
|
||||
- [background](#background)
|
||||
- [greyscale](#greyscale)
|
||||
- [grayscale](#grayscale)
|
||||
- [toColourspace](#tocolourspace)
|
||||
- [toColorspace](#tocolorspace)
|
||||
|
||||
## background
|
||||
|
||||
Set the background for the `embed`, `flatten` and `extend` operations.
|
||||
@@ -17,12 +9,26 @@ Delegates to the _color_ module, which can throw an Error
|
||||
but is liberal in what it accepts, clipping values to sensible min/max.
|
||||
The alpha value is a float between `0` (transparent) and `1` (opaque).
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `rgba` **([String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Object](https://developer.mozilla.org/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.
|
||||
- `rgba` **([String][1] \| [Object][2])** parsed by the [color][3] module to extract values for red, green, blue and alpha.
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameter
|
||||
- Throws **[Error][4]** Invalid parameter
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
## tint
|
||||
|
||||
Tint the image using the provided chroma while preserving the image luminance.
|
||||
An alpha channel may be present and will be unchanged by the operation.
|
||||
|
||||
### Parameters
|
||||
|
||||
- `rgb` **([String][1] \| [Object][2])** parsed by the [color][3] module to extract chroma values.
|
||||
|
||||
|
||||
- Throws **[Error][4]** Invalid parameter
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -35,9 +41,9 @@ This may be overridden by other sharp operations such as `toColourspace('b-w')`,
|
||||
which will produce an output image containing one color channel.
|
||||
An alpha channel may be present, and will be unchanged by the operation.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `greyscale` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
- `greyscale` **[Boolean][5]** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -45,9 +51,9 @@ Returns **Sharp**
|
||||
|
||||
Alternative spelling of `greyscale`.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `grayscale` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
- `grayscale` **[Boolean][5]** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -56,12 +62,12 @@ Returns **Sharp**
|
||||
Set the output colourspace.
|
||||
By default output image will be web-friendly sRGB, with additional channels interpreted as alpha channels.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `colourspace` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** output colourspace e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://github.com/jcupitt/libvips/blob/master/libvips/iofuncs/enumtypes.c#L568)
|
||||
- `colourspace` **[String][1]?** output colourspace e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...][6]
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][4]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -69,11 +75,23 @@ Returns **Sharp**
|
||||
|
||||
Alternative spelling of `toColourspace`.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `colorspace` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** output colorspace.
|
||||
- `colorspace` **[String][1]?** output colorspace.
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][4]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
||||
|
||||
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
||||
|
||||
[3]: https://www.npmjs.org/package/color
|
||||
|
||||
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||
|
||||
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
||||
|
||||
[6]: https://github.com/jcupitt/libvips/blob/master/libvips/iofuncs/enumtypes.c#L568
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
### Table of Contents
|
||||
|
||||
- [overlayWith](#overlaywith)
|
||||
|
||||
## overlayWith
|
||||
|
||||
Overlay (composite) an image over the processed (resized, extracted etc.) image.
|
||||
@@ -13,27 +9,27 @@ If both `top` and `left` options are provided, they take precedence over `gravit
|
||||
|
||||
If the overlay image contains an alpha channel then composition with premultiplication will occur.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `overlay` **([Buffer](https://nodejs.org/api/buffer.html) \| [String](https://developer.mozilla.org/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/docs/Web/JavaScript/Reference/Global_Objects/Object)?**
|
||||
- `options.gravity` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** gravity at which to place the overlay. (optional, default `'centre'`)
|
||||
- `options.top` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** the pixel offset from the top edge.
|
||||
- `options.left` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** the pixel offset from the left edge.
|
||||
- `options.tile` **[Boolean](https://developer.mozilla.org/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/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/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/docs/Web/JavaScript/Reference/Global_Objects/Object)?** describes overlay when using raw pixel data.
|
||||
- `options.raw.width` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.raw.height` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.raw.channels` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.create` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** describes a blank overlay to be created.
|
||||
- `options.create.width` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.create.height` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.create.channels` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** 3-4
|
||||
- `options.create.background` **([String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Object](https://developer.mozilla.org/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.
|
||||
- `overlay` **([Buffer][1] \| [String][2])** Buffer containing image data or String containing the path to an image file.
|
||||
- `options` **[Object][3]?**
|
||||
- `options.gravity` **[String][2]** gravity at which to place the overlay. (optional, default `'centre'`)
|
||||
- `options.top` **[Number][4]?** the pixel offset from the top edge.
|
||||
- `options.left` **[Number][4]?** the pixel offset from the left edge.
|
||||
- `options.tile` **[Boolean][5]** set to true to repeat the overlay image across the entire image with the given `gravity`. (optional, default `false`)
|
||||
- `options.cutout` **[Boolean][5]** 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][4]** integral number representing the DPI for vector overlay image. (optional, default `72`)
|
||||
- `options.raw` **[Object][3]?** describes overlay when using raw pixel data.
|
||||
- `options.raw.width` **[Number][4]?**
|
||||
- `options.raw.height` **[Number][4]?**
|
||||
- `options.raw.channels` **[Number][4]?**
|
||||
- `options.create` **[Object][3]?** describes a blank overlay to be created.
|
||||
- `options.create.width` **[Number][4]?**
|
||||
- `options.create.height` **[Number][4]?**
|
||||
- `options.create.channels` **[Number][4]?** 3-4
|
||||
- `options.create.background` **([String][2] \| [Object][3])?** parsed by the [color][6] module to extract values for red, green, blue and alpha.
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
sharp('input.png')
|
||||
@@ -44,8 +40,7 @@ sharp('input.png')
|
||||
.overlayWith('overlay.png', { gravity: sharp.gravity.southeast } )
|
||||
.sharpen()
|
||||
.withMetadata()
|
||||
.quality(90)
|
||||
.webp()
|
||||
.webp( { quality: 90 } )
|
||||
.toBuffer()
|
||||
.then(function(outputBuffer) {
|
||||
// outputBuffer contains upside down, 300px wide, alpha channel flattened
|
||||
@@ -54,6 +49,20 @@ sharp('input.png')
|
||||
});
|
||||
```
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][7]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
[1]: https://nodejs.org/api/buffer.html
|
||||
|
||||
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
||||
|
||||
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
||||
|
||||
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
||||
|
||||
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
||||
|
||||
[6]: https://www.npmjs.org/package/color
|
||||
|
||||
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||
|
||||
@@ -1,36 +1,30 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
### Table of Contents
|
||||
|
||||
- [Sharp](#sharp)
|
||||
- [format](#format)
|
||||
- [versions](#versions)
|
||||
- [queue](#queue)
|
||||
|
||||
## Sharp
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `input` **([Buffer](https://nodejs.org/api/buffer.html) \| [String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String))?** if present, can be
|
||||
- `input` **([Buffer][1] \| [String][2])?** 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 not present.
|
||||
- `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** if present, is an Object with optional attributes.
|
||||
- `options.failOnError` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** by default apply a "best effort"
|
||||
- `options` **[Object][3]?** if present, is an Object with optional attributes.
|
||||
- `options.failOnError` **[Boolean][4]** by default apply a "best effort"
|
||||
to decode images, even if the data is corrupt or invalid. Set this flag to true
|
||||
if you'd rather halt processing and raise an error when loading invalid images. (optional, default `false`)
|
||||
- `options.density` **[Number](https://developer.mozilla.org/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/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/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.raw.height` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.raw.channels` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** 1-4
|
||||
- `options.create` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** describes a new image to be created.
|
||||
- `options.create.width` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.create.height` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.create.channels` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** 3-4
|
||||
- `options.create.background` **([String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Object](https://developer.mozilla.org/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.
|
||||
- `options.density` **[Number][5]** integral number representing the DPI for vector images. (optional, default `72`)
|
||||
- `options.page` **[Number][5]** page number to extract for multi-page input (GIF, TIFF) (optional, default `0`)
|
||||
- `options.raw` **[Object][3]?** describes raw pixel input image data. See `raw()` for pixel ordering.
|
||||
- `options.raw.width` **[Number][5]?**
|
||||
- `options.raw.height` **[Number][5]?**
|
||||
- `options.raw.channels` **[Number][5]?** 1-4
|
||||
- `options.create` **[Object][3]?** describes a new image to be created.
|
||||
- `options.create.width` **[Number][5]?**
|
||||
- `options.create.height` **[Number][5]?**
|
||||
- `options.create.channels` **[Number][5]?** 3-4
|
||||
- `options.create.background` **([String][2] \| [Object][3])?** parsed by the [color][6] module to extract values for red, green, blue and alpha.
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
sharp('input.jpg')
|
||||
@@ -61,7 +55,7 @@ sharp({
|
||||
width: 300,
|
||||
height: 200,
|
||||
channels: 4,
|
||||
background: { r: 255, g: 0, b: 0, alpha: 128 }
|
||||
background: { r: 255, g: 0, b: 0, alpha: 0.5 }
|
||||
}
|
||||
})
|
||||
.png()
|
||||
@@ -69,27 +63,27 @@ sharp({
|
||||
.then( ... );
|
||||
```
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][7]** Invalid parameters
|
||||
|
||||
Returns **[Sharp](#sharp)**
|
||||
Returns **[Sharp][8]**
|
||||
|
||||
### format
|
||||
|
||||
An Object containing nested boolean values representing the available input and output formats/methods.
|
||||
|
||||
**Examples**
|
||||
#### Examples
|
||||
|
||||
```javascript
|
||||
console.log(sharp.format);
|
||||
```
|
||||
|
||||
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
||||
Returns **[Object][3]**
|
||||
|
||||
### versions
|
||||
|
||||
An Object containing the version numbers of libvips and its dependencies.
|
||||
|
||||
**Examples**
|
||||
#### Examples
|
||||
|
||||
```javascript
|
||||
console.log(sharp.versions);
|
||||
@@ -102,10 +96,26 @@ An EventEmitter that emits a `change` event when a task is either:
|
||||
- queued, waiting for _libuv_ to provide a worker thread
|
||||
- complete
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
sharp.queue.on('change', function(queueLength) {
|
||||
console.log('Queue contains ' + queueLength + ' task(s)');
|
||||
});
|
||||
```
|
||||
|
||||
[1]: https://nodejs.org/api/buffer.html
|
||||
|
||||
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
||||
|
||||
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
||||
|
||||
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
||||
|
||||
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
||||
|
||||
[6]: https://www.npmjs.org/package/color
|
||||
|
||||
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||
|
||||
[8]: #sharp
|
||||
|
||||
@@ -1,20 +1,12 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
### Table of Contents
|
||||
|
||||
- [clone](#clone)
|
||||
- [metadata](#metadata)
|
||||
- [stats](#stats)
|
||||
- [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.
|
||||
This allows multiple output Streams and therefore multiple processing pipelines to share a single input Stream.
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
const pipeline = sharp().rotate();
|
||||
@@ -33,25 +25,25 @@ Fast access to (uncached) image metadata without decoding any compressed image d
|
||||
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#L636)
|
||||
- `width`: Number of pixels wide (EXIF orientation is not taken into consideration)
|
||||
- `height`: Number of pixels high (EXIF orientation is not taken into consideration)
|
||||
- `space`: Name of colour space interpretation e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...][1]
|
||||
- `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)
|
||||
- `depth`: Name of pixel depth format e.g. `uchar`, `char`, `ushort`, `float` [...][2]
|
||||
- `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
|
||||
- `orientation`: Number value of the EXIF Orientation header, if present
|
||||
- `exif`: Buffer containing raw EXIF data, if present
|
||||
- `icc`: Buffer containing raw [ICC](https://www.npmjs.com/package/icc) profile data, if present
|
||||
- `icc`: Buffer containing raw [ICC][3] profile data, if present
|
||||
- `iptc`: Buffer containing raw IPTC data, if present
|
||||
- `xmp`: Buffer containing raw XMP data, if present
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `callback` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** called with the arguments `(err, metadata)`
|
||||
- `callback` **[Function][4]?** called with the arguments `(err, metadata)`
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
const image = sharp(inputJpg);
|
||||
@@ -68,7 +60,7 @@ image
|
||||
});
|
||||
```
|
||||
|
||||
Returns **([Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)> | Sharp)**
|
||||
Returns **([Promise][5]<[Object][6]> | Sharp)**
|
||||
|
||||
## stats
|
||||
|
||||
@@ -87,12 +79,13 @@ A Promise is returned when `callback` is not provided.
|
||||
- `maxX` (x-coordinate of one of the pixel where the maximum lies)
|
||||
- `maxY` (y-coordinate of one of the pixel where the maximum lies)
|
||||
- `isOpaque`: Value to identify if the image is opaque or transparent, based on the presence and use of alpha channel
|
||||
- `entropy`: Histogram-based estimation of greyscale entropy, discarding alpha channel if any (experimental)
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `callback` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** called with the arguments `(err, stats)`
|
||||
- `callback` **[Function][4]?** called with the arguments `(err, stats)`
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
const image = sharp(inputJpg);
|
||||
@@ -103,7 +96,7 @@ image
|
||||
});
|
||||
```
|
||||
|
||||
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)>**
|
||||
Returns **[Promise][5]<[Object][6]>**
|
||||
|
||||
## limitInputPixels
|
||||
|
||||
@@ -111,12 +104,12 @@ Do not process input images where the number of pixels (width _ height) exceeds
|
||||
Assumes image dimensions contained in the input metadata can be trusted.
|
||||
The default limit is 268402689 (0x3FFF _ 0x3FFF) pixels.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `limit` **([Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) \| [Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean))** an integral Number of pixels, zero or false to remove limit, true to use default limit.
|
||||
- `limit` **([Number][7] \| [Boolean][8])** an integral Number of pixels, zero or false to remove limit, true to use default limit.
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid limit
|
||||
- Throws **[Error][9]** Invalid limit
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -127,8 +120,26 @@ This will reduce memory usage and can improve performance on some systems.
|
||||
|
||||
The default behaviour _before_ function call is `false`, meaning the libvips access method is not sequential.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `sequentialRead` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
- `sequentialRead` **[Boolean][8]** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
[1]: https://github.com/jcupitt/libvips/blob/master/libvips/iofuncs/enumtypes.c#L636
|
||||
|
||||
[2]: https://github.com/jcupitt/libvips/blob/master/libvips/iofuncs/enumtypes.c#L672
|
||||
|
||||
[3]: https://www.npmjs.com/package/icc
|
||||
|
||||
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
|
||||
|
||||
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise
|
||||
|
||||
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
||||
|
||||
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
||||
|
||||
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
||||
|
||||
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||
|
||||
@@ -1,24 +1,5 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
### 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
|
||||
@@ -35,11 +16,11 @@ The use of `rotate` implies the removal of the EXIF `Orientation` tag, if any.
|
||||
Method order is important when both rotating and extracting regions,
|
||||
for example `rotate(x).extract(y)` will produce a different result to `extract(y).rotate(x)`.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `angle` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** angle of rotation, must be a multiple of 90. (optional, default `auto`)
|
||||
- `angle` **[Number][1]** angle of rotation, must be a multiple of 90. (optional, default `auto`)
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
const pipeline = sharp()
|
||||
@@ -53,7 +34,7 @@ const pipeline = sharp()
|
||||
readableStream.pipe(pipeline);
|
||||
```
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][2]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -65,15 +46,15 @@ Extract a region of the image.
|
||||
- Use `extract` after `resize` for post-resize extraction.
|
||||
- Use `extract` before and after for both.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
||||
- `options.left` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** zero-indexed offset from left edge
|
||||
- `options.top` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** zero-indexed offset from top edge
|
||||
- `options.width` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** dimension of extracted image
|
||||
- `options.height` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** dimension of extracted image
|
||||
- `options` **[Object][3]**
|
||||
- `options.left` **[Number][1]** zero-indexed offset from left edge
|
||||
- `options.top` **[Number][1]** zero-indexed offset from top edge
|
||||
- `options.width` **[Number][1]** dimension of extracted image
|
||||
- `options.height` **[Number][1]** dimension of extracted image
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
sharp(input)
|
||||
@@ -93,7 +74,7 @@ sharp(input)
|
||||
});
|
||||
```
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][2]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -102,9 +83,9 @@ Returns **Sharp**
|
||||
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**
|
||||
### Parameters
|
||||
|
||||
- `flip` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
- `flip` **[Boolean][4]** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -113,9 +94,9 @@ Returns **Sharp**
|
||||
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**
|
||||
### Parameters
|
||||
|
||||
- `flop` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
- `flop` **[Boolean][4]** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -126,14 +107,28 @@ When used without parameters, performs a fast, mild sharpen of the output image.
|
||||
When a `sigma` is provided, performs a slower, more accurate sharpen of the L channel in the LAB colour space.
|
||||
Separate control over the level of sharpening in "flat" and "jagged" areas is available.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `sigma` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`.
|
||||
- `flat` **[Number](https://developer.mozilla.org/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/docs/Web/JavaScript/Reference/Global_Objects/Number)** the level of sharpening to apply to "jagged" areas. (optional, default `2.0`)
|
||||
- `sigma` **[Number][1]?** the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`.
|
||||
- `flat` **[Number][1]** the level of sharpening to apply to "flat" areas. (optional, default `1.0`)
|
||||
- `jagged` **[Number][1]** the level of sharpening to apply to "jagged" areas. (optional, default `2.0`)
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][2]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
## median
|
||||
|
||||
Apply median filter.
|
||||
When used without parameters the default window is 3x3.
|
||||
|
||||
### Parameters
|
||||
|
||||
- `size` **[Number][1]** square mask size: size x size (optional, default `3`)
|
||||
|
||||
|
||||
- Throws **[Error][2]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -143,12 +138,12 @@ Blur the image.
|
||||
When used without parameters, performs a fast, mild blur of the output image.
|
||||
When a `sigma` is provided, performs a slower, more accurate Gaussian blur.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `sigma` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** a value between 0.3 and 1000 representing the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`.
|
||||
- `sigma` **[Number][1]?** a value between 0.3 and 1000 representing the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`.
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][2]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -157,15 +152,15 @@ Returns **Sharp**
|
||||
Extends/pads the edges of the image with the colour provided to the `background` method.
|
||||
This operation will always occur after resizing and extraction, if any.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `extend` **([Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))** single pixel count to add to all edges or an Object with per-edge counts
|
||||
- `extend.top` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `extend.left` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `extend.bottom` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `extend.right` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `extend` **([Number][1] \| [Object][3])** single pixel count to add to all edges or an Object with per-edge counts
|
||||
- `extend.top` **[Number][1]?**
|
||||
- `extend.left` **[Number][1]?**
|
||||
- `extend.bottom` **[Number][1]?**
|
||||
- `extend.right` **[Number][1]?**
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
// Resize to 140 pixels wide, then add 10 transparent pixels
|
||||
@@ -177,7 +172,7 @@ sharp(input)
|
||||
...
|
||||
```
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][2]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -185,9 +180,9 @@ Returns **Sharp**
|
||||
|
||||
Merge alpha transparency channel, if any, with `background`.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `flatten` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
- `flatten` **[Boolean][4]** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -195,12 +190,12 @@ Returns **Sharp**
|
||||
|
||||
Trim "boring" pixels from all edges that contain values within a percentage similarity of the top-left pixel.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `tolerance` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** value between 1 and 99 representing the percentage similarity. (optional, default `10`)
|
||||
- `tolerance` **[Number][1]** value between 1 and 99 representing the percentage similarity. (optional, default `10`)
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][2]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -212,12 +207,12 @@ This can improve the perceived brightness of a resized image in non-linear colou
|
||||
JPEG and WebP input images will not take advantage of the shrink-on-load performance optimisation
|
||||
when applying a gamma correction.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `gamma` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** value between 1.0 and 3.0. (optional, default `2.2`)
|
||||
- `gamma` **[Number][1]** value between 1.0 and 3.0. (optional, default `2.2`)
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][2]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -225,9 +220,9 @@ Returns **Sharp**
|
||||
|
||||
Produce the "negative" of the image.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `negate` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
- `negate` **[Boolean][4]** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -235,9 +230,9 @@ Returns **Sharp**
|
||||
|
||||
Enhance output image contrast by stretching its luminance to cover the full dynamic range.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `normalise` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
- `normalise` **[Boolean][4]** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -245,9 +240,9 @@ Returns **Sharp**
|
||||
|
||||
Alternative spelling of normalise.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `normalize` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
- `normalize` **[Boolean][4]** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -255,16 +250,16 @@ Returns **Sharp**
|
||||
|
||||
Convolve the image with the specified kernel.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `kernel` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
||||
- `kernel.width` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** width of the kernel in pixels.
|
||||
- `kernel.height` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** width of the kernel in pixels.
|
||||
- `kernel.kernel` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)>** Array of length `width*height` containing the kernel values.
|
||||
- `kernel.scale` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** the scale of the kernel in pixels. (optional, default `sum`)
|
||||
- `kernel.offset` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** the offset of the kernel in pixels. (optional, default `0`)
|
||||
- `kernel` **[Object][3]**
|
||||
- `kernel.width` **[Number][1]** width of the kernel in pixels.
|
||||
- `kernel.height` **[Number][1]** width of the kernel in pixels.
|
||||
- `kernel.kernel` **[Array][5]<[Number][1]>** Array of length `width*height` containing the kernel values.
|
||||
- `kernel.scale` **[Number][1]** the scale of the kernel in pixels. (optional, default `sum`)
|
||||
- `kernel.offset` **[Number][1]** the offset of the kernel in pixels. (optional, default `0`)
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
sharp(input)
|
||||
@@ -280,7 +275,7 @@ sharp(input)
|
||||
});
|
||||
```
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][2]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -288,15 +283,15 @@ Returns **Sharp**
|
||||
|
||||
Any pixel value greather than or equal to the threshold value will be set to 255, otherwise it will be set to 0.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `threshold` **[Number](https://developer.mozilla.org/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/docs/Web/JavaScript/Reference/Global_Objects/Object)?**
|
||||
- `options.greyscale` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** convert to single channel greyscale. (optional, default `true`)
|
||||
- `options.grayscale` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** alternative spelling for greyscale. (optional, default `true`)
|
||||
- `threshold` **[Number][1]** a value in the range 0-255 representing the level at which the threshold will be applied. (optional, default `128`)
|
||||
- `options` **[Object][3]?**
|
||||
- `options.greyscale` **[Boolean][4]** convert to single channel greyscale. (optional, default `true`)
|
||||
- `options.grayscale` **[Boolean][4]** alternative spelling for greyscale. (optional, default `true`)
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][2]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -307,17 +302,45 @@ Perform a bitwise boolean operation with operand image.
|
||||
This operation creates an output image where each pixel is the result of
|
||||
the selected bitwise boolean `operation` between the corresponding pixels of the input images.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `operand` **([Buffer](https://nodejs.org/api/buffer.html) \| [String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String))** Buffer containing image data or String containing the path to an image file.
|
||||
- `operator` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively.
|
||||
- `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?**
|
||||
- `options.raw` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** describes operand when using raw pixel data.
|
||||
- `options.raw.width` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.raw.height` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `options.raw.channels` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `operand` **([Buffer][6] \| [String][7])** Buffer containing image data or String containing the path to an image file.
|
||||
- `operator` **[String][7]** one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively.
|
||||
- `options` **[Object][3]?**
|
||||
- `options.raw` **[Object][3]?** describes operand when using raw pixel data.
|
||||
- `options.raw.width` **[Number][1]?**
|
||||
- `options.raw.height` **[Number][1]?**
|
||||
- `options.raw.channels` **[Number][1]?**
|
||||
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][2]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
## linear
|
||||
|
||||
Apply the linear formula a \* input + b to the image (levels adjustment)
|
||||
|
||||
### Parameters
|
||||
|
||||
- `a` **[Number][1]** multiplier (optional, default `1.0`)
|
||||
- `b` **[Number][1]** offset (optional, default `0.0`)
|
||||
|
||||
|
||||
- Throws **[Error][2]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
||||
|
||||
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||
|
||||
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
||||
|
||||
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
||||
|
||||
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
|
||||
|
||||
[6]: https://nodejs.org/api/buffer.html
|
||||
|
||||
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
||||
|
||||
@@ -1,18 +1,5 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
### 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.
|
||||
@@ -23,18 +10,31 @@ Note that raw pixel data is only supported for buffer output.
|
||||
|
||||
A `Promise` is returned when `callback` is not provided.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `fileOut` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the path to write the image data to.
|
||||
- `callback` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** called on completion with two arguments `(err, info)`.
|
||||
- `fileOut` **[String][1]** the path to write the image data to.
|
||||
- `callback` **[Function][2]?** called on completion with two arguments `(err, info)`.
|
||||
`info` contains the output image `format`, `size` (bytes), `width`, `height`,
|
||||
`channels` and `premultiplied` (indicating if premultiplication was used).
|
||||
When using a crop strategy also contains `cropOffsetLeft` and `cropOffsetTop`.
|
||||
|
||||
### Examples
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
```javascript
|
||||
sharp(input)
|
||||
.toFile('output.png', (err, info) => { ... });
|
||||
```
|
||||
|
||||
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)>** when no callback is provided
|
||||
```javascript
|
||||
sharp(input)
|
||||
.toFile('output.png')
|
||||
.then(info => { ... })
|
||||
.catch(err => { ... });
|
||||
```
|
||||
|
||||
- Throws **[Error][3]** Invalid parameters
|
||||
|
||||
Returns **[Promise][4]<[Object][5]>** when no callback is provided
|
||||
|
||||
## toBuffer
|
||||
|
||||
@@ -52,13 +52,34 @@ By default, the format will match the input image, except GIF and SVG input whic
|
||||
|
||||
A `Promise` is returned when `callback` is not provided.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?**
|
||||
- `options.resolveWithObject` **[Boolean](https://developer.mozilla.org/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/docs/Web/JavaScript/Reference/Statements/function)?**
|
||||
- `options` **[Object][5]?**
|
||||
- `options.resolveWithObject` **[Boolean][6]?** Resolve the Promise with an Object containing `data` and `info` properties instead of resolving only with `data`.
|
||||
- `callback` **[Function][2]?**
|
||||
|
||||
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Buffer](https://nodejs.org/api/buffer.html)>** when no callback is provided
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
sharp(input)
|
||||
.toBuffer((err, data, info) => { ... });
|
||||
```
|
||||
|
||||
```javascript
|
||||
sharp(input)
|
||||
.toBuffer()
|
||||
.then(data => { ... })
|
||||
.catch(err => { ... });
|
||||
```
|
||||
|
||||
```javascript
|
||||
sharp(input)
|
||||
.toBuffer({ resolveWithObject: true })
|
||||
.then(({ data, info }) => { ... })
|
||||
.catch(err => { ... });
|
||||
```
|
||||
|
||||
Returns **[Promise][4]<[Buffer][7]>** when no callback is provided
|
||||
|
||||
## withMetadata
|
||||
|
||||
@@ -66,13 +87,21 @@ 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.
|
||||
This will also convert to and add a web-friendly sRGB ICC profile.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `withMetadata` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?**
|
||||
- `withMetadata.orientation` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** value between 1 and 8, used to update the EXIF `Orientation` tag.
|
||||
- `withMetadata` **[Object][5]?**
|
||||
- `withMetadata.orientation` **[Number][8]?** value between 1 and 8, used to update the EXIF `Orientation` tag.
|
||||
|
||||
### Examples
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
```javascript
|
||||
sharp('input.jpg')
|
||||
.withMetadata()
|
||||
.toFile('output-with-metadata.jpg')
|
||||
.then(info => { ... });
|
||||
```
|
||||
|
||||
- Throws **[Error][3]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -80,20 +109,35 @@ Returns **Sharp**
|
||||
|
||||
Use these JPEG options for output image.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** output options
|
||||
- `options.quality` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** quality, integer 1-100 (optional, default `80`)
|
||||
- `options.progressive` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** use progressive (interlace) scan (optional, default `false`)
|
||||
- `options.chromaSubsampling` **[String](https://developer.mozilla.org/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/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** apply trellis quantisation, requires mozjpeg (optional, default `false`)
|
||||
- `options.overshootDeringing` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** apply overshoot deringing, requires mozjpeg (optional, default `false`)
|
||||
- `options.optimiseScans` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** optimise progressive scans, forces progressive, requires mozjpeg (optional, default `false`)
|
||||
- `options.optimizeScans` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** alternative spelling of optimiseScans (optional, default `false`)
|
||||
- `options.force` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** force JPEG output, otherwise attempt to use input format (optional, default `true`)
|
||||
- `options` **[Object][5]?** output options
|
||||
- `options.quality` **[Number][8]** quality, integer 1-100 (optional, default `80`)
|
||||
- `options.progressive` **[Boolean][6]** use progressive (interlace) scan (optional, default `false`)
|
||||
- `options.chromaSubsampling` **[String][1]** set to '4:4:4' to prevent chroma subsampling when quality <= 90 (optional, default `'4:2:0'`)
|
||||
- `options.trellisQuantisation` **[Boolean][6]** apply trellis quantisation, requires mozjpeg (optional, default `false`)
|
||||
- `options.overshootDeringing` **[Boolean][6]** apply overshoot deringing, requires mozjpeg (optional, default `false`)
|
||||
- `options.optimiseScans` **[Boolean][6]** optimise progressive scans, forces progressive, requires mozjpeg (optional, default `false`)
|
||||
- `options.optimizeScans` **[Boolean][6]** alternative spelling of optimiseScans (optional, default `false`)
|
||||
- `options.optimiseCoding` **[Boolean][6]** optimise Huffman coding tables (optional, default `true`)
|
||||
- `options.optimizeCoding` **[Boolean][6]** alternative spelling of optimiseCoding (optional, default `true`)
|
||||
- `options.quantisationTable` **[Number][8]** quantization table to use, integer 0-8, requires mozjpeg (optional, default `0`)
|
||||
- `options.quantizationTable` **[Number][8]** alternative spelling of quantisationTable (optional, default `0`)
|
||||
- `options.force` **[Boolean][6]** force JPEG output, otherwise attempt to use input format (optional, default `true`)
|
||||
|
||||
### Examples
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid options
|
||||
```javascript
|
||||
// Convert any input to very high quality JPEG output
|
||||
const data = await sharp(input)
|
||||
.jpeg({
|
||||
quality: 100,
|
||||
chromaSubsampling: '4:4:4'
|
||||
})
|
||||
.toBuffer();
|
||||
```
|
||||
|
||||
- Throws **[Error][3]** Invalid options
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -101,16 +145,27 @@ Returns **Sharp**
|
||||
|
||||
Use these PNG options for output image.
|
||||
|
||||
**Parameters**
|
||||
PNG output is always full colour at 8 or 16 bits per pixel.
|
||||
Indexed PNG input at 1, 2 or 4 bits per pixel is converted to 8 bits per pixel.
|
||||
|
||||
- `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?**
|
||||
- `options.progressive` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** use progressive (interlace) scan (optional, default `false`)
|
||||
- `options.compressionLevel` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** zlib compression level, 0-9 (optional, default `9`)
|
||||
- `options.adaptiveFiltering` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** use adaptive row filtering (optional, default `false`)
|
||||
- `options.force` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** force PNG output, otherwise attempt to use input format (optional, default `true`)
|
||||
### Parameters
|
||||
|
||||
- `options` **[Object][5]?**
|
||||
- `options.progressive` **[Boolean][6]** use progressive (interlace) scan (optional, default `false`)
|
||||
- `options.compressionLevel` **[Number][8]** zlib compression level, 0-9 (optional, default `9`)
|
||||
- `options.adaptiveFiltering` **[Boolean][6]** use adaptive row filtering (optional, default `false`)
|
||||
- `options.force` **[Boolean][6]** force PNG output, otherwise attempt to use input format (optional, default `true`)
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid options
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
// Convert any input to PNG output
|
||||
const data = await sharp(input)
|
||||
.png()
|
||||
.toBuffer();
|
||||
```
|
||||
|
||||
- Throws **[Error][3]** Invalid options
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -118,17 +173,25 @@ Returns **Sharp**
|
||||
|
||||
Use these WebP options for output image.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** output options
|
||||
- `options.quality` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** quality, integer 1-100 (optional, default `80`)
|
||||
- `options.alphaQuality` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** quality of alpha layer, integer 0-100 (optional, default `100`)
|
||||
- `options.lossless` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** use lossless compression mode (optional, default `false`)
|
||||
- `options.nearLossless` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** use near_lossless compression mode (optional, default `false`)
|
||||
- `options.force` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** force WebP output, otherwise attempt to use input format (optional, default `true`)
|
||||
- `options` **[Object][5]?** output options
|
||||
- `options.quality` **[Number][8]** quality, integer 1-100 (optional, default `80`)
|
||||
- `options.alphaQuality` **[Number][8]** quality of alpha layer, integer 0-100 (optional, default `100`)
|
||||
- `options.lossless` **[Boolean][6]** use lossless compression mode (optional, default `false`)
|
||||
- `options.nearLossless` **[Boolean][6]** use near_lossless compression mode (optional, default `false`)
|
||||
- `options.force` **[Boolean][6]** force WebP output, otherwise attempt to use input format (optional, default `true`)
|
||||
|
||||
### Examples
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid options
|
||||
```javascript
|
||||
// Convert any input to lossless WebP output
|
||||
const data = await sharp(input)
|
||||
.webp({ lossless: true })
|
||||
.toBuffer();
|
||||
```
|
||||
|
||||
- Throws **[Error][3]** Invalid options
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -136,19 +199,31 @@ Returns **Sharp**
|
||||
|
||||
Use these TIFF options for output image.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** output options
|
||||
- `options.quality` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** quality, integer 1-100 (optional, default `80`)
|
||||
- `options.force` **[Boolean](https://developer.mozilla.org/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/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** compression options: lzw, deflate, jpeg (optional, default `'jpeg'`)
|
||||
- `options.predictor` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** compression predictor options: none, horizontal, float (optional, default `'horizontal'`)
|
||||
- `options.xres` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** horizontal resolution in pixels/mm (optional, default `1.0`)
|
||||
- `options.yres` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** vertical resolution in pixels/mm (optional, default `1.0`)
|
||||
- `options.squash` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** squash 8-bit images down to 1 bit (optional, default `false`)
|
||||
- `options` **[Object][5]?** output options
|
||||
- `options.quality` **[Number][8]** quality, integer 1-100 (optional, default `80`)
|
||||
- `options.force` **[Boolean][6]** force TIFF output, otherwise attempt to use input format (optional, default `true`)
|
||||
- `options.compression` **[Boolean][6]** compression options: lzw, deflate, jpeg, ccittfax4 (optional, default `'jpeg'`)
|
||||
- `options.predictor` **[Boolean][6]** compression predictor options: none, horizontal, float (optional, default `'horizontal'`)
|
||||
- `options.xres` **[Number][8]** horizontal resolution in pixels/mm (optional, default `1.0`)
|
||||
- `options.yres` **[Number][8]** vertical resolution in pixels/mm (optional, default `1.0`)
|
||||
- `options.squash` **[Boolean][6]** squash 8-bit images down to 1 bit (optional, default `false`)
|
||||
|
||||
### Examples
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid options
|
||||
```javascript
|
||||
// Convert SVG input to LZW-compressed, 1 bit per pixel TIFF output
|
||||
sharp('input.svg')
|
||||
.tiff({
|
||||
compression: 'lzw',
|
||||
squash: true
|
||||
})
|
||||
.toFile('1-bpp-output.tiff')
|
||||
.then(info => { ... });
|
||||
```
|
||||
|
||||
- Throws **[Error][3]** Invalid options
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -156,19 +231,36 @@ Returns **Sharp**
|
||||
|
||||
Force output to be raw, uncompressed uint8 pixel data.
|
||||
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
// Extract raw RGB pixel data from JPEG input
|
||||
const { data, info } = await sharp('input.jpg')
|
||||
.raw()
|
||||
.toBuffer({ resolveWithObject: true });
|
||||
```
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
## toFormat
|
||||
|
||||
Force output to a given format.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `format` **([String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))** as a String or an Object with an 'id' attribute
|
||||
- `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** output options
|
||||
- `format` **([String][1] \| [Object][5])** as a String or an Object with an 'id' attribute
|
||||
- `options` **[Object][5]** output options
|
||||
|
||||
### Examples
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** unsupported format or options
|
||||
```javascript
|
||||
// Convert any input to PNG output
|
||||
const data = await sharp(input)
|
||||
.toFormat('png')
|
||||
.toBuffer();
|
||||
```
|
||||
|
||||
- Throws **[Error][3]** unsupported format or options
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -178,15 +270,19 @@ Use tile-based deep zoom (image pyramid) output.
|
||||
Set the format and options for tile images via the `toFormat`, `jpeg`, `png` or `webp` functions.
|
||||
Use a `.zip` or `.szi` file extension with `toFile` to write to a compressed archive file format.
|
||||
|
||||
**Parameters**
|
||||
Warning: multiple sharp instances concurrently producing tile output can expose a possible race condition in some versions of libgsf.
|
||||
|
||||
- `tile` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?**
|
||||
- `tile.size` **[Number](https://developer.mozilla.org/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/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/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/docs/Web/JavaScript/Reference/Global_Objects/String)** filesystem layout, possible values are `dz`, `zoomify` or `google`. (optional, default `'dz'`)
|
||||
### Parameters
|
||||
|
||||
**Examples**
|
||||
- `tile` **[Object][5]?**
|
||||
- `tile.size` **[Number][8]** tile size in pixels, a value between 1 and 8192. (optional, default `256`)
|
||||
- `tile.overlap` **[Number][8]** tile overlap in pixels, a value between 0 and 8192. (optional, default `0`)
|
||||
- `tile.angle` **[Number][8]** tile angle of rotation, must be a multiple of 90. (optional, default `0`)
|
||||
- `tile.depth` **[String][1]?** how deep to make the pyramid, possible values are `onepixel`, `onetile` or `one`, default based on layout.
|
||||
- `tile.container` **[String][1]** tile container, with value `fs` (filesystem) or `zip` (compressed file). (optional, default `'fs'`)
|
||||
- `tile.layout` **[String][1]** filesystem layout, possible values are `dz`, `zoomify` or `google`. (optional, default `'dz'`)
|
||||
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
sharp('input.tiff')
|
||||
@@ -200,6 +296,22 @@ sharp('input.tiff')
|
||||
});
|
||||
```
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][3]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
||||
|
||||
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
|
||||
|
||||
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||
|
||||
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise
|
||||
|
||||
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
||||
|
||||
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
||||
|
||||
[7]: https://nodejs.org/api/buffer.html
|
||||
|
||||
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
||||
|
||||
@@ -1,15 +1,5 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
### Table of Contents
|
||||
|
||||
- [resize](#resize)
|
||||
- [crop](#crop)
|
||||
- [embed](#embed)
|
||||
- [max](#max)
|
||||
- [min](#min)
|
||||
- [ignoreAspectRatio](#ignoreaspectratio)
|
||||
- [withoutEnlargement](#withoutenlargement)
|
||||
|
||||
## resize
|
||||
|
||||
Resize image to `width` x `height`.
|
||||
@@ -17,20 +7,20 @@ By default, the resized image is centre cropped to the exact size specified.
|
||||
|
||||
Possible kernels are:
|
||||
|
||||
- `nearest`: Use [nearest neighbour interpolation](http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation).
|
||||
- `cubic`: Use a [Catmull-Rom spline](https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline).
|
||||
- `lanczos2`: Use a [Lanczos kernel](https://en.wikipedia.org/wiki/Lanczos_resampling#Lanczos_kernel) with `a=2`.
|
||||
- `nearest`: Use [nearest neighbour interpolation][1].
|
||||
- `cubic`: Use a [Catmull-Rom spline][2].
|
||||
- `lanczos2`: Use a [Lanczos kernel][3] with `a=2`.
|
||||
- `lanczos3`: Use a Lanczos kernel with `a=3` (the default).
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `width` **[Number](https://developer.mozilla.org/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/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/docs/Web/JavaScript/Reference/Global_Objects/Object)?**
|
||||
- `options.kernel` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the kernel to use for image reduction. (optional, default `'lanczos3'`)
|
||||
- `options.fastShrinkOnLoad` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** take greater advantage of the JPEG and WebP shrink-on-load feature, which can lead to a slight moiré pattern on some images. (optional, default `true`)
|
||||
- `width` **[Number][4]?** pixels wide the resultant image should be. Use `null` or `undefined` to auto-scale the width to match the height.
|
||||
- `height` **[Number][4]?** pixels high the resultant image should be. Use `null` or `undefined` to auto-scale the height to match the width.
|
||||
- `options` **[Object][5]?**
|
||||
- `options.kernel` **[String][6]** the kernel to use for image reduction. (optional, default `'lanczos3'`)
|
||||
- `options.fastShrinkOnLoad` **[Boolean][7]** take greater advantage of the JPEG and WebP shrink-on-load feature, which can lead to a slight moiré pattern on some images. (optional, default `true`)
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
sharp(inputBuffer)
|
||||
@@ -47,7 +37,7 @@ sharp(inputBuffer)
|
||||
});
|
||||
```
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][8]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -61,14 +51,14 @@ Possible attributes of the optional `sharp.gravity` are `north`, `northeast`, `e
|
||||
The experimental strategy-based approach resizes so one dimension is at its target length
|
||||
then repeatedly ranks edge regions, discarding the edge with the lowest score based on the selected strategy.
|
||||
|
||||
- `entropy`: focus on the region with the highest [Shannon entropy](https://en.wikipedia.org/wiki/Entropy_%28information_theory%29).
|
||||
- `entropy`: focus on the region with the highest [Shannon entropy][9].
|
||||
- `attention`: focus on the region with the highest luminance frequency, colour saturation and presence of skin tones.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `crop` **[String](https://developer.mozilla.org/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][6]** A member of `sharp.gravity` to crop to an edge/corner or `sharp.strategy` to crop dynamically. (optional, default `'centre'`)
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
const transformer = sharp()
|
||||
@@ -82,7 +72,7 @@ const transformer = sharp()
|
||||
readableStream.pipe(transformer).pipe(writableStream);
|
||||
```
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][8]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -94,11 +84,11 @@ then embed on a background of the exact `width` and `height` specified.
|
||||
If the background contains an alpha value then WebP and PNG format output images will
|
||||
contain an alpha channel, even when the input image does not.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `embed` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** A member of `sharp.gravity` to embed to an edge/corner. (optional, default `'centre'`)
|
||||
- `embed` **[String][6]** A member of `sharp.gravity` to embed to an edge/corner. (optional, default `'centre'`)
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
sharp('input.gif')
|
||||
@@ -115,7 +105,7 @@ sharp('input.gif')
|
||||
});
|
||||
```
|
||||
|
||||
- Throws **[Error](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)** Invalid parameters
|
||||
- Throws **[Error][8]** Invalid parameters
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
@@ -126,7 +116,7 @@ while ensuring its dimensions are less than or equal to the `width` and `height`
|
||||
|
||||
Both `width` and `height` must be provided via `resize` otherwise the behaviour will default to `crop`.
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
sharp(inputBuffer)
|
||||
@@ -163,11 +153,30 @@ Returns **Sharp**
|
||||
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:
|
||||
"_change the dimensions of the image only if its width or height exceeds the geometry specification_".
|
||||
Use with `max()` to preserve the image's aspect ratio.
|
||||
|
||||
The default behaviour _before_ function call is `false`, meaning the image will be enlarged.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `withoutEnlargement` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `true`)
|
||||
- `withoutEnlargement` **[Boolean][7]** (optional, default `true`)
|
||||
|
||||
Returns **Sharp**
|
||||
|
||||
[1]: http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation
|
||||
|
||||
[2]: https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline
|
||||
|
||||
[3]: https://en.wikipedia.org/wiki/Lanczos_resampling#Lanczos_kernel
|
||||
|
||||
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
||||
|
||||
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
||||
|
||||
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
||||
|
||||
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
||||
|
||||
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
|
||||
|
||||
[9]: https://en.wikipedia.org/wiki/Entropy_%28information_theory%29
|
||||
|
||||
@@ -1,12 +1,5 @@
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
### Table of Contents
|
||||
|
||||
- [cache](#cache)
|
||||
- [concurrency](#concurrency)
|
||||
- [counters](#counters)
|
||||
- [simd](#simd)
|
||||
|
||||
## cache
|
||||
|
||||
Gets or, when options are provided, sets the limits of _libvips'_ operation cache.
|
||||
@@ -14,14 +7,14 @@ 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**
|
||||
### Parameters
|
||||
|
||||
- `options` **([Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) \| [Boolean](https://developer.mozilla.org/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/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/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/docs/Web/JavaScript/Reference/Global_Objects/Number)** is the maximum number of operations to cache (optional, default `100`)
|
||||
- `options` **([Object][1] \| [Boolean][2])** Object with the following attributes, or Boolean where true uses default cache settings and false removes all caching (optional, default `true`)
|
||||
- `options.memory` **[Number][3]** is the maximum memory in MB to use for this cache (optional, default `50`)
|
||||
- `options.files` **[Number][3]** is the maximum number of files to hold open (optional, default `20`)
|
||||
- `options.items` **[Number][3]** is the maximum number of operations to cache (optional, default `100`)
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
const stats = sharp.cache();
|
||||
@@ -33,7 +26,7 @@ sharp.cache( { files: 0 } );
|
||||
sharp.cache(false);
|
||||
```
|
||||
|
||||
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
||||
Returns **[Object][1]**
|
||||
|
||||
## concurrency
|
||||
|
||||
@@ -47,11 +40,11 @@ is limited by libuv's `UV_THREADPOOL_SIZE` environment variable.
|
||||
|
||||
This method always returns the current concurrency.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `concurrency` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**
|
||||
- `concurrency` **[Number][3]?**
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
const threads = sharp.concurrency(); // 4
|
||||
@@ -59,7 +52,7 @@ sharp.concurrency(2); // 2
|
||||
sharp.concurrency(0); // 4
|
||||
```
|
||||
|
||||
Returns **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** concurrency
|
||||
Returns **[Number][3]** concurrency
|
||||
|
||||
## counters
|
||||
|
||||
@@ -68,13 +61,13 @@ Provides access to internal task counters.
|
||||
- queue is the number of tasks this module has queued waiting for _libuv_ to provide a worker thread from its pool.
|
||||
- process is the number of resize tasks currently being processed.
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
const counters = sharp.counters(); // { queue: 2, process: 4 }
|
||||
```
|
||||
|
||||
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
||||
Returns **[Object][1]**
|
||||
|
||||
## simd
|
||||
|
||||
@@ -87,11 +80,11 @@ by taking advantage of the SIMD vector unit of the CPU, e.g. Intel SSE and ARM N
|
||||
This feature is currently off by default but future versions may reverse this.
|
||||
Versions of liborc prior to 0.4.25 are known to segfault under heavy load.
|
||||
|
||||
**Parameters**
|
||||
### Parameters
|
||||
|
||||
- `simd` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** (optional, default `false`)
|
||||
- `simd` **[Boolean][2]** (optional, default `false`)
|
||||
|
||||
**Examples**
|
||||
### Examples
|
||||
|
||||
```javascript
|
||||
const simd = sharp.simd();
|
||||
@@ -103,4 +96,10 @@ const simd = sharp.simd(true);
|
||||
// attempts to enable the use of SIMD, returning true if available
|
||||
```
|
||||
|
||||
Returns **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
|
||||
Returns **[Boolean][2]**
|
||||
|
||||
[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
||||
|
||||
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
||||
|
||||
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
||||
|
||||
@@ -1,9 +1,121 @@
|
||||
# Changelog
|
||||
|
||||
### v0.20 - "*prebuild*"
|
||||
|
||||
Requires libvips v8.6.1.
|
||||
|
||||
#### v0.20.7 - 21<sup>st</sup> August 2018
|
||||
|
||||
* Use copy+unlink if rename operation fails during installation.
|
||||
[#1345](https://github.com/lovell/sharp/issues/1345)
|
||||
|
||||
#### v0.20.6 - 20<sup>th</sup> August 2018
|
||||
|
||||
* Add removeAlpha operation to remove alpha channel, if any.
|
||||
[#1248](https://github.com/lovell/sharp/issues/1248)
|
||||
|
||||
* Expose mozjpeg quant_table flag.
|
||||
[#1285](https://github.com/lovell/sharp/pull/1285)
|
||||
[@rexxars](https://github.com/rexxars)
|
||||
|
||||
* Allow full WebP alphaQuality range of 0-100.
|
||||
[#1290](https://github.com/lovell/sharp/pull/1290)
|
||||
[@sylvaindumont](https://github.com/sylvaindumont)
|
||||
|
||||
* Cache libvips binaries to reduce re-install time.
|
||||
[#1301](https://github.com/lovell/sharp/issues/1301)
|
||||
|
||||
* Ensure vendor platform mismatch throws error at install time.
|
||||
[#1303](https://github.com/lovell/sharp/issues/1303)
|
||||
|
||||
* Improve install time error messages for FreeBSD users.
|
||||
[#1310](https://github.com/lovell/sharp/issues/1310)
|
||||
|
||||
* Ensure extractChannel works with 16-bit images.
|
||||
[#1330](https://github.com/lovell/sharp/issues/1330)
|
||||
|
||||
* Expose depth option for tile-based output.
|
||||
[#1342](https://github.com/lovell/sharp/pull/1342)
|
||||
[@alundavies](https://github.com/alundavies)
|
||||
|
||||
* Add experimental entropy field to stats response.
|
||||
|
||||
#### v0.20.5 - 27<sup>th</sup> June 2018
|
||||
|
||||
* Expose libjpeg optimize_coding flag.
|
||||
[#1265](https://github.com/lovell/sharp/pull/1265)
|
||||
[@tomlokhorst](https://github.com/tomlokhorst)
|
||||
|
||||
#### v0.20.4 - 20<sup>th</sup> June 2018
|
||||
|
||||
* Prevent possible rounding error when using shrink-on-load and 90/270 degree rotation.
|
||||
[#1241](https://github.com/lovell/sharp/issues/1241)
|
||||
[@anahit42](https://github.com/anahit42)
|
||||
|
||||
* Ensure extractChannel sets correct single-channel colour space interpretation.
|
||||
[#1257](https://github.com/lovell/sharp/issues/1257)
|
||||
[@jeremychone](https://github.com/jeremychone)
|
||||
|
||||
#### v0.20.3 - 29<sup>th</sup> May 2018
|
||||
|
||||
* Fix tint operation by ensuring LAB interpretation and allowing negative values.
|
||||
[#1235](https://github.com/lovell/sharp/issues/1235)
|
||||
[@wezside](https://github.com/wezside)
|
||||
|
||||
#### v0.20.2 - 28<sup>th</sup> April 2018
|
||||
|
||||
* Add tint operation to set image chroma.
|
||||
[#825](https://github.com/lovell/sharp/pull/825)
|
||||
[@rikh42](https://github.com/rikh42)
|
||||
|
||||
* Add environment variable to ignore globally-installed libvips.
|
||||
[#1165](https://github.com/lovell/sharp/pull/1165)
|
||||
[@oncletom](https://github.com/oncletom)
|
||||
|
||||
* Add support for page selection with multi-page input (GIF/TIFF).
|
||||
[#1204](https://github.com/lovell/sharp/pull/1204)
|
||||
[@woolite64](https://github.com/woolite64)
|
||||
|
||||
* Add support for Group4 (CCITTFAX4) compression with TIFF output.
|
||||
[#1208](https://github.com/lovell/sharp/pull/1208)
|
||||
[@woolite64](https://github.com/woolite64)
|
||||
|
||||
#### v0.20.1 - 17<sup>th</sup> March 2018
|
||||
|
||||
* Improve installation experience when a globally-installed libvips below the minimum required version is found.
|
||||
[#1148](https://github.com/lovell/sharp/issues/1148)
|
||||
|
||||
* Prevent smartcrop error when cumulative rounding is below target size.
|
||||
[#1154](https://github.com/lovell/sharp/issues/1154)
|
||||
[@ralrom](https://github.com/ralrom)
|
||||
|
||||
* Expose libvips' median filter operation.
|
||||
[#1161](https://github.com/lovell/sharp/pull/1161)
|
||||
[@BiancoA](https://github.com/BiancoA)
|
||||
|
||||
#### v0.20.0 - 5<sup>th</sup> March 2018
|
||||
|
||||
* Add support for prebuilt sharp binaries on common platforms.
|
||||
[#186](https://github.com/lovell/sharp/issues/186)
|
||||
|
||||
### v0.19 - "*suit*"
|
||||
|
||||
Requires libvips v8.6.1.
|
||||
|
||||
#### v0.19.1 - 24<sup>th</sup> February 2018
|
||||
|
||||
* Expose libvips' linear transform feature.
|
||||
[#1024](https://github.com/lovell/sharp/pull/1024)
|
||||
[@3epnm](https://github.com/3epnm)
|
||||
|
||||
* Expose angle option for tile-based output.
|
||||
[#1121](https://github.com/lovell/sharp/pull/1121)
|
||||
[@BiancoA](https://github.com/BiancoA)
|
||||
|
||||
* Prevent crop operation when image already at or below target dimensions.
|
||||
[#1134](https://github.com/lovell/sharp/issues/1134)
|
||||
[@pieh](https://github.com/pieh)
|
||||
|
||||
#### v0.19.0 - 11<sup>th</sup> January 2018
|
||||
|
||||
* Expose offset coordinates of strategy-based crop.
|
||||
|
||||
5
docs/css/extra.css
Normal file
@@ -0,0 +1,5 @@
|
||||
/* Nest document subheadings in navigation */
|
||||
ul.subnav ul:not(.subnav) {
|
||||
padding-left: 2em;
|
||||
font-size: 80%;
|
||||
}
|
||||
@@ -13,8 +13,9 @@ Lanczos resampling ensures quality is not sacrificed for speed.
|
||||
As well as image resizing, operations such as
|
||||
rotation, extraction, compositing and gamma correction are available.
|
||||
|
||||
OS X, Windows (x64), Linux (x64, ARM) systems do not require
|
||||
the installation of any external runtime dependencies.
|
||||
Most 64-bit OS X, Windows and Linux (glibc) systems running
|
||||
Node versions 4, 6, 8 and 10
|
||||
do not require any additional install or runtime dependencies.
|
||||
|
||||
[](https://coveralls.io/r/lovell/sharp?branch=master)
|
||||
|
||||
@@ -46,7 +47,7 @@ are held in memory and processed at a time,
|
||||
taking full advantage of multiple CPU cores and L1/L2/L3 cache.
|
||||
|
||||
Everything remains non-blocking thanks to _libuv_,
|
||||
no child processes are spawned and Promises/A+ are supported.
|
||||
no child processes are spawned and Promises/async/await are supported.
|
||||
|
||||
### Optimal
|
||||
|
||||
@@ -105,6 +106,16 @@ the help and code contributions of the following people:
|
||||
* [Matthew McEachen](https://github.com/mceachen)
|
||||
* [Jarda Kotěšovec](https://github.com/jardakotesovec)
|
||||
* [Kenric D'Souza](https://github.com/AzureByte)
|
||||
* [Oleh Aleinyk](https://github.com/oaleynik)
|
||||
* [Marcel Bretschneider](https://github.com/3epnm)
|
||||
* [Andrea Bianco](https://github.com/BiancoA)
|
||||
* [Rik Heywood](https://github.com/rikh42)
|
||||
* [Thomas Parisot](https://github.com/oncletom)
|
||||
* [Nathan Graves](https://github.com/woolite64)
|
||||
* [Tom Lokhorst](https://github.com/tomlokhorst)
|
||||
* [Espen Hovlandsdal](https://github.com/rexxars)
|
||||
* [Sylvain Dumont](https://github.com/sylvaindumont)
|
||||
* [Alun Davies](https://github.com/alundavies)
|
||||
|
||||
Thank you!
|
||||
|
||||
|
||||
@@ -8,12 +8,29 @@ npm install sharp
|
||||
yarn add sharp
|
||||
```
|
||||
|
||||
### Prerequisites
|
||||
## Prerequisites
|
||||
|
||||
* Node v4.5.0+
|
||||
|
||||
### Building from source
|
||||
|
||||
Pre-compiled binaries for sharp are provided for use with
|
||||
Node versions 4, 6, 8 and 10 on
|
||||
64-bit Windows, OS X and Linux platforms.
|
||||
|
||||
Sharp will be built from source at install time when:
|
||||
|
||||
* a globally-installed libvips is detected,
|
||||
* pre-compiled binaries do not exist for the current platform and Node version, or
|
||||
* when the `npm install --build-from-source` flag is used.
|
||||
|
||||
Building from source requires:
|
||||
|
||||
* 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 (includes Python)
|
||||
|
||||
## libvips
|
||||
|
||||
### Linux
|
||||
|
||||
[](https://travis-ci.org/lovell/sharp)
|
||||
@@ -23,21 +40,21 @@ 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 14.04, 16.04
|
||||
* Centos 7
|
||||
* Debian 7+
|
||||
* Ubuntu 14.04+
|
||||
* Centos 7+
|
||||
* Fedora
|
||||
* openSUSE 13.2
|
||||
* openSUSE 13.2+
|
||||
* Archlinux
|
||||
* Raspbian Jessie
|
||||
* Amazon Linux 2017.03.1
|
||||
* Amazon Linux
|
||||
* 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
|
||||
and that it can be located using `pkg-config --modversion vips-cpp`.
|
||||
|
||||
If you are using non-stadard paths (anything other than `/usr` or `/usr/local`),
|
||||
If you are using non-standard paths (anything other than `/usr` or `/usr/local`),
|
||||
you might need to set `PKG_CONFIG_PATH` during `npm install`
|
||||
and `LD_LIBRARY_PATH` at runtime.
|
||||
|
||||
@@ -63,7 +80,7 @@ via `sharp.cache(false)` to avoid a stack overflow.
|
||||
|
||||
### Mac OS
|
||||
|
||||
[](https://travis-ci.org/lovell/sharp)
|
||||
[](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 7MB.
|
||||
@@ -144,6 +161,18 @@ Set the Lambda runtime to Node.js 6.10.
|
||||
|
||||
To get the best performance select the largest memory available. A 1536 MB function provides ~12x more CPU time than a 128 MB function.
|
||||
|
||||
### NW.js
|
||||
|
||||
Run the `nw-gyp` tool after installation.
|
||||
|
||||
```sh
|
||||
cd node-modules/sharp
|
||||
nw-gyp rebuild --arch=x64 --target=[your nw version]
|
||||
node node_modules/sharp/install/dll-copy
|
||||
```
|
||||
|
||||
See also http://docs.nwjs.io/en/latest/For%20Users/Advanced/Use%20Native%20Node%20Modules/
|
||||
|
||||
### Build tools
|
||||
|
||||
* [gulp-responsive](https://www.npmjs.com/package/gulp-responsive)
|
||||
@@ -194,10 +223,17 @@ to the directory containing the `policy.xml` file.
|
||||
|
||||
### Pre-compiled libvips binaries
|
||||
|
||||
If a global installation of libvips that meets the
|
||||
minimum version requirement cannot be found,
|
||||
this module will attempt to download a pre-compiled bundle of libvips
|
||||
and its dependencies on Linux and Windows machines.
|
||||
This module will attempt to download a pre-compiled bundle of libvips
|
||||
and its dependencies on Linux and Windows machines under either of these
|
||||
conditions:
|
||||
|
||||
1. If a global installation of libvips that meets the
|
||||
minimum version requirement cannot be found;
|
||||
1. If `SHARP_IGNORE_GLOBAL_LIBVIPS` environment variable is set.
|
||||
|
||||
```sh
|
||||
SHARP_IGNORE_GLOBAL_LIBVIPS=1 npm install sharp
|
||||
```
|
||||
|
||||
Should you need to manually download and inspect these files,
|
||||
you can do so via https://github.com/lovell/sharp-libvips/releases
|
||||
|
||||
34
install/dll-copy.js
Normal file
@@ -0,0 +1,34 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const copyFileSync = require('fs-copy-file-sync');
|
||||
const npmLog = require('npmlog');
|
||||
|
||||
if (process.platform === 'win32') {
|
||||
const buildDir = path.join(__dirname, '..', 'build');
|
||||
const buildReleaseDir = path.join(buildDir, 'Release');
|
||||
npmLog.info('sharp', `Creating ${buildReleaseDir}`);
|
||||
try {
|
||||
fs.mkdirSync(buildDir);
|
||||
fs.mkdirSync(buildReleaseDir);
|
||||
} catch (err) {}
|
||||
const vendorLibDir = path.join(__dirname, '..', 'vendor', 'lib');
|
||||
npmLog.info('sharp', `Copying DLLs from ${vendorLibDir} to ${buildReleaseDir}`);
|
||||
try {
|
||||
fs
|
||||
.readdirSync(vendorLibDir)
|
||||
.filter(function (filename) {
|
||||
return /\.dll$/.test(filename);
|
||||
})
|
||||
.forEach(function (filename) {
|
||||
copyFileSync(
|
||||
path.join(vendorLibDir, filename),
|
||||
path.join(buildReleaseDir, filename)
|
||||
);
|
||||
});
|
||||
} catch (err) {
|
||||
npmLog.error('sharp', err.message);
|
||||
}
|
||||
}
|
||||
102
install/libvips.js
Normal file
@@ -0,0 +1,102 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
|
||||
const detectLibc = require('detect-libc');
|
||||
const npmLog = require('npmlog');
|
||||
const semver = require('semver');
|
||||
const simpleGet = require('simple-get');
|
||||
const tar = require('tar');
|
||||
const copyFileSync = require('fs-copy-file-sync');
|
||||
|
||||
const agent = require('../lib/agent');
|
||||
const libvips = require('../lib/libvips');
|
||||
const platform = require('../lib/platform');
|
||||
|
||||
const minimumLibvipsVersion = libvips.minimumLibvipsVersion;
|
||||
const distBaseUrl = process.env.SHARP_DIST_BASE_URL || `https://github.com/lovell/sharp-libvips/releases/download/v${minimumLibvipsVersion}/`;
|
||||
|
||||
const extractTarball = function (tarPath) {
|
||||
const vendorPath = path.join(__dirname, '..', 'vendor');
|
||||
if (!fs.existsSync(vendorPath)) {
|
||||
fs.mkdirSync(vendorPath);
|
||||
}
|
||||
tar
|
||||
.extract({
|
||||
file: tarPath,
|
||||
cwd: vendorPath,
|
||||
strict: true
|
||||
})
|
||||
.catch(function (err) {
|
||||
throw err;
|
||||
});
|
||||
};
|
||||
|
||||
try {
|
||||
const useGlobalLibvips = libvips.useGlobalLibvips();
|
||||
if (useGlobalLibvips) {
|
||||
const globalLibvipsVersion = libvips.globalLibvipsVersion();
|
||||
npmLog.info('sharp', `Detected globally-installed libvips v${globalLibvipsVersion}`);
|
||||
npmLog.info('sharp', 'Building from source via node-gyp');
|
||||
process.exit(1);
|
||||
} else if (libvips.hasVendoredLibvips()) {
|
||||
npmLog.info('sharp', `Using existing vendored libvips v${minimumLibvipsVersion}`);
|
||||
} else {
|
||||
// Is this arch/platform supported?
|
||||
const arch = process.env.npm_config_arch || process.arch;
|
||||
const platformAndArch = platform();
|
||||
if (platformAndArch === 'win32-ia32') {
|
||||
throw new Error('Windows x86 (32-bit) node.exe is not supported');
|
||||
}
|
||||
if (arch === 'ia32') {
|
||||
throw new Error(`Intel Architecture 32-bit systems require manual installation of libvips >= ${minimumLibvipsVersion}`);
|
||||
}
|
||||
if (platformAndArch === 'freebsd-x64') {
|
||||
throw new Error(`FreeBSD systems require manual installation of libvips >= ${minimumLibvipsVersion}`);
|
||||
}
|
||||
if (detectLibc.isNonGlibcLinux) {
|
||||
throw new Error(`Use with ${detectLibc.family} libc requires manual installation of libvips >= ${minimumLibvipsVersion}`);
|
||||
}
|
||||
if (detectLibc.family === detectLibc.GLIBC && detectLibc.version && semver.lt(`${detectLibc.version}.0`, '2.13.0')) {
|
||||
throw new Error(`Use with glibc version ${detectLibc.version} requires manual installation of libvips >= ${minimumLibvipsVersion}`);
|
||||
}
|
||||
// Download to per-process temporary file
|
||||
const tarFilename = ['libvips', minimumLibvipsVersion, platformAndArch].join('-') + '.tar.gz';
|
||||
const tarPathCache = path.join(libvips.cachePath(), tarFilename);
|
||||
if (fs.existsSync(tarPathCache)) {
|
||||
npmLog.info('sharp', `Using cached ${tarPathCache}`);
|
||||
extractTarball(tarPathCache);
|
||||
} else {
|
||||
const tarPathTemp = path.join(os.tmpdir(), `${process.pid}-${tarFilename}`);
|
||||
const tmpFile = fs.createWriteStream(tarPathTemp);
|
||||
const url = distBaseUrl + tarFilename;
|
||||
npmLog.info('sharp', `Downloading ${url}`);
|
||||
simpleGet({ url: url, agent: agent() }, function (err, response) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
if (response.statusCode !== 200) {
|
||||
throw new Error(`Status ${response.statusCode}`);
|
||||
}
|
||||
response.pipe(tmpFile);
|
||||
});
|
||||
tmpFile.on('close', function () {
|
||||
try {
|
||||
// Attempt to rename
|
||||
fs.renameSync(tarPathTemp, tarPathCache);
|
||||
} catch (err) {
|
||||
// Fall back to copy and unlink
|
||||
copyFileSync(tarPathTemp, tarPathCache);
|
||||
fs.unlinkSync(tarPathTemp);
|
||||
}
|
||||
extractTarball(tarPathCache);
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
npmLog.error('sharp', err.message);
|
||||
npmLog.error('sharp', 'Please see http://sharp.pixelplumbing.com/page/install');
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -12,6 +12,23 @@ const bool = {
|
||||
eor: 'eor'
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove alpha channel, if any. This is a no-op if the image does not have an alpha channel.
|
||||
*
|
||||
* @example
|
||||
* sharp('rgba.png')
|
||||
* .removeAlpha()
|
||||
* .toFile('rgb.png', function(err, info) {
|
||||
* // rgb.png is a 3 channel image without an alpha channel
|
||||
* });
|
||||
*
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
function removeAlpha () {
|
||||
this.options.removeAlpha = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a single channel from a multi-channel image.
|
||||
*
|
||||
@@ -102,6 +119,7 @@ function bandbool (boolOp) {
|
||||
module.exports = function (Sharp) {
|
||||
// Public instance functions
|
||||
[
|
||||
removeAlpha,
|
||||
extractChannel,
|
||||
joinChannel,
|
||||
bandbool
|
||||
|
||||
@@ -38,6 +38,21 @@ function background (rgba) {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tint the image using the provided chroma while preserving the image luminance.
|
||||
* An alpha channel may be present and will be unchanged by the operation.
|
||||
*
|
||||
* @param {String|Object} rgb - parsed by the [color](https://www.npmjs.org/package/color) module to extract chroma values.
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameter
|
||||
*/
|
||||
function tint (rgb) {
|
||||
const colour = color(rgb);
|
||||
this.options.tintA = colour.a();
|
||||
this.options.tintB = colour.b();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
@@ -95,6 +110,7 @@ module.exports = function (Sharp) {
|
||||
// Public instance functions
|
||||
[
|
||||
background,
|
||||
tint,
|
||||
greyscale,
|
||||
grayscale,
|
||||
toColourspace,
|
||||
|
||||
@@ -19,8 +19,7 @@ const is = require('./is');
|
||||
* .overlayWith('overlay.png', { gravity: sharp.gravity.southeast } )
|
||||
* .sharpen()
|
||||
* .withMetadata()
|
||||
* .quality(90)
|
||||
* .webp()
|
||||
* .webp( { quality: 90 } )
|
||||
* .toBuffer()
|
||||
* .then(function(outputBuffer) {
|
||||
* // outputBuffer contains upside down, 300px wide, alpha channel flattened
|
||||
|
||||
@@ -81,7 +81,7 @@ const debuglog = util.debuglog('sharp');
|
||||
* width: 300,
|
||||
* height: 200,
|
||||
* channels: 4,
|
||||
* background: { r: 255, g: 0, b: 0, alpha: 128 }
|
||||
* background: { r: 255, g: 0, b: 0, alpha: 0.5 }
|
||||
* }
|
||||
* })
|
||||
* .png()
|
||||
@@ -97,6 +97,7 @@ const debuglog = util.debuglog('sharp');
|
||||
* to decode images, even if the data is corrupt or invalid. Set this flag to true
|
||||
* if you'd rather halt processing and raise an error when loading invalid images.
|
||||
* @param {Number} [options.density=72] - integral number representing the DPI for vector images.
|
||||
* @param {Number} [options.page=0] - page number to extract for multi-page input (GIF, TIFF)
|
||||
* @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]
|
||||
@@ -151,8 +152,11 @@ const Sharp = function (input, options) {
|
||||
fastShrinkOnLoad: true,
|
||||
// operations
|
||||
background: [0, 0, 0, 255],
|
||||
tintA: 128,
|
||||
tintB: 128,
|
||||
flatten: false,
|
||||
negate: false,
|
||||
medianSize: 0,
|
||||
blurSigma: 0,
|
||||
sharpenSigma: 0,
|
||||
sharpenFlat: 1,
|
||||
@@ -167,6 +171,7 @@ const Sharp = function (input, options) {
|
||||
booleanFileIn: '',
|
||||
joinChannelIn: [],
|
||||
extractChannel: -1,
|
||||
removeAlpha: false,
|
||||
colourspace: 'srgb',
|
||||
// overlay
|
||||
overlayGravity: 0,
|
||||
@@ -188,6 +193,8 @@ const Sharp = function (input, options) {
|
||||
jpegTrellisQuantisation: false,
|
||||
jpegOvershootDeringing: false,
|
||||
jpegOptimiseScans: false,
|
||||
jpegOptimiseCoding: true,
|
||||
jpegQuantisationTable: 0,
|
||||
pngProgressive: false,
|
||||
pngCompressionLevel: 9,
|
||||
pngAdaptiveFiltering: false,
|
||||
@@ -203,6 +210,8 @@ const Sharp = function (input, options) {
|
||||
tiffYres: 1.0,
|
||||
tileSize: 256,
|
||||
tileOverlap: 0,
|
||||
linearA: 1,
|
||||
linearB: 0,
|
||||
// Function to notify of libvips warnings
|
||||
debuglog: debuglog,
|
||||
// Function to notify of queue length changes
|
||||
|
||||
11
lib/input.js
@@ -57,6 +57,12 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
|
||||
throw new Error('Expected width, height and channels for raw pixel input');
|
||||
}
|
||||
}
|
||||
// Page input for multi-page TIFF
|
||||
if (is.defined(inputOptions.page)) {
|
||||
if (is.integer(inputOptions.page) && is.inRange(inputOptions.page, 0, 100000)) {
|
||||
inputDescriptor.page = inputOptions.page;
|
||||
}
|
||||
}
|
||||
// Create new image
|
||||
if (is.defined(inputOptions.create)) {
|
||||
if (
|
||||
@@ -171,8 +177,8 @@ function clone () {
|
||||
* 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
|
||||
* - `width`: Number of pixels wide (EXIF orientation is not taken into consideration)
|
||||
* - `height`: Number of pixels high (EXIF orientation is not taken into consideration)
|
||||
* - `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)
|
||||
@@ -258,6 +264,7 @@ function metadata (callback) {
|
||||
* - `maxX` (x-coordinate of one of the pixel where the maximum lies)
|
||||
* - `maxY` (y-coordinate of one of the pixel where the maximum lies)
|
||||
* - `isOpaque`: Value to identify if the image is opaque or transparent, based on the presence and use of alpha channel
|
||||
* - `entropy`: Histogram-based estimation of greyscale entropy, discarding alpha channel if any (experimental)
|
||||
*
|
||||
* @example
|
||||
* const image = sharp(inputJpg);
|
||||
|
||||
82
lib/libvips.js
Normal file
@@ -0,0 +1,82 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const spawnSync = require('child_process').spawnSync;
|
||||
const semver = require('semver');
|
||||
const platform = require('./platform');
|
||||
|
||||
const env = process.env;
|
||||
const minimumLibvipsVersion = env.npm_package_config_libvips || require('../package.json').config.libvips;
|
||||
|
||||
const spawnSyncOptions = {
|
||||
encoding: 'utf8',
|
||||
shell: true
|
||||
};
|
||||
|
||||
const cachePath = function () {
|
||||
const npmCachePath = env.npm_config_cache || (env.APPDATA ? path.join(env.APPDATA, 'npm-cache') : path.join(os.homedir(), '.npm'));
|
||||
if (!fs.existsSync(npmCachePath)) {
|
||||
fs.mkdirSync(npmCachePath);
|
||||
}
|
||||
const libvipsCachePath = path.join(npmCachePath, '_libvips');
|
||||
if (!fs.existsSync(libvipsCachePath)) {
|
||||
fs.mkdirSync(libvipsCachePath);
|
||||
}
|
||||
return libvipsCachePath;
|
||||
};
|
||||
|
||||
const globalLibvipsVersion = function () {
|
||||
if (process.platform !== 'win32') {
|
||||
const globalLibvipsVersion = spawnSync(`PKG_CONFIG_PATH="${pkgConfigPath()}" pkg-config --modversion vips-cpp`, spawnSyncOptions).stdout || '';
|
||||
return globalLibvipsVersion.trim();
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
};
|
||||
|
||||
const hasVendoredLibvips = function () {
|
||||
const currentPlatformId = platform();
|
||||
let vendorPlatformId;
|
||||
try {
|
||||
vendorPlatformId = require(path.join(__dirname, '..', 'vendor', 'platform.json'));
|
||||
} catch (err) {}
|
||||
if (vendorPlatformId) {
|
||||
if (currentPlatformId === vendorPlatformId) {
|
||||
return true;
|
||||
} else {
|
||||
throw new Error(`'${vendorPlatformId}' binaries cannot be used on the '${currentPlatformId}' platform. Please remove the 'node_modules/sharp/vendor' directory and run 'npm install'.`);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const pkgConfigPath = function () {
|
||||
if (process.platform !== 'win32') {
|
||||
const brewPkgConfigPath = spawnSync('which brew >/dev/null 2>&1 && eval $(brew --env) && echo $PKG_CONFIG_LIBDIR', spawnSyncOptions).stdout || '';
|
||||
return [brewPkgConfigPath.trim(), env.PKG_CONFIG_PATH, '/usr/local/lib/pkgconfig', '/usr/lib/pkgconfig']
|
||||
.filter(function (p) { return !!p; })
|
||||
.join(':');
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
};
|
||||
|
||||
const useGlobalLibvips = function () {
|
||||
if (Boolean(env.SHARP_IGNORE_GLOBAL_LIBVIPS) === true) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const globalVipsVersion = globalLibvipsVersion();
|
||||
return !!globalVipsVersion && semver.gte(globalVipsVersion, minimumLibvipsVersion);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
minimumLibvipsVersion: minimumLibvipsVersion,
|
||||
cachePath: cachePath,
|
||||
globalLibvipsVersion: globalLibvipsVersion,
|
||||
hasVendoredLibvips: hasVendoredLibvips,
|
||||
pkgConfigPath: pkgConfigPath,
|
||||
useGlobalLibvips: useGlobalLibvips
|
||||
};
|
||||
@@ -156,6 +156,26 @@ function sharpen (sigma, flat, jagged) {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply median filter.
|
||||
* When used without parameters the default window is 3x3.
|
||||
* @param {Number} [size=3] square mask size: size x size
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
function median (size) {
|
||||
if (!is.defined(size)) {
|
||||
// No arguments: default to 3x3
|
||||
this.options.medianSize = 3;
|
||||
} else if (is.integer(size) && is.inRange(size, 1, 1000)) {
|
||||
// Numeric argument: specific sigma
|
||||
this.options.medianSize = size;
|
||||
} else {
|
||||
throw new Error('Invalid median size ' + size);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Blur the image.
|
||||
* When used without parameters, performs a fast, mild blur of the output image.
|
||||
@@ -406,6 +426,33 @@ function boolean (operand, operator, options) {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the linear formula a * input + b to the image (levels adjustment)
|
||||
* @param {Number} [a=1.0] multiplier
|
||||
* @param {Number} [b=0.0] offset
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
function linear (a, b) {
|
||||
if (!is.defined(a)) {
|
||||
this.options.linearA = 1.0;
|
||||
} else if (is.number(a)) {
|
||||
this.options.linearA = a;
|
||||
} else {
|
||||
throw new Error('Invalid linear transform multiplier ' + a);
|
||||
}
|
||||
|
||||
if (!is.defined(b)) {
|
||||
this.options.linearB = 0.0;
|
||||
} else if (is.number(b)) {
|
||||
this.options.linearB = b;
|
||||
} else {
|
||||
throw new Error('Invalid linear transform offset ' + b);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorate the Sharp prototype with operation-related functions.
|
||||
* @private
|
||||
@@ -417,6 +464,7 @@ module.exports = function (Sharp) {
|
||||
flip,
|
||||
flop,
|
||||
sharpen,
|
||||
median,
|
||||
blur,
|
||||
extend,
|
||||
flatten,
|
||||
@@ -427,7 +475,8 @@ module.exports = function (Sharp) {
|
||||
normalize,
|
||||
convolve,
|
||||
threshold,
|
||||
boolean
|
||||
boolean,
|
||||
linear
|
||||
].forEach(function (f) {
|
||||
Sharp.prototype[f.name] = f;
|
||||
});
|
||||
|
||||
134
lib/output.js
@@ -12,6 +12,16 @@ const sharp = require('../build/Release/sharp.node');
|
||||
*
|
||||
* A `Promise` is returned when `callback` is not provided.
|
||||
*
|
||||
* @example
|
||||
* sharp(input)
|
||||
* .toFile('output.png', (err, info) => { ... });
|
||||
*
|
||||
* @example
|
||||
* sharp(input)
|
||||
* .toFile('output.png')
|
||||
* .then(info => { ... })
|
||||
* .catch(err => { ... });
|
||||
*
|
||||
* @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`,
|
||||
@@ -58,6 +68,22 @@ function toFile (fileOut, callback) {
|
||||
*
|
||||
* A `Promise` is returned when `callback` is not provided.
|
||||
*
|
||||
* @example
|
||||
* sharp(input)
|
||||
* .toBuffer((err, data, info) => { ... });
|
||||
*
|
||||
* @example
|
||||
* sharp(input)
|
||||
* .toBuffer()
|
||||
* .then(data => { ... })
|
||||
* .catch(err => { ... });
|
||||
*
|
||||
* @example
|
||||
* sharp(input)
|
||||
* .toBuffer({ resolveWithObject: true })
|
||||
* .then(({ data, info }) => { ... })
|
||||
* .catch(err => { ... });
|
||||
*
|
||||
* @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]
|
||||
@@ -76,6 +102,13 @@ function toBuffer (options, callback) {
|
||||
* 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.
|
||||
* This will also convert to and add a web-friendly sRGB ICC profile.
|
||||
*
|
||||
* @example
|
||||
* sharp('input.jpg')
|
||||
* .withMetadata()
|
||||
* .toFile('output-with-metadata.jpg')
|
||||
* .then(info => { ... });
|
||||
*
|
||||
* @param {Object} [withMetadata]
|
||||
* @param {Number} [withMetadata.orientation] value between 1 and 8, used to update the EXIF `Orientation` tag.
|
||||
* @returns {Sharp}
|
||||
@@ -97,6 +130,16 @@ function withMetadata (withMetadata) {
|
||||
|
||||
/**
|
||||
* Use these JPEG options for output image.
|
||||
*
|
||||
* @example
|
||||
* // Convert any input to very high quality JPEG output
|
||||
* const data = await sharp(input)
|
||||
* .jpeg({
|
||||
* quality: 100,
|
||||
* chromaSubsampling: '4:4:4'
|
||||
* })
|
||||
* .toBuffer();
|
||||
*
|
||||
* @param {Object} [options] - output options
|
||||
* @param {Number} [options.quality=80] - quality, integer 1-100
|
||||
* @param {Boolean} [options.progressive=false] - use progressive (interlace) scan
|
||||
@@ -105,6 +148,10 @@ function withMetadata (withMetadata) {
|
||||
* @param {Boolean} [options.overshootDeringing=false] - apply overshoot deringing, requires mozjpeg
|
||||
* @param {Boolean} [options.optimiseScans=false] - optimise progressive scans, forces progressive, requires mozjpeg
|
||||
* @param {Boolean} [options.optimizeScans=false] - alternative spelling of optimiseScans
|
||||
* @param {Boolean} [options.optimiseCoding=true] - optimise Huffman coding tables
|
||||
* @param {Boolean} [options.optimizeCoding=true] - alternative spelling of optimiseCoding
|
||||
* @param {Number} [options.quantisationTable=0] - quantization table to use, integer 0-8, requires mozjpeg
|
||||
* @param {Number} [options.quantizationTable=0] - alternative spelling of quantisationTable
|
||||
* @param {Boolean} [options.force=true] - force JPEG output, otherwise attempt to use input format
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid options
|
||||
@@ -142,12 +189,34 @@ function jpeg (options) {
|
||||
this.options.jpegProgressive = true;
|
||||
}
|
||||
}
|
||||
options.optimiseCoding = is.bool(options.optimizeCoding) ? options.optimizeCoding : options.optimiseCoding;
|
||||
if (is.defined(options.optimiseCoding)) {
|
||||
this._setBooleanOption('jpegOptimiseCoding', options.optimiseCoding);
|
||||
}
|
||||
options.quantisationTable = is.number(options.quantizationTable) ? options.quantizationTable : options.quantisationTable;
|
||||
if (is.defined(options.quantisationTable)) {
|
||||
if (is.integer(options.quantisationTable) && is.inRange(options.quantisationTable, 0, 8)) {
|
||||
this.options.jpegQuantisationTable = options.quantisationTable;
|
||||
} else {
|
||||
throw new Error('Invalid quantisation table (integer, 0-8) ' + options.quantisationTable);
|
||||
}
|
||||
}
|
||||
}
|
||||
return this._updateFormatOut('jpeg', options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use these PNG options for output image.
|
||||
*
|
||||
* PNG output is always full colour at 8 or 16 bits per pixel.
|
||||
* Indexed PNG input at 1, 2 or 4 bits per pixel is converted to 8 bits per pixel.
|
||||
*
|
||||
* @example
|
||||
* // Convert any input to PNG output
|
||||
* const data = await sharp(input)
|
||||
* .png()
|
||||
* .toBuffer();
|
||||
*
|
||||
* @param {Object} [options]
|
||||
* @param {Boolean} [options.progressive=false] - use progressive (interlace) scan
|
||||
* @param {Number} [options.compressionLevel=9] - zlib compression level, 0-9
|
||||
@@ -177,6 +246,13 @@ function png (options) {
|
||||
|
||||
/**
|
||||
* Use these WebP options for output image.
|
||||
*
|
||||
* @example
|
||||
* // Convert any input to lossless WebP output
|
||||
* const data = await sharp(input)
|
||||
* .webp({ lossless: true })
|
||||
* .toBuffer();
|
||||
*
|
||||
* @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
|
||||
@@ -195,10 +271,10 @@ function webp (options) {
|
||||
}
|
||||
}
|
||||
if (is.object(options) && is.defined(options.alphaQuality)) {
|
||||
if (is.integer(options.alphaQuality) && is.inRange(options.alphaQuality, 1, 100)) {
|
||||
if (is.integer(options.alphaQuality) && is.inRange(options.alphaQuality, 0, 100)) {
|
||||
this.options.webpAlphaQuality = options.alphaQuality;
|
||||
} else {
|
||||
throw new Error('Invalid webp alpha quality (integer, 1-100) ' + options.alphaQuality);
|
||||
throw new Error('Invalid webp alpha quality (integer, 0-100) ' + options.alphaQuality);
|
||||
}
|
||||
}
|
||||
if (is.object(options) && is.defined(options.lossless)) {
|
||||
@@ -212,10 +288,21 @@ function webp (options) {
|
||||
|
||||
/**
|
||||
* Use these TIFF options for output image.
|
||||
*
|
||||
* @example
|
||||
* // Convert SVG input to LZW-compressed, 1 bit per pixel TIFF output
|
||||
* sharp('input.svg')
|
||||
* .tiff({
|
||||
* compression: 'lzw',
|
||||
* squash: true
|
||||
* })
|
||||
* .toFile('1-bpp-output.tiff')
|
||||
* .then(info => { ... });
|
||||
*
|
||||
* @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.compression='jpeg'] - compression options: lzw, deflate, jpeg, ccittfax4
|
||||
* @param {Boolean} [options.predictor='horizontal'] - 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
|
||||
@@ -255,10 +342,10 @@ function tiff (options) {
|
||||
}
|
||||
// compression
|
||||
if (is.defined(options) && is.defined(options.compression)) {
|
||||
if (is.string(options.compression) && is.inArray(options.compression, ['lzw', 'deflate', 'jpeg', 'none'])) {
|
||||
if (is.string(options.compression) && is.inArray(options.compression, ['lzw', 'deflate', 'jpeg', 'ccittfax4', 'none'])) {
|
||||
this.options.tiffCompression = options.compression;
|
||||
} else {
|
||||
const message = `Invalid compression option "${options.compression}". Should be one of: lzw, deflate, jpeg, none`;
|
||||
const message = `Invalid compression option "${options.compression}". Should be one of: lzw, deflate, jpeg, ccittfax4, none`;
|
||||
throw new Error(message);
|
||||
}
|
||||
}
|
||||
@@ -276,6 +363,13 @@ function tiff (options) {
|
||||
|
||||
/**
|
||||
* Force output to be raw, uncompressed uint8 pixel data.
|
||||
*
|
||||
* @example
|
||||
* // Extract raw RGB pixel data from JPEG input
|
||||
* const { data, info } = await sharp('input.jpg')
|
||||
* .raw()
|
||||
* .toBuffer({ resolveWithObject: true });
|
||||
*
|
||||
* @returns {Sharp}
|
||||
*/
|
||||
function raw () {
|
||||
@@ -284,6 +378,13 @@ function raw () {
|
||||
|
||||
/**
|
||||
* Force output to a given format.
|
||||
*
|
||||
* @example
|
||||
* // Convert any input to PNG output
|
||||
* const data = await sharp(input)
|
||||
* .toFormat('png')
|
||||
* .toBuffer();
|
||||
*
|
||||
* @param {(String|Object)} format - as a String or an Object with an 'id' attribute
|
||||
* @param {Object} options - output options
|
||||
* @returns {Sharp}
|
||||
@@ -305,6 +406,8 @@ function toFormat (format, options) {
|
||||
* Set the format and options for tile images via the `toFormat`, `jpeg`, `png` or `webp` functions.
|
||||
* Use a `.zip` or `.szi` file extension with `toFile` to write to a compressed archive file format.
|
||||
*
|
||||
* Warning: multiple sharp instances concurrently producing tile output can expose a possible race condition in some versions of libgsf.
|
||||
*
|
||||
* @example
|
||||
* sharp('input.tiff')
|
||||
* .png()
|
||||
@@ -319,6 +422,8 @@ function toFormat (format, options) {
|
||||
* @param {Object} [tile]
|
||||
* @param {Number} [tile.size=256] tile size in pixels, a value between 1 and 8192.
|
||||
* @param {Number} [tile.overlap=0] tile overlap in pixels, a value between 0 and 8192.
|
||||
* @param {Number} [tile.angle=0] tile angle of rotation, must be a multiple of 90.
|
||||
* @param {String} [tile.depth] how deep to make the pyramid, possible values are `onepixel`, `onetile` or `one`, default based on layout.
|
||||
* @param {String} [tile.container='fs'] tile container, with value `fs` (filesystem) or `zip` (compressed file).
|
||||
* @param {String} [tile.layout='dz'] filesystem layout, possible values are `dz`, `zoomify` or `google`.
|
||||
* @returns {Sharp}
|
||||
@@ -361,6 +466,24 @@ function tile (tile) {
|
||||
throw new Error('Invalid tile layout ' + tile.layout);
|
||||
}
|
||||
}
|
||||
|
||||
// Angle of rotation,
|
||||
if (is.defined(tile.angle)) {
|
||||
if (is.integer(tile.angle) && !(tile.angle % 90)) {
|
||||
this.options.tileAngle = tile.angle;
|
||||
} else {
|
||||
throw new Error('Unsupported angle: angle must be a positive/negative multiple of 90 ' + tile.angle);
|
||||
}
|
||||
}
|
||||
|
||||
// Depth of tiles
|
||||
if (is.defined(tile.depth)) {
|
||||
if (is.string(tile.depth) && is.inArray(tile.depth, ['onepixel', 'onetile', 'one'])) {
|
||||
this.options.tileDepth = tile.depth;
|
||||
} else {
|
||||
throw new Error("Invalid tile depth '" + tile.depth + "', should be one of 'onepixel', 'onetile' or 'one'");
|
||||
}
|
||||
}
|
||||
}
|
||||
// Format
|
||||
if (is.inArray(this.options.formatOut, ['jpeg', 'png', 'webp'])) {
|
||||
@@ -368,6 +491,7 @@ function tile (tile) {
|
||||
} else if (this.options.formatOut !== 'input') {
|
||||
throw new Error('Invalid tile format ' + this.options.formatOut);
|
||||
}
|
||||
|
||||
return this._updateFormatOut('dz');
|
||||
}
|
||||
|
||||
|
||||
@@ -253,6 +253,7 @@ function ignoreAspectRatio () {
|
||||
* 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:
|
||||
* "*change the dimensions of the image only if its width or height exceeds the geometry specification*".
|
||||
* Use with `max()` to preserve the image's aspect ratio.
|
||||
*
|
||||
* The default behaviour *before* function call is `false`, meaning the image will be enlarged.
|
||||
*
|
||||
|
||||
@@ -5,6 +5,8 @@ site_description: High performance Node.js image processing, the fastest module
|
||||
copyright: <a href="https://pixelplumbing.com/">pixelplumbing.com</a>
|
||||
google_analytics: ['UA-13034748-12', 'sharp.pixelplumbing.com']
|
||||
theme: readthedocs
|
||||
extra_css:
|
||||
- css/extra.css
|
||||
markdown_extensions:
|
||||
- toc:
|
||||
permalink: True
|
||||
|
||||
48
package.json
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "sharp",
|
||||
"description": "High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP and TIFF images",
|
||||
"version": "0.19.0",
|
||||
"version": "0.20.7",
|
||||
"author": "Lovell Fuller <npm@lovell.info>",
|
||||
"homepage": "https://github.com/lovell/sharp",
|
||||
"contributors": [
|
||||
@@ -39,17 +39,28 @@
|
||||
"Guy Maliar <guy@tailorbrands.com>",
|
||||
"Nicolas Coden <nicolas@ncoden.fr>",
|
||||
"Matt Parrish <matt.r.parrish@gmail.com>",
|
||||
"Marcel Bretschneider <marcel.bretschneider@gmail.com>",
|
||||
"Matthew McEachen <matthew+github@mceachen.org>",
|
||||
"Jarda Kotěšovec <jarda.kotesovec@gmail.com>",
|
||||
"Kenric D'Souza <kenric.dsouza@gmail.com>",
|
||||
"Oleh Aleinyk <oleg.aleynik@gmail.com>"
|
||||
"Oleh Aleinyk <oleg.aleynik@gmail.com>",
|
||||
"Marcel Bretschneider <marcel.bretschneider@gmail.com>",
|
||||
"Andrea Bianco <andrea.bianco@unibas.ch>",
|
||||
"Rik Heywood <rik@rik.org>",
|
||||
"Thomas Parisot <hi@oncletom.io>",
|
||||
"Nathan Graves <nathanrgraves+github@gmail.com>",
|
||||
"Tom Lokhorst <tom@lokhorst.eu>",
|
||||
"Espen Hovlandsdal <espen@hovlandsdal.com>",
|
||||
"Sylvain Dumont <sylvain.dumont35@gmail.com>",
|
||||
"Alun Davies <alun.owain.davies@googlemail.com>"
|
||||
],
|
||||
"scripts": {
|
||||
"install": "(node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)",
|
||||
"clean": "rm -rf node_modules/ build/ vendor/ coverage/ test/fixtures/output.*",
|
||||
"test": "semistandard && cc && 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 && prebuild-ci",
|
||||
"coverage": "./test/coverage/report.sh",
|
||||
"test-leak": "./test/leak/leak.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"
|
||||
"docs": "for m in constructor input resize composite operation colour channel output utility; do documentation build --shallow --format=md --markdown-toc=false lib/$m.js >docs/api-$m.md; done"
|
||||
},
|
||||
"main": "lib/index.js",
|
||||
"repository": {
|
||||
@@ -73,25 +84,30 @@
|
||||
"vips"
|
||||
],
|
||||
"dependencies": {
|
||||
"color": "^2.0.1",
|
||||
"color": "^3.0.0",
|
||||
"detect-libc": "^1.0.3",
|
||||
"nan": "^2.8.0",
|
||||
"semver": "^5.4.1",
|
||||
"simple-get": "^2.7.0",
|
||||
"tar": "^4.2.0",
|
||||
"nan": "^2.10.0",
|
||||
"fs-copy-file-sync": "^1.1.1",
|
||||
"npmlog": "^4.1.2",
|
||||
"prebuild-install": "^4.0.0",
|
||||
"semver": "^5.5.1",
|
||||
"simple-get": "^2.8.1",
|
||||
"tar": "^4.4.6",
|
||||
"tunnel-agent": "^0.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"async": "^2.6.0",
|
||||
"cc": "^1.0.1",
|
||||
"documentation": "^5.3.5",
|
||||
"async": "^2.6.1",
|
||||
"cc": "^1.0.2",
|
||||
"decompress-zip": "^0.3.1",
|
||||
"documentation": "^8.1.1",
|
||||
"exif-reader": "^1.0.2",
|
||||
"icc": "^1.0.0",
|
||||
"mocha": "^4.1.0",
|
||||
"nyc": "^11.4.1",
|
||||
"mocha": "^5.2.0",
|
||||
"nyc": "^12.0.2",
|
||||
"prebuild": "^7.6.2",
|
||||
"prebuild-ci": "^2.2.3",
|
||||
"rimraf": "^2.6.2",
|
||||
"semistandard": "^12.0.0",
|
||||
"unzip": "^0.1.11"
|
||||
"semistandard": "^12.0.1"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"config": {
|
||||
|
||||
@@ -63,6 +63,10 @@ namespace sharp {
|
||||
descriptor->rawWidth = AttrTo<uint32_t>(input, "rawWidth");
|
||||
descriptor->rawHeight = AttrTo<uint32_t>(input, "rawHeight");
|
||||
}
|
||||
// Page input for multi-page TIFF
|
||||
if (HasAttr(input, "page")) {
|
||||
descriptor->page = AttrTo<uint32_t>(input, "page");
|
||||
}
|
||||
// Create new image
|
||||
if (HasAttr(input, "createChannels")) {
|
||||
descriptor->createChannels = AttrTo<uint32_t>(input, "createChannels");
|
||||
@@ -229,6 +233,9 @@ namespace sharp {
|
||||
if (imageType == ImageType::MAGICK) {
|
||||
option->set("density", std::to_string(descriptor->density).data());
|
||||
}
|
||||
if (imageType == ImageType::TIFF) {
|
||||
option->set("page", descriptor->page);
|
||||
}
|
||||
image = VImage::new_from_buffer(descriptor->buffer, descriptor->bufferLength, nullptr, option);
|
||||
if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) {
|
||||
SetDensity(image, descriptor->density);
|
||||
@@ -268,6 +275,9 @@ namespace sharp {
|
||||
if (imageType == ImageType::MAGICK) {
|
||||
option->set("density", std::to_string(descriptor->density).data());
|
||||
}
|
||||
if (imageType == ImageType::TIFF) {
|
||||
option->set("page", descriptor->page);
|
||||
}
|
||||
image = VImage::new_from_file(descriptor->file.data(), option);
|
||||
if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) {
|
||||
SetDensity(image, descriptor->density);
|
||||
|
||||
@@ -53,6 +53,7 @@ namespace sharp {
|
||||
int rawChannels;
|
||||
int rawWidth;
|
||||
int rawHeight;
|
||||
int page;
|
||||
int createChannels;
|
||||
int createWidth;
|
||||
int createHeight;
|
||||
@@ -66,6 +67,7 @@ namespace sharp {
|
||||
rawChannels(0),
|
||||
rawWidth(0),
|
||||
rawHeight(0),
|
||||
page(0),
|
||||
createChannels(0),
|
||||
createWidth(0),
|
||||
createHeight(0) {
|
||||
|
||||
@@ -27,7 +27,8 @@ class MetadataWorker : public Nan::AsyncWorker {
|
||||
MetadataWorker(
|
||||
Nan::Callback *callback, MetadataBaton *baton, Nan::Callback *debuglog,
|
||||
std::vector<v8::Local<v8::Object>> const buffersToPersist) :
|
||||
Nan::AsyncWorker(callback), baton(baton), debuglog(debuglog),
|
||||
Nan::AsyncWorker(callback, "sharp:MetadataWorker"),
|
||||
baton(baton), debuglog(debuglog),
|
||||
buffersToPersist(buffersToPersist) {
|
||||
// Protect Buffer objects from GC, keyed on index
|
||||
std::accumulate(buffersToPersist.begin(), buffersToPersist.end(), 0,
|
||||
@@ -82,9 +83,9 @@ class MetadataWorker : public Nan::AsyncWorker {
|
||||
baton->iccLength = iccLength;
|
||||
}
|
||||
// IPTC
|
||||
if (image.get_typeof(VIPS_META_IPCT_NAME) == VIPS_TYPE_BLOB) {
|
||||
if (image.get_typeof(VIPS_META_IPTC_NAME) == VIPS_TYPE_BLOB) {
|
||||
size_t iptcLength;
|
||||
void const *iptc = image.get_blob(VIPS_META_IPCT_NAME, &iptcLength);
|
||||
void const *iptc = image.get_blob(VIPS_META_IPTC_NAME, &iptcLength);
|
||||
baton->iptc = static_cast<char *>(g_malloc(iptcLength));
|
||||
memcpy(baton->iptc, iptc, iptcLength);
|
||||
baton->iptcLength = iptcLength;
|
||||
@@ -165,12 +166,12 @@ class MetadataWorker : public Nan::AsyncWorker {
|
||||
std::string warning = sharp::VipsWarningPop();
|
||||
while (!warning.empty()) {
|
||||
v8::Local<v8::Value> message[1] = { New(warning).ToLocalChecked() };
|
||||
debuglog->Call(1, message);
|
||||
debuglog->Call(1, message, async_resource);
|
||||
warning = sharp::VipsWarningPop();
|
||||
}
|
||||
|
||||
// Return to JavaScript
|
||||
callback->Call(2, argv);
|
||||
callback->Call(2, argv, async_resource);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -28,6 +28,16 @@ using vips::VError;
|
||||
|
||||
namespace sharp {
|
||||
|
||||
/*
|
||||
Removes alpha channel, if any.
|
||||
*/
|
||||
VImage RemoveAlpha(VImage image) {
|
||||
if (HasAlpha(image)) {
|
||||
image = image.extract_band(0, VImage::option()->set("n", image.bands() - 1));
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/*
|
||||
Composite overlayImage over image at given position
|
||||
Assumes alpha channels are already premultiplied and will be unpremultiplied after
|
||||
@@ -152,6 +162,33 @@ namespace sharp {
|
||||
return dst.bandjoin(mask.cast(dst.format()));
|
||||
}
|
||||
|
||||
/*
|
||||
* Tint an image using the specified chroma, preserving the original image luminance
|
||||
*/
|
||||
VImage Tint(VImage image, double const a, double const b) {
|
||||
// Get original colourspace
|
||||
VipsInterpretation typeBeforeTint = image.interpretation();
|
||||
if (typeBeforeTint == VIPS_INTERPRETATION_RGB) {
|
||||
typeBeforeTint = VIPS_INTERPRETATION_sRGB;
|
||||
}
|
||||
// Extract luminance
|
||||
VImage luminance = image.colourspace(VIPS_INTERPRETATION_LAB)[0];
|
||||
// Create the tinted version by combining the L from the original and the chroma from the tint
|
||||
std::vector<double> chroma {a, b};
|
||||
VImage tinted = luminance
|
||||
.bandjoin(chroma)
|
||||
.copy(VImage::option()->set("interpretation", VIPS_INTERPRETATION_LAB))
|
||||
.colourspace(typeBeforeTint);
|
||||
// Attach original alpha channel, if any
|
||||
if (HasAlpha(image)) {
|
||||
// Extract original alpha channel
|
||||
VImage alpha = image[image.bands() - 1];
|
||||
// Join alpha channel to normalised image
|
||||
tinted = tinted.bandjoin(alpha);
|
||||
}
|
||||
return tinted;
|
||||
}
|
||||
|
||||
/*
|
||||
* Stretch luminance to cover full dynamic range.
|
||||
*/
|
||||
@@ -196,10 +233,8 @@ namespace sharp {
|
||||
VImage Gamma(VImage image, double const exponent) {
|
||||
if (HasAlpha(image)) {
|
||||
// Separate alpha channel
|
||||
VImage imageWithoutAlpha = image.extract_band(0,
|
||||
VImage::option()->set("n", image.bands() - 1));
|
||||
VImage alpha = image[image.bands() - 1];
|
||||
return imageWithoutAlpha.gamma(VImage::option()->set("exponent", exponent)).bandjoin(alpha);
|
||||
return RemoveAlpha(image).gamma(VImage::option()->set("exponent", exponent)).bandjoin(alpha);
|
||||
} else {
|
||||
return image.gamma(VImage::option()->set("exponent", exponent));
|
||||
}
|
||||
@@ -341,4 +376,16 @@ namespace sharp {
|
||||
return image.extract_area(left, top, width, height);
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate (a * in + b)
|
||||
*/
|
||||
VImage Linear(VImage image, double const a, double const b) {
|
||||
if (HasAlpha(image)) {
|
||||
// Separate alpha channel
|
||||
VImage alpha = image[image.bands() - 1];
|
||||
return RemoveAlpha(image).linear(a, b).bandjoin(alpha);
|
||||
} else {
|
||||
return image.linear(a, b);
|
||||
}
|
||||
}
|
||||
} // namespace sharp
|
||||
|
||||
@@ -25,6 +25,11 @@ using vips::VImage;
|
||||
|
||||
namespace sharp {
|
||||
|
||||
/*
|
||||
Removes alpha channel, if any.
|
||||
*/
|
||||
VImage RemoveAlpha(VImage image);
|
||||
|
||||
/*
|
||||
Alpha composite src over dst with given gravity.
|
||||
Assumes alpha channels are already premultiplied and will be unpremultiplied after.
|
||||
@@ -46,6 +51,11 @@ namespace sharp {
|
||||
*/
|
||||
VImage Cutout(VImage src, VImage dst, const int gravity);
|
||||
|
||||
/*
|
||||
* Tint an image using the specified chroma, preserving the original image luminance
|
||||
*/
|
||||
VImage Tint(VImage image, double const a, double const b);
|
||||
|
||||
/*
|
||||
* Stretch luminance to cover full dynamic range.
|
||||
*/
|
||||
@@ -92,6 +102,11 @@ namespace sharp {
|
||||
*/
|
||||
VImage Trim(VImage image, int const tolerance);
|
||||
|
||||
/*
|
||||
* Linear adjustment (a * in + b)
|
||||
*/
|
||||
VImage Linear(VImage image, double const a, double const b);
|
||||
|
||||
} // namespace sharp
|
||||
|
||||
#endif // SRC_OPERATIONS_H_
|
||||
|
||||
121
src/pipeline.cc
@@ -35,7 +35,8 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
PipelineWorker(
|
||||
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),
|
||||
Nan::AsyncWorker(callback, "sharp:PipelineWorker"),
|
||||
baton(baton), debuglog(debuglog), queueListener(queueListener),
|
||||
buffersToPersist(buffersToPersist) {
|
||||
// Protect Buffer objects from GC, keyed on index
|
||||
std::accumulate(buffersToPersist.begin(), buffersToPersist.end(), 0,
|
||||
@@ -280,15 +281,17 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
}
|
||||
}
|
||||
// Recalculate integral shrink and double residual
|
||||
int shrunkOnLoadWidth = image.width();
|
||||
int shrunkOnLoadHeight = image.height();
|
||||
int const shrunkOnLoadWidth = image.width();
|
||||
int const shrunkOnLoadHeight = image.height();
|
||||
if (!baton->rotateBeforePreExtract &&
|
||||
(rotation == VIPS_ANGLE_D90 || rotation == VIPS_ANGLE_D270)) {
|
||||
// Swap input output width and height when rotating by 90 or 270 degrees
|
||||
std::swap(shrunkOnLoadWidth, shrunkOnLoadHeight);
|
||||
// Swap when rotating by 90 or 270 degrees
|
||||
xfactor = static_cast<double>(shrunkOnLoadWidth) / static_cast<double>(targetResizeHeight);
|
||||
yfactor = static_cast<double>(shrunkOnLoadHeight) / static_cast<double>(targetResizeWidth);
|
||||
} else {
|
||||
xfactor = static_cast<double>(shrunkOnLoadWidth) / static_cast<double>(targetResizeWidth);
|
||||
yfactor = static_cast<double>(shrunkOnLoadHeight) / static_cast<double>(targetResizeHeight);
|
||||
}
|
||||
xfactor = static_cast<double>(shrunkOnLoadWidth) / static_cast<double>(targetResizeWidth);
|
||||
yfactor = static_cast<double>(shrunkOnLoadHeight) / static_cast<double>(targetResizeHeight);
|
||||
}
|
||||
|
||||
// Ensure we're using a device-independent colour space
|
||||
@@ -358,6 +361,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 shouldApplyMedian = baton->medianSize > 0;
|
||||
|
||||
bool const shouldPremultiplyAlpha = HasAlpha(image) &&
|
||||
(shouldResize || shouldBlur || shouldConv || shouldSharpen || shouldOverlayWithAlpha);
|
||||
|
||||
@@ -378,7 +383,6 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
) {
|
||||
throw vips::VError("Unknown kernel");
|
||||
}
|
||||
|
||||
image = image.resize(1.0 / xfactor, VImage::option()
|
||||
->set("vscale", 1.0 / yfactor)
|
||||
->set("kernel", kernel));
|
||||
@@ -465,7 +469,10 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
->set("extend", VIPS_EXTEND_BACKGROUND)
|
||||
->set("background", background));
|
||||
|
||||
} else if (baton->canvas != Canvas::IGNORE_ASPECT) {
|
||||
} else if (
|
||||
baton->canvas != Canvas::IGNORE_ASPECT &&
|
||||
(image.width() > baton->width || image.height() > baton->height)
|
||||
) {
|
||||
// Crop/max/min
|
||||
if (baton->crop < 9) {
|
||||
// Gravity-based crop
|
||||
@@ -478,6 +485,12 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
image = image.extract_area(left, top, width, height);
|
||||
} else {
|
||||
// Attention-based or Entropy-based crop
|
||||
if (baton->width > image.width()) {
|
||||
baton->width = image.width();
|
||||
}
|
||||
if (baton->height > image.height()) {
|
||||
baton->height = image.height();
|
||||
}
|
||||
image = image.tilecache(VImage::option()
|
||||
->set("access", baton->accessMethod)
|
||||
->set("threaded", TRUE));
|
||||
@@ -534,7 +547,10 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
image = image.embed(baton->extendLeft, baton->extendTop, baton->width, baton->height,
|
||||
VImage::option()->set("extend", VIPS_EXTEND_BACKGROUND)->set("background", background));
|
||||
}
|
||||
|
||||
// Median - must happen before blurring, due to the utility of blurring after thresholding
|
||||
if (shouldApplyMedian) {
|
||||
image = image.median(baton->medianSize);
|
||||
}
|
||||
// Threshold - must happen before blurring, due to the utility of blurring after thresholding
|
||||
if (baton->threshold != 0) {
|
||||
image = sharp::Threshold(image, baton->threshold, baton->thresholdGrayscale);
|
||||
@@ -644,6 +660,11 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
image = sharp::Gamma(image, baton->gamma);
|
||||
}
|
||||
|
||||
// Linear adjustment (a * in + b)
|
||||
if (baton->linearA != 1.0 || baton->linearB != 0.0) {
|
||||
image = sharp::Linear(image, baton->linearA, baton->linearB);
|
||||
}
|
||||
|
||||
// Apply normalisation - stretch luminance to cover full dynamic range
|
||||
if (baton->normalise) {
|
||||
image = sharp::Normalise(image);
|
||||
@@ -662,14 +683,30 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
image = sharp::Bandbool(image, baton->bandBoolOp);
|
||||
}
|
||||
|
||||
// Tint the image
|
||||
if (baton->tintA < 128.0 || baton->tintB < 128.0) {
|
||||
image = sharp::Tint(image, baton->tintA, baton->tintB);
|
||||
}
|
||||
|
||||
// Extract an image channel (aka vips band)
|
||||
if (baton->extractChannel > -1) {
|
||||
if (baton->extractChannel >= image.bands()) {
|
||||
(baton->err).append("Cannot extract channel from image. Too few channels in image.");
|
||||
return Error();
|
||||
}
|
||||
image = image.extract_band(baton->extractChannel);
|
||||
VipsInterpretation const interpretation = sharp::Is16Bit(image.interpretation())
|
||||
? VIPS_INTERPRETATION_GREY16
|
||||
: VIPS_INTERPRETATION_B_W;
|
||||
image = image
|
||||
.extract_band(baton->extractChannel)
|
||||
.copy(VImage::option()->set("interpretation", interpretation));
|
||||
}
|
||||
|
||||
// Remove alpha channel, if any
|
||||
if (baton->removeAlpha) {
|
||||
image = sharp::RemoveAlpha(image);
|
||||
}
|
||||
|
||||
// Convert image to sRGB, if not already
|
||||
if (sharp::Is16Bit(image.interpretation())) {
|
||||
image = image.cast(VIPS_FORMAT_USHORT);
|
||||
@@ -705,9 +742,10 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
->set("interlace", baton->jpegProgressive)
|
||||
->set("no_subsample", baton->jpegChromaSubsampling == "4:4:4")
|
||||
->set("trellis_quant", baton->jpegTrellisQuantisation)
|
||||
->set("quant_table", baton->jpegQuantisationTable)
|
||||
->set("overshoot_deringing", baton->jpegOvershootDeringing)
|
||||
->set("optimize_scans", baton->jpegOptimiseScans)
|
||||
->set("optimize_coding", TRUE)));
|
||||
->set("optimize_coding", baton->jpegOptimiseCoding)));
|
||||
baton->bufferOut = static_cast<char*>(area->data);
|
||||
baton->bufferOutLength = area->length;
|
||||
area->free_fn = nullptr;
|
||||
@@ -820,9 +858,10 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
->set("interlace", baton->jpegProgressive)
|
||||
->set("no_subsample", baton->jpegChromaSubsampling == "4:4:4")
|
||||
->set("trellis_quant", baton->jpegTrellisQuantisation)
|
||||
->set("quant_table", baton->jpegQuantisationTable)
|
||||
->set("overshoot_deringing", baton->jpegOvershootDeringing)
|
||||
->set("optimize_scans", baton->jpegOptimiseScans)
|
||||
->set("optimize_coding", TRUE));
|
||||
->set("optimize_coding", baton->jpegOptimiseCoding));
|
||||
baton->formatOut = "jpeg";
|
||||
baton->channels = std::min(baton->channels, 3);
|
||||
} else if (baton->formatOut == "png" || (mightMatchInput && isPng) || (willMatchInput &&
|
||||
@@ -899,20 +938,30 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
{"interlace", baton->jpegProgressive ? "TRUE" : "FALSE"},
|
||||
{"no_subsample", baton->jpegChromaSubsampling == "4:4:4" ? "TRUE": "FALSE"},
|
||||
{"trellis_quant", baton->jpegTrellisQuantisation ? "TRUE" : "FALSE"},
|
||||
{"quant_table", std::to_string(baton->jpegQuantisationTable)},
|
||||
{"overshoot_deringing", baton->jpegOvershootDeringing ? "TRUE": "FALSE"},
|
||||
{"optimize_scans", baton->jpegOptimiseScans ? "TRUE": "FALSE"},
|
||||
{"optimize_coding", "TRUE"}
|
||||
{"optimize_coding", baton->jpegOptimiseCoding ? "TRUE": "FALSE"}
|
||||
};
|
||||
suffix = AssembleSuffixString(extname, options);
|
||||
}
|
||||
// Write DZ to file
|
||||
image.dzsave(const_cast<char*>(baton->fileOut.data()), VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("tile_size", baton->tileSize)
|
||||
->set("overlap", baton->tileOverlap)
|
||||
->set("container", baton->tileContainer)
|
||||
->set("layout", baton->tileLayout)
|
||||
->set("suffix", const_cast<char*>(suffix.data())));
|
||||
vips::VOption *options = VImage::option()
|
||||
->set("strip", !baton->withMetadata)
|
||||
->set("tile_size", baton->tileSize)
|
||||
->set("overlap", baton->tileOverlap)
|
||||
->set("container", baton->tileContainer)
|
||||
->set("layout", baton->tileLayout)
|
||||
->set("suffix", const_cast<char*>(suffix.data()))
|
||||
->set("angle", CalculateAngleRotation(baton->tileAngle));
|
||||
|
||||
// libvips chooses a default depth based on layout. Instead of replicating that logic here by
|
||||
// not passing anything - libvips will handle choice
|
||||
if (baton->tileDepth < VIPS_FOREIGN_DZ_DEPTH_LAST) {
|
||||
options->set("depth", baton->tileDepth);
|
||||
}
|
||||
|
||||
image.dzsave(const_cast<char*>(baton->fileOut.data()), options);
|
||||
baton->formatOut = "dz";
|
||||
} else if (baton->formatOut == "v" || (mightMatchInput && isV) ||
|
||||
(willMatchInput && inputImageType == ImageType::VIPS)) {
|
||||
@@ -1005,18 +1054,18 @@ class PipelineWorker : public Nan::AsyncWorker {
|
||||
std::string warning = sharp::VipsWarningPop();
|
||||
while (!warning.empty()) {
|
||||
v8::Local<v8::Value> message[1] = { New(warning).ToLocalChecked() };
|
||||
debuglog->Call(1, message);
|
||||
debuglog->Call(1, message, async_resource);
|
||||
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) };
|
||||
queueListener->Call(1, queueLength);
|
||||
queueListener->Call(1, queueLength, async_resource);
|
||||
delete queueListener;
|
||||
|
||||
// Return to JavaScript
|
||||
callback->Call(3, argv);
|
||||
callback->Call(3, argv, async_resource);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -1146,6 +1195,9 @@ NAN_METHOD(pipeline) {
|
||||
for (unsigned int i = 0; i < 4; i++) {
|
||||
baton->background[i] = AttrTo<double>(background, i);
|
||||
}
|
||||
// Tint chroma
|
||||
baton->tintA = AttrTo<double>(options, "tintA");
|
||||
baton->tintB = AttrTo<double>(options, "tintB");
|
||||
// Overlay options
|
||||
if (HasAttr(options, "overlay")) {
|
||||
baton->overlay = CreateInputDescriptor(AttrAs<v8::Object>(options, "overlay"), buffersToPersist);
|
||||
@@ -1178,6 +1230,7 @@ NAN_METHOD(pipeline) {
|
||||
baton->flatten = AttrTo<bool>(options, "flatten");
|
||||
baton->negate = AttrTo<bool>(options, "negate");
|
||||
baton->blurSigma = AttrTo<double>(options, "blurSigma");
|
||||
baton->medianSize = AttrTo<uint32_t>(options, "medianSize");
|
||||
baton->sharpenSigma = AttrTo<double>(options, "sharpenSigma");
|
||||
baton->sharpenFlat = AttrTo<double>(options, "sharpenFlat");
|
||||
baton->sharpenJagged = AttrTo<double>(options, "sharpenJagged");
|
||||
@@ -1185,6 +1238,8 @@ NAN_METHOD(pipeline) {
|
||||
baton->thresholdGrayscale = AttrTo<bool>(options, "thresholdGrayscale");
|
||||
baton->trimTolerance = AttrTo<int32_t>(options, "trimTolerance");
|
||||
baton->gamma = AttrTo<double>(options, "gamma");
|
||||
baton->linearA = AttrTo<double>(options, "linearA");
|
||||
baton->linearB = AttrTo<double>(options, "linearB");
|
||||
baton->greyscale = AttrTo<bool>(options, "greyscale");
|
||||
baton->normalise = AttrTo<bool>(options, "normalise");
|
||||
baton->useExifOrientation = AttrTo<bool>(options, "useExifOrientation");
|
||||
@@ -1197,6 +1252,7 @@ NAN_METHOD(pipeline) {
|
||||
baton->extendLeft = AttrTo<int32_t>(options, "extendLeft");
|
||||
baton->extendRight = AttrTo<int32_t>(options, "extendRight");
|
||||
baton->extractChannel = AttrTo<int32_t>(options, "extractChannel");
|
||||
baton->removeAlpha = AttrTo<bool>(options, "removeAlpha");
|
||||
if (HasAttr(options, "boolean")) {
|
||||
baton->boolean = CreateInputDescriptor(AttrAs<v8::Object>(options, "boolean"), buffersToPersist);
|
||||
baton->booleanOp = sharp::GetBooleanOperation(AttrAsStr(options, "booleanOp"));
|
||||
@@ -1231,8 +1287,10 @@ NAN_METHOD(pipeline) {
|
||||
baton->jpegProgressive = AttrTo<bool>(options, "jpegProgressive");
|
||||
baton->jpegChromaSubsampling = AttrAsStr(options, "jpegChromaSubsampling");
|
||||
baton->jpegTrellisQuantisation = AttrTo<bool>(options, "jpegTrellisQuantisation");
|
||||
baton->jpegQuantisationTable = AttrTo<uint32_t>(options, "jpegQuantisationTable");
|
||||
baton->jpegOvershootDeringing = AttrTo<bool>(options, "jpegOvershootDeringing");
|
||||
baton->jpegOptimiseScans = AttrTo<bool>(options, "jpegOptimiseScans");
|
||||
baton->jpegOptimiseCoding = AttrTo<bool>(options, "jpegOptimiseCoding");
|
||||
baton->pngProgressive = AttrTo<bool>(options, "pngProgressive");
|
||||
baton->pngCompressionLevel = AttrTo<uint32_t>(options, "pngCompressionLevel");
|
||||
baton->pngAdaptiveFiltering = AttrTo<bool>(options, "pngAdaptiveFiltering");
|
||||
@@ -1256,6 +1314,7 @@ NAN_METHOD(pipeline) {
|
||||
baton->tileSize = AttrTo<uint32_t>(options, "tileSize");
|
||||
baton->tileOverlap = AttrTo<uint32_t>(options, "tileOverlap");
|
||||
std::string tileContainer = AttrAsStr(options, "tileContainer");
|
||||
baton->tileAngle = AttrTo<int32_t>(options, "tileAngle");
|
||||
if (tileContainer == "zip") {
|
||||
baton->tileContainer = VIPS_FOREIGN_DZ_CONTAINER_ZIP;
|
||||
} else {
|
||||
@@ -1270,6 +1329,17 @@ NAN_METHOD(pipeline) {
|
||||
baton->tileLayout = VIPS_FOREIGN_DZ_LAYOUT_DZ;
|
||||
}
|
||||
baton->tileFormat = AttrAsStr(options, "tileFormat");
|
||||
std::string tileDepth = AttrAsStr(options, "tileDepth");
|
||||
if (tileDepth == "onetile") {
|
||||
baton->tileDepth = VIPS_FOREIGN_DZ_DEPTH_ONETILE;
|
||||
} else if (tileDepth == "one") {
|
||||
baton->tileDepth = VIPS_FOREIGN_DZ_DEPTH_ONE;
|
||||
} else if (tileDepth == "onepixel") {
|
||||
baton->tileDepth = VIPS_FOREIGN_DZ_DEPTH_ONEPIXEL;
|
||||
} else {
|
||||
// signal that we do not want to pass any value to dzSave
|
||||
baton->tileDepth = VIPS_FOREIGN_DZ_DEPTH_LAST;
|
||||
}
|
||||
// Force random access for certain operations
|
||||
if (baton->accessMethod == VIPS_ACCESS_SEQUENTIAL && (
|
||||
baton->trimTolerance != 0 || baton->normalise ||
|
||||
@@ -1290,5 +1360,6 @@ NAN_METHOD(pipeline) {
|
||||
// Increment queued task counter
|
||||
g_atomic_int_inc(&sharp::counterQueue);
|
||||
v8::Local<v8::Value> queueLength[1] = { Nan::New<v8::Uint32>(sharp::counterQueue) };
|
||||
queueListener->Call(1, queueLength);
|
||||
v8::Local<v8::Object> recv = Nan::New<v8::Object>();
|
||||
Nan::Call(*queueListener, recv, 1, queueLength);
|
||||
}
|
||||
|
||||
@@ -70,15 +70,20 @@ struct PipelineBaton {
|
||||
std::string kernel;
|
||||
bool fastShrinkOnLoad;
|
||||
double background[4];
|
||||
double tintA;
|
||||
double tintB;
|
||||
bool flatten;
|
||||
bool negate;
|
||||
double blurSigma;
|
||||
int medianSize;
|
||||
double sharpenSigma;
|
||||
double sharpenFlat;
|
||||
double sharpenJagged;
|
||||
int threshold;
|
||||
bool thresholdGrayscale;
|
||||
int trimTolerance;
|
||||
double linearA;
|
||||
double linearB;
|
||||
double gamma;
|
||||
bool greyscale;
|
||||
bool normalise;
|
||||
@@ -97,8 +102,10 @@ struct PipelineBaton {
|
||||
bool jpegProgressive;
|
||||
std::string jpegChromaSubsampling;
|
||||
bool jpegTrellisQuantisation;
|
||||
int jpegQuantisationTable;
|
||||
bool jpegOvershootDeringing;
|
||||
bool jpegOptimiseScans;
|
||||
bool jpegOptimiseCoding;
|
||||
bool pngProgressive;
|
||||
int pngCompressionLevel;
|
||||
bool pngAdaptiveFiltering;
|
||||
@@ -124,12 +131,15 @@ struct PipelineBaton {
|
||||
VipsOperationBoolean booleanOp;
|
||||
VipsOperationBoolean bandBoolOp;
|
||||
int extractChannel;
|
||||
bool removeAlpha;
|
||||
VipsInterpretation colourspace;
|
||||
int tileSize;
|
||||
int tileOverlap;
|
||||
VipsForeignDzContainer tileContainer;
|
||||
VipsForeignDzLayout tileLayout;
|
||||
std::string tileFormat;
|
||||
int tileAngle;
|
||||
VipsForeignDzDepth tileDepth;
|
||||
|
||||
PipelineBaton():
|
||||
input(nullptr),
|
||||
@@ -151,15 +161,20 @@ struct PipelineBaton {
|
||||
cropOffsetLeft(0),
|
||||
cropOffsetTop(0),
|
||||
premultiplied(false),
|
||||
tintA(128.0),
|
||||
tintB(128.0),
|
||||
flatten(false),
|
||||
negate(false),
|
||||
blurSigma(0.0),
|
||||
medianSize(0),
|
||||
sharpenSigma(0.0),
|
||||
sharpenFlat(1.0),
|
||||
sharpenJagged(2.0),
|
||||
threshold(0),
|
||||
thresholdGrayscale(true),
|
||||
trimTolerance(0),
|
||||
linearA(1.0),
|
||||
linearB(0.0),
|
||||
gamma(0.0),
|
||||
greyscale(false),
|
||||
normalise(false),
|
||||
@@ -176,8 +191,10 @@ struct PipelineBaton {
|
||||
jpegProgressive(false),
|
||||
jpegChromaSubsampling("4:2:0"),
|
||||
jpegTrellisQuantisation(false),
|
||||
jpegQuantisationTable(0),
|
||||
jpegOvershootDeringing(false),
|
||||
jpegOptimiseScans(false),
|
||||
jpegOptimiseCoding(true),
|
||||
pngProgressive(false),
|
||||
pngCompressionLevel(9),
|
||||
pngAdaptiveFiltering(false),
|
||||
@@ -198,11 +215,14 @@ struct PipelineBaton {
|
||||
booleanOp(VIPS_OPERATION_BOOLEAN_LAST),
|
||||
bandBoolOp(VIPS_OPERATION_BOOLEAN_LAST),
|
||||
extractChannel(-1),
|
||||
removeAlpha(false),
|
||||
colourspace(VIPS_INTERPRETATION_LAST),
|
||||
tileSize(256),
|
||||
tileOverlap(0),
|
||||
tileContainer(VIPS_FOREIGN_DZ_CONTAINER_FS),
|
||||
tileLayout(VIPS_FOREIGN_DZ_LAYOUT_DZ) {
|
||||
tileLayout(VIPS_FOREIGN_DZ_LAYOUT_DZ),
|
||||
tileAngle(0),
|
||||
tileDepth(VIPS_FOREIGN_DZ_DEPTH_LAST){
|
||||
background[0] = 0.0;
|
||||
background[1] = 0.0;
|
||||
background[2] = 0.0;
|
||||
|
||||
26
src/stats.cc
@@ -28,7 +28,8 @@ class StatsWorker : public Nan::AsyncWorker {
|
||||
StatsWorker(
|
||||
Nan::Callback *callback, StatsBaton *baton, Nan::Callback *debuglog,
|
||||
std::vector<v8::Local<v8::Object>> const buffersToPersist) :
|
||||
Nan::AsyncWorker(callback), baton(baton), debuglog(debuglog),
|
||||
Nan::AsyncWorker(callback, "sharp:StatsWorker"),
|
||||
baton(baton), debuglog(debuglog),
|
||||
buffersToPersist(buffersToPersist) {
|
||||
// Protect Buffer objects from GC, keyed on index
|
||||
std::accumulate(buffersToPersist.begin(), buffersToPersist.end(), 0,
|
||||
@@ -58,7 +59,6 @@ class StatsWorker : public Nan::AsyncWorker {
|
||||
using sharp::MaximumImageAlpha;
|
||||
|
||||
vips::VImage image;
|
||||
vips::VImage stats;
|
||||
sharp::ImageType imageType = sharp::ImageType::UNKNOWN;
|
||||
|
||||
try {
|
||||
@@ -68,9 +68,8 @@ class StatsWorker : public Nan::AsyncWorker {
|
||||
}
|
||||
if (imageType != sharp::ImageType::UNKNOWN) {
|
||||
try {
|
||||
stats = image.stats();
|
||||
int bands = image.bands();
|
||||
double const max = MaximumImageAlpha(image.interpretation());
|
||||
vips::VImage stats = image.stats();
|
||||
int const bands = image.bands();
|
||||
for (int b = 1; b <= bands; b++) {
|
||||
ChannelStats cStats(static_cast<int>(stats.getpoint(STAT_MIN_INDEX, b).front()),
|
||||
static_cast<int>(stats.getpoint(STAT_MAX_INDEX, b).front()),
|
||||
@@ -82,11 +81,15 @@ class StatsWorker : public Nan::AsyncWorker {
|
||||
static_cast<int>(stats.getpoint(STAT_MAXY_INDEX, b).front()));
|
||||
baton->channelStats.push_back(cStats);
|
||||
}
|
||||
|
||||
// alpha layer is there and the last band i.e. alpha has its max value greater than 0)
|
||||
if (sharp::HasAlpha(image) && stats.getpoint(STAT_MIN_INDEX, bands).front() != max) {
|
||||
baton->isOpaque = false;
|
||||
// Image is not opaque when alpha layer is present and contains a non-mamixa value
|
||||
if (sharp::HasAlpha(image)) {
|
||||
double const minAlpha = static_cast<double>(stats.getpoint(STAT_MIN_INDEX, bands).front());
|
||||
if (minAlpha != MaximumImageAlpha(image.interpretation())) {
|
||||
baton->isOpaque = false;
|
||||
}
|
||||
}
|
||||
// Estimate entropy via histogram of greyscale value frequency
|
||||
baton->entropy = std::abs(image.colourspace(VIPS_INTERPRETATION_B_W)[0].hist_find().hist_entropy());
|
||||
} catch (vips::VError const &err) {
|
||||
(baton->err).append(err.what());
|
||||
}
|
||||
@@ -129,6 +132,7 @@ class StatsWorker : public Nan::AsyncWorker {
|
||||
|
||||
Set(info, New("channels").ToLocalChecked(), channels);
|
||||
Set(info, New("isOpaque").ToLocalChecked(), New<v8::Boolean>(baton->isOpaque));
|
||||
Set(info, New("entropy").ToLocalChecked(), New<v8::Number>(baton->entropy));
|
||||
argv[1] = info;
|
||||
}
|
||||
|
||||
@@ -145,12 +149,12 @@ class StatsWorker : public Nan::AsyncWorker {
|
||||
std::string warning = sharp::VipsWarningPop();
|
||||
while (!warning.empty()) {
|
||||
v8::Local<v8::Value> message[1] = { New(warning).ToLocalChecked() };
|
||||
debuglog->Call(1, message);
|
||||
debuglog->Call(1, message, async_resource);
|
||||
warning = sharp::VipsWarningPop();
|
||||
}
|
||||
|
||||
// Return to JavaScript
|
||||
callback->Call(2, argv);
|
||||
callback->Call(2, argv, async_resource);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -51,12 +51,14 @@ struct StatsBaton {
|
||||
// Output
|
||||
std::vector<ChannelStats> channelStats;
|
||||
bool isOpaque;
|
||||
double entropy;
|
||||
|
||||
std::string err;
|
||||
|
||||
StatsBaton():
|
||||
input(nullptr),
|
||||
isOpaque(true)
|
||||
isOpaque(true),
|
||||
entropy(0.0)
|
||||
{}
|
||||
};
|
||||
|
||||
|
||||
BIN
test/fixtures/2569067123_aca715a2ee_o.png
vendored
Normal file
|
After Width: | Height: | Size: 6.8 MiB |
BIN
test/fixtures/G31D_MULTI.TIF
vendored
Normal file
BIN
test/fixtures/expected/alpha-layer-1-fill-linear.png
vendored
Normal file
|
After Width: | Height: | Size: 154 KiB |
BIN
test/fixtures/expected/alpha-layer-1-fill-offset.png
vendored
Normal file
|
After Width: | Height: | Size: 112 KiB |
BIN
test/fixtures/expected/alpha-layer-1-fill-slope.png
vendored
Normal file
|
After Width: | Height: | Size: 179 KiB |
BIN
test/fixtures/expected/extract-alpha-16bit.jpg
vendored
Normal file
|
After Width: | Height: | Size: 685 B |
BIN
test/fixtures/expected/extract-lch.jpg
vendored
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
test/fixtures/expected/low-contrast-linear.jpg
vendored
Normal file
|
After Width: | Height: | Size: 18 KiB |
BIN
test/fixtures/expected/low-contrast-offset.jpg
vendored
Normal file
|
After Width: | Height: | Size: 7.5 KiB |
BIN
test/fixtures/expected/low-contrast-slope.jpg
vendored
Normal file
|
After Width: | Height: | Size: 11 KiB |
BIN
test/fixtures/expected/median_1.jpg
vendored
Normal file
|
After Width: | Height: | Size: 20 KiB |
BIN
test/fixtures/expected/median_3.jpg
vendored
Normal file
|
After Width: | Height: | Size: 833 B |
BIN
test/fixtures/expected/median_5.jpg
vendored
Normal file
|
After Width: | Height: | Size: 640 B |
BIN
test/fixtures/expected/median_color.jpg
vendored
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
test/fixtures/expected/tint-alpha.png
vendored
Normal file
|
After Width: | Height: | Size: 142 KiB |
BIN
test/fixtures/expected/tint-blue.jpg
vendored
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
test/fixtures/expected/tint-cmyk.jpg
vendored
Normal file
|
After Width: | Height: | Size: 26 KiB |
BIN
test/fixtures/expected/tint-green.jpg
vendored
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
test/fixtures/expected/tint-red.jpg
vendored
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
test/fixtures/expected/tint-sepia.jpg
vendored
Normal file
|
After Width: | Height: | Size: 14 KiB |
4
test/fixtures/index.js
vendored
@@ -69,6 +69,8 @@ module.exports = {
|
||||
inputJpgOverlayLayer2: getPath('alpha-layer-2-ink.jpg'),
|
||||
inputJpgTruncated: getPath('truncated.jpg'), // head -c 10000 2569067123_aca715a2ee_o.jpg > truncated.jpg
|
||||
inputJpgCenteredImage: getPath('centered_image.jpeg'),
|
||||
inputJpgRandom: getPath('random.jpg'), // convert -size 200x200 xc: +noise Random random.jpg
|
||||
inputJpgThRandom: getPath('thRandom.jpg'), // convert random.jpg -channel G -threshold 5% -separate +channel -negate thRandom.jpg
|
||||
|
||||
inputPng: getPath('50020484-00001.png'), // http://c.searspartsdirect.com/lis_png/PLDM/50020484-00001.png
|
||||
inputPngWithTransparency: getPath('blackbug.png'), // public domain
|
||||
@@ -87,10 +89,12 @@ module.exports = {
|
||||
inputPngTestJoinChannel: getPath('testJoinChannel.png'),
|
||||
inputPngTruncated: getPath('truncated.png'), // gm convert 2569067123_aca715a2ee_o.jpg -resize 320x240 saw.png ; head -c 10000 saw.png > truncated.png
|
||||
inputPngEmbed: getPath('embedgravitybird.png'), // Released to sharp under a CC BY 4.0
|
||||
inputPngRGBWithAlpha: getPath('2569067123_aca715a2ee_o.png'), // http://www.flickr.com/photos/grizdave/2569067123/ (same as inputJpg)
|
||||
|
||||
inputWebP: getPath('4.webp'), // http://www.gstatic.com/webp/gallery/4.webp
|
||||
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
|
||||
inputTiffMultipage: getPath('G31D_MULTI.TIF'), // gm convert G31D.TIF -resize 50% G31D_2.TIF ; tiffcp G31D.TIF G31D_2.TIF G31D_MULTI.TIF
|
||||
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'),
|
||||
|
||||
BIN
test/fixtures/random.jpg
vendored
Normal file
|
After Width: | Height: | Size: 68 KiB |
BIN
test/fixtures/thRandom.jpg
vendored
Normal file
|
After Width: | Height: | Size: 26 KiB |
@@ -81,35 +81,45 @@ describe('Alpha transparency', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Enlargement with non-nearest neighbor interpolation shouldn’t cause dark edges', function (done) {
|
||||
it('Enlargement with non-nearest neighbor interpolation shouldn’t cause dark edges', function () {
|
||||
const base = 'alpha-premultiply-enlargement-2048x1536-paper.png';
|
||||
const actual = fixtures.path('output.' + base);
|
||||
const expected = fixtures.expected(base);
|
||||
sharp(fixtures.inputPngAlphaPremultiplicationSmall)
|
||||
return sharp(fixtures.inputPngAlphaPremultiplicationSmall)
|
||||
.resize(2048, 1536)
|
||||
.toFile(actual, function (err) {
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
fixtures.assertMaxColourDistance(actual, expected, 102);
|
||||
done();
|
||||
}
|
||||
.toFile(actual)
|
||||
.then(function () {
|
||||
fixtures.assertMaxColourDistance(actual, expected, 102);
|
||||
});
|
||||
});
|
||||
|
||||
it('Reduction with non-nearest neighbor interpolation shouldn’t cause dark edges', function (done) {
|
||||
it('Reduction with non-nearest neighbor interpolation shouldn’t cause dark edges', function () {
|
||||
const base = 'alpha-premultiply-reduction-1024x768-paper.png';
|
||||
const actual = fixtures.path('output.' + base);
|
||||
const expected = fixtures.expected(base);
|
||||
sharp(fixtures.inputPngAlphaPremultiplicationLarge)
|
||||
return sharp(fixtures.inputPngAlphaPremultiplicationLarge)
|
||||
.resize(1024, 768)
|
||||
.toFile(actual, function (err) {
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
fixtures.assertMaxColourDistance(actual, expected, 102);
|
||||
done();
|
||||
}
|
||||
.toFile(actual)
|
||||
.then(function () {
|
||||
fixtures.assertMaxColourDistance(actual, expected, 102);
|
||||
});
|
||||
});
|
||||
|
||||
it('Removes alpha from fixtures with transparency, ignores those without', function () {
|
||||
return Promise.all([
|
||||
fixtures.inputPngWithTransparency,
|
||||
fixtures.inputPngWithTransparency16bit,
|
||||
fixtures.inputWebPWithTransparency,
|
||||
fixtures.inputJpg,
|
||||
fixtures.inputPng,
|
||||
fixtures.inputWebP
|
||||
].map(function (input) {
|
||||
return sharp(input)
|
||||
.removeAlpha()
|
||||
.toBuffer({ resolveWithObject: true })
|
||||
.then(function (result) {
|
||||
assert.strictEqual(3, result.info.channels);
|
||||
});
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -159,6 +159,24 @@ describe('Crop', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Skip crop when post-resize dimensions are at target', function () {
|
||||
return sharp(fixtures.inputJpg)
|
||||
.resize(1600, 1200)
|
||||
.toBuffer()
|
||||
.then(function (input) {
|
||||
return sharp(input)
|
||||
.resize(1110)
|
||||
.crop(sharp.strategy.attention)
|
||||
.toBuffer({ resolveWithObject: true })
|
||||
.then(function (result) {
|
||||
assert.strictEqual(1110, result.info.width);
|
||||
assert.strictEqual(832, result.info.height);
|
||||
assert.strictEqual(undefined, result.info.cropOffsetLeft);
|
||||
assert.strictEqual(undefined, result.info.cropOffsetTop);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Entropy-based strategy', function () {
|
||||
it('JPEG', function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
|
||||
@@ -54,6 +54,32 @@ describe('Image channel extraction', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('With colorspace conversion', function (done) {
|
||||
const output = fixtures.path('output.extract-lch.jpg');
|
||||
sharp(fixtures.inputJpg)
|
||||
.toColourspace('lch')
|
||||
.extractChannel(1)
|
||||
.resize(320, 240)
|
||||
.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
fixtures.assertMaxColourDistance(output, fixtures.expected('extract-lch.jpg'));
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('Alpha from 16-bit PNG', function (done) {
|
||||
const output = fixtures.path('output.extract-alpha-16bit.jpg');
|
||||
sharp(fixtures.inputPngWithTransparency16bit)
|
||||
.extractChannel(3)
|
||||
.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
fixtures.assertMaxColourDistance(output, fixtures.expected('extract-alpha-16bit.jpg'));
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('Invalid channel number', function () {
|
||||
assert.throws(function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
|
||||
150
test/unit/io.js
@@ -389,6 +389,16 @@ describe('Input/output', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Invalid JPEG quantisation table', function () {
|
||||
[-1, 88.2, 'test'].forEach(function (table) {
|
||||
it(table.toString(), function () {
|
||||
assert.throws(function () {
|
||||
sharp().jpeg({ quantisationTable: table });
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Progressive JPEG image', function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 240)
|
||||
@@ -528,6 +538,22 @@ describe('Input/output', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF file input with invalid page fails gracefully', function (done) {
|
||||
sharp(fixtures.inputTiffMultipage, { page: 2 })
|
||||
.toBuffer(function (err) {
|
||||
assert.strictEqual(true, !!err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF buffer input with invalid page fails gracefully', function (done) {
|
||||
sharp(fs.readFileSync(fixtures.inputTiffMultipage), { page: 2 })
|
||||
.toBuffer(function (err) {
|
||||
assert.strictEqual(true, !!err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Output filename with unknown extension', function () {
|
||||
it('Match JPEG input', function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
@@ -810,6 +836,67 @@ describe('Input/output', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Optimise coding generates smaller output length', function (done) {
|
||||
// First generate with optimize coding enabled (default)
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 240)
|
||||
.jpeg()
|
||||
.toBuffer(function (err, withOptimiseCoding, withInfo) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, withOptimiseCoding.length > 0);
|
||||
assert.strictEqual(withOptimiseCoding.length, withInfo.size);
|
||||
assert.strictEqual('jpeg', withInfo.format);
|
||||
assert.strictEqual(320, withInfo.width);
|
||||
assert.strictEqual(240, withInfo.height);
|
||||
// Then generate with coding disabled
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 240)
|
||||
.jpeg({ optimizeCoding: false })
|
||||
.toBuffer(function (err, withoutOptimiseCoding, withoutInfo) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, withoutOptimiseCoding.length > 0);
|
||||
assert.strictEqual(withoutOptimiseCoding.length, withoutInfo.size);
|
||||
assert.strictEqual('jpeg', withoutInfo.format);
|
||||
assert.strictEqual(320, withoutInfo.width);
|
||||
assert.strictEqual(240, withoutInfo.height);
|
||||
// Verify optimised image is of a smaller size
|
||||
assert.strictEqual(true, withOptimiseCoding.length < withoutOptimiseCoding.length);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Specifying quantisation table provides different JPEG', function (done) {
|
||||
// First generate with default quantisation table
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 240)
|
||||
.jpeg({ optimiseCoding: false })
|
||||
.toBuffer(function (err, withDefaultQuantisationTable, withInfo) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, withDefaultQuantisationTable.length > 0);
|
||||
assert.strictEqual(withDefaultQuantisationTable.length, withInfo.size);
|
||||
assert.strictEqual('jpeg', withInfo.format);
|
||||
assert.strictEqual(320, withInfo.width);
|
||||
assert.strictEqual(240, withInfo.height);
|
||||
// Then generate with different quantisation table
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 240)
|
||||
.jpeg({ optimiseCoding: false, quantisationTable: 3 })
|
||||
.toBuffer(function (err, withQuantTable3, withoutInfo) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, withQuantTable3.length > 0);
|
||||
assert.strictEqual(withQuantTable3.length, withoutInfo.size);
|
||||
assert.strictEqual('jpeg', withoutInfo.format);
|
||||
assert.strictEqual(320, withoutInfo.width);
|
||||
assert.strictEqual(240, withoutInfo.height);
|
||||
|
||||
// Verify image is same (as mozjpeg may not be present) size or less
|
||||
assert.strictEqual(true, withQuantTable3.length <= withDefaultQuantisationTable.length);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Convert SVG to PNG at default 72DPI', function (done) {
|
||||
sharp(fixtures.inputSvg)
|
||||
.resize(1024)
|
||||
@@ -880,6 +967,53 @@ describe('Input/output', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Load multi-page TIFF\'s from file', function (done) {
|
||||
sharp(fixtures.inputTiffMultipage) // defaults to page 0
|
||||
.jpeg()
|
||||
.toBuffer(function (err, defaultData, defaultInfo) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, defaultData.length > 0);
|
||||
assert.strictEqual(defaultData.length, defaultInfo.size);
|
||||
assert.strictEqual('jpeg', defaultInfo.format);
|
||||
|
||||
sharp(fixtures.inputTiffMultipage, { page: 1 }) // 50%-scale copy of page 0
|
||||
.jpeg()
|
||||
.toBuffer(function (err, scaledData, scaledInfo) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, scaledData.length > 0);
|
||||
assert.strictEqual(scaledData.length, scaledInfo.size);
|
||||
assert.strictEqual('jpeg', scaledInfo.format);
|
||||
assert.strictEqual(defaultInfo.width, scaledInfo.width * 2);
|
||||
assert.strictEqual(defaultInfo.height, scaledInfo.height * 2);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Load multi-page TIFF\'s from Buffer', function (done) {
|
||||
const inputTiffBuffer = fs.readFileSync(fixtures.inputTiffMultipage);
|
||||
sharp(inputTiffBuffer) // defaults to page 0
|
||||
.jpeg()
|
||||
.toBuffer(function (err, defaultData, defaultInfo) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, defaultData.length > 0);
|
||||
assert.strictEqual(defaultData.length, defaultInfo.size);
|
||||
assert.strictEqual('jpeg', defaultInfo.format);
|
||||
|
||||
sharp(inputTiffBuffer, { page: 1 }) // 50%-scale copy of page 0
|
||||
.jpeg()
|
||||
.toBuffer(function (err, scaledData, scaledInfo) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, scaledData.length > 0);
|
||||
assert.strictEqual(scaledData.length, scaledInfo.size);
|
||||
assert.strictEqual('jpeg', scaledInfo.format);
|
||||
assert.strictEqual(defaultInfo.width, scaledInfo.width * 2);
|
||||
assert.strictEqual(defaultInfo.height, scaledInfo.height * 2);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Save TIFF to Buffer', function (done) {
|
||||
sharp(fixtures.inputTiff)
|
||||
.resize(320, 240)
|
||||
@@ -1020,6 +1154,22 @@ describe('Input/output', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF ccittfax4 compression shrinks b-w test file', function (done) {
|
||||
const startSize = fs.statSync(fixtures.inputTiff).size;
|
||||
sharp(fixtures.inputTiff)
|
||||
.toColourspace('b-w')
|
||||
.tiff({
|
||||
squash: true,
|
||||
compression: 'ccittfax4'
|
||||
})
|
||||
.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)
|
||||
|
||||
77
test/unit/libvips.js
Normal file
@@ -0,0 +1,77 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
const semver = require('semver');
|
||||
const libvips = require('../../lib/libvips');
|
||||
|
||||
const originalPlatform = process.platform;
|
||||
|
||||
const setPlatform = function (platform) {
|
||||
Object.defineProperty(process, 'platform', { value: platform });
|
||||
};
|
||||
|
||||
const restorePlatform = function () {
|
||||
setPlatform(originalPlatform);
|
||||
};
|
||||
|
||||
describe('libvips binaries', function () {
|
||||
describe('Windows platform', function () {
|
||||
before(function () { setPlatform('win32'); });
|
||||
after(restorePlatform);
|
||||
|
||||
it('pkgConfigPath returns empty string', function () {
|
||||
assert.strictEqual('', libvips.pkgConfigPath());
|
||||
});
|
||||
it('globalLibvipsVersion returns empty string', function () {
|
||||
assert.strictEqual('', libvips.globalLibvipsVersion());
|
||||
});
|
||||
it('globalLibvipsVersion is always false', function () {
|
||||
assert.strictEqual(false, libvips.useGlobalLibvips());
|
||||
});
|
||||
});
|
||||
|
||||
describe('non-Windows platforms', function () {
|
||||
before(function () { setPlatform('linux'); });
|
||||
after(restorePlatform);
|
||||
|
||||
it('pkgConfigPath returns a string', function () {
|
||||
const pkgConfigPath = libvips.pkgConfigPath();
|
||||
assert.strictEqual('string', typeof pkgConfigPath);
|
||||
});
|
||||
it('globalLibvipsVersion returns a string', function () {
|
||||
const globalLibvipsVersion = libvips.globalLibvipsVersion();
|
||||
assert.strictEqual('string', typeof globalLibvipsVersion);
|
||||
});
|
||||
it('globalLibvipsVersion returns a boolean', function () {
|
||||
const useGlobalLibvips = libvips.useGlobalLibvips();
|
||||
assert.strictEqual('boolean', typeof useGlobalLibvips);
|
||||
});
|
||||
});
|
||||
|
||||
describe('platform agnostic', function () {
|
||||
it('minimumLibvipsVersion returns a valid semver', function () {
|
||||
const minimumLibvipsVersion = libvips.minimumLibvipsVersion;
|
||||
assert.strictEqual('string', typeof minimumLibvipsVersion);
|
||||
assert.notStrictEqual(null, semver.valid(minimumLibvipsVersion));
|
||||
});
|
||||
it('hasVendoredLibvips returns a boolean', function () {
|
||||
const hasVendoredLibvips = libvips.hasVendoredLibvips();
|
||||
assert.strictEqual('boolean', typeof hasVendoredLibvips);
|
||||
});
|
||||
it('useGlobalLibvips can be ignored via an env var', function () {
|
||||
process.env.SHARP_IGNORE_GLOBAL_LIBVIPS = 1;
|
||||
|
||||
const useGlobalLibvips = libvips.useGlobalLibvips();
|
||||
assert.strictEqual(false, useGlobalLibvips);
|
||||
|
||||
delete process.env.SHARP_IGNORE_GLOBAL_LIBVIPS;
|
||||
});
|
||||
it('cachePath returns a valid path ending with _libvips', function () {
|
||||
const cachePath = libvips.cachePath();
|
||||
assert.strictEqual('string', typeof cachePath);
|
||||
assert.strictEqual('_libvips', cachePath.substr(-8));
|
||||
assert.strictEqual(true, fs.existsSync(cachePath));
|
||||
});
|
||||
});
|
||||
});
|
||||
79
test/unit/linear.js
Normal file
@@ -0,0 +1,79 @@
|
||||
'use strict';
|
||||
|
||||
const sharp = require('../../');
|
||||
const fixtures = require('../fixtures');
|
||||
|
||||
const assert = require('assert');
|
||||
|
||||
describe('Linear adjustment', function () {
|
||||
const blackPoint = 70;
|
||||
const whitePoint = 203;
|
||||
const a = 255 / (whitePoint - blackPoint);
|
||||
const b = -blackPoint * a;
|
||||
|
||||
it('applies linear levels adjustment w/o alpha ch', function (done) {
|
||||
sharp(fixtures.inputJpgWithLowContrast)
|
||||
.linear(a, b)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
fixtures.assertSimilar(fixtures.expected('low-contrast-linear.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('applies slope level adjustment w/o alpha ch', function (done) {
|
||||
sharp(fixtures.inputJpgWithLowContrast)
|
||||
.linear(a)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
fixtures.assertSimilar(fixtures.expected('low-contrast-slope.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('applies offset level adjustment w/o alpha ch', function (done) {
|
||||
sharp(fixtures.inputJpgWithLowContrast)
|
||||
.linear(null, b)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
fixtures.assertSimilar(fixtures.expected('low-contrast-offset.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('applies linear levels adjustment w alpha ch', function (done) {
|
||||
sharp(fixtures.inputPngOverlayLayer1)
|
||||
.linear(a, b)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
fixtures.assertSimilar(fixtures.expected('alpha-layer-1-fill-linear.png'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('applies slope level adjustment w alpha ch', function (done) {
|
||||
sharp(fixtures.inputPngOverlayLayer1)
|
||||
.linear(a)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
fixtures.assertSimilar(fixtures.expected('alpha-layer-1-fill-slope.png'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('applies offset level adjustment w alpha ch', function (done) {
|
||||
sharp(fixtures.inputPngOverlayLayer1)
|
||||
.linear(null, b)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
fixtures.assertSimilar(fixtures.expected('alpha-layer-1-fill-offset.png'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('Invalid linear arguments', function () {
|
||||
assert.throws(function () {
|
||||
sharp(fixtures.inputPngOverlayLayer1)
|
||||
.linear('foo');
|
||||
});
|
||||
|
||||
assert.throws(function () {
|
||||
sharp(fixtures.inputPngOverlayLayer1)
|
||||
.linear(undefined, { 'bar': 'baz' });
|
||||
});
|
||||
});
|
||||
});
|
||||
72
test/unit/median.js
Normal file
@@ -0,0 +1,72 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('assert');
|
||||
|
||||
const sharp = require('../../');
|
||||
const fixtures = require('../fixtures');
|
||||
|
||||
describe('Median filter', function () {
|
||||
it('1x1 window', function (done) {
|
||||
sharp(fixtures.inputJpgThRandom)
|
||||
.median(1)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(200, info.width);
|
||||
assert.strictEqual(200, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected('median_1.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('3x3 window', function (done) {
|
||||
sharp(fixtures.inputJpgThRandom)
|
||||
.median(3)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(200, info.width);
|
||||
assert.strictEqual(200, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected('median_3.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
it('5x5 window', function (done) {
|
||||
sharp(fixtures.inputJpgThRandom)
|
||||
.median(5)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(200, info.width);
|
||||
assert.strictEqual(200, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected('median_5.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('color image', function (done) {
|
||||
sharp(fixtures.inputJpgRandom)
|
||||
.median(5)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(200, info.width);
|
||||
assert.strictEqual(200, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected('median_color.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('no windows size', function (done) {
|
||||
sharp(fixtures.inputJpgThRandom)
|
||||
.median()
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(200, info.width);
|
||||
assert.strictEqual(200, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected('median_3.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
it('invalid radius', function () {
|
||||
assert.throws(function () {
|
||||
sharp(fixtures.inputJpg).median(0.1);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -130,6 +130,25 @@ describe('Resize dimensions', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('JPEG shrink-on-load with 90 degree rotation, ensure recalculation is correct', function (done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(1920, 1280)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(1920, info.width);
|
||||
assert.strictEqual(1280, info.height);
|
||||
sharp(data)
|
||||
.rotate(90)
|
||||
.resize(533, 800)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(533, info.width);
|
||||
assert.strictEqual(800, info.height);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('TIFF embed known to cause rounding errors', function (done) {
|
||||
sharp(fixtures.inputTiff)
|
||||
.resize(240, 320)
|
||||
|
||||
@@ -24,6 +24,7 @@ describe('Image Stats', function () {
|
||||
if (err) throw err;
|
||||
|
||||
assert.strictEqual(true, stats.isOpaque);
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.319914765248541));
|
||||
|
||||
// red channel
|
||||
assert.strictEqual(0, stats.channels[0]['min']);
|
||||
@@ -82,6 +83,7 @@ describe('Image Stats', function () {
|
||||
if (err) throw err;
|
||||
|
||||
assert.strictEqual(true, stats.isOpaque);
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.entropy, 0.3409031108021736));
|
||||
|
||||
// red channel
|
||||
assert.strictEqual(0, stats.channels[0]['min']);
|
||||
@@ -105,7 +107,9 @@ describe('Image Stats', function () {
|
||||
it('PNG with transparency', function (done) {
|
||||
sharp(fixtures.inputPngWithTransparency).stats(function (err, stats) {
|
||||
if (err) throw err;
|
||||
|
||||
assert.strictEqual(false, stats.isOpaque);
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.entropy, 0.06778064835816622));
|
||||
|
||||
// red channel
|
||||
assert.strictEqual(0, stats.channels[0]['min']);
|
||||
@@ -180,6 +184,7 @@ describe('Image Stats', function () {
|
||||
if (err) throw err;
|
||||
|
||||
assert.strictEqual(false, stats.isOpaque);
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.entropy, 0));
|
||||
|
||||
// alpha channel
|
||||
assert.strictEqual(0, stats.channels[3]['min']);
|
||||
@@ -204,7 +209,9 @@ describe('Image Stats', function () {
|
||||
it('Tiff', function (done) {
|
||||
sharp(fixtures.inputTiff).stats(function (err, stats) {
|
||||
if (err) throw err;
|
||||
|
||||
assert.strictEqual(true, stats.isOpaque);
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.entropy, 0.3851250782608986));
|
||||
|
||||
// red channel
|
||||
assert.strictEqual(0, stats.channels[0]['min']);
|
||||
@@ -231,6 +238,7 @@ describe('Image Stats', function () {
|
||||
if (err) throw err;
|
||||
|
||||
assert.strictEqual(true, stats.isOpaque);
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.51758075132966));
|
||||
|
||||
// red channel
|
||||
assert.strictEqual(0, stats.channels[0]['min']);
|
||||
@@ -289,6 +297,7 @@ describe('Image Stats', function () {
|
||||
if (err) throw err;
|
||||
|
||||
assert.strictEqual(true, stats.isOpaque);
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.entropy, 6.087309412541799));
|
||||
|
||||
// red channel
|
||||
assert.strictEqual(35, stats.channels[0]['min']);
|
||||
@@ -345,7 +354,9 @@ describe('Image Stats', function () {
|
||||
it('Grayscale GIF with alpha', function (done) {
|
||||
sharp(fixtures.inputGifGreyPlusAlpha).stats(function (err, stats) {
|
||||
if (err) throw err;
|
||||
|
||||
assert.strictEqual(false, stats.isOpaque);
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.entropy, 1));
|
||||
|
||||
// gray channel
|
||||
assert.strictEqual(0, stats.channels[0]['min']);
|
||||
@@ -387,7 +398,9 @@ describe('Image Stats', function () {
|
||||
const readable = fs.createReadStream(fixtures.inputJpg);
|
||||
const pipeline = sharp().stats(function (err, stats) {
|
||||
if (err) throw err;
|
||||
|
||||
assert.strictEqual(true, stats.isOpaque);
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.319914765248541));
|
||||
|
||||
// red channel
|
||||
assert.strictEqual(0, stats.channels[0]['min']);
|
||||
@@ -449,6 +462,7 @@ describe('Image Stats', function () {
|
||||
|
||||
return pipeline.stats().then(function (stats) {
|
||||
assert.strictEqual(true, stats.isOpaque);
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.319914765248541));
|
||||
|
||||
// red channel
|
||||
assert.strictEqual(0, stats.channels[0]['min']);
|
||||
@@ -505,6 +519,7 @@ describe('Image Stats', function () {
|
||||
it('File in, Promise out', function () {
|
||||
return sharp(fixtures.inputJpg).stats().then(function (stats) {
|
||||
assert.strictEqual(true, stats.isOpaque);
|
||||
assert.strictEqual(true, isInAcceptableRange(stats.entropy, 7.319914765248541));
|
||||
|
||||
// red channel
|
||||
assert.strictEqual(0, stats.channels[0]['min']);
|
||||
|
||||
@@ -6,7 +6,7 @@ const assert = require('assert');
|
||||
|
||||
const eachLimit = require('async/eachLimit');
|
||||
const rimraf = require('rimraf');
|
||||
const unzip = require('unzip');
|
||||
const DecompressZip = require('decompress-zip');
|
||||
|
||||
const sharp = require('../../');
|
||||
const fixtures = require('../fixtures');
|
||||
@@ -46,6 +46,51 @@ const assertDeepZoomTiles = function (directory, expectedSize, expectedLevels, d
|
||||
}, done);
|
||||
};
|
||||
|
||||
const assertZoomifyTiles = function (directory, expectedTileSize, expectedLevels, done) {
|
||||
fs.stat(path.join(directory, 'ImageProperties.xml'), function (err, stat) {
|
||||
if (err) throw err;
|
||||
assert.ok(stat.isFile());
|
||||
assert.ok(stat.size > 0);
|
||||
|
||||
let maxTileLevel = -1;
|
||||
fs.readdirSync(path.join(directory, 'TileGroup0')).forEach(function (tile) {
|
||||
// Verify tile file name
|
||||
assert.ok(/^[0-9]+-[0-9]+-[0-9]+\.jpg$/.test(tile));
|
||||
let level = parseInt(tile.split('-')[0]);
|
||||
maxTileLevel = Math.max(maxTileLevel, level);
|
||||
});
|
||||
|
||||
assert.strictEqual(maxTileLevel + 1, expectedLevels); // add one to account for zero level tile
|
||||
|
||||
done();
|
||||
});
|
||||
};
|
||||
|
||||
const assertGoogleTiles = function (directory, expectedTileSize, expectedLevels, done) {
|
||||
const levels = fs.readdirSync(directory);
|
||||
assert.strictEqual(expectedLevels, levels.length - 1); // subtract one to account for default blank tile
|
||||
|
||||
fs.stat(path.join(directory, 'blank.png'), function (err, stat) {
|
||||
if (err) throw err;
|
||||
assert.ok(stat.isFile());
|
||||
assert.ok(stat.size > 0);
|
||||
|
||||
// Basic check to confirm lowest and highest level tiles exist
|
||||
fs.stat(path.join(directory, '0', '0', '0.jpg'), function (err, stat) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, stat.isFile());
|
||||
assert.strictEqual(true, stat.size > 0);
|
||||
|
||||
fs.stat(path.join(directory, (expectedLevels - 1).toString(), '0', '0.jpg'), function (err, stat) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, stat.isFile());
|
||||
assert.strictEqual(true, stat.size > 0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
describe('Tile', function () {
|
||||
it('Valid size values pass', function () {
|
||||
[1, 8192].forEach(function (size) {
|
||||
@@ -144,15 +189,60 @@ describe('Tile', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Valid depths pass', function () {
|
||||
['onepixel', 'onetile', 'one'].forEach(function (depth) {
|
||||
assert.doesNotThrow(function (depth) {
|
||||
sharp().tile({
|
||||
depth: depth
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Invalid depths fail', function () {
|
||||
['depth', 1].forEach(function (depth) {
|
||||
assert.throws(function () {
|
||||
sharp().tile({
|
||||
depth: depth
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Prevent larger overlap than default size', function () {
|
||||
assert.throws(function () {
|
||||
sharp().tile({overlap: 257});
|
||||
sharp().tile({
|
||||
overlap: 257
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Prevent larger overlap than provided size', function () {
|
||||
assert.throws(function () {
|
||||
sharp().tile({size: 512, overlap: 513});
|
||||
sharp().tile({
|
||||
size: 512,
|
||||
overlap: 513
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Valid rotation angle values pass', function () {
|
||||
[90, 270, -90].forEach(function (angle) {
|
||||
assert.doesNotThrow(function () {
|
||||
sharp().tile({
|
||||
angle: angle
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Invalid rotation angle values fail', function () {
|
||||
['zoinks', 1.1, -1, 27].forEach(function (angle) {
|
||||
assert.throws(function () {
|
||||
sharp().tile({
|
||||
angle: angle
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -192,6 +282,88 @@ describe('Tile', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Deep Zoom layout with custom size+angle', function (done) {
|
||||
const directory = fixtures.path('output.512_90.dzi_files');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.tile({
|
||||
size: 512,
|
||||
angle: 90
|
||||
})
|
||||
.toFile(fixtures.path('output.512_90.dzi'), function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('dz', info.format);
|
||||
assert.strictEqual(2725, info.width);
|
||||
assert.strictEqual(2225, info.height);
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual('undefined', typeof info.size);
|
||||
assertDeepZoomTiles(directory, 512, 13, done);
|
||||
// Verifies tiles in 10th level are rotated
|
||||
let tile = path.join(directory, '10', '0_1.jpeg');
|
||||
// verify that the width and height correspond to the rotated image
|
||||
// expected are w=512 and h=170 for the 0_1.jpeg.
|
||||
// if a 0 angle is supplied to the .tile function
|
||||
// the expected values are w=170 and h=512 for the 1_0.jpeg
|
||||
sharp(tile).metadata(function (err, metadata) {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
assert.strictEqual(true, metadata.width === 512);
|
||||
assert.strictEqual(true, metadata.height === 170);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Deep Zoom layout with depth of one', function (done) {
|
||||
const directory = fixtures.path('output.512_depth_one.dzi_files');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.tile({
|
||||
size: 512,
|
||||
depth: 'one'
|
||||
})
|
||||
.toFile(fixtures.path('output.512_depth_one.dzi'), function (err, info) {
|
||||
if (err) throw err;
|
||||
// Verify only one depth generated
|
||||
assertDeepZoomTiles(directory, 512, 1, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Deep Zoom layout with depth of onepixel', function (done) {
|
||||
const directory = fixtures.path('output.512_depth_onepixel.dzi_files');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.tile({
|
||||
size: 512,
|
||||
depth: 'onepixel'
|
||||
})
|
||||
.toFile(fixtures.path('output.512_depth_onepixel.dzi'), function (err, info) {
|
||||
if (err) throw err;
|
||||
// Verify only one depth generated
|
||||
assertDeepZoomTiles(directory, 512, 13, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Deep Zoom layout with depth of onetile', function (done) {
|
||||
const directory = fixtures.path('output.256_depth_onetile.dzi_files');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.tile({
|
||||
size: 256,
|
||||
depth: 'onetile'
|
||||
})
|
||||
.toFile(fixtures.path('output.256_depth_onetile.dzi'), function (err, info) {
|
||||
if (err) throw err;
|
||||
// Verify only one depth generated
|
||||
assertDeepZoomTiles(directory, 256, 5, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Zoomify layout', function (done) {
|
||||
const directory = fixtures.path('output.zoomify.dzi');
|
||||
rimraf(directory, function () {
|
||||
@@ -216,6 +388,69 @@ describe('Tile', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Zoomify layout with depth one', function (done) {
|
||||
const directory = fixtures.path('output.zoomify.depth_one.dzi');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.tile({
|
||||
size: 256,
|
||||
layout: 'zoomify',
|
||||
depth: 'one'
|
||||
})
|
||||
.toFile(directory, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('dz', info.format);
|
||||
assert.strictEqual(2725, info.width);
|
||||
assert.strictEqual(2225, info.height);
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual('number', typeof info.size);
|
||||
assertZoomifyTiles(directory, 256, 1, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Zoomify layout with depth onetile', function (done) {
|
||||
const directory = fixtures.path('output.zoomify.depth_onetile.dzi');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.tile({
|
||||
size: 256,
|
||||
layout: 'zoomify',
|
||||
depth: 'onetile'
|
||||
})
|
||||
.toFile(directory, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('dz', info.format);
|
||||
assert.strictEqual(2725, info.width);
|
||||
assert.strictEqual(2225, info.height);
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual('number', typeof info.size);
|
||||
assertZoomifyTiles(directory, 256, 5, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Zoomify layout with depth onepixel', function (done) {
|
||||
const directory = fixtures.path('output.zoomify.depth_onepixel.dzi');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.tile({
|
||||
size: 256,
|
||||
layout: 'zoomify',
|
||||
depth: 'onepixel'
|
||||
})
|
||||
.toFile(directory, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('dz', info.format);
|
||||
assert.strictEqual(2725, info.width);
|
||||
assert.strictEqual(2225, info.height);
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual('number', typeof info.size);
|
||||
assertZoomifyTiles(directory, 256, 13, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Google layout', function (done) {
|
||||
const directory = fixtures.path('output.google.dzi');
|
||||
rimraf(directory, function () {
|
||||
@@ -244,7 +479,9 @@ describe('Tile', function () {
|
||||
const directory = fixtures.path('output.jpg.google.dzi');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.jpeg({ quality: 1 })
|
||||
.jpeg({
|
||||
quality: 1
|
||||
})
|
||||
.tile({
|
||||
layout: 'google'
|
||||
})
|
||||
@@ -279,7 +516,9 @@ describe('Tile', function () {
|
||||
const directory = fixtures.path('output.png.google.dzi');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.png({ compressionLevel: 1 })
|
||||
.png({
|
||||
compressionLevel: 1
|
||||
})
|
||||
.tile({
|
||||
layout: 'google'
|
||||
})
|
||||
@@ -314,7 +553,9 @@ describe('Tile', function () {
|
||||
const directory = fixtures.path('output.webp.google.dzi');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.webp({ quality: 1 })
|
||||
.webp({
|
||||
quality: 1
|
||||
})
|
||||
.tile({
|
||||
layout: 'google'
|
||||
})
|
||||
@@ -345,6 +586,72 @@ describe('Tile', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('Google layout with depth one', function (done) {
|
||||
const directory = fixtures.path('output.google_depth_one.dzi');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.tile({
|
||||
layout: 'google',
|
||||
depth: 'one',
|
||||
size: 256
|
||||
})
|
||||
.toFile(directory, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('dz', info.format);
|
||||
assert.strictEqual(2725, info.width);
|
||||
assert.strictEqual(2225, info.height);
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual('number', typeof info.size);
|
||||
|
||||
assertGoogleTiles(directory, 256, 1, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Google layout with depth onepixel', function (done) {
|
||||
const directory = fixtures.path('output.google_depth_onepixel.dzi');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.tile({
|
||||
layout: 'google',
|
||||
depth: 'onepixel',
|
||||
size: 256
|
||||
})
|
||||
.toFile(directory, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('dz', info.format);
|
||||
assert.strictEqual(2725, info.width);
|
||||
assert.strictEqual(2225, info.height);
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual('number', typeof info.size);
|
||||
|
||||
assertGoogleTiles(directory, 256, 13, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Google layout with depth onetile', function (done) {
|
||||
const directory = fixtures.path('output.google_depth_onetile.dzi');
|
||||
rimraf(directory, function () {
|
||||
sharp(fixtures.inputJpg)
|
||||
.tile({
|
||||
layout: 'google',
|
||||
depth: 'onetile',
|
||||
size: 256
|
||||
})
|
||||
.toFile(directory, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('dz', info.format);
|
||||
assert.strictEqual(2725, info.width);
|
||||
assert.strictEqual(2225, info.height);
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual('number', typeof info.size);
|
||||
|
||||
assertGoogleTiles(directory, 256, 5, done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Write to ZIP container using file extension', function (done) {
|
||||
const container = fixtures.path('output.dz.container.zip');
|
||||
const extractTo = fixtures.path('output.dz.container');
|
||||
@@ -362,12 +669,14 @@ describe('Tile', function () {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, stat.isFile());
|
||||
assert.strictEqual(true, stat.size > 0);
|
||||
fs.createReadStream(container)
|
||||
.pipe(unzip.Extract({path: path.dirname(extractTo)}))
|
||||
.on('error', function (err) { throw err; })
|
||||
.on('close', function () {
|
||||
new DecompressZip(container)
|
||||
.on('extract', function () {
|
||||
assertDeepZoomTiles(directory, 256, 13, done);
|
||||
});
|
||||
})
|
||||
.on('error', function (err) {
|
||||
throw err;
|
||||
})
|
||||
.extract({ path: path.dirname(extractTo) });
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -394,12 +703,14 @@ describe('Tile', function () {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, stat.isFile());
|
||||
assert.strictEqual(true, stat.size > 0);
|
||||
fs.createReadStream(container)
|
||||
.pipe(unzip.Extract({path: path.dirname(extractTo)}))
|
||||
.on('error', function (err) { throw err; })
|
||||
.on('close', function () {
|
||||
new DecompressZip(container)
|
||||
.on('extract', function () {
|
||||
assertDeepZoomTiles(directory, 256, 13, done);
|
||||
});
|
||||
})
|
||||
.on('error', function (err) {
|
||||
throw err;
|
||||
})
|
||||
.extract({ path: path.dirname(extractTo) });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
102
test/unit/tint.js
Normal file
@@ -0,0 +1,102 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('assert');
|
||||
|
||||
const sharp = require('../../');
|
||||
const fixtures = require('../fixtures');
|
||||
|
||||
describe('Tint', function () {
|
||||
it('tints rgb image red', function (done) {
|
||||
const output = fixtures.path('output.tint-red.jpg');
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 240)
|
||||
.tint('#FF0000')
|
||||
.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, info.size > 0);
|
||||
fixtures.assertMaxColourDistance(output, fixtures.expected('tint-red.jpg'), 10);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('tints rgb image green', function (done) {
|
||||
const output = fixtures.path('output.tint-green.jpg');
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 240)
|
||||
.tint('#00FF00')
|
||||
.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, info.size > 0);
|
||||
fixtures.assertMaxColourDistance(output, fixtures.expected('tint-green.jpg'), 10);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('tints rgb image blue', function (done) {
|
||||
const output = fixtures.path('output.tint-blue.jpg');
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 240)
|
||||
.tint('#0000FF')
|
||||
.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, info.size > 0);
|
||||
fixtures.assertMaxColourDistance(output, fixtures.expected('tint-blue.jpg'), 10);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('tints rgb image with sepia tone', function (done) {
|
||||
const output = fixtures.path('output.tint-sepia.jpg');
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 240)
|
||||
.tint('#704214')
|
||||
.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
fixtures.assertMaxColourDistance(output, fixtures.expected('tint-sepia.jpg'), 10);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('tints rgb image with sepia tone with rgb colour', function (done) {
|
||||
const output = fixtures.path('output.tint-sepia.jpg');
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(320, 240)
|
||||
.tint([112, 66, 20])
|
||||
.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
fixtures.assertMaxColourDistance(output, fixtures.expected('tint-sepia.jpg'), 10);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('tints rgb image with alpha channel', function (done) {
|
||||
const output = fixtures.path('output.tint-alpha.png');
|
||||
sharp(fixtures.inputPngRGBWithAlpha)
|
||||
.resize(320, 240)
|
||||
.tint('#704214')
|
||||
.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
fixtures.assertMaxColourDistance(output, fixtures.expected('tint-alpha.png'), 10);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('tints cmyk image red', function (done) {
|
||||
const output = fixtures.path('output.tint-cmyk.jpg');
|
||||
sharp(fixtures.inputJpgWithCmykProfile)
|
||||
.resize(320, 240)
|
||||
.tint('#FF0000')
|
||||
.toFile(output, function (err, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(true, info.size > 0);
|
||||
fixtures.assertMaxColourDistance(output, fixtures.expected('tint-cmyk.jpg'), 10);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||