Compare commits

...

71 Commits

Author SHA1 Message Date
Lovell Fuller
a2d3fa729f Release v0.28.2 2021-05-10 17:59:50 +01:00
Lovell Fuller
cb6811bc47 CI: FreeBSD skip notifications 2021-05-10 17:44:15 +01:00
Lovell Fuller
53c6e80869 Docs: refresh index 2021-05-10 16:03:40 +01:00
Lovell Fuller
e71dca586c Bump devDeps 2021-05-10 16:03:08 +01:00
Lovell Fuller
b3cd48db5f Docs: add section about cross-platform installation 2021-05-10 14:41:28 +01:00
Sebastian
476448b9d4 Install: allow cross-libc via sharp-install-force flag (#2692) 2021-05-10 09:55:20 +01:00
Lovell Fuller
070534df5b Docs: changelog for #2685 2021-05-03 19:48:15 +01:00
Michael Nutt
9a1e8ed574 Add premultiplied boolean flag for raw pixel data input (#2685) 2021-05-03 19:30:37 +01:00
Lovell Fuller
309918a878 Move lint-related tasks to dedicated script entry 2021-05-03 10:04:19 +01:00
Lovell Fuller
cac83b94c1 Bump deps and docs refresh 2021-05-03 10:01:43 +01:00
Lovell Fuller
9c06df08a1 Docs: changelog entry for #2687 2021-05-03 10:01:17 +01:00
Jacob
52e4543d31 Detect empty input and throw a helpful error (#2687) 2021-05-03 09:29:51 +01:00
Lovell Fuller
a688468378 CI: replace Node.js 15 with 16 2021-05-01 16:24:47 +01:00
Lovell Fuller
e1760d64fb Tests: updates so latest libvips master branch passes 2021-05-01 15:25:57 +01:00
Lovell Fuller
84d4e3cf8f Require specific semver functions, aids tree-shaking 2021-04-30 20:42:46 +01:00
Raj Rajhans
f8a76372ad Docs: rewrite sentence to avoid grammatical ambiguity (#2668) 2021-04-21 14:10:24 +01:00
Lovell Fuller
4237f5520f Allow withMetadata to set density #967 2021-04-17 13:46:54 +01:00
Lovell Fuller
8c0c01c702 Docs: changelog entry for #2664 2021-04-17 09:11:48 +01:00
msalettes
9c100830e0 Allow escaped proxy credentials (#2664) 2021-04-17 08:49:07 +01:00
Lovell Fuller
ed5d753b89 Skip shrink-on-load where one dimension <4px #2653 2021-04-07 21:26:16 +01:00
Timo Hausmann
d1ca756bd8 Docs: correct flatten example to use object instead of string (#2654) 2021-04-06 17:21:14 +01:00
Lovell Fuller
16cf9f0ef2 Release v0.28.1 2021-04-05 12:28:16 +01:00
Lovell Fuller
133f69d66c Bump prebuild-install (for sharp_local_prebuilds) 2021-04-05 12:26:28 +01:00
Lovell Fuller
bc60daff9e Allow EXIF metadata to be set/update #650 2021-04-05 11:39:53 +01:00
Lovell Fuller
43a085d1ae Add support for OME-TIFF subIFDs #2557 2021-04-02 08:04:21 +01:00
Lovell Fuller
8c33d0aa56 Allow ensureAlpha to set alpha transparency level #2634 2021-04-01 21:14:06 +01:00
Lovell Fuller
fe0767df13 Install: log errors with more obvious prefix 2021-04-01 16:20:58 +01:00
Lovell Fuller
08a25a0c8f Docs: add animated WebP example #2648 2021-04-01 16:04:46 +01:00
Lovell Fuller
cd410080bd Docs: remove reference to specific Lambda runtime 2021-03-29 20:16:07 +01:00
Lovell Fuller
7555378e3b Release v0.28.0 2021-03-29 14:10:34 +01:00
Lovell Fuller
80c95ee66a Docs: libvips tarballs are a bit smaller now 2021-03-29 12:16:48 +01:00
Lovell Fuller
31563b210d Ensure GIF input will work with future libvips v8.11.0 2021-03-29 12:16:10 +01:00
Lovell Fuller
861cd93324 Pre-release v0.28.0-beta1 2021-03-27 07:11:34 +00:00
Lovell Fuller
abb344bb1a Upgrade to libvips v8.10.6 2021-03-26 21:57:12 +00:00
Lovell Fuller
6147491d9e Extend: default missing edge props to zero #2578 2021-03-25 16:34:02 +00:00
Lovell Fuller
f1f18fbb4a Docs: clarify that flatten removes alpha channel #2601 2021-03-25 14:38:55 +00:00
Lovell Fuller
9fc611f257 Docs: changelog entries for #2594 #2608 2021-03-22 20:30:46 +00:00
SHG42
34a2e14a14 Fix erroneous top/left clipping in composite #2571
Fixes bug where certain input values for top/left parameters
in composite can conflict with clipping logic, resulting in
inaccurate alignment in output.
2021-03-22 18:27:49 +00:00
Lovell Fuller
83fe65b9e9 Docs: include more relevant content in search index 2021-03-21 20:59:05 +00:00
Lovell Fuller
ec26c8aa49 Docs: ensure toBuffer pixel example works #2624 2021-03-21 20:54:09 +00:00
Lovell Fuller
da43a3055f Docs: correct typo in description of threshold operation 2021-03-21 20:51:30 +00:00
Lovell Fuller
a38126c82f Ensure composite replicates correct tiles with centre gravity #2626 2021-03-20 13:24:04 +00:00
Lovell Fuller
cb592ce588 Tests: add case for SVG with truncated embedded PNG 2021-03-18 19:34:56 +00:00
Lovell Fuller
d69c58a6da Docs: add section about Linux memory allocators 2021-03-18 19:34:07 +00:00
Lovell Fuller
bdb1986e08 Tests: run in parallel again 2021-03-17 23:25:34 +00:00
Lovell Fuller
55356c78a8 Docs: refresh markdown 2021-03-15 20:24:53 +00:00
Lovell Fuller
a0f55252b1 Tests: a few more speed improvements 2021-03-15 20:24:13 +00:00
Lovell Fuller
013f5cffa9 Tests: refactor modulate suite, ~20x faster 2021-03-15 18:20:06 +00:00
Lovell Fuller
d5d008f568 Docs: reorder readme sections 2021-03-15 13:07:16 +00:00
Lovell Fuller
3b02134cdc Tests: update latest benchmark test results 2021-03-14 21:10:26 +00:00
Lovell Fuller
a57d7b51b1 Tests: match concurrency with CPU count 2021-03-14 19:51:45 +00:00
Lovell Fuller
1a3c38d35f Pre-release v0.28.0-alpha1 2021-03-14 11:50:33 +00:00
Lovell Fuller
00aece0538 Ensure id attr can be set for IIIF tile output #2612 2021-03-14 11:19:53 +00:00
Lovell Fuller
5a9cc835b3 Reduce concurrency when using glibc-based Linux
to help prevent memory fragmentation
2021-03-14 11:19:53 +00:00
Lovell Fuller
58526cc849 Upgrade to libvips v8.10.6-alpha3 2021-03-14 11:19:53 +00:00
Lovell Fuller
955b5f43a5 Tests: small speed up to a couple of tile-related tests 2021-03-14 11:19:53 +00:00
Lovell Fuller
447aec3fde Tests: update leak suppressions 2021-03-14 11:19:53 +00:00
Lovell Fuller
473260a836 Docs: update with install-time improvements 2021-03-14 11:19:53 +00:00
Lovell Fuller
4d2784c10c Prebuilt libvips v8.10.6 binaries work with musl 1.1.x and 1.2.x 2021-03-14 11:19:53 +00:00
Lovell Fuller
d9af897595 Tests: ensure AVIF order is read, write, read+write 2021-03-14 11:19:53 +00:00
Lovell Fuller
23a48be315 Upgrade to libvips v8.10.6-alpha2
- Prebuilt Linux libvips binaries now use 'new' C++11 ABI
2021-03-14 11:19:53 +00:00
Lovell Fuller
ce8f48e5d1 CI: Add linuxmusl-arm64v8 environment 2021-03-14 11:19:53 +00:00
Kleis Auke Wolthuizen
6aaf839662 Use a single shared library 2021-03-14 11:19:53 +00:00
Lovell Fuller
984a9e653e Upgrade to libvips 8.10.6-alpha1
- Prebuilt binaries now include mozjpeg and libimagequant (BSD 2-Clause)
- Prebuilt binaries limit AVIF support to the most common 8-bit depth
- Add `mozjpeg` option to `jpeg` method, sets mozjpeg defaults
- Reduce the default PNG `compressionLevel` to the more commonly used 6
2021-03-14 11:19:53 +00:00
Lovell Fuller
8dffa28b4d Remove npmlog as a direct dependency
It remains a transitive dependency via prebuild-install
2021-03-14 11:19:53 +00:00
Lovell Fuller
b05a4bdadd Use same version of simple-get as prebuild-install
to prevent two different versions being installed
2021-03-14 11:19:53 +00:00
Lovell Fuller
36087fe518 Remove array-flatten dependency 2021-03-14 11:19:53 +00:00
Lovell Fuller
5eed87ec4d Install: skip header files when using prebuilds 2021-03-14 11:19:53 +00:00
Tobias Nießen
af66a73225 Tests: fix unit test description (#2619) 2021-03-13 16:18:40 +00:00
Alex Bradley
dcf913c17e Install: fail on incomplete download and clean up tempfile (#2608)
- Fail when the connection closes before the response is complete
- Create tempfile only when needed, and clean it up on failure
2021-03-05 15:21:34 +00:00
Lovell Fuller
68ccba8f74 Docs: refresh search index 2021-02-22 21:17:31 +00:00
94 changed files with 1776 additions and 804 deletions

View File

@@ -5,6 +5,7 @@ task:
name: FreeBSD 13.0 name: FreeBSD 13.0
env: env:
IGNORE_OSVERSION: yes IGNORE_OSVERSION: yes
skip_notifications: true
prerequisites_script: prerequisites_script:
- pkg update -f - pkg update -f
- pkg upgrade -y - pkg upgrade -y

View File

@@ -22,7 +22,7 @@ jobs:
nodejs_version: 14 nodejs_version: 14
- os: ubuntu-20.04 - os: ubuntu-20.04
container: centos:7 container: centos:7
nodejs_version: 15 nodejs_version: 16
- os: ubuntu-20.04 - os: ubuntu-20.04
container: node:10-alpine3.11 container: node:10-alpine3.11
prebuild: true prebuild: true
@@ -31,7 +31,9 @@ jobs:
- os: ubuntu-20.04 - os: ubuntu-20.04
container: node:14-alpine3.11 container: node:14-alpine3.11
- os: ubuntu-20.04 - os: ubuntu-20.04
container: node:15-alpine3.11 container: node:14-alpine3.13
- os: ubuntu-20.04
container: node:16-alpine3.11
- os: macos-10.15 - os: macos-10.15
nodejs_version: 10 nodejs_version: 10
prebuild: true prebuild: true
@@ -40,7 +42,7 @@ jobs:
- os: macos-10.15 - os: macos-10.15
nodejs_version: 14 nodejs_version: 14
- os: macos-10.15 - os: macos-10.15
nodejs_version: 15 nodejs_version: 16
- os: windows-2019 - os: windows-2019
nodejs_version: 10 nodejs_version: 10
prebuild: true prebuild: true
@@ -49,7 +51,7 @@ jobs:
- os: windows-2019 - os: windows-2019
nodejs_version: 14 nodejs_version: 14
- os: windows-2019 - os: windows-2019
nodejs_version: 15 nodejs_version: 16
steps: steps:
- name: Dependencies (Linux glibc) - name: Dependencies (Linux glibc)
if: contains(matrix.container, 'centos') if: contains(matrix.container, 'centos')

View File

@@ -43,7 +43,7 @@ jobs:
install: sudo docker exec sharp sh -c "npm install --build-from-source --unsafe-perm" install: sudo docker exec sharp sh -c "npm install --build-from-source --unsafe-perm"
script: sudo docker exec sharp sh -c "npm test" script: sudo docker exec sharp sh -c "npm test"
- name: "Linux ARM64v8 (Debian 11, glibc 2.29) - Node.js 15" - name: "Linux ARM64v8 (Debian 11, glibc 2.29) - Node.js 16"
arch: arm64 arch: arm64
os: linux os: linux
dist: bionic dist: bionic
@@ -53,10 +53,56 @@ jobs:
- sudo docker run -dit --name sharp --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp arm64v8/debian:bullseye - sudo docker run -dit --name sharp --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp arm64v8/debian:bullseye
- sudo docker exec sharp sh -c "apt-get update && apt-get install -y build-essential git python3 curl" - sudo docker exec sharp sh -c "apt-get update && apt-get install -y build-essential git python3 curl"
- sudo docker exec sharp sh -c "curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -" - sudo docker exec sharp sh -c "curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add -"
- sudo docker exec sharp sh -c "echo 'deb https://deb.nodesource.com/node_15.x sid main' >/etc/apt/sources.list.d/nodesource.list" - sudo docker exec sharp sh -c "echo 'deb https://deb.nodesource.com/node_16.x sid main' >/etc/apt/sources.list.d/nodesource.list"
- sudo docker exec sharp sh -c "apt-get update && apt-get install -y nodejs" - sudo docker exec sharp sh -c "apt-get update && apt-get install -y nodejs"
install: sudo docker exec sharp sh -c "npm install --build-from-source --unsafe-perm" install: sudo docker exec sharp sh -c "npm install --build-from-source --unsafe-perm"
script: sudo docker exec sharp sh -c "npm test" script: sudo docker exec sharp sh -c "npm test"
- name: "Linux ARM64v8 (Alpine 3.11, musl 1.1.24) - Node.js 10"
arch: arm64
os: linux
dist: focal
language: shell
before_install:
- sudo docker run -dit --name sharp --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp node:10-alpine3.11
- sudo docker exec sharp sh -c "apk add build-base git python3 --update-cache"
install: sudo docker exec sharp sh -c "npm install --build-from-source --unsafe-perm"
script: sudo docker exec sharp sh -c "npm test"
after_success: "[[ -n $TRAVIS_TAG ]] && sudo docker exec --env prebuild_upload sharp sh -c \"npx prebuild --runtime napi --target 3\""
- name: "Linux ARM64v8 (Alpine 3.11, musl 1.1.24) - Node.js 12"
arch: arm64
os: linux
dist: focal
language: shell
before_install:
- sudo docker run -dit --name sharp --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp node:12-alpine3.11
- sudo docker exec sharp sh -c "apk add build-base git python3 --update-cache"
install: sudo docker exec sharp sh -c "npm install --build-from-source --unsafe-perm"
script: sudo docker exec sharp sh -c "npm test"
- name: "Linux ARM64v8 (Alpine 3.11, musl 1.1.24) - Node.js 14"
arch: arm64
os: linux
dist: focal
language: shell
before_install:
- sudo docker run -dit --name sharp --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp node:14-alpine3.11
- sudo docker exec sharp sh -c "apk add build-base git python3 --update-cache"
install: sudo docker exec sharp sh -c "npm install --build-from-source --unsafe-perm"
script: sudo docker exec sharp sh -c "npm test"
- name: "Linux ARM64v8 (Alpine 3.11, musl 1.1.24) - Node.js 16"
arch: arm64
os: linux
dist: focal
language: shell
before_install:
- sudo chown 0.0 ${PWD}
- sudo docker run -dit --name sharp --volume "${PWD}:/mnt/sharp" --workdir /mnt/sharp node:16-alpine3.11
- sudo docker exec sharp sh -c "apk add build-base git python3 --update-cache"
install: sudo docker exec sharp sh -c "npm install --build-from-source --unsafe-perm"
script: sudo docker exec sharp sh -c "npm test"
cache: cache:
npm: false npm: false

View File

@@ -19,6 +19,14 @@ rotation, extraction, compositing and gamma correction are available.
Most modern macOS, Windows and Linux systems running Node.js v10+ Most modern macOS, Windows and Linux systems running Node.js v10+
do not require any additional install or runtime dependencies. do not require any additional install or runtime dependencies.
## Documentation
Visit [sharp.pixelplumbing.com](https://sharp.pixelplumbing.com/) for complete
[installation instructions](https://sharp.pixelplumbing.com/install),
[API documentation](https://sharp.pixelplumbing.com/api-constructor),
[benchmark tests](https://sharp.pixelplumbing.com/performance) and
[changelog](https://sharp.pixelplumbing.com/changelog).
## Examples ## Examples
```sh ```sh
@@ -43,6 +51,7 @@ sharp(inputBuffer)
sharp('input.jpg') sharp('input.jpg')
.rotate() .rotate()
.resize(200) .resize(200)
.jpeg({ mozjpeg: true })
.toBuffer() .toBuffer()
.then( data => { ... }) .then( data => { ... })
.catch( err => { ... }); .catch( err => { ... });
@@ -84,23 +93,15 @@ readableStream
.pipe(writableStream); .pipe(writableStream);
``` ```
[![Test Coverage](https://coveralls.io/repos/lovell/sharp/badge.svg?branch=master)](https://coveralls.io/r/lovell/sharp?branch=master) ## Contributing
[![N-API v3](https://img.shields.io/badge/N--API-v3-green.svg)](https://nodejs.org/dist/latest/docs/api/n-api.html#n_api_n_api_version_matrix)
### Documentation
Visit [sharp.pixelplumbing.com](https://sharp.pixelplumbing.com/) for complete
[installation instructions](https://sharp.pixelplumbing.com/install),
[API documentation](https://sharp.pixelplumbing.com/api-constructor),
[benchmark tests](https://sharp.pixelplumbing.com/performance) and
[changelog](https://sharp.pixelplumbing.com/changelog).
### Contributing
A [guide for contributors](https://github.com/lovell/sharp/blob/master/.github/CONTRIBUTING.md) A [guide for contributors](https://github.com/lovell/sharp/blob/master/.github/CONTRIBUTING.md)
covers reporting bugs, requesting features and submitting code changes. covers reporting bugs, requesting features and submitting code changes.
### Licensing [![Test Coverage](https://coveralls.io/repos/lovell/sharp/badge.svg?branch=master)](https://coveralls.io/r/lovell/sharp?branch=master)
[![N-API v3](https://img.shields.io/badge/N--API-v3-green.svg)](https://nodejs.org/dist/latest/docs/api/n-api.html#n_api_n_api_version_matrix)
## Licensing
Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Lovell Fuller and contributors. Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Lovell Fuller and contributors.

View File

@@ -8,7 +8,7 @@ environment:
prebuild: true prebuild: true
- nodejs_version: "12" - nodejs_version: "12"
- nodejs_version: "14" - nodejs_version: "14"
- nodejs_version: "15" - nodejs_version: "16"
install: install:
- ps: Update-NodeJsInstallation (Get-NodeJsLatestBuild $env:nodejs_version) - ps: Update-NodeJsInstallation (Get-NodeJsLatestBuild $env:nodejs_version)
- npm install --build-from-source - npm install --build-from-source

View File

@@ -140,8 +140,7 @@
'link_settings': { 'link_settings': {
'library_dirs': ['<(sharp_vendor_dir)/lib'], 'library_dirs': ['<(sharp_vendor_dir)/lib'],
'libraries': [ 'libraries': [
'libvips-cpp.42.dylib', 'libvips-cpp.42.dylib'
'libvips.42.dylib'
] ]
}, },
'xcode_settings': { 'xcode_settings': {
@@ -153,13 +152,12 @@
}], }],
['OS == "linux"', { ['OS == "linux"', {
'defines': [ 'defines': [
'_GLIBCXX_USE_CXX11_ABI=0' '_GLIBCXX_USE_CXX11_ABI=1'
], ],
'link_settings': { 'link_settings': {
'library_dirs': ['<(sharp_vendor_dir)/lib'], 'library_dirs': ['<(sharp_vendor_dir)/lib'],
'libraries': [ 'libraries': [
'-l:libvips-cpp.so.42', '-l:libvips-cpp.so.42'
'-l:libvips.so.42'
], ],
'ldflags': [ 'ldflags': [
# Ensure runtime linking is relative to sharp.node # Ensure runtime linking is relative to sharp.node

View File

@@ -50,6 +50,10 @@ no child processes are spawned and Promises/async/await are supported.
### Optimal ### Optimal
The features of `mozjpeg` and `pngquant` can be used
to optimise the file size of JPEG and PNG images respectively,
without having to invoke separate `imagemin` processes.
Huffman tables are optimised when generating JPEG output images Huffman tables are optimised when generating JPEG output images
without having to use separate command line tools like without having to use separate command line tools like
[jpegoptim](https://github.com/tjko/jpegoptim) and [jpegoptim](https://github.com/tjko/jpegoptim) and

View File

@@ -18,23 +18,38 @@ Returns **Sharp**
## ensureAlpha ## ensureAlpha
Ensure alpha channel, if missing. The added alpha channel will be fully opaque. This is a no-op if the image already has an alpha channel. Ensure the output image has an alpha transparency channel.
If missing, the added alpha channel will have the specified
transparency level, defaulting to fully-opaque (1).
This is a no-op if the image already has an alpha channel.
### Parameters
* `alpha` **[number][1]** alpha transparency level (0=fully-transparent, 1=fully-opaque) (optional, default `1`)
### Examples ### Examples
```javascript ```javascript
sharp('rgb.jpg') // rgba.png will be a 4 channel image with a fully-opaque alpha channel
await sharp('rgb.jpg')
.ensureAlpha() .ensureAlpha()
.toFile('rgba.png', function(err, info) { .toFile('rgba.png')
// rgba.png is a 4 channel image with a fully opaque alpha channel
});
``` ```
```javascript
// rgba is a 4 channel image with a fully-transparent alpha channel
const rgba = await sharp(rgb)
.ensureAlpha(0)
.toBuffer();
```
* Throws **[Error][2]** Invalid alpha transparency level
Returns **Sharp** Returns **Sharp**
**Meta** **Meta**
- **since**: 0.21.2 * **since**: 0.21.2
## extractChannel ## extractChannel
@@ -42,7 +57,7 @@ Extract a single channel from a multi-channel image.
### Parameters ### Parameters
- `channel` **([number][1] \| [string][2])** zero-indexed channel/band number to extract, or `red`, `green`, `blue` or `alpha`. * `channel` **([number][1] | [string][3])** zero-indexed channel/band number to extract, or `red`, `green`, `blue` or `alpha`.
### Examples ### Examples
@@ -56,7 +71,7 @@ sharp(input)
}); });
``` ```
- Throws **[Error][3]** Invalid channel * Throws **[Error][2]** Invalid channel
Returns **Sharp** Returns **Sharp**
@@ -67,19 +82,20 @@ The meaning of the added channels depends on the output colourspace, set with `t
By default the output image will be web-friendly sRGB, with additional channels interpreted as alpha channels. By default the output image will be web-friendly sRGB, with additional channels interpreted as alpha channels.
Channel ordering follows vips convention: Channel ordering follows vips convention:
- sRGB: 0: Red, 1: Green, 2: Blue, 3: Alpha. * sRGB: 0: Red, 1: Green, 2: Blue, 3: Alpha.
- CMYK: 0: Magenta, 1: Cyan, 2: Yellow, 3: Black, 4: Alpha. * CMYK: 0: Magenta, 1: Cyan, 2: Yellow, 3: Black, 4: Alpha.
Buffers may be any of the image formats supported by sharp. Buffers may be any of the image formats supported by sharp.
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. 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][4]&lt;([string][2] \| [Buffer][5])> | [string][2] \| [Buffer][5])** one or more images (file paths, Buffers). * `images` **([Array][4]<([string][3] | [Buffer][5])> | [string][3] | [Buffer][5])** one or more images (file paths, Buffers).
- `options` **[Object][6]** image options, see `sharp()` constructor. * `options` **[Object][6]** image options, see `sharp()` constructor.
<!---->
- Throws **[Error][3]** Invalid parameters * Throws **[Error][2]** Invalid parameters
Returns **Sharp** Returns **Sharp**
@@ -89,7 +105,7 @@ Perform a bitwise boolean operation on all input image channels (bands) to produ
### Parameters ### Parameters
- `boolOp` **[string][2]** one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively. * `boolOp` **[string][3]** one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively.
### Examples ### Examples
@@ -103,15 +119,15 @@ sharp('3-channel-rgb-input.png')
}); });
``` ```
- Throws **[Error][3]** Invalid parameters * Throws **[Error][2]** Invalid parameters
Returns **Sharp** Returns **Sharp**
[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number [1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String [2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error [3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array [4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array

View File

@@ -7,10 +7,11 @@ An alpha channel may be present and will be unchanged by the operation.
### Parameters ### Parameters
- `rgb` **([string][1] \| [Object][2])** parsed by the [color][3] module to extract chroma values. * `rgb` **([string][1] | [Object][2])** parsed by the [color][3] module to extract chroma values.
<!---->
- Throws **[Error][4]** Invalid parameter * Throws **[Error][4]** Invalid parameter
Returns **Sharp** Returns **Sharp**
@@ -25,7 +26,7 @@ An alpha channel may be present, and will be unchanged by the operation.
### Parameters ### Parameters
- `greyscale` **[Boolean][5]** (optional, default `true`) * `greyscale` **[Boolean][5]** (optional, default `true`)
Returns **Sharp** Returns **Sharp**
@@ -35,7 +36,7 @@ Alternative spelling of `greyscale`.
### Parameters ### Parameters
- `grayscale` **[Boolean][5]** (optional, default `true`) * `grayscale` **[Boolean][5]** (optional, default `true`)
Returns **Sharp** Returns **Sharp**
@@ -46,7 +47,7 @@ By default output image will be web-friendly sRGB, with additional channels inte
### Parameters ### Parameters
- `colourspace` **[string][1]?** output colourspace e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...][6] * `colourspace` **[string][1]?** output colourspace e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...][6]
### Examples ### Examples
@@ -57,7 +58,7 @@ await sharp(input)
.toFile('16-bpp.png') .toFile('16-bpp.png')
``` ```
- Throws **[Error][4]** Invalid parameters * Throws **[Error][4]** Invalid parameters
Returns **Sharp** Returns **Sharp**
@@ -67,10 +68,11 @@ Alternative spelling of `toColourspace`.
### Parameters ### Parameters
- `colorspace` **[string][1]?** output colorspace. * `colorspace` **[string][1]?** output colorspace.
<!---->
- Throws **[Error][4]** Invalid parameters * Throws **[Error][4]** Invalid parameters
Returns **Sharp** Returns **Sharp**

View File

@@ -19,24 +19,28 @@ and [https://www.cairographics.org/operators/][2]
### Parameters ### Parameters
- `images` **[Array][3]&lt;[Object][4]>** Ordered list of images to composite * `images` **[Array][3]<[Object][4]>** Ordered list of images to composite
- `images[].input` **([Buffer][5] \| [String][6])?** Buffer containing image data, String containing the path to an image file, or Create object (see below)
- `images[].input.create` **[Object][4]?** describes a blank overlay to be created. * `images[].input` **([Buffer][5] | [String][6])?** Buffer containing image data, String containing the path to an image file, or Create object (see below)
- `images[].input.create.width` **[Number][7]?**
- `images[].input.create.height` **[Number][7]?** * `images[].input.create` **[Object][4]?** describes a blank overlay to be created.
- `images[].input.create.channels` **[Number][7]?** 3-4
- `images[].input.create.background` **([String][6] \| [Object][4])?** parsed by the [color][8] module to extract values for red, green, blue and alpha. * `images[].input.create.width` **[Number][7]?**
- `images[].blend` **[String][6]** how to blend this image with the image below. (optional, default `'over'`) * `images[].input.create.height` **[Number][7]?**
- `images[].gravity` **[String][6]** gravity at which to place the overlay. (optional, default `'centre'`) * `images[].input.create.channels` **[Number][7]?** 3-4
- `images[].top` **[Number][7]?** the pixel offset from the top edge. * `images[].input.create.background` **([String][6] | [Object][4])?** parsed by the [color][8] module to extract values for red, green, blue and alpha.
- `images[].left` **[Number][7]?** the pixel offset from the left edge. * `images[].blend` **[String][6]** how to blend this image with the image below. (optional, default `'over'`)
- `images[].tile` **[Boolean][9]** set to true to repeat the overlay image across the entire image with the given `gravity`. (optional, default `false`) * `images[].gravity` **[String][6]** gravity at which to place the overlay. (optional, default `'centre'`)
- `images[].premultiplied` **[Boolean][9]** set to true to avoid premultipling the image below. Equivalent to the `--premultiplied` vips option. (optional, default `false`) * `images[].top` **[Number][7]?** the pixel offset from the top edge.
- `images[].density` **[Number][7]** number representing the DPI for vector overlay image. (optional, default `72`) * `images[].left` **[Number][7]?** the pixel offset from the left edge.
- `images[].raw` **[Object][4]?** describes overlay when using raw pixel data. * `images[].tile` **[Boolean][9]** set to true to repeat the overlay image across the entire image with the given `gravity`. (optional, default `false`)
- `images[].raw.width` **[Number][7]?** * `images[].premultiplied` **[Boolean][9]** set to true to avoid premultipling the image below. Equivalent to the `--premultiplied` vips option. (optional, default `false`)
- `images[].raw.height` **[Number][7]?** * `images[].density` **[Number][7]** number representing the DPI for vector overlay image. (optional, default `72`)
- `images[].raw.channels` **[Number][7]?** * `images[].raw` **[Object][4]?** describes overlay when using raw pixel data.
* `images[].raw.width` **[Number][7]?**
* `images[].raw.height` **[Number][7]?**
* `images[].raw.channels` **[Number][7]?**
### Examples ### Examples
@@ -57,13 +61,13 @@ sharp('input.png')
}); });
``` ```
- Throws **[Error][10]** Invalid parameters * Throws **[Error][10]** Invalid parameters
Returns **Sharp** Returns **Sharp**
**Meta** **Meta**
- **since**: 0.22.0 * **since**: 0.22.0
[1]: https://libvips.github.io/libvips/API/current/libvips-conversion.html#VipsBlendMode [1]: https://libvips.github.io/libvips/API/current/libvips-conversion.html#VipsBlendMode

View File

@@ -13,36 +13,43 @@ Implements the [stream.Duplex][1] class.
### Parameters ### Parameters
- `input` **([Buffer][2] \| [Uint8Array][3] \| [Uint8ClampedArray][4] \| [string][5])?** if present, can be * `input` **([Buffer][2] | [Uint8Array][3] | [Uint8ClampedArray][4] | [string][5])?** if present, can be
a Buffer / Uint8Array / Uint8ClampedArray containing JPEG, PNG, WebP, AVIF, GIF, SVG, TIFF or raw pixel image data, or a Buffer / Uint8Array / Uint8ClampedArray containing JPEG, PNG, WebP, AVIF, GIF, SVG, TIFF or raw pixel image data, or
a String containing the filesystem path to an JPEG, PNG, WebP, AVIF, GIF, SVG or TIFF image file. a String containing the filesystem path to an JPEG, PNG, WebP, AVIF, GIF, SVG or TIFF image file.
JPEG, PNG, WebP, AVIF, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when not present. JPEG, PNG, WebP, AVIF, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when not present.
- `options` **[Object][6]?** if present, is an Object with optional attributes. * `options` **[Object][6]?** if present, is an Object with optional attributes.
- `options.failOnError` **[boolean][7]** by default halt processing and raise an error when loading invalid images.
* `options.failOnError` **[boolean][7]** by default halt processing and raise an error when loading invalid images.
Set this flag to `false` if you'd rather apply a "best effort" to decode images, even if the data is corrupt or invalid. (optional, default `true`) Set this flag to `false` if you'd rather apply a "best effort" to decode images, even if the data is corrupt or invalid. (optional, default `true`)
- `options.limitInputPixels` **([number][8] \| [boolean][7])** Do not process input images where the number of pixels * `options.limitInputPixels` **([number][8] | [boolean][7])** Do not process input images where the number of pixels
(width x height) exceeds this limit. Assumes image dimensions contained in the input metadata can be trusted. (width x height) exceeds this limit. Assumes image dimensions contained in the input metadata can be trusted.
An integral Number of pixels, zero or false to remove limit, true to use default limit of 268402689 (0x3FFF x 0x3FFF). (optional, default `268402689`) An integral Number of pixels, zero or false to remove limit, true to use default limit of 268402689 (0x3FFF x 0x3FFF). (optional, default `268402689`)
- `options.sequentialRead` **[boolean][7]** Set this to `true` to use sequential rather than random access where possible. * `options.sequentialRead` **[boolean][7]** Set this to `true` to use sequential rather than random access where possible.
This can reduce memory usage and might improve performance on some systems. (optional, default `false`) This can reduce memory usage and might improve performance on some systems. (optional, default `false`)
- `options.density` **[number][8]** number representing the DPI for vector images in the range 1 to 100000. (optional, default `72`) * `options.density` **[number][8]** number representing the DPI for vector images in the range 1 to 100000. (optional, default `72`)
- `options.pages` **[number][8]** number of pages to extract for multi-page input (GIF, WebP, AVIF, TIFF, PDF), use -1 for all pages. (optional, default `1`) * `options.pages` **[number][8]** number of pages to extract for multi-page input (GIF, WebP, AVIF, TIFF, PDF), use -1 for all pages. (optional, default `1`)
- `options.page` **[number][8]** page number to start extracting from for multi-page input (GIF, WebP, AVIF, TIFF, PDF), zero based. (optional, default `0`) * `options.page` **[number][8]** page number to start extracting from for multi-page input (GIF, WebP, AVIF, TIFF, PDF), zero based. (optional, default `0`)
- `options.level` **[number][8]** level to extract from a multi-level input (OpenSlide), zero based. (optional, default `0`) * `options.subifd` **[number][8]** subIFD (Sub Image File Directory) to extract for OME-TIFF, defaults to main image. (optional, default `-1`)
- `options.animated` **[boolean][7]** Set to `true` to read all frames/pages of an animated image (equivalent of setting `pages` to `-1`). (optional, default `false`) * `options.level` **[number][8]** level to extract from a multi-level input (OpenSlide), zero based. (optional, default `0`)
- `options.raw` **[Object][6]?** describes raw pixel input image data. See `raw()` for pixel ordering. * `options.animated` **[boolean][7]** Set to `true` to read all frames/pages of an animated image (equivalent of setting `pages` to `-1`). (optional, default `false`)
- `options.raw.width` **[number][8]?** integral number of pixels wide. * `options.raw` **[Object][6]?** describes raw pixel input image data. See `raw()` for pixel ordering.
- `options.raw.height` **[number][8]?** integral number of pixels high.
- `options.raw.channels` **[number][8]?** integral number of channels, between 1 and 4. * `options.raw.width` **[number][8]?** integral number of pixels wide.
- `options.create` **[Object][6]?** describes a new image to be created. * `options.raw.height` **[number][8]?** integral number of pixels high.
- `options.create.width` **[number][8]?** integral number of pixels wide. * `options.raw.channels` **[number][8]?** integral number of channels, between 1 and 4.
- `options.create.height` **[number][8]?** integral number of pixels high. * `options.raw.premultiplied` **[boolean][7]?** specifies that the raw input has already been premultiplied, set to `true`
- `options.create.channels` **[number][8]?** integral number of channels, either 3 (RGB) or 4 (RGBA). to avoid sharp premultiplying the image. (optional, default `false`)
- `options.create.background` **([string][5] \| [Object][6])?** parsed by the [color][9] module to extract values for red, green, blue and alpha. * `options.create` **[Object][6]?** describes a new image to be created.
- `options.create.noise` **[Object][6]?** describes a noise to be created.
- `options.create.noise.type` **[string][5]?** type of generated noise, currently only `gaussian` is supported. * `options.create.width` **[number][8]?** integral number of pixels wide.
- `options.create.noise.mean` **[number][8]?** mean of pixels in generated noise. * `options.create.height` **[number][8]?** integral number of pixels high.
- `options.create.noise.sigma` **[number][8]?** standard deviation of pixels in generated noise. * `options.create.channels` **[number][8]?** integral number of channels, either 3 (RGB) or 4 (RGBA).
* `options.create.background` **([string][5] | [Object][6])?** parsed by the [color][9] module to extract values for red, green, blue and alpha.
* `options.create.noise` **[Object][6]?** describes a noise to be created.
* `options.create.noise.type` **[string][5]?** type of generated noise, currently only `gaussian` is supported.
* `options.create.noise.mean` **[number][8]?** mean of pixels in generated noise.
* `options.create.noise.sigma` **[number][8]?** standard deviation of pixels in generated noise.
### Examples ### Examples
@@ -119,7 +126,7 @@ await sharp({
}).toFile('noise.png'); }).toFile('noise.png');
``` ```
- Throws **[Error][10]** Invalid parameters * Throws **[Error][10]** Invalid parameters
Returns **[Sharp][11]** Returns **[Sharp][11]**

View File

@@ -5,34 +5,35 @@
Fast access to (uncached) image metadata without decoding any compressed image data. Fast access to (uncached) image metadata without decoding any compressed image data.
A `Promise` is returned when `callback` is not provided. 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` * `format`: Name of decoder used to decompress image data e.g. `jpeg`, `png`, `webp`, `gif`, `svg`
- `size`: Total size of image in bytes, for Stream and Buffer input only * `size`: Total size of image in bytes, for Stream and Buffer input only
- `width`: Number of pixels wide (EXIF orientation is not taken into consideration) * `width`: Number of pixels wide (EXIF orientation is not taken into consideration)
- `height`: Number of pixels high (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] * `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 * `channels`: Number of bands e.g. `3` for sRGB, `4` for CMYK
- `depth`: Name of pixel depth format e.g. `uchar`, `char`, `ushort`, `float` [...][2] * `depth`: Name of pixel depth format e.g. `uchar`, `char`, `ushort`, `float` [...][2]
- `density`: Number of pixels per inch (DPI), if present * `density`: Number of pixels per inch (DPI), if present
- `chromaSubsampling`: String containing JPEG chroma subsampling, `4:2:0` or `4:4:4` for RGB, `4:2:0:4` or `4:4:4:4` for CMYK * `chromaSubsampling`: String containing JPEG chroma subsampling, `4:2:0` or `4:4:4` for RGB, `4:2:0:4` or `4:4:4:4` for CMYK
- `isProgressive`: Boolean indicating whether the image is interlaced using a progressive scan * `isProgressive`: Boolean indicating whether the image is interlaced using a progressive scan
- `pages`: Number of pages/frames contained within the image, with support for TIFF, HEIF, PDF, animated GIF and animated WebP * `pages`: Number of pages/frames contained within the image, with support for TIFF, HEIF, PDF, animated GIF and animated WebP
- `pageHeight`: Number of pixels high each page in a multi-page image will be. * `pageHeight`: Number of pixels high each page in a multi-page image will be.
- `loop`: Number of times to loop an animated image, zero refers to a continuous loop. * `loop`: Number of times to loop an animated image, zero refers to a continuous loop.
- `delay`: Delay in ms between each page in an animated image, provided as an array of integers. * `delay`: Delay in ms between each page in an animated image, provided as an array of integers.
- `pagePrimary`: Number of the primary page in a HEIF image * `pagePrimary`: Number of the primary page in a HEIF image
- `levels`: Details of each level in a multi-level image provided as an array of objects, requires libvips compiled with support for OpenSlide * `levels`: Details of each level in a multi-level image provided as an array of objects, requires libvips compiled with support for OpenSlide
- `hasProfile`: Boolean indicating the presence of an embedded ICC profile * `subifds`: Number of Sub Image File Directories in an OME-TIFF image
- `hasAlpha`: Boolean indicating the presence of an alpha transparency channel * `hasProfile`: Boolean indicating the presence of an embedded ICC profile
- `orientation`: Number value of the EXIF Orientation header, if present * `hasAlpha`: Boolean indicating the presence of an alpha transparency channel
- `exif`: Buffer containing raw EXIF data, if present * `orientation`: Number value of the EXIF Orientation header, if present
- `icc`: Buffer containing raw [ICC][3] profile data, if present * `exif`: Buffer containing raw EXIF data, if present
- `iptc`: Buffer containing raw IPTC data, if present * `icc`: Buffer containing raw [ICC][3] profile data, if present
- `xmp`: Buffer containing raw XMP data, if present * `iptc`: Buffer containing raw IPTC data, if present
- `tifftagPhotoshop`: Buffer containing raw TIFFTAG_PHOTOSHOP data, if present * `xmp`: Buffer containing raw XMP data, if present
* `tifftagPhotoshop`: Buffer containing raw TIFFTAG_PHOTOSHOP data, if present
### Parameters ### Parameters
- `callback` **[Function][4]?** called with the arguments `(err, metadata)` * `callback` **[Function][4]?** called with the arguments `(err, metadata)`
### Examples ### Examples
@@ -51,32 +52,32 @@ image
}); });
``` ```
Returns **([Promise][5]&lt;[Object][6]> | Sharp)** Returns **([Promise][5]<[Object][6]> | Sharp)**
## stats ## stats
Access to pixel-derived image statistics for every channel in the image. Access to pixel-derived image statistics for every channel in the image.
A `Promise` is returned when `callback` is not provided. A `Promise` is returned when `callback` is not provided.
- `channels`: Array of channel statistics for each channel in the image. Each channel statistic contains * `channels`: Array of channel statistics for each channel in the image. Each channel statistic contains
- `min` (minimum value in the channel) * `min` (minimum value in the channel)
- `max` (maximum value in the channel) * `max` (maximum value in the channel)
- `sum` (sum of all values in a channel) * `sum` (sum of all values in a channel)
- `squaresSum` (sum of squared values in a channel) * `squaresSum` (sum of squared values in a channel)
- `mean` (mean of the values in a channel) * `mean` (mean of the values in a channel)
- `stdev` (standard deviation for the values in a channel) * `stdev` (standard deviation for the values in a channel)
- `minX` (x-coordinate of one of the pixel where the minimum lies) * `minX` (x-coordinate of one of the pixel where the minimum lies)
- `minY` (y-coordinate of one of the pixel where the minimum lies) * `minY` (y-coordinate of one of the pixel where the minimum lies)
- `maxX` (x-coordinate of one of the pixel where the maximum lies) * `maxX` (x-coordinate of one of the pixel where the maximum lies)
- `maxY` (y-coordinate of one of the pixel where the maximum lies) * `maxY` (y-coordinate of one of the pixel where the maximum lies)
- `isOpaque`: Is the image fully opaque? Will be `true` if the image has no alpha channel or if every pixel is fully opaque. * `isOpaque`: Is the image fully opaque? Will be `true` if the image has no alpha channel or if every pixel is fully opaque.
- `entropy`: Histogram-based estimation of greyscale entropy, discarding alpha channel if any (experimental) * `entropy`: Histogram-based estimation of greyscale entropy, discarding alpha channel if any (experimental)
- `sharpness`: Estimation of greyscale sharpness based on the standard deviation of a Laplacian convolution, discarding alpha channel if any (experimental) * `sharpness`: Estimation of greyscale sharpness based on the standard deviation of a Laplacian convolution, discarding alpha channel if any (experimental)
- `dominant`: Object containing most dominant sRGB colour based on a 4096-bin 3D histogram (experimental) * `dominant`: Object containing most dominant sRGB colour based on a 4096-bin 3D histogram (experimental)
### Parameters ### Parameters
- `callback` **[Function][4]?** called with the arguments `(err, stats)` * `callback` **[Function][4]?** called with the arguments `(err, stats)`
### Examples ### Examples
@@ -94,7 +95,7 @@ const { entropy, sharpness, dominant } = await sharp(input).stats();
const { r, g, b } = dominant; const { r, g, b } = dominant;
``` ```
Returns **[Promise][5]&lt;[Object][6]>** Returns **[Promise][5]<[Object][6]>**
[1]: https://libvips.github.io/libvips/API/current/VipsImage.html#VipsInterpretation [1]: https://libvips.github.io/libvips/API/current/VipsImage.html#VipsInterpretation

View File

@@ -21,9 +21,10 @@ for example `rotate(x).extract(y)` will produce a different result to `extract(y
### Parameters ### Parameters
- `angle` **[number][1]** angle of rotation. (optional, default `auto`) * `angle` **[number][1]** angle of rotation. (optional, default `auto`)
- `options` **[Object][2]?** if present, is an Object with optional attributes. * `options` **[Object][2]?** if present, is an Object with optional attributes.
- `options.background` **([string][3] \| [Object][2])** parsed by the [color][4] module to extract values for red, green, blue and alpha. (optional, default `"#000000"`)
* `options.background` **([string][3] | [Object][2])** parsed by the [color][4] module to extract values for red, green, blue and alpha. (optional, default `"#000000"`)
### Examples ### Examples
@@ -39,7 +40,7 @@ const pipeline = sharp()
readableStream.pipe(pipeline); readableStream.pipe(pipeline);
``` ```
- Throws **[Error][5]** Invalid parameters * Throws **[Error][5]** Invalid parameters
Returns **Sharp** Returns **Sharp**
@@ -50,7 +51,7 @@ The use of `flip` implies the removal of the EXIF `Orientation` tag, if any.
### Parameters ### Parameters
- `flip` **[Boolean][6]** (optional, default `true`) * `flip` **[Boolean][6]** (optional, default `true`)
Returns **Sharp** Returns **Sharp**
@@ -61,7 +62,7 @@ The use of `flop` implies the removal of the EXIF `Orientation` tag, if any.
### Parameters ### Parameters
- `flop` **[Boolean][6]** (optional, default `true`) * `flop` **[Boolean][6]** (optional, default `true`)
Returns **Sharp** Returns **Sharp**
@@ -75,25 +76,26 @@ A particular interpolator may also be specified. Set the `interpolator` option t
In the case of a 2x2 matrix, the transform is: In the case of a 2x2 matrix, the transform is:
- X = `matrix[0, 0]` \* (x + `idx`) + `matrix[0, 1]` \* (y + `idy`) + `odx` * X = `matrix[0, 0]` \* (x + `idx`) + `matrix[0, 1]` \* (y + `idy`) + `odx`
- Y = `matrix[1, 0]` \* (x + `idx`) + `matrix[1, 1]` \* (y + `idy`) + `ody` * Y = `matrix[1, 0]` \* (x + `idx`) + `matrix[1, 1]` \* (y + `idy`) + `ody`
where: where:
- x and y are the coordinates in input image. * x and y are the coordinates in input image.
- X and Y are the coordinates in output image. * X and Y are the coordinates in output image.
- (0,0) is the upper left corner. * (0,0) is the upper left corner.
### Parameters ### Parameters
- `matrix` **([Array][7]&lt;[Array][7]&lt;[number][1]>> | [Array][7]&lt;[number][1]>)** affine transformation matrix * `matrix` **([Array][7]<[Array][7]<[number][1]>> | [Array][7]<[number][1]>)** affine transformation matrix
- `options` **[Object][2]?** if present, is an Object with optional attributes. * `options` **[Object][2]?** if present, is an Object with optional attributes.
- `options.background` **([String][3] \| [Object][2])** parsed by the [color][4] module to extract values for red, green, blue and alpha. (optional, default `"#000000"`)
- `options.idx` **[Number][1]** input horizontal offset (optional, default `0`) * `options.background` **([String][3] | [Object][2])** parsed by the [color][4] module to extract values for red, green, blue and alpha. (optional, default `"#000000"`)
- `options.idy` **[Number][1]** input vertical offset (optional, default `0`) * `options.idx` **[Number][1]** input horizontal offset (optional, default `0`)
- `options.odx` **[Number][1]** output horizontal offset (optional, default `0`) * `options.idy` **[Number][1]** input vertical offset (optional, default `0`)
- `options.ody` **[Number][1]** output vertical offset (optional, default `0`) * `options.odx` **[Number][1]** output horizontal offset (optional, default `0`)
- `options.interpolator` **[String][3]** interpolator (optional, default `sharp.interpolators.bicubic`) * `options.ody` **[Number][1]** output vertical offset (optional, default `0`)
* `options.interpolator` **[String][3]** interpolator (optional, default `sharp.interpolators.bicubic`)
### Examples ### Examples
@@ -112,13 +114,13 @@ inputStream
.pipe(pipeline); .pipe(pipeline);
``` ```
- Throws **[Error][5]** Invalid parameters * Throws **[Error][5]** Invalid parameters
Returns **Sharp** Returns **Sharp**
**Meta** **Meta**
- **since**: 0.27.0 * **since**: 0.27.0
## sharpen ## sharpen
@@ -129,12 +131,13 @@ Separate control over the level of sharpening in "flat" and "jagged" areas is av
### Parameters ### Parameters
- `sigma` **[number][1]?** the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`. * `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`) * `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`) * `jagged` **[number][1]** the level of sharpening to apply to "jagged" areas. (optional, default `2.0`)
<!---->
- Throws **[Error][5]** Invalid parameters * Throws **[Error][5]** Invalid parameters
Returns **Sharp** Returns **Sharp**
@@ -145,10 +148,11 @@ When used without parameters the default window is 3x3.
### Parameters ### Parameters
- `size` **[number][1]** square mask size: size x size (optional, default `3`) * `size` **[number][1]** square mask size: size x size (optional, default `3`)
<!---->
- Throws **[Error][5]** Invalid parameters * Throws **[Error][5]** Invalid parameters
Returns **Sharp** Returns **Sharp**
@@ -160,21 +164,31 @@ When a `sigma` is provided, performs a slower, more accurate Gaussian blur.
### Parameters ### Parameters
- `sigma` **[number][1]?** 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][5]** Invalid parameters * Throws **[Error][5]** Invalid parameters
Returns **Sharp** Returns **Sharp**
## flatten ## flatten
Merge alpha transparency channel, if any, with a background. Merge alpha transparency channel, if any, with a background, then remove the alpha channel.
### Parameters ### Parameters
- `options` **[Object][2]?** * `options` **[Object][2]?**
- `options.background` **([string][3] \| [Object][2])** background colour, parsed by the [color][4] module, defaults to black. (optional, default `{r:0,g:0,b:0}`)
* `options.background` **([string][3] | [Object][2])** background colour, parsed by the [color][4] module, defaults to black. (optional, default `{r:0,g:0,b:0}`)
### Examples
```javascript
await sharp(rgbaInput)
.flatten({background: '#F0A703' })
.toBuffer();
```
Returns **Sharp** Returns **Sharp**
@@ -190,11 +204,12 @@ Supply a second argument to use a different output gamma value, otherwise the fi
### Parameters ### Parameters
- `gamma` **[number][1]** 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`)
- `gammaOut` **[number][1]?** value between 1.0 and 3.0. (optional, defaults to same as `gamma`) * `gammaOut` **[number][1]?** value between 1.0 and 3.0. (optional, defaults to same as `gamma`)
<!---->
- Throws **[Error][5]** Invalid parameters * Throws **[Error][5]** Invalid parameters
Returns **Sharp** Returns **Sharp**
@@ -204,7 +219,7 @@ Produce the "negative" of the image.
### Parameters ### Parameters
- `negate` **[Boolean][6]** (optional, default `true`) * `negate` **[Boolean][6]** (optional, default `true`)
Returns **Sharp** Returns **Sharp**
@@ -214,7 +229,7 @@ Enhance output image contrast by stretching its luminance to cover the full dyna
### Parameters ### Parameters
- `normalise` **[Boolean][6]** (optional, default `true`) * `normalise` **[Boolean][6]** (optional, default `true`)
Returns **Sharp** Returns **Sharp**
@@ -224,7 +239,7 @@ Alternative spelling of normalise.
### Parameters ### Parameters
- `normalize` **[Boolean][6]** (optional, default `true`) * `normalize` **[Boolean][6]** (optional, default `true`)
Returns **Sharp** Returns **Sharp**
@@ -234,12 +249,13 @@ Convolve the image with the specified kernel.
### Parameters ### Parameters
- `kernel` **[Object][2]** * `kernel` **[Object][2]**
- `kernel.width` **[number][1]** width of the kernel in pixels.
- `kernel.height` **[number][1]** width of the kernel in pixels. * `kernel.width` **[number][1]** width of the kernel in pixels.
- `kernel.kernel` **[Array][7]&lt;[number][1]>** Array of length `width*height` containing the kernel values. * `kernel.height` **[number][1]** width of the kernel in pixels.
- `kernel.scale` **[number][1]** the scale of the kernel in pixels. (optional, default `sum`) * `kernel.kernel` **[Array][7]<[number][1]>** Array of length `width*height` containing the kernel values.
- `kernel.offset` **[number][1]** the offset of the kernel in pixels. (optional, default `0`) * `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
@@ -257,23 +273,25 @@ sharp(input)
}); });
``` ```
- Throws **[Error][5]** Invalid parameters * Throws **[Error][5]** Invalid parameters
Returns **Sharp** Returns **Sharp**
## threshold ## threshold
Any pixel value greather than or equal to the threshold value will be set to 255, otherwise it will be set to 0. Any pixel value greater than or equal to the threshold value will be set to 255, otherwise it will be set to 0.
### Parameters ### Parameters
- `threshold` **[number][1]** a value in the range 0-255 representing the level at which the threshold will be applied. (optional, default `128`) * `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][2]?** * `options` **[Object][2]?**
- `options.greyscale` **[Boolean][6]** convert to single channel greyscale. (optional, default `true`)
- `options.grayscale` **[Boolean][6]** alternative spelling for greyscale. (optional, default `true`)
* `options.greyscale` **[Boolean][6]** convert to single channel greyscale. (optional, default `true`)
* `options.grayscale` **[Boolean][6]** alternative spelling for greyscale. (optional, default `true`)
- Throws **[Error][5]** Invalid parameters <!---->
* Throws **[Error][5]** Invalid parameters
Returns **Sharp** Returns **Sharp**
@@ -286,16 +304,19 @@ the selected bitwise boolean `operation` between the corresponding pixels of the
### Parameters ### Parameters
- `operand` **([Buffer][8] \| [string][3])** Buffer containing image data or string containing the path to an image file. * `operand` **([Buffer][8] | [string][3])** Buffer containing image data or string containing the path to an image file.
- `operator` **[string][3]** one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively. * `operator` **[string][3]** one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively.
- `options` **[Object][2]?** * `options` **[Object][2]?**
- `options.raw` **[Object][2]?** describes operand when using raw pixel data.
- `options.raw.width` **[number][1]?**
- `options.raw.height` **[number][1]?**
- `options.raw.channels` **[number][1]?**
* `options.raw` **[Object][2]?** describes operand when using raw pixel data.
- Throws **[Error][5]** Invalid parameters * `options.raw.width` **[number][1]?**
* `options.raw.height` **[number][1]?**
* `options.raw.channels` **[number][1]?**
<!---->
* Throws **[Error][5]** Invalid parameters
Returns **Sharp** Returns **Sharp**
@@ -305,11 +326,12 @@ Apply the linear formula a \* input + b to the image (levels adjustment)
### Parameters ### Parameters
- `a` **[number][1]** multiplier (optional, default `1.0`) * `a` **[number][1]** multiplier (optional, default `1.0`)
- `b` **[number][1]** offset (optional, default `0.0`) * `b` **[number][1]** offset (optional, default `0.0`)
<!---->
- Throws **[Error][5]** Invalid parameters * Throws **[Error][5]** Invalid parameters
Returns **Sharp** Returns **Sharp**
@@ -319,7 +341,7 @@ Recomb the image with the specified matrix.
### Parameters ### Parameters
- `inputMatrix` **[Array][7]&lt;[Array][7]&lt;[number][1]>>** 3x3 Recombination matrix * `inputMatrix` **[Array][7]<[Array][7]<[number][1]>>** 3x3 Recombination matrix
### Examples ### Examples
@@ -337,13 +359,13 @@ sharp(input)
}); });
``` ```
- Throws **[Error][5]** Invalid parameters * Throws **[Error][5]** Invalid parameters
Returns **Sharp** Returns **Sharp**
**Meta** **Meta**
- **since**: 0.21.1 * **since**: 0.21.1
## modulate ## modulate
@@ -351,10 +373,11 @@ Transforms the image using brightness, saturation and hue rotation.
### Parameters ### Parameters
- `options` **[Object][2]?** * `options` **[Object][2]?**
- `options.brightness` **[number][1]?** Brightness multiplier
- `options.saturation` **[number][1]?** Saturation multiplier * `options.brightness` **[number][1]?** Brightness multiplier
- `options.hue` **[number][1]?** Degrees for hue rotation * `options.saturation` **[number][1]?** Saturation multiplier
* `options.hue` **[number][1]?** Degrees for hue rotation
### Examples ### Examples
@@ -382,7 +405,7 @@ Returns **Sharp**
**Meta** **Meta**
- **since**: 0.22.1 * **since**: 0.22.1
[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number [1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number

View File

@@ -15,8 +15,8 @@ A `Promise` is returned when `callback` is not provided.
### Parameters ### Parameters
- `fileOut` **[string][2]** the path to write the image data to. * `fileOut` **[string][2]** the path to write the image data to.
- `callback` **[Function][3]?** called on completion with two arguments `(err, info)`. * `callback` **[Function][3]?** called on completion with two arguments `(err, info)`.
`info` contains the output image `format`, `size` (bytes), `width`, `height`, `info` contains the output image `format`, `size` (bytes), `width`, `height`,
`channels` and `premultiplied` (indicating if premultiplication was used). `channels` and `premultiplied` (indicating if premultiplication was used).
When using a crop strategy also contains `cropOffsetLeft` and `cropOffsetTop`. When using a crop strategy also contains `cropOffsetLeft` and `cropOffsetTop`.
@@ -35,9 +35,9 @@ sharp(input)
.catch(err => { ... }); .catch(err => { ... });
``` ```
- Throws **[Error][4]** Invalid parameters * Throws **[Error][4]** Invalid parameters
Returns **[Promise][5]&lt;[Object][6]>** when no callback is provided Returns **[Promise][5]<[Object][6]>** when no callback is provided
## toBuffer ## toBuffer
@@ -51,19 +51,21 @@ See [withMetadata][1] for control over this.
`callback`, if present, gets three arguments `(err, data, info)` where: `callback`, if present, gets three arguments `(err, data, info)` where:
- `err` is an error, if any. * `err` is an error, if any.
- `data` is the output image data. * `data` is the output image data.
- `info` contains the output image `format`, `size` (bytes), `width`, `height`, * `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`. `channels` and `premultiplied` (indicating if premultiplication was used).
When using a crop strategy also contains `cropOffsetLeft` and `cropOffsetTop`.
A `Promise` is returned when `callback` is not provided. A `Promise` is returned when `callback` is not provided.
### Parameters ### Parameters
- `options` **[Object][6]?** * `options` **[Object][6]?**
- `options.resolveWithObject` **[boolean][7]?** Resolve the Promise with an Object containing `data` and `info` properties instead of resolving only with `data`.
- `callback` **[Function][3]?** * `options.resolveWithObject` **[boolean][7]?** Resolve the Promise with an Object containing `data` and `info` properties instead of resolving only with `data`.
* `callback` **[Function][3]?**
### Examples ### Examples
@@ -87,10 +89,10 @@ sharp(input)
``` ```
```javascript ```javascript
const data = await sharp('my-image.jpg') const { data, info } = await sharp('my-image.jpg')
// output the raw pixels // output the raw pixels
.raw() .raw()
.toBuffer(); .toBuffer({ resolveWithObject: true });
// create a more type safe way to work with the raw pixel data // create a more type safe way to work with the raw pixel data
// this will not copy the data, instead it will change `data`s underlying ArrayBuffer // this will not copy the data, instead it will change `data`s underlying ArrayBuffer
@@ -98,10 +100,12 @@ const data = await sharp('my-image.jpg')
const pixelArray = new Uint8ClampedArray(data.buffer); const pixelArray = new Uint8ClampedArray(data.buffer);
// When you are done changing the pixelArray, sharp takes the `pixelArray` as an input // When you are done changing the pixelArray, sharp takes the `pixelArray` as an input
await sharp(pixelArray).toFile('my-changed-image.jpg'); const { width, height, channels } = info;
await sharp(pixelArray, { raw: { width, height, channels } })
.toFile('my-changed-image.jpg');
``` ```
Returns **[Promise][5]&lt;[Buffer][8]>** when no callback is provided Returns **[Promise][5]<[Buffer][8]>** when no callback is provided
## withMetadata ## withMetadata
@@ -114,9 +118,12 @@ sRGB colour space and strip all metadata, including the removal of any ICC profi
### Parameters ### Parameters
- `options` **[Object][6]?** * `options` **[Object][6]?**
- `options.orientation` **[number][9]?** value between 1 and 8, used to update the EXIF `Orientation` tag.
- `options.icc` **[string][2]?** filesystem path to output ICC profile, defaults to sRGB. * `options.orientation` **[number][9]?** value between 1 and 8, used to update the EXIF `Orientation` tag.
* `options.icc` **[string][2]?** filesystem path to output ICC profile, defaults to sRGB.
* `options.exif` **[Object][6]<[Object][6]>** Object keyed by IFD0, IFD1 etc. of key/value string pairs to write as EXIF data. (optional, default `{}`)
* `options.density` **[number][9]?** Number of pixels per inch (DPI).
### Examples ### Examples
@@ -127,7 +134,26 @@ sharp('input.jpg')
.then(info => { ... }); .then(info => { ... });
``` ```
- Throws **[Error][4]** Invalid parameters ```javascript
// Set "IFD0-Copyright" in output EXIF metadata
const data = await sharp(input)
.withMetadata({
exif: {
IFD0: {
Copyright: 'Wernham Hogg'
}
}
})
.toBuffer();
* @example
// Set output metadata to 96 DPI
const data = await sharp(input)
.withMetadata({ density: 96 })
.toBuffer();
```
* Throws **[Error][4]** Invalid parameters
Returns **Sharp** Returns **Sharp**
@@ -137,8 +163,8 @@ Force output to a given format.
### Parameters ### Parameters
- `format` **([string][2] \| [Object][6])** as a string or an Object with an 'id' attribute * `format` **([string][2] | [Object][6])** as a string or an Object with an 'id' attribute
- `options` **[Object][6]** output options * `options` **[Object][6]** output options
### Examples ### Examples
@@ -149,7 +175,7 @@ const data = await sharp(input)
.toBuffer(); .toBuffer();
``` ```
- Throws **[Error][4]** unsupported format or options * Throws **[Error][4]** unsupported format or options
Returns **Sharp** Returns **Sharp**
@@ -157,23 +183,23 @@ Returns **Sharp**
Use these JPEG options for output image. Use these JPEG options for output image.
Some of these options require the use of a globally-installed libvips compiled with support for mozjpeg.
### Parameters ### Parameters
- `options` **[Object][6]?** output options * `options` **[Object][6]?** output options
- `options.quality` **[number][9]** quality, integer 1-100 (optional, default `80`)
- `options.progressive` **[boolean][7]** use progressive (interlace) scan (optional, default `false`) * `options.quality` **[number][9]** quality, integer 1-100 (optional, default `80`)
- `options.chromaSubsampling` **[string][2]** set to '4:4:4' to prevent chroma subsampling otherwise defaults to '4:2:0' chroma subsampling (optional, default `'4:2:0'`) * `options.progressive` **[boolean][7]** use progressive (interlace) scan (optional, default `false`)
- `options.optimiseCoding` **[boolean][7]** optimise Huffman coding tables (optional, default `true`) * `options.chromaSubsampling` **[string][2]** set to '4:4:4' to prevent chroma subsampling otherwise defaults to '4:2:0' chroma subsampling (optional, default `'4:2:0'`)
- `options.optimizeCoding` **[boolean][7]** alternative spelling of optimiseCoding (optional, default `true`) * `options.optimiseCoding` **[boolean][7]** optimise Huffman coding tables (optional, default `true`)
- `options.trellisQuantisation` **[boolean][7]** apply trellis quantisation, requires libvips compiled with support for mozjpeg (optional, default `false`) * `options.optimizeCoding` **[boolean][7]** alternative spelling of optimiseCoding (optional, default `true`)
- `options.overshootDeringing` **[boolean][7]** apply overshoot deringing, requires libvips compiled with support for mozjpeg (optional, default `false`) * `options.mozjpeg` **[boolean][7]** use mozjpeg defaults, equivalent to `{ trellisQuantisation: true, overshootDeringing: true, optimiseScans: true, quantisationTable: 3 }` (optional, default `false`)
- `options.optimiseScans` **[boolean][7]** optimise progressive scans, forces progressive, requires libvips compiled with support for mozjpeg (optional, default `false`) * `options.trellisQuantisation` **[boolean][7]** apply trellis quantisation (optional, default `false`)
- `options.optimizeScans` **[boolean][7]** alternative spelling of optimiseScans, requires libvips compiled with support for mozjpeg (optional, default `false`) * `options.overshootDeringing` **[boolean][7]** apply overshoot deringing (optional, default `false`)
- `options.quantisationTable` **[number][9]** quantization table to use, integer 0-8, requires libvips compiled with support for mozjpeg (optional, default `0`) * `options.optimiseScans` **[boolean][7]** optimise progressive scans, forces progressive (optional, default `false`)
- `options.quantizationTable` **[number][9]** alternative spelling of quantisationTable, requires libvips compiled with support for mozjpeg (optional, default `0`) * `options.optimizeScans` **[boolean][7]** alternative spelling of optimiseScans (optional, default `false`)
- `options.force` **[boolean][7]** force JPEG output, otherwise attempt to use input format (optional, default `true`) * `options.quantisationTable` **[number][9]** quantization table to use, integer 0-8 (optional, default `0`)
* `options.quantizationTable` **[number][9]** alternative spelling of quantisationTable (optional, default `0`)
* `options.force` **[boolean][7]** force JPEG output, otherwise attempt to use input format (optional, default `true`)
### Examples ### Examples
@@ -187,7 +213,14 @@ const data = await sharp(input)
.toBuffer(); .toBuffer();
``` ```
- Throws **[Error][4]** Invalid options ```javascript
// Use mozjpeg to reduce output JPEG file size (slower)
const data = await sharp(input)
.jpeg({ mozjpeg: true })
.toBuffer();
```
* Throws **[Error][4]** Invalid options
Returns **Sharp** Returns **Sharp**
@@ -195,34 +228,41 @@ Returns **Sharp**
Use these PNG options for output image. Use these PNG options for output image.
PNG output is always full colour at 8 or 16 bits per pixel. By default, PNG output is 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. Indexed PNG input at 1, 2 or 4 bits per pixel is converted to 8 bits per pixel.
Set `palette` to `true` for slower, indexed PNG output.
Some of these options require the use of a globally-installed libvips compiled with support for libimagequant (GPL).
### Parameters ### Parameters
- `options` **[Object][6]?** * `options` **[Object][6]?**
- `options.progressive` **[boolean][7]** use progressive (interlace) scan (optional, default `false`)
- `options.compressionLevel` **[number][9]** zlib compression level, 0-9 (optional, default `9`) * `options.progressive` **[boolean][7]** use progressive (interlace) scan (optional, default `false`)
- `options.adaptiveFiltering` **[boolean][7]** use adaptive row filtering (optional, default `false`) * `options.compressionLevel` **[number][9]** zlib compression level, 0 (fastest, largest) to 9 (slowest, smallest) (optional, default `6`)
- `options.palette` **[boolean][7]** quantise to a palette-based image with alpha transparency support, requires libvips compiled with support for libimagequant (optional, default `false`) * `options.adaptiveFiltering` **[boolean][7]** use adaptive row filtering (optional, default `false`)
- `options.quality` **[number][9]** use the lowest number of colours needed to achieve given quality, sets `palette` to `true`, requires libvips compiled with support for libimagequant (optional, default `100`) * `options.palette` **[boolean][7]** quantise to a palette-based image with alpha transparency support (optional, default `false`)
- `options.colours` **[number][9]** maximum number of palette entries, sets `palette` to `true`, requires libvips compiled with support for libimagequant (optional, default `256`) * `options.quality` **[number][9]** use the lowest number of colours needed to achieve given quality, sets `palette` to `true` (optional, default `100`)
- `options.colors` **[number][9]** alternative spelling of `options.colours`, sets `palette` to `true`, requires libvips compiled with support for libimagequant (optional, default `256`) * `options.colours` **[number][9]** maximum number of palette entries, sets `palette` to `true` (optional, default `256`)
- `options.dither` **[number][9]** level of Floyd-Steinberg error diffusion, sets `palette` to `true`, requires libvips compiled with support for libimagequant (optional, default `1.0`) * `options.colors` **[number][9]** alternative spelling of `options.colours`, sets `palette` to `true` (optional, default `256`)
- `options.force` **[boolean][7]** force PNG output, otherwise attempt to use input format (optional, default `true`) * `options.dither` **[number][9]** level of Floyd-Steinberg error diffusion, sets `palette` to `true` (optional, default `1.0`)
* `options.force` **[boolean][7]** force PNG output, otherwise attempt to use input format (optional, default `true`)
### Examples ### Examples
```javascript ```javascript
// Convert any input to PNG output // Convert any input to full colour PNG output
const data = await sharp(input) const data = await sharp(input)
.png() .png()
.toBuffer(); .toBuffer();
``` ```
- Throws **[Error][4]** Invalid options ```javascript
// Convert any input to indexed PNG output (slower)
const data = await sharp(input)
.png({ palette: true })
.toBuffer();
```
* Throws **[Error][4]** Invalid options
Returns **Sharp** Returns **Sharp**
@@ -232,17 +272,18 @@ Use these WebP options for output image.
### Parameters ### Parameters
- `options` **[Object][6]?** output options * `options` **[Object][6]?** output options
- `options.quality` **[number][9]** quality, integer 1-100 (optional, default `80`)
- `options.alphaQuality` **[number][9]** quality of alpha layer, integer 0-100 (optional, default `100`) * `options.quality` **[number][9]** quality, integer 1-100 (optional, default `80`)
- `options.lossless` **[boolean][7]** use lossless compression mode (optional, default `false`) * `options.alphaQuality` **[number][9]** quality of alpha layer, integer 0-100 (optional, default `100`)
- `options.nearLossless` **[boolean][7]** use near_lossless compression mode (optional, default `false`) * `options.lossless` **[boolean][7]** use lossless compression mode (optional, default `false`)
- `options.smartSubsample` **[boolean][7]** use high quality chroma subsampling (optional, default `false`) * `options.nearLossless` **[boolean][7]** use near_lossless compression mode (optional, default `false`)
- `options.reductionEffort` **[number][9]** level of CPU effort to reduce file size, integer 0-6 (optional, default `4`) * `options.smartSubsample` **[boolean][7]** use high quality chroma subsampling (optional, default `false`)
- `options.pageHeight` **[number][9]?** page height for animated output * `options.reductionEffort` **[number][9]** level of CPU effort to reduce file size, integer 0-6 (optional, default `4`)
- `options.loop` **[number][9]** number of animation iterations, use 0 for infinite animation (optional, default `0`) * `options.pageHeight` **[number][9]?** page height for animated output
- `options.delay` **[Array][10]&lt;[number][9]>?** list of delays between animation frames (in milliseconds) * `options.loop` **[number][9]** number of animation iterations, use 0 for infinite animation (optional, default `0`)
- `options.force` **[boolean][7]** force WebP output, otherwise attempt to use input format (optional, default `true`) * `options.delay` **[Array][10]<[number][9]>?** list of delays between animation frames (in milliseconds)
* `options.force` **[boolean][7]** force WebP output, otherwise attempt to use input format (optional, default `true`)
### Examples ### Examples
@@ -253,7 +294,14 @@ const data = await sharp(input)
.toBuffer(); .toBuffer();
``` ```
- Throws **[Error][4]** Invalid options ```javascript
// Optimise the file size of an animated WebP
const outputWebp = await sharp(inputWebp, { animated: true })
.webp({ reductionEffort: 6 })
.toBuffer();
```
* Throws **[Error][4]** Invalid options
Returns **Sharp** Returns **Sharp**
@@ -267,14 +315,16 @@ The prebuilt binaries do not include this - see
### Parameters ### Parameters
- `options` **[Object][6]?** output options * `options` **[Object][6]?** output options
- `options.pageHeight` **[number][9]?** page height for animated output
- `options.loop` **[number][9]** number of animation iterations, use 0 for infinite animation (optional, default `0`)
- `options.delay` **[Array][10]&lt;[number][9]>?** list of delays between animation frames (in milliseconds)
- `options.force` **[boolean][7]** force GIF output, otherwise attempt to use input format (optional, default `true`)
* `options.pageHeight` **[number][9]?** page height for animated output
* `options.loop` **[number][9]** number of animation iterations, use 0 for infinite animation (optional, default `0`)
* `options.delay` **[Array][10]<[number][9]>?** list of delays between animation frames (in milliseconds)
* `options.force` **[boolean][7]** force GIF output, otherwise attempt to use input format (optional, default `true`)
- Throws **[Error][4]** Invalid options <!---->
* Throws **[Error][4]** Invalid options
Returns **Sharp** Returns **Sharp**
@@ -284,18 +334,19 @@ Use these TIFF options for output image.
### Parameters ### Parameters
- `options` **[Object][6]?** output options * `options` **[Object][6]?** output options
- `options.quality` **[number][9]** quality, integer 1-100 (optional, default `80`)
- `options.force` **[boolean][7]** force TIFF output, otherwise attempt to use input format (optional, default `true`) * `options.quality` **[number][9]** quality, integer 1-100 (optional, default `80`)
- `options.compression` **[string][2]** compression options: lzw, deflate, jpeg, ccittfax4 (optional, default `'jpeg'`) * `options.force` **[boolean][7]** force TIFF output, otherwise attempt to use input format (optional, default `true`)
- `options.predictor` **[string][2]** compression predictor options: none, horizontal, float (optional, default `'horizontal'`) * `options.compression` **[string][2]** compression options: lzw, deflate, jpeg, ccittfax4 (optional, default `'jpeg'`)
- `options.pyramid` **[boolean][7]** write an image pyramid (optional, default `false`) * `options.predictor` **[string][2]** compression predictor options: none, horizontal, float (optional, default `'horizontal'`)
- `options.tile` **[boolean][7]** write a tiled tiff (optional, default `false`) * `options.pyramid` **[boolean][7]** write an image pyramid (optional, default `false`)
- `options.tileWidth` **[number][9]** horizontal tile size (optional, default `256`) * `options.tile` **[boolean][7]** write a tiled tiff (optional, default `false`)
- `options.tileHeight` **[number][9]** vertical tile size (optional, default `256`) * `options.tileWidth` **[number][9]** horizontal tile size (optional, default `256`)
- `options.xres` **[number][9]** horizontal resolution in pixels/mm (optional, default `1.0`) * `options.tileHeight` **[number][9]** vertical tile size (optional, default `256`)
- `options.yres` **[number][9]** vertical resolution in pixels/mm (optional, default `1.0`) * `options.xres` **[number][9]** horizontal resolution in pixels/mm (optional, default `1.0`)
- `options.bitdepth` **[number][9]** reduce bitdepth to 1, 2 or 4 bit (optional, default `8`) * `options.yres` **[number][9]** vertical resolution in pixels/mm (optional, default `1.0`)
* `options.bitdepth` **[number][9]** reduce bitdepth to 1, 2 or 4 bit (optional, default `8`)
### Examples ### Examples
@@ -310,7 +361,7 @@ sharp('input.svg')
.then(info => { ... }); .then(info => { ... });
``` ```
- Throws **[Error][4]** Invalid options * Throws **[Error][4]** Invalid options
Returns **Sharp** Returns **Sharp**
@@ -323,20 +374,22 @@ most web browsers do not display these properly.
### Parameters ### Parameters
- `options` **[Object][6]?** output options * `options` **[Object][6]?** output options
- `options.quality` **[number][9]** quality, integer 1-100 (optional, default `50`)
- `options.lossless` **[boolean][7]** use lossless compression (optional, default `false`)
- `options.speed` **[number][9]** CPU effort vs file size, 0 (slowest/smallest) to 8 (fastest/largest) (optional, default `5`)
- `options.chromaSubsampling` **[string][2]** set to '4:4:4' to prevent chroma subsampling otherwise defaults to '4:2:0' chroma subsampling, requires libvips v8.11.0 (optional, default `'4:2:0'`)
* `options.quality` **[number][9]** quality, integer 1-100 (optional, default `50`)
* `options.lossless` **[boolean][7]** use lossless compression (optional, default `false`)
* `options.speed` **[number][9]** CPU effort vs file size, 0 (slowest/smallest) to 8 (fastest/largest) (optional, default `5`)
* `options.chromaSubsampling` **[string][2]** set to '4:4:4' to prevent chroma subsampling otherwise defaults to '4:2:0' chroma subsampling, requires libvips v8.11.0 (optional, default `'4:2:0'`)
- Throws **[Error][4]** Invalid options <!---->
* Throws **[Error][4]** Invalid options
Returns **Sharp** Returns **Sharp**
**Meta** **Meta**
- **since**: 0.27.0 * **since**: 0.27.0
## heif ## heif
@@ -347,21 +400,23 @@ globally-installed libvips compiled with support for libheif, libde265 and x265.
### Parameters ### Parameters
- `options` **[Object][6]?** output options * `options` **[Object][6]?** output options
- `options.quality` **[number][9]** quality, integer 1-100 (optional, default `50`)
- `options.compression` **[string][2]** compression format: av1, hevc (optional, default `'av1'`)
- `options.lossless` **[boolean][7]** use lossless compression (optional, default `false`)
- `options.speed` **[number][9]** CPU effort vs file size, 0 (slowest/smallest) to 8 (fastest/largest) (optional, default `5`)
- `options.chromaSubsampling` **[string][2]** set to '4:4:4' to prevent chroma subsampling otherwise defaults to '4:2:0' chroma subsampling, requires libvips v8.11.0 (optional, default `'4:2:0'`)
* `options.quality` **[number][9]** quality, integer 1-100 (optional, default `50`)
* `options.compression` **[string][2]** compression format: av1, hevc (optional, default `'av1'`)
* `options.lossless` **[boolean][7]** use lossless compression (optional, default `false`)
* `options.speed` **[number][9]** CPU effort vs file size, 0 (slowest/smallest) to 8 (fastest/largest) (optional, default `5`)
* `options.chromaSubsampling` **[string][2]** set to '4:4:4' to prevent chroma subsampling otherwise defaults to '4:2:0' chroma subsampling, requires libvips v8.11.0 (optional, default `'4:2:0'`)
- Throws **[Error][4]** Invalid options <!---->
* Throws **[Error][4]** Invalid options
Returns **Sharp** Returns **Sharp**
**Meta** **Meta**
- **since**: 0.23.0 * **since**: 0.23.0
## raw ## raw
@@ -400,17 +455,19 @@ Warning: multiple sharp instances concurrently producing tile output can expose
### Parameters ### Parameters
- `options` **[Object][6]?** * `options` **[Object][6]?**
- `options.size` **[number][9]** tile size in pixels, a value between 1 and 8192. (optional, default `256`)
- `options.overlap` **[number][9]** tile overlap in pixels, a value between 0 and 8192. (optional, default `0`) * `options.size` **[number][9]** tile size in pixels, a value between 1 and 8192. (optional, default `256`)
- `options.angle` **[number][9]** tile angle of rotation, must be a multiple of 90. (optional, default `0`) * `options.overlap` **[number][9]** tile overlap in pixels, a value between 0 and 8192. (optional, default `0`)
- `options.background` **([string][2] \| [Object][6])** background colour, parsed by the [color][12] module, defaults to white without transparency. (optional, default `{r:255,g:255,b:255,alpha:1}`) * `options.angle` **[number][9]** tile angle of rotation, must be a multiple of 90. (optional, default `0`)
- `options.depth` **[string][2]?** how deep to make the pyramid, possible values are `onepixel`, `onetile` or `one`, default based on layout. * `options.background` **([string][2] | [Object][6])** background colour, parsed by the [color][12] module, defaults to white without transparency. (optional, default `{r:255,g:255,b:255,alpha:1}`)
- `options.skipBlanks` **[number][9]** threshold to skip tile generation, a value 0 - 255 for 8-bit images or 0 - 65535 for 16-bit images (optional, default `-1`) * `options.depth` **[string][2]?** how deep to make the pyramid, possible values are `onepixel`, `onetile` or `one`, default based on layout.
- `options.container` **[string][2]** tile container, with value `fs` (filesystem) or `zip` (compressed file). (optional, default `'fs'`) * `options.skipBlanks` **[number][9]** threshold to skip tile generation, a value 0 - 255 for 8-bit images or 0 - 65535 for 16-bit images (optional, default `-1`)
- `options.layout` **[string][2]** filesystem layout, possible values are `dz`, `iiif`, `zoomify` or `google`. (optional, default `'dz'`) * `options.container` **[string][2]** tile container, with value `fs` (filesystem) or `zip` (compressed file). (optional, default `'fs'`)
- `options.centre` **[boolean][7]** centre image in tile. (optional, default `false`) * `options.layout` **[string][2]** filesystem layout, possible values are `dz`, `iiif`, `zoomify` or `google`. (optional, default `'dz'`)
- `options.center` **[boolean][7]** alternative spelling of centre. (optional, default `false`) * `options.centre` **[boolean][7]** centre image in tile. (optional, default `false`)
* `options.center` **[boolean][7]** alternative spelling of centre. (optional, default `false`)
* `options.id` **[string][2]** when `layout` is `iiif`, sets the `@id` attribute of `info.json` (optional, default `'https://example.com/iiif'`)
### Examples ### Examples
@@ -426,7 +483,7 @@ sharp('input.tiff')
}); });
``` ```
- Throws **[Error][4]** Invalid parameters * Throws **[Error][4]** Invalid parameters
Returns **Sharp** Returns **Sharp**

View File

@@ -6,49 +6,50 @@ Resize image to `width`, `height` or `width x height`.
When both a `width` and `height` are provided, the possible methods by which the image should **fit** these are: When both a `width` and `height` are provided, the possible methods by which the image should **fit** these are:
- `cover`: (default) Preserving aspect ratio, ensure the image covers both provided dimensions by cropping/clipping to fit. * `cover`: (default) Preserving aspect ratio, ensure the image covers both provided dimensions by cropping/clipping to fit.
- `contain`: Preserving aspect ratio, contain within both provided dimensions using "letterboxing" where necessary. * `contain`: Preserving aspect ratio, contain within both provided dimensions using "letterboxing" where necessary.
- `fill`: Ignore the aspect ratio of the input and stretch to both provided dimensions. * `fill`: Ignore the aspect ratio of the input and stretch to both provided dimensions.
- `inside`: Preserving aspect ratio, resize the image to be as large as possible while ensuring its dimensions are less than or equal to both those specified. * `inside`: Preserving aspect ratio, resize the image to be as large as possible while ensuring its dimensions are less than or equal to both those specified.
- `outside`: Preserving aspect ratio, resize the image to be as small as possible while ensuring its dimensions are greater than or equal to both those specified. * `outside`: Preserving aspect ratio, resize the image to be as small as possible while ensuring its dimensions are greater than or equal to both those specified.
Some of these values are based on the [object-fit][1] CSS property. Some of these values are based on the [object-fit][1] CSS property.
When using a `fit` of `cover` or `contain`, the default **position** is `centre`. Other options are: When using a `fit` of `cover` or `contain`, the default **position** is `centre`. Other options are:
- `sharp.position`: `top`, `right top`, `right`, `right bottom`, `bottom`, `left bottom`, `left`, `left top`. * `sharp.position`: `top`, `right top`, `right`, `right bottom`, `bottom`, `left bottom`, `left`, `left top`.
- `sharp.gravity`: `north`, `northeast`, `east`, `southeast`, `south`, `southwest`, `west`, `northwest`, `center` or `centre`. * `sharp.gravity`: `north`, `northeast`, `east`, `southeast`, `south`, `southwest`, `west`, `northwest`, `center` or `centre`.
- `sharp.strategy`: `cover` only, dynamically crop using either the `entropy` or `attention` strategy. * `sharp.strategy`: `cover` only, dynamically crop using either the `entropy` or `attention` strategy.
Some of these values are based on the [object-position][2] CSS property. Some of these values are based on the [object-position][2] CSS property.
The experimental strategy-based approach resizes so one dimension is at its target length 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. 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][3]. * `entropy`: focus on the region with the highest [Shannon entropy][3].
- `attention`: focus on the region with the highest luminance frequency, colour saturation and presence of skin tones. * `attention`: focus on the region with the highest luminance frequency, colour saturation and presence of skin tones.
Possible interpolation kernels are: Possible interpolation kernels are:
- `nearest`: Use [nearest neighbour interpolation][4]. * `nearest`: Use [nearest neighbour interpolation][4].
- `cubic`: Use a [Catmull-Rom spline][5]. * `cubic`: Use a [Catmull-Rom spline][5].
- `mitchell`: Use a [Mitchell-Netravali spline][6]. * `mitchell`: Use a [Mitchell-Netravali spline][6].
- `lanczos2`: Use a [Lanczos kernel][7] with `a=2`. * `lanczos2`: Use a [Lanczos kernel][7] with `a=2`.
- `lanczos3`: Use a Lanczos kernel with `a=3` (the default). * `lanczos3`: Use a Lanczos kernel with `a=3` (the default).
### Parameters ### Parameters
- `width` **[number][8]?** pixels wide the resultant image should be. Use `null` or `undefined` to auto-scale the width to match the height. * `width` **[number][8]?** pixels wide the resultant image should be. Use `null` or `undefined` to auto-scale the width to match the height.
- `height` **[number][8]?** pixels high the resultant image should be. Use `null` or `undefined` to auto-scale the height to match the width. * `height` **[number][8]?** pixels high the resultant image should be. Use `null` or `undefined` to auto-scale the height to match the width.
- `options` **[Object][9]?** * `options` **[Object][9]?**
- `options.width` **[String][10]?** alternative means of specifying `width`. If both are present this take priority.
- `options.height` **[String][10]?** alternative means of specifying `height`. If both are present this take priority. * `options.width` **[String][10]?** alternative means of specifying `width`. If both are present this take priority.
- `options.fit` **[String][10]** how the image should be resized to fit both provided dimensions, one of `cover`, `contain`, `fill`, `inside` or `outside`. (optional, default `'cover'`) * `options.height` **[String][10]?** alternative means of specifying `height`. If both are present this take priority.
- `options.position` **[String][10]** position, gravity or strategy to use when `fit` is `cover` or `contain`. (optional, default `'centre'`) * `options.fit` **[String][10]** how the image should be resized to fit both provided dimensions, one of `cover`, `contain`, `fill`, `inside` or `outside`. (optional, default `'cover'`)
- `options.background` **([String][10] \| [Object][9])** background colour when using a `fit` of `contain`, parsed by the [color][11] module, defaults to black without transparency. (optional, default `{r:0,g:0,b:0,alpha:1}`) * `options.position` **[String][10]** position, gravity or strategy to use when `fit` is `cover` or `contain`. (optional, default `'centre'`)
- `options.kernel` **[String][10]** the kernel to use for image reduction. (optional, default `'lanczos3'`) * `options.background` **([String][10] | [Object][9])** background colour when using a `fit` of `contain`, parsed by the [color][11] module, defaults to black without transparency. (optional, default `{r:0,g:0,b:0,alpha:1}`)
- `options.withoutEnlargement` **[Boolean][12]** do not enlarge if the width _or_ height are already less than the specified dimensions, equivalent to GraphicsMagick's `>` geometry option. (optional, default `false`) * `options.kernel` **[String][10]** the kernel to use for image reduction. (optional, default `'lanczos3'`)
- `options.fastShrinkOnLoad` **[Boolean][12]** 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`) * `options.withoutEnlargement` **[Boolean][12]** do not enlarge if the width *or* height are already less than the specified dimensions, equivalent to GraphicsMagick's `>` geometry option. (optional, default `false`)
* `options.fastShrinkOnLoad` **[Boolean][12]** 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
@@ -125,7 +126,7 @@ const scaleByHalf = await sharp(input)
); );
``` ```
- Throws **[Error][13]** Invalid parameters * Throws **[Error][13]** Invalid parameters
Returns **Sharp** Returns **Sharp**
@@ -136,12 +137,13 @@ This operation will always occur after resizing and extraction, if any.
### Parameters ### Parameters
- `extend` **([number][8] \| [Object][9])** single pixel count to add to all edges or an Object with per-edge counts * `extend` **([number][8] | [Object][9])** single pixel count to add to all edges or an Object with per-edge counts
- `extend.top` **[number][8]?**
- `extend.left` **[number][8]?** * `extend.top` **[number][8]** (optional, default `0`)
- `extend.bottom` **[number][8]?** * `extend.left` **[number][8]** (optional, default `0`)
- `extend.right` **[number][8]?** * `extend.bottom` **[number][8]** (optional, default `0`)
- `extend.background` **([String][10] \| [Object][9])** background colour, parsed by the [color][11] module, defaults to black without transparency. (optional, default `{r:0,g:0,b:0,alpha:1}`) * `extend.right` **[number][8]** (optional, default `0`)
* `extend.background` **([String][10] | [Object][9])** background colour, parsed by the [color][11] module, defaults to black without transparency. (optional, default `{r:0,g:0,b:0,alpha:1}`)
### Examples ### Examples
@@ -160,7 +162,17 @@ sharp(input)
... ...
``` ```
- Throws **[Error][13]** Invalid parameters ```javascript
// Add a row of 10 red pixels to the bottom
sharp(input)
.extend({
bottom: 10,
background: 'red'
})
...
```
* Throws **[Error][13]** Invalid parameters
Returns **Sharp** Returns **Sharp**
@@ -168,17 +180,18 @@ Returns **Sharp**
Extract/crop a region of the image. Extract/crop a region of the image.
- Use `extract` before `resize` for pre-resize extraction. * Use `extract` before `resize` for pre-resize extraction.
- Use `extract` after `resize` for post-resize extraction. * Use `extract` after `resize` for post-resize extraction.
- Use `extract` before and after for both. * Use `extract` before and after for both.
### Parameters ### Parameters
- `options` **[Object][9]** describes the region to extract using integral pixel values * `options` **[Object][9]** describes the region to extract using integral pixel values
- `options.left` **[number][8]** zero-indexed offset from left edge
- `options.top` **[number][8]** zero-indexed offset from top edge * `options.left` **[number][8]** zero-indexed offset from left edge
- `options.width` **[number][8]** width of region to extract * `options.top` **[number][8]** zero-indexed offset from top edge
- `options.height` **[number][8]** height of region to extract * `options.width` **[number][8]** width of region to extract
* `options.height` **[number][8]** height of region to extract
### Examples ### Examples
@@ -200,7 +213,7 @@ sharp(input)
}); });
``` ```
- Throws **[Error][13]** Invalid parameters * Throws **[Error][13]** Invalid parameters
Returns **Sharp** Returns **Sharp**
@@ -214,10 +227,11 @@ will contain `trimOffsetLeft` and `trimOffsetTop` properties.
### Parameters ### Parameters
- `threshold` **[number][8]** the allowed difference from the top-left pixel, a number greater than zero. (optional, default `10`) * `threshold` **[number][8]** the allowed difference from the top-left pixel, a number greater than zero. (optional, default `10`)
<!---->
- Throws **[Error][13]** Invalid parameters * Throws **[Error][13]** Invalid parameters
Returns **Sharp** Returns **Sharp**

View File

@@ -54,17 +54,18 @@ console.log(sharp.versions);
## cache ## cache
Gets or, when options are provided, sets the limits of _libvips'_ operation cache. Gets or, when options are provided, sets the limits of *libvips'* operation cache.
Existing entries in the cache will be trimmed after any change in limits. Existing entries in the cache will be trimmed after any change in limits.
This method always returns cache statistics, This method always returns cache statistics,
useful for determining how much working memory is required for a particular task. useful for determining how much working memory is required for a particular task.
### Parameters ### Parameters
- `options` **([Object][1] \| [boolean][10])** Object with the following attributes, or boolean where true uses default cache settings and false removes all caching (optional, default `true`) * `options` **([Object][1] | [boolean][10])** Object with the following attributes, or boolean where true uses default cache settings and false removes all caching (optional, default `true`)
- `options.memory` **[number][11]** is the maximum memory in MB to use for this cache (optional, default `50`)
- `options.files` **[number][11]** is the maximum number of files to hold open (optional, default `20`) * `options.memory` **[number][11]** is the maximum memory in MB to use for this cache (optional, default `50`)
- `options.items` **[number][11]** is the maximum number of operations to cache (optional, default `100`) * `options.files` **[number][11]** is the maximum number of files to hold open (optional, default `20`)
* `options.items` **[number][11]** is the maximum number of operations to cache (optional, default `100`)
### Examples ### Examples
@@ -83,9 +84,13 @@ Returns **[Object][1]**
## concurrency ## concurrency
Gets or, when a concurrency is provided, sets Gets or, when a concurrency is provided, sets
the number of threads _libvips'_ should create to process each image. the number of threads *libvips'* should create to process each image.
The default value is the number of CPU cores.
A value of `0` will reset to this default. The default value is the number of CPU cores,
except when using glibc-based Linux without jemalloc,
where the default is `1` to help reduce memory fragmentation.
A value of `0` will reset this to the number of CPU cores.
The maximum number of images that can be processed in parallel The maximum number of images that can be processed in parallel
is limited by libuv's `UV_THREADPOOL_SIZE` environment variable. is limited by libuv's `UV_THREADPOOL_SIZE` environment variable.
@@ -94,7 +99,7 @@ This method always returns the current concurrency.
### Parameters ### Parameters
- `concurrency` **[number][11]?** * `concurrency` **[number][11]?**
### Examples ### Examples
@@ -110,8 +115,8 @@ Returns **[number][11]** concurrency
An EventEmitter that emits a `change` event when a task is either: An EventEmitter that emits a `change` event when a task is either:
- queued, waiting for _libuv_ to provide a worker thread * queued, waiting for *libuv* to provide a worker thread
- complete * complete
### Examples ### Examples
@@ -125,8 +130,8 @@ sharp.queue.on('change', function(queueLength) {
Provides access to internal task counters. 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. * 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. * process is the number of resize tasks currently being processed.
### Examples ### Examples
@@ -146,7 +151,7 @@ by taking advantage of the SIMD vector unit of the CPU, e.g. Intel SSE and ARM N
### Parameters ### Parameters
- `simd` **[boolean][10]** (optional, default `true`) * `simd` **[boolean][10]** (optional, default `true`)
### Examples ### Examples

View File

@@ -1,5 +1,76 @@
# Changelog # Changelog
## v0.28 - *bijou*
Requires libvips v8.10.6
### v0.28.2 - 10th May 2021
* Allow `withMetadata` to set `density`.
[#967](https://github.com/lovell/sharp/issues/967)
* Skip shrink-on-load where one dimension <4px.
[#2653](https://github.com/lovell/sharp/issues/2653)
* Allow escaped proxy credentials.
[#2664](https://github.com/lovell/sharp/pull/2664)
[@msalettes](https://github.com/msalettes)
* Add `premultiplied` flag for raw pixel data input.
[#2685](https://github.com/lovell/sharp/pull/2685)
[@mnutt](https://github.com/mnutt)
* Detect empty input and throw a helpful error.
[#2687](https://github.com/lovell/sharp/pull/2687)
[@JakobJingleheimer](https://github.com/JakobJingleheimer)
* Add install-time flag to skip version compatibility checks.
[#2692](https://github.com/lovell/sharp/pull/2692)
[@xemle](https://github.com/xemle)
### v0.28.1 - 5th April 2021
* Ensure all installation errors are logged with a more obvious prefix.
* Allow `withMetadata` to set and update EXIF metadata.
[#650](https://github.com/lovell/sharp/issues/650)
* Add support for OME-TIFF Sub Image File Directories (subIFD).
[#2557](https://github.com/lovell/sharp/issues/2557)
* Allow `ensureAlpha` to set the alpha transparency level.
[#2634](https://github.com/lovell/sharp/issues/2634)
### v0.28.0 - 29th March 2021
* Prebuilt binaries now include mozjpeg and libimagequant (BSD 2-Clause).
* Prebuilt binaries limit AVIF support to the most common 8-bit depth.
* Add `mozjpeg` option to `jpeg` method, sets mozjpeg defaults.
* Reduce the default PNG `compressionLevel` to the more commonly used 6.
* Reduce concurrency on glibc-based Linux when using the default memory allocator to help prevent fragmentation.
* Default missing edge properties of extend operation to zero.
[#2578](https://github.com/lovell/sharp/issues/2578)
* Ensure composite does not clip top and left offsets.
[#2594](https://github.com/lovell/sharp/pull/2594)
[@SHG42](https://github.com/SHG42)
* Improve error handling of network failure at install time.
[#2608](https://github.com/lovell/sharp/pull/2608)
[@abradley](https://github.com/abradley)
* Ensure `@id` attribute can be set for IIIF tile-based output.
[#2612](https://github.com/lovell/sharp/issues/2612)
[@edsilv](https://github.com/edsilv)
* Ensure composite replicates the correct number of tiles for centred gravities.
[#2626](https://github.com/lovell/sharp/issues/2626)
## v0.27 - *avif* ## v0.27 - *avif*
Requires libvips v8.10.5 Requires libvips v8.10.5

View File

@@ -209,3 +209,6 @@ GitHub: https://github.com/beig
Name: Florian Busch Name: Florian Busch
GitHub: https://github.com/florian-busch GitHub: https://github.com/florian-busch
Name: Matthieu Salettes
GitHub: https://github.com/msalettes

View File

@@ -19,7 +19,7 @@
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="https://pixel.plumbing/px/72x72/sharp-logo.svg"> <link rel="apple-touch-icon-precomposed" sizes="72x72" href="https://pixel.plumbing/px/72x72/sharp-logo.svg">
<link rel="apple-touch-icon-precomposed" href="https://pixel.plumbing/px/57x57/sharp-logo.svg"> <link rel="apple-touch-icon-precomposed" href="https://pixel.plumbing/px/57x57/sharp-logo.svg">
<link rel="author" href="/humans.txt" type="text/plain"> <link rel="author" href="/humans.txt" type="text/plain">
<link rel="preload" href="https://cdn.jsdelivr.net/gh/lovell/sharp@v0.27.2/docs/README.md" as="fetch" type="text/markdown" crossorigin> <link rel="preload" href="https://cdn.jsdelivr.net/gh/lovell/sharp@v0.28.2/docs/README.md" as="fetch" type="text/markdown" crossorigin>
<link rel="preload" href="https://cdn.jsdelivr.net/gh/lovell/sharp@master/docs/image/sharp-logo.svg" as="image" type="image/svg+xml" crossorigin> <link rel="preload" href="https://cdn.jsdelivr.net/gh/lovell/sharp@master/docs/image/sharp-logo.svg" as="image" type="image/svg+xml" crossorigin>
<link rel="dns-prefetch" href="https://pixel.plumbing"> <link rel="dns-prefetch" href="https://pixel.plumbing">
<link rel="dns-prefetch" href="https://www.google-analytics.com"> <link rel="dns-prefetch" href="https://www.google-analytics.com">
@@ -139,7 +139,7 @@
docuteApiTitlePlugin, docuteApiTitlePlugin,
docuteApiSearchPlugin docuteApiSearchPlugin
], ],
sourcePath: 'https://cdn.jsdelivr.net/gh/lovell/sharp@v0.27.2/docs', sourcePath: 'https://cdn.jsdelivr.net/gh/lovell/sharp@v0.28.2/docs',
nav: [ nav: [
{ {
title: 'Funding', title: 'Funding',

View File

@@ -18,12 +18,12 @@ Ready-compiled sharp and libvips binaries are provided for use with
Node.js v10+ on the most common platforms: Node.js v10+ on the most common platforms:
* macOS x64 (>= 10.13) * macOS x64 (>= 10.13)
* Linux x64 (glibc >= 2.17, musl >=1.1.24 <1.2.0) * Linux x64 (glibc >= 2.17, musl >= 1.1.24)
* Linux ARM64 (glibc >= 2.29) * Linux ARM64 (glibc >= 2.29, musl >= 1.1.24)
* Windows x64 * Windows x64
* Windows x86 * Windows x86
A ~9MB tarball containing libvips and its most commonly used dependencies An ~7.5MB tarball containing libvips and its most commonly used dependencies
is downloaded via HTTPS and stored within `node_modules/sharp/vendor` during `npm install`. is downloaded via HTTPS and stored within `node_modules/sharp/vendor` during `npm install`.
This provides support for the This provides support for the
@@ -31,16 +31,16 @@ JPEG, PNG, WebP, AVIF, TIFF, GIF (input) and SVG (input) image formats.
The following platforms have prebuilt libvips but not sharp: The following platforms have prebuilt libvips but not sharp:
* macOS ARM64
* Linux ARMv6 * Linux ARMv6
* Linux ARMv7 (glibc >= 2.28) * Linux ARMv7 (glibc >= 2.28)
* Windows ARM64 * Windows ARM64
The following platforms require compilation of both libvips and sharp from source: The following platforms require compilation of both libvips and sharp from source:
* macOS ARM64
* Linux x86 * Linux x86
* Linux x64 (glibc <= 2.16, includes RHEL/CentOS 6) * Linux x64 (glibc <= 2.16, includes RHEL/CentOS 6)
* Linux ARM64 (glibc <= 2.28, musl) * Linux ARM64 (glibc <= 2.28)
* Linux PowerPC * Linux PowerPC
* FreeBSD * FreeBSD
* OpenBSD * OpenBSD
@@ -49,6 +49,7 @@ The following platforms require compilation of both libvips and sharp from sourc
The architecture and platform of Node.js used for `npm install` The architecture and platform of Node.js used for `npm install`
must be the same as the architecture and platform of Node.js used at runtime. must be the same as the architecture and platform of Node.js used at runtime.
See the [cross-platform](#cross-platform) section if this is not the case.
When using npm v6 or earlier, the `npm install --unsafe-perm` flag must be used when installing as `root` or a `sudo` user. When using npm v6 or earlier, the `npm install --unsafe-perm` flag must be used when installing as `root` or a `sudo` user.
@@ -60,21 +61,42 @@ Check the output of running `npm install --verbose sharp` for useful error messa
## Apple M1 ## Apple M1
If you are using ARM64 Node.js, which can be checked using: Prebuilt libvips binaries are provided for macOS on ARM64 (since sharp v0.28.0).
```sh During `npm install` sharp will be built locally,
node -p "process.arch === 'arm64'" which requires Xcode and Python - see
``` [building from source](#building-from-source).
then libvips must currently be installed via Homebrew before installing sharp.
```sh
brew install vips
```
When this new ARM64 CPU is made freely available When this new ARM64 CPU is made freely available
to open source projects via a CI service to open source projects via a CI service
then prebuilt binaries can be provided. then prebuilt sharp binaries can also be provided.
## Cross-platform
At `npm install` time, prebuilt binaries are automatically selected for the
current OS platform and CPU architecture, where available.
The target platform and/or architecture can be manually selected using the following flags.
```sh
npm install --platform=... --arch=... --arm-version=... sharp
```
* `--platform`: one of `linux`, `linuxmusl`, `darwin` or `win32`.
* `--arch`: one of `x64`, `ia32`, `arm` or `arm64`.
* `--arm-version`: one of `6`, `7` or `8` (`arm` defaults to `6`, `arm64` defaults to `8`).
* `--sharp-install-force`: skip version compatibility checks.
These values can also be set via environment variables,
`npm_config_platform`, `npm_config_arch`, `npm_config_arm_version`
and `SHARP_INSTALL_FORCE` respectively.
For example, if the target machine has a 64-bit ARM CPU and is running Alpine Linux,
use the following flags:
```sh
npm install --arch=arm64 --platform=linuxmusl sharp
```
## Custom libvips ## Custom libvips
@@ -108,6 +130,10 @@ To install the prebuilt sharp binaries from a custom URL,
set the `sharp_binary_host` npm config option set the `sharp_binary_host` npm config option
or the `npm_config_sharp_binary_host` environment variable. or the `npm_config_sharp_binary_host` environment variable.
To install the prebuilt sharp binaries from a directory on the local filesystem,
set the `sharp_local_prebuilds` npm config option
or the `npm_config_sharp_local_prebuilds` environment variable.
To install the prebuilt libvips binaries from a custom URL, To install the prebuilt libvips binaries from a custom URL,
set the `sharp_libvips_binary_host` npm config option set the `sharp_libvips_binary_host` npm config option
or the `npm_config_sharp_libvips_binary_host` environment variable. or the `npm_config_sharp_libvips_binary_host` environment variable.
@@ -125,7 +151,7 @@ See the Chinese mirror below for a further example.
## Chinese mirror ## Chinese mirror
Alibaba provide a mirror site based in China containing binaries for both sharp and libvips. A mirror site based in China, provided by Alibaba, contains binaries for both sharp and libvips.
To use this either set the following configuration: To use this either set the following configuration:
@@ -155,6 +181,23 @@ pkg install -y pkgconf vips
cd /usr/ports/graphics/vips/ && make install clean cd /usr/ports/graphics/vips/ && make install clean
``` ```
## Linux memory allocator
The default memory allocator on most glibc-based Linux systems
(e.g. Debian, Red Hat) is unsuitable for long-running, multi-threaded
processes that involve lots of small memory allocations.
For this reason, by default, sharp will limit the use of thread-based
[concurrency](api-utility#concurrency) when the glibc allocator is
detected at runtime.
To help avoid fragmentation and improve performance on these systems,
the use of an alternative memory allocator such as
[jemalloc](https://github.com/jemalloc/jemalloc) is recommended.
Those using musl-based Linux (e.g. Alpine) and non-Linux systems are
unaffected.
## Heroku ## Heroku
Add the Add the
@@ -167,8 +210,6 @@ to `false` when using the `yarn` package manager.
## AWS Lambda ## AWS Lambda
Set the Lambda runtime to `nodejs12.x`.
The binaries in the `node_modules` directory of the The binaries in the `node_modules` directory of the
[deployment package](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-package.html) [deployment package](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-package.html)
must be for the Linux x64 platform. must be for the Linux x64 platform.
@@ -219,22 +260,6 @@ until after all threads are complete.
## Known conflicts ## Known conflicts
### Electron and Linux
The prebuilt binaries provided by Electron for Linux depend on many shared system libraries.
One of these, `libgobject-2.0.so`,
is known to conflict with the statically-linked binaries provided by sharp
and the following error can occur:
```
basic_string::_S_construct null not valid
```
To workaround this, set the `LD_PRELOAD` environment variable before the `electron` binary is run.
```sh
LD_PRELOAD=node_modules/sharp/vendor/8.10.5/lib/libvips.so.42 electron script.js
```
### Canvas and Windows ### Canvas and Windows
The prebuilt binaries provided by `canvas` for Windows depend on the unmaintained GTK 2, last updated in 2011. The prebuilt binaries provided by `canvas` for Windows depend on the unmaintained GTK 2, last updated in 2011.

View File

@@ -5,10 +5,10 @@ A test to benchmark the performance of this module relative to alternatives.
## The contenders ## The contenders
* [jimp](https://www.npmjs.com/package/jimp) v0.16.1 - Image processing in pure JavaScript. Provides bicubic interpolation. * [jimp](https://www.npmjs.com/package/jimp) v0.16.1 - Image processing in pure JavaScript. Provides bicubic interpolation.
* [mapnik](https://www.npmjs.org/package/mapnik) v4.5.5 - Whilst primarily a map renderer, Mapnik contains bitmap image utilities. * [mapnik](https://www.npmjs.org/package/mapnik) v4.5.6 - Whilst primarily a map renderer, Mapnik contains bitmap image utilities.
* [imagemagick](https://www.npmjs.com/package/imagemagick) v0.1.3 - Supports filesystem only and "*has been unmaintained for a long time*". * [imagemagick](https://www.npmjs.com/package/imagemagick) v0.1.3 - Supports filesystem only and "*has been unmaintained for a long time*".
* [gm](https://www.npmjs.com/package/gm) v1.23.1 - Fully featured wrapper around GraphicsMagick's `gm` command line utility. * [gm](https://www.npmjs.com/package/gm) v1.23.1 - Fully featured wrapper around GraphicsMagick's `gm` command line utility.
* sharp v0.27.0 / libvips v8.10.5 - Caching within libvips disabled to ensure a fair comparison. * sharp v0.28.0 / libvips v8.10.6 - Caching within libvips disabled to ensure a fair comparison.
## The task ## The task
@@ -18,25 +18,25 @@ then compress to JPEG at a "quality" setting of 80.
## Test environment ## Test environment
* AWS EC2 eu-west-1 [c5d.large](https://aws.amazon.com/ec2/instance-types/c5/) (2x Xeon Platinum 8275CL CPU @ 3.00GHz) * AWS EC2 eu-west-1 [c5ad.xlarge](https://aws.amazon.com/ec2/instance-types/c5/) (4x AMD EPYC 7R32)
* Ubuntu 20.10 (ami-046cdbcee95cdd75c) * Ubuntu 20.10 (ami-03f10415e8b0bfb86)
* Node.js v14.15.3 * Node.js v14.16.0
## Results ## Results
| Module | Input | Output | Ops/sec | Speed-up | | Module | Input | Output | Ops/sec | Speed-up |
| :----------------- | :----- | :----- | ------: | -------: | | :----------------- | :----- | :----- | ------: | -------: |
| jimp | buffer | buffer | 0.77 | 1.0 | | jimp | buffer | buffer | 0.78 | 1.0 |
| mapnik | buffer | buffer | 3.39 | 4.4 | | mapnik | buffer | buffer | 3.39 | 4.3 |
| gm | buffer | buffer | 4.30 | 5.6 | | gm | buffer | buffer | 7.84 | 10.1 |
| gm | file | file | 4.33 | 5.6 | | gm | file | file | 9.24 | 11.8 |
| imagemagick | file | file | 4.39 | 5.7 | | imagemagick | file | file | 9.37 | 12.0 |
| sharp | stream | stream | 23.81 | 30.9 | | sharp | stream | stream | 26.84 | 34.4 |
| sharp | file | file | 25.09 | 32.6 | | sharp | file | file | 29.76 | 38.2 |
| sharp | buffer | buffer | 25.60 | 33.2 | | sharp | buffer | buffer | 31.60 | 40.5 |
Greater libvips performance can be expected with caching enabled (default) Greater libvips performance can be expected with caching enabled (default)
and using 4+ core machines, especially those with larger L1/L2 CPU caches. and using 8+ core machines, especially those with larger L1/L2 CPU caches.
The I/O limits of the relevant (de)compression library will generally determine maximum throughput. The I/O limits of the relevant (de)compression library will generally determine maximum throughput.
@@ -51,7 +51,7 @@ brew install mapnik
``` ```
```sh ```sh
sudo apt-get install imagemagick libmagick++-dev graphicsmagick libmapnik-dev sudo apt-get install build-essential imagemagick libmagick++-dev graphicsmagick libmapnik-dev
``` ```
```sh ```sh
@@ -61,7 +61,7 @@ sudo yum install ImageMagick-devel ImageMagick-c++-devel GraphicsMagick mapnik-d
```sh ```sh
git clone https://github.com/lovell/sharp.git git clone https://github.com/lovell/sharp.git
cd sharp cd sharp
npm install npm install --build-from-source
cd test/bench cd test/bench
npm install npm install
npm test npm test

File diff suppressed because one or more lines are too long

View File

@@ -8,7 +8,7 @@ const extractDescription = (str) =>
.replace(/\s+/g, ' ') .replace(/\s+/g, ' ')
.replace(/[^A-Za-z0-9_/\-,. ]/g, '') .replace(/[^A-Za-z0-9_/\-,. ]/g, '')
.replace(/\s+/g, ' ') .replace(/\s+/g, ' ')
.substr(0, 140) .substr(0, 180)
.trim(); .trim();
const extractKeywords = (str) => const extractKeywords = (str) =>

View File

@@ -4,14 +4,19 @@ module.exports = [
'about', 'about',
'after', 'after',
'all', 'all',
'allows',
'already', 'already',
'also',
'alternative', 'alternative',
'always', 'always',
'and', 'and',
'any', 'any',
'are', 'are',
'based',
'been', 'been',
'before', 'before',
'both',
'call',
'can', 'can',
'containing', 'containing',
'default', 'default',
@@ -30,7 +35,10 @@ module.exports = [
'have', 'have',
'how', 'how',
'image', 'image',
'involve',
'its', 'its',
'least',
'lots',
'may', 'may',
'more', 'more',
'most', 'most',
@@ -40,17 +48,24 @@ module.exports = [
'not', 'not',
'occur', 'occur',
'occurs', 'occurs',
'options',
'over', 'over',
'perform', 'perform',
'performs', 'performs',
'provide', 'provide',
'provided', 'provided',
'ready',
'same',
'see',
'set', 'set',
'sets', 'sets',
'should', 'should',
'since',
'spelling', 'spelling',
'such',
'support', 'support',
'supported', 'supported',
'sure',
'take', 'take',
'that', 'that',
'the', 'the',
@@ -59,6 +74,9 @@ module.exports = [
'therefore', 'therefore',
'these', 'these',
'this', 'this',
'under',
'unless',
'until',
'use', 'use',
'used', 'used',
'using', 'using',
@@ -69,5 +87,6 @@ module.exports = [
'while', 'while',
'will', 'will',
'with', 'with',
'without' 'without',
'you'
]; ];

View File

@@ -4,7 +4,6 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const libvips = require('../lib/libvips'); const libvips = require('../lib/libvips');
const npmLog = require('npmlog');
const minimumLibvipsVersion = libvips.minimumLibvipsVersion; const minimumLibvipsVersion = libvips.minimumLibvipsVersion;
@@ -12,13 +11,13 @@ const platform = process.env.npm_config_platform || process.platform;
if (platform === 'win32') { if (platform === 'win32') {
const buildDir = path.join(__dirname, '..', 'build'); const buildDir = path.join(__dirname, '..', 'build');
const buildReleaseDir = path.join(buildDir, 'Release'); const buildReleaseDir = path.join(buildDir, 'Release');
npmLog.info('sharp', `Creating ${buildReleaseDir}`); libvips.log(`Creating ${buildReleaseDir}`);
try { try {
libvips.mkdirSync(buildDir); libvips.mkdirSync(buildDir);
libvips.mkdirSync(buildReleaseDir); libvips.mkdirSync(buildReleaseDir);
} catch (err) {} } catch (err) {}
const vendorLibDir = path.join(__dirname, '..', 'vendor', minimumLibvipsVersion, 'lib'); const vendorLibDir = path.join(__dirname, '..', 'vendor', minimumLibvipsVersion, 'lib');
npmLog.info('sharp', `Copying DLLs from ${vendorLibDir} to ${buildReleaseDir}`); libvips.log(`Copying DLLs from ${vendorLibDir} to ${buildReleaseDir}`);
try { try {
fs fs
.readdirSync(vendorLibDir) .readdirSync(vendorLibDir)
@@ -32,6 +31,7 @@ if (platform === 'win32') {
); );
}); });
} catch (err) { } catch (err) {
npmLog.error('sharp', err.message); libvips.log(err);
process.exit(1);
} }
} }

View File

@@ -7,8 +7,8 @@ const stream = require('stream');
const zlib = require('zlib'); const zlib = require('zlib');
const detectLibc = require('detect-libc'); const detectLibc = require('detect-libc');
const npmLog = require('npmlog'); const semverLessThan = require('semver/functions/lt');
const semver = require('semver'); const semverSatisfies = require('semver/functions/satisfies');
const simpleGet = require('simple-get'); const simpleGet = require('simple-get');
const tarFs = require('tar-fs'); const tarFs = require('tar-fs');
@@ -22,34 +22,59 @@ const minimumGlibcVersionByArch = {
x64: '2.17' x64: '2.17'
}; };
const hasSharpPrebuild = [
'darwin-x64',
'linux-arm64',
'linux-x64',
'linuxmusl-x64',
'linuxmusl-arm64',
'win32-ia32',
'win32-x64'
];
const { minimumLibvipsVersion, minimumLibvipsVersionLabelled } = libvips; const { minimumLibvipsVersion, minimumLibvipsVersionLabelled } = libvips;
const distHost = process.env.npm_config_sharp_libvips_binary_host || 'https://github.com/lovell/sharp-libvips/releases/download'; const distHost = process.env.npm_config_sharp_libvips_binary_host || 'https://github.com/lovell/sharp-libvips/releases/download';
const distBaseUrl = process.env.npm_config_sharp_dist_base_url || process.env.SHARP_DIST_BASE_URL || `${distHost}/v${minimumLibvipsVersionLabelled}/`; const distBaseUrl = process.env.npm_config_sharp_dist_base_url || process.env.SHARP_DIST_BASE_URL || `${distHost}/v${minimumLibvipsVersionLabelled}/`;
const supportsBrotli = ('BrotliDecompress' in zlib); const supportsBrotli = ('BrotliDecompress' in zlib);
const installationForced = !!(process.env.npm_config_sharp_install_force || process.env.SHARP_INSTALL_FORCE);
const fail = function (err) { const fail = function (err) {
npmLog.error('sharp', err.message); libvips.log(err);
if (err.code === 'EACCES') { if (err.code === 'EACCES') {
npmLog.info('sharp', 'Are you trying to install as a root or sudo user? Try again with the --unsafe-perm flag'); libvips.log('Are you trying to install as a root or sudo user? Try again with the --unsafe-perm flag');
} }
npmLog.info('sharp', 'Attempting to build from source via node-gyp but this may fail due to the above error'); libvips.log('Attempting to build from source via node-gyp but this may fail due to the above error');
npmLog.info('sharp', 'Please see https://sharp.pixelplumbing.com/install for required dependencies'); libvips.log('Please see https://sharp.pixelplumbing.com/install for required dependencies');
process.exit(1); process.exit(1);
}; };
const extractTarball = function (tarPath) { const handleError = function (err) {
if (installationForced) {
libvips.log(`Installation warning: ${err.message}`);
} else {
throw err;
}
};
const extractTarball = function (tarPath, platformAndArch) {
const vendorPath = path.join(__dirname, '..', 'vendor'); const vendorPath = path.join(__dirname, '..', 'vendor');
libvips.mkdirSync(vendorPath); libvips.mkdirSync(vendorPath);
const versionedVendorPath = path.join(vendorPath, minimumLibvipsVersion); const versionedVendorPath = path.join(vendorPath, minimumLibvipsVersion);
libvips.mkdirSync(versionedVendorPath); libvips.mkdirSync(versionedVendorPath);
const ignoreVendorInclude = hasSharpPrebuild.includes(platformAndArch) && !process.env.npm_config_build_from_source;
const ignore = function (name) {
return ignoreVendorInclude && name.includes('include/');
};
stream.pipeline( stream.pipeline(
fs.createReadStream(tarPath), fs.createReadStream(tarPath),
supportsBrotli ? new zlib.BrotliDecompress() : new zlib.Gunzip(), supportsBrotli ? new zlib.BrotliDecompress() : new zlib.Gunzip(),
tarFs.extract(versionedVendorPath), tarFs.extract(versionedVendorPath, { ignore }),
function (err) { function (err) {
if (err) { if (err) {
if (/unexpected end of file/.test(err.message)) { if (/unexpected end of file/.test(err.message)) {
npmLog.error('sharp', `Please delete ${tarPath} as it is not a valid tarball`); fail(new Error(`Please delete ${tarPath} as it is not a valid tarball`));
} }
fail(err); fail(err);
} }
@@ -62,11 +87,11 @@ try {
if (useGlobalLibvips) { if (useGlobalLibvips) {
const globalLibvipsVersion = libvips.globalLibvipsVersion(); const globalLibvipsVersion = libvips.globalLibvipsVersion();
npmLog.info('sharp', `Detected globally-installed libvips v${globalLibvipsVersion}`); libvips.log(`Detected globally-installed libvips v${globalLibvipsVersion}`);
npmLog.info('sharp', 'Building from source via node-gyp'); libvips.log('Building from source via node-gyp');
process.exit(1); process.exit(1);
} else if (libvips.hasVendoredLibvips()) { } else if (libvips.hasVendoredLibvips()) {
npmLog.info('sharp', `Using existing vendored libvips v${minimumLibvipsVersion}`); libvips.log(`Using existing vendored libvips v${minimumLibvipsVersion}`);
} else { } else {
// Is this arch/platform supported? // Is this arch/platform supported?
const arch = process.env.npm_config_arch || process.arch; const arch = process.env.npm_config_arch || process.arch;
@@ -80,20 +105,21 @@ try {
if (platformAndArch === 'freebsd-x64' || platformAndArch === 'openbsd-x64' || platformAndArch === 'sunos-x64') { if (platformAndArch === 'freebsd-x64' || platformAndArch === 'openbsd-x64' || platformAndArch === 'sunos-x64') {
throw new Error(`BSD/SunOS systems require manual installation of libvips >= ${minimumLibvipsVersion}`); throw new Error(`BSD/SunOS systems require manual installation of libvips >= ${minimumLibvipsVersion}`);
} }
if (detectLibc.family === detectLibc.GLIBC && detectLibc.version) { // Linux libc version check
if (semver.lt(`${detectLibc.version}.0`, `${minimumGlibcVersionByArch[arch]}.0`)) { if (detectLibc.family === detectLibc.GLIBC && detectLibc.version && minimumGlibcVersionByArch[arch]) {
throw new Error(`Use with glibc ${detectLibc.version} requires manual installation of libvips >= ${minimumLibvipsVersion}`); if (semverLessThan(`${detectLibc.version}.0`, `${minimumGlibcVersionByArch[arch]}.0`)) {
handleError(new Error(`Use with glibc ${detectLibc.version} requires manual installation of libvips >= ${minimumLibvipsVersion}`));
} }
} }
if (detectLibc.family === detectLibc.MUSL && detectLibc.version) { if (detectLibc.family === detectLibc.MUSL && detectLibc.version) {
if (!semver.satisfies(detectLibc.version, '>=1.1.24 <1.2.0')) { if (semverLessThan(detectLibc.version, '1.1.24')) {
throw new Error(`Use with musl ${detectLibc.version} requires manual installation of libvips >= ${minimumLibvipsVersion}`); handleError(new Error(`Use with musl ${detectLibc.version} requires manual installation of libvips >= ${minimumLibvipsVersion}`));
} }
} }
// Node.js minimum version check
const supportedNodeVersion = process.env.npm_package_engines_node || require('../package.json').engines.node; const supportedNodeVersion = process.env.npm_package_engines_node || require('../package.json').engines.node;
if (!semver.satisfies(process.versions.node, supportedNodeVersion)) { if (!semverSatisfies(process.versions.node, supportedNodeVersion)) {
throw new Error(`Expected Node.js version ${supportedNodeVersion} but found ${process.versions.node}`); handleError(new Error(`Expected Node.js version ${supportedNodeVersion} but found ${process.versions.node}`));
} }
const extension = supportsBrotli ? 'br' : 'gz'; const extension = supportsBrotli ? 'br' : 'gz';
@@ -102,13 +128,11 @@ try {
const tarFilename = ['libvips', minimumLibvipsVersion, platformAndArch].join('-') + '.tar.' + extension; const tarFilename = ['libvips', minimumLibvipsVersion, platformAndArch].join('-') + '.tar.' + extension;
const tarPathCache = path.join(libvips.cachePath(), tarFilename); const tarPathCache = path.join(libvips.cachePath(), tarFilename);
if (fs.existsSync(tarPathCache)) { if (fs.existsSync(tarPathCache)) {
npmLog.info('sharp', `Using cached ${tarPathCache}`); libvips.log(`Using cached ${tarPathCache}`);
extractTarball(tarPathCache); extractTarball(tarPathCache, platformAndArch);
} else { } else {
const tarPathTemp = path.join(os.tmpdir(), `${process.pid}-${tarFilename}`);
const tmpFile = fs.createWriteStream(tarPathTemp);
const url = distBaseUrl + tarFilename; const url = distBaseUrl + tarFilename;
npmLog.info('sharp', `Downloading ${url}`); libvips.log(`Downloading ${url}`);
simpleGet({ url: url, agent: agent() }, function (err, response) { simpleGet({ url: url, agent: agent() }, function (err, response) {
if (err) { if (err) {
fail(err); fail(err);
@@ -117,13 +141,26 @@ try {
} else if (response.statusCode !== 200) { } else if (response.statusCode !== 200) {
fail(new Error(`Status ${response.statusCode} ${response.statusMessage}`)); fail(new Error(`Status ${response.statusCode} ${response.statusMessage}`));
} else { } else {
const tarPathTemp = path.join(os.tmpdir(), `${process.pid}-${tarFilename}`);
const tmpFileStream = fs.createWriteStream(tarPathTemp);
response response
.on('error', fail) .on('error', function (err) {
.pipe(tmpFile); tmpFileStream.destroy(err);
})
.on('close', function () {
if (!response.complete) {
tmpFileStream.destroy(new Error('Download incomplete (connection was terminated)'));
} }
}); })
tmpFile .pipe(tmpFileStream);
.on('error', fail) tmpFileStream
.on('error', function (err) {
// Clean up temporary file
try {
fs.unlinkSync(tarPathTemp);
} catch (e) {}
fail(err);
})
.on('close', function () { .on('close', function () {
try { try {
// Attempt to rename // Attempt to rename
@@ -133,7 +170,9 @@ try {
fs.copyFileSync(tarPathTemp, tarPathCache); fs.copyFileSync(tarPathTemp, tarPathCache);
fs.unlinkSync(tarPathTemp); fs.unlinkSync(tarPathTemp);
} }
extractTarball(tarPathCache); extractTarball(tarPathCache, platformAndArch);
});
}
}); });
} }
} }

View File

@@ -25,7 +25,7 @@ module.exports = function () {
? tunnelAgent.httpsOverHttps ? tunnelAgent.httpsOverHttps
: tunnelAgent.httpsOverHttp; : tunnelAgent.httpsOverHttp;
const proxyAuth = proxy.username && proxy.password const proxyAuth = proxy.username && proxy.password
? `${proxy.username}:${proxy.password}` ? `${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`
: null; : null;
return tunnel({ return tunnel({
proxy: { proxy: {

View File

@@ -30,21 +30,39 @@ function removeAlpha () {
} }
/** /**
* Ensure alpha channel, if missing. The added alpha channel will be fully opaque. This is a no-op if the image already has an alpha channel. * Ensure the output image has an alpha transparency channel.
* If missing, the added alpha channel will have the specified
* transparency level, defaulting to fully-opaque (1).
* This is a no-op if the image already has an alpha channel.
* *
* @since 0.21.2 * @since 0.21.2
* *
* @example * @example
* sharp('rgb.jpg') * // rgba.png will be a 4 channel image with a fully-opaque alpha channel
* await sharp('rgb.jpg')
* .ensureAlpha() * .ensureAlpha()
* .toFile('rgba.png', function(err, info) { * .toFile('rgba.png')
* // rgba.png is a 4 channel image with a fully opaque alpha channel
* });
* *
* @example
* // rgba is a 4 channel image with a fully-transparent alpha channel
* const rgba = await sharp(rgb)
* .ensureAlpha(0)
* .toBuffer();
*
* @param {number} [alpha=1] - alpha transparency level (0=fully-transparent, 1=fully-opaque)
* @returns {Sharp} * @returns {Sharp}
* @throws {Error} Invalid alpha transparency level
*/ */
function ensureAlpha () { function ensureAlpha (alpha) {
this.options.ensureAlpha = true; if (is.defined(alpha)) {
if (is.number(alpha) && is.inRange(alpha, 0, 1)) {
this.options.ensureAlpha = alpha;
} else {
throw is.invalidParameterError('alpha', 'number between 0 and 1', alpha);
}
} else {
this.options.ensureAlpha = 1;
}
return this; return this;
} }

View File

@@ -18,12 +18,10 @@ try {
help.push(`- Ensure "${process.platform}" is used at install time as well as runtime`); help.push(`- Ensure "${process.platform}" is used at install time as well as runtime`);
} else if (/dylib/.test(err.message) && /Incompatible library version/.test(err.message)) { } else if (/dylib/.test(err.message) && /Incompatible library version/.test(err.message)) {
help.push('- Run "brew update && brew upgrade vips"'); help.push('- Run "brew update && brew upgrade vips"');
} else if (/Cannot find module/.test(err.message)) {
help.push('- Run "npm rebuild --verbose sharp" and look for errors');
} else { } else {
help.push( help.push(
'- Remove the "node_modules/sharp" directory then run', '- Remove the "node_modules/sharp" directory then run',
' "npm install --ignore-scripts=false --verbose" and look for errors' ' "npm install --ignore-scripts=false --verbose sharp" and look for errors'
); );
} }
help.push( help.push(
@@ -134,12 +132,15 @@ const debuglog = util.debuglog('sharp');
* @param {number} [options.density=72] - number representing the DPI for vector images in the range 1 to 100000. * @param {number} [options.density=72] - number representing the DPI for vector images in the range 1 to 100000.
* @param {number} [options.pages=1] - number of pages to extract for multi-page input (GIF, WebP, AVIF, TIFF, PDF), use -1 for all pages. * @param {number} [options.pages=1] - number of pages to extract for multi-page input (GIF, WebP, AVIF, TIFF, PDF), use -1 for all pages.
* @param {number} [options.page=0] - page number to start extracting from for multi-page input (GIF, WebP, AVIF, TIFF, PDF), zero based. * @param {number} [options.page=0] - page number to start extracting from for multi-page input (GIF, WebP, AVIF, TIFF, PDF), zero based.
* @param {number} [options.subifd=-1] - subIFD (Sub Image File Directory) to extract for OME-TIFF, defaults to main image.
* @param {number} [options.level=0] - level to extract from a multi-level input (OpenSlide), zero based. * @param {number} [options.level=0] - level to extract from a multi-level input (OpenSlide), zero based.
* @param {boolean} [options.animated=false] - Set to `true` to read all frames/pages of an animated image (equivalent of setting `pages` to `-1`). * @param {boolean} [options.animated=false] - Set to `true` to read all frames/pages of an animated image (equivalent of setting `pages` to `-1`).
* @param {Object} [options.raw] - describes raw pixel input image data. See `raw()` for pixel ordering. * @param {Object} [options.raw] - describes raw pixel input image data. See `raw()` for pixel ordering.
* @param {number} [options.raw.width] - integral number of pixels wide. * @param {number} [options.raw.width] - integral number of pixels wide.
* @param {number} [options.raw.height] - integral number of pixels high. * @param {number} [options.raw.height] - integral number of pixels high.
* @param {number} [options.raw.channels] - integral number of channels, between 1 and 4. * @param {number} [options.raw.channels] - integral number of channels, between 1 and 4.
* @param {boolean} [options.raw.premultiplied] - specifies that the raw input has already been premultiplied, set to `true`
* to avoid sharp premultiplying the image. (optional, default `false`)
* @param {Object} [options.create] - describes a new image to be created. * @param {Object} [options.create] - describes a new image to be created.
* @param {number} [options.create.width] - integral number of pixels wide. * @param {number} [options.create.width] - integral number of pixels wide.
* @param {number} [options.create.height] - integral number of pixels high. * @param {number} [options.create.height] - integral number of pixels high.
@@ -223,7 +224,7 @@ const Sharp = function (input, options) {
joinChannelIn: [], joinChannelIn: [],
extractChannel: -1, extractChannel: -1,
removeAlpha: false, removeAlpha: false,
ensureAlpha: false, ensureAlpha: -1,
colourspace: 'srgb', colourspace: 'srgb',
composite: [], composite: [],
// output // output
@@ -232,7 +233,9 @@ const Sharp = function (input, options) {
streamOut: false, streamOut: false,
withMetadata: false, withMetadata: false,
withMetadataOrientation: -1, withMetadataOrientation: -1,
withMetadataDensity: 0,
withMetadataIcc: '', withMetadataIcc: '',
withMetadataStrs: {},
resolveWithObject: false, resolveWithObject: false,
// output format // output format
jpegQuality: 80, jpegQuality: 80,
@@ -244,7 +247,7 @@ const Sharp = function (input, options) {
jpegOptimiseCoding: true, jpegOptimiseCoding: true,
jpegQuantisationTable: 0, jpegQuantisationTable: 0,
pngProgressive: false, pngProgressive: false,
pngCompressionLevel: 9, pngCompressionLevel: 6,
pngAdaptiveFiltering: false, pngAdaptiveFiltering: false,
pngPalette: false, pngPalette: false,
pngQuality: 100, pngQuality: 100,
@@ -281,6 +284,7 @@ const Sharp = function (input, options) {
tileSkipBlanks: -1, tileSkipBlanks: -1,
tileBackground: [255, 255, 255, 255], tileBackground: [255, 255, 255, 255],
tileCentre: false, tileCentre: false,
tileId: 'https://example.com/iiif',
linearA: 1, linearA: 1,
linearB: 0, linearB: 0,
// Function to notify of libvips warnings // Function to notify of libvips warnings

View File

@@ -9,9 +9,9 @@ const sharp = require('../build/Release/sharp.node');
* @private * @private
*/ */
function _inputOptionsFromObject (obj) { function _inputOptionsFromObject (obj) {
const { raw, density, limitInputPixels, sequentialRead, failOnError, animated, page, pages } = obj; const { raw, density, limitInputPixels, sequentialRead, failOnError, animated, page, pages, subifd } = obj;
return [raw, density, limitInputPixels, sequentialRead, failOnError, animated, page, pages].some(is.defined) return [raw, density, limitInputPixels, sequentialRead, failOnError, animated, page, pages, subifd].some(is.defined)
? { raw, density, limitInputPixels, sequentialRead, failOnError, animated, page, pages } ? { raw, density, limitInputPixels, sequentialRead, failOnError, animated, page, pages, subifd }
: undefined; : undefined;
} }
@@ -30,9 +30,15 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
inputDescriptor.file = input; inputDescriptor.file = input;
} else if (is.buffer(input)) { } else if (is.buffer(input)) {
// Buffer // Buffer
if (input.length === 0) {
throw Error('Input Buffer is empty');
}
inputDescriptor.buffer = input; inputDescriptor.buffer = input;
} else if (is.uint8Array(input)) { } else if (is.uint8Array(input)) {
// Uint8Array or Uint8ClampedArray // Uint8Array or Uint8ClampedArray
if (input.length === 0) {
throw Error('Input Bit Array is empty');
}
inputDescriptor.buffer = Buffer.from(input.buffer); inputDescriptor.buffer = Buffer.from(input.buffer);
} else if (is.plainObject(input) && !is.defined(inputOptions)) { } else if (is.plainObject(input) && !is.defined(inputOptions)) {
// Plain Object descriptor, e.g. create // Plain Object descriptor, e.g. create
@@ -97,6 +103,7 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
inputDescriptor.rawWidth = inputOptions.raw.width; inputDescriptor.rawWidth = inputOptions.raw.width;
inputDescriptor.rawHeight = inputOptions.raw.height; inputDescriptor.rawHeight = inputOptions.raw.height;
inputDescriptor.rawChannels = inputOptions.raw.channels; inputDescriptor.rawChannels = inputOptions.raw.channels;
inputDescriptor.rawPremultiplied = !!inputOptions.raw.premultiplied;
} else { } else {
throw new Error('Expected width, height and channels for raw pixel input'); throw new Error('Expected width, height and channels for raw pixel input');
} }
@@ -131,6 +138,14 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
throw is.invalidParameterError('level', 'integer between 0 and 256', inputOptions.level); throw is.invalidParameterError('level', 'integer between 0 and 256', inputOptions.level);
} }
} }
// Sub Image File Directory (TIFF)
if (is.defined(inputOptions.subifd)) {
if (is.integer(inputOptions.subifd) && is.inRange(inputOptions.subifd, -1, 100000)) {
inputDescriptor.subifd = inputOptions.subifd;
} else {
throw is.invalidParameterError('subifd', 'integer between -1 and 100000', inputOptions.subifd);
}
}
// Create new image // Create new image
if (is.defined(inputOptions.create)) { if (is.defined(inputOptions.create)) {
if ( if (
@@ -255,6 +270,7 @@ function _isStreamInput () {
* - `delay`: Delay in ms between each page in an animated image, provided as an array of integers. * - `delay`: Delay in ms between each page in an animated image, provided as an array of integers.
* - `pagePrimary`: Number of the primary page in a HEIF image * - `pagePrimary`: Number of the primary page in a HEIF image
* - `levels`: Details of each level in a multi-level image provided as an array of objects, requires libvips compiled with support for OpenSlide * - `levels`: Details of each level in a multi-level image provided as an array of objects, requires libvips compiled with support for OpenSlide
* - `subifds`: Number of Sub Image File Directories in an OME-TIFF image
* - `hasProfile`: Boolean indicating the presence of an embedded ICC profile * - `hasProfile`: Boolean indicating the presence of an embedded ICC profile
* - `hasAlpha`: Boolean indicating the presence of an alpha transparency channel * - `hasAlpha`: Boolean indicating the presence of an alpha transparency channel
* - `orientation`: Number value of the EXIF Orientation header, if present * - `orientation`: Number value of the EXIF Orientation header, if present

View File

@@ -4,13 +4,15 @@ const fs = require('fs');
const os = require('os'); const os = require('os');
const path = require('path'); const path = require('path');
const spawnSync = require('child_process').spawnSync; const spawnSync = require('child_process').spawnSync;
const semver = require('semver'); const semverCoerce = require('semver/functions/coerce');
const semverGreaterThanOrEqualTo = require('semver/functions/gte');
const platform = require('./platform'); const platform = require('./platform');
const env = process.env; const env = process.env;
const minimumLibvipsVersionLabelled = env.npm_package_config_libvips || /* istanbul ignore next */ const minimumLibvipsVersionLabelled = env.npm_package_config_libvips || /* istanbul ignore next */
require('../package.json').config.libvips; require('../package.json').config.libvips;
const minimumLibvipsVersion = semver.coerce(minimumLibvipsVersionLabelled).version; const minimumLibvipsVersion = semverCoerce(minimumLibvipsVersionLabelled).version;
const spawnSyncOptions = { const spawnSyncOptions = {
encoding: 'utf8', encoding: 'utf8',
@@ -37,6 +39,14 @@ const cachePath = function () {
return libvipsCachePath; return libvipsCachePath;
}; };
const log = function (item) {
if (item instanceof Error) {
console.error(`sharp: Installation error: ${item.message}`);
} else {
console.log(`sharp: ${item}`);
}
};
const isRosetta = function () { const isRosetta = function () {
/* istanbul ignore next */ /* istanbul ignore next */
if (process.platform === 'darwin' && process.arch === 'x64') { if (process.platform === 'darwin' && process.arch === 'x64') {
@@ -97,13 +107,14 @@ const useGlobalLibvips = function () {
} }
const globalVipsVersion = globalLibvipsVersion(); const globalVipsVersion = globalLibvipsVersion();
return !!globalVipsVersion && /* istanbul ignore next */ return !!globalVipsVersion && /* istanbul ignore next */
semver.gte(globalVipsVersion, minimumLibvipsVersion); semverGreaterThanOrEqualTo(globalVipsVersion, minimumLibvipsVersion);
}; };
module.exports = { module.exports = {
minimumLibvipsVersion, minimumLibvipsVersion,
minimumLibvipsVersionLabelled, minimumLibvipsVersionLabelled,
cachePath, cachePath,
log,
globalLibvipsVersion, globalLibvipsVersion,
hasVendoredLibvips, hasVendoredLibvips,
pkgConfigPath, pkgConfigPath,

View File

@@ -1,6 +1,5 @@
'use strict'; 'use strict';
const { flatten: flattenArray } = require('array-flatten');
const color = require('color'); const color = require('color');
const is = require('./is'); const is = require('./is');
@@ -127,7 +126,7 @@ function flop (flop) {
* @throws {Error} Invalid parameters * @throws {Error} Invalid parameters
*/ */
function affine (matrix, options) { function affine (matrix, options) {
const flatMatrix = flattenArray(matrix); const flatMatrix = [].concat(...matrix);
if (flatMatrix.length === 4 && flatMatrix.every(is.number)) { if (flatMatrix.length === 4 && flatMatrix.every(is.number)) {
this.options.affineMatrix = flatMatrix; this.options.affineMatrix = flatMatrix;
} else { } else {
@@ -269,7 +268,13 @@ function blur (sigma) {
} }
/** /**
* Merge alpha transparency channel, if any, with a background. * Merge alpha transparency channel, if any, with a background, then remove the alpha channel.
*
* @example
* await sharp(rgbaInput)
* .flatten({background: '#F0A703' })
* .toBuffer();
*
* @param {Object} [options] * @param {Object} [options]
* @param {string|Object} [options.background={r: 0, g: 0, b: 0}] - background colour, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to black. * @param {string|Object} [options.background={r: 0, g: 0, b: 0}] - background colour, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to black.
* @returns {Sharp} * @returns {Sharp}
@@ -397,7 +402,7 @@ function convolve (kernel) {
} }
/** /**
* Any pixel value greather than or equal to the threshold value will be set to 255, otherwise it will be set to 0. * Any pixel value greater than or equal to the threshold value will be set to 255, otherwise it will be set to 0.
* @param {number} [threshold=128] - a value in the range 0-255 representing the level at which the threshold will be applied. * @param {number} [threshold=128] - a value in the range 0-255 representing the level at which the threshold will be applied.
* @param {Object} [options] * @param {Object} [options]
* @param {Boolean} [options.greyscale=true] - convert to single channel greyscale. * @param {Boolean} [options.greyscale=true] - convert to single channel greyscale.

View File

@@ -105,10 +105,10 @@ function toFile (fileOut, callback) {
* .catch(err => { ... }); * .catch(err => { ... });
* *
* @example * @example
* const data = await sharp('my-image.jpg') * const { data, info } = await sharp('my-image.jpg')
* // output the raw pixels * // output the raw pixels
* .raw() * .raw()
* .toBuffer(); * .toBuffer({ resolveWithObject: true });
* *
* // create a more type safe way to work with the raw pixel data * // create a more type safe way to work with the raw pixel data
* // this will not copy the data, instead it will change `data`s underlying ArrayBuffer * // this will not copy the data, instead it will change `data`s underlying ArrayBuffer
@@ -116,7 +116,9 @@ function toFile (fileOut, callback) {
* const pixelArray = new Uint8ClampedArray(data.buffer); * const pixelArray = new Uint8ClampedArray(data.buffer);
* *
* // When you are done changing the pixelArray, sharp takes the `pixelArray` as an input * // When you are done changing the pixelArray, sharp takes the `pixelArray` as an input
* await sharp(pixelArray).toFile('my-changed-image.jpg'); * const { width, height, channels } = info;
* await sharp(pixelArray, { raw: { width, height, channels } })
* .toFile('my-changed-image.jpg');
* *
* @param {Object} [options] * @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 {boolean} [options.resolveWithObject] Resolve the Promise with an Object containing `data` and `info` properties instead of resolving only with `data`.
@@ -146,9 +148,29 @@ function toBuffer (options, callback) {
* .toFile('output-with-metadata.jpg') * .toFile('output-with-metadata.jpg')
* .then(info => { ... }); * .then(info => { ... });
* *
* @example
* // Set "IFD0-Copyright" in output EXIF metadata
* const data = await sharp(input)
* .withMetadata({
* exif: {
* IFD0: {
* Copyright: 'Wernham Hogg'
* }
* }
* })
* .toBuffer();
*
* * @example
* // Set output metadata to 96 DPI
* const data = await sharp(input)
* .withMetadata({ density: 96 })
* .toBuffer();
*
* @param {Object} [options] * @param {Object} [options]
* @param {number} [options.orientation] value between 1 and 8, used to update the EXIF `Orientation` tag. * @param {number} [options.orientation] value between 1 and 8, used to update the EXIF `Orientation` tag.
* @param {string} [options.icc] filesystem path to output ICC profile, defaults to sRGB. * @param {string} [options.icc] filesystem path to output ICC profile, defaults to sRGB.
* @param {Object<Object>} [options.exif={}] Object keyed by IFD0, IFD1 etc. of key/value string pairs to write as EXIF data.
* @param {number} [options.density] Number of pixels per inch (DPI).
* @returns {Sharp} * @returns {Sharp}
* @throws {Error} Invalid parameters * @throws {Error} Invalid parameters
*/ */
@@ -162,6 +184,13 @@ function withMetadata (options) {
throw is.invalidParameterError('orientation', 'integer between 1 and 8', options.orientation); throw is.invalidParameterError('orientation', 'integer between 1 and 8', options.orientation);
} }
} }
if (is.defined(options.density)) {
if (is.number(options.density) && options.density > 0) {
this.options.withMetadataDensity = options.density;
} else {
throw is.invalidParameterError('density', 'positive number', options.density);
}
}
if (is.defined(options.icc)) { if (is.defined(options.icc)) {
if (is.string(options.icc)) { if (is.string(options.icc)) {
this.options.withMetadataIcc = options.icc; this.options.withMetadataIcc = options.icc;
@@ -169,6 +198,25 @@ function withMetadata (options) {
throw is.invalidParameterError('icc', 'string filesystem path to ICC profile', options.icc); throw is.invalidParameterError('icc', 'string filesystem path to ICC profile', options.icc);
} }
} }
if (is.defined(options.exif)) {
if (is.object(options.exif)) {
for (const [ifd, entries] of Object.entries(options.exif)) {
if (is.object(entries)) {
for (const [k, v] of Object.entries(entries)) {
if (is.string(v)) {
this.options.withMetadataStrs[`exif-${ifd.toLowerCase()}-${k}`] = v;
} else {
throw is.invalidParameterError(`exif.${ifd}.${k}`, 'string', v);
}
}
} else {
throw is.invalidParameterError(`exif.${ifd}`, 'object', entries);
}
}
} else {
throw is.invalidParameterError('exif', 'object', options.exif);
}
}
} }
return this; return this;
} }
@@ -198,8 +246,6 @@ function toFormat (format, options) {
/** /**
* Use these JPEG options for output image. * Use these JPEG options for output image.
* *
* Some of these options require the use of a globally-installed libvips compiled with support for mozjpeg.
*
* @example * @example
* // Convert any input to very high quality JPEG output * // Convert any input to very high quality JPEG output
* const data = await sharp(input) * const data = await sharp(input)
@@ -209,18 +255,25 @@ function toFormat (format, options) {
* }) * })
* .toBuffer(); * .toBuffer();
* *
* @example
* // Use mozjpeg to reduce output JPEG file size (slower)
* const data = await sharp(input)
* .jpeg({ mozjpeg: true })
* .toBuffer();
*
* @param {Object} [options] - output options * @param {Object} [options] - output options
* @param {number} [options.quality=80] - quality, integer 1-100 * @param {number} [options.quality=80] - quality, integer 1-100
* @param {boolean} [options.progressive=false] - use progressive (interlace) scan * @param {boolean} [options.progressive=false] - use progressive (interlace) scan
* @param {string} [options.chromaSubsampling='4:2:0'] - set to '4:4:4' to prevent chroma subsampling otherwise defaults to '4:2:0' chroma subsampling * @param {string} [options.chromaSubsampling='4:2:0'] - set to '4:4:4' to prevent chroma subsampling otherwise defaults to '4:2:0' chroma subsampling
* @param {boolean} [options.optimiseCoding=true] - optimise Huffman coding tables * @param {boolean} [options.optimiseCoding=true] - optimise Huffman coding tables
* @param {boolean} [options.optimizeCoding=true] - alternative spelling of optimiseCoding * @param {boolean} [options.optimizeCoding=true] - alternative spelling of optimiseCoding
* @param {boolean} [options.trellisQuantisation=false] - apply trellis quantisation, requires libvips compiled with support for mozjpeg * @param {boolean} [options.mozjpeg=false] - use mozjpeg defaults, equivalent to `{ trellisQuantisation: true, overshootDeringing: true, optimiseScans: true, quantisationTable: 3 }`
* @param {boolean} [options.overshootDeringing=false] - apply overshoot deringing, requires libvips compiled with support for mozjpeg * @param {boolean} [options.trellisQuantisation=false] - apply trellis quantisation
* @param {boolean} [options.optimiseScans=false] - optimise progressive scans, forces progressive, requires libvips compiled with support for mozjpeg * @param {boolean} [options.overshootDeringing=false] - apply overshoot deringing
* @param {boolean} [options.optimizeScans=false] - alternative spelling of optimiseScans, requires libvips compiled with support for mozjpeg * @param {boolean} [options.optimiseScans=false] - optimise progressive scans, forces progressive
* @param {number} [options.quantisationTable=0] - quantization table to use, integer 0-8, requires libvips compiled with support for mozjpeg * @param {boolean} [options.optimizeScans=false] - alternative spelling of optimiseScans
* @param {number} [options.quantizationTable=0] - alternative spelling of quantisationTable, requires libvips compiled with support for mozjpeg * @param {number} [options.quantisationTable=0] - quantization table to use, integer 0-8
* @param {number} [options.quantizationTable=0] - alternative spelling of quantisationTable
* @param {boolean} [options.force=true] - force JPEG output, otherwise attempt to use input format * @param {boolean} [options.force=true] - force JPEG output, otherwise attempt to use input format
* @returns {Sharp} * @returns {Sharp}
* @throws {Error} Invalid options * @throws {Error} Invalid options
@@ -244,6 +297,23 @@ function jpeg (options) {
throw is.invalidParameterError('chromaSubsampling', 'one of: 4:2:0, 4:4:4', options.chromaSubsampling); throw is.invalidParameterError('chromaSubsampling', 'one of: 4:2:0, 4:4:4', options.chromaSubsampling);
} }
} }
const optimiseCoding = is.bool(options.optimizeCoding) ? options.optimizeCoding : options.optimiseCoding;
if (is.defined(optimiseCoding)) {
this._setBooleanOption('jpegOptimiseCoding', optimiseCoding);
}
if (is.defined(options.mozjpeg)) {
if (is.bool(options.mozjpeg)) {
if (options.mozjpeg) {
this.options.jpegTrellisQuantisation = true;
this.options.jpegOvershootDeringing = true;
this.options.jpegOptimiseScans = true;
this.options.jpegProgressive = true;
this.options.jpegQuantisationTable = 3;
}
} else {
throw is.invalidParameterError('mozjpeg', 'boolean', options.mozjpeg);
}
}
const trellisQuantisation = is.bool(options.trellisQuantization) ? options.trellisQuantization : options.trellisQuantisation; const trellisQuantisation = is.bool(options.trellisQuantization) ? options.trellisQuantization : options.trellisQuantisation;
if (is.defined(trellisQuantisation)) { if (is.defined(trellisQuantisation)) {
this._setBooleanOption('jpegTrellisQuantisation', trellisQuantisation); this._setBooleanOption('jpegTrellisQuantisation', trellisQuantisation);
@@ -258,10 +328,6 @@ function jpeg (options) {
this.options.jpegProgressive = true; this.options.jpegProgressive = true;
} }
} }
const optimiseCoding = is.bool(options.optimizeCoding) ? options.optimizeCoding : options.optimiseCoding;
if (is.defined(optimiseCoding)) {
this._setBooleanOption('jpegOptimiseCoding', optimiseCoding);
}
const quantisationTable = is.number(options.quantizationTable) ? options.quantizationTable : options.quantisationTable; const quantisationTable = is.number(options.quantizationTable) ? options.quantizationTable : options.quantisationTable;
if (is.defined(quantisationTable)) { if (is.defined(quantisationTable)) {
if (is.integer(quantisationTable) && is.inRange(quantisationTable, 0, 8)) { if (is.integer(quantisationTable) && is.inRange(quantisationTable, 0, 8)) {
@@ -277,26 +343,31 @@ function jpeg (options) {
/** /**
* Use these PNG options for output image. * Use these PNG options for output image.
* *
* PNG output is always full colour at 8 or 16 bits per pixel. * By default, PNG output is 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. * Indexed PNG input at 1, 2 or 4 bits per pixel is converted to 8 bits per pixel.
* * Set `palette` to `true` for slower, indexed PNG output.
* Some of these options require the use of a globally-installed libvips compiled with support for libimagequant (GPL).
* *
* @example * @example
* // Convert any input to PNG output * // Convert any input to full colour PNG output
* const data = await sharp(input) * const data = await sharp(input)
* .png() * .png()
* .toBuffer(); * .toBuffer();
* *
* @example
* // Convert any input to indexed PNG output (slower)
* const data = await sharp(input)
* .png({ palette: true })
* .toBuffer();
*
* @param {Object} [options] * @param {Object} [options]
* @param {boolean} [options.progressive=false] - use progressive (interlace) scan * @param {boolean} [options.progressive=false] - use progressive (interlace) scan
* @param {number} [options.compressionLevel=9] - zlib compression level, 0-9 * @param {number} [options.compressionLevel=6] - zlib compression level, 0 (fastest, largest) to 9 (slowest, smallest)
* @param {boolean} [options.adaptiveFiltering=false] - use adaptive row filtering * @param {boolean} [options.adaptiveFiltering=false] - use adaptive row filtering
* @param {boolean} [options.palette=false] - quantise to a palette-based image with alpha transparency support, requires libvips compiled with support for libimagequant * @param {boolean} [options.palette=false] - quantise to a palette-based image with alpha transparency support
* @param {number} [options.quality=100] - use the lowest number of colours needed to achieve given quality, sets `palette` to `true`, requires libvips compiled with support for libimagequant * @param {number} [options.quality=100] - use the lowest number of colours needed to achieve given quality, sets `palette` to `true`
* @param {number} [options.colours=256] - maximum number of palette entries, sets `palette` to `true`, requires libvips compiled with support for libimagequant * @param {number} [options.colours=256] - maximum number of palette entries, sets `palette` to `true`
* @param {number} [options.colors=256] - alternative spelling of `options.colours`, sets `palette` to `true`, requires libvips compiled with support for libimagequant * @param {number} [options.colors=256] - alternative spelling of `options.colours`, sets `palette` to `true`
* @param {number} [options.dither=1.0] - level of Floyd-Steinberg error diffusion, sets `palette` to `true`, requires libvips compiled with support for libimagequant * @param {number} [options.dither=1.0] - level of Floyd-Steinberg error diffusion, sets `palette` to `true`
* @param {boolean} [options.force=true] - force PNG output, otherwise attempt to use input format * @param {boolean} [options.force=true] - force PNG output, otherwise attempt to use input format
* @returns {Sharp} * @returns {Sharp}
* @throws {Error} Invalid options * @throws {Error} Invalid options
@@ -358,6 +429,12 @@ function png (options) {
* .webp({ lossless: true }) * .webp({ lossless: true })
* .toBuffer(); * .toBuffer();
* *
* @example
* // Optimise the file size of an animated WebP
* const outputWebp = await sharp(inputWebp, { animated: true })
* .webp({ reductionEffort: 6 })
* .toBuffer();
*
* @param {Object} [options] - output options * @param {Object} [options] - output options
* @param {number} [options.quality=80] - quality, integer 1-100 * @param {number} [options.quality=80] - quality, integer 1-100
* @param {number} [options.alphaQuality=100] - quality of alpha layer, integer 0-100 * @param {number} [options.alphaQuality=100] - quality of alpha layer, integer 0-100
@@ -704,6 +781,7 @@ function raw () {
* @param {string} [options.layout='dz'] filesystem layout, possible values are `dz`, `iiif`, `zoomify` or `google`. * @param {string} [options.layout='dz'] filesystem layout, possible values are `dz`, `iiif`, `zoomify` or `google`.
* @param {boolean} [options.centre=false] centre image in tile. * @param {boolean} [options.centre=false] centre image in tile.
* @param {boolean} [options.center=false] alternative spelling of centre. * @param {boolean} [options.center=false] alternative spelling of centre.
* @param {string} [options.id='https://example.com/iiif'] when `layout` is `iiif`, sets the `@id` attribute of `info.json`
* @returns {Sharp} * @returns {Sharp}
* @throws {Error} Invalid parameters * @throws {Error} Invalid parameters
*/ */
@@ -777,6 +855,14 @@ function tile (options) {
if (is.defined(centre)) { if (is.defined(centre)) {
this._setBooleanOption('tileCentre', centre); this._setBooleanOption('tileCentre', centre);
} }
// @id attribute for IIIF layout
if (is.defined(options.id)) {
if (is.string(options.id)) {
this.options.tileId = options.id;
} else {
throw is.invalidParameterError('id', 'string', options.id);
}
}
} }
// Format // Format
if (is.inArray(this.options.formatOut, ['jpeg', 'png', 'webp'])) { if (is.inArray(this.options.formatOut, ['jpeg', 'png', 'webp'])) {

View File

@@ -302,11 +302,20 @@ function resize (width, height, options) {
* }) * })
* ... * ...
* *
* @example
* // Add a row of 10 red pixels to the bottom
* sharp(input)
* .extend({
* bottom: 10,
* background: 'red'
* })
* ...
*
* @param {(number|Object)} extend - single pixel count to add to all edges or an Object with per-edge counts * @param {(number|Object)} extend - single pixel count to add to all edges or an Object with per-edge counts
* @param {number} [extend.top] * @param {number} [extend.top=0]
* @param {number} [extend.left] * @param {number} [extend.left=0]
* @param {number} [extend.bottom] * @param {number} [extend.bottom=0]
* @param {number} [extend.right] * @param {number} [extend.right=0]
* @param {String|Object} [extend.background={r: 0, g: 0, b: 0, alpha: 1}] - background colour, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to black without transparency. * @param {String|Object} [extend.background={r: 0, g: 0, b: 0, alpha: 1}] - background colour, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to black without transparency.
* @returns {Sharp} * @returns {Sharp}
* @throws {Error} Invalid parameters * @throws {Error} Invalid parameters
@@ -317,17 +326,35 @@ function extend (extend) {
this.options.extendBottom = extend; this.options.extendBottom = extend;
this.options.extendLeft = extend; this.options.extendLeft = extend;
this.options.extendRight = extend; this.options.extendRight = extend;
} else if ( } else if (is.object(extend)) {
is.object(extend) && if (is.defined(extend.top)) {
is.integer(extend.top) && extend.top >= 0 && if (is.integer(extend.top) && extend.top >= 0) {
is.integer(extend.bottom) && extend.bottom >= 0 &&
is.integer(extend.left) && extend.left >= 0 &&
is.integer(extend.right) && extend.right >= 0
) {
this.options.extendTop = extend.top; this.options.extendTop = extend.top;
} else {
throw is.invalidParameterError('top', 'positive integer', extend.top);
}
}
if (is.defined(extend.bottom)) {
if (is.integer(extend.bottom) && extend.bottom >= 0) {
this.options.extendBottom = extend.bottom; this.options.extendBottom = extend.bottom;
} else {
throw is.invalidParameterError('bottom', 'positive integer', extend.bottom);
}
}
if (is.defined(extend.left)) {
if (is.integer(extend.left) && extend.left >= 0) {
this.options.extendLeft = extend.left; this.options.extendLeft = extend.left;
} else {
throw is.invalidParameterError('left', 'positive integer', extend.left);
}
}
if (is.defined(extend.right)) {
if (is.integer(extend.right) && extend.right >= 0) {
this.options.extendRight = extend.right; this.options.extendRight = extend.right;
} else {
throw is.invalidParameterError('right', 'positive integer', extend.right);
}
}
this._setBackgroundColourOption('extendBackground', extend.background); this._setBackgroundColourOption('extendBackground', extend.background);
} else { } else {
throw is.invalidParameterError('extend', 'integer or object', extend); throw is.invalidParameterError('extend', 'integer or object', extend);

View File

@@ -1,6 +1,8 @@
'use strict'; 'use strict';
const events = require('events'); const events = require('events');
const detectLibc = require('detect-libc');
const is = require('./is'); const is = require('./is');
const sharp = require('../build/Release/sharp.node'); const sharp = require('../build/Release/sharp.node');
@@ -84,8 +86,12 @@ cache(true);
/** /**
* Gets or, when a concurrency is provided, sets * Gets or, when a concurrency is provided, sets
* the number of threads _libvips'_ should create to process each image. * the number of threads _libvips'_ should create to process each image.
* The default value is the number of CPU cores. *
* A value of `0` will reset to this default. * The default value is the number of CPU cores,
* except when using glibc-based Linux without jemalloc,
* where the default is `1` to help reduce memory fragmentation.
*
* A value of `0` will reset this to the number of CPU cores.
* *
* The maximum number of images that can be processed in parallel * The maximum number of images that can be processed in parallel
* is limited by libuv's `UV_THREADPOOL_SIZE` environment variable. * is limited by libuv's `UV_THREADPOOL_SIZE` environment variable.
@@ -103,6 +109,11 @@ cache(true);
function concurrency (concurrency) { function concurrency (concurrency) {
return sharp.concurrency(is.integer(concurrency) ? concurrency : null); return sharp.concurrency(is.integer(concurrency) ? concurrency : null);
} }
/* istanbul ignore next */
if (detectLibc.family === detectLibc.GLIBC && !sharp._isUsingJemalloc()) {
// Reduce default concurrency to 1 when using glibc memory allocator
sharp.concurrency(1);
}
/** /**
* An EventEmitter that emits a `change` event when a task is either: * An EventEmitter that emits a `change` event when a task is either:

View File

@@ -1,7 +1,7 @@
{ {
"name": "sharp", "name": "sharp",
"description": "High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images", "description": "High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images",
"version": "0.27.2", "version": "0.28.2",
"author": "Lovell Fuller <npm@lovell.info>", "author": "Lovell Fuller <npm@lovell.info>",
"homepage": "https://github.com/lovell/sharp", "homepage": "https://github.com/lovell/sharp",
"contributors": [ "contributors": [
@@ -74,13 +74,16 @@
"Christian Flintrup <chr@gigahost.dk>", "Christian Flintrup <chr@gigahost.dk>",
"Manan Jadhav <manan@motionden.com>", "Manan Jadhav <manan@motionden.com>",
"Leon Radley <leon@radley.se>", "Leon Radley <leon@radley.se>",
"alza54 <alza54@thiocod.in>" "alza54 <alza54@thiocod.in>",
"Jacob Smith <jacob@frende.me>",
"Michael Nutt <michael@nutt.im>"
], ],
"scripts": { "scripts": {
"install": "(node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)", "install": "(node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)",
"clean": "rm -rf node_modules/ build/ vendor/ .nyc_output/ coverage/ test/fixtures/output.*", "clean": "rm -rf node_modules/ build/ vendor/ .nyc_output/ coverage/ test/fixtures/output.*",
"test": "semistandard && cpplint && npm run test-unit && npm run test-licensing", "test": "npm run test-lint && npm run test-unit && npm run test-licensing",
"test-unit": "nyc --reporter=lcov --branches=99 mocha --slow=5000 --timeout=60000 ./test/unit/*.js", "test-lint": "semistandard && cpplint",
"test-unit": "nyc --reporter=lcov --branches=99 mocha --slow=1000 --timeout=60000 ./test/unit/*.js",
"test-licensing": "license-checker --production --summary --onlyAllow=\"Apache-2.0;BSD;ISC;MIT\"", "test-licensing": "license-checker --production --summary --onlyAllow=\"Apache-2.0;BSD;ISC;MIT\"",
"test-coverage": "./test/coverage/report.sh", "test-coverage": "./test/coverage/report.sh",
"test-leak": "./test/leak/leak.sh", "test-leak": "./test/leak/leak.sh",
@@ -117,14 +120,12 @@
"vips" "vips"
], ],
"dependencies": { "dependencies": {
"array-flatten": "^3.0.0",
"color": "^3.1.3", "color": "^3.1.3",
"detect-libc": "^1.0.3", "detect-libc": "^1.0.3",
"node-addon-api": "^3.1.0", "node-addon-api": "^3.1.0",
"npmlog": "^4.1.2", "prebuild-install": "^6.1.2",
"prebuild-install": "^6.0.1", "semver": "^7.3.5",
"semver": "^7.3.4", "simple-get": "^3.1.0",
"simple-get": "^4.0.0",
"tar-fs": "^2.1.1", "tar-fs": "^2.1.1",
"tunnel-agent": "^0.6.0" "tunnel-agent": "^0.6.0"
}, },
@@ -132,12 +133,12 @@
"async": "^3.2.0", "async": "^3.2.0",
"cc": "^3.0.1", "cc": "^3.0.1",
"decompress-zip": "^0.3.3", "decompress-zip": "^0.3.3",
"documentation": "^13.1.1", "documentation": "^13.2.5",
"exif-reader": "^1.0.3", "exif-reader": "^1.0.3",
"icc": "^2.0.0", "icc": "^2.0.0",
"license-checker": "^25.0.1", "license-checker": "^25.0.1",
"mocha": "^8.3.0", "mocha": "^8.4.0",
"mock-fs": "^4.13.0", "mock-fs": "^4.14.0",
"nyc": "^15.1.0", "nyc": "^15.1.0",
"prebuild": "^10.0.1", "prebuild": "^10.0.1",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
@@ -145,7 +146,7 @@
}, },
"license": "Apache-2.0", "license": "Apache-2.0",
"config": { "config": {
"libvips": "8.10.5", "libvips": "8.10.6",
"runtime": "napi", "runtime": "napi",
"target": 3 "target": 3
}, },

View File

@@ -36,6 +36,9 @@ namespace sharp {
std::string AttrAsStr(Napi::Object obj, std::string attr) { std::string AttrAsStr(Napi::Object obj, std::string attr) {
return obj.Get(attr).As<Napi::String>(); return obj.Get(attr).As<Napi::String>();
} }
std::string AttrAsStr(Napi::Object obj, unsigned int const attr) {
return obj.Get(attr).As<Napi::String>();
}
uint32_t AttrAsUint32(Napi::Object obj, std::string attr) { uint32_t AttrAsUint32(Napi::Object obj, std::string attr) {
return obj.Get(attr).As<Napi::Number>().Uint32Value(); return obj.Get(attr).As<Napi::Number>().Uint32Value();
} }
@@ -92,6 +95,7 @@ namespace sharp {
descriptor->rawChannels = AttrAsUint32(input, "rawChannels"); descriptor->rawChannels = AttrAsUint32(input, "rawChannels");
descriptor->rawWidth = AttrAsUint32(input, "rawWidth"); descriptor->rawWidth = AttrAsUint32(input, "rawWidth");
descriptor->rawHeight = AttrAsUint32(input, "rawHeight"); descriptor->rawHeight = AttrAsUint32(input, "rawHeight");
descriptor->rawPremultiplied = AttrAsBool(input, "rawPremultiplied");
} }
// Multi-page input (GIF, TIFF, PDF) // Multi-page input (GIF, TIFF, PDF)
if (HasAttr(input, "pages")) { if (HasAttr(input, "pages")) {
@@ -104,6 +108,10 @@ namespace sharp {
if (HasAttr(input, "level")) { if (HasAttr(input, "level")) {
descriptor->level = AttrAsUint32(input, "level"); descriptor->level = AttrAsUint32(input, "level");
} }
// subIFD (OME-TIFF)
if (HasAttr(input, "subifd")) {
descriptor->subifd = AttrAsInt32(input, "subifd");
}
// Create new image // Create new image
if (HasAttr(input, "createChannels")) { if (HasAttr(input, "createChannels")) {
descriptor->createChannels = AttrAsUint32(input, "createChannels"); descriptor->createChannels = AttrAsUint32(input, "createChannels");
@@ -213,6 +221,8 @@ namespace sharp {
{ "VipsForeignLoadTiffBuffer", ImageType::TIFF }, { "VipsForeignLoadTiffBuffer", ImageType::TIFF },
{ "VipsForeignLoadGifFile", ImageType::GIF }, { "VipsForeignLoadGifFile", ImageType::GIF },
{ "VipsForeignLoadGifBuffer", ImageType::GIF }, { "VipsForeignLoadGifBuffer", ImageType::GIF },
{ "VipsForeignLoadNsgifFile", ImageType::GIF },
{ "VipsForeignLoadNsgifBuffer", ImageType::GIF },
{ "VipsForeignLoadSvgFile", ImageType::SVG }, { "VipsForeignLoadSvgFile", ImageType::SVG },
{ "VipsForeignLoadSvgBuffer", ImageType::SVG }, { "VipsForeignLoadSvgBuffer", ImageType::SVG },
{ "VipsForeignLoadHeifFile", ImageType::HEIF }, { "VipsForeignLoadHeifFile", ImageType::HEIF },
@@ -226,6 +236,7 @@ namespace sharp {
{ "VipsForeignLoadFits", ImageType::FITS }, { "VipsForeignLoadFits", ImageType::FITS },
{ "VipsForeignLoadOpenexr", ImageType::EXR }, { "VipsForeignLoadOpenexr", ImageType::EXR },
{ "VipsForeignLoadVips", ImageType::VIPS }, { "VipsForeignLoadVips", ImageType::VIPS },
{ "VipsForeignLoadVipsFile", ImageType::VIPS },
{ "VipsForeignLoadRaw", ImageType::RAW } { "VipsForeignLoadRaw", ImageType::RAW }
}; };
@@ -292,6 +303,9 @@ namespace sharp {
} else { } else {
image.get_image()->Type = VIPS_INTERPRETATION_sRGB; image.get_image()->Type = VIPS_INTERPRETATION_sRGB;
} }
if (descriptor->rawPremultiplied) {
image = image.unpremultiply();
}
imageType = ImageType::RAW; imageType = ImageType::RAW;
} else { } else {
// Compressed data // Compressed data
@@ -317,6 +331,9 @@ namespace sharp {
if (imageType == ImageType::OPENSLIDE) { if (imageType == ImageType::OPENSLIDE) {
option->set("level", descriptor->level); option->set("level", descriptor->level);
} }
if (imageType == ImageType::TIFF) {
option->set("subifd", descriptor->subifd);
}
image = VImage::new_from_buffer(descriptor->buffer, descriptor->bufferLength, nullptr, option); image = VImage::new_from_buffer(descriptor->buffer, descriptor->bufferLength, nullptr, option);
if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) { if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) {
image = SetDensity(image, descriptor->density); image = SetDensity(image, descriptor->density);
@@ -389,6 +406,9 @@ namespace sharp {
if (imageType == ImageType::OPENSLIDE) { if (imageType == ImageType::OPENSLIDE) {
option->set("level", descriptor->level); option->set("level", descriptor->level);
} }
if (imageType == ImageType::TIFF) {
option->set("subifd", descriptor->subifd);
}
image = VImage::new_from_file(descriptor->file.data(), option); image = VImage::new_from_file(descriptor->file.data(), option);
if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) { if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) {
image = SetDensity(image, descriptor->density); image = SetDensity(image, descriptor->density);
@@ -505,9 +525,8 @@ namespace sharp {
VImage SetDensity(VImage image, const double density) { VImage SetDensity(VImage image, const double density) {
const double pixelsPerMm = density / 25.4; const double pixelsPerMm = density / 25.4;
VImage copy = image.copy(); VImage copy = image.copy();
copy.set("Xres", pixelsPerMm); copy.get_image()->Xres = pixelsPerMm;
copy.set("Yres", pixelsPerMm); copy.get_image()->Yres = pixelsPerMm;
copy.set(VIPS_META_RESOLUTION_UNIT, "in");
return copy; return copy;
} }
@@ -797,10 +816,10 @@ namespace sharp {
/* /*
Ensures alpha channel, if missing. Ensures alpha channel, if missing.
*/ */
VImage EnsureAlpha(VImage image) { VImage EnsureAlpha(VImage image, double const value) {
if (!HasAlpha(image)) { if (!HasAlpha(image)) {
std::vector<double> alpha; std::vector<double> alpha;
alpha.push_back(sharp::MaximumImageAlpha(image.interpretation())); alpha.push_back(value * sharp::MaximumImageAlpha(image.interpretation()));
image = image.bandjoin_const(alpha); image = image.bandjoin_const(alpha);
} }
return image; return image;

View File

@@ -26,8 +26,8 @@
#if (VIPS_MAJOR_VERSION < 8) || \ #if (VIPS_MAJOR_VERSION < 8) || \
(VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION < 10) || \ (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION < 10) || \
(VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION == 10 && VIPS_MICRO_VERSION < 5) (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION == 10 && VIPS_MICRO_VERSION < 6)
#error "libvips version 8.10.5+ is required - please see https://sharp.pixelplumbing.com/install" #error "libvips version 8.10.6+ is required - please see https://sharp.pixelplumbing.com/install"
#endif #endif
#if ((!defined(__clang__)) && defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6))) #if ((!defined(__clang__)) && defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6)))
@@ -57,9 +57,11 @@ namespace sharp {
int rawChannels; int rawChannels;
int rawWidth; int rawWidth;
int rawHeight; int rawHeight;
bool rawPremultiplied;
int pages; int pages;
int page; int page;
int level; int level;
int subifd;
int createChannels; int createChannels;
int createWidth; int createWidth;
int createHeight; int createHeight;
@@ -79,9 +81,11 @@ namespace sharp {
rawChannels(0), rawChannels(0),
rawWidth(0), rawWidth(0),
rawHeight(0), rawHeight(0),
rawPremultiplied(false),
pages(1), pages(1),
page(0), page(0),
level(0), level(0),
subifd(-1),
createChannels(0), createChannels(0),
createWidth(0), createWidth(0),
createHeight(0), createHeight(0),
@@ -93,6 +97,7 @@ namespace sharp {
// Convenience methods to access the attributes of a Napi::Object // Convenience methods to access the attributes of a Napi::Object
bool HasAttr(Napi::Object obj, std::string attr); bool HasAttr(Napi::Object obj, std::string attr);
std::string AttrAsStr(Napi::Object obj, std::string attr); std::string AttrAsStr(Napi::Object obj, std::string attr);
std::string AttrAsStr(Napi::Object obj, unsigned int const attr);
uint32_t AttrAsUint32(Napi::Object obj, std::string attr); uint32_t AttrAsUint32(Napi::Object obj, std::string attr);
int32_t AttrAsInt32(Napi::Object obj, std::string attr); int32_t AttrAsInt32(Napi::Object obj, std::string attr);
int32_t AttrAsInt32(Napi::Object obj, unsigned int const attr); int32_t AttrAsInt32(Napi::Object obj, unsigned int const attr);
@@ -296,7 +301,7 @@ namespace sharp {
/* /*
Ensures alpha channel, if missing. Ensures alpha channel, if missing.
*/ */
VImage EnsureAlpha(VImage image); VImage EnsureAlpha(VImage image, double const value);
} // namespace sharp } // namespace sharp

View File

@@ -86,6 +86,9 @@ class MetadataWorker : public Napi::AsyncWorker {
baton->levels.push_back(std::pair<int, int>(width, height)); baton->levels.push_back(std::pair<int, int>(width, height));
} }
} }
if (image.get_typeof(VIPS_META_N_SUBIFDS) == G_TYPE_INT) {
baton->subifds = image.get_int(VIPS_META_N_SUBIFDS);
}
baton->hasProfile = sharp::HasProfile(image); baton->hasProfile = sharp::HasProfile(image);
// Derived attributes // Derived attributes
baton->hasAlpha = sharp::HasAlpha(image); baton->hasAlpha = sharp::HasAlpha(image);
@@ -203,6 +206,9 @@ class MetadataWorker : public Napi::AsyncWorker {
} }
info.Set("levels", levels); info.Set("levels", levels);
} }
if (baton->subifds > 0) {
info.Set("subifds", baton->subifds);
}
info.Set("hasProfile", baton->hasProfile); info.Set("hasProfile", baton->hasProfile);
info.Set("hasAlpha", baton->hasAlpha); info.Set("hasAlpha", baton->hasAlpha);
if (baton->orientation > 0) { if (baton->orientation > 0) {

View File

@@ -41,6 +41,7 @@ struct MetadataBaton {
int pagePrimary; int pagePrimary;
std::string compression; std::string compression;
std::vector<std::pair<int, int>> levels; std::vector<std::pair<int, int>> levels;
int subifds;
bool hasProfile; bool hasProfile;
bool hasAlpha; bool hasAlpha;
int orientation; int orientation;
@@ -68,6 +69,7 @@ struct MetadataBaton {
pageHeight(0), pageHeight(0),
loop(-1), loop(-1),
pagePrimary(-1), pagePrimary(-1),
subifds(0),
hasProfile(false), hasProfile(false),
hasAlpha(false), hasAlpha(false),
orientation(0), orientation(0),

View File

@@ -226,7 +226,8 @@ class PipelineWorker : public Napi::AsyncWorker {
if ( if (
xshrink == yshrink && xshrink >= 2 * shrink_on_load_factor && xshrink == yshrink && xshrink >= 2 * shrink_on_load_factor &&
(inputImageType == sharp::ImageType::JPEG || inputImageType == sharp::ImageType::WEBP) && (inputImageType == sharp::ImageType::JPEG || inputImageType == sharp::ImageType::WEBP) &&
baton->gamma == 0 && baton->topOffsetPre == -1 && baton->trimThreshold == 0.0 baton->gamma == 0 && baton->topOffsetPre == -1 && baton->trimThreshold == 0.0 &&
image.width() > 3 && image.height() > 3
) { ) {
if (xshrink >= 8 * shrink_on_load_factor) { if (xshrink >= 8 * shrink_on_load_factor) {
xfactor = xfactor / 8; xfactor = xfactor / 8;
@@ -346,7 +347,7 @@ class PipelineWorker : public Napi::AsyncWorker {
bool const shouldModulate = baton->brightness != 1.0 || baton->saturation != 1.0 || baton->hue != 0.0; bool const shouldModulate = baton->brightness != 1.0 || baton->saturation != 1.0 || baton->hue != 0.0;
if (shouldComposite && !sharp::HasAlpha(image)) { if (shouldComposite && !sharp::HasAlpha(image)) {
image = sharp::EnsureAlpha(image); image = sharp::EnsureAlpha(image, 1);
} }
bool const shouldPremultiplyAlpha = sharp::HasAlpha(image) && bool const shouldPremultiplyAlpha = sharp::HasAlpha(image) &&
@@ -562,9 +563,17 @@ class PipelineWorker : public Napi::AsyncWorker {
// Use gravity in overlay // Use gravity in overlay
if (compositeImage.width() <= baton->width) { if (compositeImage.width() <= baton->width) {
across = static_cast<int>(ceil(static_cast<double>(image.width()) / compositeImage.width())); across = static_cast<int>(ceil(static_cast<double>(image.width()) / compositeImage.width()));
// Ensure odd number of tiles across when gravity is centre, north or south
if (composite->gravity == 0 || composite->gravity == 1 || composite->gravity == 3) {
across |= 1;
}
} }
if (compositeImage.height() <= baton->height) { if (compositeImage.height() <= baton->height) {
down = static_cast<int>(ceil(static_cast<double>(image.height()) / compositeImage.height())); down = static_cast<int>(ceil(static_cast<double>(image.height()) / compositeImage.height()));
// Ensure odd number of tiles down when gravity is centre, east or west
if (composite->gravity == 0 || composite->gravity == 2 || composite->gravity == 4) {
down |= 1;
}
} }
if (across != 0 || down != 0) { if (across != 0 || down != 0) {
int left; int left;
@@ -586,7 +595,7 @@ class PipelineWorker : public Napi::AsyncWorker {
// Ensure image to composite is sRGB with premultiplied alpha // Ensure image to composite is sRGB with premultiplied alpha
compositeImage = compositeImage.colourspace(VIPS_INTERPRETATION_sRGB); compositeImage = compositeImage.colourspace(VIPS_INTERPRETATION_sRGB);
if (!sharp::HasAlpha(compositeImage)) { if (!sharp::HasAlpha(compositeImage)) {
compositeImage = sharp::EnsureAlpha(compositeImage); compositeImage = sharp::EnsureAlpha(compositeImage, 1);
} }
if (!composite->premultiplied) compositeImage = compositeImage.premultiply(); if (!composite->premultiplied) compositeImage = compositeImage.premultiply();
// Calculate position // Calculate position
@@ -594,8 +603,13 @@ class PipelineWorker : public Napi::AsyncWorker {
int top; int top;
if (composite->hasOffset) { if (composite->hasOffset) {
// Composite image at given offsets // Composite image at given offsets
if (composite->tile) {
std::tie(left, top) = sharp::CalculateCrop(image.width(), image.height(), std::tie(left, top) = sharp::CalculateCrop(image.width(), image.height(),
compositeImage.width(), compositeImage.height(), composite->left, composite->top); compositeImage.width(), compositeImage.height(), composite->left, composite->top);
} else {
left = composite->left;
top = composite->top;
}
} else { } else {
// Composite image with given gravity // Composite image with given gravity
std::tie(left, top) = sharp::CalculateCrop(image.width(), image.height(), std::tie(left, top) = sharp::CalculateCrop(image.width(), image.height(),
@@ -678,8 +692,8 @@ class PipelineWorker : public Napi::AsyncWorker {
} }
// Ensure alpha channel, if missing // Ensure alpha channel, if missing
if (baton->ensureAlpha) { if (baton->ensureAlpha != -1) {
image = sharp::EnsureAlpha(image); image = sharp::EnsureAlpha(image, baton->ensureAlpha);
} }
// Convert image to sRGB, if not already // Convert image to sRGB, if not already
@@ -704,11 +718,21 @@ class PipelineWorker : public Napi::AsyncWorker {
->set("input_profile", "srgb") ->set("input_profile", "srgb")
->set("intent", VIPS_INTENT_PERCEPTUAL)); ->set("intent", VIPS_INTENT_PERCEPTUAL));
} }
// Override EXIF Orientation tag // Override EXIF Orientation tag
if (baton->withMetadata && baton->withMetadataOrientation != -1) { if (baton->withMetadata && baton->withMetadataOrientation != -1) {
image = sharp::SetExifOrientation(image, baton->withMetadataOrientation); image = sharp::SetExifOrientation(image, baton->withMetadataOrientation);
} }
// Override pixel density
if (baton->withMetadataDensity > 0) {
image = sharp::SetDensity(image, baton->withMetadataDensity);
}
// Metadata key/value pairs, e.g. EXIF
if (!baton->withMetadataStrs.empty()) {
image = image.copy();
for (const auto& s : baton->withMetadataStrs) {
image.set(s.first.data(), s.second.data());
}
}
// Number of channels used in output image // Number of channels used in output image
baton->channels = image.bands(); baton->channels = image.bands();
@@ -1038,6 +1062,7 @@ class PipelineWorker : public Napi::AsyncWorker {
->set("angle", CalculateAngleRotation(baton->tileAngle)) ->set("angle", CalculateAngleRotation(baton->tileAngle))
->set("background", baton->tileBackground) ->set("background", baton->tileBackground)
->set("centre", baton->tileCentre) ->set("centre", baton->tileCentre)
->set("id", const_cast<char*>(baton->tileId.data()))
->set("skip_blanks", baton->tileSkipBlanks); ->set("skip_blanks", baton->tileSkipBlanks);
// libvips chooses a default depth based on layout. Instead of replicating that logic here by // libvips chooses a default depth based on layout. Instead of replicating that logic here by
// not passing anything - libvips will handle choice // not passing anything - libvips will handle choice
@@ -1327,7 +1352,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
baton->affineInterpolator = vips::VInterpolate::new_from_name(sharp::AttrAsStr(options, "affineInterpolator").data()); baton->affineInterpolator = vips::VInterpolate::new_from_name(sharp::AttrAsStr(options, "affineInterpolator").data());
baton->removeAlpha = sharp::AttrAsBool(options, "removeAlpha"); baton->removeAlpha = sharp::AttrAsBool(options, "removeAlpha");
baton->ensureAlpha = sharp::AttrAsBool(options, "ensureAlpha"); baton->ensureAlpha = sharp::AttrAsDouble(options, "ensureAlpha");
if (options.Has("boolean")) { if (options.Has("boolean")) {
baton->boolean = sharp::CreateInputDescriptor(options.Get("boolean").As<Napi::Object>()); baton->boolean = sharp::CreateInputDescriptor(options.Get("boolean").As<Napi::Object>());
baton->booleanOp = sharp::GetBooleanOperation(sharp::AttrAsStr(options, "booleanOp")); baton->booleanOp = sharp::GetBooleanOperation(sharp::AttrAsStr(options, "booleanOp"));
@@ -1364,7 +1389,14 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
baton->fileOut = sharp::AttrAsStr(options, "fileOut"); baton->fileOut = sharp::AttrAsStr(options, "fileOut");
baton->withMetadata = sharp::AttrAsBool(options, "withMetadata"); baton->withMetadata = sharp::AttrAsBool(options, "withMetadata");
baton->withMetadataOrientation = sharp::AttrAsUint32(options, "withMetadataOrientation"); baton->withMetadataOrientation = sharp::AttrAsUint32(options, "withMetadataOrientation");
baton->withMetadataDensity = sharp::AttrAsDouble(options, "withMetadataDensity");
baton->withMetadataIcc = sharp::AttrAsStr(options, "withMetadataIcc"); baton->withMetadataIcc = sharp::AttrAsStr(options, "withMetadataIcc");
Napi::Object mdStrs = options.Get("withMetadataStrs").As<Napi::Object>();
Napi::Array mdStrKeys = mdStrs.GetPropertyNames();
for (unsigned int i = 0; i < mdStrKeys.Length(); i++) {
std::string k = sharp::AttrAsStr(mdStrKeys, i);
baton->withMetadataStrs.insert(std::make_pair(k, sharp::AttrAsStr(mdStrs, k)));
}
// Format-specific // Format-specific
baton->jpegQuality = sharp::AttrAsUint32(options, "jpegQuality"); baton->jpegQuality = sharp::AttrAsUint32(options, "jpegQuality");
baton->jpegProgressive = sharp::AttrAsBool(options, "jpegProgressive"); baton->jpegProgressive = sharp::AttrAsBool(options, "jpegProgressive");
@@ -1438,6 +1470,7 @@ Napi::Value pipeline(const Napi::CallbackInfo& info) {
vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_DZ_DEPTH, vips_enum_from_nick(nullptr, VIPS_TYPE_FOREIGN_DZ_DEPTH,
sharp::AttrAsStr(options, "tileDepth").data())); sharp::AttrAsStr(options, "tileDepth").data()));
baton->tileCentre = sharp::AttrAsBool(options, "tileCentre"); baton->tileCentre = sharp::AttrAsBool(options, "tileCentre");
baton->tileId = sharp::AttrAsStr(options, "tileId");
// Force random access for certain operations // Force random access for certain operations
if (baton->input->access == VIPS_ACCESS_SEQUENTIAL) { if (baton->input->access == VIPS_ACCESS_SEQUENTIAL) {

View File

@@ -18,6 +18,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include <unordered_map>
#include <napi.h> #include <napi.h>
#include <vips/vips8> #include <vips/vips8>
@@ -167,7 +168,9 @@ struct PipelineBaton {
std::string err; std::string err;
bool withMetadata; bool withMetadata;
int withMetadataOrientation; int withMetadataOrientation;
double withMetadataDensity;
std::string withMetadataIcc; std::string withMetadataIcc;
std::unordered_map<std::string, std::string> withMetadataStrs;
std::unique_ptr<double[]> convKernel; std::unique_ptr<double[]> convKernel;
int convKernelWidth; int convKernelWidth;
int convKernelHeight; int convKernelHeight;
@@ -178,7 +181,7 @@ struct PipelineBaton {
VipsOperationBoolean bandBoolOp; VipsOperationBoolean bandBoolOp;
int extractChannel; int extractChannel;
bool removeAlpha; bool removeAlpha;
bool ensureAlpha; double ensureAlpha;
VipsInterpretation colourspace; VipsInterpretation colourspace;
int pageHeight; int pageHeight;
std::vector<int> delay; std::vector<int> delay;
@@ -192,6 +195,7 @@ struct PipelineBaton {
std::vector<double> tileBackground; std::vector<double> tileBackground;
int tileSkipBlanks; int tileSkipBlanks;
VipsForeignDzDepth tileDepth; VipsForeignDzDepth tileDepth;
std::string tileId;
std::unique_ptr<double[]> recombMatrix; std::unique_ptr<double[]> recombMatrix;
PipelineBaton(): PipelineBaton():
@@ -258,7 +262,7 @@ struct PipelineBaton {
jpegOptimiseScans(false), jpegOptimiseScans(false),
jpegOptimiseCoding(true), jpegOptimiseCoding(true),
pngProgressive(false), pngProgressive(false),
pngCompressionLevel(9), pngCompressionLevel(6),
pngAdaptiveFiltering(false), pngAdaptiveFiltering(false),
pngPalette(false), pngPalette(false),
pngQuality(100), pngQuality(100),
@@ -287,6 +291,7 @@ struct PipelineBaton {
heifLossless(false), heifLossless(false),
withMetadata(false), withMetadata(false),
withMetadataOrientation(-1), withMetadataOrientation(-1),
withMetadataDensity(0.0),
convKernelWidth(0), convKernelWidth(0),
convKernelHeight(0), convKernelHeight(0),
convKernelScale(0.0), convKernelScale(0.0),
@@ -296,7 +301,7 @@ struct PipelineBaton {
bandBoolOp(VIPS_OPERATION_BOOLEAN_LAST), bandBoolOp(VIPS_OPERATION_BOOLEAN_LAST),
extractChannel(-1), extractChannel(-1),
removeAlpha(false), removeAlpha(false),
ensureAlpha(false), ensureAlpha(-1.0),
colourspace(VIPS_INTERPRETATION_LAST), colourspace(VIPS_INTERPRETATION_LAST),
pageHeight(0), pageHeight(0),
delay{-1}, delay{-1},

View File

@@ -44,6 +44,7 @@ Napi::Object init(Napi::Env env, Napi::Object exports) {
exports.Set("libvipsVersion", Napi::Function::New(env, libvipsVersion)); exports.Set("libvipsVersion", Napi::Function::New(env, libvipsVersion));
exports.Set("format", Napi::Function::New(env, format)); exports.Set("format", Napi::Function::New(env, format));
exports.Set("_maxColourDistance", Napi::Function::New(env, _maxColourDistance)); exports.Set("_maxColourDistance", Napi::Function::New(env, _maxColourDistance));
exports.Set("_isUsingJemalloc", Napi::Function::New(env, _isUsingJemalloc));
exports.Set("stats", Napi::Function::New(env, stats)); exports.Set("stats", Napi::Function::New(env, stats));
return exports; return exports;
} }

View File

@@ -225,3 +225,19 @@ Napi::Value _maxColourDistance(const Napi::CallbackInfo& info) {
return Napi::Number::New(env, maxColourDistance); return Napi::Number::New(env, maxColourDistance);
} }
#if defined(__GNUC__)
// mallctl will be resolved by the runtime linker when jemalloc is being used
extern "C" {
int mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen) __attribute__((weak));
}
Napi::Value _isUsingJemalloc(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
return Napi::Boolean::New(env, mallctl != nullptr);
}
#else
Napi::Value _isUsingJemalloc(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
return Napi::Boolean::New(env, false);
}
#endif

View File

@@ -24,5 +24,6 @@ Napi::Value simd(const Napi::CallbackInfo& info);
Napi::Value libvipsVersion(const Napi::CallbackInfo& info); Napi::Value libvipsVersion(const Napi::CallbackInfo& info);
Napi::Value format(const Napi::CallbackInfo& info); Napi::Value format(const Napi::CallbackInfo& info);
Napi::Value _maxColourDistance(const Napi::CallbackInfo& info); Napi::Value _maxColourDistance(const Napi::CallbackInfo& info);
Napi::Value _isUsingJemalloc(const Napi::CallbackInfo& info);
#endif // SRC_UTILITIES_H_ #endif // SRC_UTILITIES_H_

View File

@@ -13,7 +13,7 @@
"gm": "1.23.1", "gm": "1.23.1",
"imagemagick": "0.1.3", "imagemagick": "0.1.3",
"jimp": "0.16.1", "jimp": "0.16.1",
"mapnik": "4.5.5", "mapnik": "4.5.6",
"semver": "7.3.4" "semver": "7.3.4"
}, },
"license": "Apache-2.0", "license": "Apache-2.0",

View File

@@ -1,5 +1,6 @@
'use strict'; 'use strict';
const os = require('os');
const fs = require('fs'); const fs = require('fs');
const async = require('async'); const async = require('async');
@@ -25,6 +26,9 @@ const height = 588;
// Disable libvips cache to ensure tests are as fair as they can be // Disable libvips cache to ensure tests are as fair as they can be
sharp.cache(false); sharp.cache(false);
// Spawn one thread per CPU
sharp.concurrency(os.cpus().length);
async.series({ async.series({
jpeg: function (callback) { jpeg: function (callback) {
const inputJpgBuffer = fs.readFileSync(fixtures.inputJpg); const inputJpgBuffer = fs.readFileSync(fixtures.inputJpg);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 664 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 426 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 692 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 653 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 93 KiB

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 93 KiB

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 93 KiB

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 93 KiB

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 94 KiB

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 96 KiB

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 95 KiB

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 95 KiB

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 95 KiB

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 96 KiB

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 94 KiB

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 92 KiB

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 606 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 672 KiB

View File

@@ -90,6 +90,7 @@ module.exports = {
inputPngEmbed: getPath('embedgravitybird.png'), // Released to sharp under a CC BY 4.0 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) inputPngRGBWithAlpha: getPath('2569067123_aca715a2ee_o.png'), // http://www.flickr.com/photos/grizdave/2569067123/ (same as inputJpg)
inputPngImageInAlpha: getPath('image-in-alpha.png'), // https://github.com/lovell/sharp/issues/1597 inputPngImageInAlpha: getPath('image-in-alpha.png'), // https://github.com/lovell/sharp/issues/1597
inputPngSolidAlpha: getPath('with-alpha.png'), // https://github.com/lovell/sharp/issues/1599
inputWebP: getPath('4.webp'), // http://www.gstatic.com/webp/gallery/4.webp 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 inputWebPWithTransparency: getPath('5_webp_a.webp'), // http://www.gstatic.com/webp/gallery3/5_webp_a.webp
@@ -109,7 +110,7 @@ module.exports = {
inputSvg: getPath('check.svg'), // http://dev.w3.org/SVG/tools/svgweb/samples/svg-files/check.svg inputSvg: getPath('check.svg'), // http://dev.w3.org/SVG/tools/svgweb/samples/svg-files/check.svg
inputSvgSmallViewBox: getPath('circle.svg'), inputSvgSmallViewBox: getPath('circle.svg'),
inputSvgWithEmbeddedImages: getPath('struct-image-04-t.svg'), // https://dev.w3.org/SVG/profiles/1.2T/test/svg/struct-image-04-t.svg inputSvgWithEmbeddedImages: getPath('struct-image-04-t.svg'), // https://dev.w3.org/SVG/profiles/1.2T/test/svg/struct-image-04-t.svg
inputAvif: getPath('cosmos_frame12924_yuv420_10bpc_bt2020_pq_q50.avif'), // CC by-nc-nd https://github.com/AOMediaCodec/av1-avif/tree/master/testFiles/Netflix inputAvif: getPath('sdr_cosmos12920_cicp1-13-6_yuv444_full_qp10.avif'), // CC by-nc-nd https://github.com/AOMediaCodec/av1-avif/tree/master/testFiles/Netflix
inputJPGBig: getPath('flowers.jpeg'), inputJPGBig: getPath('flowers.jpeg'),

Binary file not shown.

After

Width:  |  Height:  |  Size: 279 KiB

BIN
test/fixtures/with-alpha.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@@ -426,6 +426,16 @@
... ...
fun:vips_target_finish fun:vips_target_finish
} }
{
value8_libvips_static
Memcheck:Value8
obj:*/libvips-cpp.so.*
}
{
cond_libvips_static
Memcheck:Cond
obj:*/libvips-cpp.so.*
}
{ {
leak_libvips_init leak_libvips_init
Memcheck:Leak Memcheck:Leak
@@ -440,7 +450,7 @@
match-leak-kinds: definite match-leak-kinds: definite
fun:malloc fun:malloc
... ...
fun:rsvg_rust_handle_new_from_stream_sync fun:rsvg_handle_new_from_stream_sync
} }
{ {
leak_rsvg_rsvg_rust_handle_new_from_gfile_sync leak_rsvg_rsvg_rust_handle_new_from_gfile_sync
@@ -448,7 +458,7 @@
match-leak-kinds: definite match-leak-kinds: definite
fun:malloc fun:malloc
... ...
fun:rsvg_rust_handle_new_from_gfile_sync fun:rsvg_handle_new_from_gfile_sync
} }
{ {
leak_rsvg_rust_handle_new_from_stream_sync leak_rsvg_rust_handle_new_from_stream_sync
@@ -458,7 +468,7 @@
... ...
fun:xmlParseElement fun:xmlParseElement
... ...
fun:rsvg_rust_handle_new_from_stream_sync fun:rsvg_handle_new_from_stream_sync
} }
{ {
leak_rsvg_rust_handle_new_from_gfile_sync leak_rsvg_rust_handle_new_from_gfile_sync
@@ -468,7 +478,7 @@
... ...
fun:xmlParseElement fun:xmlParseElement
... ...
fun:rsvg_rust_handle_new_from_gfile_sync fun:rsvg_handle_new_from_gfile_sync
} }
# libuv warnings # libuv warnings

View File

@@ -19,6 +19,17 @@ describe('HTTP agent', function () {
assert.strictEqual(443, proxy.defaultPort); assert.strictEqual(443, proxy.defaultPort);
}); });
it('HTTPS proxy with auth from HTTPS_PROXY using credentials containing special characters', function () {
process.env.HTTPS_PROXY = 'https://user,:pass=@secure:123';
const proxy = agent();
delete process.env.HTTPS_PROXY;
assert.strictEqual('object', typeof proxy);
assert.strictEqual('secure', proxy.options.proxy.host);
assert.strictEqual(123, proxy.options.proxy.port);
assert.strictEqual('user,:pass=', proxy.options.proxy.proxyAuth);
assert.strictEqual(443, proxy.defaultPort);
});
it('HTTP proxy without auth from npm_config_proxy', function () { it('HTTP proxy without auth from npm_config_proxy', function () {
process.env.npm_config_proxy = 'http://plaintext:456'; process.env.npm_config_proxy = 'http://plaintext:456';
const proxy = agent(); const proxy = agent();

View File

@@ -155,4 +155,27 @@ describe('Alpha transparency', function () {
}); });
})); }));
}); });
it('Valid ensureAlpha value used for alpha channel', async () => {
const background = { r: 255, g: 0, b: 0 };
const [r, g, b, alpha] = await sharp({
create: {
width: 8,
height: 8,
channels: 3,
background
}
})
.ensureAlpha(0.5)
.raw()
.toBuffer();
assert.deepStrictEqual({ r, g, b, alpha }, { ...background, alpha: 127 });
});
it('Invalid ensureAlpha value throws', async () => {
assert.throws(() => {
sharp().ensureAlpha('fail');
});
});
}); });

View File

@@ -12,29 +12,6 @@ describe('AVIF', () => {
}); });
}); });
it('can passthrough AVIF', async () => {
const data = await sharp(inputAvif)
.resize(32)
.toBuffer();
const metadata = await sharp(data)
.metadata();
const { compression, size, ...metadataWithoutSize } = metadata;
assert.deepStrictEqual(metadataWithoutSize, {
channels: 3,
depth: 'uchar',
format: 'heif',
hasAlpha: false,
hasProfile: false,
height: 12,
isProgressive: false,
pageHeight: 12,
pagePrimary: 0,
pages: 1,
space: 'srgb',
width: 32
});
});
it('can convert AVIF to JPEG', async () => { it('can convert AVIF to JPEG', async () => {
const data = await sharp(inputAvif) const data = await sharp(inputAvif)
.resize(32) .resize(32)
@@ -81,4 +58,27 @@ describe('AVIF', () => {
width: 32 width: 32
}); });
}); });
it('can passthrough AVIF', async () => {
const data = await sharp(inputAvif)
.resize(32)
.toBuffer();
const metadata = await sharp(data)
.metadata();
const { compression, size, ...metadataWithoutSize } = metadata;
assert.deepStrictEqual(metadataWithoutSize, {
channels: 3,
depth: 'uchar',
format: 'heif',
hasAlpha: false,
hasProfile: false,
height: 12,
isProgressive: false,
pageHeight: 12,
pagePrimary: 0,
pages: 1,
space: 'srgb',
width: 32
});
});
}); });

View File

@@ -42,18 +42,15 @@ describe('Colour space conversion', function () {
}); });
}); });
if (sharp.format.tiff.input.file && sharp.format.webp.output.buffer) { it('From 1-bit TIFF to sRGB WebP', async () => {
it('From 1-bit TIFF to sRGB WebP [slow]', function (done) { const data = await sharp(fixtures.inputTiff)
sharp(fixtures.inputTiff) .resize(8, 8)
.webp() .webp()
.toBuffer(function (err, data, info) { .toBuffer();
if (err) throw err;
assert.strictEqual(true, data.length > 0); const { format } = await sharp(data).metadata();
assert.strictEqual('webp', info.format); assert.strictEqual(format, 'webp');
done();
}); });
});
}
it('From CMYK to sRGB', function (done) { it('From CMYK to sRGB', function (done) {
sharp(fixtures.inputJpgWithCmykProfile) sharp(fixtures.inputJpgWithCmykProfile)

View File

@@ -225,6 +225,24 @@ describe('composite', () => {
}); });
}); });
it('centre gravity should replicate correct number of tiles', async () => {
const red = { r: 255, g: 0, b: 0 };
const [r, g, b] = await sharp({
create: {
width: 40, height: 40, channels: 4, background: red
}
})
.composite([{
input: fixtures.inputPngWithTransparency16bit,
gravity: 'centre',
tile: true
}])
.raw()
.toBuffer();
assert.deepStrictEqual({ r, g, b }, red);
});
it('cutout via dest-in', done => { it('cutout via dest-in', done => {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.resize(300, 300) .resize(300, 300)
@@ -390,4 +408,20 @@ describe('composite', () => {
}, /Expected valid gravity for gravity but received invalid of type string/); }, /Expected valid gravity for gravity but received invalid of type string/);
}); });
}); });
it('Allow offset beyond bottom/right edge', async () => {
const red = { r: 255, g: 0, b: 0 };
const blue = { r: 0, g: 0, b: 255 };
const [r, g, b] = await sharp({ create: { width: 2, height: 2, channels: 4, background: red } })
.composite([{
input: { create: { width: 2, height: 2, channels: 4, background: blue } },
top: 1,
left: 1
}])
.raw()
.toBuffer();
assert.deepStrictEqual(red, { r, g, b });
});
}); });

View File

@@ -41,7 +41,6 @@ describe('Extend', function () {
.resize(120) .resize(120)
.extend({ .extend({
top: 50, top: 50,
bottom: 0,
left: 10, left: 10,
right: 35, right: 35,
background: { r: 0, g: 0, b: 0, alpha: 0 } background: { r: 0, g: 0, b: 0, alpha: 0 }
@@ -64,18 +63,38 @@ describe('Extend', function () {
sharp().extend(-1); sharp().extend(-1);
}); });
}); });
it('partial object fails', function () { it('invalid top fails', () => {
assert.throws(function () { assert.throws(
sharp().extend({ top: 1 }); () => sharp().extend({ top: 'fail' }),
/Expected positive integer for top but received fail of type string/
);
}); });
it('invalid bottom fails', () => {
assert.throws(
() => sharp().extend({ bottom: -1 }),
/Expected positive integer for bottom but received -1 of type number/
);
});
it('invalid left fails', () => {
assert.throws(
() => sharp().extend({ left: 0.1 }),
/Expected positive integer for left but received 0.1 of type number/
);
});
it('invalid right fails', () => {
assert.throws(
() => sharp().extend({ right: {} }),
/Expected positive integer for right but received \[object Object\] of type object/
);
});
it('can set all edges apart from right', () => {
assert.doesNotThrow(() => sharp().extend({ top: 1, left: 2, bottom: 3 }));
}); });
it('should add alpha channel before extending with a transparent Background', function (done) { it('should add alpha channel before extending with a transparent Background', function (done) {
sharp(fixtures.inputJpgWithLandscapeExif1) sharp(fixtures.inputJpgWithLandscapeExif1)
.extend({ .extend({
top: 0,
bottom: 10, bottom: 10,
left: 0,
right: 10, right: 10,
background: { r: 0, g: 0, b: 0, alpha: 0 } background: { r: 0, g: 0, b: 0, alpha: 0 }
}) })
@@ -91,9 +110,7 @@ describe('Extend', function () {
it('PNG with 2 channels', function (done) { it('PNG with 2 channels', function (done) {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithGreyAlpha)
.extend({ .extend({
top: 0,
bottom: 20, bottom: 20,
left: 0,
right: 20, right: 20,
background: 'transparent' background: 'transparent'
}) })

View File

@@ -28,7 +28,6 @@ describe('Partial image extraction', function () {
}); });
}); });
if (sharp.format.webp.output.file) {
it('WebP', function (done) { it('WebP', function (done) {
sharp(fixtures.inputWebP) sharp(fixtures.inputWebP)
.extract({ left: 100, top: 50, width: 125, height: 200 }) .extract({ left: 100, top: 50, width: 125, height: 200 })
@@ -39,9 +38,7 @@ describe('Partial image extraction', function () {
fixtures.assertSimilar(fixtures.expected('extract.webp'), data, done); fixtures.assertSimilar(fixtures.expected('extract.webp'), data, done);
}); });
}); });
}
if (sharp.format.tiff.output.file) {
it('TIFF', function (done) { it('TIFF', function (done) {
sharp(fixtures.inputTiff) sharp(fixtures.inputTiff)
.extract({ left: 34, top: 63, width: 341, height: 529 }) .extract({ left: 34, top: 63, width: 341, height: 529 })
@@ -53,7 +50,6 @@ describe('Partial image extraction', function () {
fixtures.assertSimilar(fixtures.expected('extract.tiff'), data, done); fixtures.assertSimilar(fixtures.expected('extract.tiff'), data, done);
}); });
}); });
}
it('Before resize', function (done) { it('Before resize', function (done) {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)

View File

@@ -107,7 +107,7 @@ describe('Image channel extraction', function () {
}); });
}); });
it('Non-existant channel', function (done) { it('Non-existent channel', function (done) {
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.extractChannel(1) .extractChannel(1)
.toBuffer(function (err) { .toBuffer(function (err) {

View File

@@ -335,17 +335,6 @@ describe('Input/output', function () {
}); });
}); });
it('Fail when input is empty Buffer', function (done) {
sharp(Buffer.alloc(0)).toBuffer().then(function () {
assert(false);
done();
}).catch(function (err) {
assert(err instanceof Error);
assert.strictEqual('Input buffer contains unsupported image format', err.message);
done();
});
});
it('Fail when input is invalid Buffer', function (done) { it('Fail when input is invalid Buffer', function (done) {
sharp(Buffer.from([0x1, 0x2, 0x3, 0x4])).toBuffer().then(function () { sharp(Buffer.from([0x1, 0x2, 0x3, 0x4])).toBuffer().then(function () {
assert(false); assert(false);
@@ -718,6 +707,19 @@ describe('Input/output', function () {
sharp({ level: -1 }); sharp({ level: -1 });
}, /Expected integer between 0 and 256 for level but received -1 of type number/); }, /Expected integer between 0 and 256 for level but received -1 of type number/);
}); });
it('Valid subifd property', function () {
sharp({ subifd: 1 });
});
it('Invalid subifd property (string) throws', function () {
assert.throws(function () {
sharp({ subifd: '1' });
}, /Expected integer between -1 and 100000 for subifd but received 1 of type string/);
});
it('Invalid subifd property (float) throws', function () {
assert.throws(function () {
sharp({ subifd: 1.2 });
}, /Expected integer between -1 and 100000 for subifd but received 1.2 of type number/);
});
}); });
describe('create new image', function () { describe('create new image', function () {

View File

@@ -290,4 +290,24 @@ describe('JPEG', function () {
}); });
}); });
}); });
it('Can use mozjpeg defaults', async () => {
const withoutData = await sharp(fixtures.inputJpg)
.resize(32, 24)
.jpeg({ mozjpeg: false })
.toBuffer();
const withoutMeta = await sharp(withoutData).metadata();
assert.strictEqual(false, withoutMeta.isProgressive);
const withData = await sharp(fixtures.inputJpg)
.resize(32, 24)
.jpeg({ mozjpeg: true })
.toBuffer();
const withMeta = await sharp(withData).metadata();
assert.strictEqual(true, withMeta.isProgressive);
});
it('Invalid mozjpeg value throws error', () => {
assert.throws(() => sharp().jpeg({ mozjpeg: 'fail' }));
});
}); });

View File

@@ -105,4 +105,30 @@ describe('libvips binaries', function () {
assert.strictEqual(true, fs.existsSync(nestedDirPath)); assert.strictEqual(true, fs.existsSync(nestedDirPath));
}); });
}); });
describe('logger', function () {
const consoleLog = console.log;
const consoleError = console.error;
after(function () {
console.log = consoleLog;
console.error = consoleError;
});
it('logs an info message', function (done) {
console.log = function (msg) {
assert.strictEqual(msg, 'sharp: progress');
done();
};
libvips.log('progress');
});
it('logs an error message', function (done) {
console.error = function (msg) {
assert.strictEqual(msg, 'sharp: Installation error: problem');
done();
};
libvips.log(new Error('problem'));
});
});
}); });

View File

@@ -240,40 +240,38 @@ describe('Image metadata', function () {
}) })
); );
it('GIF via giflib', function (done) { it('GIF', function (done) {
sharp(fixtures.inputGif).metadata(function (err, metadata) { sharp(fixtures.inputGif).metadata(function (err, metadata) {
if (err) throw err; if (err) throw err;
assert.strictEqual('gif', metadata.format); assert.strictEqual('gif', metadata.format);
assert.strictEqual('undefined', typeof metadata.size); assert.strictEqual('undefined', typeof metadata.size);
assert.strictEqual(800, metadata.width); assert.strictEqual(800, metadata.width);
assert.strictEqual(533, metadata.height); assert.strictEqual(533, metadata.height);
assert.strictEqual(3, metadata.channels); assert.strictEqual(true, [3, 4].includes(metadata.channels)); // libvips 8.11.0 = 4
assert.strictEqual('uchar', metadata.depth); assert.strictEqual('uchar', metadata.depth);
assert.strictEqual('undefined', typeof metadata.density); assert.strictEqual('undefined', typeof metadata.density);
assert.strictEqual('undefined', typeof metadata.chromaSubsampling); assert.strictEqual('undefined', typeof metadata.chromaSubsampling);
assert.strictEqual(false, metadata.isProgressive); assert.strictEqual(false, metadata.isProgressive);
assert.strictEqual(false, metadata.hasProfile); assert.strictEqual(false, metadata.hasProfile);
assert.strictEqual(false, metadata.hasAlpha);
assert.strictEqual('undefined', typeof metadata.orientation); assert.strictEqual('undefined', typeof metadata.orientation);
assert.strictEqual('undefined', typeof metadata.exif); assert.strictEqual('undefined', typeof metadata.exif);
assert.strictEqual('undefined', typeof metadata.icc); assert.strictEqual('undefined', typeof metadata.icc);
done(); done();
}); });
}); });
it('GIF grey+alpha via giflib', function (done) { it('GIF grey+alpha', function (done) {
sharp(fixtures.inputGifGreyPlusAlpha).metadata(function (err, metadata) { sharp(fixtures.inputGifGreyPlusAlpha).metadata(function (err, metadata) {
if (err) throw err; if (err) throw err;
assert.strictEqual('gif', metadata.format); assert.strictEqual('gif', metadata.format);
assert.strictEqual('undefined', typeof metadata.size); assert.strictEqual('undefined', typeof metadata.size);
assert.strictEqual(2, metadata.width); assert.strictEqual(2, metadata.width);
assert.strictEqual(1, metadata.height); assert.strictEqual(1, metadata.height);
assert.strictEqual(2, metadata.channels); assert.strictEqual(true, [2, 4].includes(metadata.channels)); // libvips 8.11.0 = 4
assert.strictEqual('uchar', metadata.depth); assert.strictEqual('uchar', metadata.depth);
assert.strictEqual('undefined', typeof metadata.density); assert.strictEqual('undefined', typeof metadata.density);
assert.strictEqual('undefined', typeof metadata.chromaSubsampling); assert.strictEqual('undefined', typeof metadata.chromaSubsampling);
assert.strictEqual(false, metadata.isProgressive); assert.strictEqual(false, metadata.isProgressive);
assert.strictEqual(false, metadata.hasProfile); assert.strictEqual(false, metadata.hasProfile);
assert.strictEqual(true, metadata.hasAlpha);
assert.strictEqual('undefined', typeof metadata.orientation); assert.strictEqual('undefined', typeof metadata.orientation);
assert.strictEqual('undefined', typeof metadata.exif); assert.strictEqual('undefined', typeof metadata.exif);
assert.strictEqual('undefined', typeof metadata.icc); assert.strictEqual('undefined', typeof metadata.icc);
@@ -322,7 +320,7 @@ describe('Image metadata', function () {
assert.strictEqual(isProgressive, false); assert.strictEqual(isProgressive, false);
assert.strictEqual(pages, 10); assert.strictEqual(pages, 10);
assert.strictEqual(pageHeight, 285); assert.strictEqual(pageHeight, 285);
assert.strictEqual(loop, 3); assert.strictEqual(true, [2, 3].includes(loop)); // libvips 8.11.0 = 2
assert.deepStrictEqual(delay, [...Array(9).fill(3000), 15000]); assert.deepStrictEqual(delay, [...Array(9).fill(3000), 15000]);
assert.strictEqual(hasProfile, false); assert.strictEqual(hasProfile, false);
assert.strictEqual(hasAlpha, true); assert.strictEqual(hasAlpha, true);
@@ -599,6 +597,68 @@ describe('Image metadata', function () {
}); });
}); });
it('Add EXIF metadata to JPEG', async () => {
const data = await sharp({
create: {
width: 8,
height: 8,
channels: 3,
background: 'red'
}
})
.jpeg()
.withMetadata({
exif: {
IFD0: { Software: 'sharp' },
IFD2: { ExposureTime: '0.2' }
}
})
.toBuffer();
const { exif } = await sharp(data).metadata();
const parsedExif = exifReader(exif);
assert.strictEqual(parsedExif.image.Software, 'sharp');
assert.strictEqual(parsedExif.exif.ExposureTime, 0.2);
});
it('Set density of JPEG', async () => {
const data = await sharp({
create: {
width: 8,
height: 8,
channels: 3,
background: 'red'
}
})
.withMetadata({
density: 300
})
.jpeg()
.toBuffer();
const { density } = await sharp(data).metadata();
assert.strictEqual(density, 300);
});
it('Set density of PNG', async () => {
const data = await sharp({
create: {
width: 8,
height: 8,
channels: 3,
background: 'red'
}
})
.withMetadata({
density: 96
})
.png()
.toBuffer();
const { density } = await sharp(data).metadata();
assert.strictEqual(density, 96);
});
it('chromaSubsampling 4:4:4:4 CMYK JPEG', function () { it('chromaSubsampling 4:4:4:4 CMYK JPEG', function () {
return sharp(fixtures.inputJpgWithCmykProfile) return sharp(fixtures.inputJpgWithCmykProfile)
.metadata() .metadata()
@@ -712,10 +772,35 @@ describe('Image metadata', function () {
sharp().withMetadata({ orientation: 9 }); sharp().withMetadata({ orientation: 9 });
}); });
}); });
it('Non-numeric density', function () {
assert.throws(function () {
sharp().withMetadata({ density: '1' });
});
});
it('Negative density', function () {
assert.throws(function () {
sharp().withMetadata({ density: -1 });
});
});
it('Non string icc', function () { it('Non string icc', function () {
assert.throws(function () { assert.throws(function () {
sharp().withMetadata({ icc: true }); sharp().withMetadata({ icc: true });
}); });
}); });
it('Non object exif', function () {
assert.throws(function () {
sharp().withMetadata({ exif: false });
});
});
it('Non string value in object exif', function () {
assert.throws(function () {
sharp().withMetadata({ exif: { ifd0: false } });
});
});
it('Non string value in nested object exif', function () {
assert.throws(function () {
sharp().withMetadata({ exif: { ifd0: { fail: false } } });
});
});
}); });
}); });

View File

@@ -28,115 +28,138 @@ describe('Modulate', function () {
}); });
}); });
it('should be able to hue-rotate', function () { it('should be able to hue-rotate', async () => {
const base = 'modulate-hue-120.jpg'; const [r, g, b] = await sharp({
const actual = fixtures.path('output.' + base); create: {
const expected = fixtures.expected(base); width: 1,
height: 1,
return sharp(fixtures.inputJpg) channels: 3,
background: { r: 153, g: 68, b: 68 }
}
})
.modulate({ hue: 120 }) .modulate({ hue: 120 })
.toFile(actual) .raw()
.then(function () { .toBuffer();
fixtures.assertMaxColourDistance(actual, expected, 25);
}); assert.deepStrictEqual({ r: 41, g: 107, b: 57 }, { r, g, b });
}); });
it('should be able to brighten', function () { it('should be able to brighten', async () => {
const base = 'modulate-brightness-2.jpg'; const [r, g, b] = await sharp({
const actual = fixtures.path('output.' + base); create: {
const expected = fixtures.expected(base); width: 1,
height: 1,
return sharp(fixtures.inputJpg) channels: 3,
background: { r: 153, g: 68, b: 68 }
}
})
.modulate({ brightness: 2 }) .modulate({ brightness: 2 })
.toFile(actual) .raw()
.then(function () { .toBuffer();
fixtures.assertMaxColourDistance(actual, expected, 25);
}); assert.deepStrictEqual({ r: 255, g: 173, b: 168 }, { r, g, b });
}); });
it('should be able to unbrighten', function () { it('should be able to darken', async () => {
const base = 'modulate-brightness-0-5.jpg'; const [r, g, b] = await sharp({
const actual = fixtures.path('output.' + base); create: {
const expected = fixtures.expected(base); width: 1,
height: 1,
return sharp(fixtures.inputJpg) channels: 3,
background: { r: 153, g: 68, b: 68 }
}
})
.modulate({ brightness: 0.5 }) .modulate({ brightness: 0.5 })
.toFile(actual) .raw()
.then(function () { .toBuffer();
fixtures.assertMaxColourDistance(actual, expected, 25);
}); assert.deepStrictEqual({ r: 97, g: 17, b: 25 }, { r, g, b });
}); });
it('should be able to saturate', function () { it('should be able to saturate', async () => {
const base = 'modulate-saturation-2.jpg'; const [r, g, b] = await sharp({
const actual = fixtures.path('output.' + base); create: {
const expected = fixtures.expected(base); width: 1,
height: 1,
return sharp(fixtures.inputJpg) channels: 3,
background: { r: 153, g: 68, b: 68 }
}
})
.modulate({ saturation: 2 }) .modulate({ saturation: 2 })
.toFile(actual) .raw()
.then(function () { .toBuffer();
fixtures.assertMaxColourDistance(actual, expected, 30);
}); assert.deepStrictEqual({ r: 198, g: 0, b: 43 }, { r, g, b });
}); });
it('should be able to desaturate', function () { it('should be able to desaturate', async () => {
const base = 'modulate-saturation-0.5.jpg'; const [r, g, b] = await sharp({
const actual = fixtures.path('output.' + base); create: {
const expected = fixtures.expected(base); width: 1,
height: 1,
return sharp(fixtures.inputJpg) channels: 3,
background: { r: 153, g: 68, b: 68 }
}
})
.modulate({ saturation: 0.5 }) .modulate({ saturation: 0.5 })
.toFile(actual) .raw()
.then(function () { .toBuffer();
fixtures.assertMaxColourDistance(actual, expected, 25);
}); assert.deepStrictEqual({ r: 127, g: 83, b: 81 }, { r, g, b });
}); });
it('should be able to modulate all channels', function () { it('should be able to modulate all channels', async () => {
const base = 'modulate-all.jpg'; const [r, g, b] = await sharp({
const actual = fixtures.path('output.' + base); create: {
const expected = fixtures.expected(base); width: 1,
height: 1,
return sharp(fixtures.inputJpg) channels: 3,
background: { r: 153, g: 68, b: 68 }
}
})
.modulate({ brightness: 2, saturation: 0.5, hue: 180 }) .modulate({ brightness: 2, saturation: 0.5, hue: 180 })
.toFile(actual) .raw()
.then(function () { .toBuffer();
fixtures.assertMaxColourDistance(actual, expected, 25);
}); assert.deepStrictEqual({ r: 149, g: 209, b: 214 }, { r, g, b });
}); });
describe('hue-rotate', function (done) { it('should be able to use linear and modulate together', async () => {
[30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360].forEach(function (angle) {
it('should properly hue rotate by ' + angle + 'deg', function () {
const base = 'modulate-hue-angle-' + angle + '.png';
const actual = fixtures.path('output.' + base);
const expected = fixtures.expected(base);
return sharp(fixtures.testPattern)
.modulate({ hue: angle })
.toFile(actual)
.then(function () {
fixtures.assertMaxColourDistance(actual, expected, 25);
});
});
});
});
it('should be able to use linear and modulate together', function () {
const base = 'modulate-linear.jpg';
const actual = fixtures.path('output.' + base);
const expected = fixtures.expected(base);
const contrast = 1.5; const contrast = 1.5;
const brightness = 0.5; const brightness = 0.5;
return sharp(fixtures.testPattern) const [r, g, b] = await sharp({
create: {
width: 1,
height: 1,
channels: 3,
background: { r: 153, g: 68, b: 68 }
}
})
.linear(contrast, -(128 * contrast) + 128) .linear(contrast, -(128 * contrast) + 128)
.modulate({ brightness }) .modulate({ brightness })
.raw()
.toBuffer();
assert.deepStrictEqual({ r: 81, g: 0, b: 0 }, { r, g, b });
});
describe('hue-rotate', () => {
[30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360].forEach(angle => {
it(`should hue rotate by ${angle} deg`, async () => {
const base = `modulate-hue-angle-${angle}.png`;
const actual = fixtures.path(`output.${base}`);
const expected = fixtures.expected(base);
await sharp(fixtures.testPattern)
.resize(320)
.modulate({ hue: angle })
.png({ compressionLevel: 0 })
.toFile(actual) .toFile(actual)
.then(function () { .then(() => {
fixtures.assertMaxColourDistance(actual, expected); fixtures.assertMaxColourDistance(actual, expected);
}); });
}); });
});
});
}); });

View File

@@ -54,6 +54,7 @@ describe('Normalization', function () {
it('keeps an existing alpha channel', function (done) { it('keeps an existing alpha channel', function (done) {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.resize(8, 8)
.normalize() .normalize()
.toBuffer(function (err, data) { .toBuffer(function (err, data) {
if (err) throw err; if (err) throw err;
@@ -69,6 +70,7 @@ describe('Normalization', function () {
it('keeps the alpha channel of greyscale images intact', function (done) { it('keeps the alpha channel of greyscale images intact', function (done) {
sharp(fixtures.inputPngWithGreyAlpha) sharp(fixtures.inputPngWithGreyAlpha)
.resize(8, 8)
.normalise() .normalise()
.toBuffer(function (err, data) { .toBuffer(function (err, data) {
if (err) throw err; if (err) throw err;

View File

@@ -32,23 +32,21 @@ describe('Platform-detection', function () {
delete process.env.npm_config_arch; delete process.env.npm_config_arch;
}); });
if (process.config.variables.arm_version) {
it('Can detect ARM version via process.config', function () { it('Can detect ARM version via process.config', function () {
process.env.npm_config_arch = 'arm'; process.env.npm_config_arch = 'arm';
const armVersion = process.config.variables.arm_version; assert.strictEqual(`armv${process.config.variables.arm_version}`, platform().split('-')[1]);
process.config.variables.arm_version = 'test';
assert.strictEqual('armvtest', platform().split('-')[1]);
process.config.variables.arm_version = armVersion;
delete process.env.npm_config_arch; delete process.env.npm_config_arch;
}); });
}
if (!process.config.variables.arm_version) {
it('Defaults to ARMv6 for 32-bit', function () { it('Defaults to ARMv6 for 32-bit', function () {
process.env.npm_config_arch = 'arm'; process.env.npm_config_arch = 'arm';
const armVersion = process.config.variables.arm_version;
delete process.config.variables.arm_version;
assert.strictEqual('armv6', platform().split('-')[1]); assert.strictEqual('armv6', platform().split('-')[1]);
process.config.variables.arm_version = armVersion;
delete process.env.npm_config_arch; delete process.env.npm_config_arch;
}); });
}
it('Defaults to ARMv8 for 64-bit', function () { it('Defaults to ARMv8 for 64-bit', function () {
process.env.npm_config_arch = 'arm64'; process.env.npm_config_arch = 'arm64';

View File

@@ -19,7 +19,7 @@ describe('PNG', function () {
}); });
}); });
it('default compressionLevel generates smaller file than compressionLevel=6', function (done) { it('default compressionLevel generates smaller file than compressionLevel=0', function (done) {
// First generate with default compressionLevel // First generate with default compressionLevel
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.resize(320, 240) .resize(320, 240)
@@ -31,7 +31,7 @@ describe('PNG', function () {
// Then generate with compressionLevel=6 // Then generate with compressionLevel=6
sharp(fixtures.inputPng) sharp(fixtures.inputPng)
.resize(320, 240) .resize(320, 240)
.png({ compressionLevel: 6 }) .png({ compressionLevel: 0 })
.toBuffer(function (err, largerData, largerInfo) { .toBuffer(function (err, largerData, largerInfo) {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, largerData.length > 0); assert.strictEqual(true, largerData.length > 0);

View File

@@ -7,6 +7,18 @@ const fixtures = require('../fixtures');
describe('Raw pixel data', function () { describe('Raw pixel data', function () {
describe('Raw pixel input', function () { describe('Raw pixel input', function () {
it('Empty data', function () {
assert.throws(function () {
sharp(Buffer.from(''));
}, /empty/);
assert.throws(function () {
sharp(new Uint8Array(0));
}, /empty/);
assert.throws(function () {
sharp(new Uint8ClampedArray(0));
}, /empty/);
});
it('Missing options', function () { it('Missing options', function () {
assert.throws(function () { assert.throws(function () {
sharp({ raw: {} }); sharp({ raw: {} });
@@ -95,6 +107,52 @@ describe('Raw pixel data', function () {
}); });
}); });
it('RGBA premultiplied', function (done) {
// Convert to raw pixel data
sharp(fixtures.inputPngSolidAlpha)
.resize(256)
.raw()
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(256, info.width);
assert.strictEqual(192, info.height);
assert.strictEqual(4, info.channels);
const originalData = Buffer.from(data);
// Premultiply image data
for (let i = 0; i < data.length; i += 4) {
const alpha = data[i + 3];
const norm = alpha / 255;
if (alpha < 255) {
data[i] = Math.round(data[i] * norm);
data[i + 1] = Math.round(data[i + 1] * norm);
data[i + 2] = Math.round(data[i + 2] * norm);
}
}
// Convert back to PNG
sharp(data, {
raw: {
width: info.width,
height: info.height,
channels: info.channels,
premultiplied: true
}
})
.raw()
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(256, info.width);
assert.strictEqual(192, info.height);
assert.strictEqual(4, info.channels);
assert.equal(data.compare(originalData), 0, 'output buffer matches unpremultiplied input buffer');
done();
});
});
});
it('JPEG to raw Stream and back again', function (done) { it('JPEG to raw Stream and back again', function (done) {
const width = 32; const width = 32;
const height = 24; const height = 24;

View File

@@ -605,6 +605,26 @@ describe('Resize dimensions', function () {
}); });
}); });
it('Skip shrink-on-load where one dimension <4px', async () => {
const jpeg = await sharp({
create: {
width: 100,
height: 3,
channels: 3,
background: 'red'
}
})
.jpeg()
.toBuffer();
const { info } = await sharp(jpeg)
.resize(8)
.toBuffer({ resolveWithObject: true });
assert.strictEqual(info.width, 8);
assert.strictEqual(info.height, 1);
});
it('unknown kernel throws', function () { it('unknown kernel throws', function () {
assert.throws(function () { assert.throws(function () {
sharp().resize(null, null, { kernel: 'unknown' }); sharp().resize(null, null, { kernel: 'unknown' });

View File

@@ -1,5 +1,6 @@
'use strict'; 'use strict';
const fs = require('fs');
const assert = require('assert'); const assert = require('assert');
const sharp = require('../../'); const sharp = require('../../');
@@ -99,4 +100,18 @@ describe('SVG input', function () {
fixtures.assertSimilar(fixtures.expected('svg-embedded.png'), data, done); fixtures.assertSimilar(fixtures.expected('svg-embedded.png'), data, done);
}); });
}); });
it('Converts SVG with truncated embedded PNG', async () => {
const truncatedPng = fs.readFileSync(fixtures.inputPngTruncated).toString('base64');
const svg = `<?xml version="1.0" encoding="UTF-8"?>
<svg width="294" height="240" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<image width="294" height="240" xlink:href="data:image/png;base64,${truncatedPng}"/>
</svg>`;
const { info } = await sharp(Buffer.from(svg)).toBuffer({ resolveWithObject: true });
assert.strictEqual(info.format, 'png');
assert.strictEqual(info.width, 294);
assert.strictEqual(info.height, 240);
assert.strictEqual(info.channels, 4);
});
}); });

View File

@@ -90,9 +90,11 @@ describe('TIFF', function () {
it('Increasing TIFF quality increases file size', () => it('Increasing TIFF quality increases file size', () =>
sharp(fixtures.inputJpgWithLandscapeExif1) sharp(fixtures.inputJpgWithLandscapeExif1)
.resize(320, 240)
.tiff({ quality: 40 }) .tiff({ quality: 40 })
.toBuffer() .toBuffer()
.then(tiff40 => sharp(fixtures.inputJpgWithLandscapeExif1) .then(tiff40 => sharp(fixtures.inputJpgWithLandscapeExif1)
.resize(320, 240)
.tiff({ quality: 90 }) .tiff({ quality: 90 })
.toBuffer() .toBuffer()
.then(tiff90 => .then(tiff90 =>
@@ -155,6 +157,7 @@ describe('TIFF', function () {
it('TIFF setting xres and yres on file', () => it('TIFF setting xres and yres on file', () =>
sharp(fixtures.inputTiff) sharp(fixtures.inputTiff)
.resize(8, 8)
.tiff({ .tiff({
xres: 1000, xres: 1000,
yres: 1000 yres: 1000
@@ -171,6 +174,7 @@ describe('TIFF', function () {
it('TIFF setting xres and yres on buffer', () => it('TIFF setting xres and yres on buffer', () =>
sharp(fixtures.inputTiff) sharp(fixtures.inputTiff)
.resize(8, 8)
.tiff({ .tiff({
xres: 1000, xres: 1000,
yres: 1000 yres: 1000
@@ -279,7 +283,7 @@ describe('TIFF', function () {
}); });
}); });
it('TIFF deflate compression of integral input with float predictor increases file size', function (done) { it('TIFF deflate compression with float predictor shrinks test file', function (done) {
const startSize = fs.statSync(fixtures.inputTiffUncompressed).size; const startSize = fs.statSync(fixtures.inputTiffUncompressed).size;
sharp(fixtures.inputTiffUncompressed) sharp(fixtures.inputTiffUncompressed)
.tiff({ .tiff({
@@ -289,7 +293,7 @@ describe('TIFF', function () {
.toFile(outputTiff, (err, info) => { .toFile(outputTiff, (err, info) => {
if (err) throw err; if (err) throw err;
assert.strictEqual('tiff', info.format); assert.strictEqual('tiff', info.format);
assert(info.size > startSize); assert(startSize > info.size);
rimraf(outputTiff, done); rimraf(outputTiff, done);
}); });
}); });

View File

@@ -297,6 +297,22 @@ describe('Tile', function () {
}); });
}); });
it('Valid id parameter value passes', function () {
assert.doesNotThrow(function () {
sharp().tile({
id: 'test'
});
});
});
it('Invalid id parameter value fails', function () {
assert.throws(function () {
sharp().tile({
id: true
});
});
});
it('Deep Zoom layout', function (done) { it('Deep Zoom layout', function (done) {
const directory = fixtures.path('output.dzi_files'); const directory = fixtures.path('output.dzi_files');
rimraf(directory, function () { rimraf(directory, function () {
@@ -613,7 +629,7 @@ describe('Tile', function () {
rimraf(directory, function () { rimraf(directory, function () {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.png({ .png({
compressionLevel: 1 compressionLevel: 0
}) })
.tile({ .tile({
layout: 'google' layout: 'google'
@@ -650,7 +666,8 @@ describe('Tile', function () {
rimraf(directory, function () { rimraf(directory, function () {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.webp({ .webp({
quality: 1 quality: 1,
reductionEffort: 0
}) })
.tile({ .tile({
layout: 'google' layout: 'google'
@@ -814,11 +831,14 @@ describe('Tile', function () {
}); });
it('IIIF layout', function (done) { it('IIIF layout', function (done) {
const directory = fixtures.path('output.iiif.info'); const name = 'output.iiif.info';
const directory = fixtures.path(name);
rimraf(directory, function () { rimraf(directory, function () {
const id = 'https://sharp.test.com/iiif';
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.tile({ .tile({
layout: 'iiif' layout: 'iiif',
id
}) })
.toFile(directory, function (err, info) { .toFile(directory, function (err, info) {
if (err) throw err; if (err) throw err;
@@ -827,6 +847,8 @@ describe('Tile', function () {
assert.strictEqual(2225, info.height); assert.strictEqual(2225, info.height);
assert.strictEqual(3, info.channels); assert.strictEqual(3, info.channels);
assert.strictEqual('number', typeof info.size); assert.strictEqual('number', typeof info.size);
const infoJson = require(path.join(directory, 'info.json'));
assert.strictEqual(`${id}/${name}`, infoJson['@id']);
fs.stat(path.join(directory, '0,0,256,256', '256,', '0', 'default.jpg'), function (err, stat) { fs.stat(path.join(directory, '0,0,256,256', '256,', '0', 'default.jpg'), function (err, stat) {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, stat.isFile()); assert.strictEqual(true, stat.isFile());

View File

@@ -5,22 +5,23 @@ const sharp = require('../../');
const fixtures = require('../fixtures'); const fixtures = require('../fixtures');
describe('toFormat', () => { describe('toFormat', () => {
it('accepts upper case characters as format parameter (string)', function (done) { it('accepts upper case characters as format parameter (string)', async () => {
sharp(fixtures.inputJpg) const data = await sharp(fixtures.inputJpg)
.resize(8, 8)
.toFormat('PNG') .toFormat('PNG')
.toBuffer(function (err, data, info) { .toBuffer();
if (err) throw err;
assert.strictEqual('png', info.format); const { format } = await sharp(data).metadata();
done(); assert.strictEqual(format, 'png');
}); });
});
it('accepts upper case characters as format parameter (object)', function (done) { it('accepts upper case characters as format parameter (object)', async () => {
sharp(fixtures.inputJpg) const data = await sharp(fixtures.inputJpg)
.resize(8, 8)
.toFormat({ id: 'PNG' }) .toFormat({ id: 'PNG' })
.toBuffer(function (err, data, info) { .toBuffer();
if (err) throw err;
assert.strictEqual('png', info.format); const { format } = await sharp(data).metadata();
done(); assert.strictEqual(format, 'png');
});
}); });
}); });

View File

@@ -3,8 +3,6 @@
const assert = require('assert'); const assert = require('assert');
const sharp = require('../../'); const sharp = require('../../');
const defaultConcurrency = sharp.concurrency();
describe('Utilities', function () { describe('Utilities', function () {
describe('Cache', function () { describe('Cache', function () {
it('Can be disabled', function () { it('Can be disabled', function () {
@@ -60,10 +58,10 @@ describe('Utilities', function () {
}); });
it('Can be reset to default', function () { it('Can be reset to default', function () {
sharp.concurrency(0); sharp.concurrency(0);
assert.strictEqual(defaultConcurrency, sharp.concurrency()); assert.strictEqual(true, sharp.concurrency() > 0);
}); });
it('Ignores invalid values', function () { it('Ignores invalid values', function () {
sharp.concurrency(0); const defaultConcurrency = sharp.concurrency();
sharp.concurrency('spoons'); sharp.concurrency('spoons');
assert.strictEqual(defaultConcurrency, sharp.concurrency()); assert.strictEqual(defaultConcurrency, sharp.concurrency());
}); });