BMP图片的前54个字节保存图片信息,不保存像素点
确认图片是不是BMP类型,读取整个图片大
#pragma pack (1)
typedef struct tagBITMAPFILEHEADER
{
unsigned short bfType; //2Bytes,必须为"BM",即0x424D 才是BMP图文件
unsigned int bfSize; //4Bytes,整个BMP文件的大小
unsigned short bfReserved1; //2Bytes,保留,为0
unsigned short bfReserved2; //2Bytes,保留,为0
unsigned int bfOffBits; //4Bytes,文件起始位置到图像像素数据的字节偏移量
} BMP_BITMAPFILEHEADER;
#pragma pack ()
读取图像高度和宽图以及图像的像素位数
#pragma pack(1)
typedef struct _tagBMP_INFOHEADER
{
unsigned int biSize; //4Bytes,INFOHEADER结构体大小,存在其他版本INFOHEADER,用作区分
unsigned int biWidth; //4Bytes,图像宽度(以像素为单位)
unsigned int biHeight; //4Bytes,图像高度,+:图像存储顺序为Bottom2Top,-:Top2Bottom
unsigned short biPlanes; //2Bytes,图像数据平面,BMP存储RGB数据,因此总为1
unsigned short biBitCount; //2Bytes,图像像素位数, 1个像素占据多少位
unsigned int biCompression; //4Bytes,0:不压缩,1:RLE8,2:RLE4
unsigned int biSizeImage; //4Bytes,4字节对齐的图像数据大小
unsigned int biXPelsPerMeter; //4 Bytes,用象素/米表示的水平分辨率
unsigned int biYPelsPerMeter; //4 Bytes,用象素/米表示的垂直分辨率
unsigned int biClrUsed; //4 Bytes,实际使用的调色板索引数,0:使用所有的调色板索引
unsigned int biClrImportant; //4 Bytes,重要的调色板索引数,0:所有的调色板索引都重要
}BMP_INFOHEADER;
#pragma pack()
mmap --内存映射
函数的作用:
将软件层的一片内存空间映射到内核层
所需要的头文件:
#include <sys/mman.h>
函数原型:
void *mmap(void *addr, size_t len, int prot, int flags,int fildes, off_t off);
void *addr:内核层中映射的起始地址,一般为NULL,系统会自动找到没有分配的地址进 行映射
size_t len:需要映射的文件所占内存的大小
int prot:映射的方式 //内核区对映射文件的权限
PROT_EXEC 映射区域可被执行
PROT_READ 映射区域可被读取
PROT_WRITE 映射区域可被写入
PROT_NONE 映射区域不能存取
int flags:映射区的特性标志
MAP_SHARED:写入映射区的数据会复制回文件,且运行其他映射文件的进程共享
MAP_PRIVATE:对映射区的写入操作会产生一个映射区的复制,对此区域的修改不会写会原文件
int fildes:需要映射的文件描述符
off_t off:文件映射的偏移量,通常设置为0,代表从文件最前方开始对应,offset必须是分页大小的整数倍。
函数的返回值:
实际分配的内存的起始地址。
munmap:撤销映射
#include <sys/mman.h>
int munmap(void *addr, size_t length);
实例代码:
/* 处理BMP图片 */
char *BMP(char *path, char *path_dir)
{
chdir(path_dir);
/* 打开图片 */
int fd_bmp = open(path, O_RDWR);
if (fd_bmp == -1)
{
perror("open bmp fail");
return NULL;
}
/* 读图片类型、宽、高 */
ssize_t read_type = read(fd_bmp, &type, sizeof(type));
if (read_type == -1)
{
perror("read bmp type fail");
return NULL;
}
ssize_t read_head = read(fd_bmp, &head, sizeof(head));
if (read_head == -1)
{
perror("read bmp head fail");
return NULL;
}
/* 读取图片像素点 */
char *buf = (char *)malloc(type.size - 54);
if (buf == NULL)
{
perror("malloc buf fail");
return NULL;
}
ssize_t read_color = read(fd_bmp, buf, type.size - 54);
if (read_color == -1)
{
perror("read bmp color fail");
return NULL;
}
chdir("/IOT/project");
return buf;
}
/* LCD */
int *LCD()
{
/* 打开LCD屏 */
int fd_lcd = open("/dev/fb0", O_RDWR);
if (fd_lcd == -1)
{
perror("open lcd fail");
return;
}
/* 做映射 */
int *map = mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd_lcd, 0);
return map;
}
/* 像素点赋值 */
void INPUT_BMP(int *map, char *buf)
{
int x, y;
for (y = 0; y < head.height; y++)
{
for (x = 0; x < head.width; x++)
{
map[800 * (head.height - 1 - y) + x] = buf[3 * (head.width * y + x) + 0] << 0 |
buf[3 * (head.width * y + x) + 1] << 8 |
buf[3 * (head.width * y + x) + 2] << 16 |
0 << 24;
}
}
return;
}
/* 百叶窗显示图片 */
void WINDOW_SHADEL(int *map, char *buf)
{
int x, y, i = 0;
for (y = 0; y < head.height / 4; y++)
{
for (x = 0; x < head.width; x++)
{
for (i; i < 4, i++)
{
map[800 * (head.height - 1 - (head.height / (4 * i) + y)) + x] = buf[3 * (head.width * (head.height / (4 * i) + y) + x) + 0] << 0 |
buf[3 * (head.width * (head.height / (4 * i) + y) + x) + 1] << 8 |
buf[3 * (head.width * (head.height / (4 * i) + y) + x) + 2] << 16 |
0 << 24;
}
}
usleep(15000);
}
return;
}
/* 左下角飞入 */
void LEFT_BOTTOM(int *map, char *buf)
{
int x, y;
int j = 0, k = 0;
while (1)
{
j++;
if (k < 480 && j % 5 == 0) // x轴每加5次y轴加三次
{
k++;
k++;
k++;
}
if (j > 800)
{
break;
}
for (y = 0; y < k; y++)
{
for (x = 0; x < j; x++)
{
map[800 * (head.height - 1 - y) + x] = buf[3 * (head.width * y + x) + 0] << 0 |
buf[3 * (head.width * y + x) + 1] << 8 |
buf[3 * (head.width * y + x) + 2] << 16 |
0 << 24;
}
}
}
return;
}
/* 关门显示 */
void CLOSE_DOOR(int *map, char *buf)
{
int x, y;
for (y = 0; y <= head.width / 2; y++)
{
for (x = 0; x < head.height; x++)
{
map[800 * (head.height - 1 - x) + y] = buf[3 * (head.width * x + y) + 0] << 0 |
buf[3 * (head.width * x + y) + 1] << 8 |
buf[3 * (head.width * x + y) + 2] << 16 |
0 << 24;
map[800 * (head.height - 1 - x) + (head.width - 1 - y)] = buf[3 * (head.width * x + (head.width - y)) + 0] << 0 |
buf[3 * (head.width * x + (head.width - y)) + 1] << 8 |
buf[3 * (head.width * x + (head.width - y)) + 2] << 16 |
0 << 24;
}
usleep(10000);
}
return;
}
/* 圆形算法 */
void CIRCLE(int *map, char *buf)
{
int r, x, y;
for (r = 0; r < 448; r++)
{
for (y = 0; y < head.height; y++)
{
for (x = 0; x < head.width; x++)
{
if ((y - head.height / 2) * (y - head.height / 2) + (x - head.width / 2) * (x - head.width / 2) < r * r)
{
map[800 * (head.height - 1 - y) + x] = buf[3 * (head.width * y + x) + 0] << 0 |
buf[3 * (head.width * y + x) + 1] << 8 |
buf[3 * (head.width * y + x) + 2] << 16 |
0 << 24;
}
}
}
}
return;
}
/* 释放空间 */
void FREE(char *buf, int *map)
{
free(buf);
munmap(map, 800 * 480 * 4);
}
/* 正常显示一张BMP图片 */
int SHOW_BMP(char *path, char *path_dir)
{
/* 处理图片 */
char *buf = BMP(path, path_dir);
/* LCD */
int *map = LCD();
/* 显示图片 */
INPUT_BMP(map, buf);
/* 释放空间,关闭文件 */
FREE(buf, map);
return 0;
}
/* 百叶窗显示一张图片 */
int SHOW_BMP_WINDOW_SHADEL(char *path, char *path_dir)
{
/* 处理图片 */
char *buf = BMP(path, path_dir);
/* LCD */
int *map = LCD();
/* 显示图片 */
WINDOW_SHADEL(map, buf);
/* 释放空间,关闭文件 */
FREE(buf, map);
return 0;
}
/* 左下角飞入样式显示一张bmp */
int SHOW_BMP_LEFT_BOTTOM(char *path, char *path_dir)
{
/* 处理图片 */
char *buf = BMP(path, path_dir);
/* LCD */
int *map = LCD();
/* 显示图片 */
LEFT_BOTTOM(map, buf);
/* 释放空间,关闭文件 */
FREE(buf, map);
return 0;
}
/* 关门显示一张图片 */
int SHOW_BMP_CLOSE_DOOR(char *path, char *path_dir)
{
/* 处理图片 */
char *buf = BMP(path, path_dir);
/* LCD */
int *map = LCD();
/* 显示图片 */
// CLOSE_DOOR(map, buf);
CIRCLE(map, buf);
/* 释放空间,关闭文件 */
FREE(buf, map);
return 0;
}