sharp 0.20.5 → 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(
@@ -55,7 +63,7 @@ namespace sharp {
55
63
  descriptor->failOnError = AttrTo<bool>(input, "failOnError");
56
64
  // Density for vector-based input
57
65
  if (HasAttr(input, "density")) {
58
- descriptor->density = AttrTo<uint32_t>(input, "density");
66
+ descriptor->density = AttrTo<double>(input, "density");
59
67
  }
60
68
  // Raw pixel input
61
69
  if (HasAttr(input, "rawChannels")) {
@@ -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
  }
@@ -228,7 +233,7 @@ namespace sharp {
228
233
  ->set("access", accessMethod)
229
234
  ->set("fail", descriptor->failOnError);
230
235
  if (imageType == ImageType::SVG || imageType == ImageType::PDF) {
231
- option->set("dpi", static_cast<double>(descriptor->density));
236
+ option->set("dpi", descriptor->density);
232
237
  }
233
238
  if (imageType == ImageType::MAGICK) {
234
239
  option->set("density", std::to_string(descriptor->density).data());
@@ -270,7 +275,7 @@ namespace sharp {
270
275
  ->set("access", accessMethod)
271
276
  ->set("fail", descriptor->failOnError);
272
277
  if (imageType == ImageType::SVG || imageType == ImageType::PDF) {
273
- option->set("dpi", static_cast<double>(descriptor->density));
278
+ option->set("dpi", descriptor->density);
274
279
  }
275
280
  if (imageType == ImageType::MAGICK) {
276
281
  option->set("density", std::to_string(descriptor->density).data());
@@ -355,8 +360,8 @@ namespace sharp {
355
360
  /*
356
361
  Set pixels/mm resolution based on a pixels/inch density.
357
362
  */
358
- void SetDensity(VImage image, const int density) {
359
- const double pixelsPerMm = static_cast<double>(density) / 25.4;
363
+ void SetDensity(VImage image, const double density) {
364
+ const double pixelsPerMm = density / 25.4;
360
365
  image.set("Xres", pixelsPerMm);
361
366
  image.set("Yres", pixelsPerMm);
362
367
  image.set(VIPS_META_RESOLUTION_UNIT, "in");
@@ -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
@@ -49,7 +49,7 @@ namespace sharp {
49
49
  char *buffer;
50
50
  bool failOnError;
51
51
  size_t bufferLength;
52
- int density;
52
+ double density;
53
53
  int rawChannels;
54
54
  int rawWidth;
55
55
  int rawHeight;
@@ -57,30 +57,27 @@ 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
64
  failOnError(FALSE),
65
65
  bufferLength(0),
66
- density(72),
66
+ density(72.0),
67
67
  rawChannels(0),
68
68
  rawWidth(0),
69
69
  rawHeight(0),
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
  }
@@ -186,7 +183,7 @@ namespace sharp {
186
183
  /*
187
184
  Set pixels/mm resolution based on a pixels/inch density.
188
185
  */
189
- void SetDensity(VImage image, const int density);
186
+ void SetDensity(VImage image, const double density);
190
187
 
191
188
  /*
192
189
  Check the proposed format supports the current dimensions.
@@ -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_