Utils.h
Go to the documentation of this file.
00001 /* Copyright (C) 2012 Christian Lutz, Thorsten Engesser
00002  * 
00003  * This file is part of motld
00004  * 
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation, either version 3 of the License, or
00008  * (at your option) any later version.
00009  * 
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #ifndef UTILS_H
00020 #define UTILS_H
00021 
00022 #include <cstring>
00023 
00024 #ifdef _MSC_VER
00025 #include <time.h>
00026 #else
00027 #include <sys/time.h>
00028 #endif
00029 
00030 namespace motld {
00031 
00032 /*****************************************************************************
00033  *                                 General Stuff                             *
00034  *****************************************************************************/
00035 
00037   inline int randInt(int min, int max)
00038   {
00039     return min + rand() % (1 + max - min);
00040   }
00041 
00043   inline float randFloat(float min, float max)
00044   {
00045     return min + ((float)rand() / RAND_MAX)*(max-min);
00046   }
00047 
00049   inline int getTime()
00050   {
00051 #ifdef _MSC_VER
00052     clock_t t = clock();
00053     return t * 1000 / CLOCKS_PER_SEC;
00054 #else
00055     timeval t;
00056     gettimeofday(&t, 0);
00057     return t.tv_sec*1000 + (int)(t.tv_usec/1000.);
00058 #endif
00059   }
00060 
00061 /*****************************************************************************
00062  *                   File Input / Output - PGM and PPM                       *
00063  *****************************************************************************/
00064 
00066   template <class T>
00067     T* readFromPPM(const char* aFilename, int & xSize, int & ySize, int & zSize) {
00068     T * result = 0;
00069     FILE *aStream;
00070     aStream = fopen(aFilename,"rb");
00071     if (aStream == 0)
00072       std::cerr << "File not found: " << aFilename << std::endl;
00073     int dummy;
00074     // Find beginning of file (P6)
00075     while (getc(aStream) != 'P');
00076     dummy = getc(aStream);
00077     if (dummy == '5') zSize = 1;
00078     else if (dummy == '6') zSize = 3;
00079     else
00080     {
00081       std::cerr << "Cannot read File - Invalid File Format!" << std::endl;
00082       zSize = 0;
00083       return result;
00084     }
00085     do dummy = getc(aStream); while (dummy != '\n' && dummy != ' ');
00086     // Remove comments and empty lines
00087     dummy = getc(aStream);
00088     while (dummy == '#') {
00089       while (getc(aStream) != '\n');
00090       dummy = getc(aStream);
00091     }
00092     while (dummy == '\n')
00093       dummy = getc(aStream);
00094     // Read image size
00095     xSize = dummy-48;
00096     while ((dummy = getc(aStream)) >= 48 && dummy < 58)
00097       xSize = 10*xSize+dummy-48;
00098     while ((dummy = getc(aStream)) < 48 || dummy >= 58);
00099     ySize = dummy-48;
00100     while ((dummy = getc(aStream)) >= 48 && dummy < 58)
00101       ySize = 10*ySize+dummy-48;
00102     while (dummy != '\n' && dummy != ' ')
00103       dummy = getc(aStream);
00104     while (dummy < 48 || dummy >= 58) dummy = getc(aStream);
00105     while ((dummy = getc(aStream)) >= 48 && dummy < 58);
00106     if (dummy != '\n') while (getc(aStream) != '\n');
00107     // Adjust size of data structure
00108     result = new T[xSize*ySize*zSize];
00109     //result = (T*)malloc(xSize*ySize*zSize*sizeof(T));
00110     // Read image data
00111     int aSize = xSize*ySize;
00112     if (zSize == 1)
00113       for (int i = 0; i < aSize; i++)
00114         result[i] = getc(aStream) / 255.;
00115     else {
00116       int aSizefloatwice = aSize+aSize;
00117       for (int i = 0; i < aSize; i++) {
00118         result[i] = getc(aStream);
00119         result[i+aSize] = getc(aStream);
00120         result[i+aSizefloatwice] = getc(aStream);
00121       }
00122     }
00123     fclose(aStream);
00124     return result;
00125   }
00126 
00128   template<class T>
00129     T* readFromPGM(const char* filename, int & xSize, int & ySize)
00130   {
00131     T * result = 0;
00132     FILE *flStream;
00133     flStream = fopen(filename,"rb");
00134     if (flStream == 0)
00135     { 
00136       std::cerr << "File not found: " << filename << std::endl;
00137       return result;
00138     }
00139     int dummy;
00140     // Find beginning of file (P5)
00141     while (getc(flStream) != 'P');
00142     if (getc(flStream) != '5')
00143     {
00144       std::cerr << "Cannot read File - Invalid File Format!" << std::endl;
00145       return result;
00146     }
00147     do dummy = getc(flStream); while (dummy != '\n' && dummy != ' ');
00148     // Remove comments and empty lines
00149     dummy = getc(flStream);
00150     while (dummy == '#') {
00151       while (getc(flStream) != '\n');
00152       dummy = getc(flStream);
00153     }
00154     while (dummy == '\n')
00155       dummy = getc(flStream);
00156     // Read image size
00157     xSize = dummy-48;
00158     while ((dummy = getc(flStream)) >= 48 && dummy < 58)
00159       xSize = 10*xSize+dummy-48;
00160     while ((dummy = getc(flStream)) < 48 || dummy >= 58);
00161     ySize = dummy-48;
00162     while ((dummy = getc(flStream)) >= 48 && dummy < 58)
00163       ySize = 10*ySize+dummy-48;
00164     while (dummy != '\n' && dummy != ' ')
00165       dummy = getc(flStream);
00166     while ((dummy = getc(flStream)) >= 48 && dummy < 58);
00167     if (dummy != '\n') while (getc(flStream) != '\n');
00168     result = new T[xSize*ySize];
00169     for (int i = 0; i < xSize*ySize; i++)
00170       result[i] = getc(flStream);
00171     fclose(flStream);
00172     return result;
00173   }
00174 
00176   template <class T>
00177     void writeToPGM(const char *filename, T *data, int xSize, int ySize) {
00178     FILE *flStream;
00179     flStream = fopen(filename,"wb");
00180     // write header
00181     char line[60];
00182     sprintf(line,"P5\n%d %d\n255\n",xSize,ySize);
00183     fwrite(line,strlen(line),1,flStream);
00184     // write data
00185     for (int i = 0; i < xSize*ySize; i++) {
00186       char dummy = (char)data[i];
00187       fwrite(&dummy,1,1,flStream);
00188     }
00189     fclose(flStream);
00190   }
00191 
00193   template <class T>
00194     void writeToPPM(const char * aFilename, T * data, int xSize, int ySize) {
00195   
00196     T * red = data;
00197     T * green = red + xSize * ySize;
00198     T * blue = green + xSize * ySize;
00199   
00200     FILE* outimage = fopen(aFilename, "wb");
00201     fprintf(outimage, "P6 \n");
00202     fprintf(outimage, "%d %d \n255\n", xSize,ySize);
00203     for (int p = 0; p < xSize * ySize; ++p)
00204     {
00205       fwrite (red+p, sizeof(unsigned char), 1, outimage);
00206       fwrite (green+p, sizeof(unsigned char), 1, outimage);
00207       fwrite (blue+p, sizeof(unsigned char), 1, outimage);
00208     }
00209     
00210     fclose(outimage);
00211   }
00212 
00214   template <class T>
00215     T* toGray(T * rgb, int size)
00216   {
00217     T* result = new T[size];
00218     T* red = rgb;
00219     T* green = red + size;
00220     T* blue = green + size;
00221     for (int i = 0; i < size; ++i)
00222     {
00223       result[i] = (red[i] + green[i] + blue[i]) / 3.;
00224     }
00225     return result;
00226   }
00227 
00228 }
00229 
00230 #endif // UTILS_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


motld
Author(s): Jost Tobias Springenberg, Jan Wuelfing
autogenerated on Wed Dec 26 2012 16:24:49