读取BMP文件并显示出来(C++实现)

读取BMP文件的时候我们首先需要弄清楚bmp格式文件的结构
bmp的文件格式
图片来源于《数字图像处理编程入门》

bmp文件包含在windows.h的头文件里。编写代码的时候可以直接调用Windows.h 头文件来调用。
实现代码

#include<iostream>
#include<windows.h>
#include<fstream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<iomanip>
using namespace std;
unsigned char *pBmpBuf;//读入图像数据的指针

int bmpWidth;//图像的宽
int bmpHeight;//图像的高
RGBQUAD *pColorTable;//颜色表指针

int biBitCount;//图像类型,每像素位数



//-----------------------------------------------------------------------------------------
//给定一个图像位图数据、宽、高、颜色表指针及每像素所占的位数等信息,将其写到指定文件中
bool readBmp(char *bmpName) 
{
    FILE *fp=fopen(bmpName,"rb");//二进制读方式打开指定的图像文件

    if(fp==0)
        return 0;

    //跳过位图文件头结构BITMAPFILEHEADER

    fseek(fp, sizeof(BITMAPFILEHEADER),0);

    //定义位图信息头结构变量,读取位图信息头进内存,存放在变量head中

    BITMAPINFOHEADER head;  

    fread(&head, sizeof(BITMAPINFOHEADER), 1,fp); //获取图像宽、高、每像素所占位数等信息

    bmpWidth = head.biWidth;

    bmpHeight = head.biHeight;

    biBitCount = head.biBitCount;//定义变量,计算图像每行像素所占的字节数(必须是4的倍数)

    int lineByte=(bmpWidth * biBitCount/8+3)/4*4;//灰度图像有颜色表,且颜色表表项为256

    if(biBitCount==8)
    {

        //申请颜色表所需要的空间,读颜色表进内存

        pColorTable=new RGBQUAD[256];

        fread(pColorTable,sizeof(RGBQUAD),256,fp);

    }

    //申请位图数据所需要的空间,读位图数据进内存

    pBmpBuf=new unsigned char[lineByte * bmpHeight];

    fread(pBmpBuf,1,lineByte * bmpHeight,fp);

    fclose(fp);//关闭文件

    return 1;//读取文件成功
}

bool saveBmp(char *bmpName, unsigned char *imgBuf, int width, int height, int biBitCount, RGBQUAD *pColorTable)
{

    //如果位图数据指针为0,则没有数据传入,函数返回

    if(!imgBuf)
        return 0;

    //颜色表大小,以字节为单位,灰度图像颜色表为1024字节,彩色图像颜色表大小为0

    int colorTablesize=0;

    if(biBitCount==8)
        colorTablesize=1024;

    //待存储图像数据每行字节数为4的倍数

    int lineByte=(width * biBitCount/8+3)/4*4;

    //以二进制写的方式打开文件

    FILE *fp=fopen(bmpName,"wb");

    if(fp==0)
        return 0;

    //申请位图文件头结构变量,填写文件头信息

    BITMAPFILEHEADER fileHead;

    fileHead.bfType = 0x4D42;//bmp类型

    //bfSize是图像文件4个组成部分之和

    fileHead.bfSize= sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + colorTablesize + lineByte*height;

    fileHead.bfReserved1 = 0;

    fileHead.bfReserved2 = 0;

    //bfOffBits是图像文件前3个部分所需空间之和

    fileHead.bfOffBits=54+colorTablesize;

    //写文件头进文件

    fwrite(&fileHead, sizeof(BITMAPFILEHEADER),1, fp);

    //申请位图信息头结构变量,填写信息头信息

    BITMAPINFOHEADER head; 

    head.biBitCount=biBitCount;

    head.biClrImportant=0;

    head.biClrUsed=0;

    head.biCompression=0;

    head.biHeight=height;

    head.biPlanes=1;

    head.biSize=40;

    head.biSizeImage=lineByte*height;

    head.biWidth=width;

    head.biXPelsPerMeter=0;

    head.biYPelsPerMeter=0;

    //写位图信息头进内存

    fwrite(&head, sizeof(BITMAPINFOHEADER),1, fp);

    //如果灰度图像,有颜色表,写入文件 

    if(biBitCount==8)
        fwrite(pColorTable, sizeof(RGBQUAD),256, fp);

    //写位图数据进文件

    fwrite(imgBuf, height*lineByte, 1, fp);

    //关闭文件

    fclose(fp);

    return 1;

}
 void doIt()
{
    char readPath[]="girl.bmp";
    readBmp(readPath);
    // 输出整体图像信息
    cout << "width=" << bmpWidth << "height=" << bmpHeight << "biBitCount=" << biBitCount <<endl;
    // 图像的字节数
     int linebyte1=(bmpWidth*biBitCount/8+3)/4*4;
     int n =0,m=0,count_xiang_su = 0;

     // ofstream outfile("图像像素.txt",ios::in|ios::trunc);将像素数据存入TXT文件。
    //初始化原始像素的数组。

     if(biBitCount == 8)
    {
      for(int i=0;i<bmpHeight/2;i++)
        {
            for(int j=0;j<bmpWidth/2;i++)
                *(pBmpBuf+i*linebyte1+j)=0;
        }
    }

   if(biBitCount == 24)
    {
        for(int i=0;i<bmpHeight;i++)
        {
            for(int j=0;j<bmpWidth;j++)
            {
                for(int k=0;k<3;k++)//每像素RGB三个分量分别置0才变成黑色
                {
                  m= *(pBmpBuf+i*linebyte1+j*3+k);

                    count_xiang_su++;

                }
                n++;
            }


        }
        cout<<"总的像素个素为:"<<n<<endl;
        cout<<"----------------------------------------------------"<<endl;
    }

    // jiang tuxiang shuju cunpan .
     char writePath[] = "necpy.BMP";
     saveBmp(writePath, pBmpBuf, bmpWidth, bmpHeight, biBitCount, pColorTable);

    //清除缓冲区,pBmpBuf和pColorTable是全局变量,在文件读入时申请的空间

        delete []pBmpBuf;
         if(biBitCount==8)
        delete []pColorTable;

 }




int main()
{
    doIt();
    system("pause");
    return 0;
}
  • 22
    点赞
  • 176
    收藏
    觉得还不错? 一键收藏
  • 20
    评论
你可以使用C++的标准库和一些第三方库来读取bmp图片的颜色信息。 以下是使用标准库的代码示例: ```c++ #include <iostream> #include <fstream> #include <cstdint> #pragma pack(push, 1) struct BMPHeader { char id[2]; uint32_t size; uint16_t reserved1; uint16_t reserved2; uint32_t offset; uint32_t dib_size; int32_t width; int32_t height; uint16_t planes; uint16_t bpp; uint32_t compression; uint32_t image_size; int32_t x_ppm; int32_t y_ppm; uint32_t color_used; uint32_t color_important; }; #pragma pack(pop) int main() { std::ifstream file("image.bmp", std::ios::binary); // 打开bmp文件 if (!file.is_open()) { std::cerr << "Failed to open file\n"; return 1; } BMPHeader header; file.read(reinterpret_cast<char*>(&header), sizeof(header)); // 读取bmp文件头 if (header.id[0] != 'B' || header.id[1] != 'M') { std::cerr << "Invalid file format\n"; return 1; } if (header.bpp != 24) { // 只处理24位bmp图像 std::cerr << "Only 24-bit BMP images are supported\n"; return 1; } const int32_t width = header.width; const int32_t height = std::abs(header.height); const uint32_t row_size = ((width * 3) + 3) / 4 * 4; // 每行字节数 const uint32_t data_size = row_size * height; // 图像数据大小 std::unique_ptr<uint8_t[]> data(new uint8_t[data_size]); // 分配内存用于存储图像数据 file.read(reinterpret_cast<char*>(data.get()), data_size); // 读取图像数据 // 从图像数据中提取颜色信息 for (int32_t y = 0; y < height; ++y) { for (int32_t x = 0; x < width; ++x) { const uint8_t* pixel = &data[y * row_size + x * 3]; const uint8_t red = pixel[2]; const uint8_t green = pixel[1]; const uint8_t blue = pixel[0]; // 在这里处理颜色信息,比如输出每个像素点的RGB值 std::cout << "Pixel (" << x << ", " << y << "): RGB(" << static_cast<int>(red) << ", " << static_cast<int>(green) << ", " << static_cast<int>(blue) << ")\n"; } } return 0; } ``` 如果你想使用第三方库,可以考虑使用OpenCV库。以下是使用OpenCV读取bmp文件的代码示例: ```c++ #include <iostream> #include <opencv2/opencv.hpp> int main() { cv::Mat image = cv::imread("image.bmp", cv::IMREAD_COLOR); // 读取bmp文件 if (image.empty()) { std::cerr << "Failed to open file\n"; return 1; } // 提取颜色信息 for (int32_t y = 0; y < image.rows; ++y) { for (int32_t x = 0; x < image.cols; ++x) { const cv::Vec3b& pixel = image.at<cv::Vec3b>(y, x); const uint8_t red = pixel[2]; const uint8_t green = pixel[1]; const uint8_t blue = pixel[0]; // 在这里处理颜色信息,比如输出每个像素点的RGB值 std::cout << "Pixel (" << x << ", " << y << "): RGB(" << static_cast<int>(red) << ", " << static_cast<int>(green) << ", " << static_cast<int>(blue) << ")\n"; } } return 0; } ```
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值