From f8b9645bd893702e8cc22219f641eee377fb750e Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Fri, 3 Jan 2014 23:22:07 +0000 Subject: [PATCH] move im_freqflt() to a class and move im_freq_mask() to deprecated --- ChangeLog | 2 +- TODO | 2 +- libvips/deprecated/Makefile.am | 1 + .../{freqfilt => deprecated}/im_freq_mask.c | 14 -- libvips/deprecated/vips7compat.c | 16 ++ libvips/freqfilt/Makefile.am | 5 +- libvips/freqfilt/freqfilt.c | 2 + libvips/freqfilt/freqmult.c | 179 ++++++++++++++++++ libvips/freqfilt/im_freqflt.c | 137 -------------- libvips/include/vips/freqfilt.h | 3 +- libvips/include/vips/vips7compat.h | 2 + 11 files changed, 206 insertions(+), 157 deletions(-) rename libvips/{freqfilt => deprecated}/im_freq_mask.c (94%) create mode 100644 libvips/freqfilt/freqmult.c delete mode 100644 libvips/freqfilt/im_freqflt.c diff --git a/ChangeLog b/ChangeLog index e77b1083..fbb8f048 100644 --- a/ChangeLog +++ b/ChangeLog @@ -32,7 +32,7 @@ - vipsthumbnail has a --crop option - remove video4linux1 code, it was useless on all modern linuxes - redone freq filter builders as classes -- redone im_fwfft(), im_invfft() as classes +- redone im_fwfft(), im_invfft(), im_freqflt() as classes 20/11/13 started 7.36.5 - better cache sizing in unbuffered sequential mode diff --git a/TODO b/TODO index 02706ba7..28f308c1 100644 --- a/TODO +++ b/TODO @@ -5,7 +5,7 @@ VIPS:ERROR:buffer.c:390:vips_buffer_new: assertion failed: (!buffer->cache) Aborted (core dumped) -- im_freq_mask.c can almost be deprecated .. just im_freqfilt() to do now I +- im_freq_mask.c can almost be deprecated .. just im_freqflt() to do now I think diff --git a/libvips/deprecated/Makefile.am b/libvips/deprecated/Makefile.am index 4e67065e..198d718a 100644 --- a/libvips/deprecated/Makefile.am +++ b/libvips/deprecated/Makefile.am @@ -72,6 +72,7 @@ libdeprecated_la_SOURCES = \ im_vips2tiff.c \ conver_dispatch.c \ im_vips2dz.c \ + im_freq_mask.c \ matlab.c \ radiance.c \ raw.c diff --git a/libvips/freqfilt/im_freq_mask.c b/libvips/deprecated/im_freq_mask.c similarity index 94% rename from libvips/freqfilt/im_freq_mask.c rename to libvips/deprecated/im_freq_mask.c index 802d38d3..a1218b81 100644 --- a/libvips/freqfilt/im_freq_mask.c +++ b/libvips/deprecated/im_freq_mask.c @@ -202,20 +202,6 @@ build_freq_mask( IMAGE *out, int xs, int ys, ImMaskType flag, va_list ap ) return( 0 ); } -/** - * im_flt_image_freq: - * @in: input image - * @out: output image - * @flag: mask type - * @Varargs: mask parameters - * - * Creates a mask (see im_create_fmask()) and filters an image with it (see - * im_freqflt()). - * - * See also: im_create_fmask(), im_freqflt(), - * - * Returns: 0 on success, -1 on error - */ int im_flt_image_freq( IMAGE *in, IMAGE *out, ImMaskType flag, ... ) { diff --git a/libvips/deprecated/vips7compat.c b/libvips/deprecated/vips7compat.c index bdfcca08..24aca5fa 100644 --- a/libvips/deprecated/vips7compat.c +++ b/libvips/deprecated/vips7compat.c @@ -4423,3 +4423,19 @@ im_invfftr( IMAGE *in, IMAGE *out ) return( 0 ); } +int +im_freqflt( IMAGE *in, IMAGE *mask, IMAGE *out ) +{ + VipsImage *x; + + if( vips_freqmult( in, mask, &x, NULL ) ) + return( -1 ); + + if( im_copy( x, out ) ) { + g_object_unref( x ); + return( -1 ); + } + g_object_unref( x ); + + return( 0 ); +} diff --git a/libvips/freqfilt/Makefile.am b/libvips/freqfilt/Makefile.am index c3d75aa8..9ff706ce 100644 --- a/libvips/freqfilt/Makefile.am +++ b/libvips/freqfilt/Makefile.am @@ -5,11 +5,10 @@ libfreqfilt_la_SOURCES = \ pfreqfilt.h \ fwfft.c \ invfft.c \ + freqmult.c \ im_phasecor_fft.c \ freq_dispatch.c \ im_disp_ps.c \ - im_fractsurf.c \ - im_freq_mask.c \ - im_freqflt.c + im_fractsurf.c AM_CPPFLAGS = -I${top_srcdir}/libvips/include @VIPS_CFLAGS@ @VIPS_INCLUDES@ diff --git a/libvips/freqfilt/freqfilt.c b/libvips/freqfilt/freqfilt.c index 03e7c72b..a705d76f 100644 --- a/libvips/freqfilt/freqfilt.c +++ b/libvips/freqfilt/freqfilt.c @@ -159,10 +159,12 @@ vips_freqfilt_operation_init( void ) extern GType vips_fwfft_get_type( void ); extern GType vips_invfft_get_type( void ); #endif /*HAVE_FFTW*/ + extern GType vips_freqmult_get_type( void ); #ifdef HAVE_FFTW vips_fwfft_get_type(); vips_invfft_get_type(); #endif /*HAVE_FFTW*/ + vips_freqmult_get_type(); } diff --git a/libvips/freqfilt/freqmult.c b/libvips/freqfilt/freqmult.c new file mode 100644 index 00000000..07cf610a --- /dev/null +++ b/libvips/freqfilt/freqmult.c @@ -0,0 +1,179 @@ +/* frequency-domain filter an image + * + * Author: Nicos Dessipris + * Written on: 02/05/1990 + * Modified on : 08/03/1991 + * 16/6/93 J.Cupitt + * - im_multiply() called, rather than im_cmultim() + * 27/10/93 JC + * - im_clip2*() called, rather than im_any2*() + * 20/9/95 JC + * - rewritten + * 10/9/98 JC + * - frees memory more quickly + * 4/3/03 JC + * - use im_invfftr() to get real back for speedup + * 3/1/14 + * - redone as a class + */ + +/* + + This file is part of VIPS. + + VIPS is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA + + */ + +/* + + These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk + + */ + +#ifdef HAVE_CONFIG_H +#include +#endif /*HAVE_CONFIG_H*/ +#include + +#include +#include + +#include +#include "pfreqfilt.h" + +typedef struct _VipsFreqmult { + VipsFreqfilt parent_instance; + + VipsImage *in; + VipsImage *mask; +} VipsFreqmult; + +typedef VipsFreqfiltClass VipsFreqmultClass; + +G_DEFINE_TYPE( VipsFreqmult, vips_freqmult, VIPS_TYPE_FREQFILT ); + +static int +vips_freqmult_build( VipsObject *object ) +{ + VipsFreqfilt *freqfilt = VIPS_FREQFILT( object ); + VipsFreqmult *freqmult = (VipsFreqmult *) object; + VipsImage **t = (VipsImage **) vips_object_local_array( object, 5 ); + + VipsImage *in; + + if( VIPS_OBJECT_CLASS( vips_freqmult_parent_class )-> + build( object ) ) + return( -1 ); + + in = freqmult->in; + + if( vips_bandfmt_iscomplex( in->BandFmt ) ) { + if( vips_multiply( in, freqmult->mask, &t[0], NULL ) || + vips_invfft( t[0], &t[1], "real", TRUE, NULL ) ) + return( -1 ); + + in = t[1]; + } + else { + /* Optimisation: output of vips_invfft() is double, we + * will usually cast to char, so rather than keeping a + * large double buffer and partial to char from that, + * cast to a memory buffer and copy to out from that. + * + * FIXME does this actually work now we're a class? test + * perhaps we need a temporary object + */ + t[4] = vips_image_new_buffer(); + + if( vips_fwfft( in, &t[0], NULL ) || + vips_multiply( t[0], freqmult->mask, &t[1], NULL ) || + vips_invfft( t[1], &t[2], "real", TRUE, NULL ) || + vips_cast( t[2], &t[3], in->BandFmt, NULL ) || + vips_image_write( t[3], t[4] ) ) + return( -1 ); + + in = t[4]; + } + + if( vips_image_write( in, freqfilt->out ) ) + return( -1 ); + + return( 0 ); +} + +static void +vips_freqmult_class_init( VipsFreqmultClass *class ) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS( class ); + VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class ); + + gobject_class->set_property = vips_object_set_property; + gobject_class->get_property = vips_object_get_property; + + vobject_class->nickname = "freqmult"; + vobject_class->description = _( "frequency-domain filtering" ); + vobject_class->build = vips_freqmult_build; + + VIPS_ARG_IMAGE( class, "in", -1, + _( "in" ), + _( "Input image" ), + VIPS_ARGUMENT_REQUIRED_INPUT, + G_STRUCT_OFFSET( VipsFreqmult, in ) ); + + VIPS_ARG_IMAGE( class, "mask", 0, + _( "mask" ), + _( "Input mask image" ), + VIPS_ARGUMENT_REQUIRED_INPUT, + G_STRUCT_OFFSET( VipsFreqmult, mask ) ); + +} + +static void +vips_freqmult_init( VipsFreqmult *freqmult ) +{ +} + +/** + * vips_freqmult: + * @in: input image + * @mask: mask image + * @out: output image + * @...: %NULL-terminated list of optional named arguments + * + * Multiply @in by @mask in Fourier space. + * + * @in is transformed to Fourier space, multipled with @mask, then + * transformed back to real space. If @in is already a complex image, just + * multiply then inverse transform. + * + * See also: vips_invfft(), vips_mask_ideal(). + * + * Returns: 0 on success, -1 on error. + */ +int +vips_freqmult( VipsImage *in, VipsImage *mask, VipsImage **out, ... ) +{ + va_list ap; + int result; + + va_start( ap, out ); + result = vips_call_split( "freqmult", ap, in, mask, out ); + va_end( ap ); + + return( result ); +} + diff --git a/libvips/freqfilt/im_freqflt.c b/libvips/freqfilt/im_freqflt.c deleted file mode 100644 index 7954a8ae..00000000 --- a/libvips/freqfilt/im_freqflt.c +++ /dev/null @@ -1,137 +0,0 @@ -/* @(#) Functions which takes as input a valid image and filters it - * @(#) in the fourier domain with the filter mask - * @(#) Input can have any format ; output is the same as input - * @(#) imin can be char uchar, short, ushort, int, uint, float, double - * @(#) or complex float; result is the same as input, clipped if necessary. - * @(#) mask can have any format but the sizes of input and mask are equal - * @(#) The function performs float fft and if the input is not complex float - * @(#) the output is casted to the type of input according to im_clip2..() - * @(#) Since buffer images are involved the size, is restricted to 512x512 - * @(#) for the SUN4 SPARC workstation - * @(#) - * @(#) int im_freqflt(imin, mask, imout) - * @(#) IMAGE *imin, *mask, *imout; - * @(#) - * @(#) Returns 0 on success and -1 on error - * - * Copyright: 1990, N. Dessipris. - * - * Author: Nicos Dessipris - * Written on: 02/05/1990 - * Modified on : 08/03/1991 - * 16/6/93 J.Cupitt - * - im_multiply() called, rather than im_cmultim() - * 27/10/93 JC - * - im_clip2*() called, rather than im_any2*() - * 20/9/95 JC - * - rewritten - * 10/9/98 JC - * - frees memory more quickly - * 4/3/03 JC - * - use im_invfftr() to get real back for speedup - */ - -/* - - This file is part of VIPS. - - VIPS is free software; you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301 USA - - */ - -/* - - These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk - - */ - -#ifdef HAVE_CONFIG_H -#include -#endif /*HAVE_CONFIG_H*/ -#include - -#include -#include - -#include - -/** - * im_freqflt: - * @in: input image - * @mask: mask image - * @out: output image - * - * Filter an image in Fourier space. - * - * @in is transformed to Fourier space, multipled with the mask image, then - * transformed back to real space. If @in is already a complex image, just - * multiply then inverse transform. - * - * See also: im_invfftr(), im_create_fmask(). - * - * Returns: 0 on success, -1 on error. - */ -int -im_freqflt( IMAGE *in, IMAGE *mask, IMAGE *out ) -{ - IMAGE *dummy; - - /* Placeholder for memory free. - */ - if( !(dummy = im_open( "memory-1", "p" )) ) - return( -1 ); - - if( vips_bandfmt_iscomplex( in->BandFmt ) ) { - /* Easy case! Assume it has already been transformed. - */ - IMAGE *t1 = im_open_local( dummy, "im_freqflt-1", "p" ); - - if( !t1 || - im_multiply( in, mask, t1 ) || - im_invfftr( t1, out ) ) { - im_close( dummy ); - return( -1 ); - } - } - else { - /* Harder - fft first, then mult, then force back to start - * type. - * - * Optimisation: output of im_invfft() is float buffer, we - * will usually chagetype to char, so rather than keeping a - * large float buffer and partial to char from that, do - * changetype to a memory buffer, and copy to out from that. - */ - IMAGE *t[3]; - IMAGE *t3; - - if( im_open_local_array( dummy, t, 3, "im_freqflt-1", "p" ) || - !(t3 = im_open_local( out, "im_freqflt-3", "t" )) || - im_fwfft( in, t[0] ) || - im_multiply( t[0], mask, t[1] ) || - im_invfftr( t[1], t[2] ) || - im_clip2fmt( t[2], t3, in->BandFmt ) || - im_copy( t3, out ) ) { - im_close( dummy ); - return( -1 ); - } - } - - im_close( dummy ); - - return( 0 ); -} - diff --git a/libvips/include/vips/freqfilt.h b/libvips/include/vips/freqfilt.h index 101e5bf8..242f68ec 100644 --- a/libvips/include/vips/freqfilt.h +++ b/libvips/include/vips/freqfilt.h @@ -42,8 +42,9 @@ int vips_fwfft( VipsImage *in, VipsImage **out, ... ) __attribute__((sentinel)); int vips_invfft( VipsImage *in, VipsImage **out, ... ) __attribute__((sentinel)); +int vips_freqmult( VipsImage *in, VipsImage *mask, VipsImage **out, ... ) + __attribute__((sentinel)); -int im_freqflt( VipsImage *in, VipsImage *mask, VipsImage *out ); int im_disp_ps( VipsImage *in, VipsImage *out ); int im_phasecor_fft( VipsImage *in1, VipsImage *in2, VipsImage *out ); diff --git a/libvips/include/vips/vips7compat.h b/libvips/include/vips/vips7compat.h index 1f2c1a27..a34a70ae 100644 --- a/libvips/include/vips/vips7compat.h +++ b/libvips/include/vips/vips7compat.h @@ -970,6 +970,8 @@ int im_fwfft( VipsImage *in, VipsImage *out ); int im_invfft( VipsImage *in, VipsImage *out ); int im_invfftr( VipsImage *in, VipsImage *out ); +int im_freqflt( VipsImage *in, VipsImage *mask, VipsImage *out ); + #ifdef __cplusplus } #endif /*__cplusplus*/