sharp 0.20.8 → 0.21.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -11
- package/binding.gyp +16 -1
- package/docs/api-channel.md +16 -0
- package/docs/api-colour.md +1 -19
- package/docs/api-composite.md +2 -3
- package/docs/api-constructor.md +2 -3
- package/docs/api-input.md +7 -2
- package/docs/api-operation.md +73 -115
- package/docs/api-output.md +9 -0
- package/docs/api-resize.md +160 -108
- package/docs/api-utility.md +4 -7
- package/docs/changelog.md +102 -0
- package/docs/image/sharp-logo.svg +5 -0
- package/docs/index.md +12 -6
- package/docs/install.md +41 -42
- package/docs/performance.md +14 -21
- package/install/libvips.js +28 -24
- package/lib/channel.js +20 -4
- package/lib/colour.js +35 -18
- package/lib/composite.js +2 -3
- package/lib/constructor.js +27 -42
- package/lib/input.js +9 -6
- package/lib/libvips.js +7 -1
- package/lib/operation.js +81 -123
- package/lib/output.js +128 -60
- package/lib/platform.js +4 -1
- package/lib/resize.js +310 -107
- package/lib/utility.js +5 -8
- package/package.json +30 -21
- package/src/common.cc +59 -14
- package/src/common.h +15 -12
- package/src/libvips/cplusplus/VImage.cpp +95 -95
- package/src/libvips/cplusplus/vips-operators.cpp +268 -185
- package/src/metadata.cc +34 -1
- package/src/metadata.h +10 -1
- package/src/operations.cc +47 -49
- package/src/operations.h +13 -2
- package/src/pipeline.cc +101 -95
- package/src/pipeline.h +40 -14
- package/src/sharp.cc +1 -1
- package/src/stats.cc +1 -1
- package/src/stats.h +1 -1
- package/src/utilities.cc +2 -2
- package/src/utilities.h +1 -1
package/docs/install.md
CHANGED
|
@@ -15,7 +15,7 @@ yarn add sharp
|
|
|
15
15
|
### Building from source
|
|
16
16
|
|
|
17
17
|
Pre-compiled binaries for sharp are provided for use with
|
|
18
|
-
Node versions
|
|
18
|
+
Node versions 6, 8, 10 and 11 on
|
|
19
19
|
64-bit Windows, OS X and Linux platforms.
|
|
20
20
|
|
|
21
21
|
Sharp will be built from source at install time when:
|
|
@@ -27,7 +27,7 @@ Sharp will be built from source at install time when:
|
|
|
27
27
|
Building from source requires:
|
|
28
28
|
|
|
29
29
|
* C++11 compatible compiler such as gcc 4.8+, clang 3.0+ or MSVC 2013+
|
|
30
|
-
* [node-gyp](https://github.com/
|
|
30
|
+
* [node-gyp](https://github.com/nodejs/node-gyp#installation) and its dependencies (includes Python 2.7)
|
|
31
31
|
|
|
32
32
|
## libvips
|
|
33
33
|
|
|
@@ -36,13 +36,14 @@ Building from source requires:
|
|
|
36
36
|
[](https://travis-ci.org/lovell/sharp)
|
|
37
37
|
|
|
38
38
|
libvips and its dependencies are fetched and stored within `node_modules/sharp/vendor` during `npm install`.
|
|
39
|
-
This involves an automated HTTPS download of approximately
|
|
39
|
+
This involves an automated HTTPS download of approximately 8MB.
|
|
40
40
|
|
|
41
|
-
Most
|
|
41
|
+
Most Linux-based (glibc, musl) operating systems running on x64 and ARMv6+ CPUs should "just work", e.g.:
|
|
42
42
|
|
|
43
43
|
* Debian 7+
|
|
44
44
|
* Ubuntu 14.04+
|
|
45
45
|
* Centos 7+
|
|
46
|
+
* Alpine 3.8+ (Node 8 and 10)
|
|
46
47
|
* Fedora
|
|
47
48
|
* openSUSE 13.2+
|
|
48
49
|
* Archlinux
|
|
@@ -61,9 +62,9 @@ and `LD_LIBRARY_PATH` at runtime.
|
|
|
61
62
|
This allows the use of newer versions of libvips with older versions of sharp.
|
|
62
63
|
|
|
63
64
|
For 32-bit Intel CPUs and older Linux-based operating systems such as Centos 6,
|
|
64
|
-
|
|
65
|
+
compiling libvips from source is recommended.
|
|
65
66
|
|
|
66
|
-
https://
|
|
67
|
+
[https://libvips.github.io/libvips/install.html#building-libvips-from-a-source-tarball](https://libvips.github.io/libvips/install.html#building-libvips-from-a-source-tarball)
|
|
67
68
|
|
|
68
69
|
#### Alpine Linux
|
|
69
70
|
|
|
@@ -71,7 +72,9 @@ libvips is available in the
|
|
|
71
72
|
[testing repository](https://pkgs.alpinelinux.org/packages?name=vips-dev):
|
|
72
73
|
|
|
73
74
|
```sh
|
|
74
|
-
apk add vips-dev fftw-dev --update-cache
|
|
75
|
+
apk add vips-dev fftw-dev build-base --update-cache \
|
|
76
|
+
--repository https://alpine.global.ssl.fastly.net/alpine/edge/testing/ \
|
|
77
|
+
--repository https://alpine.global.ssl.fastly.net/alpine/edge/main
|
|
75
78
|
```
|
|
76
79
|
|
|
77
80
|
The smaller stack size of musl libc means
|
|
@@ -94,7 +97,8 @@ that it can be located using `pkg-config --modversion vips-cpp`.
|
|
|
94
97
|
[](https://ci.appveyor.com/project/lovell/sharp)
|
|
95
98
|
|
|
96
99
|
libvips and its dependencies are fetched and stored within `node_modules\sharp\vendor` during `npm install`.
|
|
97
|
-
This involves an automated HTTPS download of approximately
|
|
100
|
+
This involves an automated HTTPS download of approximately 13MB. If you are having issues during
|
|
101
|
+
installation consider removing the directory ```C:\Users\[user]\AppData\Roaming\npm-cache\_libvips```.
|
|
98
102
|
|
|
99
103
|
Only 64-bit (x64) `node.exe` is supported.
|
|
100
104
|
|
|
@@ -117,9 +121,6 @@ https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=193528
|
|
|
117
121
|
|
|
118
122
|
### Heroku
|
|
119
123
|
|
|
120
|
-
libvips and its dependencies are fetched and stored within `node_modules\sharp\vendor` during `npm install`.
|
|
121
|
-
This involves an automated HTTPS download of approximately 7MB.
|
|
122
|
-
|
|
123
124
|
Set [NODE_MODULES_CACHE](https://devcenter.heroku.com/articles/nodejs-support#cache-behavior)
|
|
124
125
|
to `false` when using the `yarn` package manager.
|
|
125
126
|
|
|
@@ -148,18 +149,28 @@ docker pull tailor/docker-libvips
|
|
|
148
149
|
|
|
149
150
|
### AWS Lambda
|
|
150
151
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
152
|
+
Set the Lambda runtime to Node.js 8.10.
|
|
153
|
+
|
|
154
|
+
The binaries in the `node_modules` directory of the
|
|
155
|
+
[deployment package](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html)
|
|
156
|
+
must be for the Linux x64 platform/architecture.
|
|
157
|
+
|
|
158
|
+
On non-Linux machines such as OS X and Windows run the following:
|
|
154
159
|
|
|
155
160
|
```sh
|
|
156
161
|
rm -rf node_modules/sharp
|
|
157
|
-
|
|
162
|
+
npm install --arch=x64 --platform=linux --target=8.10.0 sharp
|
|
158
163
|
```
|
|
159
164
|
|
|
160
|
-
|
|
165
|
+
Alternatively a Docker container closely matching the Lambda runtime can be used:
|
|
166
|
+
|
|
167
|
+
```sh
|
|
168
|
+
rm -rf node_modules/sharp
|
|
169
|
+
docker run -v "$PWD":/var/task lambci/lambda:build-nodejs8.10 npm install sharp
|
|
170
|
+
```
|
|
161
171
|
|
|
162
|
-
To get the best performance select the largest memory available.
|
|
172
|
+
To get the best performance select the largest memory available.
|
|
173
|
+
A 1536 MB function provides ~12x more CPU time than a 128 MB function.
|
|
163
174
|
|
|
164
175
|
### NW.js
|
|
165
176
|
|
|
@@ -171,7 +182,7 @@ nw-gyp rebuild --arch=x64 --target=[your nw version]
|
|
|
171
182
|
node node_modules/sharp/install/dll-copy
|
|
172
183
|
```
|
|
173
184
|
|
|
174
|
-
|
|
185
|
+
[http://docs.nwjs.io/en/latest/For%20Users/Advanced/Use%20Native%20Node%20Modules/](http://docs.nwjs.io/en/latest/For%20Users/Advanced/Use%20Native%20Node%20Modules/)
|
|
175
186
|
|
|
176
187
|
### Build tools
|
|
177
188
|
|
|
@@ -199,28 +210,6 @@ and [Valgrind](http://valgrind.org/) have been used to test
|
|
|
199
210
|
the most popular web-based formats, as well as libvips itself,
|
|
200
211
|
you are advised to perform your own testing and sandboxing.
|
|
201
212
|
|
|
202
|
-
ImageMagick in particular has a relatively large attack surface,
|
|
203
|
-
which can be partially mitigated with a
|
|
204
|
-
[policy.xml](http://www.imagemagick.org/script/resources.php)
|
|
205
|
-
configuration file to prevent the use of coders known to be vulnerable.
|
|
206
|
-
|
|
207
|
-
```xml
|
|
208
|
-
<policymap>
|
|
209
|
-
<policy domain="coder" rights="none" pattern="EPHEMERAL" />
|
|
210
|
-
<policy domain="coder" rights="none" pattern="URL" />
|
|
211
|
-
<policy domain="coder" rights="none" pattern="HTTPS" />
|
|
212
|
-
<policy domain="coder" rights="none" pattern="MVG" />
|
|
213
|
-
<policy domain="coder" rights="none" pattern="MSL" />
|
|
214
|
-
<policy domain="coder" rights="none" pattern="TEXT" />
|
|
215
|
-
<policy domain="coder" rights="none" pattern="SHOW" />
|
|
216
|
-
<policy domain="coder" rights="none" pattern="WIN" />
|
|
217
|
-
<policy domain="coder" rights="none" pattern="PLT" />
|
|
218
|
-
</policymap>
|
|
219
|
-
```
|
|
220
|
-
|
|
221
|
-
Set the `MAGICK_CONFIGURE_PATH` environment variable
|
|
222
|
-
to the directory containing the `policy.xml` file.
|
|
223
|
-
|
|
224
213
|
### Pre-compiled libvips binaries
|
|
225
214
|
|
|
226
215
|
This module will attempt to download a pre-compiled bundle of libvips
|
|
@@ -236,10 +225,18 @@ SHARP_IGNORE_GLOBAL_LIBVIPS=1 npm install sharp
|
|
|
236
225
|
```
|
|
237
226
|
|
|
238
227
|
Should you need to manually download and inspect these files,
|
|
239
|
-
you can do so via
|
|
228
|
+
you can do so via
|
|
229
|
+
[https://github.com/lovell/sharp-libvips/releases](https://github.com/lovell/sharp-libvips/releases)
|
|
240
230
|
|
|
241
231
|
Should you wish to install these from your own location,
|
|
242
|
-
set the `
|
|
232
|
+
set the `sharp_dist_base_url` npm config option, e.g.
|
|
233
|
+
|
|
234
|
+
```sh
|
|
235
|
+
npm config set sharp_dist_base_url "https://hostname/path/"
|
|
236
|
+
npm install sharp
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
or set the `SHARP_DIST_BASE_URL` environment variable, e.g.
|
|
243
240
|
|
|
244
241
|
```sh
|
|
245
242
|
SHARP_DIST_BASE_URL="https://hostname/path/" npm install sharp
|
|
@@ -265,6 +262,8 @@ Use of libraries under the terms of the LGPLv3 is via the
|
|
|
265
262
|
| expat | MIT Licence |
|
|
266
263
|
| fontconfig | [fontconfig Licence](https://cgit.freedesktop.org/fontconfig/tree/COPYING) (BSD-like) |
|
|
267
264
|
| freetype | [freetype Licence](http://git.savannah.gnu.org/cgit/freetype/freetype2.git/tree/docs/FTL.TXT) (BSD-like) |
|
|
265
|
+
| fribidi | LGPLv3 |
|
|
266
|
+
| gettext | LGPLv3 |
|
|
268
267
|
| giflib | MIT Licence |
|
|
269
268
|
| glib | LGPLv3 |
|
|
270
269
|
| harfbuzz | MIT Licence |
|
package/docs/performance.md
CHANGED
|
@@ -3,19 +3,17 @@
|
|
|
3
3
|
### Test environment
|
|
4
4
|
|
|
5
5
|
* AWS EC2 eu-west-1 [c5.large](https://aws.amazon.com/ec2/instance-types/c5/) (2x Xeon Platinum 8124M CPU @ 3.00GHz)
|
|
6
|
-
* Ubuntu
|
|
7
|
-
* Node.js
|
|
6
|
+
* Ubuntu 18.04 (hvm-ssd/ubuntu-bionic-18.04-amd64-server-20180912 ami-00035f41c82244dab)
|
|
7
|
+
* Node.js v10.11.0
|
|
8
8
|
|
|
9
9
|
### The contenders
|
|
10
10
|
|
|
11
|
-
* [jimp](https://www.npmjs.com/package/jimp) v0.
|
|
12
|
-
* [
|
|
13
|
-
* [mapnik](https://www.npmjs.org/package/mapnik) v3.6.2 - Whilst primarily a map renderer, Mapnik contains bitmap image utilities.
|
|
11
|
+
* [jimp](https://www.npmjs.com/package/jimp) v0.5.3 - Image processing in pure JavaScript. Provides bicubic interpolation.
|
|
12
|
+
* [mapnik](https://www.npmjs.org/package/mapnik) v4.0.1 - Whilst primarily a map renderer, Mapnik contains bitmap image utilities.
|
|
14
13
|
* [imagemagick-native](https://www.npmjs.com/package/imagemagick-native) v1.9.3 - Wrapper around libmagick++, supports Buffers only.
|
|
15
14
|
* [imagemagick](https://www.npmjs.com/package/imagemagick) v0.1.3 - Supports filesystem only and "*has been unmaintained for a long time*".
|
|
16
15
|
* [gm](https://www.npmjs.com/package/gm) v1.23.1 - Fully featured wrapper around GraphicsMagick's `gm` command line utility.
|
|
17
|
-
*
|
|
18
|
-
* sharp v0.19.0 / libvips v8.6.1 - Caching within libvips disabled to ensure a fair comparison.
|
|
16
|
+
* sharp v0.21.0 / libvips v8.7.0 - Caching within libvips disabled to ensure a fair comparison.
|
|
19
17
|
|
|
20
18
|
### The task
|
|
21
19
|
|
|
@@ -27,19 +25,14 @@ then compress to JPEG at a "quality" setting of 80.
|
|
|
27
25
|
|
|
28
26
|
| Module | Input | Output | Ops/sec | Speed-up |
|
|
29
27
|
| :----------------- | :----- | :----- | ------: | -------: |
|
|
30
|
-
| jimp
|
|
31
|
-
|
|
|
32
|
-
|
|
|
33
|
-
| imagemagick-native | buffer | buffer | 4.
|
|
34
|
-
|
|
|
35
|
-
|
|
|
36
|
-
|
|
|
37
|
-
|
|
|
38
|
-
| sharp | stream | stream | 24.43 | 21.4 |
|
|
39
|
-
| sharp | file | file | 25.97 | 22.7 |
|
|
40
|
-
| sharp | file | buffer | 26.00 | 22.8 |
|
|
41
|
-
| sharp | buffer | file | 26.33 | 23.0 |
|
|
42
|
-
| sharp | buffer | buffer | 26.43 | 23.1 |
|
|
28
|
+
| jimp | buffer | buffer | 0.71 | 1.0 |
|
|
29
|
+
| mapnik | buffer | buffer | 3.32 | 4.7 |
|
|
30
|
+
| gm | buffer | buffer | 3.97 | 5.6 |
|
|
31
|
+
| imagemagick-native | buffer | buffer | 4.06 | 5.7 |
|
|
32
|
+
| imagemagick | file | file | 4.24 | 6.0 |
|
|
33
|
+
| sharp | stream | stream | 25.30 | 35.6 |
|
|
34
|
+
| sharp | file | file | 26.17 | 36.9 |
|
|
35
|
+
| sharp | buffer | buffer | 26.45 | 37.3 |
|
|
43
36
|
|
|
44
37
|
Greater libvips performance can be expected with caching enabled (default)
|
|
45
38
|
and using 8+ core machines, especially those with larger L1/L2 CPU caches.
|
|
@@ -57,7 +50,7 @@ brew install mapnik
|
|
|
57
50
|
```
|
|
58
51
|
|
|
59
52
|
```sh
|
|
60
|
-
sudo apt-get install imagemagick libmagick++-dev graphicsmagick
|
|
53
|
+
sudo apt-get install imagemagick libmagick++-dev graphicsmagick libmapnik-dev
|
|
61
54
|
```
|
|
62
55
|
|
|
63
56
|
```sh
|
package/install/libvips.js
CHANGED
|
@@ -16,7 +16,14 @@ const libvips = require('../lib/libvips');
|
|
|
16
16
|
const platform = require('../lib/platform');
|
|
17
17
|
|
|
18
18
|
const minimumLibvipsVersion = libvips.minimumLibvipsVersion;
|
|
19
|
-
const distBaseUrl = process.env.SHARP_DIST_BASE_URL || `https://github.com/lovell/sharp-libvips/releases/download/v${minimumLibvipsVersion}/`;
|
|
19
|
+
const distBaseUrl = process.env.npm_config_sharp_dist_base_url || process.env.SHARP_DIST_BASE_URL || `https://github.com/lovell/sharp-libvips/releases/download/v${minimumLibvipsVersion}/`;
|
|
20
|
+
|
|
21
|
+
const fail = function (err) {
|
|
22
|
+
npmLog.error('sharp', err.message);
|
|
23
|
+
npmLog.info('sharp', 'Attempting to build from source via node-gyp but this may fail due to the above error');
|
|
24
|
+
npmLog.info('sharp', 'Please see https://sharp.pixelplumbing.com/page/install for required dependencies');
|
|
25
|
+
process.exit(1);
|
|
26
|
+
};
|
|
20
27
|
|
|
21
28
|
const extractTarball = function (tarPath) {
|
|
22
29
|
const vendorPath = path.join(__dirname, '..', 'vendor');
|
|
@@ -27,9 +34,7 @@ const extractTarball = function (tarPath) {
|
|
|
27
34
|
cwd: vendorPath,
|
|
28
35
|
strict: true
|
|
29
36
|
})
|
|
30
|
-
.catch(
|
|
31
|
-
throw err;
|
|
32
|
-
});
|
|
37
|
+
.catch(fail);
|
|
33
38
|
};
|
|
34
39
|
|
|
35
40
|
try {
|
|
@@ -51,11 +56,8 @@ try {
|
|
|
51
56
|
if (arch === 'ia32') {
|
|
52
57
|
throw new Error(`Intel Architecture 32-bit systems require manual installation of libvips >= ${minimumLibvipsVersion}`);
|
|
53
58
|
}
|
|
54
|
-
if (platformAndArch === 'freebsd-x64') {
|
|
55
|
-
throw new Error(`
|
|
56
|
-
}
|
|
57
|
-
if (detectLibc.isNonGlibcLinux) {
|
|
58
|
-
throw new Error(`Use with ${detectLibc.family} libc requires manual installation of libvips >= ${minimumLibvipsVersion}`);
|
|
59
|
+
if (platformAndArch === 'freebsd-x64' || platformAndArch === 'openbsd-x64' || platformAndArch === 'sunos-x64') {
|
|
60
|
+
throw new Error(`BSD/SunOS systems require manual installation of libvips >= ${minimumLibvipsVersion}`);
|
|
59
61
|
}
|
|
60
62
|
if (detectLibc.family === detectLibc.GLIBC && detectLibc.version && semver.lt(`${detectLibc.version}.0`, '2.13.0')) {
|
|
61
63
|
throw new Error(`Use with glibc version ${detectLibc.version} requires manual installation of libvips >= ${minimumLibvipsVersion}`);
|
|
@@ -78,23 +80,25 @@ try {
|
|
|
78
80
|
if (response.statusCode !== 200) {
|
|
79
81
|
throw new Error(`Status ${response.statusCode}`);
|
|
80
82
|
}
|
|
81
|
-
response
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
try {
|
|
85
|
-
// Attempt to rename
|
|
86
|
-
fs.renameSync(tarPathTemp, tarPathCache);
|
|
87
|
-
} catch (err) {
|
|
88
|
-
// Fall back to copy and unlink
|
|
89
|
-
copyFileSync(tarPathTemp, tarPathCache);
|
|
90
|
-
fs.unlinkSync(tarPathTemp);
|
|
91
|
-
}
|
|
92
|
-
extractTarball(tarPathCache);
|
|
83
|
+
response
|
|
84
|
+
.on('error', fail)
|
|
85
|
+
.pipe(tmpFile);
|
|
93
86
|
});
|
|
87
|
+
tmpFile
|
|
88
|
+
.on('error', fail)
|
|
89
|
+
.on('close', function () {
|
|
90
|
+
try {
|
|
91
|
+
// Attempt to rename
|
|
92
|
+
fs.renameSync(tarPathTemp, tarPathCache);
|
|
93
|
+
} catch (err) {
|
|
94
|
+
// Fall back to copy and unlink
|
|
95
|
+
copyFileSync(tarPathTemp, tarPathCache);
|
|
96
|
+
fs.unlinkSync(tarPathTemp);
|
|
97
|
+
}
|
|
98
|
+
extractTarball(tarPathCache);
|
|
99
|
+
});
|
|
94
100
|
}
|
|
95
101
|
}
|
|
96
102
|
} catch (err) {
|
|
97
|
-
|
|
98
|
-
npmLog.error('sharp', 'Please see http://sharp.pixelplumbing.com/page/install');
|
|
99
|
-
process.exit(1);
|
|
103
|
+
fail(err);
|
|
100
104
|
}
|
package/lib/channel.js
CHANGED
|
@@ -29,6 +29,23 @@ function removeAlpha () {
|
|
|
29
29
|
return this;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* 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.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* sharp('rgb.jpg')
|
|
37
|
+
* .ensureAlpha()
|
|
38
|
+
* .toFile('rgba.png', function(err, info) {
|
|
39
|
+
* // rgba.png is a 4 channel image with a fully opaque alpha channel
|
|
40
|
+
* });
|
|
41
|
+
*
|
|
42
|
+
* @returns {Sharp}
|
|
43
|
+
*/
|
|
44
|
+
function ensureAlpha () {
|
|
45
|
+
this.options.ensureAlpha = true;
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
|
|
32
49
|
/**
|
|
33
50
|
* Extract a single channel from a multi-channel image.
|
|
34
51
|
*
|
|
@@ -117,14 +134,13 @@ function bandbool (boolOp) {
|
|
|
117
134
|
* @private
|
|
118
135
|
*/
|
|
119
136
|
module.exports = function (Sharp) {
|
|
120
|
-
|
|
121
|
-
|
|
137
|
+
Object.assign(Sharp.prototype, {
|
|
138
|
+
// Public instance functions
|
|
122
139
|
removeAlpha,
|
|
140
|
+
ensureAlpha,
|
|
123
141
|
extractChannel,
|
|
124
142
|
joinChannel,
|
|
125
143
|
bandbool
|
|
126
|
-
].forEach(function (f) {
|
|
127
|
-
Sharp.prototype[f.name] = f;
|
|
128
144
|
});
|
|
129
145
|
// Class attributes
|
|
130
146
|
Sharp.bool = bool;
|
package/lib/colour.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const deprecate = require('util').deprecate;
|
|
4
|
+
|
|
3
5
|
const color = require('color');
|
|
4
6
|
const is = require('./is');
|
|
5
7
|
|
|
@@ -16,25 +18,20 @@ const colourspace = {
|
|
|
16
18
|
};
|
|
17
19
|
|
|
18
20
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* Delegates to the _color_ module, which can throw an Error
|
|
23
|
-
* but is liberal in what it accepts, clipping values to sensible min/max.
|
|
24
|
-
* The alpha value is a float between `0` (transparent) and `1` (opaque).
|
|
25
|
-
*
|
|
26
|
-
* @param {String|Object} rgba - parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha.
|
|
27
|
-
* @returns {Sharp}
|
|
28
|
-
* @throws {Error} Invalid parameter
|
|
21
|
+
* @deprecated
|
|
22
|
+
* @private
|
|
29
23
|
*/
|
|
30
24
|
function background (rgba) {
|
|
31
25
|
const colour = color(rgba);
|
|
32
|
-
|
|
26
|
+
const background = [
|
|
33
27
|
colour.red(),
|
|
34
28
|
colour.green(),
|
|
35
29
|
colour.blue(),
|
|
36
30
|
Math.round(colour.alpha() * 255)
|
|
37
31
|
];
|
|
32
|
+
this.options.resizeBackground = background;
|
|
33
|
+
this.options.extendBackground = background;
|
|
34
|
+
this.options.flattenBackground = background.slice(0, 3);
|
|
38
35
|
return this;
|
|
39
36
|
}
|
|
40
37
|
|
|
@@ -80,7 +77,7 @@ function grayscale (grayscale) {
|
|
|
80
77
|
/**
|
|
81
78
|
* Set the output colourspace.
|
|
82
79
|
* By default output image will be web-friendly sRGB, with additional channels interpreted as alpha channels.
|
|
83
|
-
* @param {String} [colourspace] - output colourspace e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://github.com/
|
|
80
|
+
* @param {String} [colourspace] - output colourspace e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://github.com/libvips/libvips/blob/master/libvips/iofuncs/enumtypes.c#L568)
|
|
84
81
|
* @returns {Sharp}
|
|
85
82
|
* @throws {Error} Invalid parameters
|
|
86
83
|
*/
|
|
@@ -102,23 +99,43 @@ function toColorspace (colorspace) {
|
|
|
102
99
|
return this.toColourspace(colorspace);
|
|
103
100
|
}
|
|
104
101
|
|
|
102
|
+
/**
|
|
103
|
+
* Update a colour attribute of the this.options Object.
|
|
104
|
+
* @private
|
|
105
|
+
* @param {String} key
|
|
106
|
+
* @param {String|Object} val
|
|
107
|
+
* @throws {Error} Invalid key
|
|
108
|
+
*/
|
|
109
|
+
function _setColourOption (key, val) {
|
|
110
|
+
if (is.object(val) || is.string(val)) {
|
|
111
|
+
const colour = color(val);
|
|
112
|
+
this.options[key] = [
|
|
113
|
+
colour.red(),
|
|
114
|
+
colour.green(),
|
|
115
|
+
colour.blue(),
|
|
116
|
+
Math.round(colour.alpha() * 255)
|
|
117
|
+
];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
105
121
|
/**
|
|
106
122
|
* Decorate the Sharp prototype with colour-related functions.
|
|
107
123
|
* @private
|
|
108
124
|
*/
|
|
109
125
|
module.exports = function (Sharp) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
background,
|
|
126
|
+
Object.assign(Sharp.prototype, {
|
|
127
|
+
// Public
|
|
113
128
|
tint,
|
|
114
129
|
greyscale,
|
|
115
130
|
grayscale,
|
|
116
131
|
toColourspace,
|
|
117
|
-
toColorspace
|
|
118
|
-
|
|
119
|
-
|
|
132
|
+
toColorspace,
|
|
133
|
+
// Private
|
|
134
|
+
_setColourOption
|
|
120
135
|
});
|
|
121
136
|
// Class attributes
|
|
122
137
|
Sharp.colourspace = colourspace;
|
|
123
138
|
Sharp.colorspace = colourspace;
|
|
139
|
+
// Deprecated
|
|
140
|
+
Sharp.prototype.background = deprecate(background, 'background(background) is deprecated, use resize({ background }), extend({ background }) or flatten({ background }) instead');
|
|
124
141
|
};
|
package/lib/composite.js
CHANGED
|
@@ -14,8 +14,7 @@ const is = require('./is');
|
|
|
14
14
|
* sharp('input.png')
|
|
15
15
|
* .rotate(180)
|
|
16
16
|
* .resize(300)
|
|
17
|
-
* .flatten()
|
|
18
|
-
* .background('#ff6600')
|
|
17
|
+
* .flatten( { background: '#ff6600' } )
|
|
19
18
|
* .overlayWith('overlay.png', { gravity: sharp.gravity.southeast } )
|
|
20
19
|
* .sharpen()
|
|
21
20
|
* .withMetadata()
|
|
@@ -27,7 +26,7 @@ const is = require('./is');
|
|
|
27
26
|
* // sharpened, with metadata, 90% quality WebP image data. Phew!
|
|
28
27
|
* });
|
|
29
28
|
*
|
|
30
|
-
* @param {(Buffer|String)} overlay - Buffer containing image data or String containing the path to an image file.
|
|
29
|
+
* @param {(Buffer|String)} [overlay] - Buffer containing image data or String containing the path to an image file.
|
|
31
30
|
* @param {Object} [options]
|
|
32
31
|
* @param {String} [options.gravity='centre'] - gravity at which to place the overlay.
|
|
33
32
|
* @param {Number} [options.top] - the pixel offset from the top edge.
|
package/lib/constructor.js
CHANGED
|
@@ -4,42 +4,10 @@ const path = require('path');
|
|
|
4
4
|
const util = require('util');
|
|
5
5
|
const stream = require('stream');
|
|
6
6
|
const events = require('events');
|
|
7
|
-
const semver = require('semver');
|
|
8
7
|
const is = require('./is');
|
|
9
|
-
const platform = require('./platform');
|
|
10
|
-
const sharp = require('../build/Release/sharp.node');
|
|
11
8
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
let vendorPlatformId;
|
|
15
|
-
try {
|
|
16
|
-
vendorPlatformId = require('../vendor/platform.json');
|
|
17
|
-
} catch (err) {
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
const currentPlatformId = platform();
|
|
21
|
-
/* istanbul ignore if */
|
|
22
|
-
if (currentPlatformId !== vendorPlatformId) {
|
|
23
|
-
throw new Error(`'${vendorPlatformId}' binaries cannot be used on the '${currentPlatformId}' platform. Please remove the 'node_modules/sharp/vendor' directory and run 'npm rebuild'.`);
|
|
24
|
-
}
|
|
25
|
-
})();
|
|
26
|
-
|
|
27
|
-
// Versioning
|
|
28
|
-
let versions = {
|
|
29
|
-
vips: sharp.libvipsVersion()
|
|
30
|
-
};
|
|
31
|
-
(function () {
|
|
32
|
-
// Does libvips meet minimum requirement?
|
|
33
|
-
const libvipsVersionMin = require('../package.json').config.libvips;
|
|
34
|
-
/* istanbul ignore if */
|
|
35
|
-
if (semver.lt(versions.vips, libvipsVersionMin)) {
|
|
36
|
-
throw new Error('Found libvips ' + versions.vips + ' but require at least ' + libvipsVersionMin);
|
|
37
|
-
}
|
|
38
|
-
// Include versions of dependencies, if present
|
|
39
|
-
try {
|
|
40
|
-
versions = require('../vendor/versions.json');
|
|
41
|
-
} catch (err) {}
|
|
42
|
-
})();
|
|
9
|
+
require('./libvips').hasVendoredLibvips();
|
|
10
|
+
const sharp = require('bindings')('sharp.node');
|
|
43
11
|
|
|
44
12
|
// Use NODE_DEBUG=sharp to enable libvips warnings
|
|
45
13
|
const debuglog = util.debuglog('sharp');
|
|
@@ -93,9 +61,8 @@ const debuglog = util.debuglog('sharp');
|
|
|
93
61
|
* a String containing the path to an JPEG, PNG, WebP, GIF, SVG or TIFF image file.
|
|
94
62
|
* JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when not present.
|
|
95
63
|
* @param {Object} [options] - if present, is an Object with optional attributes.
|
|
96
|
-
* @param {Boolean} [options.failOnError=
|
|
97
|
-
* to decode images, even if the data is corrupt or invalid.
|
|
98
|
-
* if you'd rather halt processing and raise an error when loading invalid images.
|
|
64
|
+
* @param {Boolean} [options.failOnError=true] - by default halt processing and raise an error when loading invalid images.
|
|
65
|
+
* Set this flag to `false` if you'd rather apply a "best effort" to decode images, even if the data is corrupt or invalid.
|
|
99
66
|
* @param {Number} [options.density=72] - number representing the DPI for vector images.
|
|
100
67
|
* @param {Number} [options.page=0] - page number to extract for multi-page input (GIF, TIFF)
|
|
101
68
|
* @param {Object} [options.raw] - describes raw pixel input image data. See `raw()` for pixel ordering.
|
|
@@ -136,10 +103,12 @@ const Sharp = function (input, options) {
|
|
|
136
103
|
width: -1,
|
|
137
104
|
height: -1,
|
|
138
105
|
canvas: 'crop',
|
|
139
|
-
|
|
140
|
-
|
|
106
|
+
position: 0,
|
|
107
|
+
resizeBackground: [0, 0, 0, 255],
|
|
141
108
|
useExifOrientation: false,
|
|
142
109
|
angle: 0,
|
|
110
|
+
rotationAngle: 0,
|
|
111
|
+
rotationBackground: [0, 0, 0, 255],
|
|
143
112
|
rotateBeforePreExtract: false,
|
|
144
113
|
flip: false,
|
|
145
114
|
flop: false,
|
|
@@ -147,14 +116,15 @@ const Sharp = function (input, options) {
|
|
|
147
116
|
extendBottom: 0,
|
|
148
117
|
extendLeft: 0,
|
|
149
118
|
extendRight: 0,
|
|
119
|
+
extendBackground: [0, 0, 0, 255],
|
|
150
120
|
withoutEnlargement: false,
|
|
151
121
|
kernel: 'lanczos3',
|
|
152
122
|
fastShrinkOnLoad: true,
|
|
153
123
|
// operations
|
|
154
|
-
background: [0, 0, 0, 255],
|
|
155
124
|
tintA: 128,
|
|
156
125
|
tintB: 128,
|
|
157
126
|
flatten: false,
|
|
127
|
+
flattenBackground: [0, 0, 0],
|
|
158
128
|
negate: false,
|
|
159
129
|
medianSize: 0,
|
|
160
130
|
blurSigma: 0,
|
|
@@ -163,8 +133,9 @@ const Sharp = function (input, options) {
|
|
|
163
133
|
sharpenJagged: 2,
|
|
164
134
|
threshold: 0,
|
|
165
135
|
thresholdGrayscale: true,
|
|
166
|
-
|
|
136
|
+
trimThreshold: 0,
|
|
167
137
|
gamma: 0,
|
|
138
|
+
gammaOut: 0,
|
|
168
139
|
greyscale: false,
|
|
169
140
|
normalise: 0,
|
|
170
141
|
booleanBufferIn: null,
|
|
@@ -172,6 +143,7 @@ const Sharp = function (input, options) {
|
|
|
172
143
|
joinChannelIn: [],
|
|
173
144
|
extractChannel: -1,
|
|
174
145
|
removeAlpha: false,
|
|
146
|
+
ensureAlpha: false,
|
|
175
147
|
colourspace: 'srgb',
|
|
176
148
|
// overlay
|
|
177
149
|
overlayGravity: 0,
|
|
@@ -198,6 +170,10 @@ const Sharp = function (input, options) {
|
|
|
198
170
|
pngProgressive: false,
|
|
199
171
|
pngCompressionLevel: 9,
|
|
200
172
|
pngAdaptiveFiltering: false,
|
|
173
|
+
pngPalette: false,
|
|
174
|
+
pngQuality: 100,
|
|
175
|
+
pngColours: 256,
|
|
176
|
+
pngDither: 1,
|
|
201
177
|
webpQuality: 80,
|
|
202
178
|
webpAlphaQuality: 100,
|
|
203
179
|
webpLossless: false,
|
|
@@ -205,7 +181,11 @@ const Sharp = function (input, options) {
|
|
|
205
181
|
tiffQuality: 80,
|
|
206
182
|
tiffCompression: 'jpeg',
|
|
207
183
|
tiffPredictor: 'horizontal',
|
|
184
|
+
tiffPyramid: false,
|
|
208
185
|
tiffSquash: false,
|
|
186
|
+
tiffTile: false,
|
|
187
|
+
tiffTileHeight: 256,
|
|
188
|
+
tiffTileWidth: 256,
|
|
209
189
|
tiffXres: 1.0,
|
|
210
190
|
tiffYres: 1.0,
|
|
211
191
|
tileSize: 256,
|
|
@@ -251,7 +231,12 @@ Sharp.format = sharp.format();
|
|
|
251
231
|
* @example
|
|
252
232
|
* console.log(sharp.versions);
|
|
253
233
|
*/
|
|
254
|
-
Sharp.versions =
|
|
234
|
+
Sharp.versions = {
|
|
235
|
+
vips: sharp.libvipsVersion()
|
|
236
|
+
};
|
|
237
|
+
try {
|
|
238
|
+
Sharp.versions = require('../vendor/versions.json');
|
|
239
|
+
} catch (err) {}
|
|
255
240
|
|
|
256
241
|
/**
|
|
257
242
|
* Export constructor.
|
package/lib/input.js
CHANGED
|
@@ -9,7 +9,7 @@ const sharp = require('../build/Release/sharp.node');
|
|
|
9
9
|
* @private
|
|
10
10
|
*/
|
|
11
11
|
function _createInputDescriptor (input, inputOptions, containerOptions) {
|
|
12
|
-
const inputDescriptor = { failOnError:
|
|
12
|
+
const inputDescriptor = { failOnError: true };
|
|
13
13
|
if (is.string(input)) {
|
|
14
14
|
// filesystem
|
|
15
15
|
inputDescriptor.file = input;
|
|
@@ -177,12 +177,17 @@ function clone () {
|
|
|
177
177
|
* A Promises/A+ promise is returned when `callback` is not provided.
|
|
178
178
|
*
|
|
179
179
|
* - `format`: Name of decoder used to decompress image data e.g. `jpeg`, `png`, `webp`, `gif`, `svg`
|
|
180
|
+
* - `size`: Total size of image in bytes, for Stream and Buffer input only
|
|
180
181
|
* - `width`: Number of pixels wide (EXIF orientation is not taken into consideration)
|
|
181
182
|
* - `height`: Number of pixels high (EXIF orientation is not taken into consideration)
|
|
182
|
-
* - `space`: Name of colour space interpretation e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://github.com/
|
|
183
|
+
* - `space`: Name of colour space interpretation e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://github.com/libvips/libvips/blob/master/libvips/iofuncs/enumtypes.c#L636)
|
|
183
184
|
* - `channels`: Number of bands e.g. `3` for sRGB, `4` for CMYK
|
|
184
|
-
* - `depth`: Name of pixel depth format e.g. `uchar`, `char`, `ushort`, `float` [...](https://github.com/
|
|
185
|
+
* - `depth`: Name of pixel depth format e.g. `uchar`, `char`, `ushort`, `float` [...](https://github.com/libvips/libvips/blob/master/libvips/iofuncs/enumtypes.c#L672)
|
|
185
186
|
* - `density`: Number of pixels per inch (DPI), if present
|
|
187
|
+
* - `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
|
|
188
|
+
* - `isProgressive`: Boolean indicating whether the image is interlaced using a progressive scan
|
|
189
|
+
* - `pages`: Number of pages this TIFF, GIF or PDF image contains.
|
|
190
|
+
* - `pageHeight`: Number of pixels high each page in this PDF image will be.
|
|
186
191
|
* - `hasProfile`: Boolean indicating the presence of an embedded ICC profile
|
|
187
192
|
* - `hasAlpha`: Boolean indicating the presence of an alpha transparency channel
|
|
188
193
|
* - `orientation`: Number value of the EXIF Orientation header, if present
|
|
@@ -359,7 +364,7 @@ function sequentialRead (sequentialRead) {
|
|
|
359
364
|
* @private
|
|
360
365
|
*/
|
|
361
366
|
module.exports = function (Sharp) {
|
|
362
|
-
|
|
367
|
+
Object.assign(Sharp.prototype, {
|
|
363
368
|
// Private
|
|
364
369
|
_createInputDescriptor,
|
|
365
370
|
_write,
|
|
@@ -371,7 +376,5 @@ module.exports = function (Sharp) {
|
|
|
371
376
|
stats,
|
|
372
377
|
limitInputPixels,
|
|
373
378
|
sequentialRead
|
|
374
|
-
].forEach(function (f) {
|
|
375
|
-
Sharp.prototype[f.name] = f;
|
|
376
379
|
});
|
|
377
380
|
};
|