sharp 0.24.0 → 0.25.2
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 +5 -6
- package/binding.gyp +18 -3
- package/install/libvips.js +10 -8
- package/lib/channel.js +3 -2
- package/lib/composite.js +1 -2
- package/lib/constructor.js +8 -1
- package/lib/input.js +19 -37
- package/lib/libvips.js +10 -8
- package/lib/output.js +15 -4
- package/lib/resize.js +15 -3
- package/package.json +18 -12
- package/src/common.cc +46 -39
- package/src/common.h +15 -21
- package/src/metadata.cc +57 -94
- package/src/metadata.h +2 -2
- package/src/pipeline.cc +238 -294
- package/src/pipeline.h +4 -4
- package/src/sharp.cc +14 -24
- package/src/stats.cc +58 -85
- package/src/stats.h +4 -8
- package/src/utilities.cc +100 -145
- package/src/utilities.h +8 -8
package/src/utilities.cc
CHANGED
|
@@ -15,8 +15,7 @@
|
|
|
15
15
|
#include <cmath>
|
|
16
16
|
#include <string>
|
|
17
17
|
|
|
18
|
-
#include <
|
|
19
|
-
#include <nan.h>
|
|
18
|
+
#include <napi.h>
|
|
20
19
|
#include <vips/vips8>
|
|
21
20
|
#include <vips/vector.h>
|
|
22
21
|
|
|
@@ -24,183 +23,145 @@
|
|
|
24
23
|
#include "operations.h"
|
|
25
24
|
#include "utilities.h"
|
|
26
25
|
|
|
27
|
-
using v8::Boolean;
|
|
28
|
-
using v8::Integer;
|
|
29
|
-
using v8::Local;
|
|
30
|
-
using v8::Number;
|
|
31
|
-
using v8::Object;
|
|
32
|
-
using v8::String;
|
|
33
|
-
|
|
34
|
-
using Nan::HandleScope;
|
|
35
|
-
using Nan::New;
|
|
36
|
-
using Nan::Set;
|
|
37
|
-
using Nan::ThrowError;
|
|
38
|
-
using Nan::To;
|
|
39
|
-
using Nan::Utf8String;
|
|
40
|
-
|
|
41
26
|
/*
|
|
42
27
|
Get and set cache limits
|
|
43
28
|
*/
|
|
44
|
-
|
|
45
|
-
|
|
29
|
+
Napi::Value cache(const Napi::CallbackInfo& info) {
|
|
30
|
+
Napi::Env env = info.Env();
|
|
46
31
|
|
|
47
32
|
// Set memory limit
|
|
48
|
-
if (info[0]
|
|
49
|
-
vips_cache_set_max_mem(
|
|
33
|
+
if (info[0].IsNumber()) {
|
|
34
|
+
vips_cache_set_max_mem(info[0].As<Napi::Number>().Int32Value() * 1048576);
|
|
50
35
|
}
|
|
51
36
|
// Set file limit
|
|
52
|
-
if (info[1]
|
|
53
|
-
vips_cache_set_max_files(
|
|
37
|
+
if (info[1].IsNumber()) {
|
|
38
|
+
vips_cache_set_max_files(info[1].As<Napi::Number>().Int32Value());
|
|
54
39
|
}
|
|
55
40
|
// Set items limit
|
|
56
|
-
if (info[2]
|
|
57
|
-
vips_cache_set_max(
|
|
41
|
+
if (info[2].IsNumber()) {
|
|
42
|
+
vips_cache_set_max(info[2].As<Napi::Number>().Int32Value());
|
|
58
43
|
}
|
|
59
44
|
|
|
60
45
|
// Get memory stats
|
|
61
|
-
|
|
62
|
-
Set(
|
|
63
|
-
|
|
64
|
-
Set(
|
|
65
|
-
New<Integer>(static_cast<int>(round(vips_tracked_get_mem_highwater() / 1048576))));
|
|
66
|
-
Set(memory, New("max").ToLocalChecked(),
|
|
67
|
-
New<Integer>(static_cast<int>(round(vips_cache_get_max_mem() / 1048576))));
|
|
46
|
+
Napi::Object memory = Napi::Object::New(env);
|
|
47
|
+
memory.Set("current", round(vips_tracked_get_mem() / 1048576));
|
|
48
|
+
memory.Set("high", round(vips_tracked_get_mem_highwater() / 1048576));
|
|
49
|
+
memory.Set("max", round(vips_cache_get_max_mem() / 1048576));
|
|
68
50
|
// Get file stats
|
|
69
|
-
|
|
70
|
-
Set(
|
|
71
|
-
Set(
|
|
51
|
+
Napi::Object files = Napi::Object::New(env);
|
|
52
|
+
files.Set("current", vips_tracked_get_files());
|
|
53
|
+
files.Set("max", vips_cache_get_max_files());
|
|
72
54
|
|
|
73
55
|
// Get item stats
|
|
74
|
-
|
|
75
|
-
Set(
|
|
76
|
-
Set(
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
Set(
|
|
80
|
-
Set(
|
|
81
|
-
Set(
|
|
82
|
-
|
|
56
|
+
Napi::Object items = Napi::Object::New(env);
|
|
57
|
+
items.Set("current", vips_cache_get_size());
|
|
58
|
+
items.Set("max", vips_cache_get_max());
|
|
59
|
+
|
|
60
|
+
Napi::Object cache = Napi::Object::New(env);
|
|
61
|
+
cache.Set("memory", memory);
|
|
62
|
+
cache.Set("files", files);
|
|
63
|
+
cache.Set("items", items);
|
|
64
|
+
return cache;
|
|
83
65
|
}
|
|
84
66
|
|
|
85
67
|
/*
|
|
86
68
|
Get and set size of thread pool
|
|
87
69
|
*/
|
|
88
|
-
|
|
89
|
-
HandleScope();
|
|
90
|
-
|
|
70
|
+
Napi::Value concurrency(const Napi::CallbackInfo& info) {
|
|
91
71
|
// Set concurrency
|
|
92
|
-
if (info[0]
|
|
93
|
-
vips_concurrency_set(
|
|
72
|
+
if (info[0].IsNumber()) {
|
|
73
|
+
vips_concurrency_set(info[0].As<Napi::Number>().Int32Value());
|
|
94
74
|
}
|
|
95
75
|
// Get concurrency
|
|
96
|
-
info.
|
|
76
|
+
return Napi::Number::New(info.Env(), vips_concurrency_get());
|
|
97
77
|
}
|
|
98
78
|
|
|
99
79
|
/*
|
|
100
80
|
Get internal counters (queued tasks, processing tasks)
|
|
101
81
|
*/
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
Local<Object> counters = New<Object>();
|
|
108
|
-
Set(counters, New("queue").ToLocalChecked(), New<Integer>(counterQueue));
|
|
109
|
-
Set(counters, New("process").ToLocalChecked(), New<Integer>(counterProcess));
|
|
110
|
-
info.GetReturnValue().Set(counters);
|
|
82
|
+
Napi::Value counters(const Napi::CallbackInfo& info) {
|
|
83
|
+
Napi::Object counters = Napi::Object::New(info.Env());
|
|
84
|
+
counters.Set("queue", sharp::counterQueue);
|
|
85
|
+
counters.Set("process", sharp::counterProcess);
|
|
86
|
+
return counters;
|
|
111
87
|
}
|
|
112
88
|
|
|
113
89
|
/*
|
|
114
90
|
Get and set use of SIMD vector unit instructions
|
|
115
91
|
*/
|
|
116
|
-
|
|
117
|
-
HandleScope();
|
|
118
|
-
|
|
92
|
+
Napi::Value simd(const Napi::CallbackInfo& info) {
|
|
119
93
|
// Set state
|
|
120
|
-
if (info[0]
|
|
121
|
-
vips_vector_set_enabled(
|
|
94
|
+
if (info[0].IsBoolean()) {
|
|
95
|
+
vips_vector_set_enabled(info[0].As<Napi::Boolean>().Value());
|
|
122
96
|
}
|
|
123
97
|
// Get state
|
|
124
|
-
info.
|
|
98
|
+
return Napi::Boolean::New(info.Env(), vips_vector_isenabled());
|
|
125
99
|
}
|
|
126
100
|
|
|
127
101
|
/*
|
|
128
102
|
Get libvips version
|
|
129
103
|
*/
|
|
130
|
-
|
|
131
|
-
HandleScope();
|
|
104
|
+
Napi::Value libvipsVersion(const Napi::CallbackInfo& info) {
|
|
132
105
|
char version[9];
|
|
133
106
|
g_snprintf(version, sizeof(version), "%d.%d.%d", vips_version(0), vips_version(1), vips_version(2));
|
|
134
|
-
info.
|
|
107
|
+
return Napi::String::New(info.Env(), version);
|
|
135
108
|
}
|
|
136
109
|
|
|
137
110
|
/*
|
|
138
111
|
Get available input/output file/buffer/stream formats
|
|
139
112
|
*/
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
// Attribute names
|
|
144
|
-
Local<String> attrId = New("id").ToLocalChecked();
|
|
145
|
-
Local<String> attrInput = New("input").ToLocalChecked();
|
|
146
|
-
Local<String> attrOutput = New("output").ToLocalChecked();
|
|
147
|
-
Local<String> attrFile = New("file").ToLocalChecked();
|
|
148
|
-
Local<String> attrBuffer = New("buffer").ToLocalChecked();
|
|
149
|
-
Local<String> attrStream = New("stream").ToLocalChecked();
|
|
150
|
-
|
|
151
|
-
// Which load/save operations are available for each compressed format?
|
|
152
|
-
Local<Object> format = New<Object>();
|
|
113
|
+
Napi::Value format(const Napi::CallbackInfo& info) {
|
|
114
|
+
Napi::Env env = info.Env();
|
|
115
|
+
Napi::Object format = Napi::Object::New(env);
|
|
153
116
|
for (std::string const f : {
|
|
154
117
|
"jpeg", "png", "webp", "tiff", "magick", "openslide", "dz",
|
|
155
118
|
"ppm", "fits", "gif", "svg", "heif", "pdf", "vips"
|
|
156
119
|
}) {
|
|
157
120
|
// Input
|
|
158
|
-
|
|
159
|
-
New
|
|
160
|
-
|
|
161
|
-
New
|
|
162
|
-
|
|
163
|
-
Set(
|
|
164
|
-
Set(
|
|
165
|
-
Set(
|
|
121
|
+
Napi::Boolean hasInputFile =
|
|
122
|
+
Napi::Boolean::New(env, vips_type_find("VipsOperation", (f + "load").c_str()));
|
|
123
|
+
Napi::Boolean hasInputBuffer =
|
|
124
|
+
Napi::Boolean::New(env, vips_type_find("VipsOperation", (f + "load_buffer").c_str()));
|
|
125
|
+
Napi::Object input = Napi::Object::New(env);
|
|
126
|
+
input.Set("file", hasInputFile);
|
|
127
|
+
input.Set("buffer", hasInputBuffer);
|
|
128
|
+
input.Set("stream", hasInputBuffer);
|
|
166
129
|
// Output
|
|
167
|
-
|
|
168
|
-
New
|
|
169
|
-
|
|
170
|
-
New
|
|
171
|
-
|
|
172
|
-
Set(
|
|
173
|
-
Set(
|
|
174
|
-
Set(
|
|
130
|
+
Napi::Boolean hasOutputFile =
|
|
131
|
+
Napi::Boolean::New(env, vips_type_find("VipsOperation", (f + "save").c_str()));
|
|
132
|
+
Napi::Boolean hasOutputBuffer =
|
|
133
|
+
Napi::Boolean::New(env, vips_type_find("VipsOperation", (f + "save_buffer").c_str()));
|
|
134
|
+
Napi::Object output = Napi::Object::New(env);
|
|
135
|
+
output.Set("file", hasOutputFile);
|
|
136
|
+
output.Set("buffer", hasOutputBuffer);
|
|
137
|
+
output.Set("stream", hasOutputBuffer);
|
|
175
138
|
// Other attributes
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
Set(
|
|
179
|
-
Set(
|
|
180
|
-
Set(container, attrOutput, output);
|
|
139
|
+
Napi::Object container = Napi::Object::New(env);
|
|
140
|
+
container.Set("id", f);
|
|
141
|
+
container.Set("input", input);
|
|
142
|
+
container.Set("output", output);
|
|
181
143
|
// Add to set of formats
|
|
182
|
-
Set(
|
|
144
|
+
format.Set(f, container);
|
|
183
145
|
}
|
|
184
146
|
|
|
185
147
|
// Raw, uncompressed data
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
Set(
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
Set(
|
|
194
|
-
Set(
|
|
195
|
-
Set(
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
Set(
|
|
199
|
-
Set(
|
|
200
|
-
Set(
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
info.GetReturnValue().Set(format);
|
|
148
|
+
Napi::Boolean supported = Napi::Boolean::New(env, true);
|
|
149
|
+
Napi::Boolean unsupported = Napi::Boolean::New(env, false);
|
|
150
|
+
Napi::Object rawInput = Napi::Object::New(env);
|
|
151
|
+
rawInput.Set("file", unsupported);
|
|
152
|
+
rawInput.Set("buffer", supported);
|
|
153
|
+
rawInput.Set("stream", supported);
|
|
154
|
+
Napi::Object rawOutput = Napi::Object::New(env);
|
|
155
|
+
rawOutput.Set("file", unsupported);
|
|
156
|
+
rawOutput.Set("buffer", supported);
|
|
157
|
+
rawOutput.Set("stream", supported);
|
|
158
|
+
Napi::Object raw = Napi::Object::New(env);
|
|
159
|
+
raw.Set("id", "raw");
|
|
160
|
+
raw.Set("input", rawInput);
|
|
161
|
+
raw.Set("output", rawOutput);
|
|
162
|
+
format.Set("raw", raw);
|
|
163
|
+
|
|
164
|
+
return format;
|
|
204
165
|
}
|
|
205
166
|
|
|
206
167
|
/*
|
|
@@ -208,65 +169,59 @@ NAN_METHOD(format) {
|
|
|
208
169
|
Calculates the maximum colour distance using the DE2000 algorithm
|
|
209
170
|
between two images of the same dimensions and number of channels.
|
|
210
171
|
*/
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
using vips::VError;
|
|
214
|
-
using sharp::DetermineImageType;
|
|
215
|
-
using sharp::ImageType;
|
|
216
|
-
using sharp::HasAlpha;
|
|
217
|
-
|
|
218
|
-
HandleScope();
|
|
172
|
+
Napi::Value _maxColourDistance(const Napi::CallbackInfo& info) {
|
|
173
|
+
Napi::Env env = info.Env();
|
|
219
174
|
|
|
220
175
|
// Open input files
|
|
221
176
|
VImage image1;
|
|
222
|
-
ImageType imageType1 = DetermineImageType(
|
|
223
|
-
if (imageType1 != ImageType::UNKNOWN) {
|
|
177
|
+
sharp::ImageType imageType1 = sharp::DetermineImageType(info[0].As<Napi::String>().Utf8Value().data());
|
|
178
|
+
if (imageType1 != sharp::ImageType::UNKNOWN) {
|
|
224
179
|
try {
|
|
225
|
-
image1 = VImage::new_from_file(
|
|
180
|
+
image1 = VImage::new_from_file(info[0].As<Napi::String>().Utf8Value().c_str());
|
|
226
181
|
} catch (...) {
|
|
227
|
-
|
|
182
|
+
throw Napi::Error::New(env, "Input file 1 has corrupt header");
|
|
228
183
|
}
|
|
229
184
|
} else {
|
|
230
|
-
|
|
185
|
+
throw Napi::Error::New(env, "Input file 1 is of an unsupported image format");
|
|
231
186
|
}
|
|
232
187
|
VImage image2;
|
|
233
|
-
ImageType imageType2 = DetermineImageType(
|
|
234
|
-
if (imageType2 != ImageType::UNKNOWN) {
|
|
188
|
+
sharp::ImageType imageType2 = sharp::DetermineImageType(info[1].As<Napi::String>().Utf8Value().data());
|
|
189
|
+
if (imageType2 != sharp::ImageType::UNKNOWN) {
|
|
235
190
|
try {
|
|
236
|
-
image2 = VImage::new_from_file(
|
|
191
|
+
image2 = VImage::new_from_file(info[1].As<Napi::String>().Utf8Value().c_str());
|
|
237
192
|
} catch (...) {
|
|
238
|
-
|
|
193
|
+
throw Napi::Error::New(env, "Input file 2 has corrupt header");
|
|
239
194
|
}
|
|
240
195
|
} else {
|
|
241
|
-
|
|
196
|
+
throw Napi::Error::New(env, "Input file 2 is of an unsupported image format");
|
|
242
197
|
}
|
|
243
198
|
// Ensure same number of channels
|
|
244
199
|
if (image1.bands() != image2.bands()) {
|
|
245
|
-
|
|
200
|
+
throw Napi::Error::New(env, "mismatchedBands");
|
|
246
201
|
}
|
|
247
202
|
// Ensure same dimensions
|
|
248
203
|
if (image1.width() != image2.width() || image1.height() != image2.height()) {
|
|
249
|
-
|
|
204
|
+
throw Napi::Error::New(env, "mismatchedDimensions");
|
|
250
205
|
}
|
|
251
206
|
|
|
252
207
|
double maxColourDistance;
|
|
253
208
|
try {
|
|
254
209
|
// Premultiply and remove alpha
|
|
255
|
-
if (HasAlpha(image1)) {
|
|
210
|
+
if (sharp::HasAlpha(image1)) {
|
|
256
211
|
image1 = image1.premultiply().extract_band(1, VImage::option()->set("n", image1.bands() - 1));
|
|
257
212
|
}
|
|
258
|
-
if (HasAlpha(image2)) {
|
|
213
|
+
if (sharp::HasAlpha(image2)) {
|
|
259
214
|
image2 = image2.premultiply().extract_band(1, VImage::option()->set("n", image2.bands() - 1));
|
|
260
215
|
}
|
|
261
216
|
// Calculate colour distance
|
|
262
217
|
maxColourDistance = image1.dE00(image2).max();
|
|
263
|
-
} catch (VError const &err) {
|
|
264
|
-
|
|
218
|
+
} catch (vips::VError const &err) {
|
|
219
|
+
throw Napi::Error::New(env, err.what());
|
|
265
220
|
}
|
|
266
221
|
|
|
267
222
|
// Clean up libvips' per-request data and threads
|
|
268
223
|
vips_error_clear();
|
|
269
224
|
vips_thread_shutdown();
|
|
270
225
|
|
|
271
|
-
|
|
226
|
+
return Napi::Number::New(env, maxColourDistance);
|
|
272
227
|
}
|
package/src/utilities.h
CHANGED
|
@@ -15,14 +15,14 @@
|
|
|
15
15
|
#ifndef SRC_UTILITIES_H_
|
|
16
16
|
#define SRC_UTILITIES_H_
|
|
17
17
|
|
|
18
|
-
#include <
|
|
18
|
+
#include <napi.h>
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
Napi::Value cache(const Napi::CallbackInfo& info);
|
|
21
|
+
Napi::Value concurrency(const Napi::CallbackInfo& info);
|
|
22
|
+
Napi::Value counters(const Napi::CallbackInfo& info);
|
|
23
|
+
Napi::Value simd(const Napi::CallbackInfo& info);
|
|
24
|
+
Napi::Value libvipsVersion(const Napi::CallbackInfo& info);
|
|
25
|
+
Napi::Value format(const Napi::CallbackInfo& info);
|
|
26
|
+
Napi::Value _maxColourDistance(const Napi::CallbackInfo& info);
|
|
27
27
|
|
|
28
28
|
#endif // SRC_UTILITIES_H_
|