通过C ++在控制台上写小bmp

时间:2014-12-20 00:58:48

标签: c++ io console bmp

我正在尝试阅读16x16 bmp,但没有1 pixel = 3 bits (RGB)。即使前4-5行为白色,其余为黑色,每个像素的文档仍然满255 255 255

在我的情况下,我需要通过分析每个像素的RGB图层在控制台中显示这个图像,但是它有很多麻烦。

int main()
{
    FILE* f = fopen("image.bmp", "rb");
    unsigned char info[54];
    fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header
    // extract image height and width from header
    int width = *(int*)&info[18];
    int height = *(int*)&info[22];
    int size = 3 * width * height;
    unsigned char * data = new unsigned char[size]; // allocate 3 bytes per pixel
    fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
    fclose(f);
    for (int i = 0; i < size; i += 3)
    {
        unsigned char tmp = data[i];
        data[i] = data[i + 2];
        data[i + 2] = tmp;
    }
    unsigned int * byteData = new unsigned int[size];
    for (int i = 0; i <= size; i++)
    {
        byteData[i] = (int) data[i];
    }
    for (int i = 0; i <= size / 3; i++)
    {
        cout << i << ".\t" << byteData[i] << "\t" << byteData[i + 1] << "\t" << byteData[i + 2] << endl;
    }
    cout << endl;
    cout << "=======================" << endl;
    for (int j = 0; j < width; j++)
    {
        cout << j + 1 << ".\t";
        for (int i = 0; i < height; i++)
        {
            //if ((int)data[j * width + i] >= 100 && (int)data[j * width + i + 1] >= 100 && (int)data[j * width + i + 2] >= 100)
            if (((int) data[j * width + i] + (int) data[j * width + i + 1] + (int) data[j * width + i + 2]) / 3 <= 170)
                cout << " ";
            else cout << "*";
        }
        cout << endl;
    }
    getchar();
    return 0;
}

因为我认为字节序列和读取内存的问题是垃圾,但是如果你能解释哪里有泄漏?

1 个答案:

答案 0 :(得分:0)

解决方案是:bmp应该以24 bpp和自上而下的行顺序创建。

正确代码适用于16x16位图:

#include <iostream>
using namespace std;

unsigned char* readBMP(char* filename);
int main()
{
    unsigned char * data = readBMP("winLogo.bmp");
    int size = 16*16*3;
    unsigned int * byteData = new unsigned int[size];
    for (int i = 0; i <= size; i++)
    {
        byteData[i] = (int)data[i];
    }
    int k = 0;
    //uncomment to write a line of RGB values
    //for (int i = 0; i < size; i += 3)
    //{
    //  cout << k+1 << ".\t" << byteData[i] << "\t" << byteData[i + 1] << "\t" << byteData[i + 2] << endl;
    //  k++;
    //}

    for (int i = 0; i < size; i+= 3)
    {
        if (i % 16*3 == 0)
        {
            cout << endl << (i/(16*3))+1 << ".\t";
        }
        else {};
        if (byteData[i] >= 200 && byteData[i + 1] >= 200 && byteData[i + 2] >= 200)
            cout << " ";
        else
            cout << "#";
    }
    getchar();
    return 0;
}

unsigned char* readBMP(char* filename)
{
    int i;
    FILE* f = fopen(filename, "rb");
    unsigned char info[54];
    fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header
    // extract image height and width from header
    int width = *(int*)&info[18];
    int height = *(int*)&info[22];

    int size = -3 * width * height;
    unsigned char* data = new unsigned char[size]; // allocate 3 bytes per pixel
    fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
    fclose(f);
    //BGR -> RGB
    for (i = 0; i < size; i += 3)
    {
        unsigned char tmp = data[i];
        data[i] = data[i + 2];
        data[i + 2] = tmp;
    }
    return data;
}