imx6ull/linux应用编程学习(5)FrameBuffer的应用编程

发布于:2024-06-29 ⋅ 阅读:(12) ⋅ 点赞:(0)

什么是FrameBuffer?

        Frame 是的意思, buffer 是缓冲的意思,所以 Framebuffer 就是帧缓冲, 这意味着 Framebuffer 就是一块内存,里面保存着一帧图像。帧缓冲(framebuffer)是 Linux 系统中的一种显示驱动接口,它将显示设备(譬如 LCD) 进行抽象、 屏蔽了不同显示设备硬件的实现,对应用层抽象为一块显示内存(显存),它允许上层应用程序直接对显示缓冲区进行读写操作,而用户不必关心物理显存的位置等具体细节,这些都由Framebuffer 设备驱动来完成

        在 Linux 系统中,显示设备被称为 FrameBuffer 设备(帧缓冲设备),所以 LCD 显示屏自然而言就是 FrameBuffer 设备。 FrameBuffer 设备对应的设备文件为/dev/fbX(X 为数字, 0、 1、 2、 3 等) , Linux下可支持多个 FrameBuffer 设备,最多可达 32 个,分别为/dev/fb0 到/dev/fb31, 开发板出厂系统中, /dev/fb0设备节点便是 LCD 屏

        应用程序读写/dev/fbX 就相当于读写显示设备的显示缓冲区(显存),譬如 LCD 的分辨率是 800*480,每一个像素点的颜色用 24 位(譬如 RGB888)来表示,那么这个显示缓冲区的大小就是 800 x 480 x 24 / 8 = 1152000 个字节。 譬如执行下面这条命令将 LCD 清屏,也就是将其填充为黑色(假设 LCD 对应的设备节点是/dev/fb0,分辨率为 800*480, RGB888 格式):

dd if=/dev/zero of=/dev/fb0 bs=1024 count=1125

这条命令的作用就是将 1125x1024 个字节数据全部写入到 LCD 显存中,并且这些数据都是 0x0。

步骤:

在应用程序中,操作/dev/fbX 的一般步骤如下:

①、首先打开/dev/fbX 设备文件。

②、 使用 ioctl()函数获取到当前显示设备的参数信息,譬如屏幕的分辨率大小、像素格式,根据屏幕参数计算显示缓冲区的大小。

③、通过存储映射 I/O 方式将屏幕的显示缓冲区映射到用户空间(mmap)。

④、映射成功后就可以直接读写屏幕的显示缓冲区,进行绘图或图片显示等操作了。

⑤、完成显示后, 调用 munmap()取消映射、并调用 close()关闭设备文件。

使用 ioctl()获取屏幕参数信息:

        通 过 ioctl() 函 数 来 获 取 屏 幕 参 数 信 息 , 对 于 Framebuffer 设 备 来 说 , 常 用 的 request 包 括:

        1.FBIOGET_VSCREENINFO表示获取 FrameBuffer 设备的可变参数信息,可变参数信息使用 struct fb_var_screeninfo 结 构 体 来 描 述 , 所 以 此 时 ioctl() 需 要 有 第 三 个 参 数 , 它 是 一 个 struct fb_var_screeninfo *指针,指向 struct fb_var_screeninfo 类型对象, 调用 ioctl()会将 LCD 屏的可变参数信息保存在 struct fb_var_screeninfo 类型对象中

struct fb_var_screeninfo fb_var;
ioctl(fd, FBIOGET_VSCREENINFO, &fb_var)

        2.FBIOPUT_VSCREENINFO表示设置 FrameBuffer 设备的可变参数信息,既然是可变参数,那说明应用层可对其进行修改、重新配置,当然前提条件是底层驱动支持这些参数的动态调整,譬如在我们的 Windows 系统中,用户可以修改屏幕的显示分辨率,这就是一种动态调整。同样此时 ioctl()需要有第三个参数, 也是一个 struct fb_var_screeninfo *指针,指向 struct fb_var_screeninfo 类型对象, 表示用 struct fb_var_screeninfo 对象中填充的数据设置 LCD, 如下所示:

struct fb_var_screeninfo fb_var = {0};
/* 对 fb_var 进行数据填充 */
......
......
/* 设置可变参数信息 */
ioctl(fd, FBIOPUT_VSCREENINFO, &fb_var)

        3.FBIOGET_FSCREENINFO: 表示获取 FrameBuffer 设备的固定参数信息,既然是固定参数,那就意味着应用程序不可修改。固定参数信息使用struct fb_fix_screeninfo结构体来描述,所以此时ioctl()需要有第三个参数,它是一个 struct fb_fix_screeninfo *指针,指向 struct fb_fix_screeninfo 类型对象,调用 ioctl()会将 LCD 的固定参数信息保存在 struct fb_fix_screeninfo 对象中,如下所示:

struct fb_fix_screeninfo fb_fix;
ioctl(fd, FBIOGET_FSCREENINFO, &fb_fix);

struct fb_var_screeninfo 结构体:

struct fb_var_screeninfo {
__u32 xres; /* 可视区域,一行有多少个像素点, X 分辨率 */
__u32 yres; /* 可视区域,一列有多少个像素点, Y 分辨率 */
__u32 xres_virtual; /* 虚拟区域,一行有多少个像素点 */
__u32 yres_virtual; /* 虚拟区域,一列有多少个像素点 */
__u32 xoffset; /* 虚拟到可见屏幕之间的行偏移 */
__u32 yoffset; /* 虚拟到可见屏幕之间的列偏移 */
__u32 bits_per_pixel; /* 每个像素点使用多少个 bit 来描述,也就是像素深度 bpp */
__u32 grayscale; /* =0 表示彩色, =1 表示灰度, >1 表示 FOURCC 颜色 */
/* 用于描述 R、 G、 B 三种颜色分量分别用多少位来表示以及它们各自的偏移量 */
struct fb_bitfield red; /* Red 颜色分量色域偏移 */
struct fb_bitfield green; /* Green 颜色分量色域偏移 */
struct fb_bitfield blue; /* Blue 颜色分量色域偏移 */
struct fb_bitfield transp; /* 透明度分量色域偏移 */
__u32 nonstd; /* nonstd 等于 0,表示标准像素格式;不等于 0 则表示非标准像素格式 */
__u32 activate;
__u32 height; /* 用来描述 LCD 屏显示图像的高度(以毫米为单位) */
__u32 width; /* 用来描述 LCD 屏显示图像的宽度(以毫米为单位) */
__u32 accel_flags;
/* 以下这些变量表示时序参数 */
__u32 pixclock; /* pixel clock in ps (pico seconds) */
__u32 left_margin; /* time from sync to picture */
__u32 right_margin; /* time from picture to sync */
__u32 upper_margin; /* time from sync to picture */
__u32 lower_margin;
__u32 hsync_len; /* length of horizontal sync */
__u32 vsync_len; /* length of vertical sync */
__u32 sync; /* see FB_SYNC_* */
__u32 vmode; /* see FB_VMODE_* */
__u32 rotate; /* angle we rotate counter clockwise */
__u32 colorspace; /* colorspace for FOURCC-based modes */
__u32 reserved[4]; /* Reserved for future compatibility */
};

        通过 xres、 yres 获取到屏幕的水平分辨率和垂直分辨率bits_per_pixel 表示像素深度 bpp即每一个像素点使用多少个 bit 位来描述它的颜色,通过 xres * yres * bits_per_pixel / 8 计算可得到整个显示缓存区的大小。

        red、 green、 blue 描述了 RGB 颜色值中 R、 G、 B 三种颜色通道分别使用多少 bit 来表示以及它们各自的偏移量,通过 red、 green、 blue 变量可知道 LCD 的 RGB 像素格式,譬如是 RGB888 还是 RGB565,亦或者是 BGR888、 BGR565 等。 struct fb_bitfield 结构体如下所示:

struct fb_bitfield {
__u32 offset; /* 偏移量 */
__u32 length; /* 长度 */
__u32 msb_right; /* != 0 : Most significant bit is right */
};

struct fb_fix_screeninfo 结构体:

struct fb_fix_screeninfo {
char id[16]; /* 字符串形式的标识符 */
unsigned long smem_start; /* 显存的起始地址(物理地址) */
__u32 smem_len; /* 显存的长度 */
__u32 type;
__u32 type_aux;
__u32 visual;
__u16 xpanstep;
__u16 ypanstep;
__u16 ywrapstep;
__u32 line_length; /* 一行的字节数 */
unsigned long mmio_start; /* Start of Memory Mapped I/O(physical address) */
__u32 mmio_len; /* Length of Memory Mapped I/O */
__u32 accel; /* Indicate to driver which specific chip/card we have */
__u16 capabilities;
__u16 reserved[2];
};

        smem_start 表示显存的起始地址,这是一个物理地址,当然在应用层无法直接使用; smem_len 表示显存的长度,这个长度并一定等于 LCD 实际的显存大小。 line_length 表示屏幕的一行像素点有多少个字节,通常可以使用 line_length * yres 来得到屏幕显示缓冲区的大小

程序测试1:

实现:获取 LCD 屏幕的参数信息::

加注释源码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/fb.h>

int main(int argc, char *argv[])
{
    struct fb_fix_screeninfo fb_fix;  // 存放固定参数的结构体
    struct fb_var_screeninfo fb_var;  // 存放可变参数的结构体
    int fd;  // 文件描述符

    /* 打开framebuffer设备 */
    if (0 > (fd = open("/dev/fb0", O_WRONLY))) {  // 打开framebuffer设备
        perror("open error");  // 输出错误信息
        exit(-1);  // 退出程序
    }

    /* 获取参数信息 */
    ioctl(fd, FBIOGET_VSCREENINFO, &fb_var);  // 获取可变参数信息
    ioctl(fd, FBIOGET_FSCREENINFO, &fb_fix);  // 获取固定参数信息
    printf("分辨率: %d*%d\n"  // 打印分辨率
           "像素深度bpp: %d\n"  // 打印像素深度
           "一行的字节数: %d\n"  // 打印一行的字节数
           "像素格式: R<%d %d> G<%d %d> B<%d %d>\n",  // 打印像素格式
           fb_var.xres, fb_var.yres, fb_var.bits_per_pixel,
           fb_fix.line_length,
           fb_var.red.offset, fb_var.red.length,
           fb_var.green.offset, fb_var.green.length,
           fb_var.blue.offset, fb_var.blue.length);

    /* 关闭设备文件退出程序 */
    close(fd);  // 关闭设备文件
    exit(0);  // 退出程序
}

        ioctl系统调用,通过文件描述符fd获取framebuffer设备的可变屏幕参数信息,并将这些信息存储在结构体fb_var和fb_fix中。

测试:

编译:

${CC} -o lcd_info lcd_info.c

上传至开发板:

 scp -r book@192.168.5.12:/home/book/project/APP/app/19_lcd/lcd_info /home/root/app

scp -r 【用户名】@【ubuntu的ip地址】:【unbuntu上要传入文件的路径】  【开发板要存入的路径】

下载:

(Tips:正点原子的 RGB LCD 屏幕,包括 4.3 寸 800*480、 4.3 寸 480*272、 7 寸 800*480、 7 寸 1024*600 以及 10.1 寸 1280*800 硬件上均支持 RGB888, 但 ALPHA/Mini I.MX6U 开发板出厂系统中, LCD 驱动程序将其实现为一个 RGB565 格式的显示设备, 用户可修改设备树使其支持 RGB888,或者通过 ioctl 修改。)

程序测试2:

任务:在 LCD 上实现画点(俗称打点)、画线、画矩形等基本 LCD 操作

加注释原码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>

#define argb8888_to_rgb565(color)   ({ \
            unsigned int temp = (color); \
            ((temp & 0xF80000UL) >> 8) | \
            ((temp & 0xFC00UL) >> 5) | \
            ((temp & 0xF8UL) >> 3); \
            })

static int width;                   // LCD X分辨率
static int height;                  // LCD Y分辨率
static unsigned short *screen_base = NULL;   // 映射后的显存基地址

/********************************************************************
 * 函数名称: lcd_draw_point
 * 功能描述: 打点
 * 输入参数: x, y, color
 * 返 回 值: 无
 ********************************************************************/
static void lcd_draw_point(unsigned int x, unsigned int y, unsigned int color)
{
    unsigned short rgb565_color = argb8888_to_rgb565(color); // 得到RGB565颜色值

    /* 对传入参数的校验 */
    if (x >= width)
        x = width - 1;
    if (y >= height)
        y = height - 1;

    /* 填充颜色 */
    screen_base[y * width + x] = rgb565_color;
}

/********************************************************************
 * 函数名称: lcd_draw_line
 * 功能描述: 画线(水平或垂直线)
 * 输入参数: x, y, dir, length, color
 * 返 回 值: 无
 ********************************************************************/
static void lcd_draw_line(unsigned int x, unsigned int y, int dir,
            unsigned int length, unsigned int color)
{
    unsigned short rgb565_color = argb8888_to_rgb565(color); // 得到RGB565颜色值
    unsigned int end;
    unsigned long temp;

    /* 对传入参数的校验 */
    if (x >= width)
        x = width - 1;
    if (y >= height)
        y = height - 1;

    /* 填充颜色 */
    temp = y * width + x; // 定位到起点
    if (dir) {  // 水平线
        end = x + length - 1;
        if (end >= width)
            end = width - 1;

        for (; x <= end; x++, temp++)
            screen_base[temp] = rgb565_color;
    } else {  // 垂直线
        end = y + length - 1;
        if (end >= height)
            end = height - 1;

        for (; y <= end; y++, temp += width)
            screen_base[temp] = rgb565_color;
    }
}

/********************************************************************
 * 函数名称: lcd_draw_rectangle
 * 功能描述: 画矩形
 * 输入参数: start_x, end_x, start_y, end_y, color
 * 返 回 值: 无
 ********************************************************************/
static void lcd_draw_rectangle(unsigned int start_x, unsigned int end_x,
            unsigned int start_y, unsigned int end_y,
            unsigned int color)
{
    int x_len = end_x - start_x + 1;
    int y_len = end_y - start_y - 1;

    lcd_draw_line(start_x, start_y, 1, x_len, color); // 上边
    lcd_draw_line(start_x, end_y, 1, x_len, color);   // 下边
    lcd_draw_line(start_x, start_y + 1, 0, y_len, color); // 左边
    lcd_draw_line(end_x, start_y + 1, 0, y_len, color);   // 右边
}

/********************************************************************
 * 函数名称: lcd_fill
 * 功能描述: 将一个矩形区域填充为参数color所指定的颜色
 * 输入参数: start_x, end_x, start_y, end_y, color
 * 返 回 值: 无
 ********************************************************************/
static void lcd_fill(unsigned int start_x, unsigned int end_x,
            unsigned int start_y, unsigned int end_y,
            unsigned int color)
{
    unsigned short rgb565_color = argb8888_to_rgb565(color); // 得到RGB565颜色值
    unsigned long temp;
    unsigned int x;

    /* 对传入参数的校验 */
    if (end_x >= width)
        end_x = width - 1;
    if (end_y >= height)
        end_y = height - 1;

    /* 填充颜色 */
    temp = start_y * width; // 定位到起点行首
    for (; start_y <= end_y; start_y++, temp += width) {
        for (x = start_x; x <= end_x; x++)
            screen_base[temp + x] = rgb565_color;
    }
}

int main(int argc, char *argv[])
{
    struct fb_fix_screeninfo fb_fix;
    struct fb_var_screeninfo fb_var;
    unsigned int screen_size;
    int fd;

    /* 打开framebuffer设备 */
    if (0 > (fd = open("/dev/fb0", O_RDWR))) {
        perror("open error");
        exit(EXIT_FAILURE);
    }

    /* 获取参数信息 */
    ioctl(fd, FBIOGET_VSCREENINFO, &fb_var);
    ioctl(fd, FBIOGET_FSCREENINFO, &fb_fix);

    screen_size = fb_fix.line_length * fb_var.yres;
    width = fb_var.xres;
    height = fb_var.yres;

    /* 将显示缓冲区映射到进程地址空间 */
    screen_base = mmap(NULL, screen_size, PROT_WRITE, MAP_SHARED, fd, 0);
    if (MAP_FAILED == (void *)screen_base) {
        perror("mmap error");
        close(fd);
        exit(EXIT_FAILURE);
    }

    /* 画正方形方块 */
    int w = height * 0.25; // 方块的宽度为1/4屏幕高度
    lcd_fill(0, width - 1, 0, height - 1, 0x0); // 清屏(屏幕显示黑色)
    lcd_fill(0, w, 0, w, 0xFF0000);     // 红色方块
    lcd_fill(width - w, width - 1, 0, w, 0xFF00); // 绿色方块
    lcd_fill(0, w, height - w, height - 1, 0xFF);   // 蓝色方块
    lcd_fill(width - w, width - 1, height - w, height - 1, 0xFFFF00); // 黄色方块

    /* 画线: 十字交叉线 */
    lcd_draw_line(0, height * 0.5, 1, width, 0xFFFFFF);   // 白色线
    lcd_draw_line(width * 0.5, 0, 0, height, 0xFFFFFF);   // 白色线

    /* 画矩形 */
    unsigned int s_x, s_y, e_x, e_y;
    s_x = 0.25 * width;
    s_y = w;
    e_x = width - s_x;
    e_y = height - s_y;

    for (; (s_x <= e_x) && (s_y <= e_y); s_x += 5, s_y += 5, e_x -= 5, e_y -= 5)
        lcd_draw_rectangle(s_x, e_x, s_y, e_y, 0xFFFFFF);

    /* 退出 */
    munmap(screen_base, screen_size);   // 取消映射
    close(fd);   // 关闭文件
    exit(EXIT_SUCCESS);   // 退出进程
}

部分代码解释:

1.

 /* 将显示缓冲区映射到进程地址空间 */
    screen_base = mmap(NULL, screen_size, PROT_WRITE, MAP_SHARED, fd, 0);
    if (MAP_FAILED == (void *)screen_base) {
        perror("mmap error");
        close(fd);
        exit(EXIT_FAILURE);
    }

mmap函数

  • mmap(memory map)函数用于将文件或设备的一部分内容映射到进程的地址空间中,从而可以直接通过内存访问的方式来操作这些内容。
  • 在这里,mmap的作用是将framebuffer设备的显示缓冲区映射到进程的地址空间中,使得程序可以直接通过操作screen_base指针来修改显示内容,而无需通过读写文件的方式。

参数解释

  • NULL:表示让系统自动选择映射的起始地址。
  • screen_size:映射区域的大小,即显示缓冲区的大小,由fb_fix.line_length * fb_var.yres计算得出。
  • PROT_WRITE:映射区域可写,表示允许通过screen_base指针写入数据到映射的内存区域。
  • MAP_SHARED:映射区域被多个进程共享,对映射区域的写入会直接反映到文件或设备中。
  • fd:已经打开的framebuffer设备文件描述符。
  • 0:表示映射的起始偏移量,通常为0,即从文件或设备的开头开始映射。

2.

munmap(screen_base, screen_size);   // 取消映射

munmap 函数

  • munmap 函数用于取消之前通过 mmap 建立的内存映射。
  • 它接受两个参数:screen_base 是之前通过 mmap 返回的映射到进程地址空间的指针,screen_size 是映射区域的大小。
  • 调用 munmap 函数后,操作系统会解除该内存区域与进程地址空间的映射关系,并释放相关资源。

3.程序中自定义了 4 个函数:

        lcd_draw_point: 用于实现画点、打点操作,参数 x 和 y 指定像素点的位置,参数 color 表示颜色。

        lcd_draw_line: 用于实现画线操作,参数 x 和 y 指定线的起始位置;参数 dir 表示方向,水平方向(dir!=0)还是垂直方向(dir=0),不支持斜线画法,画斜线需要一些算法去操作,这不是本章内容需要去关注的知识点;参数 length 表示线的长度,以像素为单位;参数 color 表示线条的颜色。

        lcd_draw_rectangle: 用于实现画矩形操作,参数 start_x 和 start_y 指定矩形左上角的位置;参数 end_x和 end_y 指定矩形右下角的位置;参数 color 指定矩形 4 个边的线条颜色。

        lcd_fill: 将一个指定的矩形区域填充为参数 color 指定的颜色,参数 start_x 和 start_y 指定矩形左上角的位置;参数 end_x 和 end_y 指定矩形右下角的位置;参数 color 指定矩形区域填充的颜色。

4.程序流程:

(1) 首先调用 open()打开 LCD 设备文件得到文件描述符 fd;

(2)接着使用 ioctl 函数获取 LCD 的可变参数信息和固定参数信息,通过得到的信息计算 LCD 显存大小、得到 LCD 屏幕的分辨率,从图 19.3.1 可知, ALPHA/Mini I.MX6U 开发板出厂系统将 LCD 实现为一个 RGB565 显示设备,所以程序中自定义的 4 个函数在操作 LCD 像素点时、都是以 RGB565的格式写入颜色值。

(3)接着使用 mmap 建立映射;映射成功之后就可以在应用层直接操作 LCD 显存了,调用自定义的函数在 LCD 上画线、画矩形、画方块;

 (4)操作完成之后,调用 munmap 取消映射,调用 close 关闭 LCD 设备文件,退出程序。

5.颜色判断:

在RGB颜色模型中,颜色是由红色(R)、绿色(G)、蓝色(B)三个通道的组合来表示的。每个通道的取值范围是0到255(或者用十六进制表示就是00到FF)。

  1. 0xFF 和 0xFF00 的颜色表示

    • 0xFF 表示的是一个八位整数的高位,即 11111111,对应于RGB颜色模型中的一个完全饱和的颜色通道。
    • 0xFF 的十六进制表示为 0x0000FF,这表示的是纯蓝色。
    • 0xFF00 表示的是一个十六位整数的高位,即 1111111100000000
    • 0xFF00 的十六进制表示为 0x00FF00,这表示的是纯绿色。
  2. 十六进制颜色表示

    • 在RGB颜色模型中,颜色值的表示方式是由红色、绿色和蓝色通道的强度组合而成。例如:
      • 0x0000FF 表示纯蓝色(B通道最大,R和G通道为0)。
      • 0x00FF00 表示纯绿色(G通道最大,R和B通道为0)。
      • 0xFF0000 表示纯红色(R通道最大,G和B通道为0)。

测试:

开发板先退出桌面程序:设置->退出桌面应用

编译:

传至开发板,

 scp -r book@192.168.5.12:/home/book/project/APP/app/19_lcd/lcd_test /home/root/app

测试成功:

LCD 应用编程练习之显示 BMP 图片:

        最常用图片类型的有三种: JPEG(或 JPG)、 PNG、 BMP 和 GIF。其中 JPEG (或 JPG)、 PNG 以及 BMP 都是静态图片,而 GIF 则可以实现动态图片。

        BMP(全称 Bitmap)是 Window 操作系统中的标准图像文件格式,文件后缀名为“.bmp”,使用非常广。它采用位映射存储格式,除了图像深度可选以外,图像数据没有进行任何压缩,因此, BMP 图像文件所占用的空间很大,但是没有失真、 并且解析 BMP 图像简单。
        BMP 文件的图像深度可选 lbit、 4bit、 8bit、 16bit、 24bit 以及 32bit, 典型的 BMP 图像文件由四部分组成:

①、 BMP 文件头(BMP file header),它包含 BMP 文件的格式、大小、 位图数据的偏移量等信息;

②、位图信息头(bitmap information) ,它包含位图信息头大小、 图像的尺寸、 图像大小、 位平面数、压缩方式以及颜色索引等信息;

③、调色板(color palette),这部分是可选的,如果使用索引来表示图像, 调色板就是索引与其对应颜色的映射表;

④、位图数据(bitmap data),也就是图像数据。

        一般常见的图像都是以 16 位(R、 G、 B 三种颜色分别使用 5bit、 6bit、 5bit 来表示)、 24 位(R、 G、B 三种颜色都使用 8bit 来表示) 色图像为主
        以一张 BMP 图像为例 ,如下图所示

对图片右键属性,可知其为16位的bmp图片:

        可以看到该图片的分辨率为 800*480,位深度为 16bit,每个像素点使用 16 位表示,也就是 RGB565。为了向大家介绍 BMP 文件结构,接下来使用十六进制查看工具将 image.bmp 文件打开,文件头部分的内容如下所示:

1.bmp 文件头

Windows 下为 bmp 文件头定义了如下结构体:
 

typedef struct tagBITMAPFILEHEADER
{
UINT16 bfType;
DWORD bfSize;
UINT16 bfReserved1;
UINT16 bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;

结构体中每一个成员说明如下;

二、 位图信息头
三、 调色板
四、 位图数据

略,感兴趣的同学可以自己去了解了解这些,网上很多。

程序测试3:

实现:在 LCD 上显示一张指定的 BMP 图像

带注释源码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
#include <linux/fb.h>
#include <sys/mman.h>

/**** BMP文件头数据结构 ****/
typedef struct {
    unsigned char type[2];      // 文件类型
    unsigned int size;          // 文件大小
    unsigned short reserved1;   // 保留字段1
    unsigned short reserved2;   // 保留字段2
    unsigned int offset;        // 到位图数据的偏移量
} __attribute__ ((packed)) bmp_file_header;

/**** 位图信息头数据结构 ****/
typedef struct {
    unsigned int size;          // 位图信息头大小
    int width;                  // 图像宽度
    int height;                 // 图像高度
    unsigned short planes;      // 位面数
    unsigned short bpp;         // 像素深度 
    unsigned int compression;   // 压缩方式
    unsigned int image_size;    // 图像大小
    int x_pels_per_meter;       // 像素/米
    int y_pels_per_meter;       // 像素/米 
    unsigned int clr_used;
    unsigned int clr_important;
} __attribute__ ((packed)) bmp_info_header;

/**** 静态全局变量 ****/
static int width;                       // LCD X分辨率
static int height;                      // LCD Y分辨率
static unsigned short *screen_base = NULL;  // 映射后的显存基地址
static unsigned long line_length;       // LCD一行的长度(字节为单位)

/********************************************************************
 * 函数名称: show_bmp_image
 * 功能描述: 在LCD上显示指定的BMP图片
 * 输入参数: 文件路径
 * 返回值: 成功返回0, 失败返回-1
 ********************************************************************/
static int show_bmp_image(const char *path)
{
    bmp_file_header file_h;
    bmp_info_header info_h;
    unsigned short *line_buf = NULL;    // 行缓冲区
    unsigned long line_bytes;           // BMP图像一行的字节的大小
    unsigned int min_h, min_bytes;
    int fd = -1;
    int j;

    /* 打开文件 */
    if (0 > (fd = open(path, O_RDONLY))) {
        perror("open error");
        return -1;
    }

    /* 读取BMP文件头 */
    if (sizeof(bmp_file_header) != read(fd, &file_h, sizeof(bmp_file_header))) {
        perror("read error");
        close(fd);
        return -1;
    }

    if (0 != memcmp(file_h.type, "BM", 2)) {
        fprintf(stderr, "it's not a BMP file\n");
        close(fd);
        return -1;
    }

    /* 读取位图信息头 */
    if (sizeof(bmp_info_header) != read(fd, &info_h, sizeof(bmp_info_header))) {
        perror("read error");
        close(fd);
        return -1;
    }

    /* 打印信息 */
    printf("文件大小: %d\n"
           "位图数据的偏移量: %d\n"
           "位图信息头大小: %d\n"
           "图像分辨率: %d*%d\n"
           "像素深度: %d\n", file_h.size, file_h.offset,
           info_h.size, info_h.width, info_h.height,
           info_h.bpp);

    /* 将文件读写位置移动到图像数据开始处 */
    if (-1 == lseek(fd, file_h.offset, SEEK_SET)) {
        perror("lseek error");
        close(fd);
        return -1;
    }

    /* 申请一个buf、暂存bmp图像的一行数据 */
    line_bytes = info_h.width * info_h.bpp / 8;
    line_buf = malloc(line_bytes);
    if (NULL == line_buf) {
        fprintf(stderr, "malloc error\n");
        close(fd);
        return -1;
    }

    if (line_length > line_bytes)
        min_bytes = line_bytes;
    else
        min_bytes = line_length;

    /**** 读取图像数据显示到LCD ****/
    if (0 < info_h.height) {  // 倒向位图
        if (info_h.height > height) {
            min_h = height;
            lseek(fd, (info_h.height - height) * line_bytes, SEEK_CUR);
            screen_base += width * (height - 1);  // 定位到屏幕左下角位置
        } else {
            min_h = info_h.height;
            screen_base += width * (info_h.height - 1);  // 定位到屏幕左下角位置
        }

        for (j = min_h; j > 0; screen_base -= width, j--) {
            read(fd, line_buf, line_bytes);  // 读取出图像数据
            memcpy(screen_base, line_buf, min_bytes);  // 刷入LCD显存
        }
    } else {  // 正向位图
        int temp = 0 - info_h.height;  // 负数转成正数
        if (temp > height)
            min_h = height;
        else
            min_h = temp;

        for (j = 0; j < min_h; j++, screen_base += width) {
            read(fd, line_buf, line_bytes);
            memcpy(screen_base, line_buf, min_bytes);
        }
    }

    /* 关闭文件、释放内存、函数返回 */
    close(fd);
    free(line_buf);
    return 0;
}

int main(int argc, char *argv[])
{
    struct fb_fix_screeninfo fb_fix;
    struct fb_var_screeninfo fb_var;
    unsigned int screen_size;
    int fd;

    /* 参数校验 */
    if (2 != argc) {
        fprintf(stderr, "usage: %s <bmp_file>\n", argv[0]);
        exit(-1);
    }

    /* 打开framebuffer设备 */
    if (0 > (fd = open("/dev/fb0", O_RDWR))) {
        perror("open error");
        exit(EXIT_FAILURE);
    }

    /* 获取参数信息 */
    ioctl(fd, FBIOGET_VSCREENINFO, &fb_var);
    ioctl(fd, FBIOGET_FSCREENINFO, &fb_fix);

    screen_size = fb_fix.line_length * fb_var.yres;
    line_length = fb_fix.line_length;
    width = fb_var.xres;
    height = fb_var.yres;

    /* 将显示缓冲区映射到进程地址空间 */
    screen_base = mmap(NULL, screen_size, PROT_WRITE, MAP_SHARED, fd, 0);
    if (MAP_FAILED == (void *)screen_base) {
        perror("mmap error");
        close(fd);
        exit(EXIT_FAILURE);
    }

    /* 在LCD上显示BMP图片 */
    memset(screen_base, 0xFF, screen_size);  // 清空屏幕为白色
    show_bmp_image(argv[1]);  // 显示BMP图片

    /* 退出 */
    munmap(screen_base, screen_size);  // 取消映射
    close(fd);  // 关闭文件
    exit(EXIT_SUCCESS);  // 退出进程
}

        代码中有两个自定义结构体 bmp_file_header 和 bmp_info_header,描述 bmp 文件头的数据结构bmp_file_header、以及描述位图信息头的数据结构 bmp_info_header。

        当执行程序时候,需要传入参数,指定一个 bmp 文件。 main()函数中会调用 show_bmp_image()函数在LCD 上显示 bmp 图像, show_bmp_image()函数的参数为 bmp 文件路径,在 show_bmp_image()函数中首先会打开指定路径的 bmp 文件,得到对应的文件描述符 fd,接着调用 read()函数读取 bmp 文件头和位图信息头。

        获取到信息之后使用 printf 将其打印出来,接着使用 lseek()函数将文件的读写位置移动到图像数据起始位置处,也就是 bmp_file_header 结构体中的 offset 变量指定的地址偏移量。

程序流程:

  • 包含必要的头文件:包括标准库、系统调用、Linux framebuffer相关的头文件。

  • 定义BMP文件头和位图信息头结构体:分别表示BMP文件的文件头和图像信息头,使用__attribute__ ((packed))确保结构体按照实际字节对齐。

  • 声明静态全局变量:包括LCD的分辨率、映射后的显存基地址以及一行的长度。

  • show_bmp_image函数

    • 打开并读取BMP文件:打开指定路径的BMP文件,读取文件头和信息头,验证文件类型和BMP格式。
    • 获取和打印图像信息:打印出文件大小、图像数据偏移量、图像分辨率、像素深度等信息。
    • 设置读取位置:将文件指针移动到图像数据的起始位置。
    • 准备显存和行缓冲区:根据图像宽度和像素深度计算每行数据的大小,申请行缓冲区的内存。
    • 读取并显示图像数据到LCD:根据位图是否为倒向或正向,逐行读取图像数据并拷贝到LCD显存中,完成图像显示。
    • 关闭文件和释放内存:关闭BMP文件,释放申请的内存空间,函数返回。
  • main函数

    • 参数验证:检查参数个数,确保传入了BMP文件路径。
    • 打开并配置framebuffer设备:打开/dev/fb0设备文件,获取并存储屏幕信息,包括像素分辨率和一行的字节数。
    • 映射framebuffer到内存空间:使用mmap将framebuffer映射到进程的地址空间,以便直接操作显存。
    • 清空屏幕并显示BMP图像:将屏幕清空为白色,调用show_bmp_image函数显示指定的BMP图像。
    • 释放映射和关闭文件:取消映射framebuffer,关闭设备文件,正常退出程序。

测试:

scp传至开发板,将程序文件和bmp图片均传过来

成功!