如何使用C语言为高度= 480和宽度= 752写24 bpp bmp标头 [英] How to write 24 bpp bmp header for height=480 and width=752 using C language

查看:126
本文介绍了如何使用C语言为高度= 480和宽度= 752写24 bpp bmp标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每一个这个代码都是写8bpp heigt的图像,宽度等于20 * 20

实际上我想让这个24bpp的头部结构和高度= 480,宽度= 752可以任意一个帮我做这个。



#include

unsigned char bitmap [1300];





void BMPmake()

{

int i,noColor = 256,end_color = 54 + 4 * noColor;

static unsigned char temp = 0;

// - 文件头 - //



/ /位图签名

位图[0] ='B';

位图[1] ='M';



//文件大小

位图[2] = 0xc6; // 40 + 14 + 256 * 4 + 400

位图[3] = 0x05;

位图[4] = 0;

位图[5] = 0;



//保留字段(十六进制00 00 00 00)

for(i = 6; i 400像素,,,, 20x20x1

位图[35] = 0x01; // 0x190

位图[36] = 0;

位图[ 37] = 0;



//图像的水平分辨率 - 每米像素数(2835)

位图[38] = 0;

位图[39] = 0;

位图[40] = 0;

位图[41] = 0;



//图像的垂直分辨率 - 每米像素数(2835)

位图[42] = 0;

位图[ 43] = 0;

位图[44] = 0;

位图[45] = 0;



//这里的调色板信息256

位图[46] = 0;

位图[47] = 1;

for(i = 48; i< 50; i ++)bitmap [i] = 0;



//重要数量颜色

//如果为0那么所有颜色都很重要

for(i = 50;我< 54; i ++)bitmap [i] = 0;



//调色板

//小于或等于8位BMP图像我们必须创建一个4 * noofcolor大小的调色板,这只是

// [蓝色] [绿色] [红色] [ZERO]值

// 8位我们有以下代码

for(i = 54; i< end_color; i + = 4)

{

bitmap [i] = temp ;

位图[i + 1] = temp;

位图[i + 2] = temp;

位图[i + 3] = 0;

temp ++;

}



// - PIXEL DATA - //

for(i = end_color; i< end_color + 400; i ++)bitmap [i] = 0xff;

}



void BMPwrite()

{

FILE * file;

int i;



//写入二进制文件.ie时使用wb +二进制形式,而w +代表txt文件。

file = fopen(b.bmp,wb +);

for(i = 0; i< 1478 ; i ++)

{

fputc(位图[i],文件);

}

fclose(文件);

}

void main()

{



BMPmake( );

BMPwrite();

printf(完成!!);

}



我尝试了什么:



i尝试过如下代码

 #include 
unsigned char bitmap [1300];


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

//位图签名
位图[0] ='B';
bitmap [1] ='M';

//文件大小
位图[2] = 0xc6; // 40 + 14 + 256 * 4 + 400
位图[3] = 0x05;
位图[4] = 0;
位图[5] = 0;

//保留字段(十六进制00 00 00 00)
for(i = 6; i 400像素,,,, 20x20x1
位图[35] = 0x01; // 0x190
位图[36] = 0;
位图[37] = 0;

//图像的水平分辨率 - 每米像素数(2835)
位图[38] = 0;
位图[39] = 0;
位图[40] = 0;
位图[41] = 0;

/ /图像的垂直分辨率 - 每米像素数(2835)
位图[42] = 0;
位图[43] = 0;
位图[44] = 0;
bitmap [45] = 0;

//这里的调色板信息256
位图[46] = 0;
位图[47] = 1;
for( i = 48; i <50; i ++)bitmap [i] = 0;

//重要颜色数
//如果为0那么所有颜色都很重要
for (i = 50; i <54; i ++)bitmap [i] = 0;

//调色板
//小于或等于8位BMP图像我们必须创建一个4 * noofcolor大小的调色板,这只是
// [蓝色] [绿色] [红色] [零]值
//对于8位我们有以下代码
for(i = 54; i< end_color; i + = 4)
{
bitmap [i] = temp;
位图[i + 1] = temp;
位图[i + 2] = temp;
位图[i + 3] = 0;
temp ++;
}

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

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

//写入二进制文件时使用wb + .i.e。以二进制形式,而w +为txt文件。
file = fopen(b.bmp,wb +);
for(i = 0; i< 1478; i ++)
{
fputc(bitmap [i],file);
}
fclose(file);
}
void main()
{

BMPmake();
BMPwrite();
printf(完成!!);
}

解决方案

使用结构而不是字节数组:

BITMAPFILEHEADER结构(Windows) [ ^ ]

BITMAPINFO结构(Windows) [ ^ ]

BITMAPINFOHEADER结构(Windows) [ ^ ]



定义这些结构的变量,初始化它们,并将它们写入文件,然后是位图像素数据。请注意,没有24 BPP的颜色表( biClrUsed 为零)。

 BITMAPFILEHEADER fh; 
BITMAPINFOHEADER fi;

无符号 width = 752 ;
无符号 height = 480 ;
// 32位对齐
unsigned widthBytes =((width * 24 + 31 )& ~31) / 8 ;
unsigned bppSize = widthBytes * height;

fh.bfType = 0x4D42; // BM
fh.bfSize = sizeof (fh)+ sizeof (fi)+ bppSize;
fh.bfReserved1 = fh.bfReserved2 = 0 ;
fh.bfOffBits = sizeof (fh)+ sizeof (fi);

fi.biSize = sizeof (fi);
fi.biWidth = width;
fi.biHeight =身高;
fi.biPlanes = 1 ;
fi.biBitCount = 24 ;
fi.biCompression = BI_RGB; // BI_RGB为0
fi.biSizeImage = bppSize;
fi.biXPelsPerMeter = fi.biYPelsPerMeter = 0 ;
fi.biClrUsed = 0 ;
fi.biClrImportant = 0 ;

fwrite(& fh, sizeof (fh), 1 ,文件);
fwrite(& fi, sizeof (fi), 1 ,file);
// 在此处写位图数据

< br>

请参阅位图(Windows) [< a href =https://msdn.microsoft.com/en-us/library/dd183377(v=vs.85).aspx\"target =_ blanktitle =New Window> ^ ]。

hi every one this code is to write image of 8bpp heigt and width equal to 20*20
actually i want to make this header structure for 24bpp and height =480 and width=752 can any one help me to do this .

#include
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 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!!");
}

What I have tried:

i have tried as below code

 #include
   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  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!!");
}

解决方案

Use structures instead of a byte array:
BITMAPFILEHEADER structure (Windows)[^]
BITMAPINFO structure (Windows)[^]
BITMAPINFOHEADER structure (Windows)[^]

Define variables for these structures, initialise them, and write them to the file followed by the bitmap pixel data. Note that there is no colour table with 24 BPP (biClrUsed is zero).

BITMAPFILEHEADER fh;
BITMAPINFOHEADER fi;

unsigned width = 752;
unsigned height = 480;
// 32-bit aligned
unsigned widthBytes = ((width * 24 + 31) & ~31) / 8;
unsigned bppSize = widthBytes * height;

fh.bfType = 0x4D42; // "BM"
fh.bfSize = sizeof(fh) + sizeof(fi) + bppSize;
fh.bfReserved1 = fh.bfReserved2 = 0;
fh.bfOffBits = sizeof(fh) + sizeof(fi);

fi.biSize = sizeof(fi);
fi.biWidth = width;
fi.biHeight = height;
fi.biPlanes = 1;
fi.biBitCount = 24;
fi.biCompression = BI_RGB; // BI_RGB is 0
fi.biSizeImage = bppSize;
fi.biXPelsPerMeter = fi.biYPelsPerMeter = 0;
fi.biClrUsed = 0;
fi.biClrImportant = 0;

fwrite(&fh, sizeof(fh), 1, file);
fwrite(&fi, sizeof(fi), 1, file);
// write bitmap data here


See Bitmaps (Windows)[^].


这篇关于如何使用C语言为高度= 480和宽度= 752写24 bpp bmp标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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