?? bmp.c
字號:
//========================================================================
// 文件名: BMP.c
// 作 者: Xinqiang Zhang(email: xinqiang@mzdesign.com.cn)
// 日 期: 2008/01/15
// 描 述: BMP文件解碼顯示程序
//
// 參 考: 本程序直接從存儲的BMP文件當中讀取數(shù)據(jù)進行分析顯示
// 版 本:
// 2008/01/15 First version Mz Design
//========================================================================
#include "BMP.h"
#include "LCD_Dis.h"
#include "LCD_Extend.h"
#include "string.h"
#include "stdlib.h"
unsigned char bit[8]={128, 64, 32, 16, 8, 4, 2, 1};//,0x8000,0x4000,0x2000,0x1000,0x0800,0x0400,0x0200,0x0100};
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bmiHeader;
//========================================================================
// 函數(shù): void Get_Color_Table(US_COLORS * Colors_s, unsigned int nColors, RGBQUAD *fp_s)
// 描述: 從BMP的RGB顏色表中提取出用戶實際LCD所適用的色表
// 參數(shù): Colors_s 保存顏色表的緩存區(qū)
// nColors 顏色表的色彩數(shù)
// *fp_s BMP文件的色表首址
// 返回: 無
// 備注:
// 版本:
// 2008/01/15 First version
//========================================================================
void Get_Color_Table(US_COLORS * Colors_s, unsigned int nColors, RGBQUAD *fp_s)
{
unsigned int i;
unsigned int uiTemp,uiTemp2;
RGBQUAD bmiColor;
for(i=0;i<nColors;i++)
{
bmiColor = *fp_s++;
uiTemp = bmiColor.rgbRed;
uiTemp = (uiTemp<<8)&0xf800;
uiTemp2 = bmiColor.rgbGreen;
uiTemp2 = (uiTemp2<<3)&0x07e0;
uiTemp |= uiTemp2;
uiTemp2 = bmiColor.rgbBlue;
uiTemp2 = (uiTemp2>>3)&0x001f;
uiTemp |= uiTemp2;
(*Colors_s++) = uiTemp;
}
}
//========================================================================
// 函數(shù): unsigned char Get_Byte_bmpf(unsigned char *ptr,unsigned int index)
// 描述: 從BMP文件中讀取一個byte數(shù)據(jù)
// 參數(shù): *ptr BMP文件首址
// index 偏移地址
// 返回: 無
// 備注:
// 版本:
// 2008/01/15 First version
//========================================================================
unsigned char Get_Byte_bmpf(unsigned char *ptr,unsigned int index)
{
unsigned char uiTemp;
uiTemp = *(ptr+(index));
//if(index&0x01) uiTemp>>=8;
return uiTemp;
}
//========================================================================
// 函數(shù): unsigned int Bytes_Line(void)
// 描述: 計算一行所需字節(jié)數(shù)
// 參數(shù): 無
// 返回: 無
// 備注:
// 版本:
// 2008/01/15 First version
//========================================================================
unsigned int Bytes_Line(void)
{
unsigned int n;
n=bmiHeader.biWidth*bmiHeader.biBitCount;
n = ((n+31)>>5)<<2;
return n;
}
//========================================================================
// 函數(shù): void BMP_Show(unsigned int left, unsigned int top,
// unsigned char * bitmap, unsigned char stretch)
// 描述: 解碼顯示BMP文件
// 參數(shù): left 左上角橫坐標
// top 左上角縱坐標
// * bitmap BMP文件首址
// stretch 顯示放大倍數(shù)
// 返回: 無
// 備注:
// 版本:
// 2008/01/15 First version
//========================================================================
void BMP_Show(unsigned int left, unsigned int top, unsigned char * bitmap, unsigned char stretch)
{
unsigned int nColors=1;
unsigned int i,m,j,n;
unsigned int bytes_line;
unsigned int bmp_height_y,bmp_width_x;
unsigned short usTemp=0;
US_COLORS * Colors_User;
bmfHeader = *(BITMAPFILEHEADER *)bitmap;
bitmap = bitmap+sizeof(BITMAPFILEHEADER);
bmiHeader = *(BITMAPINFOHEADER *)bitmap; //獲取文件類型,即從文件頭
bitmap = bitmap+sizeof(BITMAPINFOHEADER);
if(bmiHeader.biCompress==0&&bmiHeader.biBitCount<=8)
{
nColors<<=bmiHeader.biBitCount;
if(nColors>256) nColors = 256;
Colors_User=malloc(nColors * sizeof(US_COLORS));
Get_Color_Table(Colors_User, nColors, (RGBQUAD *)bitmap);
bitmap += nColors*sizeof(RGBQUAD);
}
bytes_line = Bytes_Line(); //計算每行點需要的字節(jié)數(shù)
bitmap = bitmap+bytes_line*(bmiHeader.biHeight-1);
//設(shè)置操作窗口的X軸開始列
bmp_height_y = top+bmiHeader.biHeight*stretch-1;
bmp_width_x = left+bmiHeader.biWidth*stretch-1;
if(bmp_height_y>Dis_Y_MAX) bmp_height_y = Dis_Y_MAX;
if(bmp_width_x>Dis_X_MAX) bmp_width_x = Dis_X_MAX;
SetWindow(left,top,bmp_width_x,bmp_height_y);
bmp_height_y = (bmp_height_y-top+1)/stretch;
bmp_width_x = (bmp_width_x-left+1)/stretch;
if(bmiHeader.biCompress==0)
{
switch(bmiHeader.biBitCount)
{
case 1:
for(m=0;m<bmp_height_y;m++)
{
for(n=0;n<stretch;n++){
for(i=0;i<bmp_width_x*8;i++)
{
if((bitmap[(i>>3)]&bit[i&0x07])!=0)
{
for(j=0;j<stretch;j++) //WriteData(Colors_User[1].v[1],Colors_User[1].v[0]);
LCD_DataWrite(Colors_User[1]);
}
else for(j=0;j<stretch;j++) //WriteData(Colors_User[0].v[1],Colors_User[0].v[0]);
LCD_DataWrite(Colors_User[0]);
}}
bitmap = bitmap-(bytes_line);
}
free(Colors_User);
break;
case 4:
for(m=0;m<bmp_height_y;m++)
{
for(n=0;n<stretch;n++){
for(i=0; i<bmp_width_x; i+=2)
{
for(j=0;j<stretch;j++)
LCD_DataWrite(Colors_User[(bitmap[i>>3]>>4)%0x0f]);
for(j=0;j<stretch;j++)
LCD_DataWrite(Colors_User[(bitmap[i>>3])%0x0f]);
}}
bitmap = bitmap-(bytes_line);
}
free(Colors_User);
break;
case 8:
for(m=0;m<bmp_height_y;m++)
{
for(n=0;n<stretch;n++){
for(i=0; i<bmp_width_x; i++)
{
for(j=0;j<stretch;j++)
{LCD_DataWrite(Colors_User[bitmap[i]]);}
}}
bitmap = bitmap-(bytes_line);
}
free(Colors_User);
break;
case 24:
bmp_width_x = bmp_width_x*3;
for(m=0;m<bmp_height_y;m++)
{
for(n=0;n<stretch;n++){
for(i=0; i<bmp_width_x; i+=3)
{
for(j=0;j<stretch;j++)
{
usTemp = ((unsigned short)(bitmap[i+2])<<8)&0xf800;
usTemp |= (((unsigned short)(bitmap[i+1])<<3)&0x07e0);
usTemp |= ((unsigned short)bitmap[i]>>3)&0x001f;
LCD_DataWrite(usTemp);
}
}}
bitmap = bitmap-(bytes_line);
}
break;
}
}
EndWindow();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -