⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tga_image.cpp

📁 此程序需要Brew sdk2.1版本以上,是关于OpenGL的手机编程.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	// store texture information
	m_width = tga.width;
	m_height = tga.height;

	// colormode -> 3 = BGR, 4 = BGRA
	int colorMode = tga.bpp / 8;

	// won't handle < 24 bpp for now
	if (colorMode < 3)
	{
		if (pFile) IFILE_Release(pFile);
		if (m_pIFileMgr) IFILEMGR_Release(m_pIFileMgr);

		return NULL;
	}

	m_imageSize = m_width * m_height * colorMode;

	// allocate memory for TGA image data
	m_pData = (GLubyte*) MALLOC(sizeof(GLubyte)*(m_imageSize+1));

	// read image data
	if (tga.imageType == TGA_RGB || tga.imageType == TGA_GRAYSCALE)
	{
		IFILE_Read(pFile, m_pData, m_imageSize);
	}
	else // must be RLE compressed
	{
		GLubyte id;
		GLubyte length;
		rgba_t color = { 0, 0, 0, 0 };
		GLuint i = 0;

		while(i < m_imageSize)
		{

			IFILE_Read(pFile, &id, sizeof(char));

			// see if this is run length data
			if(id & 0x80)
			{
				// find the run length
				length = (GLubyte)(id - 127);

				// next 3 (or 4) bytes are the repeated values

				IFILE_Read(pFile, &color.b, sizeof(char));
				IFILE_Read(pFile, &color.g, sizeof(char));
				IFILE_Read(pFile, &color.r, sizeof(char));

				if(colorMode == 4)
				{
					IFILE_Read(pFile, &color.a, sizeof(char));
				}

				// save everything in this run
				while(length > 0)
				{
					m_pData[i++] = color.b;
					m_pData[i++] = color.g;
					m_pData[i++] = color.r;
					if (colorMode == 4)
					{
						m_pData[i++] = color.a;
					}

					--length;
				}
			}
			else
			{
				// the number of non RLE pixels
				length = GLubyte(id + 1);

				while (length > 0)
				{

					IFILE_Read(pFile, &color.b, sizeof(char));
					IFILE_Read(pFile, &color.g, sizeof(char));
					IFILE_Read(pFile, &color.r, sizeof(char));

					if(colorMode == 4)
					{
						IFILE_Read(pFile, &color.a, sizeof(char));
					}
					m_pData[i++] = color.b;
					m_pData[i++] = color.g;
					m_pData[i++] = color.r;
					if(colorMode == 4)
					{
						m_pData[i++] = color.a;
					}

					--length;
				}
			}
		}
	}

	if (pFile) IFILE_Release(pFile);
	if (m_pIFileMgr) IFILEMGR_Release(m_pIFileMgr);

	switch(tga.imageType)
	{
		case TGA_RGB:
		case TGA_RGB_RLE:
			if (3 == colorMode)
			{
				m_dataFormat = GL_RGB;
				m_dataType = GL_UNSIGNED_BYTE;
				m_colorDepth = 24;
			}
			else
			{
				m_dataFormat = GL_RGBA;
				m_dataType = GL_UNSIGNED_BYTE;
				m_colorDepth = 32;
			}
			break;
		case TGA_GRAYSCALE:
		case TGA_GRAYSCALE_RLE:
			m_dataFormat = GL_LUMINANCE;
			m_dataType = GL_UNSIGNED_BYTE;
			m_colorDepth = 8;
			break;
	}

	SwapBlueAndRed();

	return (m_pData != NULL);


} // end TGA::LoadTGAFile()


/*===========================================================================
FUNCTION: TGA_IMAGE::Get4444()
  
DESCRIPTION:
   Get image in 4 bits red, 4 bits green, 4 bits blue and 4 bits alpha format
   
PROTOTYPE:
   TGA_IMAGE::Get4444()
      
PARAMETERS:
   none
            
DEPENDENCIES
   none
              
RETURN VALUE
   Pointer to struct rgba4444_t
                
===========================================================================*/
rgba4444_t* TGA_IMAGE::Get4444()
{
	
	if (m_colorDepth == 24)
		AddAlphaChannel(255);

	if (m_colorDepth != 32)
		return NULL;

	// for 24 bit images, just add another 8 bits for alpha
	rgba4444_t* newImage = new rgba4444_t[m_width * m_height];

	if (!newImage)
		return NULL;

	rgba_t* source = (rgba_t*)m_pData;
	rgba4444_t* dest = newImage;

	for (int x = 0; x < m_height; ++x)
	{
		for (int y = 0; y < m_width; ++y)
		{
			dest->r = source->r >> 4;
			dest->g = source->g >> 4;
			dest->b = source->b >> 4;
			dest->a = source->a >> 4;

			++source;
			++dest;
		}
	}

	return newImage;
 	
}


/*===========================================================================
FUNCTION: TGA_IMAGE::Get5551()
  
DESCRIPTION:
   Get image in 5 bits red, 5 bits green, 5 bits blue and 1 bits alpha format
   
PROTOTYPE:
   TGA_IMAGE::Get5551()
      
PARAMETERS:
   none
            
DEPENDENCIES
   none
              
RETURN VALUE
   Pointer to struct rgba5551_t
                
===========================================================================*/
rgba5551_t* TGA_IMAGE::Get5551()
{
	
	if (m_colorDepth == 24)
		AddAlphaChannel(255);
	if (m_colorDepth != 32)
		return NULL;

	// for 24 bit images, just add another 8 bits for alpha
	rgba5551_t* newImage = new rgba5551_t[m_width * m_height];

	if (!newImage)
		return NULL;

	rgba_t* source = (rgba_t*)m_pData;
	rgba5551_t* dest = newImage;

	for (int x = 0; x < m_height; ++x)
	{
		for (int y = 0; y < m_width; ++y)
		{
			dest->r = source->r >> 3;
			dest->g = source->g >> 3;
			dest->b = source->b >> 3;
			dest->a = source->a >> 7;

			++source;
			++dest;
		}
	}

	return newImage;
  	
}


/*===========================================================================
FUNCTION: TGA_IMAGE::Get565()
  
DESCRIPTION:
   Get image in 5 bits red, 6 bits green, 5 bits blue and 0 bits alpha format
   
PROTOTYPE:
   TGA_IMAGE::Get565()
      
PARAMETERS:
   none
            
DEPENDENCIES
   none
              
RETURN VALUE
   Pointer to struct rgb565_t
                
===========================================================================*/
rgb565_t* TGA_IMAGE::Get565()
{
	
	if (m_colorDepth != 24)
		return NULL;

	// for 24 bit images, just add another 8 bits for alpha
	rgb565_t* newImage = new rgb565_t[m_width * m_height];

	if (!newImage)
		return NULL;

	rgb_t* source = (rgb_t*)m_pData;
	rgb565_t* dest = newImage;

	for (int x = 0; x < m_height; ++x)
	{
		for (int y = 0; y < m_width; ++y)
		{
			dest->r = source->r >> 3;
			dest->g = source->g >> 2;
			dest->b = source->b >> 3;

			++source;
			++dest;
		}
	}

	return newImage;

}


/*===========================================================================
FUNCTION: TGA_IMAGE::GetAlpha()
  
DESCRIPTION:
   Gets the alpha bits of an image
   
PROTOTYPE:
   TGA_IMAGE::GetAlpha()
      
PARAMETERS:
   none
            
DEPENDENCIES
   none
              
RETURN VALUE
   Pointer to GLubyte
                
===========================================================================*/
GLubyte* TGA_IMAGE::GetAlpha()
{
	
	if (m_colorDepth != 32)
		return NULL;

	// for 24 bit images, just add another 8 bits for alpha
	GLubyte* newImage = new GLubyte[m_width * m_height];

	if (!newImage)
		return NULL;

	rgba_t* source = (rgba_t*)m_pData;
	GLubyte* dest = newImage;

	for (int x = 0; x < m_height; ++x)
	{
		for (int y = 0; y < m_width; ++y)
		{
			*dest = source->a;

			++source;
			++dest;
		}
	}

	return newImage;
  
}


/*===========================================================================
FUNCTION: TGA_IMAGE::GetLuminanceAlpha()
  
DESCRIPTION:
   Gets the luminance alpha bit of an image
   
PROTOTYPE:
   TGA_IMAGE::GetLuminanceAlpha()
      
PARAMETERS:
   none
            
DEPENDENCIES
   none
              
RETURN VALUE
   Pointer to GLubyte
                
===========================================================================*/

GLubyte* TGA_IMAGE::GetLuminanceAlpha()
{
	
	if (m_colorDepth == 24)
		AddAlphaChannel(255);
	if (m_colorDepth !=32)
		return NULL;

	// for 24 bit images, just add another 8 bits for alpha
	GLubyte* newImage = new GLubyte[m_width * m_height * 2];

	if (!newImage)
		return NULL;

	rgba_t* source = (rgba_t*)m_pData;
	la_t* dest = (la_t*)newImage;

	for (int x = 0; x < m_height; ++x)
	{
		for (int y = 0; y < m_width; ++y)
		{
			dest->a = source->a;
			dest->l = (GLubyte)((source->r + source->g + source->b) / 3);

			++source;
			++dest;
		}
	}

	return newImage;

}


/*===========================================================================
FUNCTION: TGA_IMAGE::GetLuminance()
  
DESCRIPTION:
   Gets the luminance bits of an image
   
PROTOTYPE:
   TGA_IMAGE::GetLuminance()
      
PARAMETERS:
   none
            
DEPENDENCIES
   none
              
RETURN VALUE
   Pointer to GLubyte
                
===========================================================================*/

GLubyte* TGA_IMAGE::GetLuminance()
{
	
	if (m_colorDepth != 32)
		return NULL;

	// for 24 bit images, just add another 8 bits for alpha
	GLubyte* newImage = new GLubyte[m_width * m_height];

	if (!newImage)
		return NULL;

	rgba_t* source = (rgba_t*)m_pData;
	GLubyte* dest = newImage;

	for (int x = 0; x < m_height; ++x)
	{
		for (int y = 0; y < m_width; ++y)
		{
			*dest = (GLubyte)((source->r + source->g + source->b) / 3);

			++source;
			++dest;
		}
	}

	return newImage;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -