查看: 2341|回复: 0

[原创] 一个程序告诉你bmp图像是什么样子

[复制链接]
  • TA的每日心情
    开心
    2024-1-16 17:48
  • 签到天数: 592 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2018-9-24 15:40:18 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 robe.zhang 于 2018-9-24 15:45 编辑

    先看这个博文,写的很详细:
    https://www.cnblogs.com/wainiwann/p/7086844.html
    这个程序是读出文件头和图像信息头:
    Screenshot from 2018-09-24 15-30-56.png
    8bit 图像用到了调色板

    Screenshot from 2018-09-24 15-30-33.png
    16bit图像没有调色板

    Screenshot from 2018-09-24 15-30-06.png
    24bit图像也没有调色板,正好比8bit图像少256个调色索引项,每一个调色索引项4byte,正好是1024字节。16/24bit数据偏移比8bit数据偏移少1024。

    这个是程序代码:

    1. #include<stdio.h>
    2. #include<string.h>
    3. #include<sys/types.h>
    4. #include <iostream>
    5. #pragma pack(2)
    6. using namespace std;

    7. #define u_int16_t unsigned short int
    8. #define u_int32_t unsigned int


    9. //下面两个结构是位图的结构
    10. typedef struct BITMAPFILEHEADER
    11. {
    12.     u_int16_t bfType;
    13.     u_int32_t bfSize;
    14.     u_int16_t bfReserved1;
    15.     u_int16_t bfReserved2;
    16.     u_int32_t bfOffBits;
    17. }BITMAPFILEHEADER;

    18. typedef struct BITMAPINFOHEADER
    19. {
    20.     u_int32_t biSize;
    21.     u_int32_t biWidth;
    22.     u_int32_t biHeight;
    23.     u_int16_t biPlanes;
    24.     u_int16_t biBitCount;
    25.     u_int32_t biCompression;
    26.     u_int32_t biSizeImage;
    27.     u_int32_t biXPelsPerMeter;
    28.     u_int32_t biYPelsPerMeter;
    29.     u_int32_t biClrUsed;
    30.     u_int32_t biClrImportant;
    31. }BITMAPINFODEADER;

    32. void showBmpHead(BITMAPFILEHEADER &pBmpHead){
    33.     cout<<"====位图文件头信息====,大小:"<< sizeof(struct BITMAPFILEHEADER) <<endl;
    34.     cout<<"类型 :"<<pBmpHead.bfType;
    35.     char a,b;
    36.     a=pBmpHead.bfType&0x00ff;
    37.     b=(pBmpHead.bfType&0xff00)>>8;
    38.     cout<<" / "<< a << b << ". ** 备注:是'BM',符合windows bitmap 图像标记" << endl;
    39.     cout<<"大小 :"<<pBmpHead.bfSize<<endl;
    40.     cout<<"保留1:"<<pBmpHead.bfReserved1<<endl;
    41.     cout<<"保留2:"<<pBmpHead.bfReserved2<<endl;
    42.     cout<<"偏移 :"<<pBmpHead.bfOffBits<<endl;
    43. }

    44. void showBmpInforHead(BITMAPINFODEADER &pBmpInforHead){
    45.     cout<<"====位图信息头信息====,大小:"<< sizeof(BITMAPINFODEADER) << endl;
    46.     cout<<"信息头长度   :"<<pBmpInforHead.biSize<<endl;
    47.     cout<<"位图宽      :"<<pBmpInforHead.biWidth<<endl;
    48.     cout<<"位图高      :"<<pBmpInforHead.biHeight<<endl;
    49.     cout<<"biPlanes平面数    :"<<pBmpInforHead.biPlanes<<endl;
    50.     cout<<"biBitCount颜色位数:"<<pBmpInforHead.biBitCount<<endl;
    51.     cout<<"压缩       :"<<pBmpInforHead.biCompression<<endl;
    52.     cout<<"biSizeImage大小:"<<pBmpInforHead.biSizeImage<<endl;
    53.     cout<<"X方向分辨率 :"<<pBmpInforHead.biXPelsPerMeter<<endl;
    54.     cout<<"Y方向分辨率 :"<<pBmpInforHead.biYPelsPerMeter<<endl;
    55.     cout<<"使用的颜色数 :"<<pBmpInforHead.biClrUsed<<endl;
    56.     cout<<"重要颜色数  :"<<pBmpInforHead.biClrImportant<<endl;
    57. }

    58. int main( int argc, char **argv )
    59. {
    60.         if(argc!=2)
    61.         {
    62.                 cout << "usage: ./main /path/to/file.bmp" << endl;
    63.                 return 0;
    64.         }
    65.     FILE *fp = fopen(argv[1], "rb");

    66.     BITMAPFILEHEADER head;
    67.     BITMAPINFODEADER info;

    68.     fread(&head, 1, sizeof(BITMAPFILEHEADER), fp);
    69.     fread(&info, 1, sizeof(BITMAPINFODEADER), fp);

    70.     showBmpHead(head);
    71.     showBmpInforHead(info);

    72.     fclose(fp);
    73.     return 0;
    74. }
    复制代码
    这是三张已经转好的bmp图像,linux gimp 处理图像功能还是很强大的,这个软件还免费
    photo.bmp.tar (320 KB, 下载次数: 0)
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-24 23:08 , Processed in 0.101459 second(s), 15 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.