#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
int main(int argc, char **argv)
{
int fd;
if (argc != 2)
{
fprintf(stderr, "Usage: ./a.out pnm_file\n");
return -1;
}
fd = open("/dev/fb0", O_RDWR);
if (fd < 0)
{
perror("opne fb0");
return -1;
}
struct fb_fix_screeninfo fix_info;
struct fb_var_screeninfo var_info;
int ret, x, y;
int line_length;
ret = ioctl(fd, FBIOGET_FSCREENINFO, &fix_info);
if (ret < 0)
{
perror("ioctl fix_info");
close(fd);
return -2;
}
ret = ioctl(fd, FBIOGET_VSCREENINFO, &var_info);
if (ret < 0)
{
perror("ioctl var_info");
close(fd);
return -3;
}
line_length = fix_info.line_length;
y = var_info.yres;
x = line_length / (var_info.bits_per_pixel / 8);
unsigned char *fb_addr = mmap(NULL, y * line_length, PROT_READ, MAP_SHARED, fd, 0);
if (MAP_FAILED == fb_addr)
{
perror("mmap");
close(fd);
return -4;
}
FILE *fl;
fl = fopen(argv[1], "w");
if (NULL == fl)
{
fprintf(stderr, "open %s failed!\n", argv[1]);
munmap(fb_addr, y * line_length);
close(fd);
return -5;
}
fprintf(fl, "P6\n%d %d\n255\n", x, y);
int i, j;
unsigned char *tmp = fb_addr;
for (i = 0; i < y; i++)
{
for (j = 0; j < x; j++)
{
fwrite(&tmp[2], 1, 1, fl);
fwrite(&tmp[1], 1, 1, fl);
fwrite(&tmp[0], 1, 1, fl);
tmp += 4;
}
}
fclose(fl);
close(fd);
munmap(fb_addr, y * line_length);
return 0;
}