读取BMP 32位

时间:2020-04-18 02:40:36

标签: c++ file struct bmp

我尝试读取一个位图文件(32位)。这是我的程序:

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

#pragma pack(1)
struct header
{
    int16_t header;
    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;
    unsigned char A;
};





int main()
{
    cout << 1 << " ";
    header h;
    infoheader info;
    PIxel* p;
    string name;
    getline(cin, name);

    ifstream file(name, ios::binary);
    if (file.is_open())
    {

        cout << "true" << endl;
        file.read((char*)&h, sizeof(h));
        if (h.header == 0x4D42)
        {
            cout << 1 << endl;
            file.read((char*)&info, sizeof(info));
            cout << info.width << " " << info.height << " " << h.filesize << " " << info.bpp << endl;
           // int pa = 3;
            cout << info.bpp << endl;
            int size = info.width * info.height * info.bpp / 8 ;
            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 = info.height - 1; i >= 0; i--)
            {
                for (int j = 0; j < info.width; j++)
                {
                    //temp++;
                    int index = i * info.width + j;
                    p[index].A = *(temp++);
                    p[index].B = *(temp++);
                    p[index].G = *(temp++);
                    p[index].R = *(temp++);


                }
             // temp += 1;
            }


            HWND consoleWindow = GetConsoleWindow();
            HDC hdc = GetDC(consoleWindow);
            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.A,m.R, m.G, m.B));


                }
            }
            ReleaseDC(consoleWindow, hdc);
        }
        else
        {
            cout << 0;
        }
    }
}

它可以工作,但是控制台上的图像不正确... enter image description here 输入图片: enter image description here

我不知道alpha的实际作用是什么,向位图图像添加透明度并不像听起来那样容易。并非所有应用程序都支持透明位图。 您能帮我解决它并解释Alpha的实际作用吗?

0 个答案:

没有答案