在LCD屏内显示任意尺寸任意大小的bmp图像

在LCD屏内显示任意尺寸任意大小的bmp图像

定义结构体

/****************************************************************************
 *
 * function name     :ShowBmp
 * function          : 显示图片
 * argument
 *                      @name :bmp图片
 *                      @x :x轴
 *                      @y :y轴
 *
 * Return results    : none
 * note              : None
 * author            : tongyaqi1110@163.com
 * date              : 2024-05-12
 * version           : V1.0
 * revision history  : None
 *
 ****************************************************************************/
int ShowBmp(const char *name, int x, int y)
{
    // 1.打开待显示的BMP图像  fopen
    FILE *bmp_fp = fopen(name, "rb");
    if (NULL == bmp_fp)
    {
        printf("fopen bmp failed\n");
        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图片的颜色分量
    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);
    if (-1 == lcd_fd)
    {
        printf("open lcd is failed\n");
        return -1;
    }

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

    // 7.循环的把BMP图像的颜色分量依次写入到LCD的像素点中

    if (pic.biHeight > 480 || pic.biWidth > 800)
    {
        printf("your bmp'size over!");
    }

    int i = 0;
    int data = 0;

    for (int j = y + pic.biHeight - 1; j >= y; j--)
    {
        for (int k = x; k < pic.biWidth + x; ++k)
        {
            // 把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 * j + k] = data; // BGR BGR BGR ....

            i += 3;
            data = 0;
        }
    }
    // 8.关闭LCD
    close(lcd_fd);
    munmap(lcd_mp, 800 * 480 * 4);
    free(bmp_buf);

    return 1;
}


posted @ 2024-05-12 21:52  Dazz_24  阅读(72)  评论(0)    收藏  举报