00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00026 #include <vil/vil_load.h>
00027 #include <vil/vil_save.h>
00028 #include <vcl_iostream.h>
00029
00030 #include <vxl_config.h>
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
00039 vil_image_view_base_sptr in = vil_load(argv[1]);
00040
00041
00042 vil_image_view<vxl_byte> out(in->ni(),in->nj(),in->nplanes());
00043
00044 if (in->nplanes() == 3) {
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) {
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()) {
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
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 }