core/vil/vil_load.cxx
Go to the documentation of this file.
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 #include <vil/vil_exception.h>
00018 
00019 vil_image_resource_sptr vil_load_image_resource_raw(vil_stream *is,
00020                                                     bool verbose)
00021 {
00022   for (vil_file_format** p = vil_file_format::all(); *p; ++p) {
00023 #if 0 // debugging
00024     vcl_cerr << __FILE__ " : trying \'" << (*p)->tag() << "\'\n";
00025 #endif
00026     is->seek(0);
00027     vil_image_resource_sptr im = (*p)->make_input_image(is);
00028     if (im)
00029       return im;
00030   }
00031 
00032   // failed.
00033   if (verbose) {
00034     vcl_cerr << __FILE__ ": Unable to load image;\ntried";
00035     for (vil_file_format** p = vil_file_format::all(); *p; ++p)
00036       // 'flush' in case of segfault next time through loop. Else, we
00037       // will not see those printed tags still in the stream buffer.
00038       vcl_cerr << " \'" << (*p)->tag() << "\'" << vcl_flush;
00039     vcl_cerr << vcl_endl;
00040   }
00041 
00042   return 0;
00043 }
00044 
00045 vil_image_resource_sptr vil_load_image_resource_raw(char const* filename,
00046                                                     bool verbose)
00047 {
00048   vil_smart_ptr<vil_stream> is = vil_open(filename, "r");
00049   vil_image_resource_sptr isp = 0;
00050   if (is)
00051   {
00052 #ifdef VCL_HAS_EXCEPTIONS
00053     try
00054     {
00055       isp = vil_load_image_resource_raw(is.as_pointer(), verbose);
00056     }
00057     catch (const vil_exception_corrupt_image_file &e)
00058     {
00059       throw vil_exception_corrupt_image_file(e.function_name, e.file_type, filename, e.details);
00060     }
00061 #else
00062     isp = vil_load_image_resource_raw(is.as_pointer(), verbose);
00063 #endif
00064   }
00065 
00066   if (!isp && verbose)
00067     vcl_cerr << __FILE__ ": Failed to load [" << filename << "]\n";
00068   return isp;
00069 }
00070 
00071 vil_image_resource_sptr vil_load_image_resource(char const* filename,
00072                                                 bool verbose)
00073 {
00074   vil_image_resource_sptr im = vil_load_image_resource_plugin(filename);
00075   if (!im)
00076     im=vil_load_image_resource_raw(filename, verbose);
00077   if (!im && verbose)
00078     vcl_cerr << __FILE__ ": Failed to load [" << filename << "]\n";
00079   return im;
00080 }
00081 
00082 
00083 vil_image_resource_sptr vil_load_image_resource_plugin(char const* filename)
00084 {
00085   vil_image_resource_plugin im_resource_plugin;
00086   if (im_resource_plugin.can_be_loaded(filename))
00087   {
00088     vil_image_view_base* img=new vil_image_view<vxl_byte>(640,480,3);
00089     vil_image_resource_sptr im;
00090     vil_image_view_base_sptr im_view(img);
00091     if (im_resource_plugin.load_the_image(im_view,filename))
00092     {
00093       im=vil_new_image_resource(im_view->ni(),im_view->nj(),
00094                                 im_view->nplanes(),im_view->pixel_format());
00095       if (im->put_view((const vil_image_view_base&)*im_view,0,0))
00096         return im;
00097     }
00098   }
00099   return vil_image_resource_sptr(0);
00100 }
00101 
00102 vil_pyramid_image_resource_sptr
00103 vil_load_pyramid_resource(char const* directory_or_file, bool verbose)
00104 {
00105   for (vil_file_format** p = vil_file_format::all(); *p; ++p) {
00106 #if 0 // debugging
00107     vcl_cerr << __FILE__ " : trying \'" << (*p)->tag() << "\'\n";
00108 
00109 
00110     vcl_cerr << "make_input_pyramid_image(" << directory_or_file << ")\n";
00111 #endif
00112     vil_pyramid_image_resource_sptr pir =
00113       (*p)->make_input_pyramid_image(directory_or_file);
00114     if (pir)
00115       return pir;
00116   }
00117   // failed.
00118   if (verbose) {
00119     vcl_cerr << __FILE__ ": Unable to load pyramid image;\ntried";
00120     for (vil_file_format** p = vil_file_format::all(); *p; ++p)
00121       // 'flush' in case of segfault next time through loop. Else, we
00122       // will not see those printed tags still in the stream buffer.
00123       vcl_cerr << " \'" << (*p)->tag() << "\'" << vcl_flush;
00124     vcl_cerr << vcl_endl;
00125   }
00126   return 0;
00127 }
00128 
00129 //: Convenience function for loading an image into an image view.
00130 vil_image_view_base_sptr vil_load(const char *file, bool verbose)
00131 {
00132   vil_image_resource_sptr data = vil_load_image_resource(file, verbose);
00133   if (!data) return 0;
00134   return data -> get_view();
00135 }
00136 
00137 
00138 #if defined(VCL_WIN32) && VXL_USE_WIN_WCHAR_T
00139 //  --------------------------------------------------------------------------------
00140 //  Windows' wchar_t overloading version
00141 //
00142 //
00143 vil_image_resource_sptr vil_load_image_resource_raw(wchar_t const* filename, bool verbose)
00144 {
00145   vil_smart_ptr<vil_stream> is = vil_open(filename, "r");
00146   vil_image_resource_sptr isp = 0;
00147   if (is)
00148     isp = vil_load_image_resource_raw(is.as_pointer(), verbose);
00149   if (!isp && verbose)
00150     std::wcerr << __FILE__ << L": Failed to load [" << filename << L"]\n";
00151   return isp;
00152 }
00153 
00154 vil_image_resource_sptr vil_load_image_resource(wchar_t const* filename, bool verbose)
00155 {
00156   // do not support image resource plugin for the time being
00157   //vil_image_resource_sptr im = vil_load_image_resource_plugin(filename);
00158   //if (!im)
00159   //  im=vil_load_image_resource_raw(filename);
00160   vil_image_resource_sptr im = vil_load_image_resource_raw(filename);
00161   return im;
00162 }
00163 
00164 //: Convenience function for loading an image into an image view.
00165 vil_image_view_base_sptr vil_load(const wchar_t *file, bool verbose)
00166 {
00167   vil_image_resource_sptr data = vil_load_image_resource(file, verbose);
00168   if (!data) return 0;
00169   return data -> get_view();
00170 }
00171 
00172 #endif //defined(VCL_WIN32) && VXL_USE_WIN_WCHAR_T