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/src/utilities.cc CHANGED
@@ -15,8 +15,7 @@
15
15
  #include <cmath>
16
16
  #include <string>
17
17
 
18
- #include <node.h>
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
- NAN_METHOD(cache) {
45
- HandleScope();
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]->IsInt32()) {
49
- vips_cache_set_max_mem(To<int32_t>(info[0]).FromJust() * 1048576);
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]->IsInt32()) {
53
- vips_cache_set_max_files(To<int32_t>(info[1]).FromJust());
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]->IsInt32()) {
57
- vips_cache_set_max(To<int32_t>(info[2]).FromJust());
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
- Local<Object> memory = New<Object>();
62
- Set(memory, New("current").ToLocalChecked(),
63
- New<Integer>(static_cast<int>(round(vips_tracked_get_mem() / 1048576))));
64
- Set(memory, New("high").ToLocalChecked(),
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
- Local<Object> files = New<Object>();
70
- Set(files, New("current").ToLocalChecked(), New<Integer>(vips_tracked_get_files()));
71
- Set(files, New("max").ToLocalChecked(), New<Integer>(vips_cache_get_max_files()));
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
- Local<Object> items = New<Object>();
75
- Set(items, New("current").ToLocalChecked(), New<Integer>(vips_cache_get_size()));
76
- Set(items, New("max").ToLocalChecked(), New<Integer>(vips_cache_get_max()));
77
-
78
- Local<Object> cache = New<Object>();
79
- Set(cache, New("memory").ToLocalChecked(), memory);
80
- Set(cache, New("files").ToLocalChecked(), files);
81
- Set(cache, New("items").ToLocalChecked(), items);
82
- info.GetReturnValue().Set(cache);
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
- NAN_METHOD(concurrency) {
89
- HandleScope();
90
-
70
+ Napi::Value concurrency(const Napi::CallbackInfo& info) {
91
71
  // Set concurrency
92
- if (info[0]->IsInt32()) {
93
- vips_concurrency_set(To<int32_t>(info[0]).FromJust());
72
+ if (info[0].IsNumber()) {
73
+ vips_concurrency_set(info[0].As<Napi::Number>().Int32Value());
94
74
  }
95
75
  // Get concurrency
96
- info.GetReturnValue().Set(New<Integer>(vips_concurrency_get()));
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
- NAN_METHOD(counters) {
103
- using sharp::counterProcess;
104
- using sharp::counterQueue;
105
-
106
- HandleScope();
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
- NAN_METHOD(simd) {
117
- HandleScope();
118
-
92
+ Napi::Value simd(const Napi::CallbackInfo& info) {
119
93
  // Set state
120
- if (info[0]->IsBoolean()) {
121
- vips_vector_set_enabled(To<bool>(info[0]).FromJust());
94
+ if (info[0].IsBoolean()) {
95
+ vips_vector_set_enabled(info[0].As<Napi::Boolean>().Value());
122
96
  }
123
97
  // Get state
124
- info.GetReturnValue().Set(New<Boolean>(vips_vector_isenabled()));
98
+ return Napi::Boolean::New(info.Env(), vips_vector_isenabled());
125
99
  }
126
100
 
127
101
  /*
128
102
  Get libvips version
129
103
  */
130
- NAN_METHOD(libvipsVersion) {
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.GetReturnValue().Set(New(version).ToLocalChecked());
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
- NAN_METHOD(format) {
141
- HandleScope();
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
- Local<Boolean> hasInputFile =
159
- New<Boolean>(vips_type_find("VipsOperation", (f + "load").c_str()));
160
- Local<Boolean> hasInputBuffer =
161
- New<Boolean>(vips_type_find("VipsOperation", (f + "load_buffer").c_str()));
162
- Local<Object> input = New<Object>();
163
- Set(input, attrFile, hasInputFile);
164
- Set(input, attrBuffer, hasInputBuffer);
165
- Set(input, attrStream, hasInputBuffer);
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
- Local<Boolean> hasOutputFile =
168
- New<Boolean>(vips_type_find("VipsOperation", (f + "save").c_str()));
169
- Local<Boolean> hasOutputBuffer =
170
- New<Boolean>(vips_type_find("VipsOperation", (f + "save_buffer").c_str()));
171
- Local<Object> output = New<Object>();
172
- Set(output, attrFile, hasOutputFile);
173
- Set(output, attrBuffer, hasOutputBuffer);
174
- Set(output, attrStream, hasOutputBuffer);
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
- Local<Object> container = New<Object>();
177
- Local<String> formatId = New(f).ToLocalChecked();
178
- Set(container, attrId, formatId);
179
- Set(container, attrInput, input);
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(format, formatId, container);
144
+ format.Set(f, container);
183
145
  }
184
146
 
185
147
  // Raw, uncompressed data
186
- Local<Object> raw = New<Object>();
187
- Local<String> rawId = New("raw").ToLocalChecked();
188
- Set(raw, attrId, rawId);
189
- Set(format, rawId, raw);
190
- Local<Boolean> supported = New<Boolean>(true);
191
- Local<Boolean> unsupported = New<Boolean>(false);
192
- Local<Object> rawInput = New<Object>();
193
- Set(rawInput, attrFile, unsupported);
194
- Set(rawInput, attrBuffer, supported);
195
- Set(rawInput, attrStream, supported);
196
- Set(raw, attrInput, rawInput);
197
- Local<Object> rawOutput = New<Object>();
198
- Set(rawOutput, attrFile, unsupported);
199
- Set(rawOutput, attrBuffer, supported);
200
- Set(rawOutput, attrStream, supported);
201
- Set(raw, attrOutput, rawOutput);
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
- NAN_METHOD(_maxColourDistance) {
212
- using vips::VImage;
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(*Utf8String(info[0]));
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(*Utf8String(info[0]));
180
+ image1 = VImage::new_from_file(info[0].As<Napi::String>().Utf8Value().c_str());
226
181
  } catch (...) {
227
- return ThrowError("Input file 1 has corrupt header");
182
+ throw Napi::Error::New(env, "Input file 1 has corrupt header");
228
183
  }
229
184
  } else {
230
- return ThrowError("Input file 1 is of an unsupported image format");
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(*Utf8String(info[1]));
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(*Utf8String(info[1]));
191
+ image2 = VImage::new_from_file(info[1].As<Napi::String>().Utf8Value().c_str());
237
192
  } catch (...) {
238
- return ThrowError("Input file 2 has corrupt header");
193
+ throw Napi::Error::New(env, "Input file 2 has corrupt header");
239
194
  }
240
195
  } else {
241
- return ThrowError("Input file 2 is of an unsupported image format");
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
- return ThrowError("mismatchedBands");
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
- return ThrowError("mismatchedDimensions");
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
- return ThrowError(err.what());
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
- info.GetReturnValue().Set(New<Number>(maxColourDistance));
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 <nan.h>
18
+ #include <napi.h>
19
19
 
20
- NAN_METHOD(cache);
21
- NAN_METHOD(concurrency);
22
- NAN_METHOD(counters);
23
- NAN_METHOD(simd);
24
- NAN_METHOD(libvipsVersion);
25
- NAN_METHOD(format);
26
- NAN_METHOD(_maxColourDistance);
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_