Added class TargaImage
~G2k
This commit is contained in:
169
src/tga.cpp
169
src/tga.cpp
@@ -1,13 +1,72 @@
|
||||
|
||||
#include "tga.h"
|
||||
#ifdef WIN32
|
||||
#pragma warning(disable:4996)
|
||||
#endif
|
||||
|
||||
namespace OpenArena{
|
||||
TextureImage* LoadTGA(const char * filename)
|
||||
{
|
||||
TGAHeader tgaheader;
|
||||
|
||||
uint8 * TargaImage::GetImageData() const
|
||||
{
|
||||
return _data;
|
||||
}
|
||||
|
||||
uint32 TargaImage::GetBitsPerPixel() const
|
||||
{
|
||||
return _bpp;
|
||||
}
|
||||
|
||||
uint32 TargaImage::GetBytesPerPixel() const
|
||||
{
|
||||
return _bpp >> 8;
|
||||
}
|
||||
|
||||
uint32 TargaImage::GetWidth() const
|
||||
{
|
||||
return _width;
|
||||
}
|
||||
|
||||
uint32 TargaImage::GetHeight() const
|
||||
{
|
||||
return _height;
|
||||
}
|
||||
|
||||
Image::Type TargaImage::GetType() const
|
||||
{
|
||||
return _type;
|
||||
}
|
||||
|
||||
TargaImage::~TargaImage(void)
|
||||
{
|
||||
delete [] _data;
|
||||
}
|
||||
|
||||
TargaImage* TargaImage::CreateFromFile(const char* filename)
|
||||
{
|
||||
TextureImage* texImage = LoadTGA(filename);
|
||||
TargaImage* image = new TargaImage(texImage->sizeX, texImage->sizeY, texImage->bpp);
|
||||
image->_data = texImage->data;
|
||||
image->_type = (Image::Type)texImage->type;
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
TargaImage::TargaImage(uint32 width, uint32 height, uint32 bpp, Image::Type type)
|
||||
{
|
||||
_width = width;
|
||||
_height = height;
|
||||
_bpp = bpp;
|
||||
_type = type;
|
||||
_data = new uint8[_width*_height* (_bpp >> 8)];
|
||||
}
|
||||
|
||||
TargaImage::TargaImage()
|
||||
{
|
||||
TargaImage(1,1);
|
||||
}
|
||||
|
||||
TextureImage* LoadTGA(const char * filename)
|
||||
{
|
||||
TargaImage::TGAHeader tgaheader;
|
||||
TextureImage* image;
|
||||
std::string errmsg;
|
||||
FILE* file = fopen(filename, "rb");
|
||||
@@ -17,18 +76,18 @@ TextureImage* LoadTGA(const char * filename)
|
||||
errmsg = "Could not open texture file ";
|
||||
errmsg = errmsg + filename;
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, errmsg.c_str(), "ERROR", MB_OK);
|
||||
#endif
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(fread(&tgaheader, sizeof(TGAHeader), 1, file) == 0)
|
||||
if(fread(&tgaheader, sizeof(TargaImage::TGAHeader), 1, file) == 0)
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Could not read file header", "ERROR", MB_OK);
|
||||
#endif
|
||||
#endif
|
||||
if(file != NULL)
|
||||
fclose(file);
|
||||
return NULL;
|
||||
@@ -41,26 +100,26 @@ TextureImage* LoadTGA(const char * filename)
|
||||
else
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "TGA file must be type 2 or type 10 ", "Invalid Image", MB_OK);
|
||||
#endif
|
||||
#endif
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
TextureImage* LoadUncompressedTGA(FILE * fTGA)
|
||||
{
|
||||
TGA tga;
|
||||
TextureImage* LoadUncompressedTGA(FILE * fTGA)
|
||||
{
|
||||
TargaImage::TGA tga;
|
||||
TextureImage* image = new TextureImage;
|
||||
|
||||
if(fread(tga.header, sizeof(tga.header), 1, fTGA) == 0)
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Could not read info header", "ERROR", MB_OK);
|
||||
#endif
|
||||
#endif
|
||||
if(fTGA != NULL)
|
||||
{
|
||||
fclose(fTGA);
|
||||
@@ -78,9 +137,9 @@ TextureImage* LoadUncompressedTGA(FILE * fTGA)
|
||||
if((image->sizeX <= 0) || (image->sizeY <= 0) || ((image->bpp != 24) && (image->bpp !=32)))
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Invalid texture information", "ERROR", MB_OK);
|
||||
#endif
|
||||
#endif
|
||||
if(fTGA != NULL)
|
||||
{
|
||||
fclose(fTGA);
|
||||
@@ -104,9 +163,9 @@ TextureImage* LoadUncompressedTGA(FILE * fTGA)
|
||||
if(image->data == NULL)
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Could not allocate memory for image", "ERROR", MB_OK);
|
||||
#endif
|
||||
#endif
|
||||
fclose(fTGA);
|
||||
return NULL;
|
||||
}
|
||||
@@ -114,9 +173,9 @@ TextureImage* LoadUncompressedTGA(FILE * fTGA)
|
||||
if(fread(image->data, 1, tga.imageSize, fTGA) != tga.imageSize)
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Could not read image data", "ERROR", MB_OK);
|
||||
#endif
|
||||
#endif
|
||||
if(image->data != NULL)
|
||||
{
|
||||
free(image->data);
|
||||
@@ -134,19 +193,19 @@ TextureImage* LoadUncompressedTGA(FILE * fTGA)
|
||||
|
||||
fclose(fTGA);
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
TextureImage* LoadCompressedTGA(FILE * fTGA)
|
||||
{
|
||||
TextureImage* LoadCompressedTGA(FILE * fTGA)
|
||||
{
|
||||
TextureImage* image = new TextureImage;
|
||||
TGA tga;
|
||||
TargaImage::TGA tga;
|
||||
|
||||
if(fread(tga.header, sizeof(tga.header), 1, fTGA) == 0)
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Could not read info header", "ERROR", MB_OK);
|
||||
#endif
|
||||
#endif
|
||||
if(fTGA != NULL)
|
||||
{
|
||||
fclose(fTGA);
|
||||
@@ -164,9 +223,9 @@ TextureImage* LoadCompressedTGA(FILE * fTGA)
|
||||
if((image->sizeX <= 0) || (image->sizeY <= 0) || ((image->bpp != 24) && (image->bpp !=32)))
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Invalid texture information", "ERROR", MB_OK);
|
||||
#endif
|
||||
#endif
|
||||
if(fTGA != NULL)
|
||||
{
|
||||
fclose(fTGA);
|
||||
@@ -181,9 +240,9 @@ TextureImage* LoadCompressedTGA(FILE * fTGA)
|
||||
if(image->data == NULL)
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Could not allocate memory for image", "ERROR", MB_OK);
|
||||
#endif
|
||||
#endif
|
||||
fclose(fTGA);
|
||||
return NULL;
|
||||
}
|
||||
@@ -200,9 +259,9 @@ TextureImage* LoadCompressedTGA(FILE * fTGA)
|
||||
if(fread(&chunkheader, sizeof(GLubyte), 1, fTGA) == 0)
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Could not read RLE header", "ERROR", MB_OK);
|
||||
#endif
|
||||
#endif
|
||||
if(fTGA != NULL)
|
||||
{
|
||||
fclose(fTGA);
|
||||
@@ -222,9 +281,9 @@ TextureImage* LoadCompressedTGA(FILE * fTGA)
|
||||
if(fread(colorbuffer, 1, tga.bytesPerPixel, fTGA) != tga.bytesPerPixel)
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Could not read image data", "ERROR", MB_OK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if(fTGA != NULL)
|
||||
{
|
||||
@@ -259,9 +318,9 @@ TextureImage* LoadCompressedTGA(FILE * fTGA)
|
||||
if(currentpixel > pixelcount)
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Too many pixels read", "ERROR", NULL);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if(fTGA != NULL)
|
||||
{
|
||||
@@ -288,9 +347,9 @@ TextureImage* LoadCompressedTGA(FILE * fTGA)
|
||||
if(fread(colorbuffer, 1, tga.bytesPerPixel, fTGA) != tga.bytesPerPixel)
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Could not read from file", "ERROR", MB_OK);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if(fTGA != NULL)
|
||||
{
|
||||
@@ -327,9 +386,9 @@ TextureImage* LoadCompressedTGA(FILE * fTGA)
|
||||
if(currentpixel > pixelcount)
|
||||
{
|
||||
//This needs to be abstracted somehow
|
||||
#ifdef WIN32
|
||||
#ifdef WIN32
|
||||
MessageBox(NULL, "Too many pixels read", "ERROR", NULL);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if(fTGA != NULL)
|
||||
{
|
||||
@@ -355,12 +414,12 @@ TextureImage* LoadCompressedTGA(FILE * fTGA)
|
||||
while(currentpixel < pixelcount);
|
||||
fclose(fTGA);
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
TextureImage* LoadTGA(const char * filename)
|
||||
{
|
||||
/*
|
||||
TextureImage* LoadTGA(const char * filename)
|
||||
{
|
||||
TGAHeader tgaheader;
|
||||
TextureImage* image;
|
||||
std::string errmsg;
|
||||
@@ -393,10 +452,10 @@ TextureImage* LoadTGA(const char * filename)
|
||||
return NULL;
|
||||
}
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
TextureImage* LoadUncompressedTGA(FILE * fTGA)
|
||||
{
|
||||
TextureImage* LoadUncompressedTGA(FILE * fTGA)
|
||||
{
|
||||
TGA tga;
|
||||
TextureImage* image = new TextureImage;
|
||||
|
||||
@@ -467,10 +526,10 @@ TextureImage* LoadUncompressedTGA(FILE * fTGA)
|
||||
|
||||
fclose(fTGA);
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
TextureImage* LoadCompressedTGA(FILE * fTGA)
|
||||
{
|
||||
TextureImage* LoadCompressedTGA(FILE * fTGA)
|
||||
{
|
||||
TextureImage* image = new TextureImage;
|
||||
TGA tga;
|
||||
|
||||
@@ -664,6 +723,6 @@ TextureImage* LoadCompressedTGA(FILE * fTGA)
|
||||
while(currentpixel < pixelcount);
|
||||
fclose(fTGA);
|
||||
return image;
|
||||
}
|
||||
*/
|
||||
}
|
||||
*/
|
||||
};
|
||||
21
src/tga.h
21
src/tga.h
@@ -15,8 +15,19 @@
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include "texture.h"
|
||||
#include "Image.h"
|
||||
|
||||
namespace OpenArena{
|
||||
class TargaImage: public Image{
|
||||
private:
|
||||
TargaImage();
|
||||
TargaImage(uint32 width, uint32 height, uint32 bpp = 24, Image::Type type = Image::Type_RGB);
|
||||
uint32 _width;
|
||||
uint32 _height;
|
||||
uint32 _bpp;
|
||||
Image::Type _type;
|
||||
uint8 * _data;
|
||||
public:
|
||||
struct TGAHeader
|
||||
{
|
||||
GLubyte Header[12]; // File Header To Determine File Type
|
||||
@@ -33,6 +44,16 @@ namespace OpenArena{
|
||||
GLuint Bpp; // Number Of BITS Per Pixel (24 Or 32)
|
||||
};
|
||||
|
||||
virtual uint8 * GetImageData() const;
|
||||
virtual uint32 GetBitsPerPixel() const;
|
||||
virtual uint32 GetBytesPerPixel() const;
|
||||
virtual uint32 GetWidth() const;
|
||||
virtual uint32 GetHeight() const;
|
||||
virtual Image::Type GetType() const;
|
||||
virtual ~TargaImage(void);
|
||||
static TargaImage* CreateFromFile(const char* filename);
|
||||
};
|
||||
|
||||
TextureImage* LoadTGA(const char *filename);
|
||||
|
||||
const GLubyte uTGAcompare[12] = {0,0,2, 0,0,0,0,0,0,0,0,0}; // Uncompressed TGA Header
|
||||
|
||||
Reference in New Issue
Block a user