fix a memleak in vips__correl

and some more tests
This commit is contained in:
John Cupitt 2021-02-06 15:42:31 +00:00
parent 6782769d8c
commit 679665b403
2 changed files with 43 additions and 9 deletions

View File

@ -97,7 +97,7 @@ vips__correl( VipsImage *ref, VipsImage *sec,
{ {
VipsImage *surface = vips_image_new(); VipsImage *surface = vips_image_new();
VipsImage **t = (VipsImage **) VipsImage **t = (VipsImage **)
vips_object_local_array( VIPS_OBJECT( surface ), 4 ); vips_object_local_array( VIPS_OBJECT( surface ), 5 );
VipsRect refr, secr; VipsRect refr, secr;
VipsRect winr, srhr; VipsRect winr, srhr;
@ -137,35 +137,37 @@ vips__correl( VipsImage *ref, VipsImage *sec,
g_object_unref( surface ); g_object_unref( surface );
return( -1 ); return( -1 );
} }
ref = t[0];
sec = t[1];
/* Make sure we have just one band. From vips_*mosaic() we will, but /* Make sure we have just one band. From vips_*mosaic() we will, but
* from vips_match() etc. we may not. * from vips_match() etc. we may not.
*/ */
if( t[0]->Bands != 1 ) { if( ref->Bands != 1 ) {
if( vips_extract_band( t[0], &t[2], 0, NULL ) ) { if( vips_extract_band( ref, &t[2], 0, NULL ) ) {
g_object_unref( surface ); g_object_unref( surface );
return( -1 ); return( -1 );
} }
t[0] = t[2]; ref = t[2];
} }
if( t[1]->Bands != 1 ) { if( sec->Bands != 1 ) {
if( vips_extract_band( t[1], &t[3], 0, NULL ) ) { if( vips_extract_band( sec, &t[3], 0, NULL ) ) {
g_object_unref( surface ); g_object_unref( surface );
return( -1 ); return( -1 );
} }
t[1] = t[3]; sec = t[3];
} }
/* Search! /* Search!
*/ */
if( vips_spcor( t[1], t[0], &surface, NULL ) ) { if( vips_spcor( sec, ref, &t[4], NULL ) ) {
g_object_unref( surface ); g_object_unref( surface );
return( -1 ); return( -1 );
} }
/* Find maximum of correlation surface. /* Find maximum of correlation surface.
*/ */
if( vips_max( surface, correlation, "x", x, "y", y, NULL ) ) { if( vips_max( t[4], correlation, "x", x, "y", y, NULL ) ) {
g_object_unref( surface ); g_object_unref( surface );
return( -1 ); return( -1 );
} }

View File

@ -47,6 +47,38 @@ class TestMosaicing:
mosaiced_image = None mosaiced_image = None
for i in range(0, len(MOSAIC_FILES), 2):
files = MOSAIC_FILES[i:i + 2]
marks = MOSAIC_MARKS[i:i + 2]
im = pyvips.Image.new_from_file(files[0])
sec_im = pyvips.Image.new_from_file(files[1])
horizontal_part = im.mosaic(sec_im,
pyvips.Direction.HORIZONTAL,
marks[0][0], marks[0][1],
marks[1][0], marks[1][1])
if mosaiced_image is None:
mosaiced_image = horizontal_part
else:
vertical_marks = MOSAIC_VERTICAL_MARKS[i - 2:i]
mosaiced_image = mosaiced_image.mosaic(horizontal_part,
pyvips.Direction.VERTICAL,
vertical_marks[1][0], vertical_marks[1][1],
vertical_marks[0][0], vertical_marks[0][1])
# Uncomment to see output file
#mosaiced_image.write_to_file('after.jpg')
# hard to test much more than this
assert mosaiced_image.width == 1005
assert mosaiced_image.height == 1295
assert mosaiced_image.interpretation == pyvips.Interpretation.B_W
assert mosaiced_image.bands == 1
def test_globalbalance(self):
mosaiced_image = None
for i in range(0, len(MOSAIC_FILES), 2): for i in range(0, len(MOSAIC_FILES), 2):
files = MOSAIC_FILES[i:i + 2] files = MOSAIC_FILES[i:i + 2]
marks = MOSAIC_MARKS[i:i + 2] marks = MOSAIC_MARKS[i:i + 2]