1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
#include <stdio.h>
#include <dlfcn.h> // 动态加载动态库的头文件:dlopen()、dlsym()
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/mman.h>
struct bitmap_header
{
int16_t type;
int32_t size; // 图像文件大小
int16_t reserved1;
int16_t reserved2;
int32_t offbits; // bmp图像数据偏移量
}__attribute__((packed));
struct bitmap_info
{
int32_t size; // 本结构大小
int32_t width; // 图像宽
int32_t height; // 图像高
int16_t planes;
int16_t bit_count; // 色深
int32_t compression;
int32_t size_img; // bmp数据大小,必须是4的整数倍
int32_t X_pel;
int32_t Y_pel;
int32_t clrused;
int32_t clrImportant;
}__attribute__((packed));
// 以下结构体不一定存在于BMP文件中,除非:
// bitmap_info.compression为真
struct rgb_quad
{
int8_t blue;
int8_t green;
int8_t red;
int8_t reserved;
}__attribute__((packed));
#define FB_FILE "/dev/fb0"
unsigned int *mem_p;
int lcd_fd;
// BMP格式头规范
int lcd_init(void);
int lcd_uninit(void);
int show_bmp_png(const char *pathname,int start_x, int start_y);
int lcd_init(void)
{
//打开file.txt文件, 文件不存在,则打开失败,如果使用O_CREAT,那必须要添加文件权限。
lcd_fd = open(FB_FILE, O_RDWR);
if(lcd_fd == -1)
{
printf("open a.txt fail\n");
return -1;
}
//屏幕映射
mem_p = (unsigned int *)mmap(NULL, 800*480*4, PROT_READ|PROT_WRITE, MAP_SHARED, lcd_fd, 0);
if(mem_p == MAP_FAILED)
{
printf("mmap fail\n");
}
return 0;
}
int lcd_uninit(void)
{
// 解除映射
munmap(mem_p, 800*480*4);
close(lcd_fd);
}
/*
参数说明:*pathname-----图片路径
start_x--------在屏幕的哪个位置显示-x轴
start_y--------在屏幕的哪个位置显示-y轴
*/
int show_bmp_png(const char *pathname,int start_x, int start_y)
{
int width, high; //图片亮度与高度
int i, j, x, y;
// 读取BMP格式头,获取图片信息
struct bitmap_header header;
struct bitmap_info info;
struct rgb_quad quad;
int bmp_fd = open(pathname, O_RDONLY);
if(bmp_fd == -1)
{
printf("open bmp fail\n");
return -1;
}
//跳过54个字节头
// 第一、二个结构体是必有信息
read(bmp_fd, &header, sizeof(header));
read(bmp_fd, &info, sizeof(info));
width = info.width;
high = info.height;
if(start_x+width > 800 || start_y+high > 480)
{
printf("%s图片显示溢出!\n",pathname);
close(bmp_fd);
return -1;
}
//变长数组
unsigned char bmpbuff[width*high*3];
unsigned int buff[width*high];
unsigned int tmpbuff[width*high];
read(bmp_fd, bmpbuff, sizeof(bmpbuff));
for(i=0; i<width*high; i++)
{
buff[i] = bmpbuff[3*i+0] | bmpbuff[3*i+1]<<8 | bmpbuff[3*i+2]<<16;
}
//正真显示图片
for(y=start_y,j=0; y<(start_y+high),j<high; y++,j++)
{
for(x=start_x,i=0; x<(start_x+width),i<width; x++,i++)
{
if(buff[(high-1-j)*width+i] == 0x00ffffff)
continue;
*(mem_p+y*800+x) = buff[(high-1-j)*width+i];
}
}
close(bmp_fd);
return 0;
}
int main()
{
show_bmp_png("1.bmp",0,0);
return 0 ;
}
|