?? tga_image.cpp
字號(hào):
/*=================================================================================
FILE: tga_image.cpp
DESCRIPTION: This file is provide as a standard sample Brew source file.
Please refer to this OpenGL(R)ES brew sample application as a
reference on how to use the standard OpenGL-ES and EGL APIs.
ABSTRACT: Port code to work with Brew
AUTHOR: QUALCOMM
Copyright (c) 2004 QUALCOMM Incorporated.
All Rights Reserved.
QUALCOMM Proprietary/GTDR
=================================================================================*/
/*-------------------------------------------------------------------------------*
* I N C L U D E F I L E S *
*-------------------------------------------------------------------------------*/
#include "tga_image.h"
#include "AEE.h"
#include "AEEShell.h"
#include "AEEFile.h"
#include "AEEstdlib.h"
/*-------------------------------------------------------------------------------*
* D A T A S T R U C T U R E *
*-------------------------------------------------------------------------------*/
struct tgaheader_t
{
GLubyte idLength;
GLubyte colorMapType;
GLubyte imageType;
GLubyte colorMapSpec[5];
GLushort xOrigin;
GLushort yOrigin;
GLushort width;
GLushort height;
GLubyte bpp;
GLubyte imageDesc;
};
enum TGATypes
{
TGA_NODATA = 0,
TGA_INDEXED = 1,
TGA_RGB = 2,
TGA_GRAYSCALE = 3,
TGA_INDEXED_RLE = 9,
TGA_RGB_RLE = 10,
TGA_GRAYSCALE_RLE = 11
};
static int GetPadding(int width, int bpp)
{
return (((width * bpp + 31) & 0xFFFFFFE0) - width * bpp) / 8;
}
/*-------------------------------------------------------------------------------*
* B E G I N P R O G R A M *
*-------------------------------------------------------------------------------*/
/*===========================================================================
FUNCTION: TGA_IMAGE::TGA_IMAGE() : m_pData(NULL)
DESCRIPTION:
The TGA_IMAGE default constructor.
PROTOTYPE:
TGA_IMAGE::TGA_IMAGE()
PARAMETERS:
none
DEPENDENCIES
none
RETURN VALUE
none
===========================================================================*/
TGA_IMAGE::TGA_IMAGE() : m_pData(NULL)
{
} // end TGA_IMAGE::constructor
/*===========================================================================
FUNCTION: TGA_IMAGE::TGA_IMAGE(IShell *shell) : m_pData(NULL)
DESCRIPTION:
The TGA_IMAGE default constructor.
PROTOTYPE:
TGA_IMAGE::TGA_IMAGE(IShell *shell)
PARAMETERS:
shell : Pointer to the IShell Interface object
DEPENDENCIES
none
RETURN VALUE
none
===========================================================================*/
TGA_IMAGE::TGA_IMAGE(IShell *shell) : m_pData(NULL)
{
m_pIShell = shell;
} // end TGA_IMAGE::constructor
/*===========================================================================
FUNCTION: TGA_IMAGE::~TGA_IMAGE()
DESCRIPTION:
The TGA_IMAGE default destructor.
PROTOTYPE:
TGA_IMAGE::~TGA_IMAGE()
PARAMETERS:
none
DEPENDENCIES
none
RETURN VALUE
none
===========================================================================*/
TGA_IMAGE::~TGA_IMAGE()
{
FreeData();
} // end TGA_IMAGE::destructor
/*===========================================================================
FUNCTION: TGA_IMAGE::FreeData()
DESCRIPTION:
Release the memory currently holding the image data.
PROTOTYPE:
TGA_IMAGE::FreeData()
PARAMETERS:
none
DEPENDENCIES
none
RETURN VALUE
none
===========================================================================*/
void TGA_IMAGE::FreeData()
{
FREE(m_pData);
m_pData = NULL;
} // end TGA_IMAGE::FreeData()
/*===========================================================================
FUNCTION: TGA_IMAGE::AddAlphaChannel()
DESCRIPTION:
Adds an alpha channel to an image
PROTOTYPE:
TGA_IMAGE::AddAlphaChannel()
PARAMETERS:
none
DEPENDENCIES
none
RETURN VALUE
none
===========================================================================*/
void TGA_IMAGE::AddAlphaChannel(GLubyte value)
{
if (24 == m_colorDepth)
{
// for 24 bit images, just add another 8 bits for alpha
GLubyte* newImage = new GLubyte[m_width * m_height * 4];
if (!newImage)
return;
m_imageSize = m_width * m_height * 4;
int pad = GetPadding(m_width, 24);
rgb_t* source = (rgb_t*)m_pData;
rgba_t* dest = (rgba_t*)newImage;
for (int x = 0; x < m_height; ++x)
{
for (int y = 0; y < m_width; ++y)
{
dest->r = source->r;
dest->g = source->g;
dest->b = source->b;
dest->a = value;
++source;
++dest;
}
source = (rgb_t*)((GLubyte*)source + pad);
}
delete [] m_pData;
m_pData = newImage;
m_colorDepth = 32;
m_dataType = GL_UNSIGNED_BYTE;
m_dataFormat = GL_RGBA;
}
} // end TGA_IMAGE::AddAlphaChannel()
/*===========================================================================
FUNCTION: TGA_IMAGE::Flip()
DESCRIPTION:
Flips the image vertically
PROTOTYPE:
TGA_IMAGE::Flip()
PARAMETERS:
none
DEPENDENCIES
none
RETURN VALUE
TRUE: if flipping the image successful
FALSE: if error occured
===========================================================================*/
bool TGA_IMAGE::Flip()
{
if (!m_pData)
return false;
rgba_t* tmpBits = new rgba_t[m_width];
if (!tmpBits)
return false;
int lineWidth = m_width * 4;
rgba_t* top = (rgba_t*)m_pData;
rgba_t* bottom = (rgba_t*)(m_pData + lineWidth*(m_height-1));
for (int i = 0; i < (m_height / 2); ++i)
{
MEMCPY(tmpBits, top, lineWidth);
MEMCPY(top, bottom, lineWidth);
MEMCPY(bottom, tmpBits, lineWidth);
top = (rgba_t*)((GLubyte*)top + lineWidth);
bottom = (rgba_t* )((GLubyte*)bottom - lineWidth);
}
delete [] tmpBits;
tmpBits = 0;
return true;
} // TGA_IMAGE::Flip()
/*===========================================================================
FUNCTION: TGA_IMAGE::SwapBlueAndRed()
DESCRIPTION:
Flips the blue and red components of every pixel
PROTOTYPE:
TGA_IMAGE::SwapBlueAndRed()
PARAMETERS:
none
DEPENDENCIES
none
RETURN VALUE
none
===========================================================================*/
void TGA_IMAGE::SwapBlueAndRed()
{
switch (m_colorDepth)
{
case 32:
{
GLubyte temp;
rgba_t* source = (rgba_t*)m_pData;
for (int pixel = 0; pixel < (m_width * m_height); ++pixel)
{
temp = source[pixel].b;
source[pixel].b = source[pixel].r;
source[pixel].r = temp;
}
} break;
case 24:
{
GLubyte temp;
rgb_t* source = (rgb_t*)m_pData;
for (int pixel = 0; pixel < (m_width * m_height); ++pixel)
{
temp = source[pixel].b;
source[pixel].b = source[pixel].r;
source[pixel].r = temp;
}
} break;
default:
// ignore other color depths
break;
}
} // end TGA_IMAGE::ToggleBlueAndRed()
/*===========================================================================
FUNCTION: TGA_IMAGE::SetAlpha32()
DESCRIPTION:
Sets the alpha value for every pixel matching color to the specified
alpha. If color is NULL, every pixel will be set to the indicated value.
PROTOTYPE:
TGA_IMAGE::SetAlpha32()
PARAMETERS:
none
DEPENDENCIES
none
RETURN VALUE
none
===========================================================================*/
void TGA_IMAGE::SetAlpha32(const rgb_t* color, GLubyte alpha)
{
if (32 == m_colorDepth)
{
rgba_t* pixel = (rgba_t*)m_pData;
for (int i = 0; i < (m_width * m_height); ++i)
{
if (color)
{
if (pixel->r == color->r &&
pixel->g == color->g &&
pixel->b == color->b)
{
pixel->a = alpha;
}
}
else
{
pixel->a = alpha;
}
++pixel;
}
}
}
/*===========================================================================
FUNCTION: TGA_IMAGE::Load(const char* filename)
DESCRIPTION:
Adds an alpha channel to an image
PROTOTYPE:
TGA_IMAGE::Load(const char* filename)
PARAMETERS:
filename : Pointer to the character array of filename
DEPENDENCIES
none
RETURN VALUE
TRUE: if loading the image successful
FALSE: if error occured
===========================================================================*/
bool TGA_IMAGE::Load(const char* filename)
{
IFileMgr *m_pIFileMgr;
IFile* pFile;
//if (m_pData != NULL) FreeData();
if (ISHELL_CreateInstance(m_pIShell, AEECLSID_FILEMGR, (void **)&m_pIFileMgr)
!= SUCCESS)
return EFAILED;
pFile = IFILEMGR_OpenFile(m_pIFileMgr, filename, _OFM_READ);
if (!pFile)
return false;
// read in the image type
tgaheader_t tga; // TGA header
IFILE_Read(pFile, &tga, sizeof(tgaheader_t));
// see if the type is one that we support
if (( (tga.imageType != TGA_RGB)
&& (tga.imageType != TGA_GRAYSCALE)
&& (tga.imageType != TGA_RGB_RLE)
&& (tga.imageType != TGA_GRAYSCALE_RLE) )
|| (tga.colorMapType != 0) )
{
if (pFile) IFILE_Release(pFile);
if (m_pIFileMgr) IFILEMGR_Release(m_pIFileMgr);
return NULL;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -