core/vil/io/vil_io_image_view.h
Go to the documentation of this file.
00001 // This is core/vil/io/vil_io_image_view.h
00002 #ifndef vil_io_image_view_h_
00003 #define vil_io_image_view_h_
00004 //:
00005 // \file
00006 // \author Tim Cootes
00007 
00008 #include <vcl_cstddef.h>
00009 #include <vcl_iostream.h>
00010 #include <vil/vil_image_view.h>
00011 #include <vil/io/vil_io_memory_chunk.h>
00012 
00013 //: Binary save vil_image_view<T> to stream.
00014 // \relatesalso vil_image_view
00015 template<class T>
00016 inline void vsl_b_write(vsl_b_ostream &os, const vil_image_view<T>& image)
00017 {
00018   const short io_version_no = 1;
00019   vsl_b_write(os, io_version_no);
00020   vsl_b_write(os, image.ni());
00021   vsl_b_write(os, image.nj());
00022   vsl_b_write(os, image.nplanes());
00023   vsl_b_write(os, image.istep());
00024   vsl_b_write(os, image.jstep());
00025   vsl_b_write(os, image.planestep());
00026   if (image.size()>0)
00027   {
00028     vsl_b_write(os, image.memory_chunk());
00029 
00030     vcl_ptrdiff_t offset = (image.top_left_ptr()-
00031                             reinterpret_cast<const T*>(image.memory_chunk()->data()));
00032     vsl_b_write(os, offset);
00033   }
00034 }
00035 
00036 
00037 //: Binary load vil_image_view<T> from stream.
00038 // \relatesalso vil_image_view
00039 template<class T>
00040 inline void vsl_b_read(vsl_b_istream &is, vil_image_view<T>& image)
00041 {
00042   if (!is) return;
00043 
00044   unsigned ni,nj,np;
00045   vcl_ptrdiff_t istep,jstep,pstep;
00046   vil_memory_chunk_sptr chunk;
00047   vcl_ptrdiff_t offset;
00048 
00049   short w;
00050   vsl_b_read(is, w);
00051   switch (w)
00052   {
00053    case 1:
00054     vsl_b_read(is, ni);
00055     vsl_b_read(is, nj);
00056     vsl_b_read(is, np);
00057     vsl_b_read(is, istep);
00058     vsl_b_read(is, jstep);
00059     vsl_b_read(is, pstep);
00060     if (ni*nj*np==0)
00061       image.set_size(0,0,0);
00062     else
00063     {
00064       vsl_b_read(is, chunk);
00065       if (vil_pixel_format_component_format(image.pixel_format()) != chunk->pixel_format())
00066       {
00067         vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vil_image_view<T>&)\n"
00068                  << "           Mismatched pixel format.\n";
00069         is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00070         return;
00071       }
00072       vsl_b_read(is, offset);
00073       const T* data = reinterpret_cast<const T*>(chunk->data());
00074 
00075       if (chunk->size() < np*ni*nj*sizeof(T) ||
00076           offset < 0 || offset*sizeof(T) >= chunk->size())
00077       {
00078         vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vil_image_view<T>&)\n"
00079                  << "           Image details not compatible with chunk data.\n";
00080         is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00081         return;
00082       }
00083 
00084       image = vil_image_view<T>(chunk,data+offset,
00085                                 ni,nj,np,istep,jstep,pstep);
00086     }
00087     break;
00088 
00089    default:
00090     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vil_image_view<T>&)\n"
00091              << "           Unknown version number "<< w << '\n';
00092     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00093     break;
00094   }
00095 }
00096 
00097 //: Binary load vil_image_view<T> from stream onto the heap
00098 // \relatesalso vil_image_view
00099 template<class T>
00100 inline void vsl_b_read(vsl_b_istream &is, vil_image_view<T>*& p)
00101 {
00102   delete p;
00103   bool not_null_ptr;
00104   vsl_b_read(is, not_null_ptr);
00105   if (not_null_ptr)
00106   {
00107     p = new vil_image_view<T>();
00108     vsl_b_read(is, *p);
00109   }
00110   else
00111     p = 0;
00112 }
00113 
00114 //: Print human readable summary of a vil_image_view<T> object to a stream
00115 // \relatesalso vil_image_view
00116 template<class T>
00117 inline void vsl_print_summary(vcl_ostream& os,const vil_image_view<T>& image)
00118 {
00119   image.print(os);
00120 }
00121 
00122 #endif // vil_io_image_view_h_