C对bmp图像的处理(一)

这学期选了图像处理课,由于本人不想装盗版的matlab,所以突然想干一件蛋疼的事情,那就是自己用C语言处理图像好了。于是乎就有了接下来的种种蛋疼………………

首先我选择拿bmp图开刀,原因嘛,bmp相对来说比较简单,好弄= =。

废话不多说,今天先把bmp的文件头里的信息全部提取出来了。

首先来说说bmp的文件信息有哪些。

struct _bmp_file_h
{
    unsigned short bftype;           /*文件类型,必须为"BM"(0x4D42)*/
    unsigned long bfsize;            /*文件大小(字节)*/
    unsigned short bfreserved1;      /*保留,必须为0*/
    unsigned short bfreserved2;      /*保留,必须为0*/
    unsigned long bfoffbits;         /*位图阵列相对于文件头的偏移量(字节)*/
};

 

以上是bmp的文件头的一个结构体,刚好顺序存在bmp文件的最开头,其中开头两个字节刚好为字符'B'和字符'M',以此可以判断该文件是否为bmp图,也就是说文件起始的两个字节若不是'B'和'M'该文件一定不是一个bmp文件。之后的64位用于储存文件大小。之后四个字节貌似和bmp文件的什么行补齐有关,下次再研究……接着是保存位图阵列相对于文件头的偏移量。接下来文件中是文件信息,我们把他们保存在下面的结构体中。

struct _bmp_info_h
{
     unsigned long   bisize;             /*size of BITMAPINFOHEADER   */  
     unsigned long   biwidth;            /*位图宽度(像素)*/  
     unsigned long   biheight;           /*位图高度(像素)*/  
     unsigned short  biplanes;           /*目标设备的位平面数,必须置为1 */  
     unsigned short  bibitcount;         /*每个像素的位数,1,4,8或24*/  
     unsigned long   bicompress;         /*位图阵列的压缩方法,0=不压缩*/  
     unsigned long   bisizeimage;        /*图像大小(字节)*/  
     unsigned long   bixppm;             /*目标设备水平每米像素个数*/  
     unsigned long   biyppm;             /*目标设备垂直每米像素个数*/  
     unsigned long   biclrused;          /*位图实际使用的颜色表的颜色数*/  
     unsigned long   biclrimportant;     /*重要颜色索引的个数*/    
};

= =这个结构体中保存的信息是用来干什么的几乎不太看得懂,除了几个显而易见的。

接下来就是一堆的文件操作,我也不多赘述了,把文件中的信息一次读进来就好,问题是手疼,工作量有点大。然后你可以打印出图片信息,主要可以对校一下位图宽度和位图高度是否和你读入的位图相符,如果不相符那么一定是你读的时候次序出错或者是少读了某个数据。如果你懒得写的话,也可一抄我的= =

    FILE *bmpfile = fopen(bmpfilename, "r");
    struct bmp_file bf;

    if(bmpfile != NULL)
    {
        /*read the file into bmp_file_h*/
        fread(&bf.bmp_file_h.bftype, sizeof(bf.bmp_file_h.bftype), 1, bmpfile);
        fread(&bf.bmp_file_h.bfsize, sizeof(bf.bmp_file_h.bfsize), 1, bmpfile);
        fread(&bf.bmp_file_h.bfreserved1, sizeof(bf.bmp_file_h.bfreserved1), 1, bmpfile);
        fread(&bf.bmp_file_h.bfreserved2, sizeof(bf.bmp_file_h.bfreserved2), 1, bmpfile);
        fread(&bf.bmp_file_h.bfoffbits, sizeof(bf.bmp_file_h.bfoffbits), 1, bmpfile);

        fread(&bf.bmp_info_h.bisize, sizeof(bf.bmp_info_h.bisize), 1, bmpfile);
        fread(&bf.bmp_info_h.biwidth, sizeof(bf.bmp_info_h.biwidth), 1, bmpfile);
        fread(&bf.bmp_info_h.biheight, sizeof(bf.bmp_info_h.biheight), 1, bmpfile);
        fread(&bf.bmp_info_h.biplanes, sizeof(bf.bmp_info_h.biplanes), 1, bmpfile);
        fread(&bf.bmp_info_h.bibitcount, sizeof(bf.bmp_info_h.bibitcount), 1, bmpfile);
        fread(&bf.bmp_info_h.bicompress, sizeof(bf.bmp_info_h.bicompress), 1, bmpfile);
        fread(&bf.bmp_info_h.bisizeimage, sizeof(bf.bmp_info_h.bisizeimage), 1, bmpfile);
        fread(&bf.bmp_info_h.bixppm, sizeof(bf.bmp_info_h.bixppm), 1, bmpfile);
        fread(&bf.bmp_info_h.biyppm, sizeof(bf.bmp_info_h.biyppm), 1, bmpfile);
        fread(&bf.bmp_info_h.biclrused, sizeof(bf.bmp_info_h.biclrused), 1, bmpfile);
        fread(&bf.bmp_info_h.biclrimportant, sizeof(bf.bmp_info_h.biclrimportant), 1, bmpfile);

        printf("%ld\n", bf.bmp_info_h.bisize);
        printf("%ld\n", bf.bmp_info_h.biwidth);
        printf("%ld\n", bf.bmp_info_h.biheight);
        printf("%d\n", bf.bmp_info_h.biplanes);
        printf("%d\n", bf.bmp_info_h.bibitcount);
        printf("%ld\n", bf.bmp_info_h.bicompress);
        printf("%ld\n", bf.bmp_info_h.bisizeimage);
        printf("%ld\n", bf.bmp_info_h.bixppm);
        printf("%ld\n", bf.bmp_info_h.biyppm);
        printf("%ld\n", bf.bmp_info_h.biclrused);
        printf("%ld\n", bf.bmp_info_h.biclrimportant);
        fclose(bmpfile);
    }

好了,看看你打开的bmp文件的位宽和位高是否正确吧!如果正确下次再来把像素信息读出来= =真正扯淡的才刚刚开始。

转载于:https://www.cnblogs.com/bananapeel/archive/2012/05/03/2481977.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值