?? bitmap.c
字號(hào):
//======================================================
// 文件名稱: BitMap.c
// 功能描述: 位圖處理
// 維護(hù)記錄: 2007-02-09 v1.0 by lijian
// 注意事項(xiàng): 該程序中涉及到的數(shù)據(jù)類型需要根據(jù)平臺(tái)確定
// 其中,COLOR類型為無(wú)符號(hào)16bit數(shù)據(jù)類型
// RGB()是一個(gè)宏主要完成RGB三色數(shù)據(jù)向16bit 565格式的轉(zhuǎn)換,
// 該宏的完整定義如下:
// #define RGB(r, g, b) RGB24(r, g, b)
// #define RGB24(r, g, b) ( (((COLOR)r&0xf8)<<8) | (((COLOR)g&0xfc)<<3) | (((COLOR)b&0xf8)>>3) )
// #define RGB16(rgb) ( (((COLOR)rgb&0xffe0)<<1) | ((COLOR)rgb&0x1f) )
//======================================================
#include "BitMap.h"
//=============================================================
//語(yǔ)法格式: UINT8 BMP_OpenFile(UINT8 *BitFile)
//實(shí)現(xiàn)功能: 讀取位圖文件信息
//參數(shù): BFile - BMP文件指針
// BitFile - 位圖文件保存首地址
//返回值: 成功返回0, 失敗(不支持的格式)返回-1
//=============================================================
UINT8 BMP_OpenFile(BITMAP_FILE *BFile, UINT8 *BitFile)
{
UINT32 i;
BFile->Opened = 0;
BFile->FileHead.Type = (UINT16)BitFile[0] + ((UINT16)BitFile[1] << 8);
BFile->FileHead.FileSize = (UINT32)BitFile[2] + ((UINT32)BitFile[3] << 8) + ((UINT32)BitFile[4] << 16) + ((UINT32)BitFile[5] << 24);
BFile->FileHead.Offset = (UINT32)BitFile[0x0a] + ((UINT32)BitFile[0x0b] << 8) + ((UINT32)BitFile[0x0c] << 16) + ((UINT32)BitFile[0x0d] << 24);
BFile->InfoHead.HeadSize = (UINT32)BitFile[0x0e] + ((UINT32)BitFile[0x0f] << 8) + ((UINT32)BitFile[0x10] << 16) + ((UINT32)BitFile[0x11] << 24);
BFile->InfoHead.Width = (UINT32)BitFile[0x12] + ((UINT32)BitFile[0x13] << 8) + ((UINT32)BitFile[0x14] << 16) + ((UINT32)BitFile[0x15] << 24);
BFile->InfoHead.Height = (UINT32)BitFile[0x16] + ((UINT32)BitFile[0x17] << 8) + ((UINT32)BitFile[0x18] << 16) + ((UINT32)BitFile[0x19] << 24);
BFile->InfoHead.BitPerPixel = (UINT16)BitFile[0x1c] + ((UINT16)BitFile[0x1d] << 8);
BFile->InfoHead.CompresType = (UINT32)BitFile[0x1e] + ((UINT32)BitFile[0x1f] << 8) + ((UINT32)BitFile[0x20] << 16) + ((UINT32)BitFile[0x21] << 24);
BFile->InfoHead.ColorTable.CTOffset = BFile->InfoHead.HeadSize + 0x0e;
BFile->InfoHead.ColorTable.CTSize = (BFile->FileHead.Offset - BFile->InfoHead.HeadSize - 0x0e) >> 2;
BFile->InfoHead.DataSize = (UINT32)BitFile[0x22] + ((UINT32)BitFile[0x23] << 8) + ((UINT32)BitFile[0x24] << 16) + ((UINT32)BitFile[0x25] << 24);
BFile->Data = BitFile;
BFile->LineWidth = (((BFile->InfoHead.Width * BFile->InfoHead.BitPerPixel) + 31) >> 5) << 2;
if((BFile->FileHead.Type != 0x4d42) || ((BFile->InfoHead.CompresType != BI_RGB) && (BFile->InfoHead.CompresType != BI_BITFIELDS)))
// 僅支持不壓縮的位圖格式
{
return -1;
}
for(i = 0; i < BFile->InfoHead.ColorTable.CTSize; i++)
{
BFile->InfoHead.ColorTable.ColorTable[i].B = BitFile[BFile->InfoHead.ColorTable.CTOffset + 4 * i];
BFile->InfoHead.ColorTable.ColorTable[i].G = BitFile[BFile->InfoHead.ColorTable.CTOffset + 4 * i + 1];
BFile->InfoHead.ColorTable.ColorTable[i].R = BitFile[BFile->InfoHead.ColorTable.CTOffset + 4 * i + 2];
BFile->InfoHead.ColorTable.ColorTable[i].Filled = 0;
}
BFile->Opened = 1;
return 0;
}
//=============================================================
//語(yǔ)法格式: UINT8 BMP_CheckFormat(void)
//實(shí)現(xiàn)功能: 檢查文件格式是否支持
//參數(shù): BFile - BMP文件指針
//返回值: 支持返回1, 不支持返回0
//注意事項(xiàng): 需要首先調(diào)用BMP_OpenFile()函數(shù)讀取文件信息
//=============================================================
UINT8 BMP_CheckFormat(BITMAP_FILE *BFile)
{
return BFile->Opened;
}
//=============================================================
//語(yǔ)法格式: UINT32 BMP_GetWidth(void)
//實(shí)現(xiàn)功能: 得到文件寬度
//參數(shù): BFile - BMP文件指針
//返回值: 文件寬度
//注意事項(xiàng): 需要首先調(diào)用BMP_OpenFile()函數(shù)讀取文件信息
//=============================================================
UINT32 BMP_GetWidth(BITMAP_FILE *BFile)
{
if(BFile->Opened == 0)
return 0;
return(BFile->InfoHead.Width);
}
//=============================================================
//語(yǔ)法格式: UINT32 BMP_GetHeight(void)
//實(shí)現(xiàn)功能: 得到文件高度
//參數(shù): BFile - BMP文件指針
//返回值: 文件高度
//注意事項(xiàng): 需要首先調(diào)用BMP_OpenFile()函數(shù)讀取文件信息
//=============================================================
UINT32 BMP_GetHeight(BITMAP_FILE *BFile)
{
if(BFile->Opened == 0)
return 0;
return(BFile->InfoHead.Height);
}
//=============================================================
//語(yǔ)法格式: UINT32 BMP_GetOffset(void)
//實(shí)現(xiàn)功能: 得到位圖數(shù)據(jù)的偏移地址
//參數(shù): BFile - BMP文件指針
//返回值: 位圖數(shù)據(jù)偏移
//注意事項(xiàng): 需要首先調(diào)用BMP_OpenFile()函數(shù)讀取文件信息
//=============================================================
UINT32 BMP_GetOffset(BITMAP_FILE *BFile)
{
if(BFile->Opened == 0)
return 0;
return(BFile->FileHead.Offset);
}
//=============================================================
//語(yǔ)法格式: UINT32 BMP_GetLineWidth(void)
//實(shí)現(xiàn)功能: 得到每行圖象占用的byte數(shù)
//參數(shù): BFile - BMP文件指針
//返回值: 每行圖象占用的byte數(shù)
//注意事項(xiàng): 需要首先調(diào)用BMP_OpenFile()函數(shù)讀取文件信息
//=============================================================
UINT32 BMP_GetLineWidth(BITMAP_FILE *BFile)
{
if(BFile->Opened == 0)
return 0;
return(BFile->LineWidth);
}
//=============================================================
//語(yǔ)法格式: UINT32 BMP_GetLineColor2(BITMAP_FILE *BFile, UINT8 *DataAddr, COLOR *ColorTable)
//實(shí)現(xiàn)功能: 從指定地址處獲取該行的顏色
//參數(shù): BFile - BMP文件指針
// DataAddr- 位圖行數(shù)據(jù)保存首地址
// ColorTable- 保存顏色的數(shù)組
//返回值: 成功返回0, 失敗(不支持的格式)返回-1
//=============================================================
UINT32 BMP_GetLineColor2(BITMAP_FILE *BFile, UINT8 *DataAddr, COLOR *ColorTable)
{
UINT32 Offset = 0;
RGBQUAD CT;
int i;
if(BFile->Opened == 0)
return 0;
// Offset = (BFile->InfoHead.Height - 1 - line) * (BFile->LineWidth << 3) + BFile->FileHead.Offset * 8;
Offset = 0;
switch(BFile->InfoHead.BitPerPixel)
{
case BP_SINGLE:
for(i = 0; i < BFile->InfoHead.Width; i++)
{
if((DataAddr[Offset >> 3] & (0x80 >> ((UINT8)Offset & 0x07))) != 0)
ColorTable[i] = RGB(0xff, 0xff, 0xff);
else
ColorTable[i] = RGB(0, 0, 0);
Offset++;
}
break;
case BP_16COLOR:
for(i = 0; i < BFile->InfoHead.Width; i++)
{
if(((UINT8)Offset & 0x08))
CT = BFile->InfoHead.ColorTable.ColorTable[DataAddr[Offset >> 3] & 0x0f];
else
CT = BFile->InfoHead.ColorTable.ColorTable[(DataAddr[Offset >> 3] >> 4) & 0x0f];
ColorTable[i] = RGB(CT.R, CT.G, CT.B);
Offset += 4;
}
break;
case BP_256COLOR:
for(i = 0; i < BFile->InfoHead.Width; i++)
{
CT = BFile->InfoHead.ColorTable.ColorTable[DataAddr[Offset >> 3]];
ColorTable[i] = RGB(CT.R, CT.G, CT.B);
Offset += 8;
}
break;
case BP_16BITCOLOR:
Offset >>= 3;
for(i = 0; i < BFile->InfoHead.Width; i++)
{
ColorTable[i] = RGB16(*(UINT16 *)(DataAddr + Offset));
Offset += 2;
}
break;
case BP_24BITCOLOR:
case BP_32BITCOLOR:
Offset >>= 3;
for(i = 0; i < BFile->InfoHead.Width; i++)
{
ColorTable[i] = RGB(DataAddr[Offset + 2], DataAddr[Offset + 1], DataAddr[Offset]);
Offset += 3;
}
break;
default:
return 0;
break;
}
return BFile->InfoHead.Width;
}
//=============================================================
//語(yǔ)法格式: UINT32 BMP_GetLineColor(BITMAP_FILE *BFile, UINT16 line, COLOR *ColorTable)
//實(shí)現(xiàn)功能: 獲取指定行的顏色
//參數(shù): BFile - BMP文件指針
// line - 行數(shù)(自上而下)
// ColorTable- 保存顏色的數(shù)組
//返回值: 成功返回0, 失敗(不支持的格式)返回-1
//=============================================================
UINT32 BMP_GetLineColor(BITMAP_FILE *BFile, UINT16 line, COLOR *ColorTable)
{
UINT32 Offset = 0;
RGBQUAD CT;
int i;
if(BFile->Opened == 0)
return 0;
Offset = (BFile->InfoHead.Height - 1 - line) * (BFile->LineWidth << 3) + BFile->FileHead.Offset * 8;
switch(BFile->InfoHead.BitPerPixel)
{
case BP_SINGLE:
for(i = 0; i < BFile->InfoHead.Width; i++)
{
if((BFile->Data[Offset >> 3] & (0x80 >> ((UINT8)Offset & 0x07))) != 0)
ColorTable[i] = RGB(0xff, 0xff, 0xff);
else
ColorTable[i] = RGB(0, 0, 0);
Offset++;
}
break;
case BP_16COLOR:
for(i = 0; i < BFile->InfoHead.Width; i++)
{
if(((UINT8)Offset & 0x08))
CT = BFile->InfoHead.ColorTable.ColorTable[BFile->Data[Offset >> 3] & 0x0f];
else
CT = BFile->InfoHead.ColorTable.ColorTable[(BFile->Data[Offset >> 3] >> 4) & 0x0f];
ColorTable[i] = RGB(CT.R, CT.G, CT.B);
Offset += 4;
}
break;
case BP_256COLOR:
for(i = 0; i < BFile->InfoHead.Width; i++)
{
CT = BFile->InfoHead.ColorTable.ColorTable[BFile->Data[Offset >> 3]];
ColorTable[i] = RGB(CT.R, CT.G, CT.B);
Offset += 8;
}
break;
case BP_16BITCOLOR:
Offset >>= 3;
for(i = 0; i < BFile->InfoHead.Width; i++)
{
ColorTable[i] = RGB16(*(UINT16 *)(BFile->Data + Offset));
Offset += 2;
}
break;
case BP_24BITCOLOR:
case BP_32BITCOLOR:
Offset >>= 3;
for(i = 0; i < BFile->InfoHead.Width; i++)
{
ColorTable[i] = RGB(BFile->Data[Offset + 2], BFile->Data[Offset + 1], BFile->Data[Offset]);
Offset += 3;
}
break;
default:
return 0;
break;
}
return BFile->InfoHead.Width;
}
//=============================================================
//語(yǔ)法格式: COLOR BMP_GetPixelColor(UINT16 x, UINT16 y)
//實(shí)現(xiàn)功能: 得到指定點(diǎn)的顏色
//參數(shù): BFile - BMP文件指針
// x - 橫坐標(biāo)
// y - 縱坐標(biāo)
//返回值: 顏色(保存格式為16bit 565真彩格式:rrrrr-ggggg-bbbbb)
//注意事項(xiàng): 需要首先調(diào)用BMP_OpenFile()函數(shù)讀取文件信息
// 另外,函數(shù)中沒(méi)有對(duì)坐標(biāo)的有效性進(jìn)行檢查,使用的時(shí)候注意!
//=============================================================
COLOR BMP_GetPixelColor(BITMAP_FILE *BFile, UINT16 x, UINT16 y)
{
UINT32 Offset = 0;
COLOR Ret = RGB(0, 0, 0);
RGBQUAD CT;
if(BFile->Opened == 0)
return RGB(0, 0, 0);
Offset = (BFile->InfoHead.Height - 1 - y) * (BFile->LineWidth << 3) + x * BFile->InfoHead.BitPerPixel + BFile->FileHead.Offset * 8;
switch(BFile->InfoHead.BitPerPixel)
{
case BP_SINGLE:
if((BFile->Data[Offset >> 3] & (0x80 >> ((UINT8)Offset & 0x07))) != 0)
Ret = RGB(0xff, 0xff, 0xff);
else
Ret = RGB(0, 0, 0);
break;
case BP_16COLOR:
if(((UINT8)Offset & 0x08))
CT = BFile->InfoHead.ColorTable.ColorTable[BFile->Data[Offset >> 3] & 0x0f];
else
CT = BFile->InfoHead.ColorTable.ColorTable[(BFile->Data[Offset >> 3] >> 4) & 0x0f];
Ret = RGB(CT.R, CT.G, CT.B);
break;
case BP_256COLOR:
CT = BFile->InfoHead.ColorTable.ColorTable[BFile->Data[Offset >> 3]];
Ret = RGB(CT.R, CT.G, CT.B);
break;
case BP_16BITCOLOR:
Ret = RGB16(*(UINT16 *)(BFile->Data + (Offset >> 3)));
break;
case BP_24BITCOLOR:
case BP_32BITCOLOR:
Offset >>= 3;
Ret = RGB(BFile->Data[Offset + 2], BFile->Data[Offset + 1], BFile->Data[Offset]);
break;
default:
Ret = RGB(0, 0, 0);
break;
}
return Ret;
}
//=============================================================
//語(yǔ)法格式: void BMP_CloseFile(void)
//實(shí)現(xiàn)功能: 關(guān)閉位圖文件
//參數(shù): BFile - BMP文件指針
//返回值: 無(wú)
//=============================================================
void BMP_CloseFile(BITMAP_FILE *BFile)
{
BFile->Opened = 0;
}
#if 0
int BMP_CreateFile(BITMAP_FILE *BFile, UINT8 *SaveAddr, UINT32 Width, UINT32 Height, INT32 BitMode)
{
BFile->LineWidth = (((BFile->InfoHead.Width * BFile->InfoHead.BitPerPixel) + 31) >> 5) << 2;
BFile->InfoHead.HeadSize = 0x28;
BFile->InfoHead.Width = Width;
BFile->InfoHead.Height = Height;
BFile->InfoHead.Planes = 1;
if(BitMode==24)
{
BFile->InfoHead.BitPerPixel = 24;
BFile->InfoHead.DataSize = BFile->LineWidth * Height * 3;
}
else if(BitMode==16)
{
BFile->InfoHead.BitPerPixel = 16;
BFile->InfoHead.DataSize = BFile->LineWidth * Height * 2;
}
else
return -1;
BFile->InfoHead.CompresType = 0;
BFile->InfoHead.HResolution = 0;
BFile->InfoHead.VResolution = 0;
BFile->InfoHead.Colors = 0;
BFile->InfoHead.ImportColor = 0;
BFile->FileHead.Type = 0x4d42;
BFile->FileHead.FileSize = 0x36 + BFile->InfoHead.DataSize;
BFile->FileHead.Reserved = 0;
BFile->FileHead.Offset = 0x36;
memcpy(SaveAddr, (UINT8 *)BFile + 2, 0x36);
BFile->Data = SaveAddr + 0x36;
return 0;
}
void BMP_PutPixel(BITMAP_FILE *BFile, UINT16 x, UINT16 y, COLOR Color)
{
}
#endif
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -