Go to the documentation of this file.00001
00022 #ifndef DGIMAGE_H
00023 #define DGIMAGE_H
00024
00025 #include <string>
00026 #include <fstream>
00027
00028 #include "DGVGlobal.h"
00029
00030 using namespace std;
00031
00033 const int lineLength = 256;
00034
00039 template<typename type>
00040 class DGV_TEMPLATEDLL DGImage
00041 {
00042 public:
00043 DGImage();
00044 virtual ~DGImage();
00045
00050 bool isPNMBinaryFormat(string filename);
00051
00057 bool readPNM(string filename, Array<type,2> &data, int &greyMax);
00063 bool writePNM(Array<type,2> &data, const int greyMax, string filename);
00064 protected:
00065
00066 };
00067
00068 template<typename type>
00069 DGImage<type>::DGImage()
00070 {
00071
00072 }
00073
00074 template<typename type>
00075 DGImage<type>::~DGImage()
00076 {
00077
00078 }
00079
00080 template<typename type>
00081 bool DGImage<type>::isPNMBinaryFormat(string filename)
00082 {
00083 ifstream inFile(filename.c_str());
00084 char c;
00085 string magic;
00086
00087 if(inFile.fail())
00088 return false;
00089
00090 do
00091 {
00092 inFile >> c;
00093 if (c == '\0')
00094 {
00095
00096 inFile.close();
00097 return false;
00098 }
00099 }
00100 while (c != 'P');
00101 magic += c;
00102 inFile >> c;
00103 magic += c;
00104
00105
00106 if ( magic == "P4" || magic == "P5" || magic == "P6" )
00107 return true;
00108 else
00109 return false;
00110 }
00111
00112 template<typename type>
00113 bool DGImage<type>::readPNM(string filename, Array<type,2> &data, int &greyMax)
00114 {
00115 char portable = 0, fileType, comment[lineLength];
00116 int width = 0, height = 0;
00117 ifstream inFile;
00118
00119 inFile.open(filename.c_str());
00120
00121 if(inFile.fail())
00122 return false;
00123
00126 while(portable != 'P')
00127 inFile >> portable;
00128 inFile >> fileType;
00129 cerr << "File Type: " << portable << fileType << endl;
00130 while(portable != '#')
00131 inFile >> portable;
00132 inFile.getline(comment,lineLength);
00133 cerr << "File Info: " << comment << endl;
00134
00136 inFile >> width;
00137 inFile >> height;
00138 if(fileType != '1')
00139 inFile >> greyMax;
00140 else
00141 greyMax = 1;
00142 printf("File Size: %dx%d, Grey Scale Max: %d\n",width, height, greyMax);
00143
00145 data.resizeAndPreserve(height,width);
00146
00148 if((fileType == '2') || (fileType == '1'))
00149 {
00150 for(int j = 0; j < height; j ++)
00151 for(int k = 0; k < width; k ++)
00152 inFile >> data(j,k);
00153 }
00154 else if(fileType == '3')
00155 {
00156 for(int j = 0; j < height; j ++)
00157 for(int k = 0; k < width; k ++)
00158 {
00159 inFile >> data(j,k);
00160 inFile >> data(j,k);
00161 inFile >> data(j,k);
00162 }
00163 }
00164
00165 inFile.close();
00166
00167 return true;
00168 }
00169
00170 template<typename type>
00171 bool DGImage<type>::writePNM(Array<type,2> &data, const int greyMax, string filename)
00172 {
00173 ofstream outFile;
00174
00175 outFile.open(filename.c_str());
00176 if(outFile.fail())
00177 return false;
00178
00179 outFile << "P2\n# Generated PGM by DGV.\n";
00180 outFile << data.cols() << " " << data.rows() << endl << greyMax << endl;
00181
00182 for(int j = 0; j < data.rows(); j ++)
00183 {
00184 for(int k = 0; k < data.cols(); k ++)
00185 outFile << data(j,k) << " ";
00186 outFile << endl;
00187 }
00188
00189 outFile.close();
00190 return true;
00191 }
00192
00193 #endif // DGIMAGE_H