中级会员
积分 452
金钱 452
注册时间 2019-5-20
在线时间 65 小时
5 金钱
刚学完LCD的驱动,觉得linux默认的LCD驱动应该可以显示图片汉字的,下面是在友善的tiny4412板子可以显示的应用程序在I.MX6ULL板子上面执行遇到Segmentation fault 分段内存访问错误,求助解释,谢谢
/******************************************************************************
****Nane : main(int argc, char *argv[] )
****Usge : ./app 240x320.bmp 图片路径根据自己实际情况修改
****Note : this application only to show for input file bmp24 ,and display mode is 16bpp:565
******************************************************************************/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
//14byte
typedef struct
{
char cfType[2]; /* 文件类型, 必须为 "BM" (0x4D42)*/
char cfSize[4]; /* 文件的大小(字节) */
char cfReserved[4]; /* 保留, 必须为 0 */
char cfoffBits[4]; /* 位图阵列相对于文件头的偏移量(字节)*/
}__attribute__((packed)) BITMAPFILEHEADER; /* 文件头结构 */
//40byte
typedef struct
{
char ciSize[4]; /* size of BITMAPINFOHEADER */
char ciWidth[4]; /* 位图宽度(像素) */
char ciHeight[4]; /* 位图高度(像素) */
char ciPlanes[2]; /* 目标设备的位平面数, 必须置为1 */
char ciBitCount[2]; /* 每个像素的位数, 1,4,8或24 */
char ciCompress[4]; /* 位图阵列的压缩方法,0=不压缩 */
char ciSizeImage[4]; /* 图像大小(字节) */
char ciXPelsPerMeter[4];/* 目标设备水平每米像素个数 */
char ciYPelsPerMeter[4];/* 目标设备垂直每米像素个数 */
char ciClrUsed[4]; /* 位图实际使用的颜色表的颜色数 */
char ciClrImportant[4]; /* 重要颜色索引的个数 */
}__attribute__((packed)) BITMAPINFOHEADER; /* 位图信息头结构 */
//the bpm(888) File pix struct;
typedef struct
{
unsigned short blue:8;
unsigned short green:8;
unsigned short red: 8;
}__attribute__((packed)) PIXEL;
BITMAPFILEHEADER FileHead;
BITMAPINFOHEADER InfoHead;
char *fbp = 0;
int xres = 0;
int yres = 0;
int bits_per_pixel = 0;
int show_bmp ( char *bmpfile );
long chartolong ( char * string, int length );
/***************************************************************************
****mode : Showbmp.c
****Note : this application only to show for input file bmp24 ,and display mode is 16bpp:565
****************************************************************************/
int main ( int argc, char *argv[] )
{
int fbfd = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screensize = 0;
// Open the file for reading and writing
fbfd = open("/dev/fb0", O_RDWR);
if (!fbfd)
{
printf("Error cannot open framebuffer device.\n");
exit(1);
}
// Get fixed screen information
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo))
{
printf("Error reading fixed information.\n");
exit(2);
}
// Get variable screen information
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo))
{
printf("Error reading variable information.\n");
exit(3);
}
printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel );
xres = vinfo.xres;
yres = vinfo.yres;
bits_per_pixel = vinfo.bits_per_pixel;
// Figure out the size of the screen in bytes
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
// Map the device to memory
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
fbfd, 0);
if ((int)fbp == -1)
{
printf("Error failed to map framebuffer device to memory.\n");
exit(4);
}
printf("sizeof header=%d\n", sizeof(BITMAPFILEHEADER));
printf("into show_bmp fun\n");
show_bmp( argv[1] );
munmap(fbp, screensize);
close(fbfd);
return 0;
}
inline unsigned short make16color(unsigned char r, unsigned char g, unsigned char b)
{
return (
(((r >> 3) & 31) << 11) |
(((g >> 2) & 63) << 5) |
((b >> 3) & 31) );
}
/******************************************************************************
*
******************************************************************************/
int show_bmp( char *bmpfile )
{
FILE *fp;
int rc;
int ciBitCount, ciWidth, ciHeight;
int line_x, line_y;
long int location = 0, BytesPerLine = 0;
char tmp[1024*10];
printf("DDD!\n");
/* 打开位图文件 */
fp = fopen( bmpfile, "rb" );
if (fp == NULL)
{
printf("fopen error!\n");
return( -1 );
}
/* 读取位图文件头 */
rc = fread( &FileHead, sizeof(BITMAPFILEHEADER),1, fp );
if ( rc != 1)
{
printf("read header error!\n");
fclose( fp );
return( -2 );
}
printf("if (memcmp(FileHead.cfType, \"BM\", 2) != 0)\n");
/* 判断位图的类型 */
if (memcmp(FileHead.cfType, "BM", 2) != 0)
{
printf("it's not a BMP file\n");
fclose( fp );
return( -3 );
}
printf("fread( (char *)&InfoHead, sizeof(BITMAPINFOHEADER),1, fp )\n");
/* 读取位图信息头 */
rc = fread( (char *)&InfoHead, sizeof(BITMAPINFOHEADER),1, fp );
if ( rc != 1)
{
printf("read infoheader error!\n");
fclose( fp );
return( -4 );
}
ciWidth = (int) chartolong( InfoHead.ciWidth, 4 );
ciHeight = (int) chartolong( InfoHead.ciHeight, 4 );
ciBitCount = (int) chartolong( InfoHead.ciBitCount, 4 );
//skip the bmpfile head and file(InfoHead),mov file point to the addr for color data.
fseek(fp, (int)chartolong(FileHead.cfoffBits, 4), SEEK_SET);
//BytesPerLine = (ciWidth * ciBitCount + 31) / 32 * 4;
BytesPerLine = (ciWidth * ciBitCount) / 8;
printf("width=%d, height=%d, bitCount=%d, offset=%d,BytesPerLine=%d\n", ciWidth, ciHeight, ciBitCount, (int)chartolong(FileHead.cfoffBits, 4),BytesPerLine);
line_x = line_y = 0;
while (!feof(fp))//循环读取文件数据直到文件结尾
{
PIXEL pix;
unsigned int tmp=0xffffffff;
/* fread(buffer,size,count,fp);
fwrite(buffer,size,count,fp);
(1)buffer:是一个指针,对fread来说,它是读入数据的存放地址。对fwrite来说,是要输出数据的地址。
(2)size:要读写的字节数;
(3)count:要进行读写多少个size字节的数据项;
(4)fp:文件型指针。
return:返回实际读取到的count数目,如果此值比参数count来得小,则代表可能读到了文件尾或有错误发生,这时必须用feof()或ferror()来决定发生什么情况。
*/
rc = fread( (char *)&pix, 3, 1, fp );//因本程序的bmp图是24bpp,其文件中是以b:8 g:8 r:8的形式存放数据,,所在一次读3字节,
if (rc != 1 )//如果读取出错则退出
{ break; }
//计算图片在framebuff 中的起始地址
location = line_x * bits_per_pixel / 8 + (ciHeight - line_y - 1) * xres * bits_per_pixel / 8;
//rgb888 to rgb565
//tmp = make16color(pix.red,pix.green,pix.blue);
tmp = pix.red <<16 | pix.green << 8 | pix.blue<<0;
//fill color to framebuff
*((unsigned int*)(fbp + location)) = tmp;
//next point
line_x++;
if (line_x == ciWidth )
{
line_x = 0;
line_y++;
if (line_y==ciHeight-1)
{
break;
}
}
}
fclose( fp );
return( 0 );
}
/******************************************************************************
* 把存放在内存中的字符(实际是我们的数字在内存中存放以字节形式存放的,
如0x12345678,其实是0x78,0x56,0x34,0x12这样存放,这信函数只是把这种存放方式
拼接为一个长整形数据返回。
******************************************************************************/
long chartolong( char * string, int length )
{
long number;
if (length <= 4)
{
memset( &number, 0x00, sizeof(long) );
memcpy( &number, string, length );
}
return( number );
}
可以拿张bmp的图片试试,./app *.bmp
我来回答