bugfixes for static init filereader concrete classes

This commit is contained in:
Mark Vejvoda
2010-03-24 22:50:45 +00:00
parent b22adbfa02
commit 844ca894b4
6 changed files with 59 additions and 18 deletions

View File

@@ -20,6 +20,7 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <typeinfo>
using std::map; using std::map;
using std::string; using std::string;
@@ -30,6 +31,8 @@ using std::runtime_error;
using Shared::Platform::extractExtension; using Shared::Platform::extractExtension;
#define AS_STRING(...) #__VA_ARGS__
namespace Shared{ namespace Shared{
// ===================================================== // =====================================================
@@ -38,7 +41,7 @@ namespace Shared{
template <class T> template <class T>
class FileReader { class FileReader {
protected: public:
string const * extensions; string const * extensions;
/**Creates a filereader being able to possibly load files /**Creates a filereader being able to possibly load files
@@ -46,6 +49,10 @@ protected:
**/ **/
FileReader(string const * extensions); FileReader(string const * extensions);
/**Creates a low-priority filereader
**/
FileReader();
public: public:
/*Return the - existing and initialized - fileReadersMap /*Return the - existing and initialized - fileReadersMap
*/ */
@@ -53,7 +60,16 @@ public:
static map<string, vector<FileReader<T> const * >* > fileReaderByExtension; static map<string, vector<FileReader<T> const * >* > fileReaderByExtension;
return fileReaderByExtension; return fileReaderByExtension;
} }
static vector<FileReader<T> const * > fileReaders;
static vector<FileReader<T> const * >& getFileReaders() {
static vector<FileReader<T> const*> fileReaders;
return fileReaders;
};
static vector<FileReader<T> const * >& getLowPriorityFileReaders() {
static vector<FileReader<T> const*> lowPriorityFileReaders;
return lowPriorityFileReaders;
};
public: public:
@@ -125,14 +141,16 @@ public:
template <typename T> template <typename T>
static inline T* readFromFileReaders(vector<FileReader<T> const *>* readers, const string& filepath) { static inline T* readFromFileReaders(vector<FileReader<T> const *>* readers, const string& filepath) {
for (typename vector<FileReader<T> const *>::const_iterator i = readers->begin(); i != readers->end(); ++i) { for (typename vector<FileReader<T> const *>::const_iterator i = readers->begin(); i != readers->end(); ++i) {
T* ret = NULL;
try { try {
FileReader<T> const * reader = *i; FileReader<T> const * reader = *i;
T* ret = reader->read(filepath); //It is guaranteed that at least the filepath matches ... ret = reader->read(filepath); //It is guaranteed that at least the filepath matches ...
if (ret != NULL) {
return ret;
}
} catch (...) { //TODO: Specific exceptions } catch (...) { //TODO: Specific exceptions
continue;
} }
if (ret != NULL) {
return ret;
}
} }
return NULL; return NULL;
} }
@@ -140,16 +158,17 @@ static inline T* readFromFileReaders(vector<FileReader<T> const *>* readers, con
template <typename T> template <typename T>
static inline T* readFromFileReaders(vector<FileReader<T> const *>* readers, const string& filepath, T* object) { static inline T* readFromFileReaders(vector<FileReader<T> const *>* readers, const string& filepath, T* object) {
for (typename vector<FileReader<T> const *>::const_iterator i = readers->begin(); i != readers->end(); ++i) { for (typename vector<FileReader<T> const *>::const_iterator i = readers->begin(); i != readers->end(); ++i) {
T* ret = NULL;
try { try {
FileReader<T> const * reader = *i; FileReader<T> const * reader = *i;
T* ret = reader->read(filepath, object); //It is guaranteed that at least the filepath matches ... ret = reader->read(filepath, object); //It is guaranteed that at least the filepath matches ...
if (ret != NULL) {
return ret;
}
} catch (...) { //TODO: Specific exceptions } catch (...) { //TODO: Specific exceptions
continue;
} }
if (ret != NULL) {
return ret;
}
} }
std::cerr << "Could not parse filepath: " << filepath << std::endl;
return NULL; return NULL;
} }
@@ -168,7 +187,11 @@ T* FileReader<T>::readPath(const string& filepath) {
return ret; return ret;
} }
} }
T* ret = readFromFileReaders(&fileReaders, filepath); //Try all other T* ret = readFromFileReaders(&(getFileReaders()), filepath); //Try all other
if (ret == NULL) {
std::cerr << "Could not parse filepath: " << filepath << std::endl;
ret = readFromFileReaders(&(getLowPriorityFileReaders()), filepath); //Try to get dummy file
}
return ret; return ret;
} }
@@ -187,17 +210,20 @@ T* FileReader<T>::readPath(const string& filepath, T* object) {
return ret; return ret;
} }
} }
T* ret = readFromFileReaders(&fileReaders, filepath, object); //Try all other T* ret = readFromFileReaders(&(getFileReaders()), filepath, object); //Try all other
if (ret == NULL) {
std::cerr << "Could not parse filepath: " << filepath << std::endl;
ret = readFromFileReaders(&(getLowPriorityFileReaders()), filepath); //Try to get dummy file
if (ret == NULL) {
throw runtime_error(string("Could not parse ") + filepath + " as object of type " + typeid(T).name());
}
}
return ret; return ret;
} }
template<typename T>
vector<FileReader<T> const * > FileReader<T>::fileReaders;
template <typename T> template <typename T>
FileReader<T>::FileReader(string const * extensions): extensions(extensions) { FileReader<T>::FileReader(string const * extensions): extensions(extensions) {
fileReaders.push_back(this); getFileReaders().push_back(this);
string const * nextExtension = extensions; string const * nextExtension = extensions;
while (((*nextExtension) != "")) { while (((*nextExtension) != "")) {
vector<FileReader<T> const* >* curPossibleReaders = (getFileReadersMap())[*nextExtension]; vector<FileReader<T> const* >* curPossibleReaders = (getFileReadersMap())[*nextExtension];

View File

@@ -81,6 +81,7 @@ Pixmap2D* BMPReader::read(ifstream& in, const string& path, Pixmap2D* ret) const
int h= infoHeader.height; int h= infoHeader.height;
int w= infoHeader.width; int w= infoHeader.width;
int components= (ret->getComponents() == -1)?3:ret->getComponents(); int components= (ret->getComponents() == -1)?3:ret->getComponents();
std::cout << "BMP-Components: Pic: " << components << " old: " << (ret->getComponents()) << " File: " << 3 << std::endl;
ret->init(w,h,components); ret->init(w,h,components);
uint8* pixels = ret->getPixels(); uint8* pixels = ret->getPixels();
for(int i=0; i<h*w*components; i+=components){ for(int i=0; i<h*w*components; i+=components){

View File

@@ -65,9 +65,18 @@ Pixmap2D* JPGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const
//Read file //Read file
is.seekg(0, ios::end); is.seekg(0, ios::end);
size_t length = is.tellg(); size_t length = is.tellg();
if (length < 8) {
return NULL;
}
is.seekg(0, ios::beg); is.seekg(0, ios::beg);
uint8 * buffer = new uint8[length]; uint8 * buffer = new uint8[length];
is.read((char*)buffer, length); is.read((char*)buffer, length);
//Check buffer (weak jpeg check)
if (buffer[0] != 0x46 || buffer[1] != 0xA0) {
delete[] buffer;
std::cout << "Returning NULL jpeg" << std::endl;
return NULL;
}
struct jpeg_decompress_struct cinfo; struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr; struct jpeg_error_mgr jerr;
@@ -112,6 +121,7 @@ Pixmap2D* JPGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const
std::cout << "Color components per fixel: " << cinfo.num_components << std::endl; std::cout << "Color components per fixel: " << cinfo.num_components << std::endl;
std::cout << "Color space: " << cinfo.jpeg_color_space << std::endl;*/ std::cout << "Color space: " << cinfo.jpeg_color_space << std::endl;*/
const int picComponents = (ret->getComponents() == -1)?cinfo.num_components:ret->getComponents(); const int picComponents = (ret->getComponents() == -1)?cinfo.num_components:ret->getComponents();
std::cout << "JPG-Components: Pic: " << picComponents << " old: " << (ret->getComponents()) << " File: " << cinfo.num_components << std::endl;
ret->init(cinfo.image_width, cinfo.image_height, picComponents); ret->init(cinfo.image_width, cinfo.image_height, picComponents);
uint8* pixels = ret->getPixels(); uint8* pixels = ret->getPixels();
//TODO: Irrlicht has some special CMYK-handling - maybe needed too? //TODO: Irrlicht has some special CMYK-handling - maybe needed too?

View File

@@ -108,6 +108,7 @@ Pixmap2D* PNGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const
png_read_image(png_ptr, row_pointers); png_read_image(png_ptr, row_pointers);
int fileComponents = info_ptr->rowbytes/info_ptr->width; int fileComponents = info_ptr->rowbytes/info_ptr->width;
int picComponents = (ret->getComponents()==-1)?fileComponents:ret->getComponents(); int picComponents = (ret->getComponents()==-1)?fileComponents:ret->getComponents();
std::cout << "PNG-Components: Pic: " << picComponents << " old: " << (ret->getComponents()) << " File: " << fileComponents << std::endl;
//Copy image //Copy image
ret->init(width,height,picComponents); ret->init(width,height,picComponents);
uint8* pixels = ret->getPixels(); uint8* pixels = ret->getPixels();

View File

@@ -85,6 +85,7 @@ Pixmap2D* TGAReader::read(ifstream& in, const string& path, Pixmap2D* ret) const
const int w = fileHeader.width; const int w = fileHeader.width;
const int fileComponents= fileHeader.bitsPerPixel/8; const int fileComponents= fileHeader.bitsPerPixel/8;
const int picComponents = (ret->getComponents()==-1)?fileComponents:ret->getComponents(); const int picComponents = (ret->getComponents()==-1)?fileComponents:ret->getComponents();
std::cout << "BMP-Components: Pic: " << picComponents << " old: " << (ret->getComponents()) << " File: " << fileComponents << std::endl;
ret->init(w,h,picComponents); ret->init(w,h,picComponents);
uint8* pixels = ret->getPixels(); uint8* pixels = ret->getPixels();
//read file //read file

View File

@@ -15,6 +15,7 @@
#include "opengl.h" #include "opengl.h"
#include "leak_dumper.h" #include "leak_dumper.h"
#include <iostream>
using namespace std; using namespace std;
@@ -47,6 +48,7 @@ GLint toFormatGl(Texture::Format format, int components){
case 4: case 4:
return GL_RGBA; return GL_RGBA;
default: default:
std::cerr << "Components = " << (components) << std::endl;
assert(false); assert(false);
return GL_RGBA; return GL_RGBA;
} }