1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
/* 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 <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <stdio.h>
#include <stdlib.h>
#include <vips/vips.h>
#include "pfreqfilt.h"
typedef struct _VipsFreqmult {
VipsFreqfilt parent_instance;
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 = freqfilt->in;
if( vips_band_format_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_memory();
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, "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 );
}
|