/******************************************************************************
 * $Id: gfx_tga.hh,v 1.12 2006/08/03 02:17:21 devel Exp $
 * @file
 * @brief	Loader for Truevision Targa images
 * @author	Dan Maas <dmaas@dcine.com>,
 *          Piotr Pawlow <pp@siedziba.pl>,
 *          Jim E. Brooks http://www.jimbrooks.org
 * @remarks	Modifications by Jim Brooks:
 *          - Added Targa wrapper C++ class around C code.
 *//*
 * LEGAL:	THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE (GPL).
 ******************************************************************************/

#ifndef GFX_TGA_HH
#define GFX_TGA_HH

#include <assert.h>
#ifndef ASSERT
#define ASSERT assert
#endif

namespace gfx {

unsigned char* tga_load( unsigned int* olen, const unsigned char* ibuf, int ibufLen, int* w, int* h );

////////////////////////////////////////////////////////////////////////////////
// Wrapper class around tga_load().
class Targa
{
private:
    //PREVENT_COPYING(Targa)  // its .hh file not included
    Targa( const Targa& );              // prevent copying
    Targa& operator=( const Targa& );   // prevent copying

public:
	// Construct Targa object from buffer containing a TGA file (ibuf).
	Targa( const unsigned char* ibuf, unsigned int ibufLen )
	{
		mWidth = mHeight = mBufLen = 0;
		mBuf = tga_load( &mBufLen, ibuf, ibufLen, &mWidth, &mHeight );
	}
	~Targa()
	{
		free( mBuf ); mBuf = NULL;
	}
	bool			IfValid( void ) const	{ return mBuf != NULL; }
	unsigned int	GetWidth( void ) const	{ ASSERT( mWidth ); return mWidth; }
	unsigned int	GetHeight( void ) const	{ ASSERT( mHeight ); return mHeight; }
	unsigned char*	GetBuf( void )			{ ASSERT( mBuf ); return mBuf; }
	unsigned int	GetBufLen( void ) const { ASSERT( mBufLen ); return mBufLen; }
	unsigned char	ReadBuf( uint i ) const				{ ASSERT( mBuf && i < mBufLen ); return mBuf[i]; }
	void			WriteBuf( uint i, unsigned char c )	{ ASSERT( mBuf && i < mBufLen ); mBuf[i] = c; }

private:
	int				mWidth;
	int				mHeight;
	unsigned char*	mBuf;
	unsigned int	mBufLen;
};

} // namespace gfx

#endif // GFX_TGA_HH
