iSpike
2.1
Spike conversion library for robotics
|
00001 //iSpike includes 00002 #include <iSpike/Reader/FileVisualReader.hpp> 00003 #include <iSpike/Bitmap.hpp> 00004 #include <iSpike/ISpikeException.hpp> 00005 #include <iSpike/Log/Log.hpp> 00006 using namespace ispike; 00007 00008 //Other includes 00009 #include <iostream> 00010 #include <fstream> 00011 using namespace std; 00012 00013 //Enable/disable debug output 00014 //#define DEBUG 00015 00016 FileVisualReader::FileVisualReader() 00017 { 00018 // Define the properties of this reader 00019 addProperty(Property("imageIn.ppm", "File Name", "The file where the image will be read from", true)); 00020 00021 //Create description 00022 readerDescription = Description("File Visual Reader", "This is a file visual reader", "Visual Reader"); 00023 } 00024 00025 00026 FileVisualReader::~FileVisualReader(){ 00027 } 00028 00029 00030 /*--------------------------------------------------------------------*/ 00031 /*--------- PUBLIC METHODS -------*/ 00032 /*--------------------------------------------------------------------*/ 00033 00035 Bitmap& FileVisualReader::getBitmap(){ 00036 return bitmap; 00037 } 00038 00039 00040 //Inherited from Reader 00041 void FileVisualReader::initialize(map<string, Property>& properties){ 00042 setProperties(properties); 00043 setInitialized(true); 00044 } 00045 00046 00047 //Inherited from PropertyHolder 00048 void FileVisualReader::setProperties(map<string, Property>& properties){ 00049 string fileName = updateStringProperty(properties["File Name"]); 00050 readPPMImage(fileName); 00051 } 00052 00053 00054 /*--------------------------------------------------------------------*/ 00055 /*--------- PRIVATE METHODS -------*/ 00056 /*--------------------------------------------------------------------*/ 00057 00059 void FileVisualReader::readPPMImage(string& fname){ 00060 #ifdef DEBUG 00061 LOG(LOG_INFO) << "FileVisualReader: Reading image from: " << fname; 00062 #endif//DEBUG 00063 00064 ifstream ifp; 00065 00066 ifp.open(fname.c_str(), ios::in | ios::binary); 00067 00068 if (!ifp) { 00069 ostringstream messageStream; 00070 messageStream << "Can't read image: " << fname; 00071 string message(messageStream.str()); 00072 throw ISpikeException(message); 00073 } 00074 00075 // read header 00076 char header [100]; 00077 ifp.getline(header,100,'\n'); 00078 00079 if ( (header[0]!=80) || /* 'P' */ 00080 (header[1]!=54) ) { /* '6' */ 00081 ostringstream messageStream; 00082 messageStream << "Image " << fname << " is not PPM"; 00083 string message(messageStream.str()); 00084 throw ISpikeException(message); 00085 } 00086 00087 ifp.getline(header,100,'\n'); 00088 while(header[0]=='#') { 00089 ifp.getline(header,100,'\n'); 00090 } 00091 00092 char* ptr; 00093 int M=strtol(header, &ptr, 0); 00094 int N=atoi(ptr); 00095 00096 #ifdef DEBUG 00097 LOG(LOG_INFO) << "FileVisualReader: Retrieved image with Width: " << M << " Height: " << N; 00098 #endif//DEBUG 00099 00100 ifp.getline(header,100,'\n'); 00101 00102 bitmap.reset(M, N, 3); 00103 unsigned char *charImage = bitmap.getContents(); 00104 00105 //Read image into bitmap's contents 00106 ifp.read((char*)charImage, 3*M*N); 00107 if (ifp.fail()) { 00108 ostringstream messageStream; 00109 messageStream << "Image " << fname << " has wrong size"; 00110 string message(messageStream.str()); 00111 throw ISpikeException(message); 00112 } 00113 00114 //Clean up 00115 ifp.close(); 00116 00117 #ifdef DEBUG 00118 LOG(LOG_DEBUG)<<"File visual reader bitmap size: "<< bitmap.size(); 00119 #endif//DEBUG 00120 00121 //Increment image id 00122 ++imageID; 00123 } 00124