sharp 0.33.5 → 0.35.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 +12 -18
- package/dist/channel.cjs +177 -0
- package/{lib/channel.js → dist/channel.mjs} +11 -8
- package/dist/colour.cjs +199 -0
- package/{lib/colour.js → dist/colour.mjs} +42 -23
- package/dist/composite.cjs +213 -0
- package/{lib/composite.js → dist/composite.mjs} +14 -11
- package/{lib/constructor.js → dist/constructor.cjs} +102 -43
- package/dist/constructor.mjs +511 -0
- package/dist/index.cjs +25 -0
- package/dist/index.d.cts +1999 -0
- package/dist/index.d.mts +2046 -0
- package/dist/index.mjs +25 -0
- package/dist/input.cjs +814 -0
- package/{lib/input.js → dist/input.mjs} +232 -76
- package/{lib/is.js → dist/is.cjs} +19 -45
- package/dist/is.mjs +143 -0
- package/{lib/libvips.js → dist/libvips.cjs} +52 -43
- package/dist/libvips.mjs +212 -0
- package/dist/operation.cjs +998 -0
- package/{lib/operation.js → dist/operation.mjs} +131 -91
- package/{lib/output.js → dist/output.cjs} +262 -61
- package/dist/output.mjs +1788 -0
- package/dist/resize.cjs +617 -0
- package/{lib/resize.js → dist/resize.mjs} +65 -35
- package/dist/sharp.cjs +174 -0
- package/dist/sharp.mjs +174 -0
- package/{lib/utility.js → dist/utility.cjs} +30 -25
- package/dist/utility.mjs +301 -0
- package/install/build.js +38 -0
- package/lib/index.d.ts +387 -142
- package/package.json +90 -85
- package/src/binding.gyp +49 -26
- package/src/common.cc +203 -111
- package/src/common.h +68 -40
- package/src/metadata.cc +106 -22
- package/src/metadata.h +15 -5
- package/src/operations.cc +62 -21
- package/src/operations.h +15 -3
- package/src/pipeline.cc +391 -201
- package/src/pipeline.h +36 -7
- package/src/sharp.cc +11 -8
- package/src/stats.cc +16 -13
- package/src/stats.h +6 -3
- package/src/utilities.cc +35 -15
- package/src/utilities.h +5 -2
- package/install/check.js +0 -41
- package/lib/index.js +0 -16
- package/lib/sharp.js +0 -114
package/src/operations.cc
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/*!
|
|
2
|
+
Copyright 2013 Lovell Fuller and others.
|
|
3
|
+
SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
3
5
|
|
|
4
6
|
#include <algorithm>
|
|
5
7
|
#include <functional>
|
|
@@ -8,11 +10,10 @@
|
|
|
8
10
|
#include <vector>
|
|
9
11
|
#include <vips/vips8>
|
|
10
12
|
|
|
11
|
-
#include "common.h"
|
|
12
|
-
#include "operations.h"
|
|
13
|
+
#include "./common.h"
|
|
14
|
+
#include "./operations.h"
|
|
13
15
|
|
|
14
16
|
using vips::VImage;
|
|
15
|
-
using vips::VError;
|
|
16
17
|
|
|
17
18
|
namespace sharp {
|
|
18
19
|
/*
|
|
@@ -40,7 +41,7 @@ namespace sharp {
|
|
|
40
41
|
typeBeforeTint = VIPS_INTERPRETATION_sRGB;
|
|
41
42
|
}
|
|
42
43
|
// Apply lookup table
|
|
43
|
-
if (
|
|
44
|
+
if (image.has_alpha()) {
|
|
44
45
|
VImage alpha = image[image.bands() - 1];
|
|
45
46
|
image = RemoveAlpha(image)
|
|
46
47
|
.colourspace(VIPS_INTERPRETATION_B_W)
|
|
@@ -83,7 +84,7 @@ namespace sharp {
|
|
|
83
84
|
// Scale luminance, join to chroma, convert back to original colourspace
|
|
84
85
|
VImage normalized = luminance.linear(f, a).bandjoin(chroma).colourspace(typeBeforeNormalize);
|
|
85
86
|
// Attach original alpha channel, if any
|
|
86
|
-
if (
|
|
87
|
+
if (image.has_alpha()) {
|
|
87
88
|
// Extract original alpha channel
|
|
88
89
|
VImage alpha = image[image.bands() - 1];
|
|
89
90
|
// Join alpha channel to normalised image
|
|
@@ -106,7 +107,7 @@ namespace sharp {
|
|
|
106
107
|
* Gamma encoding/decoding
|
|
107
108
|
*/
|
|
108
109
|
VImage Gamma(VImage image, double const exponent) {
|
|
109
|
-
if (
|
|
110
|
+
if (image.has_alpha()) {
|
|
110
111
|
// Separate alpha channel
|
|
111
112
|
VImage alpha = image[image.bands() - 1];
|
|
112
113
|
return RemoveAlpha(image).gamma(VImage::option()->set("exponent", exponent)).bandjoin(alpha);
|
|
@@ -132,7 +133,7 @@ namespace sharp {
|
|
|
132
133
|
* Produce the "negative" of the image.
|
|
133
134
|
*/
|
|
134
135
|
VImage Negate(VImage image, bool const negateAlpha) {
|
|
135
|
-
if (
|
|
136
|
+
if (image.has_alpha() && !negateAlpha) {
|
|
136
137
|
// Separate alpha channel
|
|
137
138
|
VImage alpha = image[image.bands() - 1];
|
|
138
139
|
return RemoveAlpha(image).invert().bandjoin(alpha);
|
|
@@ -205,7 +206,7 @@ namespace sharp {
|
|
|
205
206
|
VImage Modulate(VImage image, double const brightness, double const saturation,
|
|
206
207
|
int const hue, double const lightness) {
|
|
207
208
|
VipsInterpretation colourspaceBeforeModulate = image.interpretation();
|
|
208
|
-
if (
|
|
209
|
+
if (image.has_alpha()) {
|
|
209
210
|
// Separate alpha channel
|
|
210
211
|
VImage alpha = image[image.bands() - 1];
|
|
211
212
|
return RemoveAlpha(image)
|
|
@@ -283,9 +284,9 @@ namespace sharp {
|
|
|
283
284
|
/*
|
|
284
285
|
Trim an image
|
|
285
286
|
*/
|
|
286
|
-
VImage Trim(VImage image, std::vector<double> background, double threshold, bool const lineArt) {
|
|
287
|
+
VImage Trim(VImage image, std::vector<double> background, double threshold, bool const lineArt, int const margin) {
|
|
287
288
|
if (image.width() < 3 && image.height() < 3) {
|
|
288
|
-
throw
|
|
289
|
+
throw std::runtime_error("Image to trim must be at least 3x3 pixels");
|
|
289
290
|
}
|
|
290
291
|
if (background.size() == 0) {
|
|
291
292
|
// Top-left pixel provides the default background colour if none is given
|
|
@@ -297,7 +298,7 @@ namespace sharp {
|
|
|
297
298
|
threshold *= 256.0;
|
|
298
299
|
}
|
|
299
300
|
std::vector<double> backgroundAlpha({ background.back() });
|
|
300
|
-
if (
|
|
301
|
+
if (image.has_alpha()) {
|
|
301
302
|
background.pop_back();
|
|
302
303
|
} else {
|
|
303
304
|
background.resize(image.bands());
|
|
@@ -307,7 +308,7 @@ namespace sharp {
|
|
|
307
308
|
->set("background", background)
|
|
308
309
|
->set("line_art", lineArt)
|
|
309
310
|
->set("threshold", threshold));
|
|
310
|
-
if (
|
|
311
|
+
if (image.has_alpha()) {
|
|
311
312
|
// Search alpha channel (A)
|
|
312
313
|
int leftA, topA, widthA, heightA;
|
|
313
314
|
VImage alpha = image[image.bands() - 1];
|
|
@@ -318,18 +319,36 @@ namespace sharp {
|
|
|
318
319
|
if (widthA > 0 && heightA > 0) {
|
|
319
320
|
if (width > 0 && height > 0) {
|
|
320
321
|
// Combined bounding box (B)
|
|
321
|
-
int
|
|
322
|
-
int
|
|
323
|
-
int
|
|
324
|
-
int
|
|
322
|
+
int leftB = std::min(left, leftA);
|
|
323
|
+
int topB = std::min(top, topA);
|
|
324
|
+
int widthB = std::max(left + width, leftA + widthA) - leftB;
|
|
325
|
+
int heightB = std::max(top + height, topA + heightA) - topB;
|
|
326
|
+
if (margin > 0) {
|
|
327
|
+
leftB = std::max(0, leftB - margin);
|
|
328
|
+
topB = std::max(0, topB - margin);
|
|
329
|
+
widthB = std::min(image.width() - leftB, widthB + 2 * margin);
|
|
330
|
+
heightB = std::min(image.height() - topB, heightB + 2 * margin);
|
|
331
|
+
}
|
|
325
332
|
return image.extract_area(leftB, topB, widthB, heightB);
|
|
326
333
|
} else {
|
|
327
334
|
// Use alpha only
|
|
335
|
+
if (margin > 0) {
|
|
336
|
+
leftA = std::max(0, leftA - margin);
|
|
337
|
+
topA = std::max(0, topA - margin);
|
|
338
|
+
widthA = std::min(image.width() - leftA, widthA + 2 * margin);
|
|
339
|
+
heightA = std::min(image.height() - topA, heightA + 2 * margin);
|
|
340
|
+
}
|
|
328
341
|
return image.extract_area(leftA, topA, widthA, heightA);
|
|
329
342
|
}
|
|
330
343
|
}
|
|
331
344
|
}
|
|
332
345
|
if (width > 0 && height > 0) {
|
|
346
|
+
if (margin > 0) {
|
|
347
|
+
left = std::max(0, left - margin);
|
|
348
|
+
top = std::max(0, top - margin);
|
|
349
|
+
width = std::min(image.width() - left, width + 2 * margin);
|
|
350
|
+
height = std::min(image.height() - top, height + 2 * margin);
|
|
351
|
+
}
|
|
333
352
|
return image.extract_area(left, top, width, height);
|
|
334
353
|
}
|
|
335
354
|
return image;
|
|
@@ -341,10 +360,10 @@ namespace sharp {
|
|
|
341
360
|
VImage Linear(VImage image, std::vector<double> const a, std::vector<double> const b) {
|
|
342
361
|
size_t const bands = static_cast<size_t>(image.bands());
|
|
343
362
|
if (a.size() > bands) {
|
|
344
|
-
throw
|
|
363
|
+
throw std::runtime_error("Band expansion using linear is unsupported");
|
|
345
364
|
}
|
|
346
365
|
bool const uchar = !Is16Bit(image.interpretation());
|
|
347
|
-
if (
|
|
366
|
+
if (image.has_alpha() && a.size() != bands && (a.size() == 1 || a.size() == bands - 1 || bands - 1 == 1)) {
|
|
348
367
|
// Separate alpha channel
|
|
349
368
|
VImage alpha = image[bands - 1];
|
|
350
369
|
return RemoveAlpha(image).linear(a, b, VImage::option()->set("uchar", uchar)).bandjoin(alpha);
|
|
@@ -357,7 +376,7 @@ namespace sharp {
|
|
|
357
376
|
* Unflatten
|
|
358
377
|
*/
|
|
359
378
|
VImage Unflatten(VImage image) {
|
|
360
|
-
if (
|
|
379
|
+
if (image.has_alpha()) {
|
|
361
380
|
VImage alpha = image[image.bands() - 1];
|
|
362
381
|
VImage noAlpha = RemoveAlpha(image);
|
|
363
382
|
return noAlpha.bandjoin(alpha & (noAlpha.colourspace(VIPS_INTERPRETATION_B_W) < 255));
|
|
@@ -472,4 +491,26 @@ namespace sharp {
|
|
|
472
491
|
}
|
|
473
492
|
}
|
|
474
493
|
|
|
494
|
+
/*
|
|
495
|
+
* Dilate an image
|
|
496
|
+
*/
|
|
497
|
+
VImage Dilate(VImage image, int const width) {
|
|
498
|
+
int const maskWidth = 2 * width + 1;
|
|
499
|
+
VImage mask = VImage::new_matrix(maskWidth, maskWidth);
|
|
500
|
+
return image.morph(
|
|
501
|
+
mask,
|
|
502
|
+
VIPS_OPERATION_MORPHOLOGY_DILATE).invert();
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/*
|
|
506
|
+
* Erode an image
|
|
507
|
+
*/
|
|
508
|
+
VImage Erode(VImage image, int const width) {
|
|
509
|
+
int const maskWidth = 2 * width + 1;
|
|
510
|
+
VImage mask = VImage::new_matrix(maskWidth, maskWidth);
|
|
511
|
+
return image.morph(
|
|
512
|
+
mask,
|
|
513
|
+
VIPS_OPERATION_MORPHOLOGY_ERODE).invert();
|
|
514
|
+
}
|
|
515
|
+
|
|
475
516
|
} // namespace sharp
|
package/src/operations.h
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/*!
|
|
2
|
+
Copyright 2013 Lovell Fuller and others.
|
|
3
|
+
SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
3
5
|
|
|
4
6
|
#ifndef SRC_OPERATIONS_H_
|
|
5
7
|
#define SRC_OPERATIONS_H_
|
|
@@ -8,6 +10,7 @@
|
|
|
8
10
|
#include <functional>
|
|
9
11
|
#include <memory>
|
|
10
12
|
#include <tuple>
|
|
13
|
+
#include <vector>
|
|
11
14
|
#include <vips/vips8>
|
|
12
15
|
|
|
13
16
|
using vips::VImage;
|
|
@@ -79,7 +82,7 @@ namespace sharp {
|
|
|
79
82
|
/*
|
|
80
83
|
Trim an image
|
|
81
84
|
*/
|
|
82
|
-
VImage Trim(VImage image, std::vector<double> background, double threshold, bool const lineArt);
|
|
85
|
+
VImage Trim(VImage image, std::vector<double> background, double threshold, bool const lineArt, int const margin);
|
|
83
86
|
|
|
84
87
|
/*
|
|
85
88
|
* Linear adjustment (a * in + b)
|
|
@@ -120,6 +123,15 @@ namespace sharp {
|
|
|
120
123
|
VImage EmbedMultiPage(VImage image, int left, int top, int width, int height,
|
|
121
124
|
VipsExtend extendWith, std::vector<double> background, int nPages, int *pageHeight);
|
|
122
125
|
|
|
126
|
+
/*
|
|
127
|
+
* Dilate an image
|
|
128
|
+
*/
|
|
129
|
+
VImage Dilate(VImage image, int const maskWidth);
|
|
130
|
+
|
|
131
|
+
/*
|
|
132
|
+
* Erode an image
|
|
133
|
+
*/
|
|
134
|
+
VImage Erode(VImage image, int const maskWidth);
|
|
123
135
|
} // namespace sharp
|
|
124
136
|
|
|
125
137
|
#endif // SRC_OPERATIONS_H_
|