如何解决这种 1 位单色黑白 bmp 图像的二进制表示有什么问题?
我编写了一个 ANSI C 代码来构建一个简单的单色 BMP 图像(仅限黑白),每个像素用 1 位表示。
图像像素数为 16*16(32 字节),我选择此大小以避免填充作为可被 4 整除的字节数,然后我用 0xff 填充所有 32 字节以显示白色图像。
这是我的 Ubuntu 终端上的二进制输出
00000000 42 4d 5e 00 00 00 00 00 00 00 3e 00 00 00 28 00 |BM^.......>...(.|
00000010 00 00 10 00 00 00 10 00 00 00 01 00 01 00 00 00 |................|
00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000030 00 00 00 00 00 00 00 00 00 00 00 00 ff 00 ff ff |................|
00000040 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff |................|
*
0000005e
我的代码:
#include "stdio.h"
#include "stdint.h"
#include "stdlib.h"
struct bmpHeader{
unsigned char Signature[2];
unsigned char FileSize[4];
unsigned char reserved[4];
unsigned char DataOffset[4];
}__attribute__( (packed) );
struct bmpInfoHeader{
unsigned char Size[4];
unsigned char Width[4];
unsigned char Height[4];
unsigned char Planes[2];
unsigned char BitsPerPixel[2];
unsigned char Compression[4];
unsigned char ImageSize[4];
unsigned char XpixelsPerM[4];
unsigned char YpixelsPerM[4];
unsigned char ColorsUsed[4];
unsigned char ImportantColors[4];
}__attribute__( (packed) );
int main(void)
{
uint32_t QR_width = 16; uint32_t QR_height = 16;
unsigned char palette_data[8] = {0x00,0x00,0xff,0x00};
uint32_t size = ( ((QR_width*QR_height)/8)+ ( ((QR_width*QR_height) % 8 )? 1 : 0 ) + 54 + sizeof(palette_data));
struct bmpHeader a;
struct bmpInfoHeader b;
unsigned char headerSize = sizeof(a) + sizeof(b) + sizeof(pallete_data);
printf(" size of struct is %d\n",sizeof(a) + sizeof(b));
printf(" size of image data in bytes = %d\n",size-54-8);
printf(" size of file in bytes = %d\n",size);
a.Signature[0] = 0x42; a.Signature[1] = 0x4d; //'BM'
a.FileSize[0] = (unsigned char) size; a.FileSize[1] = (unsigned char) size>>8; a.FileSize[2] = (unsigned char) size>>16; a.FileSize[3] = (unsigned char) size>>24;
a.reserved[0] = 0; a.reserved[1] = 0; a.reserved[2] = 0; a.reserved[3] = 0;
a.DataOffset[0] = headerSize; a.DataOffset[1] = headerSize>>8; a.DataOffset[2] = headerSize>>16; a.DataOffset[3] = headerSize>>24;
b.Size[0]=40;b.Size[1]=0;b.Size[2]=0;b.Size[3]=0;
b.Width[0]=QR_width; b.Width[1]=QR_width >> 8; b.Width[2]=QR_width >> 16; b.Width[3]=QR_width >> 24;
b.Height[0]=QR_height; b.Height[1]=QR_height>>8; b.Height[2]=QR_height>>16; b.Height[3]=QR_height>>24;
b.Planes[0] = 1; b.Planes[1] = 0;
b.BitsPerPixel[0] = 1; b.BitsPerPixel[1] = 0;
b.Compression[0]=0; b.Compression[1]=0; b.Compression[2]=0; b.Compression[3]=0;
b.ImageSize[0]=0; b.ImageSize[1]=0; b.ImageSize[2]=0; b.ImageSize[3]=0;
b.XpixelsPerM[0]=0; b.XpixelsPerM[1]=0; b.XpixelsPerM[2]=0; b.XpixelsPerM[3]=0;
b.YpixelsPerM[0]=0; b.YpixelsPerM[1]=0; b.YpixelsPerM[2]=0; b.YpixelsPerM[3]=0;
b.ColorsUsed[0]=0; b.ColorsUsed[1]=0; b.ColorsUsed[2]=0; b.ColorsUsed[3]=0;
b.ImportantColors[0]=0; b.ImportantColors[1]=0; b.ImportantColors[2]=0; b.ImportantColors[3]=0;
int i=0;
char* img = calloc( (QR_width*QR_height)/8,sizeof(unsigned char));
for(i=0; i<32; i++)
img[i] = 0xff;
FILE *fptr = fopen("QRcode.bmp","wb");
if(fptr != NULL)
{
fwrite(&a,sizeof(struct bmpHeader),1,fptr);
fwrite(&b,sizeof(struct bmpInfoHeader),fptr);
fwrite(palette_data,sizeof(palette_data),fptr);
fwrite(img,32,fptr);
fclose(fptr);
}
free(img);
return 0;
}
预期结果是一个小的白色 bmp 图像,但我没有正确理解。这里出了什么问题?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。