【问题标题】:Creating bitmap using C使用 C 创建位图
【发布时间】:2015-05-31 16:15:34
【问题描述】:

这是来自这里的后续问题,Link

我从 Code::Blocks 的链接中阅读并复制粘贴了代码,但做了一点改动,

现在我想创建一个 20x20 黑色图像

所以我编辑了代码,但我无法在 Win7 上打开图像,它显示“Windows 照片查看器无法打开此图像”。

谁能告诉我代码有什么问题??

我的代码:-

#include<stdio.h>
 unsigned char bitmap[1000];


void BMPmake()
{
    int i;
    // -- FILE HEADER -- //

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

    // file size
    bitmap[2] = 0xc6; // 40 + 14 + 400
    bitmap[3] = 0x01;
    bitmap[4] = 0;
    bitmap[5] = 0;

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

    // offset of pixel data inside the image
    //thats is 54 or d8 the difference between the starting and the position where data actually starts.
    bitmap[10]=0xd8;
    for( i = 11; i < 14; i++) bitmap[i] = 0;


    // -- BITMAP HEADER -- //

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

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

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

    // no of color planes, must be 1
    bitmap[26] = 1;
    bitmap[27] = 0;

    // number of bits per pixel
    bitmap[28] = 8; // 1 byte
    bitmap[29] = 0;

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

    // size of pixel data
    bitmap[34] = 0x90; // 400 bytes => 400 pixels ,,,, 20x20x1
    bitmap[35] = 0x01;
    bitmap[36] = 0;
    bitmap[37] = 0;

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

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

    // color palette information here 256
    bitmap[46]=0xff;
    bitmap[47]=1;
    for( i = 48; i < 50; i++) bitmap[i] = 0;

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

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

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

    BMPmake();
    BMPwrite();
    printf("Done!!");
}

【问题讨论】:

  • bitmap[2] = 454; 该值不适合单个字符。
  • 我有一个建议:如何创建一个大小为20x20 的黑色背景位图,然后在一些十六进制编辑器中打开这个位图和你的输出并将它们进行比较以找到可能的错误 -造成差异?
  • 如果你使用的是数组而不是结构体,它应该是 unsigned char bitmap[1000];
  • 偏移量 46 是调色板条目的数量 - 此处为 0xFF,但您没有提供 256 条目的调色板表。更糟糕的是,它是一个 4 字节字段,而您有 0x1FF
  • 我很想重写代码,但由于一切都是“硬编码”的,我将把它留给你。请注意,图像数据的偏移量需要更改。那里有很多资源,例如en.wikipedia.org/wiki/BMP_file_format。我还推荐@user35443 的建议,如果这不是太麻烦的话……只需几分钟!

标签: c bitmap


【解决方案1】:

感谢 Weather Vanewildplasseruser35443,我能够解决我的问题。

主要问题是no of bits/pixel&lt;=8时我没有实现位图中必须的颜色表,所以通过与我的原始位图比较我能够找到8位灰度颜色表的格式,我生成了它使用以下代码:

unsigned char temp=0;
int end_color=54+4*noColor;
//where noColor is 256 as 2^8 here where 8 is no of bits/pixel.
for (i=54;i<end_color;i+=4)
    {
        bitmap[i]=temp;
        bitmap[i+1]=temp;
        bitmap[i+2]=temp;
        bitmap[i+3]=0;
        temp++;
    }

总体代码:-

#include<stdio.h>
   unsigned char bitmap[1300];


void BMPmake()
{
    int i,noColor=256,end_color=54+4*noColor;
    static unsigned char temp=0;
    // -- FILE HEADER -- //

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

    // file size
    bitmap[2] = 0xc6; // 40 + 14 + 256*4+400
    bitmap[3] = 0x05;
    bitmap[4] = 0;
    bitmap[5] = 0;

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

    // offset of pixel data inside the image
    //The offset, i.e. starting address, of the byte where the bitmap image data (pixel array) can be found.
    //here 1078
    bitmap[10]=0x36;
    bitmap[11]=0x04;
    for( i = 12; i < 14; i++) bitmap[i] = 0;


    // -- BITMAP HEADER -- //

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

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

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

    // no of color planes, must be 1
    bitmap[26] = 1;
    bitmap[27] = 0;

    // number of bits per pixel
    bitmap[28] = 8; // 1 byte
    bitmap[29] = 0;

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

    // size of pixel data
    bitmap[34] = 0x90; // 400 bytes => 400 pixels ,,,, 20x20x1
    bitmap[35] = 0x01;//0x190
    bitmap[36] = 0;
    bitmap[37] = 0;

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

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

    // color palette information here 256
    bitmap[46]=0;
    bitmap[47]=1;
    for( i = 48; i < 50; i++) bitmap[i] = 0;

    // number of important colors
    // if 0 then all colors are important
    for( i = 50; i < 54; i++) bitmap[i] = 0;

    //Color Palette
    //for less then or equal to 8 bit BMP Image we have to create a 4*noofcolor size color palette which is nothing but
    //[BLUE][GREEN][RED][ZERO] values
    //for 8 bit we have the following code
    for (i=54;i<end_color;i+=4)
    {
        bitmap[i]=temp;
        bitmap[i+1]=temp;
        bitmap[i+2]=temp;
        bitmap[i+3]=0;
        temp++;
    }

    // -- PIXEL DATA -- //
    for( i = end_color; i < end_color+400; i++) bitmap[i] = 0xff;
}

void BMPwrite()
{
    FILE *file;
    int i;

    //use wb+ when writing to binary file .i.e. in binary form whereas w+ for txt file.
    file = fopen("b.bmp", "wb+");
    for( i = 0; i < 1478; i++)
    {
        fputc(bitmap[i], file);
    }
    fclose(file);
}
void main()
{

    BMPmake();
    BMPwrite();
    printf("Done!!");
}

【讨论】:

    【解决方案2】:

    0xff 是 255,那么它指向调色板 255 的索引,它保存 rgb 的值全部为 255,然后它应该显示白色,你怎么会变黑

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 2012-02-03
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    相关资源
    最近更新 更多