c++ - 如何在 C++ 中分析位图文件?

标签 c++

我现在需要了解如何分析 BMP,这是最简单的方法。这只是一个更大计划的一小部分,我真的没有太多时间来做这件事。基本上我现在只需要知道黑色像素和白色像素的“坐标”。我做了一些研究,发现以下网站很有趣: site I , site II
第二个是波兰语,但那里发布的代码是可以理解的。第一个似乎要复杂得多(并且只能在 Windows 上工作,这对我来说没问题,但我尽量避免使用),第二个使用 vcl.h,它是 Borland 特定的头文件(我使用 VisualStudio)。如果能提供任何帮助、建议或指向涵盖该主题的网站的链接,我将不胜感激。

PS:这是我的第一篇文章,如果您不喜欢我问问题的方式,请告诉我如何做得更好。如果您需要更多信息,请询问。

这是代码,感谢您的帮助。

// read_and_send_bmp.cpp
#include <iostream><\code>
#include <string>
#include "bitmap_image.hpp"

using std::string;
using std::cin;
using std::cout;
using std::endl;

char * check(string name_of_bitmap);

int main()
{
    string name_of_bitmap;
    cout << "Name of file: ";
    cin >> name_of_bitmap;
    char * toPrint = check(name_of_bitmap);
    cout << endl << "this is the value of 'toPrint': "<< toPrint;
    /* send
    ...
    */
    system("PAUSE");
    return 0;
}

char * check(string name_of_bitmap)
{
    bitmap_image myBitMapImage(name_of_bitmap);
    int size = myBitMapImage.pixel_count();
    char * toReturn = new char[size+1];
        for(int i = 0; i < myBitMapImage.pixel_count(); i++)
        toReturn[i] = 'f';
     int h = 1;
     for(int j = 0, jlen = myBitMapImage.height(); j < jlen; j++)
    {
        for(int i = 0, ilen = myBitMapImage.width(); i < ilen; i++, h++)
            {
            if(myBitMapImage.red_channel(i, j) == 0 && myBitMapImage.green_channel(i, j) == 0 && myBitMapImage.blue_channel(i, j) == 0)
            {
            toReturn[(j)*myBitMapImage.width()+i] = 'b';
            cout << "pixel nr." << (j)*myBitMapImage.width()+i << " = b" << endl;
            }
            else
            {
            toReturn[(j)*myBitMapImage.width()+i] = 'w';
            cout << "pixel nr." << (j)*myBitMapImage.width()+i << " = w" << endl;
            }
        }
    }
    toReturn[size]='\0';
    return toReturn;
}

最佳答案

看看这个:http://www.kalytta.com/bitmap.h .

使用 CBitmap::Load 加载文件,然后使用 CBitmap::GetBits 获取指向大小为 4 x 宽 x 高的 RGBA 缓冲区的指针。

白色像素有 R = G = B = 255,黑色像素有 R = G = B = 0。

关于c++ - 如何在 C++ 中分析位图文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14123744/

相关文章:

c++ - 使用 C++11 for() 循环遍历 vector<unique_ptr<mytype>>

c++ - 在 C++ 中处理多维数组长度

c++ - SDL 将光标更改为 Windows 默认值

C++ ifstream 和 "umlauts"

c++ - 如果没有使用带有虚函数的类,是否会生成虚函数表?

c++ - 线程处理 C++ linux OS

c++ - 右值委托(delegate),或 const 成员函数的右值等价物

c++ - 如何有效地使用 boost asio 套接字进行全双工流式传输?

C++17 内联变量与内联静态变量

c++ - 方法体中的静态变量是否由所有实例共享