1 /* creates an butterworth filter.
2  *
3  * 02/01/14
4  * 	- from butterworth.c
5  */
6 
7 /*
8 
9     This file is part of VIPS.
10 
11     VIPS is free software; you can redistribute it and/or modify
12     it under the terms of the GNU Lesser General Public License as published by
13     the Free Software Foundation; either version 2 of the License, or
14     (at your option) any later version.
15 
16     This program is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     GNU Lesser General Public License for more details.
20 
21     You should have received a copy of the GNU Lesser General Public License
22     along with this program; if not, write to the Free Software
23     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24     02110-1301  USA
25 
26  */
27 
28 /*
29 
30     These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
31 
32  */
33 
34 /*
35 #define VIPS_DEBUG
36  */
37 
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif /*HAVE_CONFIG_H*/
41 #include <vips/intl.h>
42 
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <math.h>
47 
48 #include <vips/vips.h>
49 
50 #include "pcreate.h"
51 #include "point.h"
52 #include "pmask.h"
53 
54 typedef struct _VipsMaskButterworthRing {
55 	VipsMaskButterworth parent_instance;
56 
57 	double ringwidth;
58 
59 } VipsMaskButterworthRing;
60 
61 typedef VipsMaskButterworthClass VipsMaskButterworthRingClass;
62 
63 G_DEFINE_TYPE( VipsMaskButterworthRing, vips_mask_butterworth_ring,
64 	VIPS_TYPE_MASK_BUTTERWORTH );
65 
66 static double
vips_mask_butterworth_ring_point(VipsMask * mask,double dx,double dy)67 vips_mask_butterworth_ring_point( VipsMask *mask,
68 	double dx, double dy )
69 {
70 	VipsMaskButterworth *butterworth =
71 		(VipsMaskButterworth *) mask;
72 	VipsMaskButterworthRing *butterworth_ring =
73 		(VipsMaskButterworthRing *) mask;
74 
75 	double order = butterworth->order;
76 	double fc = butterworth->frequency_cutoff;
77 	double ac = butterworth->amplitude_cutoff;
78 	double ringwidth = butterworth_ring->ringwidth;
79 
80 	double df = ringwidth / 2.0;
81 	double cnst = (1.0 / ac) - 1.0;
82 	double df2 = df * df;
83 	double dist = sqrt( dx * dx + dy * dy );
84 
85 	return( 1.0 / (1.0 + cnst * pow(
86 		(dist - fc) * (dist - fc) / df2, order )) );
87 }
88 
89 static void
vips_mask_butterworth_ring_class_init(VipsMaskButterworthRingClass * class)90 vips_mask_butterworth_ring_class_init(
91 	VipsMaskButterworthRingClass *class )
92 {
93 	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
94 	VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
95 	VipsMaskClass *mask_class = VIPS_MASK_CLASS( class );
96 
97 	gobject_class->set_property = vips_object_set_property;
98 	gobject_class->get_property = vips_object_get_property;
99 
100 	vobject_class->nickname = "mask_butterworth_ring";
101 	vobject_class->description = _( "make a butterworth ring filter" );
102 
103 	mask_class->point = vips_mask_butterworth_ring_point;
104 
105 	VIPS_ARG_DOUBLE( class, "ringwidth", 20,
106 		_( "Ringwidth" ),
107 		_( "Ringwidth" ),
108 		VIPS_ARGUMENT_REQUIRED_INPUT,
109 		G_STRUCT_OFFSET( VipsMaskButterworthRing, ringwidth ),
110 		0.0, 1000000.0, 0.1 );
111 
112 }
113 
114 static void
vips_mask_butterworth_ring_init(VipsMaskButterworthRing * butterworth_ring)115 vips_mask_butterworth_ring_init(
116 	VipsMaskButterworthRing *butterworth_ring )
117 {
118 	butterworth_ring->ringwidth = 0.1;
119 }
120 
121 /**
122  * vips_mask_butterworth_ring:
123  * @out: (out): output image
124  * @width: image size
125  * @height: image size
126  * @order: filter order
127  * @frequency_cutoff: frequency threshold
128  * @amplitude_cutoff: amplitude threshold
129  * @ringwidth: ringwidth
130  * @...: %NULL-terminated list of optional named arguments
131  *
132  * Optional arguments:
133  *
134  * * @nodc: don't set the DC pixel
135  * * @reject: invert the filter sense
136  * * @optical: coordinates in optical space
137  * * @uchar: output a uchar image
138  *
139  * Make a butterworth ring-pass or ring-reject filter, that is, one with a
140  * variable,
141  * smooth transition
142  * positioned at @frequency_cutoff of width @width, where @frequency_cutoff is
143  * in the range 0 - 1. The shape of the curve is controlled by
144  * @order --- higher values give a sharper transition. See Gonzalez and Wintz,
145  * Digital Image Processing, 1987.
146  *
147  * See also: vips_mask_ideal().
148  *
149  * Returns: 0 on success, -1 on error
150  */
151 int
vips_mask_butterworth_ring(VipsImage ** out,int width,int height,double order,double frequency_cutoff,double amplitude_cutoff,double ringwidth,...)152 vips_mask_butterworth_ring( VipsImage **out, int width, int height,
153 	double order, double frequency_cutoff, double amplitude_cutoff,
154 	double ringwidth, ... )
155 {
156 	va_list ap;
157 	int result;
158 
159 	va_start( ap, ringwidth );
160 	result = vips_call_split( "mask_butterworth_ring", ap,
161 		out, width, height,
162 		order, frequency_cutoff, amplitude_cutoff, ringwidth );
163 	va_end( ap );
164 
165 	return( result );
166 }
167