BMP图片的数据结构

拿个小本本记一下

#include"stdio.h"
#include<windows.h>
#include<string.h>
//定义头文件 信息头 调色板的结构体
struct BITMAPFILEHEADER
{
    byte bfType;/*文件类型*/
    DWORD bfSize;/*文件大小,以字节为单位*/
    byte bfReserved1;
    byte bfReserved2;
    DWORD bfOffBits;/*从此数据结构到数据字节间的偏移量*/
};
struct BITMAPINFOHEADER
{
    DWORD biSize;/*此数据结构所需字节数*/
    LONG biWidth;/*图像宽度,以像素为单位*/
    LONG biHeight;/*图像高度,以像素为单位*/
    byte biPlanes;
    byte biBitCount;/*每像素位数,为1 4 8 24*/
    DWORD biCompression;/*压缩类型 0为不压缩*/
    DWORD biSizeImage;
    LONG biXPelsPerMeter;
    LONG biYPelsPerMeter;
    DWORD biClrUsed;
    DWORD biClrImportant;/*重要色彩数,0表示都重要*/
};
struct RGB_QUAD
{
    byte rgbBlue;/*蓝色分量*/
    byte rgbGreen;/*绿色分量*/
    byte rgbRed;/*红色分量*/
    byte rgbReversed;/*保留值*/
};
struct RGB_QUAD img[1000][1000];//假定像素为1000*1000

int main(void)
{
    BITMAPFILEHEADER fileHeader;//定义位图头文件
    BITMAPINFOHEADER infoHeader;//定义位图信息头

    printf("Please indicate the path and filename of the input image(For example:F:\\in.bmp): ");
    char str_in[100];
    memset(str_in,'\0',sizeof(str_in));
    scanf("%s",str_in);
    printf("Please indicate the path and filename of the output image(For example:F:\\out.bmp): ");
    char str_out[100];
    memset(str_out,'0',sizeof(str_out));
    scanf("%s",str_out);

    FILE *pfin=fopen(str_in,"rb");//输入文件指针
    FILE *pfout=fopen(str_out,"wb");//输出文件指针

    fread(&fileHeader,sizeof(BITMAPFILEHEADER),1,pfin);//文件流读入位图头文件
    fread(&infoHeader,sizeof(BITMAPINFOHEADER),1,pfin);//文件流读入位图信息头

    if(infoHeader.biBitCount==4)//如果是16位图
    {
        int size=infoHeader.biWidth*infoHeader.biHeight;
        fread(img,sizeof(struct RGB_QUAD),size,pfin);//读入像素矩阵

        int i,j;
        for(i=0; i<infoHeader.biHeight; i++)//灰度化处理
        {
            for(j=0; j<infoHeader.biWidth; j++)
            {
                img[i][j].rgbBlue=img[i][j].rgbBlue<<3;
                img[i][j].rgbGreen=img[i][j].rgbGreen<<2;
                img[i][j].rgbRed=img[i][j].rgbRed<<3;
                img[i][j].rgbBlue=img[i][j].rgbGreen=img[i][j].rgbRed=img[i][j].rgbBlue*0.11+img[i][j].rgbGreen*0.59+img[i][j].rgbRed*0.30;
            }
        }

        fwrite(&fileHeader,sizeof(fileHeader),1,pfout);//写入头文件
        fwrite(&infoHeader,sizeof(infoHeader),1,pfout);//写入信息头
        fwrite(img,sizeof(struct RGB_QUAD),size,pfout);//写入像素矩阵
    }
    else
    {
        printf("此图并非16位BMP图\n");
    }
    fclose(pfin);//关闭指针
    fclose(pfout);
    return 0;
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值