find_trim works for all bg images

width/height == 0 if whole image is bg
This commit is contained in:
John Cupitt 2017-07-26 15:13:12 +01:00
parent 254e6a8b2a
commit 18595ca0dd
2 changed files with 7 additions and 6 deletions

View File

@ -147,8 +147,8 @@ vips_find_trim_build( VipsObject *object )
g_object_set( find_trim, g_object_set( find_trim,
"left", (int) left, "left", (int) left,
"top", (int) top, "top", (int) top,
"width", (int) ((t[5]->Xsize - right) - left), "width", (int) VIPS_MAX( 0, (t[5]->Xsize - right) - left ),
"height", (int) ((t[6]->Ysize - bottom) - top), "height", (int) VIPS_MAX( 0, (t[6]->Ysize - bottom) - top ),
NULL ); NULL );
return( 0 ); return( 0 );
@ -208,14 +208,14 @@ vips_find_trim_class_init( VipsFindTrimClass *class )
_( "Width of extract area" ), _( "Width of extract area" ),
VIPS_ARGUMENT_REQUIRED_OUTPUT, VIPS_ARGUMENT_REQUIRED_OUTPUT,
G_STRUCT_OFFSET( VipsFindTrim, width ), G_STRUCT_OFFSET( VipsFindTrim, width ),
1, VIPS_MAX_COORD, 1 ); 0, VIPS_MAX_COORD, 1 );
VIPS_ARG_INT( class, "height", 13, VIPS_ARG_INT( class, "height", 13,
_( "Height" ), _( "Height" ),
_( "Height of extract area" ), _( "Height of extract area" ),
VIPS_ARGUMENT_REQUIRED_OUTPUT, VIPS_ARGUMENT_REQUIRED_OUTPUT,
G_STRUCT_OFFSET( VipsFindTrim, height ), G_STRUCT_OFFSET( VipsFindTrim, height ),
1, VIPS_MAX_COORD, 1 ); 0, VIPS_MAX_COORD, 1 );
} }
@ -249,6 +249,9 @@ vips_find_trim_init( VipsFindTrim *find_trim )
* four directions where the sum is greater than @threshold gives the bounding * four directions where the sum is greater than @threshold gives the bounding
* box. * box.
* *
* If the image is entirely background, vips_find_trim() returns @width == 0
* and @height == 0.
*
* @background defaults to 255, or 65535 for 16-bit images. Set another value, * @background defaults to 255, or 65535 for 16-bit images. Set another value,
* or use vips_getpoint() to pick a value from an edge. * or use vips_getpoint() to pick a value from an edge.
* *

View File

@ -645,8 +645,6 @@ class TestArithmetic(unittest.TestCase):
im = Vips.Image.black(50, 60) + 100 im = Vips.Image.black(50, 60) + 100
test = im.embed(10, 20, 200, 300, extend = "white") test = im.embed(10, 20, 200, 300, extend = "white")
test.write_to_file("x.v")
for x in unsigned_formats + float_formats: for x in unsigned_formats + float_formats:
a = test.cast(x) a = test.cast(x)
left, top, width, height = a.find_trim() left, top, width, height = a.find_trim()