复制BMP图片
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdlib.h>
/*
* 复制bmp图片
*
*
*/
#pragma pack(1)
// 定义BMP文件头部结构
typedef struct
{
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
} BITMAPFILEHEADER;
typedef struct
{
unsigned int biSize;
int biWidth; // 宽
int biHeight; // 高
unsigned short biPlanes;
unsigned short biBitCount; // 色深
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
} BITMAPINFOHEADER;
#pragma pack()
int main(int argc, char const *argv[])
{
// 1.打开BMP图像
FILE *bmp_fp = fopen("1.bmp", "rb");
if (NULL == bmp_fp)
{
return -1;
}
// 2
BITMAPFILEHEADER h1; // 文件头 14
BITMAPINFOHEADER h2; // 信息头 40
fread(&h1, 1, 14, bmp_fp);
fread(&h2, 1, 40, bmp_fp);
// 3每次读一行像素点颜色分量大小
int line_size = h2.biWidth * 3;
char *src_pbuf = (char *)calloc(1, line_size);
// 4打开新图片
FILE *new_fp = fopen("new.bmp", "wb");
// 信息写入新图片
fwrite(&h1, 1, 14, new_fp);
fwrite(&h2, 1, 40, new_fp);
// 数据写入新图片
int line_num = h2.biHeight;
while (line_num--)
{
// 读:空间+1+颜色分量+打开的原图
fread(src_pbuf, 1, line_size, bmp_fp);
// 写
fwrite(src_pbuf, 1, line_size, new_fp);
}
return 0;
}
BMP图片显示到显示器
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
/*
* 把400*240bmp图片展示到800*480的显示屏中
*
*
*/
#pragma pack(1)
// 定义BMP文件头部结构
typedef struct{
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
}BITMAPFILEHEADER;
typedef struct{
unsigned int biSize;
int biWidth; //宽
int biHeight; //高
unsigned short biPlanes;
unsigned short biBitCount; //色深
unsigned int biCompression;
unsigned int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
}BITMAPINFOHEADER;
#pragma pack()
int main(int argc, char const *argv[])
{
//1.打开待显示的BMP图像 fopen
FILE * bmp_fp = fopen("1.bmp","rb");
if (NULL == bmp_fp)
{
return -1;
}
//2.读取BMP文件的图像信息,获取BMP的宽和高
BITMAPINFOHEADER headerinfo;
fseek(bmp_fp,14,SEEK_SET);
fread(&headerinfo,1,40,bmp_fp); //读取40字节
printf("bmp width = %d,height = %d\n",headerinfo.biWidth,headerinfo.biHeight);
//3.读取BMP图*片的颜色分量 400* 240*3h
char bmp_buf[400*240*3] = {0};
fread(bmp_buf,1,400*240*3,bmp_fp);
//4.关闭BMP
fclose(bmp_fp);
//5.打开LCD open
int lcd_fd = open("/dev/fb0",O_RDWR);
//6.对LCD进行内存映射 mmap
int * lcd_mp = (int *)mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcd_fd,0);
//7.循环的把BMP图像的颜色分量依次写入到LCD的像素点中
int i = 0;
int data = 0;
for (int y = 240-1; y >= 0; y--)
{
for (int x = 0; x < 400 ; ++x)
{
//把BMP图片的一个像素点的颜色分量转换为LCD屏幕的一个像素点的颜色分量格式 ARGB <--- BGR
data |= bmp_buf[i]; //B
data |= bmp_buf[i+1]<<8; //G
data |= bmp_buf[i+2]<<16; //R
lcd_mp[800*y + x] = data; //BGR BGR BGR ....
i+=3;
data = 0;
}
}
//8.关闭LCD
close(lcd_fd);
munmap(lcd_mp,400*240*4);
return 0;
}
标签:fp,int,显示屏,unsigned,bmp,BMP,复制,include
From: https://www.cnblogs.com/zcx0326/p/18190510