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

📄 bmp.c

📁 MzT24-2模块驱动例程(Keil LPC22XX)320*240TFT液晶屏驱动程序
💻 C
字号:
//========================================================================
// 文件名: BMP.c
// 作  者: Xinqiang Zhang(email: xinqiang@mzdesign.com.cn)
// 日  期: 2008/01/15
// 描  述: BMP文件解码显示程序
//
// 参  考: 本程序直接从存储的BMP文件当中读取数据进行分析显示
// 版  本:
//      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;
//========================================================================
// 函数: void Get_Color_Table(US_COLORS * Colors_s, unsigned int nColors, RGBQUAD *fp_s)
// 描述: 从BMP的RGB颜色表中提取出用户实际LCD所适用的色表
// 参数: Colors_s	保存颜色表的缓存区
//		 nColors  	颜色表的色彩数
//		 *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;
	}
}
//========================================================================
// 函数: unsigned char Get_Byte_bmpf(unsigned char *ptr,unsigned int index)
// 描述: 从BMP文件中读取一个byte数据
// 参数: *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;
}
//========================================================================
// 函数: unsigned int Bytes_Line(void)
// 描述: 计算一行所需字节数
// 参数: 无
// 返回: 无
// 备注: 
// 版本:
//      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;
}
//========================================================================
// 函数: void BMP_Show(unsigned int left, unsigned int top, 
//						unsigned char * bitmap, unsigned char stretch)
// 描述: 解码显示BMP文件
// 参数: left	左上角横坐标
//		 top	左上角纵坐标
//		 * bitmap	BMP文件首址
//		 stretch	显示放大倍数
// 返回: 无
// 备注: 
// 版本:
//      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();				//计算每行点需要的字节数
    bitmap = bitmap+bytes_line*(bmiHeader.biHeight-1);
    
	//设置操作窗口的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();   
}

⌨️ 快捷键说明

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