用虚拟机和crt做的。
内容是将bmp显示到lcd板子上,
打开开发版成功了,就是打不开bmp,怎么解决
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
int *plcd = NULL;
int init_lcd(){
int fd = open("/dev/fb0",O_RDWR);//可读可写打开显示屏
if(fd < 0){
perror("fail to open");
return -1;
}
plcd = (int*)mmap(NULL,800*480*4,PROT_READ | PROT_WRITE,MAP_SHARED,fd,0);
//读写权限 映射标志
if(plcd == MAP_FAILED){//失败返回MAP_FALED,同时errno被设置
perror("mmap error");
return -1;
}
return fd;
}
void close_lcd(int fd){
munmap(plcd,800*480*4);//解映射
close(fd);
}
/*
display_point:显示一个点
x:像素点的横坐标
y:像素点的纵坐标
color:要显示的颜色
*/
void display_point(int x,int y,int color){
if(x>=0 && x<800 && y>=0 && y<480){//像素点在开发板上有效
*(plcd + 800*y+x) = color;
}
}
/*
功能:解析bmp图片并且居中显示
参数:
@bmpname:图片名
@x0:从哪列开始
@y0:从哪行开始
返回值:0是没问题
-1,-2,-3对应的出错
*/
int display_bmp(const char *bmpname,int x0,int y0)
{
//1、打开图片
int fd = open(bmpname,O_RDONLY);
if(fd < 0){
printf("fail to open bmp");
return -1;
}
//2、判断是不是bmp图片
char buf[2]={0};
read(fd,buf,2);
if(buf[0] != 'B' || buf[1] != 'M'){
printf("Not a bmp file\n");
close(fd);
return -2;
}
//3、获取图片属性 宽、高、色深
int width,height,depth;
lseek(fd,0x12,SEEK_SET);
read(fd,&width,4);
read(fd,&height,4);
lseek(fd,0x1C,SEEK_SET);
read(fd,&depth,2);
if(!(depth == 24 || depth == 32))
{
printf("NOT depth\n");
close(fd);
return -3;
}
printf("bmpname : %s , width = %d , height = %d , depth = %d\n",bmpname,width,height,depth);
//4、获取像素数组
int size = width*height*depth/8;
char *pbuf = (char *)malloc(size);
lseek(fd,54,SEEK_SET);
read(fd,pbuf,size);
//5、显示这个图片 就是一个一个像素点显示
//进行显示
for(int x = 0;x < height;x++){
for(int y = 0;y < width;y++){
char b = *(pbuf + x * width * depth / 8 + y * depth / 8);
char g = *(pbuf + x * width * depth / 8 + y * depth / 8 + 1);
char r = *(pbuf + x * width * depth / 8 + y * depth / 8 + 2);
unsigned int color = r << 16 | g << 8 | b;
display_point(x0+x,y0+y,color);
}
}
free(pbuf);
close(fd);
return 0;
}
int main()
{
int fd = init_lcd();
char bmpname[] = "lufei.bmp";
display_bmp(bmpname,0,0);
close_lcd(fd);
}