中级会员
 
- 积分
- 233
- 金钱
- 233
- 注册时间
- 2017-12-4
- 在线时间
- 86 小时
|
3金钱
//BMP编码函数
//将当前LCD屏幕的指定区域截图,存为16位格式的BMP文件 RGB565格式.
//保存为rgb565则需要掩码,需要利用原来的调色板位置增加掩码.这里我们已经增加了掩码.
//保存为rgb555格式则需要颜色转换,耗时间比较久,所以保存为565是最快速的办法.
//filename:存放路径
//x,y:在屏幕上的起始坐标
//mode:模式.0,仅仅创建新文件的方式编码;1,如果之前存在文件,则覆盖之前的文件.如果没有,则创建新的文件.
//返回值:0,成功;其他,错误码.
u8 bmp_encode(u8 *filename,u16 x,u16 y,u16 width,u16 height,u8 mode)
{
FIL* f_bmp;
u16 BMPHeadSize;
BITMAPINFO HBMP;
u8 res=0;
u16 tx,ty; //图像尺寸
u16 *databuf; //数据缓存区地址
u16 pixcnt; //像素计数器
u16 bi4width; //水平像素字节数
if(width==0||height==0)return 1; //区域错误
if((x+width-1)>TLCD_W) return 1; //区域错误
if((y+height-1)>TLCD_H)return 1; //区域错误
#if BMP_USE_MALLOC == 1 //使用malloc
databuf=(u16*)pic_memalloc(720); //开辟至少bi4width大小的字节的内存区域 ,对240宽的屏,480个字节就够了.
if(databuf==NULL)return 2; //内存申请失败.
f_bmp=(FIL *)pic_memalloc(sizeof(FIL)); //开辟FIL字节的内存区域
if(f_bmp==NULL) //内存申请失败.
{
pic_memfree(databuf);
return 2;
}
#else
databuf=(u16*)bmpreadbuf;
f_bmp=&f_bfile;
#endif
BMPHeadSize=sizeof(HBMP); //得到bmp文件头的大小
mymemset((u8*)&HBMP,0,sizeof(HBMP)); //清空申请到的内存
//写入文件头数据
HBMP.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); //信息头大小
HBMP.bmiHeader.biWidth=width; //BMP宽度
HBMP.bmiHeader.biHeight=height; //BMP高度
HBMP.bmiHeader.biPlanes=1; //恒定为1
HBMP.bmiHeader.biBitCount=16; //BMP为16位色
HBMP.bmiHeader.biCompression=BI_BITFIELDS;//每个像素的比特大小由指定掩码组成(调色板处)
HBMP.bmiHeader.biSizeImage=HBMP.bmiHeader.biHeight*HBMP.bmiHeader.biWidth*HBMP.bmiHeader.biBitCount/8; //BMP数据区的字节数大小(宽*高*(颜色位数/8))
HBMP.bmfHeader.bfType=((u16)'M'<<8)+'B'; //BM格式标志
HBMP.bmfHeader.bfSize=BMPHeadSize+HBMP.bmiHeader.biSizeImage; //整个BMP文件的字节数大小(头文件大小+数据区大小)
HBMP.bmfHeader.bfOffBits=BMPHeadSize; //从文件开始到数据区的偏移量
HBMP.RGB_MASK[0]=0x0000F800; //红色掩码
HBMP.RGB_MASK[1]=0x00007E00; //绿色掩码
HBMP.RGB_MASK[2]=0x0000001F; //蓝色掩码
//判断模式
if (mode==1)res=f_open(f_bmp,(const TCHAR*)filename,FA_CREATE_ALWAYS|FA_WRITE);
else if(mode==0||res==0x04)f_open(f_bmp,(const TCHAR*)filename,FA_WRITE|FA_CREATE_NEW);
else return 1;
//判断水平像素是否为4倍数
if((HBMP.bmiHeader.biWidth*2)%4)
bi4width=((HBMP.bmiHeader.biWidth*2)/4+1)*4;//实际要写入的宽度像素,必须为4的倍数(BMP规定).
else
bi4width=HBMP.bmiHeader.biWidth*2;
if(res==FR_OK)
{
res=f_write(f_bmp,(u8*)&HBMP,BMPHeadSize,&bw); //写入BMP文件头
for(ty=y+height-1;HBMP.bmiHeader.biHeight;ty--)
{
pixcnt=0;
for(tx=x;pixcnt!=(bi4width/2);)
{
if(pixcnt<HBMP.bmiHeader.biWidth)
databuf[pixcnt]=TLCD_ReadPoint(tx,ty);//读取坐标点的值
else
databuf[pixcnt]=0xFFFF;//补充白色的像素
pixcnt++;
tx++;
}
HBMP.bmiHeader.biHeight--;
res=f_write(f_bmp,(u8*)databuf,bi4width,&bw);//写入数据
}
f_close(f_bmp);
}
#if BMP_USE_MALLOC == 1 //使用malloc
pic_memfree(databuf);
pic_memfree(f_bmp);
#endif
return res;
}
第一次接触这么复杂的程序,还望赐教
|
|