/**********************************************************************
This Program Displays Raw Image (640×480) On LCD Screen Using /dev/fb0.
For Displaying Different Resolution Image You Need To Change following
values :
XRES = width
YRES = height
IMAGE_RES = XRES * YRES * BITS_PER_PIXEL / 8
***********************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define XRES 640
#define YRES 480
#define IMAGE_SIZE 921600
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
FILE *fp_image;
char *fbp = 0;
void display(char *Image_name)
{
int i = 0;
int count = 0;
int x = 0, y = 0;
char buff[IMAGE_SIZE];
long int location = 0;
fp_image = fopen(Image_name,”r” );
if(fp_image == NULL)
printf(“Image cannot be opened\n”);
count = fread(buff, 1, IMAGE_SIZE, fp_image);
fclose(fp_image);
for (y =0; y < YRES; y++)
for (x =0 ; x < XRES ; x++)
{
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + (y+vinfo.yoffset) * finfo.line_length;
*(fbp + location + 0) = buff[i++]; // blue
*(fbp + location + 1) = buff[i++] ; // green
*(fbp + location + 2) = buff[i++]; // red
*(fbp + location + 3) = 0; // No transparency
}
}
int main(int argc, char *argv[])
{
int fd = 0;
long int screensize = 0;
if(argc != 2)
{
printf(“Usage : ./disp image_name \nTry Again\n”);
exit(1);
}
system(“clear”);
fd = open(“/dev/fb0”, O_RDWR); //Getting File Descriptor for fb0
if (fd == -1)
{
perror(“Cannot Open Framebuffer Device”);
exit(1);
}
if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) //Getting fixed screen information
{
perror(“Error reading fixed information”);
exit(2);
}
if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) == -1) //Getting variable screen information
{
perror(“Error reading variable information”);
exit(3);
}
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8; //Visible screen size
fbp = (char *)mmap(0, screensize , PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); //Mapping fb device
display(argv[1]);
munmap(fbp, screensize);
close(fd);
printf(“File descriptor closed\n”);
return 0;
}
Leave a comment