sharp 0.20.8 → 0.21.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/src/common.cc CHANGED
@@ -1,4 +1,4 @@
1
- // Copyright 2013, 2014, 2015, 2016, 2017 Lovell Fuller and contributors.
1
+ // Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019 Lovell Fuller and contributors.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -37,6 +37,14 @@ namespace sharp {
37
37
  std::string AttrAsStr(v8::Handle<v8::Object> obj, std::string attr) {
38
38
  return *Nan::Utf8String(Nan::Get(obj, Nan::New(attr).ToLocalChecked()).ToLocalChecked());
39
39
  }
40
+ std::vector<double> AttrAsRgba(v8::Handle<v8::Object> obj, std::string attr) {
41
+ v8::Local<v8::Object> background = AttrAs<v8::Object>(obj, attr);
42
+ std::vector<double> rgba(4);
43
+ for (unsigned int i = 0; i < 4; i++) {
44
+ rgba[i] = AttrTo<double>(background, i);
45
+ }
46
+ return rgba;
47
+ }
40
48
 
41
49
  // Create an InputDescriptor instance from a v8::Object describing an input image
42
50
  InputDescriptor* CreateInputDescriptor(
@@ -72,10 +80,7 @@ namespace sharp {
72
80
  descriptor->createChannels = AttrTo<uint32_t>(input, "createChannels");
73
81
  descriptor->createWidth = AttrTo<uint32_t>(input, "createWidth");
74
82
  descriptor->createHeight = AttrTo<uint32_t>(input, "createHeight");
75
- v8::Local<v8::Object> createBackground = AttrAs<v8::Object>(input, "createBackground");
76
- for (unsigned int i = 0; i < 4; i++) {
77
- descriptor->createBackground[i] = AttrTo<double>(createBackground, i);
78
- }
83
+ descriptor->createBackground = AttrAsRgba(input, "createBackground");
79
84
  }
80
85
  return descriptor;
81
86
  }
@@ -132,6 +137,7 @@ namespace sharp {
132
137
  case ImageType::VIPS: id = "v"; break;
133
138
  case ImageType::RAW: id = "raw"; break;
134
139
  case ImageType::UNKNOWN: id = "unknown"; break;
140
+ case ImageType::MISSING: id = "missing"; break;
135
141
  }
136
142
  return id;
137
143
  }
@@ -198,6 +204,10 @@ namespace sharp {
198
204
  } else if (EndsWith(loader, "Magick") || EndsWith(loader, "MagickFile")) {
199
205
  imageType = ImageType::MAGICK;
200
206
  }
207
+ } else {
208
+ if (EndsWith(vips::VError().what(), " not found\n")) {
209
+ imageType = ImageType::MISSING;
210
+ }
201
211
  }
202
212
  return imageType;
203
213
  }
@@ -240,8 +250,8 @@ namespace sharp {
240
250
  if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) {
241
251
  SetDensity(image, descriptor->density);
242
252
  }
243
- } catch (...) {
244
- throw vips::VError("Input buffer has corrupt header");
253
+ } catch (vips::VError const &err) {
254
+ throw vips::VError(std::string("Input buffer has corrupt header: ") + err.what());
245
255
  }
246
256
  } else {
247
257
  throw vips::VError("Input buffer contains unsupported image format");
@@ -264,6 +274,9 @@ namespace sharp {
264
274
  } else {
265
275
  // From filesystem
266
276
  imageType = DetermineImageType(descriptor->file.data());
277
+ if (imageType == ImageType::MISSING) {
278
+ throw vips::VError("Input file is missing");
279
+ }
267
280
  if (imageType != ImageType::UNKNOWN) {
268
281
  try {
269
282
  vips::VOption *option = VImage::option()
@@ -282,11 +295,11 @@ namespace sharp {
282
295
  if (imageType == ImageType::SVG || imageType == ImageType::PDF || imageType == ImageType::MAGICK) {
283
296
  SetDensity(image, descriptor->density);
284
297
  }
285
- } catch (...) {
286
- throw vips::VError("Input file has corrupt header");
298
+ } catch (vips::VError const &err) {
299
+ throw vips::VError(std::string("Input file has corrupt header: ") + err.what());
287
300
  }
288
301
  } else {
289
- throw vips::VError("Input file is missing or of an unsupported image format");
302
+ throw vips::VError("Input file contains unsupported image format");
290
303
  }
291
304
  }
292
305
  }
@@ -370,10 +383,6 @@ namespace sharp {
370
383
  if (image.width() > 65535 || image.height() > 65535) {
371
384
  throw vips::VError("Processed image is too large for the JPEG format");
372
385
  }
373
- } else if (imageType == ImageType::PNG) {
374
- if (image.width() > 2147483647 || image.height() > 2147483647) {
375
- throw vips::VError("Processed image is too large for the PNG format");
376
- }
377
386
  } else if (imageType == ImageType::WEBP) {
378
387
  if (image.width() > 16383 || image.height() > 16383) {
379
388
  throw vips::VError("Processed image is too large for the WebP format");
@@ -606,4 +615,40 @@ namespace sharp {
606
615
  }
607
616
  }
608
617
 
618
+ /*
619
+ Apply the alpha channel to a given colour
620
+ */
621
+ std::tuple<VImage, std::vector<double>> ApplyAlpha(VImage image, std::vector<double> colour) {
622
+ // Scale up 8-bit values to match 16-bit input image
623
+ double const multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0;
624
+ // Create alphaColour colour
625
+ std::vector<double> alphaColour;
626
+ if (image.bands() > 2) {
627
+ alphaColour = {
628
+ multiplier * colour[0],
629
+ multiplier * colour[1],
630
+ multiplier * colour[2]
631
+ };
632
+ } else {
633
+ // Convert sRGB to greyscale
634
+ alphaColour = { multiplier * (
635
+ 0.2126 * colour[0] +
636
+ 0.7152 * colour[1] +
637
+ 0.0722 * colour[2])
638
+ };
639
+ }
640
+ // Add alpha channel to alphaColour colour
641
+ if (colour[3] < 255.0 || HasAlpha(image)) {
642
+ alphaColour.push_back(colour[3] * multiplier);
643
+ }
644
+ // Ensure alphaColour colour uses correct colourspace
645
+ alphaColour = sharp::GetRgbaAsColourspace(alphaColour, image.interpretation());
646
+ // Add non-transparent alpha channel, if required
647
+ if (colour[3] < 255.0 && !HasAlpha(image)) {
648
+ image = image.bandjoin(
649
+ VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier));
650
+ }
651
+ return std::make_tuple(image, alphaColour);
652
+ }
653
+
609
654
  } // namespace sharp
package/src/common.h CHANGED
@@ -1,4 +1,4 @@
1
- // Copyright 2013, 2014, 2015, 2016, 2017 Lovell Fuller and contributors.
1
+ // Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019 Lovell Fuller and contributors.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -25,8 +25,8 @@
25
25
 
26
26
  // Verify platform and compiler compatibility
27
27
 
28
- #if (VIPS_MAJOR_VERSION < 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION < 6))
29
- #error libvips version 8.6.1+ is required - see sharp.pixelplumbing.com/page/install
28
+ #if (VIPS_MAJOR_VERSION < 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION < 7))
29
+ #error libvips version 8.7.0+ is required - see sharp.pixelplumbing.com/page/install
30
30
  #endif
31
31
 
32
32
  #if ((!defined(__clang__)) && defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6)))
@@ -57,11 +57,11 @@ namespace sharp {
57
57
  int createChannels;
58
58
  int createWidth;
59
59
  int createHeight;
60
- double createBackground[4];
60
+ std::vector<double> createBackground;
61
61
 
62
62
  InputDescriptor():
63
63
  buffer(nullptr),
64
- failOnError(FALSE),
64
+ failOnError(TRUE),
65
65
  bufferLength(0),
66
66
  density(72.0),
67
67
  rawChannels(0),
@@ -70,17 +70,14 @@ namespace sharp {
70
70
  page(0),
71
71
  createChannels(0),
72
72
  createWidth(0),
73
- createHeight(0) {
74
- createBackground[0] = 0.0;
75
- createBackground[1] = 0.0;
76
- createBackground[2] = 0.0;
77
- createBackground[3] = 255.0;
78
- }
73
+ createHeight(0),
74
+ createBackground{ 0.0, 0.0, 0.0, 255.0 } {}
79
75
  };
80
76
 
81
77
  // Convenience methods to access the attributes of a v8::Object
82
78
  bool HasAttr(v8::Handle<v8::Object> obj, std::string attr);
83
79
  std::string AttrAsStr(v8::Handle<v8::Object> obj, std::string attr);
80
+ std::vector<double> AttrAsRgba(v8::Handle<v8::Object> obj, std::string attr);
84
81
  template<typename T> v8::Local<T> AttrAs(v8::Handle<v8::Object> obj, std::string attr) {
85
82
  return Nan::Get(obj, Nan::New(attr).ToLocalChecked()).ToLocalChecked().As<T>();
86
83
  }
@@ -109,7 +106,8 @@ namespace sharp {
109
106
  FITS,
110
107
  VIPS,
111
108
  RAW,
112
- UNKNOWN
109
+ UNKNOWN,
110
+ MISSING
113
111
  };
114
112
 
115
113
  // How many tasks are in the queue?
@@ -255,6 +253,11 @@ namespace sharp {
255
253
  */
256
254
  std::vector<double> GetRgbaAsColourspace(std::vector<double> const rgba, VipsInterpretation const interpretation);
257
255
 
256
+ /*
257
+ Apply the alpha channel to a given colour
258
+ */
259
+ std::tuple<VImage, std::vector<double>> ApplyAlpha(VImage image, std::vector<double> colour);
260
+
258
261
  } // namespace sharp
259
262
 
260
263
  #endif // SRC_COMMON_H_