1 // This is tbl/vepl/vepl_monadic.cxx
2 #include <iostream>
3 #include <cmath>
4 #include "vepl_monadic.h"
5 #include <vepl/accessors/vipl_accessors_vil_image_view_base.h>
6 #include <vipl/vipl_monadic.h>
7 #include "vil/vil_image_view.h"
8 #include "vil/vil_new.h"
9 #ifdef _MSC_VER
10 # include "vcl_msvc_warnings.h"
11 #endif
12 #include "vxl_config.h" // for vxl_byte
13
abs_byte(vxl_sbyte const & a)14 vxl_sbyte abs_byte(vxl_sbyte const& a) { return (a<0) ? -a : a; }
abs_short(vxl_int_16 const & a)15 vxl_int_16 abs_short(vxl_int_16 const& a) { return (a<0) ? -a : a; }
abs_int(vxl_int_32 const & a)16 vxl_int_32 abs_int(vxl_int_32 const& a) { return (a<0) ? -a : a; }
abs_float(float const & a)17 float abs_float(float const& a) { return (a<0) ? -a : a; }
abs_double(double const & a)18 double abs_double(double const& a) { return std::fabs(a); }
sqrt_float(float const & a)19 float sqrt_float(float const& a) { return (a<0) ? -std::sqrt(-a) : std::sqrt(a); }
sqrt_double(double const & a)20 double sqrt_double(double const& a) { return (a<0) ? -std::sqrt(-a) : std::sqrt(a); }
sqr_ubyte(vxl_byte const & a)21 vxl_byte sqr_ubyte(vxl_byte const& a) { return vxl_byte(a*a); }
sqr_float(float const & a)22 float sqr_float(float const& a) { return a*a; }
sqr_short(vxl_uint_16 const & a)23 vxl_uint_16 sqr_short(vxl_uint_16 const& a) { vxl_uint_32 b = a; return vxl_uint_16(b*b); }
sqr_int(vxl_uint_32 const & a)24 vxl_uint_32 sqr_int(vxl_uint_32 const& a) { return a*a; }
sqr_double(double const & a)25 double sqr_double(double const& a) { return a*a; }
26 static double shift_=0.0, scale_=1.0;
shear_ubyte(vxl_byte const & a)27 vxl_byte shear_ubyte(vxl_byte const& a) { return vxl_byte((a+shift_)*scale_); }
shear_short(vxl_uint_16 const & a)28 vxl_uint_16 shear_short(vxl_uint_16 const& a) { return vxl_uint_16((a+shift_)*scale_); }
shear_int(vxl_uint_32 const & a)29 vxl_uint_32 shear_int(vxl_uint_32 const& a) { return vxl_uint_32((a+shift_)*scale_); }
shear_float(float const & a)30 float shear_float(float const& a) { return float((a+shift_)*scale_); }
shear_double(double const & a)31 double shear_double(double const& a) { return (a+shift_)*scale_; }
32
33
vepl_monadic_abs(const vil_image_resource_sptr & image)34 vil_image_resource_sptr vepl_monadic_abs(const vil_image_resource_sptr& image)
35 {
36 vil_image_resource_sptr img_out = vil_new_image_resource(image->ni(), image->nj(), image->nplanes(), image->pixel_format());
37
38 // first the unsigned types
39 //
40 // byte rgb
41 if (image->pixel_format() == VIL_PIXEL_FORMAT_RGB_BYTE) {
42 // vxl_byte is unsigned so nothing happens
43 img_out->put_view(*(image->get_view()));
44 }
45
46 // byte greyscale
47 else if (image->pixel_format() == VIL_PIXEL_FORMAT_BYTE) {
48 // vxl_byte is unsigned so nothing happens
49 img_out->put_view(*(image->get_view()));
50 }
51
52 // short
53 else if (image->pixel_format() == VIL_PIXEL_FORMAT_UINT_16) {
54 // vxl_uint_16 is unsigned so nothing happens
55 img_out->put_view(*(image->get_view()));
56 }
57
58 // int
59 else if (image->pixel_format() == VIL_PIXEL_FORMAT_UINT_32) {
60 // vxl_uint_32 is unsigned so nothing happens
61 img_out->put_view(*(image->get_view()));
62 }
63
64 // then the signed types
65 //
66 // byte greyscale
67 else if (image->pixel_format() == VIL_PIXEL_FORMAT_SBYTE) {
68 vil_image_view<vxl_sbyte> in = image->get_view();
69 vil_image_view<vxl_sbyte> out = image->get_copy_view();
70 vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_sbyte,vxl_sbyte> op(abs_byte);
71 op.put_in_data_ptr(&in);
72 op.put_out_data_ptr(&out);
73 op.filter();
74 img_out->put_view(out);
75 }
76
77 // short int greyscale
78 else if (image->pixel_format() == VIL_PIXEL_FORMAT_INT_16) {
79 vil_image_view<vxl_int_16> in = image->get_view();
80 vil_image_view<vxl_int_16> out = image->get_copy_view();
81 vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_int_16,vxl_int_16> op(abs_short);
82 op.put_in_data_ptr(&in);
83 op.put_out_data_ptr(&out);
84 op.filter();
85 img_out->put_view(out);
86 }
87
88 // int greyscale
89 else if (image->pixel_format() == VIL_PIXEL_FORMAT_INT_32) {
90 vil_image_view<vxl_int_32> in = image->get_view();
91 vil_image_view<vxl_int_32> out = image->get_copy_view();
92 vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_int_32,vxl_int_32> op(abs_int);
93 op.put_in_data_ptr(&in);
94 op.put_out_data_ptr(&out);
95 op.filter();
96 img_out->put_view(out);
97 }
98
99 // and finally the "floating point" types
100 //
101 // float
102 else if (image->pixel_format() == VIL_PIXEL_FORMAT_FLOAT) {
103 vil_image_view<float> in = image->get_view();
104 vil_image_view<float> out = image->get_copy_view();
105 vipl_monadic<vil_image_view_base,vil_image_view_base,float,float> op(abs_float);
106 op.put_in_data_ptr(&in);
107 op.put_out_data_ptr(&out);
108 op.filter();
109 img_out->put_view(out);
110 }
111
112 // double
113 else if (image->pixel_format() == VIL_PIXEL_FORMAT_DOUBLE) {
114 vil_image_view<double> in = image->get_view();
115 vil_image_view<double> out = image->get_copy_view();
116 vipl_monadic<vil_image_view_base,vil_image_view_base,double,double> op(abs_double);
117 op.put_in_data_ptr(&in);
118 op.put_out_data_ptr(&out);
119 op.filter();
120 img_out->put_view(out);
121 }
122
123 //
124 else
125 std::cerr << __FILE__ ": vepl_monadic_abs() not implemented for " << image << '\n';
126
127 return img_out;
128 }
129
vepl_monadic_sqrt(const vil_image_resource_sptr & image)130 vil_image_resource_sptr vepl_monadic_sqrt(const vil_image_resource_sptr& image)
131 {
132 vil_image_resource_sptr img_out = vil_new_image_resource(image->ni(), image->nj(), image->nplanes(), image->pixel_format());
133
134 // float
135 if (image->pixel_format() == VIL_PIXEL_FORMAT_FLOAT) {
136 vil_image_view<float> in = image->get_view();
137 vil_image_view<float> out = image->get_copy_view();
138 vipl_monadic<vil_image_view_base,vil_image_view_base,float,float> op(sqrt_float);
139 op.put_in_data_ptr(&in);
140 op.put_out_data_ptr(&out);
141 op.filter();
142 img_out->put_view(out);
143 }
144
145 // double
146 else if (image->pixel_format() == VIL_PIXEL_FORMAT_DOUBLE) {
147 vil_image_view<double> in = image->get_view();
148 vil_image_view<double> out = image->get_copy_view();
149 vipl_monadic<vil_image_view_base,vil_image_view_base,double,double> op(sqrt_double);
150 op.put_in_data_ptr(&in);
151 op.put_out_data_ptr(&out);
152 op.filter();
153 img_out->put_view(out);
154 }
155
156 //
157 else
158 std::cerr << __FILE__ ": vepl_monadic_sqrt() not implemented for " << image << '\n';
159
160 return img_out;
161 }
162
vepl_monadic_sqr(const vil_image_resource_sptr & image)163 vil_image_resource_sptr vepl_monadic_sqr(const vil_image_resource_sptr& image)
164 {
165 vil_image_resource_sptr img_out = vil_new_image_resource(image->ni(), image->nj(), image->nplanes(), image->pixel_format());
166
167 // byte greyscale
168 if (image->pixel_format() == VIL_PIXEL_FORMAT_BYTE) {
169 vil_image_view<vxl_byte> in = image->get_view();
170 vil_image_view<vxl_byte> out = image->get_copy_view();
171 vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_byte,vxl_byte> op(sqr_ubyte);
172 op.put_in_data_ptr(&in);
173 op.put_out_data_ptr(&out);
174 op.filter();
175 img_out->put_view(out);
176 }
177
178 // short
179 else if (image->pixel_format() == VIL_PIXEL_FORMAT_UINT_16) {
180 vil_image_view<vxl_uint_16> in = image->get_view();
181 vil_image_view<vxl_uint_16> out = image->get_copy_view();
182 vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_uint_16,vxl_uint_16> op(sqr_short);
183 op.put_in_data_ptr(&in);
184 op.put_out_data_ptr(&out);
185 op.filter();
186 img_out->put_view(out);
187 }
188
189 // int
190 else if (image->pixel_format() == VIL_PIXEL_FORMAT_UINT_32) {
191 vil_image_view<vxl_uint_32> in = image->get_view();
192 vil_image_view<vxl_uint_32> out = image->get_copy_view();
193 vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_uint_32,vxl_uint_32> op(sqr_int);
194 op.put_in_data_ptr(&in);
195 op.put_out_data_ptr(&out);
196 op.filter();
197 img_out->put_view(out);
198 }
199
200 // float
201 else if (image->pixel_format() == VIL_PIXEL_FORMAT_FLOAT) {
202 vil_image_view<float> in = image->get_view();
203 vil_image_view<float> out = image->get_copy_view();
204 vipl_monadic<vil_image_view_base,vil_image_view_base,float,float> op(sqr_float);
205 op.put_in_data_ptr(&in);
206 op.put_out_data_ptr(&out);
207 op.filter();
208 img_out->put_view(out);
209 }
210
211 // double
212 else if (image->pixel_format() == VIL_PIXEL_FORMAT_DOUBLE) {
213 vil_image_view<double> in = image->get_view();
214 vil_image_view<double> out = image->get_copy_view();
215 vipl_monadic<vil_image_view_base,vil_image_view_base,double,double> op(sqr_double);
216 op.put_in_data_ptr(&in);
217 op.put_out_data_ptr(&out);
218 op.filter();
219 img_out->put_view(out);
220 }
221
222 //
223 else
224 std::cerr << __FILE__ ": vepl_monadic_sqr() not implemented for " << image << '\n';
225
226 return img_out;
227 }
228
vepl_monadic_shear(const vil_image_resource_sptr & image,double shift,double scale)229 vil_image_resource_sptr vepl_monadic_shear(const vil_image_resource_sptr& image, double shift, double scale)
230 {
231 shift_ = shift; scale_ = scale;
232 vil_image_resource_sptr img_out = vil_new_image_resource(image->ni(), image->nj(), image->nplanes(), image->pixel_format());
233
234 // byte greyscale
235 if (image->pixel_format() == VIL_PIXEL_FORMAT_BYTE) {
236 vil_image_view<vxl_byte> in = image->get_view();
237 vil_image_view<vxl_byte> out = image->get_copy_view();
238 vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_byte,vxl_byte> op(shear_ubyte);
239 op.put_in_data_ptr(&in);
240 op.put_out_data_ptr(&out);
241 op.filter();
242 img_out->put_view(out);
243 }
244
245 // short
246 else if (image->pixel_format() == VIL_PIXEL_FORMAT_UINT_16) {
247 vil_image_view<vxl_uint_16> in = image->get_view();
248 vil_image_view<vxl_uint_16> out = image->get_copy_view();
249 vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_uint_16,vxl_uint_16> op(shear_short);
250 op.put_in_data_ptr(&in);
251 op.put_out_data_ptr(&out);
252 op.filter();
253 img_out->put_view(out);
254 }
255
256 // int
257 else if (image->pixel_format() == VIL_PIXEL_FORMAT_UINT_32) {
258 vil_image_view<vxl_uint_32> in = image->get_view();
259 vil_image_view<vxl_uint_32> out = image->get_copy_view();
260 vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_uint_32,vxl_uint_32> op(shear_int);
261 op.put_in_data_ptr(&in);
262 op.put_out_data_ptr(&out);
263 op.filter();
264 img_out->put_view(out);
265 }
266
267 // float
268 else if (image->pixel_format() == VIL_PIXEL_FORMAT_FLOAT) {
269 vil_image_view<float> in = image->get_view();
270 vil_image_view<float> out = image->get_copy_view();
271 vipl_monadic<vil_image_view_base,vil_image_view_base,float,float> op(shear_float);
272 op.put_in_data_ptr(&in);
273 op.put_out_data_ptr(&out);
274 op.filter();
275 img_out->put_view(out);
276 }
277
278 // double
279 else if (image->pixel_format() == VIL_PIXEL_FORMAT_DOUBLE) {
280 vil_image_view<double> in = image->get_view();
281 vil_image_view<double> out = image->get_copy_view();
282 vipl_monadic<vil_image_view_base,vil_image_view_base,double,double> op(shear_double);
283 op.put_in_data_ptr(&in);
284 op.put_out_data_ptr(&out);
285 op.filter();
286 img_out->put_view(out);
287 }
288
289 //
290 else
291 std::cerr << __FILE__ ": vepl_monadic_shear() not implemented for " << image << '\n';
292
293 return img_out;
294 }
295