contrib/tbl/vepl/vepl_monadic.cxx
Go to the documentation of this file.
00001 // This is tbl/vepl/vepl_monadic.cxx
00002 #include "vepl_monadic.h"
00003 #include <vepl/accessors/vipl_accessors_vil_image_view_base.h>
00004 #include <vipl/vipl_monadic.h>
00005 #include <vil/vil_image_view.h>
00006 #include <vil/vil_new.h>
00007 #include <vcl_cmath.h>
00008 #include <vcl_iostream.h>
00009 #include <vxl_config.h> // for vxl_byte
00010 
00011 vxl_sbyte abs_byte(vxl_sbyte const& a) { return (a<0) ? -a : a; }
00012 vxl_int_16 abs_short(vxl_int_16 const& a) { return (a<0) ? -a : a; }
00013 vxl_int_32 abs_int(vxl_int_32 const& a) { return (a<0) ? -a : a; }
00014 float abs_float(float const& a) { return (a<0) ? -a : a; }
00015 double abs_double(double const& a) { return vcl_fabs(a); }
00016 float sqrt_float(float const& a) { return (a<0) ? -vcl_sqrt(-a) : vcl_sqrt(a); }
00017 double sqrt_double(double const& a) { return (a<0) ? -vcl_sqrt(-a) : vcl_sqrt(a); }
00018 vxl_byte sqr_ubyte(vxl_byte const& a) { return vxl_byte(a*a); }
00019 float sqr_float(float const& a) { return a*a; }
00020 vxl_uint_16 sqr_short(vxl_uint_16 const& a) { vxl_uint_32 b = a; return vxl_uint_16(b*b); }
00021 vxl_uint_32 sqr_int(vxl_uint_32 const& a) { return a*a; }
00022 double sqr_double(double const& a) { return a*a; }
00023 static double shift_=0.0, scale_=1.0;
00024 vxl_byte shear_ubyte(vxl_byte const& a) { return vxl_byte((a+shift_)*scale_); }
00025 vxl_uint_16 shear_short(vxl_uint_16 const& a) { return vxl_uint_16((a+shift_)*scale_); }
00026 vxl_uint_32 shear_int(vxl_uint_32 const& a) { return vxl_uint_32((a+shift_)*scale_); }
00027 float shear_float(float const& a) { return float((a+shift_)*scale_); }
00028 double shear_double(double const& a) { return (a+shift_)*scale_; }
00029 
00030 
00031 vil_image_resource_sptr vepl_monadic_abs(vil_image_resource_sptr image)
00032 {
00033   vil_image_resource_sptr img_out = vil_new_image_resource(image->ni(), image->nj(), image->nplanes(), image->pixel_format());
00034 
00035   // first the unsigned types
00036   //
00037   // byte rgb
00038   if (image->pixel_format() == VIL_PIXEL_FORMAT_RGB_BYTE) {
00039     // vxl_byte is unsigned so nothing happens
00040     img_out->put_view(*(image->get_view()));
00041   }
00042 
00043   // byte greyscale
00044   else if (image->pixel_format() == VIL_PIXEL_FORMAT_BYTE) {
00045     // vxl_byte is unsigned so nothing happens
00046     img_out->put_view(*(image->get_view()));
00047   }
00048 
00049   // short
00050   else if (image->pixel_format() == VIL_PIXEL_FORMAT_UINT_16) {
00051     // vxl_uint_16 is unsigned so nothing happens
00052     img_out->put_view(*(image->get_view()));
00053   }
00054 
00055   // int
00056   else if (image->pixel_format() == VIL_PIXEL_FORMAT_UINT_32) {
00057     // vxl_uint_32 is unsigned so nothing happens
00058     img_out->put_view(*(image->get_view()));
00059   }
00060 
00061   // then the signed types
00062   //
00063   // byte greyscale
00064   else if (image->pixel_format() == VIL_PIXEL_FORMAT_SBYTE) {
00065     vil_image_view<vxl_sbyte> in = image->get_view();
00066     vil_image_view<vxl_sbyte> out = image->get_copy_view();
00067     vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_sbyte,vxl_sbyte> op(abs_byte);
00068     op.put_in_data_ptr(&in);
00069     op.put_out_data_ptr(&out);
00070     op.filter();
00071     img_out->put_view(out);
00072   }
00073 
00074   // short int greyscale
00075   else if (image->pixel_format() == VIL_PIXEL_FORMAT_INT_16) {
00076     vil_image_view<vxl_int_16> in = image->get_view();
00077     vil_image_view<vxl_int_16> out = image->get_copy_view();
00078     vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_int_16,vxl_int_16> op(abs_short);
00079     op.put_in_data_ptr(&in);
00080     op.put_out_data_ptr(&out);
00081     op.filter();
00082     img_out->put_view(out);
00083   }
00084 
00085   // int greyscale
00086   else if (image->pixel_format() == VIL_PIXEL_FORMAT_INT_32) {
00087     vil_image_view<vxl_int_32> in = image->get_view();
00088     vil_image_view<vxl_int_32> out = image->get_copy_view();
00089     vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_int_32,vxl_int_32> op(abs_int);
00090     op.put_in_data_ptr(&in);
00091     op.put_out_data_ptr(&out);
00092     op.filter();
00093     img_out->put_view(out);
00094   }
00095 
00096   // and finally the "floating point" types
00097   //
00098   // float
00099   else if (image->pixel_format() == VIL_PIXEL_FORMAT_FLOAT) {
00100     vil_image_view<float> in = image->get_view();
00101     vil_image_view<float> out = image->get_copy_view();
00102     vipl_monadic<vil_image_view_base,vil_image_view_base,float,float> op(abs_float);
00103     op.put_in_data_ptr(&in);
00104     op.put_out_data_ptr(&out);
00105     op.filter();
00106     img_out->put_view(out);
00107   }
00108 
00109   // double
00110   else if (image->pixel_format() == VIL_PIXEL_FORMAT_DOUBLE) {
00111     vil_image_view<double> in = image->get_view();
00112     vil_image_view<double> out = image->get_copy_view();
00113     vipl_monadic<vil_image_view_base,vil_image_view_base,double,double> op(abs_double);
00114     op.put_in_data_ptr(&in);
00115     op.put_out_data_ptr(&out);
00116     op.filter();
00117     img_out->put_view(out);
00118   }
00119 
00120   //
00121   else
00122     vcl_cerr << __FILE__ ": vepl_monadic_abs() not implemented for " << image << '\n';
00123 
00124   return img_out;
00125 }
00126 
00127 vil_image_resource_sptr vepl_monadic_sqrt(vil_image_resource_sptr image)
00128 {
00129   vil_image_resource_sptr img_out = vil_new_image_resource(image->ni(), image->nj(), image->nplanes(), image->pixel_format());
00130 
00131   // float
00132   if (image->pixel_format() == VIL_PIXEL_FORMAT_FLOAT) {
00133     vil_image_view<float> in = image->get_view();
00134     vil_image_view<float> out = image->get_copy_view();
00135     vipl_monadic<vil_image_view_base,vil_image_view_base,float,float> op(sqrt_float);
00136     op.put_in_data_ptr(&in);
00137     op.put_out_data_ptr(&out);
00138     op.filter();
00139     img_out->put_view(out);
00140   }
00141 
00142   // double
00143   else if (image->pixel_format() == VIL_PIXEL_FORMAT_DOUBLE) {
00144     vil_image_view<double> in = image->get_view();
00145     vil_image_view<double> out = image->get_copy_view();
00146     vipl_monadic<vil_image_view_base,vil_image_view_base,double,double> op(sqrt_double);
00147     op.put_in_data_ptr(&in);
00148     op.put_out_data_ptr(&out);
00149     op.filter();
00150     img_out->put_view(out);
00151   }
00152 
00153   //
00154   else
00155     vcl_cerr << __FILE__ ": vepl_monadic_sqrt() not implemented for " << image << '\n';
00156 
00157   return img_out;
00158 }
00159 
00160 vil_image_resource_sptr vepl_monadic_sqr(vil_image_resource_sptr image)
00161 {
00162   vil_image_resource_sptr img_out = vil_new_image_resource(image->ni(), image->nj(), image->nplanes(), image->pixel_format());
00163 
00164   // byte greyscale
00165   if (image->pixel_format() == VIL_PIXEL_FORMAT_BYTE) {
00166     vil_image_view<vxl_byte> in = image->get_view();
00167     vil_image_view<vxl_byte> out = image->get_copy_view();
00168     vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_byte,vxl_byte> op(sqr_ubyte);
00169     op.put_in_data_ptr(&in);
00170     op.put_out_data_ptr(&out);
00171     op.filter();
00172     img_out->put_view(out);
00173   }
00174 
00175   // short
00176   else if (image->pixel_format() == VIL_PIXEL_FORMAT_UINT_16) {
00177     vil_image_view<vxl_uint_16> in = image->get_view();
00178     vil_image_view<vxl_uint_16> out = image->get_copy_view();
00179     vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_uint_16,vxl_uint_16> op(sqr_short);
00180     op.put_in_data_ptr(&in);
00181     op.put_out_data_ptr(&out);
00182     op.filter();
00183     img_out->put_view(out);
00184   }
00185 
00186   // int
00187   else if (image->pixel_format() == VIL_PIXEL_FORMAT_UINT_32) {
00188     vil_image_view<vxl_uint_32> in = image->get_view();
00189     vil_image_view<vxl_uint_32> out = image->get_copy_view();
00190     vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_uint_32,vxl_uint_32> op(sqr_int);
00191     op.put_in_data_ptr(&in);
00192     op.put_out_data_ptr(&out);
00193     op.filter();
00194     img_out->put_view(out);
00195   }
00196 
00197   // float
00198   else if (image->pixel_format() == VIL_PIXEL_FORMAT_FLOAT) {
00199     vil_image_view<float> in = image->get_view();
00200     vil_image_view<float> out = image->get_copy_view();
00201     vipl_monadic<vil_image_view_base,vil_image_view_base,float,float> op(sqr_float);
00202     op.put_in_data_ptr(&in);
00203     op.put_out_data_ptr(&out);
00204     op.filter();
00205     img_out->put_view(out);
00206   }
00207 
00208   // double
00209   else if (image->pixel_format() == VIL_PIXEL_FORMAT_DOUBLE) {
00210     vil_image_view<double> in = image->get_view();
00211     vil_image_view<double> out = image->get_copy_view();
00212     vipl_monadic<vil_image_view_base,vil_image_view_base,double,double> op(sqr_double);
00213     op.put_in_data_ptr(&in);
00214     op.put_out_data_ptr(&out);
00215     op.filter();
00216     img_out->put_view(out);
00217   }
00218 
00219   //
00220   else
00221     vcl_cerr << __FILE__ ": vepl_monadic_sqr() not implemented for " << image << '\n';
00222 
00223   return img_out;
00224 }
00225 
00226 vil_image_resource_sptr vepl_monadic_shear(vil_image_resource_sptr image, double shift, double scale)
00227 {
00228   shift_ = shift; scale_ = scale;
00229   vil_image_resource_sptr img_out = vil_new_image_resource(image->ni(), image->nj(), image->nplanes(), image->pixel_format());
00230 
00231   // byte greyscale
00232   if (image->pixel_format() == VIL_PIXEL_FORMAT_BYTE) {
00233     vil_image_view<vxl_byte> in = image->get_view();
00234     vil_image_view<vxl_byte> out = image->get_copy_view();
00235     vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_byte,vxl_byte> op(shear_ubyte);
00236     op.put_in_data_ptr(&in);
00237     op.put_out_data_ptr(&out);
00238     op.filter();
00239     img_out->put_view(out);
00240   }
00241 
00242   // short
00243   else if (image->pixel_format() == VIL_PIXEL_FORMAT_UINT_16) {
00244     vil_image_view<vxl_uint_16> in = image->get_view();
00245     vil_image_view<vxl_uint_16> out = image->get_copy_view();
00246     vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_uint_16,vxl_uint_16> op(shear_short);
00247     op.put_in_data_ptr(&in);
00248     op.put_out_data_ptr(&out);
00249     op.filter();
00250     img_out->put_view(out);
00251   }
00252 
00253   // int
00254   else if (image->pixel_format() == VIL_PIXEL_FORMAT_UINT_32) {
00255     vil_image_view<vxl_uint_32> in = image->get_view();
00256     vil_image_view<vxl_uint_32> out = image->get_copy_view();
00257     vipl_monadic<vil_image_view_base,vil_image_view_base,vxl_uint_32,vxl_uint_32> op(shear_int);
00258     op.put_in_data_ptr(&in);
00259     op.put_out_data_ptr(&out);
00260     op.filter();
00261     img_out->put_view(out);
00262   }
00263 
00264   // float
00265   else if (image->pixel_format() == VIL_PIXEL_FORMAT_FLOAT) {
00266     vil_image_view<float> in = image->get_view();
00267     vil_image_view<float> out = image->get_copy_view();
00268     vipl_monadic<vil_image_view_base,vil_image_view_base,float,float> op(shear_float);
00269     op.put_in_data_ptr(&in);
00270     op.put_out_data_ptr(&out);
00271     op.filter();
00272     img_out->put_view(out);
00273   }
00274 
00275   // double
00276   else if (image->pixel_format() == VIL_PIXEL_FORMAT_DOUBLE) {
00277     vil_image_view<double> in = image->get_view();
00278     vil_image_view<double> out = image->get_copy_view();
00279     vipl_monadic<vil_image_view_base,vil_image_view_base,double,double> op(shear_double);
00280     op.put_in_data_ptr(&in);
00281     op.put_out_data_ptr(&out);
00282     op.filter();
00283     img_out->put_view(out);
00284   }
00285 
00286   //
00287   else
00288     vcl_cerr << __FILE__ ": vepl_monadic_shear() not implemented for " << image << '\n';
00289 
00290   return img_out;
00291 }
00292