📄 tga.cpp
字号:
#include "stdafx.h"
#include "..\..\Include\Pic\Image.h"
#include "..\..\Include\Compress\Rle.h"
//===================================================================
BOOL FCImage::LoadTga (BYTE * pStart)
{
TGAHEAD * pTga = (TGAHEAD *) pStart ;
BYTE * pCurr ;
if (pStart == NULL)
return FALSE ;
// Create Dib
if (!this->Create (pTga->wWidth, pTga->wHeight, pTga->byColorBits))
return FALSE ;
pCurr = pStart + sizeof(TGAHEAD) + pTga->byID_Length ;
// 设置调色板
if (pTga->byPalType == 1)
{
RGBQUAD * pPalette = new RGBQUAD [pTga->wPalLength] ;
if ((pTga->byPalBits == 24) || (pTga->byPalBits == 32)) // 24,32位调色板
for (int i = 0 ; i < pTga->wPalLength ; i++)
{
pPalette[i].rgbBlue = *pCurr++ ;
pPalette[i].rgbGreen = *pCurr++ ;
pPalette[i].rgbRed = *pCurr++ ;
if (pTga->byPalBits == 32)
pPalette[i].rgbReserved = *pCurr++ ;
}
else // 不支持15, 16位色调色板
{
delete[] pPalette ;
this->Unload () ;
return FALSE ;
}
this->SetColorTable (pTga->wPalFirstNdx, pTga->wPalLength, pPalette) ;
delete[] pPalette ;
}
// 解码像素
DWORD dwPitch = this->Width() * this->ColorBits() / 8 ;
if ((pTga->byImageType == 0x09) || (pTga->byImageType == 0x0A)) // 压缩
if (pStart[0x11] & 0x20) // 上下颠倒
for (int i = 0 ; i < this->Height() ; i++)
pCurr = ::RLE_TGA_DecodeLine (pCurr, this->ColorBits(), this->Width(), this->GetBits (i)) ;
else // same as BMP
for (int i = this->Height() - 1 ; i >= 0 ; i--)
pCurr = ::RLE_TGA_DecodeLine (pCurr, this->ColorBits(), this->Width(), this->GetBits (i)) ;
else // 未压缩
if (pStart[0x11] & 0x20) // 上下颠倒
for (int i = 0 ; i < this->Height() ; i++, pCurr += dwPitch)
CopyMemory (this->GetBits (i), pCurr, dwPitch) ;
else // same as BMP
for (int i = this->Height() - 1 ; i >= 0 ; i--, pCurr += dwPitch)
CopyMemory (this->GetBits (i), pCurr, dwPitch) ;
return TRUE ;
}
//===================================================================
BOOL FCImage::LoadTga (PCTSTR resName, PCTSTR resType)
{
HRSRC res = ::FindResource (NULL, resName, resType) ;
HGLOBAL gol = ::LoadResource (NULL, res) ;
BYTE * pTgaData = (BYTE *) ::LockResource (gol) ;
return this->LoadTga (pTgaData) ;
}
//===================================================================
BOOL FCImage::LoadTga (PCTSTR szFileName)
{
BOOL bRet = FALSE ;
BYTE * pStart = NULL ;
HANDLE hMap = NULL ;
HANDLE hFile = INVALID_HANDLE_VALUE ;
__try
{
if (pFooDib->hBitmap != NULL)
this->Unload () ;
// 映射文件
pStart = this->__fooImageReadFile (szFileName, &hFile, &hMap) ;
if (pStart == NULL)
__leave ;
if (!this->LoadTga (pStart)) // 读文件
__leave ;
bRet = TRUE ;
}
__finally
{
this->__fooImageUnmapFile ((BYTE *)pStart, hMap, hFile) ;
if (!bRet)
this->Unload () ;
}
return bRet ;
}
//===================================================================
BOOL FCImage::SaveTga (PCTSTR szFileName, bool bEncode)
{
const char fTgaInfo[] = {"foolish -- TGA"} ;
BOOL bRet = FALSE ;
HANDLE hMap = NULL ;
HANDLE hFile = INVALID_HANDLE_VALUE ;
BYTE * pStart = NULL, * pCurr = NULL ;
__try
{
if (pFooDib->hBitmap == NULL)
__leave ;
if (this->ColorBits() < 8)
this->ConvertColorTo (8) ; // 1,4位色 ==> 8位色
// 创建文件
pStart = this->__fooImageSaveFile (szFileName, &hFile, &hMap, this->GetPitch() * this->Height() * 2 + 2048) ;
if (pStart == NULL)
__leave ;
pCurr = pStart ;
// 设置头
*pCurr++ = 14 ; // 附加信息
*pCurr++ = (this->ColorBits() == 8) ? 1 : 0 ;
*pCurr++ = (this->ColorBits() == 8) ? (bEncode ? 9 : 1)
: (bEncode ? 10 : 2) ;
* (WORD *) pCurr = 0 ; pCurr += 2 ; // 调色板起始索引
* (WORD *) pCurr = 256 ; pCurr += 2 ; // 调色板长度
*pCurr++ = 24 ; // 调色板中每一颜色所占位数
* (DWORD *) pCurr = 0 ; // 左下角(X,Y)坐标
pCurr += 4 ;
* (WORD *) pCurr = (WORD) this->Width() ; pCurr += 2 ; // 宽
* (WORD *) pCurr = (WORD) this->Height() ; pCurr += 2 ; // 高
*pCurr++ = (BYTE) this->ColorBits() ; // 色彩位数
*pCurr++ = 0x20 ;
memcpy (pCurr, fTgaInfo, 14) ;
pCurr += 14 ; // 跳过14 BYTE
// 写调色板
if (this->ColorBits() == 8)
{
RGBQUAD * prgb = new RGBQUAD [256] ;
this->GetColorTable (0, 256, prgb) ;
for (int i = 0 ; i < 256 ; i++)
{
*pCurr++ = prgb[i].rgbBlue ;
*pCurr++ = prgb[i].rgbGreen ;
*pCurr++ = prgb[i].rgbRed ;
}
delete[] prgb ;
}
// 写像素
DWORD dwLineByte = this->ColorBits() * this->Width() / 8 ;
if (bEncode)
for (int Y = 0 ; Y < this->Height() ; Y++)
pCurr = ::RLE_TGA_EncodeLine (this->GetBits(Y), this->ColorBits(), this->Width(), pCurr) ;
else
for (int Y = 0 ; Y < this->Height() ; Y++, pCurr += dwLineByte)
CopyMemory (pCurr, this->GetBits(Y), dwLineByte) ;
bRet = TRUE ;
}
__finally
{
if (pStart != NULL) ::UnmapViewOfFile (pStart) ;
if (hMap != NULL)
{
::CloseHandle (hMap) ;
::SetFilePointer (hFile, pCurr - pStart, NULL, FILE_BEGIN) ;
::SetEndOfFile (hFile) ; // 设置文件大小
}
if (hFile != INVALID_HANDLE_VALUE) ::CloseHandle (hFile) ;
}
return bRet ;
}
//===================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -