在 C 中创建 BMP 文件(位图) [英] Creating a BMP file (bitmap) in C

查看:19
本文介绍了在 C 中创建 BMP 文件(位图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 C 语言制作位图,只是通过代码.我目前正在尝试制作一个非常简单的 .bmp 图像,高度为 1px,宽度为 4 像素,全为白色像素.我已阅读格式说明并尝试应用它.这导致了以下代码:

I'm trying to make a bitmap in C, just from code. I'm currently trying to make a very easy .bmp image, with a height of 1px and a width of 4 pixels, with all white pixels. I have read the format description and tried to apply it. This resulted in the following code:

char bitmap[1000];

void BMPmake()
{
    // -- FILE HEADER -- //

    // bitmap signature
    bitmap[0] = 'B';
    bitmap[1] = 'M';

    // file size
    bitmap[2] = 66; // 40 + 14 + 12
    bitmap[3] = 0;
    bitmap[4] = 0;
    bitmap[5] = 0;

    // reserved field (in hex. 00 00 00 00)
    for(int i = 6; i < 10; i++) bitmap[i] = 0;

    // offset of pixel data inside the image
    for(int i = 10; i < 14; i++) bitmap[i] = 0;

    // -- BITMAP HEADER -- //

    // header size
    bitmap[14] = 40;
    for(int i = 15; i < 18; i++) bitmap[i] = 0;

    // width of the image
    bitmap[18] = 4;
    for(int i = 19; i < 22; i++) bitmap[i] = 0;

    // height of the image
    bitmap[22] = 1;
    for(int i = 23; i < 26; i++) bitmap[i] = 0;

    // reserved field
    bitmap[26] = 1;
    bitmap[27] = 0;

    // number of bits per pixel
    bitmap[28] = 24; // 3 byte
    bitmap[29] = 0;

    // compression method (no compression here)
    for(int i = 30; i < 34; i++) bitmap[i] = 0;

    // size of pixel data
    bitmap[34] = 12; // 12 bits => 4 pixels
    bitmap[35] = 0;
    bitmap[36] = 0;
    bitmap[37] = 0;

    // horizontal resolution of the image - pixels per meter (2835)
    bitmap[38] = 0;
    bitmap[39] = 0;
    bitmap[40] = 0b00110000;
    bitmap[41] = 0b10110001;

    // vertical resolution of the image - pixels per meter (2835)
    bitmap[42] = 0;
    bitmap[43] = 0;
    bitmap[44] = 0b00110000;
    bitmap[45] = 0b10110001;

    // color pallette information
    for(int i = 46; i < 50; i++) bitmap[i] = 0;

    // number of important colors
    for(int i = 50; i < 54; i++) bitmap[i] = 0;

    // -- PIXEL DATA -- //
    for(int i = 54; i < 66; i++) bitmap[i] = 0;
}

void BMPwrite()
{
    FILE *file;
    file = fopen("bitmap.bmp", "w+");
    for(int i = 0; i < 66; i++)
    {
        fputc(bitmap[i], file);
    }
    fclose(file);
}

当我尝试打开此图像时,它说图像已损坏.我在这里遗漏了什么吗?

When I try to open this image, it says that the image is damaged. Am I missing something here?

我还注意到 .bmp 整数的编码是小端的.我认为这意味着我必须颠倒字节的顺序.例如,4 个字节中的 256 是:00000000 00000000 00000001 00000000,我认为在小端中这将是:00000000 00000001 00000000 00000000

I also noticed that the encoding of the .bmp integers is little endian. I thought that this mean that I have to reverse the order of the bytes. For example, 256 in four bytes is: 00000000 00000000 00000001 00000000, and I think in little endian this would be: 00000000 00000001 00000000 00000000

有人可以帮我吗?我是否使用了正确的方法?任何帮助将不胜感激!

Can anyone give me a hand here? Am I using a right approach? Any help would be appreciated!

提前致谢!

推荐答案

您的像素偏移量(字节 10..13)为零,但像素数据实际上并不是从文件的开头开始,而是从字节开始54.

Your pixel offset (bytes 10..13) is zero, but the pixel data don't actually start at the beginning of the file, they start at byte 54.

还有:

  • 您对字节 34 的评论说的是位",但意思是字节",但这当然无关紧要.

  • Your comment on byte 34 says "bits" but means "bytes", but of course that doesn't matter.

您的水平和垂直分辨率的字节顺序有误,但我非常怀疑这是否重要.

Your horizontal and vertical resolutions have the wrong byte order, but I very much doubt that that matters.

如果我这样做,我会为标头数据定义结构(实际上,如果您使用的是 Windows,Microsoft 已经这样做了)并使用宏或其他东西将字节按可移植的正确顺序排列.

If I were doing this I'd define structs for the header data (indeed, if you're on Windows, Microsoft have already done this) and use a macro or something for putting bytes into the right order portably.

您是否必须颠倒字节顺序"取决于您使用的处理器的字节序.像您一样单独写入单独的字节是避免担心这一点的有效方法.

Whether you "have to reverse the order of the bytes" depends on the endianity of the processor you're using. Writing separate bytes separately, as you're doing, is an effective way to avoid having to worry about this.

这篇关于在 C 中创建 BMP 文件(位图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆