首页 > 其他分享 >系统IO下查看bmp照片信息

系统IO下查看bmp照片信息

时间:2024-05-11 21:45:06浏览次数:17  
标签:int BMP 照片 bmp fd IO include 图片

IO编程

用系统IO实现查看bmp照片信息

设计程序,利用系统IO读取磁盘上指定BMP图片的宽和高,以及BMP图片的大小,并输出到终端,要求图片名称通过命令行传递。

/****************************************************************************
 *
 * file name: 2024-05-11_GetBmpInfo.c
 * author   : [email protected]
 * date     : 2024-05-11
 * function : 实现在系统IO中读取一bmp图片的相关信息
 *
 * note     : None
 *
 * CopyRight (c)   2024  [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>
#pragma pack(1)

// 第一个结构体位图文件头结构:BITMAPFILEHEADER 的详细结构
typedef struct tagBITMAPFILEHEADER
{
  unsigned short int bfType;      // 位图文件的类型,必须为BM
  unsigned int bfSize;            // 是整个文件的大小,以字节为单位
  unsigned short int bfReserved1; // 位图保留字,必须是0
  unsigned short int bfReserved2; //  位图保留字,必须是0
  unsigned int bfOffBits;         // 是位图数据在文件中的偏移,以字节为单位
} BITMAPFILEHEADER;

// 第二个结构体位图信息头:BITMAPINFOHEADER
typedef struct tagBITMAPINFOHEADER
{
  int biSize;              // BITMAPINFOHEADER这个结构体的大小,字节为单位 必须为40(字节)
  int biWidth;             // BMP位图的宽度,以像素为单位
  int biHeight;            // BMP位图的高度,以像素为单位
  short int biPlanes;      // 目标设备的级别。这个值必须为1
  short int biBitCount;    // 颜色深度  每个像素所需要的位数
  short int biCompression; // 位图的压缩格式
  int biSizeImage;         // 位图数据块的大小。以字节为单位。如果你的位图没有经过压缩,这个值可以是0
  int biXPelsPerMeter;     // 表示横向的每米的像素数。可以为0
  int biYPelsPerMeter;     // 表示纵向的每米的像素数。可以为0
  int biClrUsed;           // 位图实际使用过的调色板的颜色数
  int biClrImportant;      // 位图显示过程中表示重要的颜色数。如果为0,表示颜色都重要。通常它的值等于biClrUsed
} BITMAPINFOHEADER;

#pragma pack()

int main(int argc, char const *argv[])
{
  // int bmp_size = 0;   // 用于存储BMP图片的大小
  // int bmp_height = 0; // 记录BMP图片的高
  // int bmp_width = 0;  // 记录BMP图片的宽

  // 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;
  }

  // // 3.从被打开的BMP图片中读取图像信息
  // lseek(bmp_fd, 2, SEEK_SET);
  // read(bmp_fd, &bmp_size, 4); // 读取BMP图片大小

  // lseek(bmp_fd, 18, SEEK_SET);
  // read(bmp_fd, &bmp_width, 4); // 读取BMP图片的宽

  // lseek(bmp_fd, 22, SEEK_SET);
  // read(bmp_fd, &bmp_height, 4); // 读取BMP图片的高

  struct tagBITMAPFILEHEADER s1;
  struct tagBITMAPINFOHEADER s2;

  read(bmp_fd, &s1, 14);
  read(bmp_fd, &s2, 40);
  // 4.输出BMP信息
  printf("bmp name is %s ,width = %d,height = %d,size = %d\n", argv[1], s2.biWidth, s2.biHeight, s1.bfSize);

  return 0;
}

标签:int,BMP,照片,bmp,fd,IO,include,图片
From: https://www.cnblogs.com/little-mirror/p/18187182

相关文章

  • 树莓派4b openwrt 安装RPI.GPIO控制PWM风扇
    1、安装python3#opkgupdate#opkginstallpython3-base#opkginstallpython3#opkginstallpython3-pip#opkginstallpython3-dev2、安装RPI.GPIO#pipinstallrpi.gpio3、GPIO控制PWM风扇###交互模式演示代码#pythonimportRPi.GPIOasGPIO#GPIO设置GPIO.s......
  • 记一次由sequence引发的enq sv-contention等待事件
    转自:https://www.cnblogs.com/lijiaman/p/10423272.html#4237610数据库版本:11.2.0.4RAC(1)问题现象从EM里面可以看到,在23号早上8:45~8:55时,数据库等待会话暴增,大约到了80个会话。通过查看EM的SQL信息,发现等待产生于SQL语句selectTIMEKEYID.nextvalfromdual (二)问题追踪......
  • gradio 将 webui 从 127.0.0.1 映射到局域网 IP
    在局域网中,假设当前用Python调试gradiowebui的设备IP地址是192.168.1.101,若app.py内容如下:importgradioasgrwithgr.Blocks()asdemo:#TODO:demo.launch(server_name="0.0.0.0")运行即可实现将127.0.0.1:7860映射到192.168.1.101:7860其实是launch......
  • luogu P4342[IOI1998]Polygon
    阅读前需深剖析分系列是记录我个人的做题思路,实现过程的全面分析,存在内容可靠、思路健全、分析到位、试错纠错等优于一般题解的特征,其中,Quest部分表示探索问题,我会在此提出做题时的想法、问题,并在内容中得到解决,因此建议从上到下按序浏览,以防出现思路断层,内容不衔接的情况,感谢理......
  • 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......
  • bmp位图文件信息结构体
    /***************************************************filename:BmpInfoStruct.c*author:[email protected]*date:2024/05/11*brief:构造一个结构体用于存储bmp图片的文件信息*note:None**CopyRight(c)2024momolyl@126.......
  • BMP文件格式及相应结构体
    //文件信息结构体typedefstructtag_bitmap_file_header{unsignedshortfile_type;//文件标识,为字母ASCII码“BM”unsignedintfile_size;//位图文件大小,以字节为单位unsignedshortreserved1;//位图文件保留字,必须为0unsignedshortreserved......
  • 文件IO
    文件IO记录常用的一些IO接口标准IOFILE*fopen(constchar*path,constchar*mode);/***********************************************************@path 操作的文件@mode 操作的权限 ("r","r+","w","w+","a","a+")返回值 成功 文......
  • 利用系统IO读取磁盘上指定BMP图片的宽和高以及大小
    文件IO代码/***************************************************************************************filename:1.c*author: [email protected]*date:2024/05/11*function: 利用系统IO读取磁盘上指定BMP图片的宽和高以及大小*......