linux framebuffer显示图片问题
加州西梅 2012-10-09 03:31:51 如题,代码如下,我写的显示图片,可是显示的效果是屏幕中有的地方就是一个紫颜色 ,大部分都没有,麻烦帮我看一下,根本就不是原来的图像,效果相差太大,图片是512*512的 图片位深度是16,麻烦请大侠帮忙看一下,我是在fedora下实现的
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
int main(void)
{
FILE *fp;
unsigned short buf[512];
int fd_fb = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screen_size = 0;
short *fbp565 = NULL;
int x = 0, y = 0;
//buf = (unsigned short *)malloc(sizeof(unsigned short)*512);
if(NULL == buf)
{
printf("malloc error");
return -1;
}
fd_fb = open("/dev/fb0", O_RDWR);
if (!fd_fb)
{
printf("Error: cannot open framebuffer device.\n");
exit(1);
}
// 获得固定屏幕参数
if (ioctl(fd_fb, FBIOGET_FSCREENINFO, &finfo))
{
printf("Error reading fixed information.\n");
exit(2);
}
// 获得可变的屏幕参数
if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &vinfo))
{
printf("Error reading variable information.\n");
exit(3);
}
// 计算像素位
screen_size =512*512*2;
screen_size=vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
printf("%dx%d, %dbpp, screen_size = %d\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel, screen_size );
fbp565 = (short *)mmap(0, screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);
if ((int)fbp565 == -1)
{
printf("Error: failed to map framebuffer device to memory.\n");
exit(4);
}
fp = fopen("./test.bmp","rb");
fseek(fp,54,SEEK_SET);
int i=0;
if(vinfo.bits_per_pixel == 16)
{
printf("16 bpp framebuffer\n");
printf("Red Screen\n");
for(y = 512; y >0 ; y--)
{
memset(buf,0x00,sizeof(unsigned short)*512);
fread(buf,512,sizeof(unsigned short),fp);
i =0;
for(x = 0; x < 512 ; x++)
{
*(fbp565 + (y-1) * vinfo.xres + x) = buf[i];
i ++;
}
}
}
else
{
printf("warnning: bpp is not 16\n");
}
munmap(fbp565, screen_size);
close(fd_fb);
return 0;
}