1 /* creates an ideal filter.
2 *
3 * 02/01/14
4 * - from ideal.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 _VipsMaskIdealBand {
55 VipsMask parent_instance;
56
57 double frequency_cutoff_x;
58 double frequency_cutoff_y;
59 double radius;
60
61 } VipsMaskIdealBand;
62
63 typedef VipsMaskClass VipsMaskIdealBandClass;
64
65 G_DEFINE_TYPE( VipsMaskIdealBand, vips_mask_ideal_band,
66 VIPS_TYPE_MASK );
67
68 static double
vips_mask_ideal_band_point(VipsMask * mask,double dx,double dy)69 vips_mask_ideal_band_point( VipsMask *mask, double dx, double dy )
70 {
71 VipsMaskIdealBand *ideal_band = (VipsMaskIdealBand *) mask;
72 double fcx = ideal_band->frequency_cutoff_x;
73 double fcy = ideal_band->frequency_cutoff_y;
74 double r2 = ideal_band->radius * ideal_band->radius;
75
76 double d1 = (dx - fcx) * (dx - fcx) + (dy - fcy) * (dy - fcy);
77 double d2 = (dx + fcx) * (dx + fcx) + (dy + fcy) * (dy + fcy);
78
79 return( (d1 < r2 || d2 < r2) ? 1.0 : 0.0 );
80 }
81
82 static void
vips_mask_ideal_band_class_init(VipsMaskIdealBandClass * class)83 vips_mask_ideal_band_class_init( VipsMaskIdealBandClass *class )
84 {
85 GObjectClass *gobject_class = G_OBJECT_CLASS( class );
86 VipsObjectClass *vobject_class = VIPS_OBJECT_CLASS( class );
87 VipsMaskClass *mask_class = VIPS_MASK_CLASS( class );
88
89 gobject_class->set_property = vips_object_set_property;
90 gobject_class->get_property = vips_object_get_property;
91
92 vobject_class->nickname = "mask_ideal_band";
93 vobject_class->description = _( "make an ideal band filter" );
94
95 mask_class->point = vips_mask_ideal_band_point;
96
97 VIPS_ARG_DOUBLE( class, "frequency_cutoff_x", 6,
98 _( "Frequency cutoff x" ),
99 _( "Frequency cutoff x" ),
100 VIPS_ARGUMENT_REQUIRED_INPUT,
101 G_STRUCT_OFFSET( VipsMaskIdealBand, frequency_cutoff_x ),
102 0.0, 1000000.0, 0.5 );
103
104 VIPS_ARG_DOUBLE( class, "frequency_cutoff_y", 7,
105 _( "Frequency cutoff y" ),
106 _( "Frequency cutoff y" ),
107 VIPS_ARGUMENT_REQUIRED_INPUT,
108 G_STRUCT_OFFSET( VipsMaskIdealBand, frequency_cutoff_y ),
109 0.0, 1000000.0, 0.5 );
110
111 VIPS_ARG_DOUBLE( class, "radius", 8,
112 _( "radius" ),
113 _( "radius of circle" ),
114 VIPS_ARGUMENT_REQUIRED_INPUT,
115 G_STRUCT_OFFSET( VipsMaskIdealBand, radius ),
116 0.0, 1000000.0, 0.1 );
117
118 }
119
120 static void
vips_mask_ideal_band_init(VipsMaskIdealBand * ideal_band)121 vips_mask_ideal_band_init( VipsMaskIdealBand *ideal_band )
122 {
123 ideal_band->frequency_cutoff_x = 0.5;
124 ideal_band->frequency_cutoff_y = 0.5;
125 ideal_band->radius = 0.1;
126 }
127
128 /**
129 * vips_mask_ideal_band:
130 * @out: (out): output image
131 * @width: image size
132 * @height: image size
133 * @frequency_cutoff_x: position of band
134 * @frequency_cutoff_y: position of band
135 * @radius: size of band
136 * @...: %NULL-terminated list of optional named arguments
137 *
138 * Optional arguments:
139 *
140 * * @nodc: don't set the DC pixel
141 * * @reject: invert the filter sense
142 * * @optical: coordinates in optical space
143 * * @uchar: output a uchar image
144 *
145 * Make an ideal band-pass or band-reject filter, that is, one with a
146 * sharp cutoff around the point @frequency_cutoff_x, @frequency_cutoff_y,
147 * of size @radius.
148 *
149 * See also: vips_mask_ideal().
150 *
151 * Returns: 0 on success, -1 on error
152 */
153 int
vips_mask_ideal_band(VipsImage ** out,int width,int height,double frequency_cutoff_x,double frequency_cutoff_y,double radius,...)154 vips_mask_ideal_band( VipsImage **out, int width, int height,
155 double frequency_cutoff_x, double frequency_cutoff_y,
156 double radius, ... )
157 {
158 va_list ap;
159 int result;
160
161 va_start( ap, radius );
162 result = vips_call_split( "mask_ideal_band", ap, out, width, height,
163 frequency_cutoff_x, frequency_cutoff_y, radius );
164 va_end( ap );
165
166 return( result );
167 }
168