Changed Moved LoadBMP into the OpenArena namespace
~g2k
This commit is contained in:
@@ -5,6 +5,8 @@
|
|||||||
#include "datatypes.h"
|
#include "datatypes.h"
|
||||||
#include "texture.h"
|
#include "texture.h"
|
||||||
|
|
||||||
TextureImage* LoadBMP(const char* Filename);
|
namespace OpenArena{
|
||||||
|
TextureImage* LoadBMP(const char* Filename);
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
314
src/bmp.cpp
314
src/bmp.cpp
@@ -20,175 +20,181 @@
|
|||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include "../include/bmp.h"
|
#include "../include/bmp.h"
|
||||||
|
#ifdef WIN32
|
||||||
|
#pragma warning(disable:4996)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace OpenArena{
|
||||||
|
|
||||||
#pragma pack(push,1)
|
#pragma pack(push,1)
|
||||||
struct BITMAP_HEADER
|
struct BITMAP_HEADER
|
||||||
{
|
{
|
||||||
uint16 type;
|
uint16 type;
|
||||||
uint32 size;
|
uint32 size;
|
||||||
uint16 reserved1;
|
uint16 reserved1;
|
||||||
uint16 reserved2;
|
uint16 reserved2;
|
||||||
uint32 offset;
|
uint32 offset;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct BITMAP_INFO
|
struct BITMAP_INFO
|
||||||
{
|
{
|
||||||
uint32 size;
|
uint32 size;
|
||||||
uint32 width;
|
uint32 width;
|
||||||
uint32 height;
|
uint32 height;
|
||||||
uint16 planes;
|
uint16 planes;
|
||||||
uint16 bitCount;
|
uint16 bitCount;
|
||||||
uint32 compression;
|
uint32 compression;
|
||||||
uint32 sizeImage;
|
uint32 sizeImage;
|
||||||
uint32 xPelsPerMeter;
|
uint32 xPelsPerMeter;
|
||||||
uint32 yPelsPerMeter;
|
uint32 yPelsPerMeter;
|
||||||
uint32 clrUsed;
|
uint32 clrUsed;
|
||||||
uint32 clrImportant;
|
uint32 clrImportant;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct BITMAP_QUAD
|
struct BITMAP_QUAD
|
||||||
{
|
{
|
||||||
uint8 blue;
|
uint8 blue;
|
||||||
uint8 green;
|
uint8 green;
|
||||||
uint8 red;
|
uint8 red;
|
||||||
uint8 reserved;
|
uint8 reserved;
|
||||||
};
|
};
|
||||||
#pragma pack(pop)
|
#pragma pack(pop)
|
||||||
|
|
||||||
#define BITMAP_MAGIC 19778
|
#define BITMAP_MAGIC 19778
|
||||||
|
|
||||||
TextureImage* LoadBMP(const char* fn)
|
TextureImage* LoadBMP(const char* fn)
|
||||||
{
|
|
||||||
//If anything is not perfect return NULL after cleaning up our mess
|
|
||||||
|
|
||||||
FILE* f=NULL; //A pointer to our file structure
|
|
||||||
|
|
||||||
//If our filename is null
|
|
||||||
if(!fn)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
//Try to open our file and if successfull...
|
|
||||||
|
|
||||||
f=fopen(fn, "rb");
|
|
||||||
if(f)
|
|
||||||
{
|
{
|
||||||
BITMAP_HEADER bmpHeader;
|
//If anything is not perfect return NULL after cleaning up our mess
|
||||||
BITMAP_INFO bmpInfo;
|
|
||||||
BITMAP_QUAD* bmpPallette = NULL;
|
FILE* f=NULL; //A pointer to our file structure
|
||||||
uint32 palletteEntries = 0;
|
|
||||||
|
//If our filename is null
|
||||||
fread(&bmpHeader, sizeof(bmpHeader), 1, f);
|
if(!fn)
|
||||||
fread(&bmpInfo, sizeof(bmpInfo), 1, f);
|
|
||||||
|
|
||||||
if(bmpInfo.width < 0)
|
|
||||||
{
|
|
||||||
//This needs to be abstracted somehow
|
|
||||||
#ifdef WIN32
|
|
||||||
MessageBox(NULL, "Image width is negative", "ERROR", MB_OK);
|
|
||||||
#endif
|
|
||||||
fclose(f);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
//Try to open our file and if successfull...
|
||||||
if(bmpInfo.width % 4 != 0)
|
|
||||||
|
f=fopen(fn, "rb");
|
||||||
|
if(f)
|
||||||
{
|
{
|
||||||
//This needs to be abstracted somehow
|
BITMAP_HEADER bmpHeader;
|
||||||
#ifdef WIN32
|
BITMAP_INFO bmpInfo;
|
||||||
MessageBox(NULL, "Image width must be a multiple of 8", "ERROR", MB_OK);
|
BITMAP_QUAD* bmpPallette = NULL;
|
||||||
#endif
|
uint32 palletteEntries = 0;
|
||||||
fclose(f);
|
|
||||||
return NULL;
|
fread(&bmpHeader, sizeof(bmpHeader), 1, f);
|
||||||
}
|
fread(&bmpInfo, sizeof(bmpInfo), 1, f);
|
||||||
|
|
||||||
if(bmpInfo.height < 0)
|
if(bmpInfo.width < 0)
|
||||||
{
|
{
|
||||||
//This needs to be abstracted somehow
|
//This needs to be abstracted somehow
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
MessageBox(NULL, "Image height is negative", "ERROR", MB_OK);
|
MessageBox(NULL, "Image width is negative", "ERROR", MB_OK);
|
||||||
#endif
|
#endif
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(bmpInfo.height % 4 != 0)
|
if(bmpInfo.width % 4 != 0)
|
||||||
{
|
{
|
||||||
//This needs to be abstracted somehow
|
//This needs to be abstracted somehow
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
MessageBox(NULL, "Image height must be a multiple of 8", "ERROR", MB_OK);
|
MessageBox(NULL, "Image width must be a multiple of 8", "ERROR", MB_OK);
|
||||||
#endif
|
#endif
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if((bmpInfo.bitCount != 8 && bmpInfo.bitCount != 24) || bmpInfo.compression != 0)
|
if(bmpInfo.height < 0)
|
||||||
{
|
{
|
||||||
//This needs to be abstracted somehow
|
//This needs to be abstracted somehow
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
MessageBox(NULL, "Only 8 and 24 bit uncompressed windows bmp files are currently supported", "ERROR", MB_OK);
|
MessageBox(NULL, "Image height is negative", "ERROR", MB_OK);
|
||||||
#endif
|
#endif
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Allocate memory for a TextureImage structure
|
if(bmpInfo.height % 4 != 0)
|
||||||
TextureImage* tex = new TextureImage;
|
{
|
||||||
tex->sizeX = bmpInfo.width;
|
//This needs to be abstracted somehow
|
||||||
tex->sizeY = bmpInfo.height;
|
#ifdef WIN32
|
||||||
if(bmpInfo.bitCount >= 8)
|
MessageBox(NULL, "Image height must be a multiple of 8", "ERROR", MB_OK);
|
||||||
{
|
#endif
|
||||||
tex->bpp = bmpInfo.bitCount >> 3;
|
fclose(f);
|
||||||
}
|
return NULL;
|
||||||
tex->type = GL_RGB;
|
}
|
||||||
uint32 pixels = tex->sizeX * tex->sizeY;
|
|
||||||
uint32 bytes = pixels * tex->bpp;
|
if((bmpInfo.bitCount != 8 && bmpInfo.bitCount != 24) || bmpInfo.compression != 0)
|
||||||
tex->data = new uint8[bytes];
|
{
|
||||||
|
//This needs to be abstracted somehow
|
||||||
if(bmpInfo.bitCount == 8)
|
#ifdef WIN32
|
||||||
{
|
MessageBox(NULL, "Only 8 and 24 bit uncompressed windows bmp files are currently supported", "ERROR", MB_OK);
|
||||||
//Load the pallette
|
#endif
|
||||||
palletteEntries = bmpInfo.bitCount << 8;
|
fclose(f);
|
||||||
bmpPallette = new BITMAP_QUAD[palletteEntries];
|
return NULL;
|
||||||
fread(bmpPallette, sizeof(BITMAP_QUAD), palletteEntries, f);
|
}
|
||||||
}
|
|
||||||
|
//Allocate memory for a TextureImage structure
|
||||||
fseek(f, bmpHeader.offset, SEEK_SET);
|
TextureImage* tex = new TextureImage;
|
||||||
fread(tex->data, bytes, 1, f);
|
tex->sizeX = bmpInfo.width;
|
||||||
|
tex->sizeY = bmpInfo.height;
|
||||||
if(bmpInfo.bitCount == 8)
|
if(bmpInfo.bitCount >= 8)
|
||||||
{
|
{
|
||||||
//Apply the pallette
|
tex->bpp = bmpInfo.bitCount >> 3;
|
||||||
uint8* image = tex->data;
|
}
|
||||||
tex->bpp = 24;
|
tex->type = GL_RGB;
|
||||||
bytes = pixels * tex->bpp;
|
uint32 pixels = tex->sizeX * tex->sizeY;
|
||||||
|
uint32 bytes = pixels * tex->bpp;
|
||||||
tex->data = new uint8[bytes];
|
tex->data = new uint8[bytes];
|
||||||
|
|
||||||
uint32 i;
|
if(bmpInfo.bitCount == 8)
|
||||||
uint32 i2;
|
|
||||||
for(i=0; i<pixels; i++)
|
|
||||||
{
|
{
|
||||||
i2 = (i << 1) + 1;
|
//Load the pallette
|
||||||
//Should make sure image[i] < palletteEntries
|
palletteEntries = bmpInfo.bitCount << 8;
|
||||||
tex->data[i2] = bmpPallette[image[i]].red;
|
bmpPallette = new BITMAP_QUAD[palletteEntries];
|
||||||
tex->data[i2 + 1] = bmpPallette[image[i]].blue;
|
fread(bmpPallette, sizeof(BITMAP_QUAD), palletteEntries, f);
|
||||||
tex->data[i2 + 2] = bmpPallette[image[i]].green;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delete [] image;
|
fseek(f, bmpHeader.offset, SEEK_SET);
|
||||||
image = NULL;
|
fread(tex->data, bytes, 1, f);
|
||||||
}
|
|
||||||
else if(bmpInfo.bitCount == 24)
|
if(bmpInfo.bitCount == 8)
|
||||||
{
|
|
||||||
uint32 i;
|
|
||||||
uint8 t;
|
|
||||||
|
|
||||||
for(i=0; i<bytes; i+=3)
|
|
||||||
{
|
{
|
||||||
t = tex->data[i];
|
//Apply the pallette
|
||||||
tex->data[i] = tex->data[i+2];
|
uint8* image = tex->data;
|
||||||
tex->data[i+2] = t;
|
tex->bpp = 24;
|
||||||
|
bytes = pixels * tex->bpp;
|
||||||
|
tex->data = new uint8[bytes];
|
||||||
|
|
||||||
|
uint32 i;
|
||||||
|
uint32 i2;
|
||||||
|
for(i=0; i<pixels; i++)
|
||||||
|
{
|
||||||
|
i2 = (i << 1) + 1;
|
||||||
|
//Should make sure image[i] < palletteEntries
|
||||||
|
tex->data[i2] = bmpPallette[image[i]].red;
|
||||||
|
tex->data[i2 + 1] = bmpPallette[image[i]].blue;
|
||||||
|
tex->data[i2 + 2] = bmpPallette[image[i]].green;
|
||||||
|
}
|
||||||
|
|
||||||
|
delete [] image;
|
||||||
|
image = NULL;
|
||||||
}
|
}
|
||||||
|
else if(bmpInfo.bitCount == 24)
|
||||||
|
{
|
||||||
|
uint32 i;
|
||||||
|
uint8 t;
|
||||||
|
|
||||||
|
for(i=0; i<bytes; i+=3)
|
||||||
|
{
|
||||||
|
t = tex->data[i];
|
||||||
|
tex->data[i] = tex->data[i+2];
|
||||||
|
tex->data[i+2] = t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tex;
|
||||||
}
|
}
|
||||||
return tex;
|
return NULL;
|
||||||
}
|
}
|
||||||
return NULL;
|
};
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user