Changed: Updated to compile under linux
~g2k
This commit is contained in:
179
src/bmp.cpp
179
src/bmp.cpp
@@ -3,7 +3,7 @@
|
||||
// Module: bmp.cpp
|
||||
// Author: Tom Hicks
|
||||
// Creation: 09-01-2003
|
||||
// LastEdit: 10-09-2003
|
||||
// LastEdit: 06-20-2005
|
||||
// Editors: Tom Hicks
|
||||
//
|
||||
// Purpose:
|
||||
@@ -18,8 +18,45 @@
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <cstdio>
|
||||
#include "../include/bmp.h"
|
||||
|
||||
#pragma pack(push,1)
|
||||
struct BITMAP_HEADER
|
||||
{
|
||||
uint16 type;
|
||||
uint32 size;
|
||||
uint16 reserved1;
|
||||
uint16 reserved2;
|
||||
uint32 offset;
|
||||
};
|
||||
|
||||
struct BITMAP_INFO
|
||||
{
|
||||
uint32 size;
|
||||
uint32 width;
|
||||
uint32 height;
|
||||
uint16 planes;
|
||||
uint16 bitCount;
|
||||
uint32 compression;
|
||||
uint32 sizeImage;
|
||||
uint32 xPelsPerMeter;
|
||||
uint32 yPelsPerMeter;
|
||||
uint32 clrUsed;
|
||||
uint32 clrImportant;
|
||||
};
|
||||
|
||||
struct BITMAP_QUAD
|
||||
{
|
||||
uint8 blue;
|
||||
uint8 green;
|
||||
uint8 red;
|
||||
uint8 reserved;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
#define BITMAP_MAGIC 19778
|
||||
|
||||
TextureImage* LoadBMP(const char* fn)
|
||||
{
|
||||
//If anything is not perfect return NULL after cleaning up our mess
|
||||
@@ -30,34 +67,128 @@ TextureImage* LoadBMP(const char* fn)
|
||||
if(!fn)
|
||||
return NULL;
|
||||
|
||||
|
||||
//Try to open our file and if successfull...
|
||||
f=fopen(fn, "r");
|
||||
|
||||
f=fopen(fn, "rb");
|
||||
if(f)
|
||||
{
|
||||
//close the file
|
||||
fclose(f);
|
||||
|
||||
//Load our bitmap structure from the file.
|
||||
AUX_RGBImageRec* bmp = auxDIBImageLoad(fn);
|
||||
|
||||
//If the load was successfull.
|
||||
if(bmp)
|
||||
BITMAP_HEADER bmpHeader;
|
||||
BITMAP_INFO bmpInfo;
|
||||
BITMAP_QUAD* bmpPallette = NULL;
|
||||
uint32 palletteEntries = 0;
|
||||
|
||||
fread(&bmpHeader, sizeof(bmpHeader), 1, f);
|
||||
fread(&bmpInfo, sizeof(bmpInfo), 1, f);
|
||||
|
||||
if(bmpInfo.width < 0)
|
||||
{
|
||||
//Allocate memory for a TextureImage Structure.
|
||||
TextureImage* tex = new TextureImage;
|
||||
|
||||
//Convert the bitmap to a TextureImage.
|
||||
tex->data = bmp->data;
|
||||
tex->sizeX = bmp->sizeX;
|
||||
tex->sizeY = bmp->sizeY;
|
||||
tex->type = GL_RGB;
|
||||
tex->bpp = 3;
|
||||
|
||||
//Free the bitmap but not it's data and return our TextureImage.
|
||||
free(bmp);
|
||||
return tex;
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Image width is negative", "ERROR", MB_OK);
|
||||
#endif
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(bmpInfo.width % 4 != 0)
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Image width must be a multiple of 8", "ERROR", MB_OK);
|
||||
#endif
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(bmpInfo.height < 0)
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Image height is negative", "ERROR", MB_OK);
|
||||
#endif
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(bmpInfo.height % 4 != 0)
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Image height must be a multiple of 8", "ERROR", MB_OK);
|
||||
#endif
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if((bmpInfo.bitCount != 8 && bmpInfo.bitCount != 24) || bmpInfo.compression != 0)
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Only 8 and 24 bit uncompressed windows bmp files are currently supported", "ERROR", MB_OK);
|
||||
#endif
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
//Allocate memory for a TextureImage structure
|
||||
TextureImage* tex = new TextureImage;
|
||||
tex->sizeX = bmpInfo.width;
|
||||
tex->sizeY = bmpInfo.height;
|
||||
if(bmpInfo.bitCount >= 8)
|
||||
{
|
||||
tex->bpp = bmpInfo.bitCount >> 3;
|
||||
}
|
||||
tex->type = GL_RGB;
|
||||
uint32 pixels = tex->sizeX * tex->sizeY;
|
||||
uint32 bytes = pixels * tex->bpp;
|
||||
tex->data = new uint8[bytes];
|
||||
|
||||
if(bmpInfo.bitCount == 8)
|
||||
{
|
||||
//Load the pallette
|
||||
palletteEntries = bmpInfo.bitCount << 8;
|
||||
bmpPallette = new BITMAP_QUAD[palletteEntries];
|
||||
fread(bmpPallette, sizeof(BITMAP_QUAD), palletteEntries, f);
|
||||
}
|
||||
|
||||
fseek(f, bmpHeader.offset, SEEK_SET);
|
||||
fread(tex->data, bytes, 1, f);
|
||||
|
||||
if(bmpInfo.bitCount == 8)
|
||||
{
|
||||
//Apply the pallette
|
||||
uint8* image = tex->data;
|
||||
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 NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user