@revizly/sharp 0.35.0-revizly4 → 0.35.0-revizly41

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.
Files changed (46) hide show
  1. package/README.md +12 -18
  2. package/{lib/channel.js → dist/channel.cjs} +1 -1
  3. package/dist/channel.mjs +177 -0
  4. package/{lib/colour.js → dist/colour.cjs} +11 -7
  5. package/dist/colour.mjs +199 -0
  6. package/{lib/composite.js → dist/composite.cjs} +2 -1
  7. package/dist/composite.mjs +213 -0
  8. package/{lib/constructor.js → dist/constructor.cjs} +42 -29
  9. package/dist/constructor.mjs +512 -0
  10. package/dist/index.cjs +25 -0
  11. package/dist/index.d.cts +2001 -0
  12. package/dist/index.d.mts +2048 -0
  13. package/dist/index.mjs +25 -0
  14. package/{lib/input.js → dist/input.cjs} +26 -21
  15. package/dist/input.mjs +814 -0
  16. package/{lib/is.js → dist/is.cjs} +1 -1
  17. package/dist/is.mjs +143 -0
  18. package/{lib/libvips.js → dist/libvips.cjs} +35 -30
  19. package/dist/libvips.mjs +212 -0
  20. package/{lib/operation.js → dist/operation.cjs} +33 -51
  21. package/dist/operation.mjs +998 -0
  22. package/{lib/output.js → dist/output.cjs} +161 -28
  23. package/dist/output.mjs +1799 -0
  24. package/{lib/resize.js → dist/resize.cjs} +46 -24
  25. package/dist/resize.mjs +617 -0
  26. package/dist/sharp.cjs +125 -0
  27. package/dist/sharp.mjs +125 -0
  28. package/{lib/utility.js → dist/utility.cjs} +18 -8
  29. package/dist/utility.mjs +301 -0
  30. package/install/build.js +3 -3
  31. package/lib/index.d.ts +105 -75
  32. package/package.json +46 -27
  33. package/src/binding.gyp +18 -13
  34. package/src/common.cc +70 -17
  35. package/src/common.h +22 -3
  36. package/src/metadata.cc +66 -8
  37. package/src/metadata.h +6 -1
  38. package/src/operations.cc +25 -8
  39. package/src/operations.h +1 -1
  40. package/src/pipeline.cc +206 -69
  41. package/src/pipeline.h +16 -1
  42. package/src/stats.cc +6 -6
  43. package/src/utilities.cc +7 -6
  44. package/install/check.js +0 -14
  45. package/lib/index.js +0 -16
  46. package/lib/sharp.js +0 -121
package/src/common.cc CHANGED
@@ -196,8 +196,8 @@ namespace sharp {
196
196
  if (HasAttr(input, "joinValign")) {
197
197
  descriptor->joinValign = AttrAsEnum<VipsAlign>(input, "joinValign", VIPS_TYPE_ALIGN);
198
198
  }
199
- // Limit input images to a given number of pixels, where pixels = width * height
200
199
  descriptor->limitInputPixels = static_cast<uint64_t>(AttrAsInt64(input, "limitInputPixels"));
200
+ descriptor->limitInputChannels = static_cast<uint64_t>(AttrAsInt64(input, "limitInputChannels"));
201
201
  if (HasAttr(input, "access")) {
202
202
  descriptor->access = AttrAsBool(input, "sequentialRead") ? VIPS_ACCESS_SEQUENTIAL : VIPS_ACCESS_RANDOM;
203
203
  }
@@ -289,6 +289,7 @@ namespace sharp {
289
289
  case ImageType::JXL: id = "jxl"; break;
290
290
  case ImageType::RAD: id = "rad"; break;
291
291
  case ImageType::DCRAW: id = "dcraw"; break;
292
+ case ImageType::UHDR: id = "uhdr"; break;
292
293
  case ImageType::VIPS: id = "vips"; break;
293
294
  case ImageType::RAW: id = "raw"; break;
294
295
  case ImageType::UNKNOWN: id = "unknown"; break;
@@ -339,6 +340,9 @@ namespace sharp {
339
340
  { "VipsForeignLoadRadBuffer", ImageType::RAD },
340
341
  { "VipsForeignLoadDcRawFile", ImageType::DCRAW },
341
342
  { "VipsForeignLoadDcRawBuffer", ImageType::DCRAW },
343
+ { "VipsForeignLoadUhdr", ImageType::UHDR },
344
+ { "VipsForeignLoadUhdrFile", ImageType::UHDR },
345
+ { "VipsForeignLoadUhdrBuffer", ImageType::UHDR },
342
346
  { "VipsForeignLoadVips", ImageType::VIPS },
343
347
  { "VipsForeignLoadVipsFile", ImageType::VIPS },
344
348
  { "VipsForeignLoadRaw", ImageType::RAW }
@@ -356,6 +360,9 @@ namespace sharp {
356
360
  imageType = it->second;
357
361
  }
358
362
  }
363
+ if (imageType == ImageType::UHDR) {
364
+ imageType = ImageType::JPEG;
365
+ }
359
366
  return imageType;
360
367
  }
361
368
 
@@ -375,6 +382,9 @@ namespace sharp {
375
382
  imageType = ImageType::MISSING;
376
383
  }
377
384
  }
385
+ if (imageType == ImageType::UHDR) {
386
+ imageType = ImageType::JPEG;
387
+ }
378
388
  return imageType;
379
389
  }
380
390
 
@@ -416,7 +426,7 @@ namespace sharp {
416
426
  }
417
427
  if (ImageTypeSupportsPage(imageType)) {
418
428
  option->set("n", descriptor->pages);
419
- option->set("page", descriptor->page);
429
+ option->set("page", std::max(0, descriptor->page));
420
430
  }
421
431
  switch (imageType) {
422
432
  case ImageType::SVG:
@@ -446,6 +456,22 @@ namespace sharp {
446
456
  return option;
447
457
  }
448
458
 
459
+ /*
460
+ Should HEIF image be re-opened using the primary item?
461
+ */
462
+ static bool HeifPrimaryPageReopen(VImage image, InputDescriptor *descriptor) {
463
+ if (image.get_typeof(VIPS_META_N_PAGES) == G_TYPE_INT && image.get_typeof("heif-primary") == G_TYPE_INT) {
464
+ if (image.get_int(VIPS_META_N_PAGES) > 1 && descriptor->pages == 1 && descriptor->page == -1) {
465
+ int const pagePrimary = image.get_int("heif-primary");
466
+ if (pagePrimary != 0) {
467
+ descriptor->page = pagePrimary;
468
+ return true;
469
+ }
470
+ }
471
+ }
472
+ return false;
473
+ }
474
+
449
475
  /*
450
476
  Open an image from the given InputDescriptor (filesystem, compressed buffer, raw pixel data)
451
477
  */
@@ -480,12 +506,15 @@ namespace sharp {
480
506
  image = VImage::new_from_buffer(descriptor->buffer, descriptor->bufferLength, nullptr, option);
481
507
  if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) {
482
508
  image = SetDensity(image, descriptor->density);
509
+ } else if (imageType == ImageType::HEIF && HeifPrimaryPageReopen(image, descriptor)) {
510
+ option = GetOptionsForImageType(imageType, descriptor);
511
+ image = VImage::new_from_buffer(descriptor->buffer, descriptor->bufferLength, nullptr, option);
483
512
  }
484
- } catch (vips::VError const &err) {
485
- throw vips::VError(std::string("Input buffer has corrupt header: ") + err.what());
513
+ } catch (std::runtime_error const &err) {
514
+ throw std::runtime_error(std::string("Input buffer has corrupt header: ") + err.what());
486
515
  }
487
516
  } else {
488
- throw vips::VError("Input buffer contains unsupported image format");
517
+ throw std::runtime_error("Input buffer contains unsupported image format");
489
518
  }
490
519
  }
491
520
  } else {
@@ -556,10 +585,10 @@ namespace sharp {
556
585
  imageType = DetermineImageType(descriptor->file.data());
557
586
  if (imageType == ImageType::MISSING) {
558
587
  if (descriptor->file.find("<svg") != std::string::npos) {
559
- throw vips::VError("Input file is missing, did you mean "
588
+ throw std::runtime_error("Input file is missing, did you mean "
560
589
  "sharp(Buffer.from('" + descriptor->file.substr(0, 8) + "...')?");
561
590
  }
562
- throw vips::VError("Input file is missing: " + descriptor->file);
591
+ throw std::runtime_error("Input file is missing: " + descriptor->file);
563
592
  }
564
593
  if (imageType != ImageType::UNKNOWN) {
565
594
  try {
@@ -567,12 +596,15 @@ namespace sharp {
567
596
  image = VImage::new_from_file(descriptor->file.data(), option);
568
597
  if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) {
569
598
  image = SetDensity(image, descriptor->density);
599
+ } else if (imageType == ImageType::HEIF && HeifPrimaryPageReopen(image, descriptor)) {
600
+ option = GetOptionsForImageType(imageType, descriptor);
601
+ image = VImage::new_from_file(descriptor->file.data(), option);
570
602
  }
571
- } catch (vips::VError const &err) {
572
- throw vips::VError(std::string("Input file has corrupt header: ") + err.what());
603
+ } catch (std::runtime_error const &err) {
604
+ throw std::runtime_error(std::string("Input file has corrupt header: ") + err.what());
573
605
  }
574
606
  } else {
575
- throw vips::VError("Input file contains unsupported image format");
607
+ throw std::runtime_error("Input file contains unsupported image format");
576
608
  }
577
609
  }
578
610
  }
@@ -580,7 +612,10 @@ namespace sharp {
580
612
  // Limit input images to a given number of pixels, where pixels = width * height
581
613
  if (descriptor->limitInputPixels > 0 &&
582
614
  static_cast<uint64_t>(image.width()) * image.height() > descriptor->limitInputPixels) {
583
- throw vips::VError("Input image exceeds pixel limit");
615
+ throw std::runtime_error("Input image exceeds pixel limit");
616
+ }
617
+ if (descriptor->limitInputChannels > 0 && static_cast<uint64_t>(image.bands()) > descriptor->limitInputChannels) {
618
+ throw std::runtime_error("Input image exceeds channel limit");
584
619
  }
585
620
  return std::make_tuple(image, imageType);
586
621
  }
@@ -600,7 +635,7 @@ namespace sharp {
600
635
  if (HasProfile(image)) {
601
636
  size_t length;
602
637
  const void *data = image.get_blob(VIPS_META_ICC_NAME, &length);
603
- icc.first = static_cast<char*>(g_malloc(length));
638
+ icc.first = static_cast<char*>(vips_malloc(reinterpret_cast<VipsObject*>(image.get_image()), length));
604
639
  icc.second = length;
605
640
  memcpy(icc.first, data, length);
606
641
  }
@@ -613,7 +648,7 @@ namespace sharp {
613
648
  VImage SetProfile(VImage image, std::pair<char*, size_t> icc) {
614
649
  if (icc.first != nullptr) {
615
650
  image = image.copy();
616
- image.set(VIPS_META_ICC_NAME, reinterpret_cast<VipsCallbackFn>(vips_area_free_cb), icc.first, icc.second);
651
+ image.set(VIPS_META_ICC_NAME, nullptr, icc.first, icc.second);
617
652
  }
618
653
  return image;
619
654
  }
@@ -756,19 +791,19 @@ namespace sharp {
756
791
  : image.height();
757
792
  if (imageType == ImageType::JPEG) {
758
793
  if (image.width() > 65535 || height > 65535) {
759
- throw vips::VError("Processed image is too large for the JPEG format");
794
+ throw std::runtime_error("Processed image is too large for the JPEG format");
760
795
  }
761
796
  } else if (imageType == ImageType::WEBP) {
762
797
  if (image.width() > 16383 || height > 16383) {
763
- throw vips::VError("Processed image is too large for the WebP format");
798
+ throw std::runtime_error("Processed image is too large for the WebP format");
764
799
  }
765
800
  } else if (imageType == ImageType::GIF) {
766
801
  if (image.width() > 65535 || height > 65535) {
767
- throw vips::VError("Processed image is too large for the GIF format");
802
+ throw std::runtime_error("Processed image is too large for the GIF format");
768
803
  }
769
804
  } else if (imageType == ImageType::HEIF) {
770
805
  if (image.width() > 16384 || height > 16384) {
771
- throw vips::VError("Processed image is too large for the HEIF format");
806
+ throw std::runtime_error("Processed image is too large for the HEIF format");
772
807
  }
773
808
  }
774
809
  }
@@ -1127,4 +1162,22 @@ namespace sharp {
1127
1162
  }
1128
1163
  return image;
1129
1164
  }
1165
+
1166
+ /*
1167
+ Does this image have a gain map?
1168
+ */
1169
+ bool HasGainMap(VImage image) {
1170
+ return image.get_typeof("gainmap") == VIPS_TYPE_BLOB ||
1171
+ image.get_typeof("gainmap-data") == VIPS_TYPE_BLOB;
1172
+ }
1173
+
1174
+ /*
1175
+ Removes gain map, if any.
1176
+ */
1177
+ VImage RemoveGainMap(VImage image) {
1178
+ VImage copy = image.copy();
1179
+ copy.remove("gainmap");
1180
+ copy.remove("gainmap-data");
1181
+ return copy;
1182
+ }
1130
1183
  } // namespace sharp
package/src/common.h CHANGED
@@ -19,8 +19,8 @@
19
19
 
20
20
  #if (VIPS_MAJOR_VERSION < 8) || \
21
21
  (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION < 18) || \
22
- (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION == 18 && VIPS_MICRO_VERSION < 0)
23
- #error "libvips version 8.18.0+ is required - please see https://sharp.pixelplumbing.com/install"
22
+ (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION == 18 && VIPS_MICRO_VERSION < 3)
23
+ #error "libvips version 8.18.3+ is required - please see https://sharp.pixelplumbing.com/install"
24
24
  #endif
25
25
 
26
26
  #if defined(__has_include)
@@ -29,6 +29,12 @@
29
29
  #endif
30
30
  #endif
31
31
 
32
+ #ifdef __EMSCRIPTEN__
33
+ #define SHARP_CALLBACK_FN_NAME Call
34
+ #else
35
+ #define SHARP_CALLBACK_FN_NAME MakeCallback
36
+ #endif
37
+
32
38
  using vips::VImage;
33
39
 
34
40
  namespace sharp {
@@ -40,6 +46,7 @@ namespace sharp {
40
46
  char *buffer;
41
47
  VipsFailOn failOn;
42
48
  uint64_t limitInputPixels;
49
+ uint64_t limitInputChannels;
43
50
  bool unlimited;
44
51
  VipsAccess access;
45
52
  size_t bufferLength;
@@ -92,6 +99,7 @@ namespace sharp {
92
99
  buffer(nullptr),
93
100
  failOn(VIPS_FAIL_ON_WARNING),
94
101
  limitInputPixels(0x3FFF * 0x3FFF),
102
+ limitInputChannels(5),
95
103
  unlimited(false),
96
104
  access(VIPS_ACCESS_SEQUENTIAL),
97
105
  bufferLength(0),
@@ -105,7 +113,7 @@ namespace sharp {
105
113
  rawPremultiplied(false),
106
114
  rawPageHeight(0),
107
115
  pages(1),
108
- page(0),
116
+ page(-1),
109
117
  createChannels(0),
110
118
  createWidth(0),
111
119
  createHeight(0),
@@ -173,6 +181,7 @@ namespace sharp {
173
181
  JXL,
174
182
  RAD,
175
183
  DCRAW,
184
+ UHDR,
176
185
  VIPS,
177
186
  RAW,
178
187
  UNKNOWN,
@@ -397,6 +406,16 @@ namespace sharp {
397
406
  */
398
407
  VImage StaySequential(VImage image, bool condition = true);
399
408
 
409
+ /*
410
+ Does this image have a gain map?
411
+ */
412
+ bool HasGainMap(VImage image);
413
+
414
+ /*
415
+ Removes gain map, if any.
416
+ */
417
+ VImage RemoveGainMap(VImage image);
418
+
400
419
  } // namespace sharp
401
420
 
402
421
  #endif // SRC_COMMON_H_
package/src/metadata.cc CHANGED
@@ -31,7 +31,7 @@ class MetadataWorker : public Napi::AsyncWorker {
31
31
  sharp::ImageType imageType = sharp::ImageType::UNKNOWN;
32
32
  try {
33
33
  std::tie(image, imageType) = OpenInput(baton->input);
34
- } catch (vips::VError const &err) {
34
+ } catch (std::runtime_error const &err) {
35
35
  (baton->err).append(err.what());
36
36
  }
37
37
  if (imageType != sharp::ImageType::UNKNOWN) {
@@ -141,8 +141,60 @@ class MetadataWorker : public Napi::AsyncWorker {
141
141
  memcpy(baton->tifftagPhotoshop, tifftagPhotoshop, tifftagPhotoshopLength);
142
142
  baton->tifftagPhotoshopLength = tifftagPhotoshopLength;
143
143
  }
144
+ // Gain Map
145
+ if (image.get_typeof("gainmap-data") == VIPS_TYPE_BLOB) {
146
+ size_t gainMapLength;
147
+ void const *gainMap = image.get_blob("gainmap-data", &gainMapLength);
148
+ baton->gainMap = static_cast<char *>(g_malloc(gainMapLength));
149
+ memcpy(baton->gainMap, gainMap, gainMapLength);
150
+ baton->gainMapLength = gainMapLength;
151
+ }
144
152
  // PNG comments
145
153
  vips_image_map(image.get_image(), readPNGComment, &baton->comments);
154
+ // Media type
155
+ std::string mediaType;
156
+ switch (imageType) {
157
+ case sharp::ImageType::JPEG:
158
+ case sharp::ImageType::PNG:
159
+ case sharp::ImageType::WEBP:
160
+ case sharp::ImageType::JP2:
161
+ case sharp::ImageType::TIFF:
162
+ case sharp::ImageType::GIF:
163
+ case sharp::ImageType::FITS:
164
+ case sharp::ImageType::JXL:
165
+ baton->mediaType = "image/" + baton->format;
166
+ break;
167
+ case sharp::ImageType::SVG:
168
+ baton->mediaType = "image/svg+xml";
169
+ break;
170
+ case sharp::ImageType::HEIF:
171
+ if (baton->compression == "av1") {
172
+ baton->mediaType = "image/avif";
173
+ } else if (baton->compression == "hevc") {
174
+ baton->mediaType = "image/heic";
175
+ }
176
+ break;
177
+ case sharp::ImageType::PDF:
178
+ baton->mediaType = "application/pdf";
179
+ break;
180
+ case sharp::ImageType::OPENSLIDE:
181
+ baton->mediaType = "image/tiff";
182
+ break;
183
+ case sharp::ImageType::PPM:
184
+ baton->mediaType = "image/x-portable-pixmap";
185
+ break;
186
+ case sharp::ImageType::EXR:
187
+ baton->mediaType = "image/x-exr";
188
+ break;
189
+ case sharp::ImageType::RAD:
190
+ baton->mediaType = "image/vnd.radiance";
191
+ break;
192
+ case sharp::ImageType::UHDR:
193
+ baton->mediaType = "image/jpeg";
194
+ break;
195
+ default:
196
+ break;
197
+ }
146
198
  }
147
199
 
148
200
  // Clean up
@@ -157,13 +209,16 @@ class MetadataWorker : public Napi::AsyncWorker {
157
209
  // Handle warnings
158
210
  std::string warning = sharp::VipsWarningPop();
159
211
  while (!warning.empty()) {
160
- debuglog.Call(Receiver().Value(), { Napi::String::New(env, warning) });
212
+ debuglog.SHARP_CALLBACK_FN_NAME(Receiver().Value(), { Napi::String::New(env, warning) });
161
213
  warning = sharp::VipsWarningPop();
162
214
  }
163
215
 
164
216
  if (baton->err.empty()) {
165
217
  Napi::Object info = Napi::Object::New(env);
166
218
  info.Set("format", baton->format);
219
+ if (!baton->mediaType.empty()) {
220
+ info.Set("mediaType", baton->mediaType);
221
+ }
167
222
  if (baton->input->bufferLength > 0) {
168
223
  info.Set("size", baton->input->bufferLength);
169
224
  }
@@ -182,10 +237,6 @@ class MetadataWorker : public Napi::AsyncWorker {
182
237
  info.Set("isPalette", baton->isPalette);
183
238
  if (baton->bitsPerSample > 0) {
184
239
  info.Set("bitsPerSample", baton->bitsPerSample);
185
- if (baton->isPalette) {
186
- // Deprecated, remove with libvips 8.17.0
187
- info.Set("paletteBitDepth", baton->bitsPerSample);
188
- }
189
240
  }
190
241
  if (baton->pages > 0) {
191
242
  info.Set("pages", baton->pages);
@@ -276,6 +327,12 @@ class MetadataWorker : public Napi::AsyncWorker {
276
327
  Napi::Buffer<char>::NewOrCopy(env, baton->tifftagPhotoshop,
277
328
  baton->tifftagPhotoshopLength, sharp::FreeCallback));
278
329
  }
330
+ if (baton->gainMapLength > 0) {
331
+ Napi::Object gainMap = Napi::Object::New(env);
332
+ info.Set("gainMap", gainMap);
333
+ gainMap.Set("image",
334
+ Napi::Buffer<char>::NewOrCopy(env, baton->gainMap, baton->gainMapLength, sharp::FreeCallback));
335
+ }
279
336
  if (baton->comments.size() > 0) {
280
337
  int i = 0;
281
338
  Napi::Array comments = Napi::Array::New(env, baton->comments.size());
@@ -287,9 +344,10 @@ class MetadataWorker : public Napi::AsyncWorker {
287
344
  }
288
345
  info.Set("comments", comments);
289
346
  }
290
- Callback().Call(Receiver().Value(), { env.Null(), info });
347
+ Callback().SHARP_CALLBACK_FN_NAME(Receiver().Value(), { env.Null(), info });
291
348
  } else {
292
- Callback().Call(Receiver().Value(), { Napi::Error::New(env, sharp::TrimEnd(baton->err)).Value() });
349
+ Callback().SHARP_CALLBACK_FN_NAME(Receiver().Value(),
350
+ { Napi::Error::New(env, sharp::TrimEnd(baton->err)).Value() });
293
351
  }
294
352
 
295
353
  delete baton->input;
package/src/metadata.h CHANGED
@@ -19,6 +19,7 @@ struct MetadataBaton {
19
19
  sharp::InputDescriptor *input;
20
20
  // Output
21
21
  std::string format;
22
+ std::string mediaType;
22
23
  int width;
23
24
  int height;
24
25
  std::string space;
@@ -53,6 +54,8 @@ struct MetadataBaton {
53
54
  size_t xmpLength;
54
55
  char *tifftagPhotoshop;
55
56
  size_t tifftagPhotoshopLength;
57
+ char *gainMap;
58
+ size_t gainMapLength;
56
59
  MetadataComments comments;
57
60
  std::string err;
58
61
 
@@ -82,7 +85,9 @@ struct MetadataBaton {
82
85
  xmp(nullptr),
83
86
  xmpLength(0),
84
87
  tifftagPhotoshop(nullptr),
85
- tifftagPhotoshopLength(0) {}
88
+ tifftagPhotoshopLength(0),
89
+ gainMap(nullptr),
90
+ gainMapLength(0) {}
86
91
  };
87
92
 
88
93
  Napi::Value metadata(const Napi::CallbackInfo& info);
package/src/operations.cc CHANGED
@@ -14,7 +14,6 @@
14
14
  #include "./operations.h"
15
15
 
16
16
  using vips::VImage;
17
- using vips::VError;
18
17
 
19
18
  namespace sharp {
20
19
  /*
@@ -285,9 +284,9 @@ namespace sharp {
285
284
  /*
286
285
  Trim an image
287
286
  */
288
- 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) {
289
288
  if (image.width() < 3 && image.height() < 3) {
290
- throw VError("Image to trim must be at least 3x3 pixels");
289
+ throw std::runtime_error("Image to trim must be at least 3x3 pixels");
291
290
  }
292
291
  if (background.size() == 0) {
293
292
  // Top-left pixel provides the default background colour if none is given
@@ -320,18 +319,36 @@ namespace sharp {
320
319
  if (widthA > 0 && heightA > 0) {
321
320
  if (width > 0 && height > 0) {
322
321
  // Combined bounding box (B)
323
- int const leftB = std::min(left, leftA);
324
- int const topB = std::min(top, topA);
325
- int const widthB = std::max(left + width, leftA + widthA) - leftB;
326
- int const heightB = std::max(top + height, topA + heightA) - topB;
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
+ }
327
332
  return image.extract_area(leftB, topB, widthB, heightB);
328
333
  } else {
329
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
+ }
330
341
  return image.extract_area(leftA, topA, widthA, heightA);
331
342
  }
332
343
  }
333
344
  }
334
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
+ }
335
352
  return image.extract_area(left, top, width, height);
336
353
  }
337
354
  return image;
@@ -343,7 +360,7 @@ namespace sharp {
343
360
  VImage Linear(VImage image, std::vector<double> const a, std::vector<double> const b) {
344
361
  size_t const bands = static_cast<size_t>(image.bands());
345
362
  if (a.size() > bands) {
346
- throw VError("Band expansion using linear is unsupported");
363
+ throw std::runtime_error("Band expansion using linear is unsupported");
347
364
  }
348
365
  bool const uchar = !Is16Bit(image.interpretation());
349
366
  if (image.has_alpha() && a.size() != bands && (a.size() == 1 || a.size() == bands - 1 || bands - 1 == 1)) {
package/src/operations.h CHANGED
@@ -82,7 +82,7 @@ namespace sharp {
82
82
  /*
83
83
  Trim an image
84
84
  */
85
- 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);
86
86
 
87
87
  /*
88
88
  * Linear adjustment (a * in + b)