core/vil1/io/vil1_io_memory_image_impl.cxx

Go to the documentation of this file.
00001 // This is core/vil1/io/vil1_io_memory_image_impl.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author Ian Scott and Nick Costen (Manchester)
00008 
00009 #include "vil1_io_memory_image_impl.h"
00010 #include <vil1/vil1_memory_image_impl.h>
00011 #include <vil1/io/vil1_io_memory_image_format.h>
00012 #include <vsl/vsl_binary_io.h>
00013 #include <vsl/vsl_binary_explicit_io.h>
00014 #include <vcl_climits.h> // for CHAR_BIT
00015 
00016 //: Constructor
00017 vil1_io_memory_image_impl::vil1_io_memory_image_impl()
00018 {
00019 }
00020 
00021 //: Destructor
00022 vil1_io_memory_image_impl::~vil1_io_memory_image_impl()
00023 {
00024 }
00025 
00026 //: Create new object of type vil1_memory_image_impl on heap
00027 vil1_image_impl* vil1_io_memory_image_impl::new_object() const
00028 {
00029   return new vil1_memory_image_impl(0,0,0,VIL1_BYTE );
00030 }
00031 
00032 //: Write derived class to os using vil1_image_impl reference
00033 void vil1_io_memory_image_impl::b_write_by_base(vsl_b_ostream& os,
00034                                                 const vil1_image_impl& base)
00035                                                 const
00036 {
00037   vsl_b_write(os,(vil1_memory_image_impl&) base);
00038 }
00039 
00040 //: Write derived class to os using vil1_image_impl reference
00041 void vil1_io_memory_image_impl::b_read_by_base(vsl_b_istream& is,
00042                                                vil1_image_impl& base) const
00043 {
00044   vsl_b_read(is,(vil1_memory_image_impl&) base);
00045 }
00046 
00047 
00048 //: Copy this object onto the heap and return a pointer
00049 vil1_io_image_impl* vil1_io_memory_image_impl::clone() const
00050 {
00051   return new vil1_io_memory_image_impl(*this);
00052 }
00053 
00054 
00055 //========================================================================
00056 //: Binary save self to stream.
00057 void vsl_b_write(vsl_b_ostream &os, const vil1_memory_image_impl & p)
00058 {
00059   const short io_version_no = 1;
00060   vsl_b_write(os, io_version_no);
00061   vsl_b_write(os, p.planes());
00062   vsl_b_write(os, p.height());
00063   vsl_b_write(os, p.width());
00064   vsl_b_write(os,p.components());
00065   vsl_b_write(os,p.bits_per_component());
00066   vsl_b_write(os,(int)(p.component_format()));
00067   int nelems = p.planes() * p.height() * p.width() * p.components();
00068   int size = nelems * p.bits_per_component() / CHAR_BIT;
00069   unsigned char* buf = new unsigned char[size];
00070   p.get_section(buf,0,0,p.width(),p.height());
00071   vsl_swap_bytes((char*) buf,p.bits_per_component() / CHAR_BIT,nelems);
00072   os.os().write((const char*) buf,size);
00073   delete[] buf;
00074 }
00075 
00076 //========================================================================
00077 //: Binary load self from stream.
00078 void vsl_b_read(vsl_b_istream &is, vil1_memory_image_impl & p)
00079 {
00080   if (!is) return;
00081 
00082   short v;
00083   vsl_b_read(is, v);
00084   switch (v)
00085   {
00086    case 1:
00087    {
00088     int planes, height, width;
00089     int components, bits_per_component, component_format;
00090     vsl_b_read(is, planes);
00091     vsl_b_read(is, height);
00092     vsl_b_read(is, width);
00093     vsl_b_read(is,components);
00094     vsl_b_read(is,bits_per_component);
00095     vsl_b_read(is,component_format);
00096     p.resize(planes,width,height,components,
00097              bits_per_component,vil1_component_format(component_format));
00098     int nelems = p.planes() * p.height() * p.width() * p.components();
00099     int size = nelems * p.bits_per_component() / CHAR_BIT;
00100     unsigned char* buf = new unsigned char[size];
00101     is.is().read((char*) buf,size);
00102     vsl_swap_bytes((char *) buf,p.bits_per_component() / CHAR_BIT, nelems);
00103     p.put_section(buf,0,0,width,height);
00104     delete[] buf;
00105     break;
00106    }
00107    default:
00108     vcl_cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vil1_memory_image_impl&)\n"
00109              << "           Unknown version number "<< v << '\n';
00110     is.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00111   }
00112 }
00113 
00114 //========================================================================
00115 //: Output a human readable summary to the stream
00116 void vsl_print_summary(vcl_ostream& os,const vil1_memory_image_impl & p)
00117 {
00118   os<<"vil1_memory_image_impl :"
00119     <<"\nNum planes : "<<p.planes()
00120     <<" Num rows : "<<p.height()
00121     <<" Num cols "<<p.width()
00122     <<" Num components : "<<p.components()
00123     <<"\nNum bits per comp : "<<p.bits_per_component()
00124     <<" Component format ";
00125   switch (p.component_format())
00126   {
00127    case VIL1_COMPONENT_FORMAT_UNKNOWN:
00128     os << "VIL1_COMPONENT_FORMAT_UNKNOWN";
00129     break;
00130    case VIL1_COMPONENT_FORMAT_UNSIGNED_INT:
00131     os << "VIL1_COMPONENT_FORMAT_UNSIGNED_INT";
00132     break;
00133    case VIL1_COMPONENT_FORMAT_SIGNED_INT:
00134     os << "VIL1_COMPONENT_FORMAT_SIGNED_INT";
00135     break;
00136    case VIL1_COMPONENT_FORMAT_IEEE_FLOAT:
00137     os << "VIL1_COMPONENT_FORMAT_IEEE_FLOAT";
00138     break;
00139    case VIL1_COMPONENT_FORMAT_COMPLEX:
00140     os << "VIL1_COMPONENT_FORMAT_COMPLEX";
00141     break;
00142    default:
00143     os << "unknown";
00144     break;
00145   }
00146 }

Generated on Sat Nov 22 05:08:29 2008 for core/vil1 by  doxygen 1.5.1