【问题标题】:Program to create bitmap file创建位图文件的程序
【发布时间】:2020-11-10 22:47:37
【问题描述】:

我的代码有问题,它是 .bmp 文件的生成器,没有任何库来生成文件。

程序生成一个空白图像,即使是像素数据。我试图改变字节,但我没有工作。

有什么想法吗?谢谢。 c++ 中的代码。

代码:

#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;

#define BYTES_PER_PIXEL 3
#define FILE_HEADER_SIZE 14
#define INFO_HEADER_SIZE 40

struct BITMAPFILEHEADER {
    unsigned short bfType;
    unsigned int bfSize;
    unsigned short bfReserved1;
    unsigned short bfReserved2;
    unsigned int bfOffBits;
};
struct BITMAPINFOHEADER {
    unsigned int biSize;
    unsigned int biWidth;  
    unsigned int biHeight;
    unsigned short biPlanes;
    unsigned short biBitCount;  
    unsigned int biCompression;
    unsigned int biSizeImage;
    unsigned int biXPelsPerMeter;
    unsigned int biYPelsPerMeter;
    unsigned int biClrUsed;
    unsigned int biClrImportant;
};

struct RGB_data {
    unsigned char b;
    unsigned char g;
    unsigned char r;
};

int main() {
    size_t height = 100;
    size_t width = 50;
    size_t size = width * height * 3;
    //------------------------------------------------------------------------------
    FILE *file = fopen("./sample.bmp", "wb");
    //------------------------------------------------------------------------------
    BITMAPFILEHEADER bmfh;
    BITMAPINFOHEADER bmih;
    //------------------------------------------------------------------------------
    RGB_data buffer[height][width];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            RGB_data data;
            data.b = 0;
            data.r = 0;
            data.b = 0;
            buffer[y][x] = data;
        }
    }
    //------------------------------------------------------------------------------
    bmfh.bfType = 0x4D42;
    bmfh.bfSize = size + FILE_HEADER_SIZE + INFO_HEADER_SIZE;
    bmfh.bfReserved1 = 0;
    bmfh.bfReserved2 = 0;
    bmfh.bfOffBits = bmfh.bfSize - size;
    //------------------------------------------------------------------------------
    bmih.biSize = 40;
    bmih.biWidth = width;
    bmih.biHeight = height;
    bmih.biPlanes = 1;
    bmih.biBitCount = 24;
    bmih.biCompression = 0;
    bmih.biSizeImage = size;
    bmih.biXPelsPerMeter = 0;
    bmih.biYPelsPerMeter = 0;
    bmih.biClrUsed = 0;
    bmih.biClrImportant = 0;
    //------------------------------------------------------------------------------
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            buffer[y][x].b = 10; 
            buffer[y][x].r = 20;
            buffer[y][x].g = 30;  
        }
    }
    //------------------------------------------------------------------------------
    fwrite(&bmfh, 1, FILE_HEADER_SIZE, file);
    fwrite(&bmih, 1, INFO_HEADER_SIZE, file);
    fwrite(&buffer, 3, size, file);
    fclose(file);
    //------------------------------------------------------------------------------ 
    cout << "Image generated successfully!" << endl;
    return 0;
}

附:我检查了正确的数据类型。我也尝试更改一些变量名称,但我没有工作。

【问题讨论】:

    标签: c++


    【解决方案1】:

    您没有在此处初始化 g

        data.b = 0;
        data.r = 0;
        data.b = 0;
    

    还包括#pragma pack(push, 1)在第一个结构的开头和#pragma pack(pop)在最后一个结构的结尾

    #include <iostream>
    #include <fstream>
    #include <stdio.h>
    
    using namespace std;
    
    #define BYTES_PER_PIXEL 3
    #define FILE_HEADER_SIZE 14
    #define INFO_HEADER_SIZE 40
    
    #pragma pack(push, 1)
    
    struct BITMAPFILEHEADER {
        unsigned short bfType;
        unsigned int bfSize;
        unsigned short bfReserved1;
        unsigned short bfReserved2;
        unsigned int bfOffBits;
    };
    struct BITMAPINFOHEADER {
        unsigned int biSize;
        unsigned int biWidth;  
        unsigned int biHeight;
        unsigned short biPlanes;
        unsigned short biBitCount;  
        unsigned int biCompression;
        unsigned int biSizeImage;
        unsigned int biXPelsPerMeter;
        unsigned int biYPelsPerMeter;
        unsigned int biClrUsed;
        unsigned int biClrImportant;
    };
    
    struct RGB_data {
        unsigned char b;
        unsigned char g;
        unsigned char r;
    };
    
    #pragma pack(pop)
    
    int main() {
        size_t height = 100;
        size_t width = 50;
        size_t size = width * height * 3;
        //------------------------------------------------------------------------------
        FILE *file = fopen("./sample.bmp", "wb");
        //------------------------------------------------------------------------------
        BITMAPFILEHEADER bmfh;
        BITMAPINFOHEADER bmih;
        //------------------------------------------------------------------------------
        RGB_data buffer[height][width];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                RGB_data data;
                data.b = 0;
                data.r = 0;
                data.b = 0;
                buffer[y][x] = data;
            }
        }
        //------------------------------------------------------------------------------
        bmfh.bfType = 0x4D42;
        bmfh.bfSize = size + FILE_HEADER_SIZE + INFO_HEADER_SIZE;
        bmfh.bfReserved1 = 0;
        bmfh.bfReserved2 = 0;
        bmfh.bfOffBits = bmfh.bfSize - size;
        //------------------------------------------------------------------------------
        bmih.biSize = 40;
        bmih.biWidth = width;
        bmih.biHeight = height;
        bmih.biPlanes = 1;
        bmih.biBitCount = 24;
        bmih.biCompression = 0;
        bmih.biSizeImage = size;
        bmih.biXPelsPerMeter = 0;
        bmih.biYPelsPerMeter = 0;
        bmih.biClrUsed = 0;
        bmih.biClrImportant = 0;
        //------------------------------------------------------------------------------
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                buffer[y][x].b = 10; 
                buffer[y][x].r = 20;
                buffer[y][x].g = 30;  
            }
        }
        //------------------------------------------------------------------------------
        fwrite(&bmfh, 1, FILE_HEADER_SIZE, file);
        fwrite(&bmih, 1, INFO_HEADER_SIZE, file);
        fwrite(&buffer, 3, size, file);
        fclose(file);
        //------------------------------------------------------------------------------ 
        cout << "Image generated successfully!" << endl;
        return 0;
    }
    

    【讨论】:

    猜你喜欢
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    相关资源
    最近更新 更多