【发布时间】:2017-05-09 01:02:21
【问题描述】:
我需要使用.bmp 类型的图像。
它的格式是:
struct bmp_fileheader
{
unsigned char fileMarker1; /* 'B' */
unsigned char fileMarker2; /* 'M' */
unsigned int bfSize; /* File's size */
unsigned short unused1; /* Aplication specific */
unsigned short unused2; /* Aplication specific */
unsigned int imageDataOffset; /* Offset to the start of image data */
};
struct bmp_infoheader
{
unsigned int biSize; /* Size of the info header - 40 bytes */
signed int width; /* Width of the image */
signed int height; /* Height of the image */
unsigned short planes;
unsigned short bitPix; /* Number of bits per pixel = 3 * 8 (for each channel R, G, B we need 8 bits */
unsigned int biCompression; /* Type of compression */
unsigned int biSizeImage; /* Size of the image data */
int biXPelsPerMeter;
int biYPelsPerMeter;
unsigned int biClrUsed;
unsigned int biClrImportant;
};
typedef struct pi {
unsigned char r;
unsigned char g;
unsigned char b;
}Pixel;
struct bmp_image {
struct bmp_fileheader file_header;
struct bmp_infoheader info_header;
Pixel ** pixel;
};
struct bmp_image image;
因此,图像包含标题和像素矩阵(height * width)。
我从文件中读取图像:
FILE *image_file = fopen("path.bmp", "rb");
之后,我读取了标题的所有变量,然后读取了像素矩阵。我需要对图像进行一些更改,以便从初始图像创建另一个黑白格式的图像。
这样做的算法是用(B,B,B)改变(X,Y,Z)像素,其中B = (X + Y + Z) / 3;。我创建了黑白图像就好了。
当我尝试将我的黑白图像与绘图程序制作的黑白图像进行比较时,问题就出现了。
cmp -lb airplane_black_white.bmp ref/airplane_black_white.bmp
cmp: EOF on airplane_black_white.bmp
【问题讨论】:
-
第一步是创建一个程序,从两个文件中读取头信息,并打印出所有头字段。然后看看有什么不同。
-
我认为像素之后还有其他东西。我不确定...
-
此代码跳过了 .bmp 图像的一些重要细节。 1) 像素的宽度可以是 1 字节(黑白图像)到 4 字节(其中透明度是第 4 字节)的任意值 2) 每行必须是 4 字节的倍数,因此宽度需要向上取整,比如
int numBytesPerRow = (((bitpix>>3)*width)+3) & 0xFFFFFFFC -
per: paulbourke.net/dataformats/bitmaps> 格式:BMP/DIB 平台:主要是 DOS-Windows 所有者:MicroSoft 备注:Microsoft Windows 格式,其他地方很少支持。支持 1、2、4、8 和 32 位彩色图像。
-
per: dragonwins.com/domains/getteched/bmp/bmpfileformat.htm> 像素数据逐像素颜色信息逐行,从下到上。行从双字(4 字节)边界开始,必要时填充空。每行是从左到右逐列的。在 24 位图像中,颜色顺序为红、绿、蓝。在小于 8 位的图像中,高位是最左边的像素。