contrib/mul/vil3d/io/vil3d_io_image_view.h
Go to the documentation of this file.
00001 // This is mul/vil3d/io/vil3d_io_image_view.h
00002 #ifndef vil3d_io_image_view_h_
00003 #define vil3d_io_image_view_h_
00004 //:
00005 // \file
00006 // \author Tim Cootes
00007 
00008 #include <vcl_cstddef.h>
00009 #include <vcl_iostream.h>
00010 #include <vil3d/vil3d_image_view.h>
00011 #include <vil/io/vil_io_memory_chunk.h>
00012 #include <vil/io/vil_io_smart_ptr.h>
00013 
00014 //: Binary save vil3d_image_view<T> to stream.
00015 template<class T>
00016 inline void vsl_b_write(vsl_b_ostream &os, const vil3d_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.nk());
00023   vsl_b_write(os, image.nplanes());
00024   vsl_b_write(os, image.istep());
00025   vsl_b_write(os, image.jstep());
00026   vsl_b_write(os, image.kstep());
00027   vsl_b_write(os, image.planestep());
00028   if (image.size()>0)
00029   {
00030     vsl_b_write(os, image.memory_chunk());
00031 
00032     vcl_ptrdiff_t offset = (image.origin_ptr()-reinterpret_cast<const T*>(image.memory_chunk()->data()));
00033     vsl_b_write(os, offset);
00034   }
00035 }
00036 
00037 
00038 //: Binary load vil3d_image_view<T> from stream.
00039 template<class T>
00040 inline void vsl_b_read(vsl_b_istream &is, vil3d_image_view<T>& image)
00041 {
00042   if (!is) return;
00043 
00044   unsigned ni,nj,nk,np;
00045   vcl_ptrdiff_t istep,jstep,kstep,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, nk);
00057     vsl_b_read(is, np);
00058     vsl_b_read(is, istep);
00059     vsl_b_read(is, jstep);
00060     vsl_b_read(is, kstep);
00061     vsl_b_read(is, pstep);
00062     if (ni*nj*np==0) image.set_size(0,0,0);
00063     else
00064     {
00065       vsl_b_read(is, chunk);
00066       vsl_b_read(is, offset);
00067       const T* data = reinterpret_cast<const T*>(chunk->data());
00068       image = vil3d_image_view<T>(chunk,data+offset,
00069                                   ni,nj,nk,np,istep,jstep,kstep,pstep);
00070     }
00071     break;
00072 
00073   default:
00074     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vil3d_image_view<T>&)\n"
00075              << "           Unknown version number "<< w << "\n";
00076     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00077     return;
00078   }
00079 }
00080 
00081 //: Binary load vil3d_image_view<T> from stream  onto the heap
00082 template<class T>
00083 inline void vsl_b_read(vsl_b_istream &is, vil3d_image_view<T>*& p)
00084 {
00085   delete p;
00086   bool not_null_ptr;
00087   vsl_b_read(is, not_null_ptr);
00088   if (not_null_ptr)
00089   {
00090     p = new vil3d_image_view<T>();
00091     vsl_b_read(is, *p);
00092   }
00093   else
00094     p = 0;
00095 }
00096 
00097 //: Print human readable summary of a vil3d_image_view<T> object to a stream
00098 template<class T>
00099 inline void vsl_print_summary(vcl_ostream& os,const vil3d_image_view<T>& image)
00100 {
00101   image.print(os);
00102 }
00103 
00104 #endif // vil3d_io_image_view_h_