bugfixes for static init filereader concrete classes
This commit is contained in:
parent
b22adbfa02
commit
844ca894b4
|
@ -20,6 +20,7 @@
|
|||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <typeinfo>
|
||||
|
||||
using std::map;
|
||||
using std::string;
|
||||
|
@ -30,6 +31,8 @@ using std::runtime_error;
|
|||
|
||||
using Shared::Platform::extractExtension;
|
||||
|
||||
#define AS_STRING(...) #__VA_ARGS__
|
||||
|
||||
namespace Shared{
|
||||
|
||||
// =====================================================
|
||||
|
@ -38,7 +41,7 @@ namespace Shared{
|
|||
|
||||
template <class T>
|
||||
class FileReader {
|
||||
protected:
|
||||
public:
|
||||
string const * extensions;
|
||||
|
||||
/**Creates a filereader being able to possibly load files
|
||||
|
@ -46,6 +49,10 @@ protected:
|
|||
**/
|
||||
FileReader(string const * extensions);
|
||||
|
||||
/**Creates a low-priority filereader
|
||||
**/
|
||||
FileReader();
|
||||
|
||||
public:
|
||||
/*Return the - existing and initialized - fileReadersMap
|
||||
*/
|
||||
|
@ -53,7 +60,16 @@ public:
|
|||
static map<string, vector<FileReader<T> const * >* > 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:
|
||||
|
@ -125,13 +141,15 @@ public:
|
|||
template <typename T>
|
||||
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) {
|
||||
T* ret = NULL;
|
||||
try {
|
||||
FileReader<T> const * reader = *i;
|
||||
T* ret = reader->read(filepath); //It is guaranteed that at least the filepath matches ...
|
||||
if (ret != NULL) {
|
||||
return ret;
|
||||
}
|
||||
ret = reader->read(filepath); //It is guaranteed that at least the filepath matches ...
|
||||
} catch (...) { //TODO: Specific exceptions
|
||||
continue;
|
||||
}
|
||||
if (ret != NULL) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
|
@ -140,16 +158,17 @@ static inline T* readFromFileReaders(vector<FileReader<T> const *>* readers, con
|
|||
template <typename T>
|
||||
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) {
|
||||
T* ret = NULL;
|
||||
try {
|
||||
FileReader<T> const * reader = *i;
|
||||
T* ret = reader->read(filepath, object); //It is guaranteed that at least the filepath matches ...
|
||||
if (ret != NULL) {
|
||||
return ret;
|
||||
}
|
||||
ret = reader->read(filepath, object); //It is guaranteed that at least the filepath matches ...
|
||||
} catch (...) { //TODO: Specific exceptions
|
||||
continue;
|
||||
}
|
||||
if (ret != NULL) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
std::cerr << "Could not parse filepath: " << filepath << std::endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -168,7 +187,11 @@ T* FileReader<T>::readPath(const string& filepath) {
|
|||
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;
|
||||
}
|
||||
|
||||
|
@ -187,17 +210,20 @@ T* FileReader<T>::readPath(const string& filepath, T* object) {
|
|||
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;
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
vector<FileReader<T> const * > FileReader<T>::fileReaders;
|
||||
|
||||
template <typename T>
|
||||
FileReader<T>::FileReader(string const * extensions): extensions(extensions) {
|
||||
fileReaders.push_back(this);
|
||||
getFileReaders().push_back(this);
|
||||
string const * nextExtension = extensions;
|
||||
while (((*nextExtension) != "")) {
|
||||
vector<FileReader<T> const* >* curPossibleReaders = (getFileReadersMap())[*nextExtension];
|
||||
|
|
|
@ -81,6 +81,7 @@ Pixmap2D* BMPReader::read(ifstream& in, const string& path, Pixmap2D* ret) const
|
|||
int h= infoHeader.height;
|
||||
int w= infoHeader.width;
|
||||
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);
|
||||
uint8* pixels = ret->getPixels();
|
||||
for(int i=0; i<h*w*components; i+=components){
|
||||
|
|
|
@ -65,9 +65,18 @@ Pixmap2D* JPGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const
|
|||
//Read file
|
||||
is.seekg(0, ios::end);
|
||||
size_t length = is.tellg();
|
||||
if (length < 8) {
|
||||
return NULL;
|
||||
}
|
||||
is.seekg(0, ios::beg);
|
||||
uint8 * buffer = new uint8[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_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 space: " << cinfo.jpeg_color_space << std::endl;*/
|
||||
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);
|
||||
uint8* pixels = ret->getPixels();
|
||||
//TODO: Irrlicht has some special CMYK-handling - maybe needed too?
|
||||
|
|
|
@ -108,6 +108,7 @@ Pixmap2D* PNGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const
|
|||
png_read_image(png_ptr, row_pointers);
|
||||
int fileComponents = info_ptr->rowbytes/info_ptr->width;
|
||||
int picComponents = (ret->getComponents()==-1)?fileComponents:ret->getComponents();
|
||||
std::cout << "PNG-Components: Pic: " << picComponents << " old: " << (ret->getComponents()) << " File: " << fileComponents << std::endl;
|
||||
//Copy image
|
||||
ret->init(width,height,picComponents);
|
||||
uint8* pixels = ret->getPixels();
|
||||
|
|
|
@ -85,6 +85,7 @@ Pixmap2D* TGAReader::read(ifstream& in, const string& path, Pixmap2D* ret) const
|
|||
const int w = fileHeader.width;
|
||||
const int fileComponents= fileHeader.bitsPerPixel/8;
|
||||
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);
|
||||
uint8* pixels = ret->getPixels();
|
||||
//read file
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
#include "opengl.h"
|
||||
#include "leak_dumper.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
@ -47,6 +48,7 @@ GLint toFormatGl(Texture::Format format, int components){
|
|||
case 4:
|
||||
return GL_RGBA;
|
||||
default:
|
||||
std::cerr << "Components = " << (components) << std::endl;
|
||||
assert(false);
|
||||
return GL_RGBA;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user