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

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

问题描述

我试图让在C位图,刚刚从code。我目前正在做一个非常简单的.BMP图像,用1px的高度和4个像素宽度,所有的白色像素。我已阅读的格式说明,并试图将其应用。这导致以下code:

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整数的编码,就是little endian。我认为,这意味着我必须反转字节顺序。例如,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

谁能给我一只手在这里?我使用一个正确的做法?任何帮助将是AP preciated!

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,微软已经做到了这一点),并使用宏或东西把字节到正确的顺序可移植。

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.

您是否有扭转字节顺序取决于你所使用的处理器的endianity。分开单独写字节,因为你正在做的,是为了避免不必担心这样的一个有效途径。

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天全站免登陆