zoukankan      html  css  js  c++  java
  • [代码片段]读取BMP文件

    文件名mybmp.c

    //实现了读取24位BMP文件,还有一些测试信息

      1 //定义mybmp.c里面用到的函数和相关头文件、常量
      2 //参考了网友sailinghz在CSDN论坛的帖子:http://bbs.csdn.net/topics/220060950
      3 
      4 
      5 
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 
      9 
     10 /*定义U16为两个字节的类型*/
     11 typedef unsigned short U16;
     12 /*定义U32为四个字节的类型*/
     13 typedef unsigned long U32;
     14 
     15 /*位图文件头*/
     16 typedef struct BMP_FILE_HEADER
     17 {
     18     unsigned bType:16;             /*  文件标识符          */
     19     unsigned bSize:32;           /*  文件的大小          */
     20     unsigned bReserved1:16;        /*  保留值,必须设置为0  */       
     21     unsigned bReserved2:16;        /*  保留值,必须设置为0  */
     22     unsigned bOffset:32;          /*  文件头的最后到图像数据位开始的偏移量    */
     23 } BMPFILEHEADER;
     24 
     25 /*位图信息头*/
     26 typedef struct BMP_INFO
     27 {
     28     U32 bInfoSize;       /*  信息头的大小             */
     29     U32 bWidth;          /*  图像的宽度               */
     30     U32 bHeight;         /*  图像的高度               */
     31     U16 bPlanes;          /*  图像的位面数             */
     32     U16 bBitCount;        /*  每个像素的位数           */
     33     U32 bCompression;    /*  压缩类型                 */
     34     U32 bmpImageSize;    /*  图像的大小,以字节为单位  */
     35     U32 bXPelsPerMeter;  /*  水平分辨率               */
     36     U32 bYPelsPerMeter;  /*  垂直分辨率               */
     37     U32 bClrUsed;        /*  使用的色彩数             */
     38     U32 bClrImportant;   /*  重要的颜色数             */
     39 } BMPINF;
     40 
     41 /*彩色表*/
     42 typedef struct RGB_QUAD 
     43 {
     44     U16 rgbBlue;         /*  蓝色强度  */
     45     U16 rgbGreen;        /*  绿色强度  */
     46     U16 rgbRed;          /*  红色强度  */
     47     U16 rgbReversed;     /*  保留值    */
     48 } RGBQUAD;
     49 
     50 int myLoadImage(unsigned char *image, char *filename);
     51 
     52 
     53 //只能读取24位的位图
     54 int myLoadImage(unsigned char *image, char *filename)
     55 {
     56     FILE *fp;    //文件指针
     57     BMPFILEHEADER fileHeader;        //位图文件头
     58     BMPINF infoHeader;        //位图信息头
     59     long offset, bmpImageSize, width, height, bytesPerPixel, size, bitCount;
     60     int i = 0, j, count = 0;
     61     int lcount = 0;
     62     int cl[3] = {0};
     63     //    unsigned char **p;
     64     U16 c;
     65 
     66     char fname_bmp[128];
     67     sprintf(fname_bmp, "%s", filename);
     68     printf("FileName:%s
    ",fname_bmp);
     69 
     70     if((fp = fopen(fname_bmp, "rb")) == NULL)
     71     {
     72         printf("Cann't open the file!
    ");
     73         return -1;        //读取失败,直接返回
     74     }
     75 
     76     fseek(fp, 54L, 0);    //指定偏移量
     77     fread(&fileHeader, sizeof(fileHeader), 1, fp);        //读取文件头
     78     fread(&infoHeader, sizeof(infoHeader), 1, fp);        //读取位图信息头
     79     
     80     //system("pause");        //暂停
     81 
     82     /*
     83     printf("typedef struct BMP_FILE_HEADER
    ");
     84     printf("{
    ");
     85     printf("    U16 bType;     #%04x        *  文件标识符          *
    ",fileHeader.bType);
     86     printf("    U32 bSize;     #%08x        *  文件的大小          *
    ",fileHeader.bSize);
     87     printf("    U16 bReserved1;#%04x        *  保留值,必须设置为0  *
    ",fileHeader.bReserved1);
     88     printf("    U16 bReserved2;#%04x        *  保留值,必须设置为0  *
    ",fileHeader.bReserved2);
     89     printf("    U32 bOffset;   #%08x        *  文件头的最后到图像数据位开始的偏移量*
    ",fileHeader.bOffset);
     90     printf("} BMPFILEHEADER;%d个字节
    ",sizeof(fileHeader));
     91     */
     92 
     93     /*
     94     printf("typedef struct BMP_INFO
    ");
     95     printf("{
    ");
     96     printf("    U32 bInfoSize;      #%08x       /*  信息头的大小             *
    ",infoHeader.bInfoSize);
     97     printf("    U32 bWidth;         #%08x          /*  图像的宽度               *
    ",infoHeader.bWidth);
     98     printf("    U32 bHeight;        #%08x         /*  图像的高度               *
    ",infoHeader.bHeight);
     99     printf("    U16 bPlanes;        #%04x          /*  图像的位面数             *
    ",infoHeader.bPlanes);
    100     printf("    U16 bBitCount;      #%04x        /*  每个像素的位数           *
    ",infoHeader.bBitCount);
    101     printf("    U32 bCompression;   #%08x    /*  压缩类型                 *
    ",infoHeader.bClrImportant);
    102     printf("    U32 bmpImageSize;   #%08x    /*  图像的大小,以字节为单位  *
    ",infoHeader.bmpImageSize);
    103     printf("    U32 bXPelsPerMeter; #%08x  /*  水平分辨率               *
    ",infoHeader.bXPelsPerMeter);
    104     printf("    U32 bYPelsPerMeter; #%08x  /*  垂直分辨率               *
    ",infoHeader.bYPelsPerMeter);
    105     printf("    U32 bClrUsed;       #%08x        /*  使用的色彩数             *
    ",infoHeader.bClrUsed);
    106     printf("    U32 bClrImportant;  #%08x   /*  重要的颜色数             *
    ",infoHeader.bClrImportant);
    107     printf("} BMPINF;%d个字节
    
    ",sizeof(infoHeader));
    108     */
    109 
    110     //printf("unsigned short:%d
    ",8*sizeof(U16));
    111     //printf("unsigned long:%d
    ",8*sizeof(U32));
    112 
    113     printf("Size:%d个字节
    ",sizeof(infoHeader) + sizeof(fileHeader));
    114 
    115     //计算并输出位图数据的偏移量,图像的大小,宽度和高度,每个像素点所占的字节
    116     /*size = fileHeader.bSize;
    117     offset = fileHeader.bOffset;
    118     bmpImageSize = infoHeader.bmpImageSize;
    119     width = infoHeader.bWidth;
    120     height = infoHeader.bHeight;
    121     bitCount = infoHeader.bBitCount;
    122     bytesPerPixel = infoHeader.bBitCount / 8;*/
    123 
    124     //输出基本信息,文件信息 & 位图信息
    125     //printf("bSize:%#x
    bOffset:%#x
    mpImageSize:%#x
    bWidth:%#x
    bHeight:%#x
    bBitCount:%#x
    bytesPerPixel:%#x
    ", 
    126             size, offset, bmpImageSize, width, height, bitCount, bytesPerPixel);
    127 
    128     //system("pause");        //暂停
    129 
    130     
    131     //fread(&image, sizeof(unsigned char), (size_t)(long)width*height*3, fp);    //直接读取像素信息给image数组
    132 
    133 
    134     
    135     //输出每个像素点所占字节中的内容
    136     c = fgetc(fp);
    137 
    138     
    139     //直接输出
    140     while (!feof(fp))
    141     {
    142         cl[count] = fgetc(fp);
    143         count++;
    144         if (count == 3)        //读一个像素
    145         {
    146             lcount++;
    147             c = (unsigned char)((cl[0]*11 + cl[1]*59 +cl[2]*30+ 50)/100);        //BGR 2 GRAY
    148             printf("%d",c>0?1:0);
    149             image[i] = (c>0?1:0) + 48;
    150             i++;
    151             count = 0;
    152             if (lcount == 64)
    153             {
    154                 printf("
    ");
    155                 lcount = 0;
    156             }
    157         }
    158         
    159         
    160     }
    161 
    162     printf("
    count = %d
    ",count);
    163     
    164 
    165     //格式输出
    166     /*for (i = 0; i<64; i++)
    167     {
    168         for (j = 0; j<64; j++)
    169         {
    170             cl[0] = fgetc(fp);
    171             cl[1] = fgetc(fp);
    172             cl[2] = fgetc(fp);
    173             c = (unsigned char)((cl[0]*11 + cl[1]*59 +cl[2]*30+ 50)/100);
    174             printf("%d", c>0?1:0);
    175             //image[i][j] = c;        //直接给image
    176         }
    177         printf("
    ");
    178     }*/
    179 
    180     printf("
    ");
    181 
    182     //system("pause");        //暂停
    183 
    184     fclose(fp);
    185 
    186     printf("Call myLoadImage() Ok!
    ");
    187 
    188     return 0;
    189 }

    代码片段:2014年2月7日

  • 相关阅读:
    关于java集合框架(二):List
    仪式感
    java的foreach(增强for循环)
    关于Java集合框架(一):概述与Set
    重新开始
    简单fork循环分析
    fork,写时复制(copy-on-write),vfork
    树莓派换源
    Windows下TexLive2018环境配置及检测
    Linux下高精度时间
  • 原文地址:https://www.cnblogs.com/rongfangliu/p/Code_ReadBMP.html
Copyright © 2011-2022 走看看