在开发板的屏幕上显示一张800*480的bmp格式图片

/*********************************************************************************
* @Description  : 展示一张800*480的bmp图片
* @Author       : ice_cui
* @Date         : 2025-04-28 22:53:46
* @Email        : Lazypanda_ice@163.com
* @LastEditTime : 2025-04-29 00:15:56
* @Version      : V1.0.0
* @Copyright    : Copyright (c) 2025 Lazypanda_ice@163.com All rights reserved.
**********************************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#pragma pack(1)


typedef struct BITMAP_FILE_HEADER
{
    short        bfType;//文件标识
    int          bfSize;//文件大小
    short        bfReserved1;//保留字
    short        bfReserved2;//保留字
    int          bfOffBits;//文件指示器偏移量相较于文件开头
} bpFile_Header, *PbpFile_Header;
//信息头结构体
typedef struct BITMAP_INFO_HEADER
{
    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()

int main(int argc, char const *argv[])
{
    //1,打开待显示的bmp图像
    FILE *bmp_fp = fopen("1.bmp","rb");
    if (NULL == bmp_fp)
    {
        return -1;
    }
    
    //2,读取bmp文件的图像信息,获取bmp的宽和高
    bpInfo_Header headerinfo;
    fseek(bmp_fp,14,SEEK_SET);
    fread(&headerinfo,1,40,bmp_fp);
    printf("bmp width = %d,height = %d\n",headerinfo.bpWidth,headerinfo.bpHeight);

    //3,读取bmp文件的颜色分量
    char bmp_buf[800*480*3] = {0};
    fread(bmp_buf,1,800*480*3,bmp_fp);

    //4,关闭bmp
    fclose(bmp_fp);

    //5,打开LCD
    int lcd_fd = open ("/dev/fb0",O_RDWR);
    if (-1 == lcd_fd)
    {
        perror("open lcd error");
        return -1;
    }
    //6,对LCD屏进行内容映射 mmap函数
    int *lcd_mp = (int*)mmap(NULL,
                            800*480*4,
                            PROT_READ|PROT_WRITE,
                            MAP_SHARED,
                            lcd_fd,
                            0);
    if (MAP_FAILED == lcd_mp)
    {
        printf("mmap for lcd is error\n");
        return -1;
    }
    //7,把颜色分量写入到申请的映射内存空间中
    int i = 0;
    for (int x = 479; x >= 0; x--)
    {
        for (int y = 0; y < 800; y++,i += 3)
        {
            lcd_mp[800*x +y] = bmp_buf[i] | bmp_buf[i+1]<<8 | bmp_buf[i+2]<<16;
        }
        
    }
    //8,解除映射并关闭LCD
    munmap(lcd_mp,800*480*4);
    close(lcd_fd);
    return 0;
}

posted @ 2025-04-29 00:48  ice_cui  阅读(311)  评论(3)    收藏  举报