#include <vipl/accessors/vipl_accessors_vil_image_view.h>
#include <vil/vil_image_view.h>
#include <vipl/vipl_moment.h>
#include <vipl/vipl_monadic.h>
#include <vipl/vipl_dyadic.h>
#include <vipl/vipl_convert.h>
#include <vil/vil_load.h>
#include <vil/vil_save.h>
#include <vcl_iostream.h>
#include <vcl_cmath.h>
#include <vxl_config.h>
float square(float const& x) { return x*x; }
void is_minus(float& x, float const& y) { x-=y; }
float squareroot(float const& x) { return vcl_sqrt(x); }
int
main(int argc, char** argv)
{
if (argc < 3) { vcl_cerr << "Syntax: example_vipl_moment file_in file_out\n"; return 1; }
vil_image_view<vxl_byte> in = vil_load(argv[1]);
if (!in) { vcl_cerr << "Please use a ubyte image as input\n"; return 2; }
vil_image_view<float> out(in.ni(),in.nj(),in.nplanes());
vil_image_view<float> tmp(in.ni(),in.nj(),in.nplanes());
vipl_moment<vil_image_view<vxl_byte>,vil_image_view<float>,vxl_byte,float> scnd_moment(2,5,5);
scnd_moment.put_in_data_ptr(&in);
scnd_moment.put_out_data_ptr(&out);
scnd_moment.filter();
vipl_moment<vil_image_view<vxl_byte>,vil_image_view<float>,vxl_byte,float> frst_moment(1,5,5);
frst_moment.put_in_data_ptr(&in);
frst_moment.put_out_data_ptr(&tmp);
frst_moment.filter();
vipl_monadic<vil_image_view<float>,vil_image_view<float>,float,float> square_op(square);
square_op.put_in_data_ptr(&tmp);
square_op.put_out_data_ptr(&tmp);
square_op.filter();
vipl_dyadic<vil_image_view<float>,vil_image_view<float>,float,float> minus_op(is_minus);
minus_op.put_in_data_ptr(&tmp);
minus_op.put_out_data_ptr(&out);
minus_op.filter();
vipl_monadic<vil_image_view<float>,vil_image_view<float>,float,float> sqrt_op(squareroot);
sqrt_op.put_in_data_ptr(&out);
sqrt_op.put_out_data_ptr(&out);
sqrt_op.filter();
vil_image_view<vxl_byte> pgm(in.ni(),in.nj(),in.nplanes());
vipl_convert<vil_image_view<float>,vil_image_view<vxl_byte>,float,vxl_byte> op;
op.put_in_data_ptr(&out);
op.put_out_data_ptr(&pgm);
op.filter();
vil_save(pgm, argv[2], "pnm");
vcl_cout << "Written image of type PGM to " << argv[2] << vcl_endl;
return 0;
}