examples/example_convert.cxx

00001 //:
00002 // \file
00003 //  This example program shows a typical use of the vipl_convert IP class on
00004 //  any image.  The input image (argv[1]) is converted to a ubyte image
00005 //  and written to argv[2] which is always a PGM file image.
00006 //  When the input image is RGB, its intensity is extracted.
00007 //  To this end an explicit converter from vil_rgb<ubyte> to ubyte is necessary.
00008 //  When it is short, int or float, an implicit mapping is done to 0--255.
00009 //  Uses vipl_convert<vil_image_view<T>,vil_image_view<T>,T,ubyte>.
00010 //
00011 // \author Peter Vanroose, K.U.Leuven, ESAT/PSI
00012 // \date   29 may 1998
00013 //
00014 // \verbatim
00015 // Modifications:
00016 //   Peter Vanroose, Aug.2000 - adapted to vxl
00017 //   Peter Vanroose, Feb.2004 - replaced vil1_image by vil2_image_view<T>
00018 // \endverbatim
00019 //
00020 #include <vil/vil_rgb.h>
00021 #include <vil/vil_image_view.h>
00022 #include <vipl/accessors/vipl_accessors_vil_image_view.h>
00023 #include <vipl/vipl_convert.h>
00024 
00025 // for I/O:
00026 #include <vil/vil_load.h>
00027 #include <vil/vil_save.h>
00028 #include <vcl_iostream.h>
00029 
00030 #include <vxl_config.h> // for vxl_byte
00031 
00032 
00033 int
00034 main(int argc, char** argv)
00035 {
00036   if (argc < 3) { vcl_cerr << "Syntax: example_convert file_in file_out\n"; return 1; }
00037 
00038   // The input image:
00039   vil_image_view_base_sptr in = vil_load(argv[1]);
00040 
00041   // The output image:
00042   vil_image_view<vxl_byte> out(in->ni(),in->nj(),in->nplanes());
00043 
00044   if (in->nplanes() == 3) { // colour (RGB)
00045     if (in->pixel_format() == VIL_PIXEL_FORMAT_BYTE) {
00046       vipl_convert<vil_image_view<vxl_byte>,vil_image_view<vxl_byte>,vxl_byte,vxl_byte> op;
00047       vil_image_view<vil_rgb<vxl_byte> > tmp = in; vil_image_view<vxl_byte> tmp2 = tmp;
00048       op.put_in_data_ptr(&tmp2); op.put_out_data_ptr(&out); op.filter();
00049       vcl_cout << "vipl_converted ubyte RGB image to PGM image " << argv[2] << vcl_endl;
00050     }
00051     else {
00052       vcl_cerr<<"Not yet implemented conversion for non-8-bit colour pixels\n";
00053       return 1;
00054     }
00055   }
00056   else if (in->nplanes() != 1) { // not monochrome nore RGB
00057     vcl_cerr << "Cannot currently convert image with "<< in->nplanes() <<" planes\n";
00058     return 1;
00059   }
00060   else if (in->pixel_format() == VIL_PIXEL_FORMAT_COMPLEX_FLOAT ||
00061            in->pixel_format() == VIL_PIXEL_FORMAT_COMPLEX_DOUBLE) {
00062     vcl_cerr << "Cannot currently convert complex pixel type images\n";
00063     return 1;
00064   }
00065   else if (vil_pixel_format_component_format(in->pixel_format()) != in->pixel_format()) { // RGB or RGBA
00066     if (in->pixel_format() == VIL_PIXEL_FORMAT_RGB_BYTE) {
00067       vipl_convert<vil_image_view<vxl_byte>,vil_image_view<vxl_byte>,vxl_byte,vxl_byte> op;
00068       vil_image_view<vil_rgb<vxl_byte> > tmp = in; vil_image_view<vxl_byte> tmp2 = tmp;
00069       op.put_in_data_ptr(&tmp2); op.put_out_data_ptr(&out); op.filter();
00070       vcl_cout << "vipl_converted ubyte RGB image to PGM image " << argv[2] << vcl_endl;
00071     }
00072     else {
00073       vcl_cerr<<"Not yet implemented conversion for non-8-bit colour pixels\n";
00074       return 1;
00075     }
00076   }
00077   else if (in->pixel_format() == VIL_PIXEL_FORMAT_BYTE)
00078   {
00079     // no conversion necessary
00080     out = in;
00081     vcl_cout << "vipl_converted ubyte image to PGM image "<< argv[2] <<" (no conversion was necessary)\n";
00082   }
00083   else if (in->pixel_format() == VIL_PIXEL_FORMAT_SBYTE)
00084   {
00085     vipl_convert<vil_image_view<vxl_sbyte>,vil_image_view<vxl_byte>,vxl_sbyte,vxl_byte> op;
00086     vil_image_view<vxl_sbyte> tmp = in;
00087     op.put_in_data_ptr(&tmp); op.put_out_data_ptr(&out); op.filter();
00088     vcl_cout << "vipl_converted sbyte image to PGM image "<< argv[2] << vcl_endl;
00089   }
00090   else if (in->pixel_format() == VIL_PIXEL_FORMAT_UINT_16)
00091   {
00092     vipl_convert<vil_image_view<vxl_uint_16>,vil_image_view<vxl_byte>,vxl_uint_16,vxl_byte> op;
00093     vil_image_view<vxl_uint_16> tmp = in;
00094     op.put_in_data_ptr(&tmp); op.put_out_data_ptr(&out); op.filter();
00095     vcl_cout << "vipl_converted uint_16 image to PGM image "<< argv[2] << vcl_endl;
00096   }
00097   else if (in->pixel_format() == VIL_PIXEL_FORMAT_UINT_16)
00098   {
00099     vipl_convert<vil_image_view<vxl_int_16>,vil_image_view<vxl_byte>,vxl_int_16,vxl_byte> op;
00100     vil_image_view<vxl_int_16> tmp = in;
00101     op.put_in_data_ptr(&tmp); op.put_out_data_ptr(&out); op.filter();
00102     vcl_cout << "vipl_converted int_16 image to PGM image "<< argv[2] << vcl_endl;
00103   }
00104   else if (in->pixel_format() == VIL_PIXEL_FORMAT_UINT_32)
00105   {
00106     vipl_convert<vil_image_view<vxl_uint_32>,vil_image_view<vxl_byte>,vxl_uint_32,vxl_byte> op;
00107     vil_image_view<vxl_uint_32> tmp = in;
00108     op.put_in_data_ptr(&tmp); op.put_out_data_ptr(&out); op.filter();
00109     vcl_cout << "vipl_converted uint_32 image to PGM image "<< argv[2] << vcl_endl;
00110   }
00111   else if (in->pixel_format() == VIL_PIXEL_FORMAT_UINT_32)
00112   {
00113     vipl_convert<vil_image_view<vxl_int_32>,vil_image_view<vxl_byte>,vxl_int_32,vxl_byte> op;
00114     vil_image_view<vxl_int_32> tmp = in;
00115     op.put_in_data_ptr(&tmp); op.put_out_data_ptr(&out); op.filter();
00116     vcl_cout << "vipl_converted int_32 image to PGM image "<< argv[2] << vcl_endl;
00117   }
00118 #if VXL_HAS_INT_64
00119   else if (in->pixel_format() == VIL_PIXEL_FORMAT_UINT_64)
00120   {
00121     vipl_convert<vil_image_view<vxl_uint_64>,vil_image_view<vxl_byte>,vxl_uint_64,vxl_byte> op;
00122     vil_image_view<vxl_uint_64> tmp = in;
00123     op.put_in_data_ptr(&tmp); op.put_out_data_ptr(&out); op.filter();
00124     vcl_cout << "vipl_converted uint_64 image to PGM image "<< argv[2] << vcl_endl;
00125   }
00126   else if (in->pixel_format() == VIL_PIXEL_FORMAT_UINT_64)
00127   {
00128     vipl_convert<vil_image_view<vxl_int_64>,vil_image_view<vxl_byte>,vxl_int_64,vxl_byte> op;
00129     vil_image_view<vxl_int_64> tmp = in;
00130     op.put_in_data_ptr(&tmp); op.put_out_data_ptr(&out); op.filter();
00131     vcl_cout << "vipl_converted int_64 image to PGM image "<< argv[2] << vcl_endl;
00132   }
00133 #endif
00134   else if (in->pixel_format() == VIL_PIXEL_FORMAT_FLOAT)
00135   {
00136     vipl_convert<vil_image_view<float>,vil_image_view<vxl_byte>,float,vxl_byte> op;
00137     vil_image_view<float> tmp = in;
00138     op.put_in_data_ptr(&tmp); op.put_out_data_ptr(&out); op.filter();
00139     vcl_cout << "vipl_converted float image to PGM image "<< argv[2] << vcl_endl;
00140   }
00141   else if (in->pixel_format() == VIL_PIXEL_FORMAT_DOUBLE)
00142   {
00143     vipl_convert<vil_image_view<double>,vil_image_view<vxl_byte>,double,vxl_byte> op;
00144     vil_image_view<double> tmp = in;
00145     op.put_in_data_ptr(&tmp); op.put_out_data_ptr(&out); op.filter();
00146     vcl_cout << "vipl_converted double image to PGM image "<< argv[2]<<vcl_endl;
00147   }
00148 #if 0
00149   else if (in->pixel_format() == VIL_PIXEL_FORMAT_PYRAMID) {
00150     vcl_cerr << "Cannot currently convert pyramid images\n";
00151     return 1;
00152   }
00153   else if (in->pixel_format() == VIL_PIXEL_FORMAT_BANDED) {
00154     vcl_cerr << "Cannot currently convert banded images\n";
00155     return 1;
00156   }
00157 #endif
00158   else {
00159     vcl_cerr << "Never heared of image format "<< int(in->pixel_format())<<'\n';
00160     return 1;
00161   }
00162 
00163   vil_save(out, argv[2], "pnm");
00164   return 0;
00165 }

Generated on Sat Sep 6 05:14:08 2008 for contrib/tbl/vipl by  doxygen 1.5.1