00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00037 #ifndef IO_RAW_H
00038 #define IO_RAW_H
00039
00040
00041 namespace IO
00042 {
00044 template <typename DataType>
00045 void readRaw(const char* filename, DataType* &image, unsigned int size);
00046
00047
00049 template <typename DataType>
00050 void writeRaw(const char* filename, const DataType* image, unsigned int size);
00051
00052
00054 class Raw
00055 {
00056 public:
00058 template <typename DataType>
00059 static void read(const char* filename, DataType* &image, unsigned int size);
00060
00062 template <typename DataType>
00063 static void write(const char* filename, const DataType* image, unsigned int size);
00064 };
00065
00066 }
00067
00068
00069 #include <fstream>
00070 #include <cstdio>
00071 #include <string>
00072 #include <error.h>
00073
00074
00075 #ifdef __APPLE__
00076 template <typename T> inline void endian_swap(T& x) { x = x; }
00077 template <> inline void endian_swap<short>(short& x) { x = (x>>8) | (x<<8); }
00078 template <> inline void endian_swap<unsigned short>(unsigned short& x) { x = (x>>8) | (x<<8); }
00079 template <> inline void endian_swap<int>(int& i) { i = ((i&0xff000000)>>24) | (((i&0x00ff0000)>>16)<<8) | (((i&0x0000ff00)>>8)<<16) | ((i&0x000000ff)<<24); }
00080 template <> inline void endian_swap<unsigned int>(unsigned int& i) { i = ((i&0xff000000)>>24) | (((i&0x00ff0000)>>16)<<8) | (((i&0x0000ff00)>>8)<<16) | ((i&0x000000ff)<<24); }
00081 #endif
00082
00083
00084 namespace IO
00085 {
00086 template <typename DataType>
00087 static void readRaw(const char* filename, DataType* &image, unsigned int size)
00088 {
00089 Raw::read(filename, image, size);
00090 }
00091
00092
00093 template <typename DataType>
00094 static void writeRaw(const char* filename, const DataType* image, unsigned int size)
00095 {
00096 Raw::write(filename, image, size);
00097 }
00098
00099
00100 template <typename DataType>
00101 void Raw::read(const char* filename, DataType* &image, unsigned int size)
00102 {
00103 using namespace std;
00104
00105
00106 ifstream file;
00107
00108 file.open(filename, ios::binary);
00109 if(file.fail()) throw LOCATE(Error("open: %s", filename));
00110
00111 image = new DataType [size];
00112
00113 file.read((char*)image, sizeof(DataType)*size);
00114
00115 file.close();
00116
00117 #ifdef __APPLE__
00118 for(unsigned int i=0; i<size; ++i)
00119 endian_swap(image[i]);
00120 #endif
00121 }
00122
00123
00124 template <typename DataType>
00125 void Raw::write(const char* filename, const DataType* image, unsigned int size)
00126 {
00127 using namespace std;
00128
00129
00130 ofstream file;
00131 file.open(filename, ios::binary);
00132 if(file.fail()) throw LOCATE(Error("open: %s", filename));
00133
00134 #if __APPLE__
00135 for(unsigned int i=0; i<size; ++i)
00136 {
00137 DataType p = image[i];
00138 endian_swap(p);
00139 file.write((char*)p, sizeof(DataType));
00140 }
00141 #else
00142 file.write((char*)image, sizeof(DataType)*size);
00143 #endif
00144
00145 file.close();
00146 }
00147
00148
00149 };
00150
00151
00152 #endif