00001 // This is core/vil/vil_load.cxx 00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE 00003 #pragma implementation 00004 #endif 00005 //: 00006 // \file 00007 00008 #include "vil_load.h" 00009 #include <vcl_iostream.h> 00010 #include <vil/vil_open.h> 00011 #include <vil/vil_new.h> 00012 #include <vil/vil_file_format.h> 00013 #include <vil/vil_stream.h> 00014 #include <vil/vil_image_resource.h> 00015 #include <vil/vil_image_resource_plugin.h> 00016 #include <vil/vil_image_view.h> 00017 00018 vil_image_resource_sptr vil_load_image_resource_raw(vil_stream *is) 00019 { 00020 for (vil_file_format** p = vil_file_format::all(); *p; ++p) { 00021 #if 0 // debugging 00022 vcl_cerr << __FILE__ " : trying \'" << (*p)->tag() << "\'\n"; 00023 #endif 00024 is->seek(0); 00025 vil_image_resource_sptr im = (*p)->make_input_image(is); 00026 if (im) 00027 return im; 00028 } 00029 00030 // failed. 00031 vcl_cerr << __FILE__ ": Unable to load image;\ntried"; 00032 for (vil_file_format** p = vil_file_format::all(); *p; ++p) 00033 // 'flush' in case of segfault next time through loop. Else, we 00034 // will not see those printed tags still in the stream buffer. 00035 vcl_cerr << " \'" << (*p)->tag() << "\'" << vcl_flush; 00036 vcl_cerr << vcl_endl; 00037 00038 return 0; 00039 } 00040 00041 vil_image_resource_sptr vil_load_image_resource_raw(char const* filename) 00042 { 00043 vil_smart_ptr<vil_stream> is = vil_open(filename, "r"); 00044 if (is) 00045 return vil_load_image_resource_raw(is.as_pointer()); 00046 else { 00047 vcl_cerr << __FILE__ ": Failed to load [" << filename << "]\n"; 00048 return vil_image_resource_sptr(0); 00049 } 00050 } 00051 00052 vil_image_resource_sptr vil_load_image_resource(char const* filename) 00053 { 00054 vil_image_resource_sptr im = vil_load_image_resource_plugin(filename); 00055 if (!im) 00056 im=vil_load_image_resource_raw(filename); 00057 return im; 00058 } 00059 00060 00061 vil_image_resource_sptr vil_load_image_resource_plugin(char const* filename) 00062 { 00063 vil_image_resource_plugin im_resource_plugin; 00064 if (im_resource_plugin.can_be_loaded(filename)) 00065 { 00066 vil_image_view_base* img=new vil_image_view<vxl_byte>(640,480,3); 00067 vil_image_resource_sptr im; 00068 vil_image_view_base_sptr im_view(img); 00069 if (im_resource_plugin.load_the_image(im_view,filename)) 00070 { 00071 im=vil_new_image_resource(im_view->ni(),im_view->nj(), 00072 im_view->nplanes(),im_view->pixel_format()); 00073 if (im->put_view((const vil_image_view_base&)*im_view,0,0)) 00074 return im; 00075 } 00076 } 00077 return vil_image_resource_sptr(0); 00078 } 00079 00080 vil_pyramid_image_resource_sptr 00081 vil_load_pyramid_resource(char const* directory_or_file) 00082 { 00083 for (vil_file_format** p = vil_file_format::all(); *p; ++p) { 00084 #if 0 // debugging 00085 vcl_cerr << __FILE__ " : trying \'" << (*p)->tag() << "\'\n"; 00086 00087 00088 vcl_cerr << "make_input_pyramid_image(" << directory_or_file << ")\n"; 00089 #endif 00090 vil_pyramid_image_resource_sptr pir = 00091 (*p)->make_input_pyramid_image(directory_or_file); 00092 if (pir) 00093 return pir; 00094 } 00095 // failed. 00096 vcl_cerr << __FILE__ ": Unable to load pyramid image;\ntried"; 00097 for (vil_file_format** p = vil_file_format::all(); *p; ++p) 00098 // 'flush' in case of segfault next time through loop. Else, we 00099 // will not see those printed tags still in the stream buffer. 00100 vcl_cerr << " \'" << (*p)->tag() << "\'" << vcl_flush; 00101 vcl_cerr << vcl_endl; 00102 00103 return 0; 00104 } 00105 00106 00107 //: Convenience function for loading an image into an image view. 00108 vil_image_view_base_sptr vil_load(const char *file) 00109 { 00110 vil_image_resource_sptr data = vil_load_image_resource(file); 00111 if (!data) return 0; 00112 return data -> get_view(); 00113 }
1.5.1