sharp 0.26.3 → 0.28.0
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 +16 -15
- package/binding.gyp +3 -5
- package/install/dll-copy.js +4 -4
- package/install/libvips.js +65 -31
- package/lib/channel.js +1 -1
- package/lib/colour.js +7 -0
- package/lib/composite.js +10 -7
- package/lib/constructor.js +53 -19
- package/lib/input.js +41 -10
- package/lib/is.js +10 -0
- package/lib/libvips.js +25 -3
- package/lib/operation.js +9 -4
- package/lib/output.js +137 -58
- package/lib/resize.js +42 -15
- package/lib/utility.js +17 -10
- package/package.json +20 -18
- package/src/common.cc +73 -51
- package/src/common.h +10 -3
- package/src/metadata.cc +6 -0
- package/src/metadata.h +1 -0
- package/src/operations.cc +1 -1
- package/src/pipeline.cc +43 -14
- package/src/pipeline.h +12 -5
- package/src/sharp.cc +2 -0
- package/src/utilities.cc +16 -0
- package/src/utilities.h +1 -0
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
The typical use case for this high speed Node.js module
|
|
6
6
|
is to convert large images in common formats to
|
|
7
|
-
smaller, web-friendly JPEG, PNG and
|
|
7
|
+
smaller, web-friendly JPEG, PNG, WebP and AVIF images of varying dimensions.
|
|
8
8
|
|
|
9
9
|
Resizing an image is typically 4x-5x faster than using the
|
|
10
10
|
quickest ImageMagick and GraphicsMagick settings
|
|
@@ -19,6 +19,14 @@ rotation, extraction, compositing and gamma correction are available.
|
|
|
19
19
|
Most modern macOS, Windows and Linux systems running Node.js v10+
|
|
20
20
|
do not require any additional install or runtime dependencies.
|
|
21
21
|
|
|
22
|
+
## Documentation
|
|
23
|
+
|
|
24
|
+
Visit [sharp.pixelplumbing.com](https://sharp.pixelplumbing.com/) for complete
|
|
25
|
+
[installation instructions](https://sharp.pixelplumbing.com/install),
|
|
26
|
+
[API documentation](https://sharp.pixelplumbing.com/api-constructor),
|
|
27
|
+
[benchmark tests](https://sharp.pixelplumbing.com/performance) and
|
|
28
|
+
[changelog](https://sharp.pixelplumbing.com/changelog).
|
|
29
|
+
|
|
22
30
|
## Examples
|
|
23
31
|
|
|
24
32
|
```sh
|
|
@@ -43,6 +51,7 @@ sharp(inputBuffer)
|
|
|
43
51
|
sharp('input.jpg')
|
|
44
52
|
.rotate()
|
|
45
53
|
.resize(200)
|
|
54
|
+
.jpeg({ mozjpeg: true })
|
|
46
55
|
.toBuffer()
|
|
47
56
|
.then( data => { ... })
|
|
48
57
|
.catch( err => { ... });
|
|
@@ -84,25 +93,17 @@ readableStream
|
|
|
84
93
|
.pipe(writableStream);
|
|
85
94
|
```
|
|
86
95
|
|
|
87
|
-
|
|
88
|
-
[](https://nodejs.org/dist/latest/docs/api/n-api.html#n_api_n_api_version_matrix)
|
|
89
|
-
|
|
90
|
-
### Documentation
|
|
91
|
-
|
|
92
|
-
Visit [sharp.pixelplumbing.com](https://sharp.pixelplumbing.com/) for complete
|
|
93
|
-
[installation instructions](https://sharp.pixelplumbing.com/install),
|
|
94
|
-
[API documentation](https://sharp.pixelplumbing.com/api-constructor),
|
|
95
|
-
[benchmark tests](https://sharp.pixelplumbing.com/performance) and
|
|
96
|
-
[changelog](https://sharp.pixelplumbing.com/changelog).
|
|
97
|
-
|
|
98
|
-
### Contributing
|
|
96
|
+
## Contributing
|
|
99
97
|
|
|
100
98
|
A [guide for contributors](https://github.com/lovell/sharp/blob/master/.github/CONTRIBUTING.md)
|
|
101
99
|
covers reporting bugs, requesting features and submitting code changes.
|
|
102
100
|
|
|
103
|
-
|
|
101
|
+
[](https://coveralls.io/r/lovell/sharp?branch=master)
|
|
102
|
+
[](https://nodejs.org/dist/latest/docs/api/n-api.html#n_api_n_api_version_matrix)
|
|
103
|
+
|
|
104
|
+
## Licensing
|
|
104
105
|
|
|
105
|
-
Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Lovell Fuller and contributors.
|
|
106
|
+
Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Lovell Fuller and contributors.
|
|
106
107
|
|
|
107
108
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
108
109
|
you may not use this file except in compliance with the License.
|
package/binding.gyp
CHANGED
|
@@ -140,8 +140,7 @@
|
|
|
140
140
|
'link_settings': {
|
|
141
141
|
'library_dirs': ['<(sharp_vendor_dir)/lib'],
|
|
142
142
|
'libraries': [
|
|
143
|
-
'libvips-cpp.42.dylib'
|
|
144
|
-
'libvips.42.dylib'
|
|
143
|
+
'libvips-cpp.42.dylib'
|
|
145
144
|
]
|
|
146
145
|
},
|
|
147
146
|
'xcode_settings': {
|
|
@@ -153,13 +152,12 @@
|
|
|
153
152
|
}],
|
|
154
153
|
['OS == "linux"', {
|
|
155
154
|
'defines': [
|
|
156
|
-
'_GLIBCXX_USE_CXX11_ABI=
|
|
155
|
+
'_GLIBCXX_USE_CXX11_ABI=1'
|
|
157
156
|
],
|
|
158
157
|
'link_settings': {
|
|
159
158
|
'library_dirs': ['<(sharp_vendor_dir)/lib'],
|
|
160
159
|
'libraries': [
|
|
161
|
-
'-l:libvips-cpp.so.42'
|
|
162
|
-
'-l:libvips.so.42'
|
|
160
|
+
'-l:libvips-cpp.so.42'
|
|
163
161
|
],
|
|
164
162
|
'ldflags': [
|
|
165
163
|
# Ensure runtime linking is relative to sharp.node
|
package/install/dll-copy.js
CHANGED
|
@@ -4,7 +4,6 @@ const fs = require('fs');
|
|
|
4
4
|
const path = require('path');
|
|
5
5
|
|
|
6
6
|
const libvips = require('../lib/libvips');
|
|
7
|
-
const npmLog = require('npmlog');
|
|
8
7
|
|
|
9
8
|
const minimumLibvipsVersion = libvips.minimumLibvipsVersion;
|
|
10
9
|
|
|
@@ -12,13 +11,13 @@ const platform = process.env.npm_config_platform || process.platform;
|
|
|
12
11
|
if (platform === 'win32') {
|
|
13
12
|
const buildDir = path.join(__dirname, '..', 'build');
|
|
14
13
|
const buildReleaseDir = path.join(buildDir, 'Release');
|
|
15
|
-
|
|
14
|
+
libvips.log(`Creating ${buildReleaseDir}`);
|
|
16
15
|
try {
|
|
17
16
|
libvips.mkdirSync(buildDir);
|
|
18
17
|
libvips.mkdirSync(buildReleaseDir);
|
|
19
18
|
} catch (err) {}
|
|
20
19
|
const vendorLibDir = path.join(__dirname, '..', 'vendor', minimumLibvipsVersion, 'lib');
|
|
21
|
-
|
|
20
|
+
libvips.log(`Copying DLLs from ${vendorLibDir} to ${buildReleaseDir}`);
|
|
22
21
|
try {
|
|
23
22
|
fs
|
|
24
23
|
.readdirSync(vendorLibDir)
|
|
@@ -32,6 +31,7 @@ if (platform === 'win32') {
|
|
|
32
31
|
);
|
|
33
32
|
});
|
|
34
33
|
} catch (err) {
|
|
35
|
-
|
|
34
|
+
libvips.log(err);
|
|
35
|
+
process.exit(1);
|
|
36
36
|
}
|
|
37
37
|
}
|
package/install/libvips.js
CHANGED
|
@@ -7,7 +7,6 @@ const stream = require('stream');
|
|
|
7
7
|
const zlib = require('zlib');
|
|
8
8
|
|
|
9
9
|
const detectLibc = require('detect-libc');
|
|
10
|
-
const npmLog = require('npmlog');
|
|
11
10
|
const semver = require('semver');
|
|
12
11
|
const simpleGet = require('simple-get');
|
|
13
12
|
const tarFs = require('tar-fs');
|
|
@@ -22,34 +21,50 @@ const minimumGlibcVersionByArch = {
|
|
|
22
21
|
x64: '2.17'
|
|
23
22
|
};
|
|
24
23
|
|
|
24
|
+
const hasSharpPrebuild = [
|
|
25
|
+
'darwin-x64',
|
|
26
|
+
'linux-arm64',
|
|
27
|
+
'linux-x64',
|
|
28
|
+
'linuxmusl-x64',
|
|
29
|
+
'linuxmusl-arm64',
|
|
30
|
+
'win32-ia32',
|
|
31
|
+
'win32-x64'
|
|
32
|
+
];
|
|
33
|
+
|
|
25
34
|
const { minimumLibvipsVersion, minimumLibvipsVersionLabelled } = libvips;
|
|
26
35
|
const distHost = process.env.npm_config_sharp_libvips_binary_host || 'https://github.com/lovell/sharp-libvips/releases/download';
|
|
27
36
|
const distBaseUrl = process.env.npm_config_sharp_dist_base_url || process.env.SHARP_DIST_BASE_URL || `${distHost}/v${minimumLibvipsVersionLabelled}/`;
|
|
28
37
|
const supportsBrotli = ('BrotliDecompress' in zlib);
|
|
29
38
|
|
|
30
39
|
const fail = function (err) {
|
|
31
|
-
|
|
40
|
+
libvips.log(err);
|
|
32
41
|
if (err.code === 'EACCES') {
|
|
33
|
-
|
|
42
|
+
libvips.log('Are you trying to install as a root or sudo user? Try again with the --unsafe-perm flag');
|
|
34
43
|
}
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
libvips.log('Attempting to build from source via node-gyp but this may fail due to the above error');
|
|
45
|
+
libvips.log('Please see https://sharp.pixelplumbing.com/install for required dependencies');
|
|
37
46
|
process.exit(1);
|
|
38
47
|
};
|
|
39
48
|
|
|
40
|
-
const extractTarball = function (tarPath) {
|
|
49
|
+
const extractTarball = function (tarPath, platformAndArch) {
|
|
41
50
|
const vendorPath = path.join(__dirname, '..', 'vendor');
|
|
42
51
|
libvips.mkdirSync(vendorPath);
|
|
43
52
|
const versionedVendorPath = path.join(vendorPath, minimumLibvipsVersion);
|
|
44
53
|
libvips.mkdirSync(versionedVendorPath);
|
|
54
|
+
|
|
55
|
+
const ignoreVendorInclude = hasSharpPrebuild.includes(platformAndArch) && !process.env.npm_config_build_from_source;
|
|
56
|
+
const ignore = function (name) {
|
|
57
|
+
return ignoreVendorInclude && name.includes('include/');
|
|
58
|
+
};
|
|
59
|
+
|
|
45
60
|
stream.pipeline(
|
|
46
61
|
fs.createReadStream(tarPath),
|
|
47
62
|
supportsBrotli ? new zlib.BrotliDecompress() : new zlib.Gunzip(),
|
|
48
|
-
tarFs.extract(versionedVendorPath),
|
|
63
|
+
tarFs.extract(versionedVendorPath, { ignore }),
|
|
49
64
|
function (err) {
|
|
50
65
|
if (err) {
|
|
51
66
|
if (/unexpected end of file/.test(err.message)) {
|
|
52
|
-
|
|
67
|
+
fail(new Error(`Please delete ${tarPath} as it is not a valid tarball`));
|
|
53
68
|
}
|
|
54
69
|
fail(err);
|
|
55
70
|
}
|
|
@@ -62,11 +77,11 @@ try {
|
|
|
62
77
|
|
|
63
78
|
if (useGlobalLibvips) {
|
|
64
79
|
const globalLibvipsVersion = libvips.globalLibvipsVersion();
|
|
65
|
-
|
|
66
|
-
|
|
80
|
+
libvips.log(`Detected globally-installed libvips v${globalLibvipsVersion}`);
|
|
81
|
+
libvips.log('Building from source via node-gyp');
|
|
67
82
|
process.exit(1);
|
|
68
83
|
} else if (libvips.hasVendoredLibvips()) {
|
|
69
|
-
|
|
84
|
+
libvips.log(`Using existing vendored libvips v${minimumLibvipsVersion}`);
|
|
70
85
|
} else {
|
|
71
86
|
// Is this arch/platform supported?
|
|
72
87
|
const arch = process.env.npm_config_arch || process.arch;
|
|
@@ -74,6 +89,9 @@ try {
|
|
|
74
89
|
if (arch === 'ia32' && !platformAndArch.startsWith('win32')) {
|
|
75
90
|
throw new Error(`Intel Architecture 32-bit systems require manual installation of libvips >= ${minimumLibvipsVersion}`);
|
|
76
91
|
}
|
|
92
|
+
if (platformAndArch === 'darwin-arm64') {
|
|
93
|
+
throw new Error("Please run 'brew install vips' to install libvips on Apple M1 (ARM64) systems");
|
|
94
|
+
}
|
|
77
95
|
if (platformAndArch === 'freebsd-x64' || platformAndArch === 'openbsd-x64' || platformAndArch === 'sunos-x64') {
|
|
78
96
|
throw new Error(`BSD/SunOS systems require manual installation of libvips >= ${minimumLibvipsVersion}`);
|
|
79
97
|
}
|
|
@@ -82,6 +100,11 @@ try {
|
|
|
82
100
|
throw new Error(`Use with glibc ${detectLibc.version} requires manual installation of libvips >= ${minimumLibvipsVersion}`);
|
|
83
101
|
}
|
|
84
102
|
}
|
|
103
|
+
if (detectLibc.family === detectLibc.MUSL && detectLibc.version) {
|
|
104
|
+
if (semver.lt(detectLibc.version, '1.1.24')) {
|
|
105
|
+
throw new Error(`Use with musl ${detectLibc.version} requires manual installation of libvips >= ${minimumLibvipsVersion}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
85
108
|
|
|
86
109
|
const supportedNodeVersion = process.env.npm_package_engines_node || require('../package.json').engines.node;
|
|
87
110
|
if (!semver.satisfies(process.versions.node, supportedNodeVersion)) {
|
|
@@ -94,13 +117,11 @@ try {
|
|
|
94
117
|
const tarFilename = ['libvips', minimumLibvipsVersion, platformAndArch].join('-') + '.tar.' + extension;
|
|
95
118
|
const tarPathCache = path.join(libvips.cachePath(), tarFilename);
|
|
96
119
|
if (fs.existsSync(tarPathCache)) {
|
|
97
|
-
|
|
98
|
-
extractTarball(tarPathCache);
|
|
120
|
+
libvips.log(`Using cached ${tarPathCache}`);
|
|
121
|
+
extractTarball(tarPathCache, platformAndArch);
|
|
99
122
|
} else {
|
|
100
|
-
const tarPathTemp = path.join(os.tmpdir(), `${process.pid}-${tarFilename}`);
|
|
101
|
-
const tmpFile = fs.createWriteStream(tarPathTemp);
|
|
102
123
|
const url = distBaseUrl + tarFilename;
|
|
103
|
-
|
|
124
|
+
libvips.log(`Downloading ${url}`);
|
|
104
125
|
simpleGet({ url: url, agent: agent() }, function (err, response) {
|
|
105
126
|
if (err) {
|
|
106
127
|
fail(err);
|
|
@@ -109,24 +130,37 @@ try {
|
|
|
109
130
|
} else if (response.statusCode !== 200) {
|
|
110
131
|
fail(new Error(`Status ${response.statusCode} ${response.statusMessage}`));
|
|
111
132
|
} else {
|
|
133
|
+
const tarPathTemp = path.join(os.tmpdir(), `${process.pid}-${tarFilename}`);
|
|
134
|
+
const tmpFileStream = fs.createWriteStream(tarPathTemp);
|
|
112
135
|
response
|
|
113
|
-
.on('error',
|
|
114
|
-
|
|
136
|
+
.on('error', function (err) {
|
|
137
|
+
tmpFileStream.destroy(err);
|
|
138
|
+
})
|
|
139
|
+
.on('close', function () {
|
|
140
|
+
if (!response.complete) {
|
|
141
|
+
tmpFileStream.destroy(new Error('Download incomplete (connection was terminated)'));
|
|
142
|
+
}
|
|
143
|
+
})
|
|
144
|
+
.pipe(tmpFileStream);
|
|
145
|
+
tmpFileStream
|
|
146
|
+
.on('error', function (err) {
|
|
147
|
+
// Clean up temporary file
|
|
148
|
+
fs.unlinkSync(tarPathTemp);
|
|
149
|
+
fail(err);
|
|
150
|
+
})
|
|
151
|
+
.on('close', function () {
|
|
152
|
+
try {
|
|
153
|
+
// Attempt to rename
|
|
154
|
+
fs.renameSync(tarPathTemp, tarPathCache);
|
|
155
|
+
} catch (err) {
|
|
156
|
+
// Fall back to copy and unlink
|
|
157
|
+
fs.copyFileSync(tarPathTemp, tarPathCache);
|
|
158
|
+
fs.unlinkSync(tarPathTemp);
|
|
159
|
+
}
|
|
160
|
+
extractTarball(tarPathCache);
|
|
161
|
+
});
|
|
115
162
|
}
|
|
116
163
|
});
|
|
117
|
-
tmpFile
|
|
118
|
-
.on('error', fail)
|
|
119
|
-
.on('close', function () {
|
|
120
|
-
try {
|
|
121
|
-
// Attempt to rename
|
|
122
|
-
fs.renameSync(tarPathTemp, tarPathCache);
|
|
123
|
-
} catch (err) {
|
|
124
|
-
// Fall back to copy and unlink
|
|
125
|
-
fs.copyFileSync(tarPathTemp, tarPathCache);
|
|
126
|
-
fs.unlinkSync(tarPathTemp);
|
|
127
|
-
}
|
|
128
|
-
extractTarball(tarPathCache);
|
|
129
|
-
});
|
|
130
164
|
}
|
|
131
165
|
}
|
|
132
166
|
} catch (err) {
|
package/lib/channel.js
CHANGED
|
@@ -85,7 +85,7 @@ function extractChannel (channel) {
|
|
|
85
85
|
* - sRGB: 0: Red, 1: Green, 2: Blue, 3: Alpha.
|
|
86
86
|
* - CMYK: 0: Magenta, 1: Cyan, 2: Yellow, 3: Black, 4: Alpha.
|
|
87
87
|
*
|
|
88
|
-
* Buffers may be any of the image formats supported by sharp
|
|
88
|
+
* Buffers may be any of the image formats supported by sharp.
|
|
89
89
|
* 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.
|
|
90
90
|
*
|
|
91
91
|
* @param {Array<string|Buffer>|string|Buffer} images - one or more images (file paths, Buffers).
|
package/lib/colour.js
CHANGED
|
@@ -57,6 +57,13 @@ function grayscale (grayscale) {
|
|
|
57
57
|
/**
|
|
58
58
|
* Set the output colourspace.
|
|
59
59
|
* By default output image will be web-friendly sRGB, with additional channels interpreted as alpha channels.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* // Output 16 bits per pixel RGB
|
|
63
|
+
* await sharp(input)
|
|
64
|
+
* .toColourspace('rgb16')
|
|
65
|
+
* .toFile('16-bpp.png')
|
|
66
|
+
*
|
|
60
67
|
* @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)
|
|
61
68
|
* @returns {Sharp}
|
|
62
69
|
* @throws {Error} Invalid parameters
|
package/lib/composite.js
CHANGED
|
@@ -105,8 +105,9 @@ function composite (images) {
|
|
|
105
105
|
input: this._createInputDescriptor(image.input, inputOptions, { allowStream: false }),
|
|
106
106
|
blend: 'over',
|
|
107
107
|
tile: false,
|
|
108
|
-
left:
|
|
109
|
-
top:
|
|
108
|
+
left: 0,
|
|
109
|
+
top: 0,
|
|
110
|
+
hasOffset: false,
|
|
110
111
|
gravity: 0,
|
|
111
112
|
premultiplied: false
|
|
112
113
|
};
|
|
@@ -125,21 +126,23 @@ function composite (images) {
|
|
|
125
126
|
}
|
|
126
127
|
}
|
|
127
128
|
if (is.defined(image.left)) {
|
|
128
|
-
if (is.integer(image.left)
|
|
129
|
+
if (is.integer(image.left)) {
|
|
129
130
|
composite.left = image.left;
|
|
130
131
|
} else {
|
|
131
|
-
throw is.invalidParameterError('left', '
|
|
132
|
+
throw is.invalidParameterError('left', 'integer', image.left);
|
|
132
133
|
}
|
|
133
134
|
}
|
|
134
135
|
if (is.defined(image.top)) {
|
|
135
|
-
if (is.integer(image.top)
|
|
136
|
+
if (is.integer(image.top)) {
|
|
136
137
|
composite.top = image.top;
|
|
137
138
|
} else {
|
|
138
|
-
throw is.invalidParameterError('top', '
|
|
139
|
+
throw is.invalidParameterError('top', 'integer', image.top);
|
|
139
140
|
}
|
|
140
141
|
}
|
|
141
|
-
if (
|
|
142
|
+
if (is.defined(image.top) !== is.defined(image.left)) {
|
|
142
143
|
throw new Error('Expected both left and top to be set');
|
|
144
|
+
} else {
|
|
145
|
+
composite.hasOffset = is.integer(image.top) && is.integer(image.left);
|
|
143
146
|
}
|
|
144
147
|
if (is.defined(image.gravity)) {
|
|
145
148
|
if (is.integer(image.gravity) && is.inRange(image.gravity, 0, 8)) {
|
package/lib/constructor.js
CHANGED
|
@@ -18,12 +18,10 @@ try {
|
|
|
18
18
|
help.push(`- Ensure "${process.platform}" is used at install time as well as runtime`);
|
|
19
19
|
} else if (/dylib/.test(err.message) && /Incompatible library version/.test(err.message)) {
|
|
20
20
|
help.push('- Run "brew update && brew upgrade vips"');
|
|
21
|
-
} else if (/Cannot find module/.test(err.message)) {
|
|
22
|
-
help.push('- Run "npm rebuild --verbose sharp" and look for errors');
|
|
23
21
|
} else {
|
|
24
22
|
help.push(
|
|
25
23
|
'- Remove the "node_modules/sharp" directory then run',
|
|
26
|
-
' "npm install --ignore-scripts=false --verbose" and look for errors'
|
|
24
|
+
' "npm install --ignore-scripts=false --verbose sharp" and look for errors'
|
|
27
25
|
);
|
|
28
26
|
}
|
|
29
27
|
help.push(
|
|
@@ -40,7 +38,7 @@ const debuglog = util.debuglog('sharp');
|
|
|
40
38
|
/**
|
|
41
39
|
* Constructor factory to create an instance of `sharp`, to which further methods are chained.
|
|
42
40
|
*
|
|
43
|
-
* JPEG, PNG, WebP or TIFF format image data can be streamed out from this object.
|
|
41
|
+
* JPEG, PNG, WebP, AVIF or TIFF format image data can be streamed out from this object.
|
|
44
42
|
* When using Stream based output, derived attributes are available from the `info` event.
|
|
45
43
|
*
|
|
46
44
|
* Non-critical problems encountered during processing are emitted as `warning` events.
|
|
@@ -90,10 +88,39 @@ const debuglog = util.debuglog('sharp');
|
|
|
90
88
|
* // Convert an animated GIF to an animated WebP
|
|
91
89
|
* await sharp('in.gif', { animated: true }).toFile('out.webp');
|
|
92
90
|
*
|
|
93
|
-
* @
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
91
|
+
* @example
|
|
92
|
+
* // Read a raw array of pixels and save it to a png
|
|
93
|
+
* const input = Uint8Array.from([255, 255, 255, 0, 0, 0]); // or Uint8ClampedArray
|
|
94
|
+
* const image = sharp(input, {
|
|
95
|
+
* // because the input does not contain its dimensions or how many channels it has
|
|
96
|
+
* // we need to specify it in the constructor options
|
|
97
|
+
* raw: {
|
|
98
|
+
* width: 2,
|
|
99
|
+
* height: 1,
|
|
100
|
+
* channels: 3
|
|
101
|
+
* }
|
|
102
|
+
* });
|
|
103
|
+
* await image.toFile('my-two-pixels.png');
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* // Generate RGB Gaussian noise
|
|
107
|
+
* await sharp({
|
|
108
|
+
* create: {
|
|
109
|
+
* width: 300,
|
|
110
|
+
* height: 200,
|
|
111
|
+
* channels: 3,
|
|
112
|
+
* noise: {
|
|
113
|
+
* type: 'gaussian',
|
|
114
|
+
* mean: 128,
|
|
115
|
+
* sigma: 30
|
|
116
|
+
* }
|
|
117
|
+
* }
|
|
118
|
+
* }).toFile('noise.png');
|
|
119
|
+
*
|
|
120
|
+
* @param {(Buffer|Uint8Array|Uint8ClampedArray|string)} [input] - if present, can be
|
|
121
|
+
* a Buffer / Uint8Array / Uint8ClampedArray containing JPEG, PNG, WebP, AVIF, GIF, SVG, TIFF or raw pixel image data, or
|
|
122
|
+
* a String containing the filesystem path to an JPEG, PNG, WebP, AVIF, GIF, SVG or TIFF image file.
|
|
123
|
+
* JPEG, PNG, WebP, AVIF, GIF, SVG, TIFF or raw pixel image data can be streamed into the object when not present.
|
|
97
124
|
* @param {Object} [options] - if present, is an Object with optional attributes.
|
|
98
125
|
* @param {boolean} [options.failOnError=true] - by default halt processing and raise an error when loading invalid images.
|
|
99
126
|
* Set this flag to `false` if you'd rather apply a "best effort" to decode images, even if the data is corrupt or invalid.
|
|
@@ -103,19 +130,23 @@ const debuglog = util.debuglog('sharp');
|
|
|
103
130
|
* @param {boolean} [options.sequentialRead=false] - Set this to `true` to use sequential rather than random access where possible.
|
|
104
131
|
* This can reduce memory usage and might improve performance on some systems.
|
|
105
132
|
* @param {number} [options.density=72] - number representing the DPI for vector images in the range 1 to 100000.
|
|
106
|
-
* @param {number} [options.pages=1] - number of pages to extract for multi-page input (GIF, TIFF, PDF), use -1 for all pages.
|
|
107
|
-
* @param {number} [options.page=0] - page number to start extracting from for multi-page input (GIF, TIFF, PDF), zero based.
|
|
133
|
+
* @param {number} [options.pages=1] - number of pages to extract for multi-page input (GIF, WebP, AVIF, TIFF, PDF), use -1 for all pages.
|
|
134
|
+
* @param {number} [options.page=0] - page number to start extracting from for multi-page input (GIF, WebP, AVIF, TIFF, PDF), zero based.
|
|
108
135
|
* @param {number} [options.level=0] - level to extract from a multi-level input (OpenSlide), zero based.
|
|
109
136
|
* @param {boolean} [options.animated=false] - Set to `true` to read all frames/pages of an animated image (equivalent of setting `pages` to `-1`).
|
|
110
137
|
* @param {Object} [options.raw] - describes raw pixel input image data. See `raw()` for pixel ordering.
|
|
111
|
-
* @param {number} [options.raw.width]
|
|
112
|
-
* @param {number} [options.raw.height]
|
|
113
|
-
* @param {number} [options.raw.channels] - 1
|
|
138
|
+
* @param {number} [options.raw.width] - integral number of pixels wide.
|
|
139
|
+
* @param {number} [options.raw.height] - integral number of pixels high.
|
|
140
|
+
* @param {number} [options.raw.channels] - integral number of channels, between 1 and 4.
|
|
114
141
|
* @param {Object} [options.create] - describes a new image to be created.
|
|
115
|
-
* @param {number} [options.create.width]
|
|
116
|
-
* @param {number} [options.create.height]
|
|
117
|
-
* @param {number} [options.create.channels] - 3
|
|
142
|
+
* @param {number} [options.create.width] - integral number of pixels wide.
|
|
143
|
+
* @param {number} [options.create.height] - integral number of pixels high.
|
|
144
|
+
* @param {number} [options.create.channels] - integral number of channels, either 3 (RGB) or 4 (RGBA).
|
|
118
145
|
* @param {string|Object} [options.create.background] - parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha.
|
|
146
|
+
* @param {Object} [options.create.noise] - describes a noise to be created.
|
|
147
|
+
* @param {string} [options.create.noise.type] - type of generated noise, currently only `gaussian` is supported.
|
|
148
|
+
* @param {number} [options.create.noise.mean] - mean of pixels in generated noise.
|
|
149
|
+
* @param {number} [options.create.noise.sigma] - standard deviation of pixels in generated noise.
|
|
119
150
|
* @returns {Sharp}
|
|
120
151
|
* @throws {Error} Invalid parameters
|
|
121
152
|
*/
|
|
@@ -211,7 +242,7 @@ const Sharp = function (input, options) {
|
|
|
211
242
|
jpegOptimiseCoding: true,
|
|
212
243
|
jpegQuantisationTable: 0,
|
|
213
244
|
pngProgressive: false,
|
|
214
|
-
pngCompressionLevel:
|
|
245
|
+
pngCompressionLevel: 6,
|
|
215
246
|
pngAdaptiveFiltering: false,
|
|
216
247
|
pngPalette: false,
|
|
217
248
|
pngQuality: 100,
|
|
@@ -233,9 +264,11 @@ const Sharp = function (input, options) {
|
|
|
233
264
|
tiffTileWidth: 256,
|
|
234
265
|
tiffXres: 1.0,
|
|
235
266
|
tiffYres: 1.0,
|
|
236
|
-
heifQuality:
|
|
267
|
+
heifQuality: 50,
|
|
237
268
|
heifLossless: false,
|
|
238
|
-
heifCompression: '
|
|
269
|
+
heifCompression: 'av1',
|
|
270
|
+
heifSpeed: 5,
|
|
271
|
+
heifChromaSubsampling: '4:2:0',
|
|
239
272
|
tileSize: 256,
|
|
240
273
|
tileOverlap: 0,
|
|
241
274
|
tileContainer: 'fs',
|
|
@@ -246,6 +279,7 @@ const Sharp = function (input, options) {
|
|
|
246
279
|
tileSkipBlanks: -1,
|
|
247
280
|
tileBackground: [255, 255, 255, 255],
|
|
248
281
|
tileCentre: false,
|
|
282
|
+
tileId: 'https://example.com/iiif',
|
|
249
283
|
linearA: 1,
|
|
250
284
|
linearB: 0,
|
|
251
285
|
// Function to notify of libvips warnings
|
package/lib/input.js
CHANGED
|
@@ -31,6 +31,9 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
|
|
|
31
31
|
} else if (is.buffer(input)) {
|
|
32
32
|
// Buffer
|
|
33
33
|
inputDescriptor.buffer = input;
|
|
34
|
+
} else if (is.uint8Array(input)) {
|
|
35
|
+
// Uint8Array or Uint8ClampedArray
|
|
36
|
+
inputDescriptor.buffer = Buffer.from(input.buffer);
|
|
34
37
|
} else if (is.plainObject(input) && !is.defined(inputOptions)) {
|
|
35
38
|
// Plain Object descriptor, e.g. create
|
|
36
39
|
inputOptions = input;
|
|
@@ -134,22 +137,50 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
|
|
|
134
137
|
is.object(inputOptions.create) &&
|
|
135
138
|
is.integer(inputOptions.create.width) && inputOptions.create.width > 0 &&
|
|
136
139
|
is.integer(inputOptions.create.height) && inputOptions.create.height > 0 &&
|
|
137
|
-
is.integer(inputOptions.create.channels)
|
|
138
|
-
is.defined(inputOptions.create.background)
|
|
140
|
+
is.integer(inputOptions.create.channels)
|
|
139
141
|
) {
|
|
140
142
|
inputDescriptor.createWidth = inputOptions.create.width;
|
|
141
143
|
inputDescriptor.createHeight = inputOptions.create.height;
|
|
142
144
|
inputDescriptor.createChannels = inputOptions.create.channels;
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
145
|
+
// Noise
|
|
146
|
+
if (is.defined(inputOptions.create.noise)) {
|
|
147
|
+
if (!is.object(inputOptions.create.noise)) {
|
|
148
|
+
throw new Error('Expected noise to be an object');
|
|
149
|
+
}
|
|
150
|
+
if (!is.inArray(inputOptions.create.noise.type, ['gaussian'])) {
|
|
151
|
+
throw new Error('Only gaussian noise is supported at the moment');
|
|
152
|
+
}
|
|
153
|
+
if (!is.inRange(inputOptions.create.channels, 1, 4)) {
|
|
154
|
+
throw is.invalidParameterError('create.channels', 'number between 1 and 4', inputOptions.create.channels);
|
|
155
|
+
}
|
|
156
|
+
inputDescriptor.createNoiseType = inputOptions.create.noise.type;
|
|
157
|
+
if (is.number(inputOptions.create.noise.mean) && is.inRange(inputOptions.create.noise.mean, 0, 10000)) {
|
|
158
|
+
inputDescriptor.createNoiseMean = inputOptions.create.noise.mean;
|
|
159
|
+
} else {
|
|
160
|
+
throw is.invalidParameterError('create.noise.mean', 'number between 0 and 10000', inputOptions.create.noise.mean);
|
|
161
|
+
}
|
|
162
|
+
if (is.number(inputOptions.create.noise.sigma) && is.inRange(inputOptions.create.noise.sigma, 0, 10000)) {
|
|
163
|
+
inputDescriptor.createNoiseSigma = inputOptions.create.noise.sigma;
|
|
164
|
+
} else {
|
|
165
|
+
throw is.invalidParameterError('create.noise.sigma', 'number between 0 and 10000', inputOptions.create.noise.sigma);
|
|
166
|
+
}
|
|
167
|
+
} else if (is.defined(inputOptions.create.background)) {
|
|
168
|
+
if (!is.inRange(inputOptions.create.channels, 3, 4)) {
|
|
169
|
+
throw is.invalidParameterError('create.channels', 'number between 3 and 4', inputOptions.create.channels);
|
|
170
|
+
}
|
|
171
|
+
const background = color(inputOptions.create.background);
|
|
172
|
+
inputDescriptor.createBackground = [
|
|
173
|
+
background.red(),
|
|
174
|
+
background.green(),
|
|
175
|
+
background.blue(),
|
|
176
|
+
Math.round(background.alpha() * 255)
|
|
177
|
+
];
|
|
178
|
+
} else {
|
|
179
|
+
throw new Error('Expected valid noise or background to create a new input image');
|
|
180
|
+
}
|
|
150
181
|
delete inputDescriptor.buffer;
|
|
151
182
|
} else {
|
|
152
|
-
throw new Error('Expected width, height
|
|
183
|
+
throw new Error('Expected valid width, height and channels to create a new input image');
|
|
153
184
|
}
|
|
154
185
|
}
|
|
155
186
|
} else if (is.defined(inputOptions)) {
|
package/lib/is.js
CHANGED
|
@@ -48,6 +48,15 @@ const buffer = function (val) {
|
|
|
48
48
|
return val instanceof Buffer;
|
|
49
49
|
};
|
|
50
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Is this value a Uint8Array or Uint8ClampedArray object?
|
|
53
|
+
* @private
|
|
54
|
+
*/
|
|
55
|
+
const uint8Array = function (val) {
|
|
56
|
+
// allow both since Uint8ClampedArray simply clamps the values between 0-255
|
|
57
|
+
return val instanceof Uint8Array || val instanceof Uint8ClampedArray;
|
|
58
|
+
};
|
|
59
|
+
|
|
51
60
|
/**
|
|
52
61
|
* Is this value a non-empty string?
|
|
53
62
|
* @private
|
|
@@ -110,6 +119,7 @@ module.exports = {
|
|
|
110
119
|
fn: fn,
|
|
111
120
|
bool: bool,
|
|
112
121
|
buffer: buffer,
|
|
122
|
+
uint8Array: uint8Array,
|
|
113
123
|
string: string,
|
|
114
124
|
number: number,
|
|
115
125
|
integer: integer,
|
package/lib/libvips.js
CHANGED
|
@@ -37,10 +37,28 @@ const cachePath = function () {
|
|
|
37
37
|
return libvipsCachePath;
|
|
38
38
|
};
|
|
39
39
|
|
|
40
|
+
const log = function (item) {
|
|
41
|
+
if (item instanceof Error) {
|
|
42
|
+
console.error(`sharp: ${item.message}`);
|
|
43
|
+
} else {
|
|
44
|
+
console.log(`sharp: ${item}`);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const isRosetta = function () {
|
|
49
|
+
/* istanbul ignore next */
|
|
50
|
+
if (process.platform === 'darwin' && process.arch === 'x64') {
|
|
51
|
+
const translated = spawnSync('sysctl sysctl.proc_translated', spawnSyncOptions).stdout;
|
|
52
|
+
return (translated || '').trim() === 'sysctl.proc_translated: 1';
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
};
|
|
56
|
+
|
|
40
57
|
const globalLibvipsVersion = function () {
|
|
41
58
|
if (process.platform !== 'win32') {
|
|
42
|
-
const globalLibvipsVersion = spawnSync(`PKG_CONFIG_PATH="${pkgConfigPath()}" pkg-config --modversion vips-cpp`, spawnSyncOptions).stdout
|
|
43
|
-
|
|
59
|
+
const globalLibvipsVersion = spawnSync(`PKG_CONFIG_PATH="${pkgConfigPath()}" pkg-config --modversion vips-cpp`, spawnSyncOptions).stdout;
|
|
60
|
+
/* istanbul ignore next */
|
|
61
|
+
return (globalLibvipsVersion || '').trim();
|
|
44
62
|
} else {
|
|
45
63
|
return '';
|
|
46
64
|
}
|
|
@@ -81,7 +99,10 @@ const useGlobalLibvips = function () {
|
|
|
81
99
|
if (Boolean(env.SHARP_IGNORE_GLOBAL_LIBVIPS) === true) {
|
|
82
100
|
return false;
|
|
83
101
|
}
|
|
84
|
-
|
|
102
|
+
/* istanbul ignore next */
|
|
103
|
+
if (isRosetta()) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
85
106
|
const globalVipsVersion = globalLibvipsVersion();
|
|
86
107
|
return !!globalVipsVersion && /* istanbul ignore next */
|
|
87
108
|
semver.gte(globalVipsVersion, minimumLibvipsVersion);
|
|
@@ -91,6 +112,7 @@ module.exports = {
|
|
|
91
112
|
minimumLibvipsVersion,
|
|
92
113
|
minimumLibvipsVersionLabelled,
|
|
93
114
|
cachePath,
|
|
115
|
+
log,
|
|
94
116
|
globalLibvipsVersion,
|
|
95
117
|
hasVendoredLibvips,
|
|
96
118
|
pkgConfigPath,
|