首页 > 其他分享 >在LCD屏内显示任意尺寸任意大小的bmp图像

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

时间:2024-05-12 21:52:14浏览次数:10  
标签:int LCD 屏内 headerinfo unsigned bmp BMP 任意

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

定义结构体

/****************************************************************************
 *
 * file name: ShowBmp.c
 * author   : Dazz
 * date     : 2024-05-12
 * function : 在LCD上显示任意大小任意尺寸的bmp图片
 * note     : None
 * CopyRight (c)  2024-202x   [email protected]   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()

函数接口:

/****************************************************************************
 *
 * function name     :ShowBmp
 * function          : 显示图片
 * argument          
 *                      @name :bmp图片
 *                      @x :x轴
 *                      @y :y轴
 *
 * Return results    : none
 * note              : None
 * author            : [email protected]
 * 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)
  {
    return -1;
  }

  // 2.读取BMP文件的图像信息,获取BMP的宽和高
  BITMAPINFOHEADER headerinfo;
  BITMAPFILEHEADER fileinfo;
  fseek(bmp_fp, 14, SEEK_SET);
  fread(&headerinfo, 1, 40, bmp_fp); // 读取40字节
  printf("bmp width = %d,height = %d\n", headerinfo.biWidth, headerinfo.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 == bmp_fd)
  {
    printf("open %s is error\n", argv[1]);
    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 (headerinfo.biHeight > 480 || headerinfo.biWidth > 800)
  {
    printf("your bmp'size over!");
  }

  int i = 0;
  int data = 0;

  for (int j = y + headerinfo.biHeight - 1; j >= y; j--)
  {
    for (int k = x; k < headerinfo.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 0;
}

标签:int,LCD,屏内,headerinfo,unsigned,bmp,BMP,任意
From: https://www.cnblogs.com/Dazz24/p/18188256

相关文章

  • 获取BMP图片件并在LCD屏上打开
    目录目录获取BMP文件获取信息进行缩放并产生新的BMP文件并在LCD屏上进行显示头文件BMP文件结构体主函数/********************************************************************filename:ChangeBmp.c*author:[email protected]*date:2024-05-12*func......
  • bmp任意大小任意尺寸显示在lcd屏内
    IO编程在LCD屏内显示任意尺寸任意大小的图像/******************************************************************************filename:2024-05-12_ShowBmp.c*author:[email protected]*date:2024-05-12*function:在LCD上显示任意大小任意尺......
  • 在LCD屏幕上任意位置打开以缩减图片(待修改)
    #include<stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>#include<sys/mman.h>#pragmapack(1)//设置取消字节对齐//文件信息结构体typedefstructtag_bitmap_file_header{un......
  • 自定义函数在LCD上显示一张不超过LCD像素大小的色深为 24bit的bmp图片
    设计程序实现在LCD上任意位置显示一张任意大小的色深为24bit的bmp图片,要求图像不失真可以在开发板的LCD上显示。头文件包含#include<stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>#include<sys/mman.h>#include<linux/......
  • 在 LCD 上任意位置显示一张任意大小
    /***************************************************************************************filename:1.c*author: [email protected]*date:2024/05/12*function: 设计程序,实现在LCD上任意位置显示一张任意大小*note :none*......
  • 在lcd屏幕上的任意位置显示任意大小的图片
    /***************************************************filename:ShowBmp2.c*author:[email protected]*date:2024/05/12*brief:在lcd屏幕上的任意位置显示任意大小的图片*note:None**CopyRight(c)[email protected]......
  • C语言实现获取BMP文件信息
    通过命令行传递文件路径参数,输出BMP的width、height、size1、从BMP的结构信息可知,文件大小、图片宽度、图片高度均占据4个字节,所以只需要打开文件读取对应位置的信息并打印即可。2、为了提高可移植性,可以定义结构体,保持和BMP文件结构一致,这样可以一次性读取保存,方便后续调用。但......
  • 系统IO下查看bmp照片信息
    IO编程用系统IO实现查看bmp照片信息设计程序,利用系统IO读取磁盘上指定BMP图片的宽和高,以及BMP图片的大小,并输出到终端,要求图片名称通过命令行传递。/******************************************************************************filename:2024-05-11_GetBmpInfo.c......
  • GETbmpinfo
    /*************************************************/***@filename: GETbmpinfo*@brief实现对BMP图片位图信息的获取*@[email protected]*@date2024/05/11*@version1.0:在下坂本,有何贵干*@property:none*@notenone*CopyRigh......
  • BMP图片内部结构
    BMP图片内部结构​ BMP文件的数据按照从文件头开始的先后顺序分为四个部分:分别是位图文件头、位图信息头、调色板(24bit位图是没有的)、位图数据(RGB)。(1)位图文件头(Bitmap-FileHeader)包含了图像类型、图像大小、两个保留字以及位图数据存放地址。(2)位图信息头(Bitmap-InformationH......