img_io.h
Go to the documentation of this file.
00001 #ifndef IMG_IO_H_
00002 #define IMG_IO_H_
00003 
00004 // minimal input/output
00005 
00006 #include <iostream>
00007 #include <fstream>
00008 #include <limits>
00009 namespace img {
00010 
00011 template<int Channels, typename ScalarType, bool Safe>
00012 inline bool saveNativeFormat(const Image<Channels,ScalarType,Safe> &image, const char *filename)
00013 {
00014   assert(image.isValid());
00015   if(Safe){
00016     if(!image.isValid()) throw ImageException("Invalid image");
00017   }
00018   using namespace std;
00019   ofstream output (filename, ios::out|ios::binary);
00020   
00021   if (output.is_open()) {
00022     int channels=Channels;
00023     output.write(reinterpret_cast<const char*>(& channels), sizeof(int));
00024     int scalartype=0;
00025     if(typeid(ScalarType) == typeid(float)) {
00026       scalartype=1;
00027     } else if(typeid(ScalarType) == typeid(double)) {
00028       scalartype=2;
00029     } else {
00030       assert(0);
00031     }
00032     output.write(reinterpret_cast<const char*>(& scalartype), sizeof(int));
00033     int width=image.width();
00034     output.write(reinterpret_cast<const char*>(& width), sizeof(int));
00035     int height=image.height();
00036     output.write(reinterpret_cast<const char*>(& height), sizeof(int));
00037     output.write(reinterpret_cast<const char*>(& image.attributes), sizeof(ImgAttributes<ScalarType>));    
00038     output.write(reinterpret_cast<const char*>(image.dataValues()), sizeof(ScalarType) * image.dataValuesSize());
00039     output.flush();
00040     output.close();
00041     return true;
00042   }
00043   if(Safe)
00044     throw ImageException("Unable to save file");
00045   return false;
00046 }
00047 
00048 template<int Channels, typename ScalarType, bool Safe>
00049 inline bool openNativeFormat(const char *filename, Image<Channels,ScalarType,Safe> &image)
00050 {
00051 
00052   using namespace std;
00053   ifstream input (filename, ios::in|ios::binary);
00054   if (input.is_open()) {    
00055     int channels;
00056     input.read(reinterpret_cast<char*>(&channels), sizeof(int));
00057     assert(channels==Channels);
00058     int scalartype;
00059     input.read(reinterpret_cast<char*>(&scalartype), sizeof(int));
00060     if(typeid(ScalarType) == typeid(float)) {
00061       assert(scalartype==1);
00062     } else if(typeid(ScalarType) == typeid(double)) {
00063       assert(scalartype==2);
00064     } else { 
00065       assert(0);
00066     }
00067     int width;
00068     input.read(reinterpret_cast<char*>(&width), sizeof(int));
00069     int height;
00070     input.read(reinterpret_cast<char*>(&height), sizeof(int));
00071     image.setZero(width,height);
00072     input.read(reinterpret_cast<char*>(& image.attributes), sizeof(ImgAttributes<ScalarType>));    
00073     input.read(reinterpret_cast<char*>(image.dataValues()), sizeof(ScalarType) * image.dataValuesSize());
00074     input.close();
00075     return true;
00076   }
00077   if(Safe)
00078     throw ImageException("Unable to open file");
00079   return false;
00080 }
00081 
00082 } //end namespace img
00083 
00084 #endif /*IMG_IO_H_*/


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:31:54