在lcd屏幕上的任意位置显示任意大小的图片

/*************************************************
 *
 *   file name:ShowBmp2.c
 *   author   :momolyl@126.com
 *   date     :2024/05/12
 *   brief    :在lcd屏幕上的任意位置显示任意大小的图片
 *   note     :None
 *
 *   CopyRight (c) 2024    momolyl@126.com    All Right Reseverd
 *
 **************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdlib.h>
#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(argv[1], "rb");
	if (NULL == bmp_fp)
	{
		return -1;
	}

	// 2.读取BMP文件的图像信息,获取BMP的宽和高
	BITMAPINFOHEADER pic;
	fseek(bmp_fp, 14, SEEK_SET);
	fread(&pic, 1, 40, bmp_fp); // 读取40字节
	printf("bmp width = %d,height = %d\n", pic.biWidth, pic.biHeight);

	// 3.读取BMP图片的颜色分量  pic.biWidth*pic.biHeight*3

	char *bmp_buf = (char *)calloc(pic.biWidth * pic.biHeight * 3, 1);
	int offset = (4 - (pic.biWidth * 3) % 4) % 4;
	for (int i = 0; i < pic.biHeight; i++)
	{
		fread(bmp_buf + i * pic.biWidth * 3, 1, pic.biWidth * 3, bmp_fp);
		fseek(bmp_fp, offset, SEEK_CUR);
	}

	// 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;
	int X = 0, Y = 0;
	printf("please input the picture's site:");
	scanf("%d,%d", &X, &Y);
	for (int y = Y + pic.biHeight - 1; y >= Y; y--)
	{
		for (int x = X; x < X + pic.biWidth; ++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, 800 * 480 * 4);

	return 0;
}
posted @   铃是铃铛的铃  阅读(47)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示