Changed Moved resources around
~G2k
This commit is contained in:
230
src/bmp.cpp
230
src/bmp.cpp
@@ -19,48 +19,208 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <cstdio>
|
||||
#include "../include/bmp.h"
|
||||
#include "bmp.h"
|
||||
#ifdef WIN32
|
||||
#pragma warning(disable:4996)
|
||||
#endif
|
||||
|
||||
namespace OpenArena{
|
||||
|
||||
#pragma pack(push,1)
|
||||
struct BITMAP_HEADER
|
||||
Bitmap::Bitmap()
|
||||
{
|
||||
uint16 type;
|
||||
uint32 size;
|
||||
uint16 reserved1;
|
||||
uint16 reserved2;
|
||||
uint32 offset;
|
||||
};
|
||||
Bitmap(1, 1);
|
||||
}
|
||||
|
||||
struct BITMAP_INFO
|
||||
|
||||
Bitmap::Bitmap(uint32 width, uint32 height, uint32 bitsPerPixel, Image::Type type)
|
||||
{
|
||||
uint32 size;
|
||||
uint32 width;
|
||||
uint32 height;
|
||||
uint16 planes;
|
||||
uint16 bitCount;
|
||||
uint32 compression;
|
||||
uint32 sizeImage;
|
||||
uint32 xPelsPerMeter;
|
||||
uint32 yPelsPerMeter;
|
||||
uint32 clrUsed;
|
||||
uint32 clrImportant;
|
||||
};
|
||||
_width = width;
|
||||
_height = height;
|
||||
_bpp = bitsPerPixel;
|
||||
_type = type;
|
||||
_data = new uint8[_width * _height * (_bpp >> 3)];
|
||||
}
|
||||
|
||||
struct BITMAP_QUAD
|
||||
Bitmap::~Bitmap()
|
||||
{
|
||||
uint8 blue;
|
||||
uint8 green;
|
||||
uint8 red;
|
||||
uint8 reserved;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
delete [] _data;
|
||||
}
|
||||
|
||||
#define BITMAP_MAGIC 19778
|
||||
uint8 * Bitmap::GetImageData() const
|
||||
{
|
||||
return _data;
|
||||
}
|
||||
|
||||
uint32 Bitmap::GetBitsPerPixel() const
|
||||
{
|
||||
return _bpp;
|
||||
}
|
||||
|
||||
uint32 Bitmap::GetBytesPerPixel() const
|
||||
{
|
||||
return _bpp >> 3;
|
||||
}
|
||||
|
||||
uint32 Bitmap::GetWidth() const
|
||||
{
|
||||
return _width;
|
||||
}
|
||||
|
||||
uint32 Bitmap::GetHeight() const
|
||||
{
|
||||
return _height;
|
||||
}
|
||||
|
||||
Image::Type Bitmap::GetType() const
|
||||
{
|
||||
return _type;
|
||||
}
|
||||
|
||||
Bitmap* Bitmap::CreateFromFile(const char* filename)
|
||||
{
|
||||
FILE* file = NULL; //A file from cstdlib?
|
||||
|
||||
//If our filename is null return an empty 1x1 image
|
||||
if(filename == NULL)
|
||||
{
|
||||
return new Bitmap(1,1);
|
||||
}
|
||||
|
||||
//Try to open the file
|
||||
file = fopen(filename, "rb");
|
||||
|
||||
//If the open failed return an empry 1x1 image
|
||||
if(file == NULL)
|
||||
{
|
||||
return new Bitmap(1,1);
|
||||
}
|
||||
|
||||
BITMAP_HEADER bmpHeader;
|
||||
BITMAP_INFO bmpInfo;
|
||||
BITMAP_QUAD* pallette = NULL;
|
||||
uint32 numPalletteEntries = 0;
|
||||
uint32 numPixels;
|
||||
|
||||
//Read the header from the file
|
||||
fread(&bmpHeader, sizeof(bmpHeader), 1, file);
|
||||
fread(&bmpInfo, sizeof(bmpInfo), 1, file);
|
||||
|
||||
//Check for an invalid header
|
||||
if(bmpInfo.width < 0) //See if the width is negative
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Image width is negative", "ERROR", MB_OK);
|
||||
#endif
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
if(bmpInfo.width % 4 != 0) //If the width is not a multiple of 4
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Image width must be a multiple of 8", "ERROR", MB_OK);
|
||||
#endif
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(bmpInfo.height < 0) //If the height is negative
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Image height is negative", "ERROR", MB_OK);
|
||||
#endif
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
if(bmpInfo.height % 4 != 0) //If the height is not a multiple of 4
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Image height must be a multiple of 8", "ERROR", MB_OK);
|
||||
#endif
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if((bmpInfo.bitCount != 8 && bmpInfo.bitCount != 24) || bmpInfo.compression != 0) //Make sure the file is 8 or 24 bit and uncompressed
|
||||
{ //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(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Bitmap* image = new Bitmap(bmpInfo.width, bmpInfo.height, bmpInfo.bitCount, (Type)GL_RGB);
|
||||
|
||||
numPixels = image->GetWidth() * image->GetHeight();
|
||||
|
||||
//If 8-bit load the pallette
|
||||
if(image->GetBitsPerPixel() == 8)
|
||||
{
|
||||
//TODO finish this
|
||||
numPalletteEntries = image->GetBytesPerPixel();
|
||||
pallette = new BITMAP_QUAD[numPalletteEntries];
|
||||
// fread(pallette, sizeof(OpenArena::Bitmap::BITMAP_QUAD), pallette, file);
|
||||
}
|
||||
|
||||
//Seek to the start of data
|
||||
fseek(file, bmpHeader.offset, SEEK_SET);
|
||||
|
||||
//Read the image data
|
||||
fread(image->_data, numPixels * image->GetBytesPerPixel(), 1, file);
|
||||
|
||||
//If 8-bit apply the pallette
|
||||
if(image->GetBitsPerPixel() == 8)
|
||||
{
|
||||
//TODO finish this
|
||||
uint32 i1, i2;
|
||||
for(i1=0; i1<numPixels; i1++)
|
||||
{
|
||||
i2 = (i1 << 1) + 1;
|
||||
if(image->_data[i1] < numPalletteEntries)
|
||||
{
|
||||
image->_data[i2] = pallette[image->_data[i1]].red;
|
||||
image->_data[i2+1] = pallette[image->_data[i1]].blue;
|
||||
image->_data[i2+2] = pallette[image->_data[i1]].green;
|
||||
}
|
||||
}
|
||||
//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(image->GetBitsPerPixel() == 24)
|
||||
{
|
||||
//Convert to rgb
|
||||
uint32 i;
|
||||
uint8 swap;
|
||||
|
||||
for(i=0; i<numPixels * image->GetBytesPerPixel(); i+=3)
|
||||
{
|
||||
swap = image->_data[i];
|
||||
image->_data[i] = image->_data[i+2];
|
||||
image->_data[i+2] = swap;
|
||||
}
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
TextureImage* LoadBMP(const char* fn)
|
||||
{
|
||||
@@ -77,9 +237,9 @@ namespace OpenArena{
|
||||
f=fopen(fn, "rb");
|
||||
if(f)
|
||||
{
|
||||
BITMAP_HEADER bmpHeader;
|
||||
BITMAP_INFO bmpInfo;
|
||||
BITMAP_QUAD* bmpPallette = NULL;
|
||||
Bitmap::BITMAP_HEADER bmpHeader;
|
||||
Bitmap::BITMAP_INFO bmpInfo;
|
||||
Bitmap::BITMAP_QUAD* bmpPallette = NULL;
|
||||
uint32 palletteEntries = 0;
|
||||
|
||||
fread(&bmpHeader, sizeof(bmpHeader), 1, f);
|
||||
@@ -152,8 +312,8 @@ namespace OpenArena{
|
||||
{
|
||||
//Load the pallette
|
||||
palletteEntries = bmpInfo.bitCount << 8;
|
||||
bmpPallette = new BITMAP_QUAD[palletteEntries];
|
||||
fread(bmpPallette, sizeof(BITMAP_QUAD), palletteEntries, f);
|
||||
bmpPallette = new Bitmap::BITMAP_QUAD[palletteEntries];
|
||||
fread(bmpPallette, sizeof(Bitmap::BITMAP_QUAD), palletteEntries, f);
|
||||
}
|
||||
|
||||
fseek(f, bmpHeader.offset, SEEK_SET);
|
||||
|
||||
Reference in New Issue
Block a user