文件IO-在屏幕上显示图像

在屏幕上输出行字节为4的整数倍的bmp格式的图像,通过键盘指定要显示的文件名称,屏幕尺寸为1024*600

/**************************************************************************
*
*
* 设计在屏幕指定位置显示图像的接口
* author:jindouliu2024@163.com 
* date:2025.4.18
* 
*
* Copyright (c)  2024-2025   jindouliu2024@163.com   All right Reserved
* **************************************************************************/

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

#include <unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include <sys/mman.h>
#pragma pack(1)
typedef struct tagBITMAP_FILE_HEADER
{
        short        bfType;//文件标识
        int          bfSize;//文件大小
        short        bfReserved1;//保留字
        short        bfReserved2;//保留字
        int          bfOffBits;//文件指示器偏移量相较于文件开头
} bpFile_Header, *PbpFile_Header;
typedef struct tagBITMAPINFOHEADER
{
        int          bpSize;//图像描述信息块的大小
        int          bpWidth;//图像宽度
        int          bpHeight;//图像高度
        short        bpPlanes;//图像的plane总数(恒为1)
        short        bpBitCount;//记录颜色的位数取值1(双色),4,6,24,32
        int          bpCompression;//数据压缩方式(0:不压缩;1:8位压缩;2:4位压缩)
        int          bpSizeImage;//图像区数据的大小,必须是4的倍数
        int          bpXPelsPerMeter;//水平每米有多少像素,在设备无关位图中,填写00H
        int          bpYPelsPerMeter;//垂直每米有多少像素,在设备无关位图中,填写00H
        int          bpClrUsed;// 此图像所有的颜色数,不用,固定为0
        int          bpClrImportant;// 重要颜色数,不用,固定为0
} bpINFO_HEADER, *PbpINFO_HEADER;
#pragma pack()

bool ShowPicture(char *name,int x,int y)//
{
	//定义存储图像信息的结构体
	bpINFO_HEADER bp_infoheader;
	//打开图像文件
	FILE *fp = fopen(name,"rb");
	if(fp == NULL){
		printf("open picture failed\n ");
		return -1;
	}
	//读取文件头信息
	fseek(fp,14,SEEK_SET);
	fread(&bp_infoheader,1,40,fp);
	//定义存储数据的数组
	char info_buf[bp_infoheader.bpWidth*bp_infoheader.bpHeight*3];
	//读取图像信息
	fread(info_buf,1,bp_infoheader.bpWidth*bp_infoheader.bpHeight*3,fp);
	printf("width = %d,heigth =%d",bp_infoheader.bpWidth,bp_infoheader.bpHeight);
	//关闭图像文件
	fclose(fp);
	//打开屏幕文件
	int lcd_dp = open("/dev/fb0",O_RDWR);
	//将图像信息写入mmap
	//申请mmap的空间
	short *lcd_mp = (short *)mmap(NULL,1024*600*2,PROT_READ|PROT_WRITE,MAP_SHARED,lcd_dp,0);
	//按高度扫描
	int i=0;
	short data=0;
	for(int heigth =  x+bp_infoheader.bpHeight-1;heigth >= x ;heigth--){
		//按宽度扫描
		for(int width = y;width < y+bp_infoheader.bpWidth;width++){
			//写入像素值
			data = (info_buf[i]/8)|((info_buf[i+1]/4)<<5)|((info_buf[i+2]/4)<<11);//针对位深为2的屏幕
			*(lcd_mp+1024*heigth+width) = data;
			i+=3;
			data = 0;
		}
	}
	//关闭屏幕文件并解除内存映射
	munmap(lcd_mp,1024*600*2);
	close(lcd_dp);

	
}
int main(int argc, char *argv[])
{
	ShowPicture(argv[1],100,100);
	 return 0;

}

来源链接:https://www.cnblogs.com/lradian/p/18834287

© 版权声明
THE END
支持一下吧
点赞13 分享
评论 抢沙发
头像
请文明发言!
提交
头像

昵称

取消
昵称表情代码快捷回复

    暂无评论内容