首页 > 其他分享 >将bmp图片画到开发板的指定坐标上

将bmp图片画到开发板的指定坐标上

时间:2024-05-14 14:58:50浏览次数:11  
标签:fp 画到 int unsigned 开发板 bmp BMP include

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>

/**********************************************************************************
*

  •      file name:  005_showbmp.c
    
  •      author   :  [email protected]
    
  •      date     :  2024/05/12
    
  •      function :  将bmp图片画到开发板上
    
  •      note     :  None          
    
  •      CopyRight (c) 2024-2024  [email protected]    All Right Reseverd
    
  • *******************************************************************************/
#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()

/* *********************************************************************************
*

  •      函数名称  :  ShowBmp
    
  •      函数功能  :  将bmp图片画到开发板的指定坐标上
    
  •      函数参数  :                   
    
  •                  @name_path:图片路径
    
  •                  @x_offset :bmp图片在开发板坐标的X轴的偏移量
    
  •  				@y_offset :bmp图片在开发板坐标的Y轴的偏移量
    
  •  				@addr     :mmap的映射地址(作用是提高画图效率)
    
  •      返回结果  :  无
    
  •      函数作者  :  wq`
    
  •      创建日期  :  2024/05/12
    
  •      修改历史  :  无
    
  •      函数版本  :  V1.0
    
  •      注意事项  :  None
    
  • *******************************************************************************/
void ShowBmp(char *name_path,int x_offset,int y_offset,int *addr)
{
    //1.打开待显示的BMP图像  fopen
    FILE * bmp_fp = fopen(name_path,"rb");
	if (NULL == bmp_fp)
	{
		return;
	}
    //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图*片的颜色分量  800*480*3
	char bmp_buf[800*480*3] = {0};
	fread(bmp_buf,1,800*480*3,bmp_fp);
    //4.关闭BMP
    fclose(bmp_fp);
    // int i = x_offset;
	for (int y = 0; y <headerinfo.biHeight; y++)
	{
		for (int x = 0;x < headerinfo.biWidth ; ++x)
		{
			addr[800*(headerinfo.biHeight-y+y_offset-1) + x_offset+x] = bmp_buf[(y*headerinfo.biWidth+x)*3] | 
                                                                     bmp_buf[(y*headerinfo.biWidth+x)*3+1]<<8 |
                                                                     bmp_buf[(y*headerinfo.biWidth+x)*3+2]<<16;  
		}
	}
}

int main(int argc, char const *argv[])
{
	//1.打开LCD   open  
	int lcd_fd = open("/dev/fb0",O_RDWR);


	//2.对LCD进行内存映射  mmap
	int *lcd_mp = (int *)mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcd_fd,0);

    //3.定义x_offset和y_offset存储偏移量
    int x_offset;
    int y_offset;
    printf("请输入一个在X轴的坐标(小于800)和请输入Y轴的坐标(小于480):");
    scanf("%d %d",&x_offset,&y_offset);
	//4.将图片的参数传给ShowBmp函数
	ShowBmp("./photo/Hz.bmp",x_offset,y_offset,lcd_mp);
	//5.关闭LCD
	close(lcd_fd);
    //6.取消映射
	munmap(lcd_mp,800*480*4);

	return 0;
}

标签:fp,画到,int,unsigned,开发板,bmp,BMP,include
From: https://www.cnblogs.com/wwwwariana/p/18191296

相关文章