关于在lcd上显示一张jpg图片的程序~~编译通过了,但启动程序后没有任何显示~~求解!!
头文件,myhead.h
========================
#ifndef __MYHEAD_H
#define __MYHEAD_H
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <strings.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/mman.h> // for mmap
#include <linux/fb.h>
#include "jpeglib.h"
struct RGB
{
int width;
int height;
int bpp; // bits per pixel
char *rgb_data;
};
bool jpg2rgb(struct RGB *, char *, struct stat *);
#endif
==================================
jpg_demo.c
===================================
#include "myhead.h"
bool jpg2rgb(struct RGB *rgb, char *jpg_data,
struct stat *jpg_info)
{
// [固定步骤]声明JPG信息结构体,以及错误管理结构体
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
// [固定步骤]使用缺省的出错处理来初始化cinfo
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
// [固定步骤]配置该cinfo,使其从jpg_data中读取st_size个字节
jpeg_mem_src(&cinfo, jpg_data, jpg_info->st_size);
// [固定步骤]读取JPG数据的头,并判断其格式是否合法
if(jpeg_read_header(&cinfo, true) != 1)
{
perror("这不是JPG图片");
return false;
}
// [固定步骤]开始解码
jpeg_start_decompress(&cinfo);
rgb->width = cinfo.output_width;
rgb->height= cinfo.output_height;
rgb->bpp = cinfo.output_components*8;
// 根据图片的尺寸大小,分配一块相应的内存rgb_data
// 用来存放从jpg_data解码出来的RGB数据
unsigned long rgb_size = cinfo.output_width *
cinfo.output_height *
cinfo.output_components;
rgb->rgb_data = calloc(1, rgb_size);
int row_stride = cinfo.output_width * cinfo.output_components;
while(cinfo.output_scanline < cinfo.output_height)
{
unsigned char *tmp[1];
tmp[0] = rgb->rgb_data + (cinfo.output_scanline) * row_stride;
jpeg_read_scanlines(&cinfo, tmp, 1);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return true;
}
=======================================
main.c
========================================
#include "myhead.h"
void read_jpg_image(int fd, char *jpg_data, int size)
{
int n;
char *tmp = jpg_data;
while(size > 0)
{
// n <= size
n = read(fd, tmp, size);
if(n == -1)
{
perror("read failed");
exit(0);
}
if(n == 0)
break;
size -= n;
tmp += n;
}
}
int main(int argc, char **argv) // ./showjpg xx.jpg
{
// 读取指定的JPG图片数据
int fd = open(argv[1], O_RDONLY);
struct stat *jpg_info
= calloc(1, sizeof(struct stat));
stat(argv[1], jpg_info);
char *jpg_data = calloc(1, jpg_info->st_size);
read_jpg_image(fd, jpg_data, jpg_info->st_size);
// 调用JPG标准的解码过程
// 将JPG数据变成RGB数据
struct RGB rgb;
jpg2rgb(&rgb, jpg_data, jpg_info);
// 准备好LCD,并将rgb_data妥善放置到显存上
int lcd = open("/dev/fb0", O_RDWR);
if(lcd == -1)
{
perror("打开LCD出错");
exit(0);
}
char *fbmem = mmap(NULL,
800*480*4,
PROT_READ|PROT_WRITE,
MAP_SHARED,
lcd,
0);
if(fbmem == MAP_FAILED)
{
perror("映射内存失败");
exit(0);
}
int rgb_size = rgb.width * rgb.height * rgb.bpp/8;
memcpy(fbmem, rgb.rgb_data, rgb.width);
close(fd);
close(lcd);
return 0;
}