- added byte order logic for loading textures and sounds

This commit is contained in:
Mark Vejvoda 2012-11-01 23:07:51 +00:00
parent 00e075df97
commit 7243a22abc
6 changed files with 301 additions and 6 deletions

View File

@ -80,15 +80,41 @@ Pixmap2D* BMPReader::read(ifstream& in, const string& path, Pixmap2D* ret) const
//read file header
BitmapFileHeader fileHeader;
in.read((char*)&fileHeader, sizeof(BitmapFileHeader));
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
if(bigEndianSystem == true) {
fileHeader.offsetBits = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.offsetBits);
fileHeader.reserved1 = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.reserved1);
fileHeader.reserved2 = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.reserved2);
fileHeader.size = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.size);
fileHeader.type1 = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.type1);
fileHeader.type2 = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.type2);
}
if(fileHeader.type1!='B' || fileHeader.type2!='M'){
throw megaglest_runtime_error(path +" is not a bitmap");
}
//read info header
//read info header
BitmapInfoHeader infoHeader;
in.read((char*)&infoHeader, sizeof(BitmapInfoHeader));
if(bigEndianSystem == true) {
infoHeader.bitCount = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.bitCount);
infoHeader.clrImportant = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.clrImportant);
infoHeader.clrUsed = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.clrUsed);
infoHeader.compression = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.compression);
infoHeader.height = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.height);
infoHeader.planes = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.planes);
infoHeader.size = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.size);
infoHeader.sizeImage = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.sizeImage);
infoHeader.width = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.width);
infoHeader.xPelsPerMeter = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.xPelsPerMeter);
infoHeader.yPelsPerMeter = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.yPelsPerMeter);
}
if(infoHeader.bitCount!=24){
throw megaglest_runtime_error(path+" is not a 24 bit bitmap");
}
int h= infoHeader.height;
int w= infoHeader.width;
int components= (ret->getComponents() == -1)?3:ret->getComponents();
@ -104,8 +130,20 @@ Pixmap2D* BMPReader::read(ifstream& in, const string& path, Pixmap2D* ret) const
for (int x = 0; x < w; ++x, i+=components) {
uint8 r, g, b;
in.read((char*)&b, 1);
if(bigEndianSystem == true) {
b = Shared::PlatformByteOrder::fromCommonEndian(b);
}
in.read((char*)&g, 1);
if(bigEndianSystem == true) {
g = Shared::PlatformByteOrder::fromCommonEndian(g);
}
in.read((char*)&r, 1);
if(bigEndianSystem == true) {
r = Shared::PlatformByteOrder::fromCommonEndian(r);
}
if (!in.good()) {
return NULL;
}

View File

@ -82,6 +82,10 @@ Pixmap2D* JPGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const
is.seekg(0, ios::beg);
uint8 *buffer = new uint8[(unsigned int)length];
is.read((char*)buffer, (std::streamsize)length);
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
if(bigEndianSystem == true) {
Shared::PlatformByteOrder::fromEndianTypeArray<uint8>(buffer,length);
}
//Check buffer (weak jpeg check)
//if (buffer[0] != 0x46 || buffer[1] != 0xA0) {
// Proper header check found from: http://www.fastgraph.com/help/jpeg_header_format.html

View File

@ -33,6 +33,11 @@ namespace Shared{ namespace Graphics{
static void user_read_data(png_structp read_ptr, png_bytep data, png_size_t length) {
ifstream& is = *((ifstream*)png_get_io_ptr(read_ptr));
is.read((char*)data,(std::streamsize)length);
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
if(bigEndianSystem == true) {
Shared::PlatformByteOrder::fromEndianTypeArray<png_byte>(data,length);
}
if (!is.good()) {
png_error(read_ptr,"Could not read from png-file");
}
@ -67,6 +72,10 @@ Pixmap2D* PNGReader::read(ifstream& is, const string& path, Pixmap2D* ret) const
is.seekg(0, ios::beg);
uint8 *buffer = new uint8[8];
is.read((char*)buffer, 8);
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
if(bigEndianSystem == true) {
Shared::PlatformByteOrder::fromEndianTypeArray<uint8>(buffer,8);
}
if (png_sig_cmp(buffer, 0, 8) != 0) {
delete [] buffer;
@ -208,6 +217,10 @@ Pixmap3D* PNGReader3D::read(ifstream& is, const string& path, Pixmap3D* ret) con
is.seekg(0, ios::beg);
uint8 *buffer = new uint8[8];
is.read((char*)buffer, 8);
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
if(bigEndianSystem == true) {
Shared::PlatformByteOrder::fromEndianTypeArray<uint8>(buffer,8);
}
if (png_sig_cmp(buffer, 0, 8) != 0) {
delete [] buffer;

View File

@ -70,6 +70,20 @@ Pixmap3D* TGAReader3D::read(ifstream& in, const string& path, Pixmap3D* ret) con
//read header
TargaFileHeader fileHeader;
in.read((char*)&fileHeader, sizeof(TargaFileHeader));
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);
}
if (!in.good()) {
throw megaglest_runtime_error(path + " could not be read");
}
@ -108,6 +122,10 @@ Pixmap3D* TGAReader3D::read(ifstream& in, const string& path, Pixmap3D* ret) con
if(fileComponents==1){
in.read((char*)&l,1);
if(bigEndianSystem == true) {
l = Shared::PlatformByteOrder::fromCommonEndian(l);
}
r= l;
g= l;
b= l;
@ -115,10 +133,26 @@ Pixmap3D* TGAReader3D::read(ifstream& in, const string& path, Pixmap3D* ret) con
}
else{
in.read((char*)&b, 1);
if(bigEndianSystem == true) {
b = Shared::PlatformByteOrder::fromCommonEndian(b);
}
in.read((char*)&g, 1);
if(bigEndianSystem == true) {
g = Shared::PlatformByteOrder::fromCommonEndian(g);
}
in.read((char*)&r, 1);
if(bigEndianSystem == true) {
r = Shared::PlatformByteOrder::fromCommonEndian(r);
}
if(fileComponents==4){
in.read((char*)&a, 1);
if(bigEndianSystem == true) {
a = Shared::PlatformByteOrder::fromCommonEndian(a);
}
} else {
a= 255;
}
@ -169,6 +203,20 @@ Pixmap2D* TGAReader::read(ifstream& in, const string& path, Pixmap2D* ret) const
//read header
TargaFileHeader fileHeader;
in.read((char*)&fileHeader, sizeof(TargaFileHeader));
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);
}
if (!in.good()) {
throw megaglest_runtime_error(path + " could not be read");
}
@ -200,6 +248,10 @@ Pixmap2D* TGAReader::read(ifstream& in, const string& path, Pixmap2D* ret) const
if(fileComponents==1){
in.read((char*)&l,1);
if(bigEndianSystem == true) {
l = Shared::PlatformByteOrder::fromCommonEndian(l);
}
r= l;
g= l;
b= l;
@ -207,10 +259,26 @@ Pixmap2D* TGAReader::read(ifstream& in, const string& path, Pixmap2D* ret) const
}
else{
in.read((char*)&b, 1);
if(bigEndianSystem == true) {
b = Shared::PlatformByteOrder::fromCommonEndian(b);
}
in.read((char*)&g, 1);
if(bigEndianSystem == true) {
g = Shared::PlatformByteOrder::fromCommonEndian(g);
}
in.read((char*)&r, 1);
if(bigEndianSystem == true) {
r = Shared::PlatformByteOrder::fromCommonEndian(r);
}
if(fileComponents==4){
in.read((char*)&a, 1);
if(bigEndianSystem == true) {
a = Shared::PlatformByteOrder::fromCommonEndian(a);
}
} else {
a= 255;
}

View File

@ -125,7 +125,19 @@ void PixmapIoTga::openRead(const string &path) {
snprintf(szBuf,8096,"fread returned wrong size = %zu 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");
@ -150,6 +162,8 @@ void PixmapIoTga::read(uint8 *pixels) {
}
void PixmapIoTga::read(uint8 *pixels, int components) {
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;
@ -160,7 +174,9 @@ void PixmapIoTga::read(uint8 *pixels, int components) {
snprintf(szBuf,8096,"fread returned wrong size = %zu on line: %d.",readBytes,__LINE__);
throw megaglest_runtime_error(szBuf);
}
if(bigEndianSystem == true) {
l = Shared::PlatformByteOrder::fromCommonEndian(l);
}
r= l;
g= l;
b= l;
@ -173,6 +189,9 @@ void PixmapIoTga::read(uint8 *pixels, int components) {
snprintf(szBuf,8096,"fread returned wrong size = %zu 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) {
@ -180,6 +199,9 @@ void PixmapIoTga::read(uint8 *pixels, int components) {
snprintf(szBuf,8096,"fread returned wrong size = %zu 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) {
@ -187,6 +209,9 @@ void PixmapIoTga::read(uint8 *pixels, int components) {
snprintf(szBuf,8096,"fread returned wrong size = %zu 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);
@ -195,6 +220,9 @@ void PixmapIoTga::read(uint8 *pixels, int components) {
snprintf(szBuf,8096,"fread returned wrong size = %zu on line: %d.",readBytes,__LINE__);
throw megaglest_runtime_error(szBuf);
}
if(bigEndianSystem == true) {
a = Shared::PlatformByteOrder::fromCommonEndian(a);
}
}
else {
@ -244,14 +272,39 @@ void PixmapIoTga::openWrite(const string &path, int w, int h, int components) {
fileHeader.height= h;
fileHeader.imageDescriptor= components==4? 8: 0;
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
if(bigEndianSystem == true) {
fileHeader.bitsPerPixel = Shared::PlatformByteOrder::toCommonEndian(fileHeader.bitsPerPixel);
fileHeader.colourMapDepth = Shared::PlatformByteOrder::toCommonEndian(fileHeader.colourMapDepth);
fileHeader.colourMapLength = Shared::PlatformByteOrder::toCommonEndian(fileHeader.colourMapDepth);
fileHeader.colourMapOrigin = Shared::PlatformByteOrder::toCommonEndian(fileHeader.colourMapOrigin);
fileHeader.colourMapType = Shared::PlatformByteOrder::toCommonEndian(fileHeader.colourMapType);
fileHeader.dataTypeCode = Shared::PlatformByteOrder::toCommonEndian(fileHeader.dataTypeCode);
fileHeader.height = Shared::PlatformByteOrder::toCommonEndian(fileHeader.height);
fileHeader.idLength = Shared::PlatformByteOrder::toCommonEndian(fileHeader.idLength);
fileHeader.imageDescriptor = Shared::PlatformByteOrder::toCommonEndian(fileHeader.imageDescriptor);
fileHeader.width = Shared::PlatformByteOrder::toCommonEndian(fileHeader.width);
}
fwrite(&fileHeader, sizeof(TargaFileHeader), 1, file);
}
void PixmapIoTga::write(uint8 *pixels) {
if(components == 1) {
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
if(bigEndianSystem == true) {
Shared::PlatformByteOrder::toEndianTypeArray<uint8>(pixels,h*w);
}
fwrite(pixels, h*w, 1, file);
}
else {
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
if(bigEndianSystem == true) {
Shared::PlatformByteOrder::toEndianTypeArray<uint8>(pixels,h*w*components);
}
for(int i=0; i<h*w*components; i+=components) {
fwrite(&pixels[i+2], 1, 1, file);
fwrite(&pixels[i+1], 1, 1, file);
@ -296,6 +349,15 @@ void PixmapIoBmp::openRead(const string &path){
snprintf(szBuf,8096,"fread returned wrong size = %zu on line: %d.",readBytes,__LINE__);
throw megaglest_runtime_error(szBuf);
}
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
if(bigEndianSystem == true) {
fileHeader.offsetBits = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.offsetBits);
fileHeader.reserved1 = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.reserved1);
fileHeader.reserved2 = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.reserved2);
fileHeader.size = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.size);
fileHeader.type1 = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.type1);
fileHeader.type2 = Shared::PlatformByteOrder::fromCommonEndian(fileHeader.type2);
}
if(fileHeader.type1!='B' || fileHeader.type2!='M'){
throw megaglest_runtime_error(path +" is not a bitmap");
@ -309,7 +371,19 @@ void PixmapIoBmp::openRead(const string &path){
snprintf(szBuf,8096,"fread returned wrong size = %zu on line: %d.",readBytes,__LINE__);
throw megaglest_runtime_error(szBuf);
}
if(bigEndianSystem == true) {
infoHeader.bitCount = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.bitCount);
infoHeader.clrImportant = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.clrImportant);
infoHeader.clrUsed = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.clrUsed);
infoHeader.compression = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.compression);
infoHeader.height = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.height);
infoHeader.planes = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.planes);
infoHeader.size = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.size);
infoHeader.sizeImage = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.sizeImage);
infoHeader.width = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.width);
infoHeader.xPelsPerMeter = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.xPelsPerMeter);
infoHeader.yPelsPerMeter = Shared::PlatformByteOrder::fromCommonEndian(infoHeader.yPelsPerMeter);
}
if(infoHeader.bitCount!=24){
throw megaglest_runtime_error(path+" is not a 24 bit bitmap");
}
@ -324,7 +398,9 @@ void PixmapIoBmp::read(uint8 *pixels) {
}
void PixmapIoBmp::read(uint8 *pixels, int components) {
for(int i=0; i<h*w*components; i+=components) {
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
for(int i=0; i<h*w*components; i+=components) {
uint8 r, g, b;
size_t readBytes = fread(&b, 1, 1, file);
if(readBytes != 1) {
@ -332,13 +408,18 @@ void PixmapIoBmp::read(uint8 *pixels, int components) {
snprintf(szBuf,8096,"fread returned wrong size = %zu 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 = %zu 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) {
@ -346,6 +427,9 @@ void PixmapIoBmp::read(uint8 *pixels, int components) {
snprintf(szBuf,8096,"fread returned wrong size = %zu on line: %d.",readBytes,__LINE__);
throw megaglest_runtime_error(szBuf);
}
if(bigEndianSystem == true) {
r = Shared::PlatformByteOrder::fromCommonEndian(r);
}
switch(components){
@ -387,6 +471,16 @@ void PixmapIoBmp::openWrite(const string &path, int w, int h, int components) {
fileHeader.offsetBits=sizeof(BitmapFileHeader)+sizeof(BitmapInfoHeader);
fileHeader.size=sizeof(BitmapFileHeader)+sizeof(BitmapInfoHeader)+3*h*w;
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
if(bigEndianSystem == true) {
fileHeader.offsetBits = Shared::PlatformByteOrder::toCommonEndian(fileHeader.offsetBits);
fileHeader.reserved1 = Shared::PlatformByteOrder::toCommonEndian(fileHeader.reserved1);
fileHeader.reserved2 = Shared::PlatformByteOrder::toCommonEndian(fileHeader.reserved2);
fileHeader.size = Shared::PlatformByteOrder::toCommonEndian(fileHeader.size);
fileHeader.type1 = Shared::PlatformByteOrder::toCommonEndian(fileHeader.type1);
fileHeader.type2 = Shared::PlatformByteOrder::toCommonEndian(fileHeader.type2);
}
fwrite(&fileHeader, sizeof(BitmapFileHeader), 1, file);
//info header
@ -403,10 +497,29 @@ void PixmapIoBmp::openWrite(const string &path, int w, int h, int components) {
infoHeader.xPelsPerMeter= 0;
infoHeader.yPelsPerMeter= 0;
if(bigEndianSystem == true) {
infoHeader.bitCount = Shared::PlatformByteOrder::toCommonEndian(infoHeader.bitCount);
infoHeader.clrImportant = Shared::PlatformByteOrder::toCommonEndian(infoHeader.clrImportant);
infoHeader.clrUsed = Shared::PlatformByteOrder::toCommonEndian(infoHeader.clrUsed);
infoHeader.compression = Shared::PlatformByteOrder::toCommonEndian(infoHeader.compression);
infoHeader.height = Shared::PlatformByteOrder::toCommonEndian(infoHeader.height);
infoHeader.planes = Shared::PlatformByteOrder::toCommonEndian(infoHeader.planes);
infoHeader.size = Shared::PlatformByteOrder::toCommonEndian(infoHeader.size);
infoHeader.sizeImage = Shared::PlatformByteOrder::toCommonEndian(infoHeader.sizeImage);
infoHeader.width = Shared::PlatformByteOrder::toCommonEndian(infoHeader.width);
infoHeader.xPelsPerMeter = Shared::PlatformByteOrder::toCommonEndian(infoHeader.xPelsPerMeter);
infoHeader.yPelsPerMeter = Shared::PlatformByteOrder::toCommonEndian(infoHeader.yPelsPerMeter);
}
fwrite(&infoHeader, sizeof(BitmapInfoHeader), 1, file);
}
void PixmapIoBmp::write(uint8 *pixels) {
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
if(bigEndianSystem == true) {
Shared::PlatformByteOrder::toEndianTypeArray<uint8>(pixels,h*w*components);
}
for (int i=0; i<h*w*components; i+=components){
fwrite(&pixels[i+2], 1, 1, file);
fwrite(&pixels[i+1], 1, 1, file);

View File

@ -17,6 +17,7 @@
#include "sound.h"
#include "util.h"
#include "platform_util.h"
#include "byte_order.h"
#include "leak_dumper.h"
using namespace Shared::Platform;
@ -44,6 +45,12 @@ void WavSoundFileLoader::open(const string &path, SoundInfo *soundInfo){
//RIFF chunk - Id
f.read(chunkId, 4);
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
if(bigEndianSystem == true) {
for(unsigned int i = 0; i < 4; ++i) {
chunkId[i] = Shared::PlatformByteOrder::fromCommonEndian(chunkId[i]);
}
}
if(strcmp(chunkId, "RIFF")!=0){
throw megaglest_runtime_error("Not a valid wav file (first four bytes are not RIFF):" + path);
@ -51,9 +58,17 @@ void WavSoundFileLoader::open(const string &path, SoundInfo *soundInfo){
//RIFF chunk - Size
f.read((char*) &size32, 4);
if(bigEndianSystem == true) {
size32 = Shared::PlatformByteOrder::fromCommonEndian(size32);
}
//RIFF chunk - Data (WAVE string)
f.read(chunkId, 4);
if(bigEndianSystem == true) {
for(unsigned int i = 0; i < 4; ++i) {
chunkId[i] = Shared::PlatformByteOrder::fromCommonEndian(chunkId[i]);
}
}
if(strcmp(chunkId, "WAVE")!=0){
throw megaglest_runtime_error("Not a valid wav file (wave data don't start by WAVE): " + path);
@ -63,6 +78,11 @@ void WavSoundFileLoader::open(const string &path, SoundInfo *soundInfo){
//first sub-chunk (header) - Id
f.read(chunkId, 4);
if(bigEndianSystem == true) {
for(unsigned int i = 0; i < 4; ++i) {
chunkId[i] = Shared::PlatformByteOrder::fromCommonEndian(chunkId[i]);
}
}
if(strcmp(chunkId, "fmt ")!=0){
throw megaglest_runtime_error("Not a valid wav file (first sub-chunk Id is not fmt): "+ path);
@ -70,26 +90,50 @@ void WavSoundFileLoader::open(const string &path, SoundInfo *soundInfo){
//first sub-chunk (header) - Size
f.read((char*) &size32, 4);
if(bigEndianSystem == true) {
size32 = Shared::PlatformByteOrder::fromCommonEndian(size32);
}
//first sub-chunk (header) - Data (encoding type) - Ignore
f.read((char*) &size16, 2);
if(bigEndianSystem == true) {
size16 = Shared::PlatformByteOrder::fromCommonEndian(size16);
}
//first sub-chunk (header) - Data (nChannels)
f.read((char*) &size16, 2);
if(bigEndianSystem == true) {
size16 = Shared::PlatformByteOrder::fromCommonEndian(size16);
}
soundInfo->setChannels(size16);
//first sub-chunk (header) - Data (nsamplesPerSecond)
f.read((char*) &size32, 4);
if(bigEndianSystem == true) {
size32 = Shared::PlatformByteOrder::fromCommonEndian(size32);
}
soundInfo->setsamplesPerSecond(size32);
//first sub-chunk (header) - Data (nAvgBytesPerSec) - Ignore
f.read((char*) &size32, 4);
if(bigEndianSystem == true) {
size32 = Shared::PlatformByteOrder::fromCommonEndian(size32);
}
//first sub-chunk (header) - Data (blockAlign) - Ignore
f.read((char*) &size16, 2);
if(bigEndianSystem == true) {
size16 = Shared::PlatformByteOrder::fromCommonEndian(size16);
}
//first sub-chunk (header) - Data (nsamplesPerSecond)
f.read((char*) &size16, 2);
if(bigEndianSystem == true) {
size16 = Shared::PlatformByteOrder::fromCommonEndian(size16);
}
soundInfo->setBitsPerSample(size16);
soundInfo->setBitRate(soundInfo->getSamplesPerSecond() * soundInfo->getChannels() * soundInfo->getBitsPerSample() / 8);
@ -106,12 +150,22 @@ void WavSoundFileLoader::open(const string &path, SoundInfo *soundInfo){
// === DATA ===
//second sub-chunk (samples) - Id
f.read(chunkId, 4);
if(bigEndianSystem == true) {
for(unsigned int i = 0; i < 4; ++i) {
chunkId[i] = Shared::PlatformByteOrder::fromCommonEndian(chunkId[i]);
}
}
if(strncmp(chunkId, "data", 4)!=0){
continue;
}
//second sub-chunk (samples) - Size
f.read((char*) &size32, 4);
if(bigEndianSystem == true) {
size32 = Shared::PlatformByteOrder::fromCommonEndian(size32);
}
dataSize= size32;
soundInfo->setSize(dataSize);
}
@ -127,6 +181,11 @@ void WavSoundFileLoader::open(const string &path, SoundInfo *soundInfo){
uint32 WavSoundFileLoader::read(int8 *samples, uint32 size){
f.read(reinterpret_cast<char*> (samples), size);
static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian();
if(bigEndianSystem == true) {
Shared::PlatformByteOrder::toEndianTypeArray<int8>(samples,size);
}
return (uint32)f.gcount();
}