头部互动开始 -->

GETbmpinfo

image

/*************************************************
/**
  * @file name:	GETbmpinfo
  * @brief  实现对BMP图片位图信息的获取
  * @author 15070884254@163.com
  * @date 2024/05/11
  * @version 1.0 :在下坂本,有何贵干 
  * @property :none
  * @note   none
  * CopyRight (c)  2023-2024  15070884254@163.com   All Right Reseverd
 *
 **************************************************/
#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
{
  unsigned short bftype;
  unsigned int bfsize;
  unsigned short bfreserved1;
  unsigned short bfreserved2;
  unsigned int bfoffsetbits;
};

typedef struct bitmap_info_header
{
  unsigned int bisize;
  int width;
  int height;
  unsigned short biplanes;
  unsigned short bidepth;
  unsigned int bicompression;
  unsigned int bisizeimage;
  int x_pels_permeter;
  int y_pels_permeter;
  unsigned int color_used;
  unsigned int color_important;
};
#pragma pack()

int main(int argc, char const *argv[])
{

  // 1.BMP图片的路径是通过命令行传递,所以需要检测参数有效性
  if (2 != argc)
  {
    printf("argument is invaild\n");
    return -1;
  }

  // 2.利用系统调用open()打开待读取数据的BMP图片
  int bmp_fd = open(argv[1], O_RDWR);
  if (-1 == bmp_fd)
  {
    printf("open %s is error\n", argv[1]);
    return -1;
  }

  struct bitmap_file_header bmpfile;
  struct bitmap_info_header bmpinfo;

  read(bmp_fd,&bmpfile,14);
  read(bmp_fd,&bmpinfo,40);

  // 4.输出BMP信息
  printf("bmp name is %s ,width = %d,height = %d,size = %d\n", argv[1], bmpinfo.width, bmpinfo.height, bmpfile.bfsize);

  return 0;
}
posted @ 2024-05-11 20:14  罗天天  阅读(8)  评论(0编辑  收藏  举报