一、BMP图的实现
练习1:
再实现一张BMP图
//打开lcd
int lcd = open("/dev/fb0",O_RDWR);
if(lcd < 0)
{
perror("open error!\n");
return -1;
}
//lcd映射
//指针指向一个像素点
int *p = mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcd,0);
if(p == NULL)
{
perror("mmap fail!\n");
return -1;
}
//打开bmp图片
int lcd_bmp = open("1.bmp",O_RDWR);
if(lcd_bmp < 0)
{
printf("open bmp fail!\n");
return -1;
}
//去除掉头54个字节
lseek(lcd_bmp,54,SEEK_SET);
//存储bmp图片的buffer:800*480*3
char buf[800*480*3] = {0};
//读bmp图片
read(lcd_bmp,buf,800*480*3);
//将buf数据通过指针p填充到LCD中
// int i;
// for(i=0;i<800*480;i++)
// {
// *(p+i) = buf[3*i] |buf[3*i+1]<<8 |buf[3*i+2]<<16;
// }
//将buf数据通过指针p填充到LCD中
int x;//x表示横轴
int y;//y表示纵轴
for(y=0;y<480;y++)
{
for(x=0;x<800;x++)
{
/*
将buf[]里面的数据由BGR换成RGB
将3个字节封装成4个字节
*/
*(p+((479-y)*800+x)) = (buf[3*(y*800+x)+0]) | (buf[3*(y*800+x)+1]<<8) | (buf[3*(y*800+x)+2]<<16);
/*
0 = buf[0] | buf[1]<<8 | buf[2]<<16
1 = buf[3] | buf[4]<<8 | buf[5]<<16
2 = buf[6] | buf[7]<<8 | buf[8]<<16
800 = buf[800*3] | buf[800*3+1]<<8 | buf[800*3+2]<<16
*/
}
}
//lcd映射释放
munmap(p,800*480*4);
//关闭lcd\bmp
close(lcd);
close(lcd_bmp);
练习2:
实现3张照片的轮流播放
sleep(1);//延时1秒
usleep(1); //延时微秒
推展:能否将你实现bmp照片的代码封装成一个函数
int show_pic(int *p,char *bmp_path)
{
//打开bmp图片
int bmp = open(bmp_path,O_RDWR);
if(bmp < 0)
{
printf("open bmp fail!\n");
return -1;
}
//去除掉头54个字节
lseek(bmp,54,SEEK_SET);
//存储bmp图片的buffer:800*480*3
char buf[800*480*3] = {0};
int ret1 = read(bmp,buf,800*480*3);
sleep(1);
//读bmp图片
//将buf数据通过指针p填充到LCD中
int x;//x表示横轴
int y;//y表示纵轴
for(y=0;y<480;y++)
{
for(x=0;x<800;x++)
{
/*
将buf[]里面的数据由BGR换成RGB
将3个字节封装成4个字节
*/
*(p+((479-y)*800+x)) = (buf[3*(y*800+x)+0]) | (buf[3*(y*800+x)+1]<<8) | (buf[3*(y*800+x)+2]<<16);
/*
0 = buf[0] | buf[1]<<8 | buf[2]<<16
1 = buf[3] | buf[4]<<8 | buf[5]<<16
2 = buf[6] | buf[7]<<8 | buf[8]<<16
800 = buf[800*3] | buf[800*3+1]<<8 | buf[800*3+2]<<16
*/
}
}
close(bmp);
return 0;
}
int main(int argc,char *argv[])
{
//打开lcd
int lcd = open("/dev/fb0",O_RDWR);
if(lcd < 0)
{
perror("open lcd error!\n");
return -1;
}
//lcd映射
//指针指向一个像素点
int *p = mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcd,0);
if(p == NULL)
{
perror("mmap fail!\n");
return -1;
}
//指针数组存储字符串
char *bmp_path[] = {"1.bmp","zhao.bmp",