sharp 0.20.8 → 0.21.0

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
@@ -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
  }
@@ -370,10 +375,6 @@ namespace sharp {
370
375
  if (image.width() > 65535 || image.height() > 65535) {
371
376
  throw vips::VError("Processed image is too large for the JPEG format");
372
377
  }
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
378
  } else if (imageType == ImageType::WEBP) {
378
379
  if (image.width() > 16383 || image.height() > 16383) {
379
380
  throw vips::VError("Processed image is too large for the WebP format");
@@ -606,4 +607,40 @@ namespace sharp {
606
607
  }
607
608
  }
608
609
 
610
+ /*
611
+ Apply the alpha channel to a given colour
612
+ */
613
+ std::tuple<VImage, std::vector<double>> ApplyAlpha(VImage image, std::vector<double> colour) {
614
+ // Scale up 8-bit values to match 16-bit input image
615
+ double const multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0;
616
+ // Create alphaColour colour
617
+ std::vector<double> alphaColour;
618
+ if (image.bands() > 2) {
619
+ alphaColour = {
620
+ multiplier * colour[0],
621
+ multiplier * colour[1],
622
+ multiplier * colour[2]
623
+ };
624
+ } else {
625
+ // Convert sRGB to greyscale
626
+ alphaColour = { multiplier * (
627
+ 0.2126 * colour[0] +
628
+ 0.7152 * colour[1] +
629
+ 0.0722 * colour[2])
630
+ };
631
+ }
632
+ // Add alpha channel to alphaColour colour
633
+ if (colour[3] < 255.0 || HasAlpha(image)) {
634
+ alphaColour.push_back(colour[3] * multiplier);
635
+ }
636
+ // Ensure alphaColour colour uses correct colourspace
637
+ alphaColour = sharp::GetRgbaAsColourspace(alphaColour, image.interpretation());
638
+ // Add non-transparent alpha channel, if required
639
+ if (colour[3] < 255.0 && !HasAlpha(image)) {
640
+ image = image.bandjoin(
641
+ VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier));
642
+ }
643
+ return std::make_tuple(image, alphaColour);
644
+ }
645
+
609
646
  } // namespace sharp
package/src/common.h CHANGED
@@ -57,7 +57,7 @@ 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),
@@ -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
  }
@@ -255,6 +252,11 @@ namespace sharp {
255
252
  */
256
253
  std::vector<double> GetRgbaAsColourspace(std::vector<double> const rgba, VipsInterpretation const interpretation);
257
254
 
255
+ /*
256
+ Apply the alpha channel to a given colour
257
+ */
258
+ std::tuple<VImage, std::vector<double>> ApplyAlpha(VImage image, std::vector<double> colour);
259
+
258
260
  } // namespace sharp
259
261
 
260
262
  #endif // SRC_COMMON_H_