- more detailed error handling for bad textures and/or models and sounds

This commit is contained in:
Mark Vejvoda 2012-11-11 04:52:26 +00:00
parent 8ba5524f35
commit 1966710628
6 changed files with 166 additions and 304 deletions

View File

@ -20,7 +20,6 @@ using std::runtime_error;
namespace Shared{ namespace Graphics{
/**Copied from pixmap.cpp*/
// =====================================================
// Structs used for BMP-reading
// =====================================================
@ -91,7 +90,7 @@ Pixmap2D* BMPReader::read(ifstream& in, const string& path, Pixmap2D* ret) const
}
if(fileHeader.type1!='B' || fileHeader.type2!='M'){
throw megaglest_runtime_error(path +" is not a bitmap");
throw megaglest_runtime_error(path +" is not a bitmap",true);
}
//read info header
@ -112,7 +111,7 @@ Pixmap2D* BMPReader::read(ifstream& in, const string& path, Pixmap2D* ret) const
}
if(infoHeader.bitCount!=24){
throw megaglest_runtime_error(path+" is not a 24 bit bitmap");
throw megaglest_runtime_error(path+" is not a 24 bit bitmap",true);
}
int h= infoHeader.height;

View File

@ -24,8 +24,6 @@
using std::runtime_error;
using std::ios;
/**Used things from CImageLoaderJPG.cpp from Irrlicht*/
namespace Shared{ namespace Graphics{
// =====================================================
@ -92,7 +90,7 @@ Pixmap2D* JPGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const
if (buffer[0] != 0xFF || buffer[1] != 0xD8) {
std::cout << "0 = [" << std::hex << (int)buffer[0] << "] 1 = [" << std::hex << (int)buffer[1] << "]" << std::endl;
delete[] buffer;
return NULL;
throw megaglest_runtime_error(path +" is not a jpeg",true);
}
struct jpeg_decompress_struct cinfo;
@ -116,7 +114,7 @@ Pixmap2D* JPGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const
if (row_pointer[0] != NULL) {
delete[] row_pointer[0];
}
return NULL;
throw megaglest_runtime_error(path +" is a corrupt(1) jpeg",true);
}
source.init_source = init_source;
@ -129,7 +127,7 @@ Pixmap2D* JPGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const
if (jpeg_read_header( &cinfo, TRUE ) != JPEG_HEADER_OK) {
delete[] buffer;
jpeg_destroy_decompress(&cinfo);
return NULL;
throw megaglest_runtime_error(path +" is a corrupt(1) jpeg",true);
}

View File

@ -22,8 +22,6 @@
using std::runtime_error;
using std::ios;
/**Used things from CImageLoaderJPG.cpp from Irrlicht*/
namespace Shared{ namespace Graphics{
// =====================================================
@ -79,24 +77,26 @@ Pixmap2D* PNGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const
if (png_sig_cmp(buffer, 0, 8) != 0) {
delete [] buffer;
return NULL; //This is not a PNG file - could be used for fast checking whether file is supported or not
//This is not a PNG file - could be used for fast checking whether file is supported or not
throw megaglest_runtime_error(path +" is not a png",true);
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
delete [] buffer;
return NULL;
throw megaglest_runtime_error(path +" is a corrupt(1) png",true);
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_read_struct(&png_ptr, (png_infopp)NULL,(png_infopp)NULL);
delete [] buffer;
return NULL;
throw megaglest_runtime_error(path +" is a corrupt(2) png",true);
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr,(png_infopp)NULL);
delete [] buffer;
return NULL; //Error during init_io
//Error during init_io
throw megaglest_runtime_error(path +" is a corrupt(3) png",true);
}
png_set_read_fn(png_ptr, &is, user_read_data);
png_set_sig_bytes(png_ptr, 8);
@ -127,7 +127,8 @@ Pixmap2D* PNGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const
if (setjmp(png_jmpbuf(png_ptr))) {
delete[] row_pointers;
delete [] buffer;
return NULL; //error during read_image
//error during read_image
throw megaglest_runtime_error(path +" is a corrupt(4) png",true);
}
for (int y = 0; y < height; ++y) {
row_pointers[y] = new png_byte[png_get_rowbytes(png_ptr, info_ptr)];
@ -224,24 +225,26 @@ Pixmap3D* PNGReader3D::read(ifstream& is, const string& path, Pixmap3D* ret) con
if (png_sig_cmp(buffer, 0, 8) != 0) {
delete [] buffer;
return NULL; //This is not a PNG file - could be used for fast checking whether file is supported or not
//This is not a PNG file - could be used for fast checking whether file is supported or not
throw megaglest_runtime_error(path +" is not a png(2)",true);
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
delete [] buffer;
return NULL;
throw megaglest_runtime_error(path +" is a corrupt(5) png",true);
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_read_struct(&png_ptr, (png_infopp)NULL,(png_infopp)NULL);
delete [] buffer;
return NULL;
throw megaglest_runtime_error(path +" is a corrupt(6) png",true);
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr,(png_infopp)NULL);
delete [] buffer;
return NULL; //Error during init_io
//Error during init_io
throw megaglest_runtime_error(path +" is a corrupt(7) png",true);
}
png_set_read_fn(png_ptr, &is, user_read_data);
png_set_sig_bytes(png_ptr, 8);
@ -271,7 +274,8 @@ Pixmap3D* PNGReader3D::read(ifstream& is, const string& path, Pixmap3D* ret) con
if (setjmp(png_jmpbuf(png_ptr))) {
delete[] row_pointers;
delete [] buffer;
return NULL; //error during read_image
//error during read_image
throw megaglest_runtime_error(path +" is a corrupt(8) png",true);
}
for (int y = 0; y < height; ++y) {
row_pointers[y] = new png_byte[png_get_rowbytes(png_ptr, info_ptr)];

View File

@ -419,13 +419,13 @@ void Mesh::loadV2(int meshIndex, const string &dir, FILE *f, TextureManager *tex
if(meshHeader.normalFrameCount != meshHeader.vertexFrameCount) {
char szBuf[8096]="";
snprintf(szBuf,8096,"Old v2 model: vertex frame count different from normal frame count [v = %d, n = %d] meshIndex = %d modelFile [%s]",meshHeader.vertexFrameCount,meshHeader.normalFrameCount,meshIndex,modelFile.c_str());
throw megaglest_runtime_error(szBuf);
throw megaglest_runtime_error(szBuf,true);
}
if(meshHeader.texCoordFrameCount != 1) {
char szBuf[8096]="";
snprintf(szBuf,8096,"Old v2 model: texture coord frame count is not 1 [t = %d] meshIndex = %d modelFile [%s]",meshHeader.texCoordFrameCount,meshIndex,modelFile.c_str());
throw megaglest_runtime_error(szBuf);
throw megaglest_runtime_error(szBuf,true);
}
//init
@ -556,7 +556,7 @@ void Mesh::loadV3(int meshIndex, const string &dir, FILE *f,
if(meshHeader.normalFrameCount != meshHeader.vertexFrameCount) {
char szBuf[8096]="";
snprintf(szBuf,8096,"Old v3 model: vertex frame count different from normal frame count [v = %d, n = %d] meshIndex = %d modelFile [%s]",meshHeader.vertexFrameCount,meshHeader.normalFrameCount,meshIndex,modelFile.c_str());
throw megaglest_runtime_error(szBuf);
throw megaglest_runtime_error(szBuf,true);
}
//init
@ -1153,7 +1153,7 @@ void Model::loadG3d(const string &path, bool deletePixMapAfterLoad,
#endif
if (f == NULL) {
printf("In [%s::%s] cannot load file = [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,path.c_str());
throw megaglest_runtime_error("Error opening g3d model file [" + path + "]");
throw megaglest_runtime_error("Error opening g3d model file [" + path + "]",true);
}
if(loadedFileList) {
@ -1177,7 +1177,7 @@ void Model::loadG3d(const string &path, bool deletePixMapAfterLoad,
fclose(f);
f = NULL;
printf("In [%s::%s] file = [%s] fileheader.id = [%s][%c]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,path.c_str(),reinterpret_cast<char*>(fileHeader.id),fileHeader.id[0]);
throw megaglest_runtime_error("Not a valid G3D model");
throw megaglest_runtime_error("Not a valid G3D model",true);
}
fileVersion= fileHeader.version;

View File

@ -110,61 +110,64 @@ PixmapIoTga::~PixmapIoTga() {
void PixmapIoTga::openRead(const string &path) {
try {
#ifdef WIN32
file= _wfopen(utf8_decode(path).c_str(), L"rb");
#else
file= fopen(path.c_str(),"rb");
#endif
if (file == NULL) {
throw megaglest_runtime_error("Can't open TGA file: "+ path);
}
#ifdef WIN32
file= _wfopen(utf8_decode(path).c_str(), L"rb");
#else
file= fopen(path.c_str(),"rb");
#endif
if (file == NULL) {
throw megaglest_runtime_error("Can't open TGA file: "+ path,true);
}
//read header
TargaFileHeader fileHeader;
size_t readBytes = fread(&fileHeader, sizeof(TargaFileHeader), 1, file);
if(readBytes != 1) {
//read header
TargaFileHeader fileHeader;
size_t readBytes = fread(&fileHeader, sizeof(TargaFileHeader), 1, file);
if(readBytes != 1) {
char szBuf[8096]="";
snprintf(szBuf,8096,"fread returned wrong size = " MG_SIZE_T_SPECIFIER " on line: %d.",readBytes,__LINE__);
throw megaglest_runtime_error(szBuf);
}
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
if(bigEndianSystem == true) {
fileHeader.bitsPerPixel = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.bitsPerPixel);
fileHeader.colourMapDepth = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.colourMapDepth);
fileHeader.colourMapLength = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.colourMapDepth);
fileHeader.colourMapOrigin = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.colourMapOrigin);
fileHeader.colourMapType = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.colourMapType);
fileHeader.dataTypeCode = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.dataTypeCode);
fileHeader.height = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.height);
fileHeader.idLength = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.idLength);
fileHeader.imageDescriptor = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.imageDescriptor);
fileHeader.width = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.width);
}
//check that we can load this tga file
if(fileHeader.idLength != 0) {
throw megaglest_runtime_error(path + ": id field is not 0",true);
}
if(fileHeader.dataTypeCode != tgaUncompressedRgb && fileHeader.dataTypeCode != tgaUncompressedBw) {
throw megaglest_runtime_error(path + ": only uncompressed BW and RGB targa images are supported",true);
}
//check bits per pixel
if(fileHeader.bitsPerPixel != 8 && fileHeader.bitsPerPixel != 24 && fileHeader.bitsPerPixel !=32) {
throw megaglest_runtime_error(path + ": only 8, 24 and 32 bit targa images are supported",true);
}
h= fileHeader.height;
w= fileHeader.width;
components= fileHeader.bitsPerPixel / 8;
}
catch(megaglest_runtime_error& ex) {
char szBuf[8096]="";
snprintf(szBuf,8096,"fread returned wrong size = " MG_SIZE_T_SPECIFIER " on line: %d.",readBytes,__LINE__);
throw megaglest_runtime_error(szBuf);
}
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
if(bigEndianSystem == true) {
fileHeader.bitsPerPixel = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.bitsPerPixel);
fileHeader.colourMapDepth = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.colourMapDepth);
fileHeader.colourMapLength = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.colourMapDepth);
fileHeader.colourMapOrigin = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.colourMapOrigin);
fileHeader.colourMapType = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.colourMapType);
fileHeader.dataTypeCode = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.dataTypeCode);
fileHeader.height = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.height);
fileHeader.idLength = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.idLength);
fileHeader.imageDescriptor = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.imageDescriptor);
fileHeader.width = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.width);
}
//check that we can load this tga file
if(fileHeader.idLength != 0) {
throw megaglest_runtime_error(path + ": id field is not 0");
}
if(fileHeader.dataTypeCode != tgaUncompressedRgb && fileHeader.dataTypeCode != tgaUncompressedBw) {
throw megaglest_runtime_error(path + ": only uncompressed BW and RGB targa images are supported");
}
//check bits per pixel
if(fileHeader.bitsPerPixel != 8 && fileHeader.bitsPerPixel != 24 && fileHeader.bitsPerPixel !=32) {
throw megaglest_runtime_error(path + ": only 8, 24 and 32 bit targa images are supported");
}
h= fileHeader.height;
w= fileHeader.width;
components= fileHeader.bitsPerPixel / 8;
snprintf(szBuf,8096,"Error in [%s] on line: %d msg: %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,ex.what());
throw megaglest_runtime_error(szBuf,!ex.wantStackTrace());
}
catch(exception& ex) {
char szBuf[8096]="";
snprintf(szBuf,8096,"Error in [%s] on line: %d msg: %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,ex.what());
throw megaglest_runtime_error(szBuf);
}
}
void PixmapIoTga::read(uint8 *pixels) {
@ -173,100 +176,103 @@ void PixmapIoTga::read(uint8 *pixels) {
void PixmapIoTga::read(uint8 *pixels, int components) {
try {
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
for(int i=0; i<h*w*components; i+=components) {
uint8 r=0, g=0, b=0, a=0, l=0;
for(int i=0; i<h*w*components; i+=components) {
uint8 r=0, g=0, b=0, a=0, l=0;
if(this->components == 1) {
size_t readBytes = fread(&l, 1, 1, file);
if(readBytes != 1) {
char szBuf[8096]="";
snprintf(szBuf,8096,"fread returned wrong size = " MG_SIZE_T_SPECIFIER " on line: %d.",readBytes,__LINE__);
throw megaglest_runtime_error(szBuf);
}
if(bigEndianSystem == true) {
l = Shared::PlatformByteOrder::fromCommonEndian(l);
}
r= l;
g= l;
b= l;
a= 255;
}
else {
size_t readBytes = fread(&b, 1, 1, file);
if(readBytes != 1) {
char szBuf[8096]="";
snprintf(szBuf,8096,"fread returned wrong size = " MG_SIZE_T_SPECIFIER " on line: %d.",readBytes,__LINE__);
throw megaglest_runtime_error(szBuf);
}
if(bigEndianSystem == true) {
b = Shared::PlatformByteOrder::fromCommonEndian(b);
}
readBytes = fread(&g, 1, 1, file);
if(readBytes != 1) {
char szBuf[8096]="";
snprintf(szBuf,8096,"fread returned wrong size = " MG_SIZE_T_SPECIFIER " on line: %d.",readBytes,__LINE__);
throw megaglest_runtime_error(szBuf);
}
if(bigEndianSystem == true) {
g = Shared::PlatformByteOrder::fromCommonEndian(g);
}
readBytes = fread(&r, 1, 1, file);
if(readBytes != 1) {
char szBuf[8096]="";
snprintf(szBuf,8096,"fread returned wrong size = " MG_SIZE_T_SPECIFIER " on line: %d.",readBytes,__LINE__);
throw megaglest_runtime_error(szBuf);
}
if(bigEndianSystem == true) {
r = Shared::PlatformByteOrder::fromCommonEndian(r);
}
if(this->components == 4) {
readBytes = fread(&a, 1, 1, file);
if(this->components == 1) {
size_t readBytes = fread(&l, 1, 1, file);
if(readBytes != 1) {
char szBuf[8096]="";
snprintf(szBuf,8096,"fread returned wrong size = " MG_SIZE_T_SPECIFIER " on line: %d.",readBytes,__LINE__);
throw megaglest_runtime_error(szBuf);
}
if(bigEndianSystem == true) {
a = Shared::PlatformByteOrder::fromCommonEndian(a);
l = Shared::PlatformByteOrder::fromCommonEndian(l);
}
}
else {
r= l;
g= l;
b= l;
a= 255;
}
l= (r+g+b)/3;
}
else {
size_t readBytes = fread(&b, 1, 1, file);
if(readBytes != 1) {
char szBuf[8096]="";
snprintf(szBuf,8096,"fread returned wrong size = " MG_SIZE_T_SPECIFIER " on line: %d.",readBytes,__LINE__);
throw megaglest_runtime_error(szBuf);
}
if(bigEndianSystem == true) {
b = Shared::PlatformByteOrder::fromCommonEndian(b);
}
switch(components) {
case 1:
pixels[i]= l;
break;
case 3:
pixels[i]= r;
pixels[i+1]= g;
pixels[i+2]= b;
break;
case 4:
pixels[i]= r;
pixels[i+1]= g;
pixels[i+2]= b;
pixels[i+3]= a;
break;
readBytes = fread(&g, 1, 1, file);
if(readBytes != 1) {
char szBuf[8096]="";
snprintf(szBuf,8096,"fread returned wrong size = " MG_SIZE_T_SPECIFIER " on line: %d.",readBytes,__LINE__);
throw megaglest_runtime_error(szBuf);
}
if(bigEndianSystem == true) {
g = Shared::PlatformByteOrder::fromCommonEndian(g);
}
readBytes = fread(&r, 1, 1, file);
if(readBytes != 1) {
char szBuf[8096]="";
snprintf(szBuf,8096,"fread returned wrong size = " MG_SIZE_T_SPECIFIER " on line: %d.",readBytes,__LINE__);
throw megaglest_runtime_error(szBuf);
}
if(bigEndianSystem == true) {
r = Shared::PlatformByteOrder::fromCommonEndian(r);
}
if(this->components == 4) {
readBytes = fread(&a, 1, 1, file);
if(readBytes != 1) {
char szBuf[8096]="";
snprintf(szBuf,8096,"fread returned wrong size = " MG_SIZE_T_SPECIFIER " on line: %d.",readBytes,__LINE__);
throw megaglest_runtime_error(szBuf);
}
if(bigEndianSystem == true) {
a = Shared::PlatformByteOrder::fromCommonEndian(a);
}
}
else {
a= 255;
}
l= (r+g+b)/3;
}
switch(components) {
case 1:
pixels[i]= l;
break;
case 3:
pixels[i]= r;
pixels[i+1]= g;
pixels[i+2]= b;
break;
case 4:
pixels[i]= r;
pixels[i+1]= g;
pixels[i+2]= b;
pixels[i+3]= a;
break;
}
}
}
catch(megaglest_runtime_error& ex) {
char szBuf[8096]="";
snprintf(szBuf,8096,"Error in [%s] on line: %d msg: %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,ex.what());
throw megaglest_runtime_error(szBuf,!ex.wantStackTrace());
}
catch(exception& ex) {
char szBuf[8096]="";
snprintf(szBuf,8096,"Error in [%s] on line: %d msg: %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,ex.what());
throw megaglest_runtime_error(szBuf);
}
}
void PixmapIoTga::openWrite(const string &path, int w, int h, int components) {
@ -280,7 +286,7 @@ void PixmapIoTga::openWrite(const string &path, int w, int h, int components) {
file= fopen(path.c_str(),"wb");
#endif
if (file == NULL) {
throw megaglest_runtime_error("Can't open TGA file: "+ path);
throw megaglest_runtime_error("Can't open TGA file: "+ path,true);
}
TargaFileHeader fileHeader;
@ -357,7 +363,7 @@ void PixmapIoBmp::openRead(const string &path){
file= fopen(path.c_str(),"rb");
#endif
if (file==NULL){
throw megaglest_runtime_error("Can't open BMP file: "+ path);
throw megaglest_runtime_error("Can't open BMP file: "+ path,true);
}
//read file header
@ -379,7 +385,7 @@ void PixmapIoBmp::openRead(const string &path){
}
if(fileHeader.type1!='B' || fileHeader.type2!='M'){
throw megaglest_runtime_error(path +" is not a bitmap");
throw megaglest_runtime_error(path +" is not a bitmap",true);
}
//read info header
@ -404,7 +410,7 @@ void PixmapIoBmp::openRead(const string &path){
infoHeader.yPelsPerMeter = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.yPelsPerMeter);
}
if(infoHeader.bitCount!=24){
throw megaglest_runtime_error(path+" is not a 24 bit bitmap");
throw megaglest_runtime_error(path+" is not a 24 bit bitmap",true);
}
h= infoHeader.height;
@ -593,31 +599,7 @@ void PixmapIoPng::openWrite(const string &path, int w, int h, int components) {
}
void PixmapIoPng::write(uint8 *pixels) {
// initialize stuff
/*
std::auto_ptr<png_byte*> imrow(new png_byte*[h]);
for(int i = 0; i < h; ++i) {
imrow.get()[i] = pixels+(h-1-i) * w * components;
}
png_structp imgp = png_create_write_struct(PNG_LIBPNG_VER_STRING,0,0,0);
png_infop infop = png_create_info_struct(imgp);
png_init_io(imgp, file);
int color_type = PNG_COLOR_TYPE_RGB;
if(components == 4) {
color_type = PNG_COLOR_TYPE_RGBA;
}
png_set_IHDR(imgp, infop, w, h,
8, color_type, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
// write file
png_write_info(imgp, infop);
png_write_image(imgp, imrow.get());
png_write_end(imgp, NULL);
*/
png_bytep *imrow = new png_bytep[h];
//png_bytep *imrow = (png_bytep*) malloc(sizeof(png_bytep) * height);
for(int i = 0; i < h; ++i) {
@ -642,128 +624,8 @@ void PixmapIoPng::write(uint8 *pixels) {
png_write_end(imgp, NULL);
delete [] imrow;
/*
// Allocate write & info structures
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!png_ptr) {
fclose(file);
throw megaglest_runtime_error("OpenGlDevice::saveImageAsPNG() - out of memory creating write structure");
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr) {
png_destroy_write_struct(&png_ptr,
(png_infopp)NULL);
fclose(file);
throw megaglest_runtime_error("OpenGlDevice::saveImageAsPNG() - out of memery creating info structure");
}
// setjmp() must be called in every function that calls a PNG-writing
// libpng function, unless an alternate error handler was installed--
// but compatible error handlers must either use longjmp() themselves
// (as in this program) or exit immediately, so here we go:
if(setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(file);
throw megaglest_runtime_error("OpenGlDevice::saveImageAsPNG() - setjmp problem");
}
// make sure outfile is (re)opened in BINARY mode
png_init_io(png_ptr, file);
// set the compression levels--in general, always want to leave filtering
// turned on (except for palette images) and allow all of the filters,
// which is the default; want 32K zlib window, unless entire image buffer
// is 16K or smaller (unknown here)--also the default; usually want max
// compression (NOT the default); and remaining compression flags should
// be left alone
//png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
png_set_compression_level(png_ptr, Z_DEFAULT_COMPRESSION);
//
// this is default for no filtering; Z_FILTERED is default otherwise:
// png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY);
// these are all defaults:
// png_set_compression_mem_level(png_ptr, 8);
// png_set_compression_window_bits(png_ptr, 15);
// png_set_compression_method(png_ptr, 8);
// Set some options: color_type, interlace_type
int color_type=0, interlace_type=0, numChannels=0;
// color_type = PNG_COLOR_TYPE_GRAY;
// color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
color_type = PNG_COLOR_TYPE_RGBA;
numChannels = 4;
// color_type = PNG_COLOR_TYPE_RGB_ALPHA;
interlace_type = PNG_INTERLACE_NONE;
// interlace_type = PNG_INTERLACE_ADAM7;
int bit_depth = 8;
png_set_IHDR(png_ptr, info_ptr, this->w, this->h,
bit_depth,
color_type,
interlace_type,
PNG_COMPRESSION_TYPE_BASE,
PNG_FILTER_TYPE_BASE);
// Optional gamma chunk is strongly suggested if you have any guess
// as to the correct gamma of the image. (we don't have a guess)
//
// png_set_gAMA(png_ptr, info_ptr, image_gamma);
//png_set_strip_alpha(png_ptr);
// write all chunks up to (but not including) first IDAT
png_write_info(png_ptr, info_ptr);
//png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL );
// set up the row pointers for the image so we can use png_write_image
png_bytep* row_pointers = new png_bytep[this->h];
if (row_pointers == 0) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(file);
throw megaglest_runtime_error("OpenGlDevice::failed to allocate memory for row pointers");
}
unsigned int row_stride = this->w * numChannels;
unsigned char *rowptr = (unsigned char*) pixels;
for (int row = this->h-1; row >=0 ; row--) {
row_pointers[row] = rowptr;
rowptr += row_stride;
}
// now we just write the whole image; libpng takes care of interlacing for us
png_write_image(png_ptr, row_pointers);
// since that's it, we also close out the end of the PNG file now--if we
// had any text or time info to write after the IDATs, second argument
// would be info_ptr, but we optimize slightly by sending NULL pointer:
png_write_end(png_ptr, info_ptr);
//
// clean up after the write
// free any memory allocated & close the file
//
png_destroy_write_struct(&png_ptr, &info_ptr);
delete [] row_pointers;
*/
}
// =====================================================
// class PixmapIoJpg
// =====================================================
@ -780,7 +642,6 @@ PixmapIoJpg::~PixmapIoJpg() {
}
void PixmapIoJpg::openRead(const string &path) {
throw megaglest_runtime_error("PixmapIoJpg::openRead not implemented!");
}

View File

@ -40,7 +40,7 @@ void WavSoundFileLoader::open(const string &path, SoundInfo *soundInfo){
f.open(path.c_str(), ios_base::in | ios_base::binary);
if(!f.is_open()){
throw megaglest_runtime_error("Error opening wav file: "+ string(path));
throw megaglest_runtime_error("Error opening wav file: "+ string(path),true);
}
//RIFF chunk - Id
@ -53,7 +53,7 @@ void WavSoundFileLoader::open(const string &path, SoundInfo *soundInfo){
}
if(strcmp(chunkId, "RIFF")!=0){
throw megaglest_runtime_error("Not a valid wav file (first four bytes are not RIFF):" + path);
throw megaglest_runtime_error("Not a valid wav file (first four bytes are not RIFF):" + path,true);
}
//RIFF chunk - Size
@ -71,7 +71,7 @@ void WavSoundFileLoader::open(const string &path, SoundInfo *soundInfo){
}
if(strcmp(chunkId, "WAVE")!=0){
throw megaglest_runtime_error("Not a valid wav file (wave data don't start by WAVE): " + path);
throw megaglest_runtime_error("Not a valid wav file (wave data don't start by WAVE): " + path,true);
}
// === HEADER ===
@ -85,7 +85,7 @@ void WavSoundFileLoader::open(const string &path, SoundInfo *soundInfo){
}
if(strcmp(chunkId, "fmt ")!=0){
throw megaglest_runtime_error("Not a valid wav file (first sub-chunk Id is not fmt): "+ path);
throw megaglest_runtime_error("Not a valid wav file (first sub-chunk Id is not fmt): "+ path,true);
}
//first sub-chunk (header) - Size
@ -139,7 +139,7 @@ void WavSoundFileLoader::open(const string &path, SoundInfo *soundInfo){
soundInfo->setBitRate(soundInfo->getSamplesPerSecond() * soundInfo->getChannels() * soundInfo->getBitsPerSample() / 8);
if (soundInfo->getBitsPerSample() != 8 && soundInfo->getBitsPerSample()!=16){
throw megaglest_runtime_error("Bits per sample must be 8 or 16: " + path);
throw megaglest_runtime_error("Bits per sample must be 8 or 16: " + path,true);
}
bytesPerSecond= soundInfo->getBitsPerSample()*8*soundInfo->getSamplesPerSecond()*soundInfo->getChannels();
@ -172,7 +172,7 @@ void WavSoundFileLoader::open(const string &path, SoundInfo *soundInfo){
while(strncmp(chunkId, "data", 4)!=0 && count<maxDataRetryCount);
if(f.bad() || count==maxDataRetryCount){
throw megaglest_runtime_error("Error reading samples: "+ path);
throw megaglest_runtime_error("Error reading samples: "+ path,true);
}
dataOffset= (uint32)f.tellg();
@ -210,19 +210,19 @@ void OggSoundFileLoader::open(const string &path, SoundInfo *soundInfo){
f= fopen(path.c_str(), "rb");
#endif
if(f==NULL){
throw megaglest_runtime_error("Can't open ogg file: "+path);
throw megaglest_runtime_error("Can't open ogg file: "+path,true);
}
vf= new OggVorbis_File();
if(vf==NULL) {
throw megaglest_runtime_error("Can't create ogg object for file: "+path);
throw megaglest_runtime_error("Can't create ogg object for file: "+path,true);
}
ov_open(f, vf, NULL, 0);
vorbis_info *vi= ov_info(vf, -1);
if(vi==NULL) {
throw megaglest_runtime_error("Can't read ogg header info for file: "+path);
throw megaglest_runtime_error("Can't read ogg header info for file: "+path,true);
}
uint32 samples = static_cast<uint32>(ov_pcm_total(vf, -1));