!
下面是代码
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <jpeglib.h>
#include <jerror.h>
#define FB_DEV “/dev/fb0”
unsigned char fbmem;
unsigned int screensize;
unsigned int fb_width;
unsigned int fb_height;
unsigned int fb_depth;
unsigned int x;
unsigned int y;
/**************** function declaration ****************/
void usage(char msg);
int fb_open(char fb_device);
int fb_close(int fd);
int fb_stat(int fd, int width, int height, int depth);
void fb_mmap(int fd, unsigned int screensize);
int fb_munmap(void start, size_t length);
int fb_pixel(void fbmem, int width, int height,
int x, int y, int r, int g, int b);
unsigned char * display_jpeg(char * filename,int w, int h);
/ function implementation ********************/
unsigned char * display_jpeg(char * filename, int *w, int *h)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *infile;
unsigned char *buffer;
unsigned char *temp;
if ((infile = fopen(filename, "rb")) == NULL) {
fprintf(stderr, "open %s failed/n", filename);
exit(-1);
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
*w = cinfo.output_width;
*h = cinfo.output_height;
printf("output_width====%d\n",cinfo.output_width);
printf("output_height====%d\n",cinfo.output_height);
if ((cinfo.output_width > fb_width) ||
(cinfo.output_height > fb_height)) {
printf("too large JPEG file,cannot display/n");
return (-1);
}
buffer = (unsigned char *) malloc(cinfo.output_width *
cinfo.output_components * cinfo.output_height);
temp = buffer;
while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines(&cinfo, &buffer, 1);
buffer += cinfo.output_width * cinfo.output_components;
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return temp;
fclose(infile);
}
void usage(char *msg)
{
fprintf(stderr, “%s/n”, msg);
printf(“Usage: fv some-jpeg-file.jpg/n”);
}
int fb_open(char *fb_device)
{
int fd;
if ((fd = open(fb_device, O_RDWR)) < 0)
{
perror(__func__);
return (-1);
}
return (fd);
}
/*
- get framebuffer’s width,height,and depth.
- return 0 if success, else return -1.
*/
int fb_stat(int fd, int *width, int *height, int *depth)
{
struct fb_fix_screeninfo fb_finfo;
struct fb_var_screeninfo fb_vinfo;
if (ioctl(fd, FBIOGET_FSCREENINFO, &fb_finfo))
{
perror(__func__);
return (-1);
}
if (ioctl(fd, FBIOGET_VSCREENINFO, &fb_vinfo))
{
perror(__func__);
return (-1);
}
*width = fb_vinfo.xres;
*height = fb_vinfo.yres;
*depth = fb_vinfo.bits_per_pixel;
return (0);
}
void *fb_mmap(int fd, unsigned int screensize)
{
caddr_t fbmem;
if ((fbmem = mmap(0, screensize, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0)) == MAP_FAILED)
{
perror(__func__);
return (void *) (-1);
}
return (fbmem);
}
int fb_munmap(void *start, size_t length)
{
return (munmap(start, length));
}
int fb_close(int fd)
{
return (close(fd));
}
int fb_pixel(void *fbmem, int width, int height,
int x, int y, int r, int g, int b)
{
if ((x > width) || (y > height))
return (-1);
unsigned char *dst = fbmem + y * 5504 + x * 4; //'5504'为个人PC机的bpp
dst[0] = b;
dst[1] = g;
dst[2] = r;
return (0);
}
int main1(char *name)
{
int fbdev;
char *fb_device;
int w ,h,i,j;
unsigned short color;
fb_width=320;
fb_height=240;
fb_depth=16;
unsigned char *imgbuf, r, g, b;
imgbuf = display_jpeg(name,&w,&h);
for(j = 0; j < h; j++)
{
for( i = 0; i < w; i++)
{
color= imgbuf[i*3 + j*w*3+2];
color<<=8;
color|= imgbuf[i*3 + j*w*3 + 1];
color<<=8;
color|= imgbuf[i*3 + j*w*3];
draw_dot(i,j,rgb2tft(color));
}
}
return (0);
}
void drawbmp(unsigned int x,unsigned int y,unsigned char *name)
{
main1(name);
}