简体   繁体   English

读取 Bitmap 文件

[英]Reading Bitmap file

I try to read a bitmap file.我尝试阅读 bitmap 文件。 This my program:这是我的程序:

#include<iostream>
#include<fstream>
#include <string>
#include<windows.h>
using namespace std;

#pragma pack(1)
struct header
{
    char header[2];
    int32_t filesize;
    int16_t reser;
    int16_t reser1;
    int32_t dataoffset;
};

struct infoheader
{
    int32_t headersize;
    int32_t width;
    int32_t height;
    int16_t plans;
    int16_t bpp;
    int32_t compression;
    int32_t datasize;
    int32_t re;
    int32_t ve;
    int32_t color;
    int32_t importantcolor;
};

struct  PIxel
{
    unsigned char G;
    unsigned char B;
    unsigned char R;
};

int main()
{
    header h;
    infoheader info;
    PIxel *p;
    ifstream file("bmp2.bmp", ios::binary);
    if (file.is_open())
    {
        cout << "true" << endl;
        file.read((char*)&h, sizeof(h));
        file.read((char*)&info, sizeof(info));
        cout << info.width << " " << info.height << " " << h.filesize << " " << info.bpp << endl;
        int pa = info.width % 4;
        int size = info.width * info.height * (info.bpp / 3) + pa * info.height;
        char* arr = new char[size];
        file.read(arr, size);
        char* temp = arr;
        int sizep = info.height * info.width;
        p = new PIxel[sizep];

        for (int i = 0; i < info.height; i++)
        {
            for (int j = 0; j < info.width; j++)
            {
                p[i * info.height + j].B = *(temp++);
                p[i * info.height + j].G = *(temp++);
                p[i * info.height + j].R = *(temp++);
                //p = p + 3;
            }
            p += pa;
        }

        HWND consoleWindow = GetConsoleWindow();
        HDC hdc = GetDC(consoleWindow);
        for (int i = 0; i < info.height; i++)
        {
            for (int j = 0; j < info.width; j++)
            {
                PIxel m = p[i * info.height + j];
                SetPixel(hdc, i, j, RGB(m.R, m.G, m.B));
            }
        }
        ReleaseDC(consoleWindow, hdc);
    }
}

It works but the image on my console is not right...它可以工作,但我控制台上的图像不正确......

图片

Can you help me to fix it?你能帮我修一下吗?

int size = info.width * info.height * (info.bpp / 3) + pa * info.height;

The above calculation for size is incorrect.上述尺寸计算不正确。 Bits per pixel should be divided by 8. The indexing in the for loops is also wrong.每个像素的位数应除以 8。for 循环中的索引也是错误的。 It ends ups multiplying height x height.它最终乘以高度 x 高度。

Also SetPixel(... i, j...) should be changed to SetPixel(... j, i...) since i in your case, refers to the y-axis. SetPixel(... i, j...)也应更改为SetPixel(... j, i...)因为在您的情况下, i指的是 y 轴。

As mentioned in previous answer, the padding has to be fixed too.如上一个答案所述,填充也必须固定。

Note that you can use LoadImage and other Windows GDI functions to open and draw bitmaps.请注意,您可以使用LoadImage和其他 Windows GDI 函数来打开和绘制位图。

int size = (info.width * (info.bpp / 8) + pa) * info.height;
...
for(int i = info.height - 1; i >= 0; i--)
{
    for(int j = 0; j < info.width; j++)
    {
        int index = i * (info.width) + j;
        p[index].B = *(temp++);
        p[index].G = *(temp++);
        p[index].R = *(temp++);
    }
    temp += pa;
}

for(int i = 0; i < info.height; i++)
{
    for(int j = 0; j < info.width; j++)
    {
        int index = i * (info.width) + j;
        PIxel m = p[index];
        SetPixel(hdc, j, i, RGB(m.R, m.G, m.B));
    }
}

I believe you have your padding adjustment on the wrong pointer.我相信您在错误的指针上进行了填充调整。 The padding is present on the source image.填充存在于源图像上。 You don't want it on the destination image.您不希望它出现在目标图像上。 You are accounting for the padding with p += pa;您正在使用p += pa;来计算填充。 you should instead replace this line with temp += pa to account for the padding of the source image.您应该将此行替换为temp += pa以考虑源图像的填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM