2014-06-26 105 views
-2

我有一个bmp文件。我的目标是将文件缩小到文件总大小的70%。我已将bmp标题和像素数组读入src_buffer。现在我有一个功能,可以摄取像素阵列和src_buffer,位图宽度(以像素为单位)src_width,位图高度(以像素为单位)src_height和缩小系数r作为输入。这个函数在缩放至dest_buffer之后将其输出写入,位图的宽度(像素)为dest_width,位图的高度为dest_height。 现在我想把它写回bmp格式。这是我面临困难的地方。要回写,我发现难以填充structBITMAPINFOHEADERBITMAPFILEHEADER的数据成员。我应该考虑哪些其他事情。我已阅读关于像素阵列行之间的填充。我该如何实现它。我在C编码。我不想在这里编码的帮助。我想帮助确切的算法来编写一个BMP文件。按内存大小缩放bmp文件

+2

_现在我有一个函数,可以摄取像素数组和src_buffer,位图宽度(以像素为单位)src_width,位图高度(以像素为单位)src_height和缩小系数ras input_ ***显示它和其他相关代码。***如果你提供你的代码,那么更有可能有人可以提供帮助。 – ryyker

+0

该函数看起来像'bool image_resize(WORD * src_buffer,unsigned int src_width,unsigned int src_height,WORD * dest_buffer,int * dest_width,int * dest_height,float r)'。 – tinutomson

+0

'BITMAPFILEHEADER'和'BITMAPINFOHEADER'的哪些成员感到困惑?如果你所做的只是缩小图像的大小,那么你应该可以将原始数据用于几乎所有这些结构的字段,除了那些描述图像大小的字段。 –

回答

0

我想出了如果你不得不缩小一个bmp文件究竟该做什么。我只会谈论24位bmp文件。 24位bmp文件由两个标头组成,后面跟着图像像素数组。 BMP文件中的第一个标题被称为BITMAPFILEHEADER,总大小为14个字节。它看起来有点像这样。

typedef struct 
{ 
    uint16_t bfType; //2 bytes storing "BM". This means that it is bmp 
    uint32_t bfSize; //4 bytes total file size of bmp. Including both the header and pixel array 
    uint16_t bfReserved1; //Always 0 
    uint16_t bfReserved2; //Always 0 
    uint32_t bfOffBits; // stores 54. (40 +14)Total size of the headers. 
} __attribute__((__packed__)) 
BITMAPFILEHEADER; 

接下来是struct BITMAPINFOHEADER,总大小为40字节。以下是它的声明

typedef struct 
{ 
    uint32_t biSize; //40 bytes. Size of the header. 
    int32_t biWidth; //Width of the bitmap image. How many pixel wide is the image. 
    int32_t biHeight; //Height of the bitmap image. 
    uint16_t biPlanes; // Always 1. 
    uint16_t biBitCount; //24 for 24 bit bitmap. Number of bit representing each pixel. 
    uint32_t biCompression; 
    uint32_t biSizeImage; //Total size of bitmap pixel array including padding. 
    int32_t biXPelsPerMeter; 
    int32_t biYPelsPerMeter; 
    uint32_t biClrUsed; 
    uint32_t biClrImportant; 
} __attribute__((__packed__)) 
BITMAPINFOHEADER; 

现在,这2个结构紧接着是像素数组。单个像素由3个字节表示。说它代表的是

typedef struct 
{ 
    uint8_t rgbtBlue; 
    uint8_t rgbtGreen; 
    BYTE rgbtRed; 
} __attribute__((__packed__)) 
RGBTRIPLE; 

我们有一个以线性方式放入的RGBTRIPLE数组。准确地说,像素数由以下公式给出。

BITMAPINFOHEADER.biWidth * BITMAPINFOHEADER.biHeight * sizeof(RGBTRIPLE) 

通过任何单列因此将是所需的内存:

BITMAPINFOHEADER.biWidth * sizeof(RGBTRIPLE) 

这里是

BITMAPINFOHEADER.biWidth * BITMAPINFOHEADER.biHeight 

的存储任何图像信息所需的存储器的量由下式给出catch,如果任何单个行的总内存不是4个字节的倍数,则将填充添加到该行中,以使其为4的倍数。说图像大小为41X30。存储一行所需的字节数为41*3 = 123bytes。因此,一个1字节的填充将以bmp格式添加到此行。因此BITMAPINFOHEADER.biSizeImage = 30 * 124 = 3720 bytes 将bmp解码为原始像素数组时,应始终记住删除这些填充。 同样,当回写一个bmp文件(编码)时,确保你添加了这些额外的填充。 还要注意的是,

BITMAPINFOHEADER.biSizeImage = (BITMAPINFOHEADER.biWidth * sizeof(RGBTRIPLE) +padding) * BITMAPINFOHEADER.biHeight 

或者也可以用一个公式来计算:

BITMAPINFOHEADER.biSizeImage = (((BITMAPINFOHEADER.biWidth * bmpiheader.biBitCount) + 31)/32) * 4 * BITMAPINFOHEADER.biHeight 

请注意,BITMAPINFOHEADER.biBitCount = sizeof(RGBTRIPLE) * 8 = 24 bits 24位位图。