sharp 0.30.7 → 0.31.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.
@@ -0,0 +1,27 @@
1
+ // Object part of VRegion class
2
+
3
+ #ifdef HAVE_CONFIG_H
4
+ #include <config.h>
5
+ #endif /*HAVE_CONFIG_H*/
6
+
7
+ #include <vips/vips8>
8
+
9
+ #include <vips/debug.h>
10
+
11
+ VIPS_NAMESPACE_START
12
+
13
+ VRegion
14
+ VRegion::new_from_image( VImage image )
15
+ {
16
+ VipsRegion *region;
17
+
18
+ if( !(region = vips_region_new( image.get_image() )) ) {
19
+ throw VError();
20
+ }
21
+
22
+ VRegion out( region );
23
+
24
+ return( out );
25
+ }
26
+
27
+ VIPS_NAMESPACE_END
@@ -1,5 +1,4 @@
1
1
  // bodies for vips operations
2
- // Mon Nov 1 03:31:09 PM CET 2021
3
2
  // this file is generated automatically, do not edit!
4
3
 
5
4
  VImage VImage::CMC2LCh( VOption *options ) const
@@ -943,6 +942,14 @@ VipsBlob *VImage::dzsave_buffer( VOption *options ) const
943
942
  return( buffer );
944
943
  }
945
944
 
945
+ void VImage::dzsave_target( VTarget target, VOption *options ) const
946
+ {
947
+ call( "dzsave_target",
948
+ (options ? options : VImage::option())->
949
+ set( "in", *this )->
950
+ set( "target", target ) );
951
+ }
952
+
946
953
  VImage VImage::embed( int x, int y, int width, int height, VOption *options ) const
947
954
  {
948
955
  VImage out;
@@ -3521,6 +3528,14 @@ VipsBlob *VImage::tiffsave_buffer( VOption *options ) const
3521
3528
  return( buffer );
3522
3529
  }
3523
3530
 
3531
+ void VImage::tiffsave_target( VTarget target, VOption *options ) const
3532
+ {
3533
+ call( "tiffsave_target",
3534
+ (options ? options : VImage::option())->
3535
+ set( "in", *this )->
3536
+ set( "target", target ) );
3537
+ }
3538
+
3524
3539
  VImage VImage::tilecache( VOption *options ) const
3525
3540
  {
3526
3541
  VImage out;
package/src/operations.cc CHANGED
@@ -68,10 +68,9 @@ namespace sharp {
68
68
  // Extract luminance
69
69
  VImage luminance = lab[0];
70
70
  // Find luminance range
71
- VImage stats = luminance.stats();
72
- double min = stats(0, 0)[0];
73
- double max = stats(1, 0)[0];
74
- if (min != max) {
71
+ int const min = luminance.percent(1);
72
+ int const max = luminance.percent(99);
73
+ if (std::abs(max - min) > 1) {
75
74
  // Extract chroma
76
75
  VImage chroma = lab.extract_band(1, VImage::option()->set("n", 2));
77
76
  // Calculate multiplication factor and addition
@@ -112,6 +111,19 @@ namespace sharp {
112
111
  }
113
112
  }
114
113
 
114
+ /*
115
+ * Flatten image to remove alpha channel
116
+ */
117
+ VImage Flatten(VImage image, std::vector<double> flattenBackground) {
118
+ double const multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0;
119
+ std::vector<double> background {
120
+ flattenBackground[0] * multiplier,
121
+ flattenBackground[1] * multiplier,
122
+ flattenBackground[2] * multiplier
123
+ };
124
+ return image.flatten(VImage::option()->set("background", background));
125
+ }
126
+
115
127
  /**
116
128
  * Produce the "negative" of the image.
117
129
  */
@@ -262,42 +274,74 @@ namespace sharp {
262
274
  /*
263
275
  Trim an image
264
276
  */
265
- VImage Trim(VImage image, double const threshold) {
277
+ VImage Trim(VImage image, std::vector<double> background, double threshold) {
266
278
  if (image.width() < 3 && image.height() < 3) {
267
279
  throw VError("Image to trim must be at least 3x3 pixels");
268
280
  }
269
- // Top-left pixel provides the background colour
270
- VImage background = image.extract_area(0, 0, 1, 1);
271
- if (HasAlpha(background)) {
272
- background = background.flatten();
281
+
282
+ // Scale up 8-bit values to match 16-bit input image
283
+ double multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0;
284
+ threshold *= multiplier;
285
+
286
+ std::vector<double> backgroundAlpha(1);
287
+ if (background.size() == 0) {
288
+ // Top-left pixel provides the default background colour if none is given
289
+ background = image.extract_area(0, 0, 1, 1)(0, 0);
290
+ multiplier = 1.0;
291
+ }
292
+ if (background.size() == 4) {
293
+ // Just discard the alpha because flattening the background colour with
294
+ // itself (effectively what find_trim() does) gives the same result
295
+ backgroundAlpha[0] = background[3] * multiplier;
273
296
  }
297
+ background = {
298
+ background[0] * multiplier,
299
+ background[1] * multiplier,
300
+ background[2] * multiplier
301
+ };
302
+
274
303
  int left, top, width, height;
275
304
  left = image.find_trim(&top, &width, &height, VImage::option()
276
- ->set("background", background(0, 0))
305
+ ->set("background", background)
277
306
  ->set("threshold", threshold));
278
- if (width == 0 || height == 0) {
279
- if (HasAlpha(image)) {
280
- // Search alpha channel
281
- VImage alpha = image[image.bands() - 1];
282
- VImage backgroundAlpha = alpha.extract_area(0, 0, 1, 1);
283
- left = alpha.find_trim(&top, &width, &height, VImage::option()
284
- ->set("background", backgroundAlpha(0, 0))
285
- ->set("threshold", threshold));
286
- }
287
- if (width == 0 || height == 0) {
288
- throw VError("Unexpected error while trimming. Try to lower the tolerance");
307
+ if (HasAlpha(image)) {
308
+ // Search alpha channel (A)
309
+ int leftA, topA, widthA, heightA;
310
+ VImage alpha = image[image.bands() - 1];
311
+ leftA = alpha.find_trim(&topA, &widthA, &heightA, VImage::option()
312
+ ->set("background", backgroundAlpha)
313
+ ->set("threshold", threshold));
314
+ if (widthA > 0 && heightA > 0) {
315
+ if (width > 0 && height > 0) {
316
+ // Combined bounding box (B)
317
+ int const leftB = std::min(left, leftA);
318
+ int const topB = std::min(top, topA);
319
+ int const widthB = std::max(left + width, leftA + widthA) - leftB;
320
+ int const heightB = std::max(top + height, topA + heightA) - topB;
321
+ return image.extract_area(leftB, topB, widthB, heightB);
322
+ } else {
323
+ // Use alpha only
324
+ return image.extract_area(leftA, topA, widthA, heightA);
325
+ }
289
326
  }
290
327
  }
291
- return image.extract_area(left, top, width, height);
328
+ if (width > 0 && height > 0) {
329
+ return image.extract_area(left, top, width, height);
330
+ }
331
+ return image;
292
332
  }
293
333
 
294
334
  /*
295
335
  * Calculate (a * in + b)
296
336
  */
297
- VImage Linear(VImage image, double const a, double const b) {
298
- if (HasAlpha(image)) {
337
+ VImage Linear(VImage image, std::vector<double> const a, std::vector<double> const b) {
338
+ size_t const bands = static_cast<size_t>(image.bands());
339
+ if (a.size() > bands) {
340
+ throw VError("Band expansion using linear is unsupported");
341
+ }
342
+ if (HasAlpha(image) && a.size() != bands && (a.size() == 1 || a.size() == bands - 1 || bands - 1 == 1)) {
299
343
  // Separate alpha channel
300
- VImage alpha = image[image.bands() - 1];
344
+ VImage alpha = image[bands - 1];
301
345
  return RemoveAlpha(image).linear(a, b).bandjoin(alpha);
302
346
  } else {
303
347
  return image.linear(a, b);
package/src/operations.h CHANGED
@@ -45,6 +45,11 @@ namespace sharp {
45
45
  */
46
46
  VImage Gamma(VImage image, double const exponent);
47
47
 
48
+ /*
49
+ * Flatten image to remove alpha channel
50
+ */
51
+ VImage Flatten(VImage image, std::vector<double> flattenBackground);
52
+
48
53
  /*
49
54
  * Produce the "negative" of the image.
50
55
  */
@@ -85,12 +90,12 @@ namespace sharp {
85
90
  /*
86
91
  Trim an image
87
92
  */
88
- VImage Trim(VImage image, double const threshold);
93
+ VImage Trim(VImage image, std::vector<double> background, double const threshold);
89
94
 
90
95
  /*
91
96
  * Linear adjustment (a * in + b)
92
97
  */
93
- VImage Linear(VImage image, double const a, double const b);
98
+ VImage Linear(VImage image, std::vector<double> const a, std::vector<double> const b);
94
99
 
95
100
  /*
96
101
  * Recomb with a Matrix of the given bands/channel size.