iSpike  2.1
Spike conversion library for robotics
D:/Home/Programs/iSpike/src/Common.cpp
Go to the documentation of this file.
00001 //iSpike includes
00002 #include <iSpike/Common.hpp>
00003 #include <iSpike/Bitmap.hpp>
00004 #include <iSpike/ISpikeException.hpp>
00005 #include <boost/lexical_cast.hpp>
00006 using namespace ispike;
00007 
00008 //Other includes
00009 #include <string>
00010 #include <string.h>
00011 #include <iostream>
00012 #include <fstream>
00013 #include <ios>
00014 #include <vector>
00015 #include <algorithm>
00016 
00017 
00019 void Common::savePPMImage(const char* filename, Bitmap* image){
00020   std::ofstream file_handle(filename, std::ios::binary);
00021   if (file_handle) {
00022     file_handle << "P6" << std::endl << image->getWidth() << ' ' << image->getHeight() << std::endl << 255 << std::endl;
00023         //Write colour image
00024         if(image->getDepth() ==3)
00025                 file_handle.write((char *)image->getContents(), image->getWidth() * image->getHeight() * image->getDepth());
00026 
00027         //Write black and white image
00028         else if(image->getDepth() == 1){
00029                 int imageSize = image->size();
00030                 char* imageContents = (char*)image->getContents();
00031                 for(int i=0; i<imageSize; ++i){
00032                         //Write each pixel three times
00033                         file_handle.write(&imageContents[i], 1);
00034                         file_handle.write(&imageContents[i], 1);
00035                         file_handle.write(&imageContents[i], 1);
00036                 }
00037         }
00038 
00039         //Unknown image
00040         else
00041                 throw ISpikeException("Common: Image that is not depth 1 or 3 cannot be written.");
00042     file_handle.close();
00043   }
00044 }
00045 
00046 
00048 void Common::writePatternToFile(const char* fileName, std::vector<int> pattern, int numOfNeurons){
00049  std::ofstream fileStream;
00050 
00051  fileStream.open(fileName, std::fstream::out | std::fstream::app);
00052 
00053  if (!fileStream) {
00054    std::ostringstream messageStream;
00055    messageStream << "Can't write angles: " << fileName;
00056    std::string message(messageStream.str());
00057    throw ISpikeException(message);
00058  }
00059 
00060  //fileStream << boost::lexical_cast<std::string>(angle) << std::endl;
00061  for( int i = 0; i < numOfNeurons; i++ ) {
00062    if(std::find(pattern.begin(), pattern.end(), i) != pattern.end()) {
00063      fileStream << "1,";
00064    }
00065    else {
00066      fileStream << "0,";
00067    }
00068  }
00069  fileStream << std::endl;
00070 
00071  if (fileStream.fail()) {
00072    std::ostringstream messageStream;
00073    messageStream << "Can't write angles: " << fileName;
00074    std::string message(messageStream.str());
00075    throw ISpikeException(message);
00076  }
00077 
00078  fileStream.close();
00079 }
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines