关闭

分享维基的:图片文件数据结构,非常详细。

1477人阅读 评论(0) 收藏 举报

源于我在天地会发问的贴子:http://bbs.9ria.com/viewthread.php?tid=118085

部分自己理解了理出来让大家吸收吸收:


在google一下:

bmp file format
jpg file format
png file format


以下分享的就是bmp的文件数据结构:其它格式自己再去强大的维基百科找吧。

英文链接:

http://en.wikipedia.org/wiki/BMP_file_format


中文链接:

http://zh.wikipedia.org/wiki/BMP


不得不说:维基里的文档,每每都是极强的精品!非常有用。

当然我想说的是,这些精品博文都是英文版的源汁源味。

其它语言版本的博文都相对英文版的信息删减了很多,就上面我给出的两个链接就可以看出来。

所以还是有空多学学英文啊。


计算文件大小:

n位2n种颜色的包含调色板的位图近似字节数可以用下面的公式计算:

BMP文件大小 \approx 54+4 \cdot 2^n+\frac{{\rm width} \cdot {\rm height} \cdot n}{8},其中高度(height)和宽度(width)都以像素为单位。

需要注意的是上面公式中的54是位图文件的文件头4 \cdot 2^n是彩色调色板的大小。 如果位图文件不包含调色板,如24位,32位位图,则位图的近似字节数可以用下面的公式计算:

BMP文件大小 \approx 54+\frac{{\rm width} \cdot {\rm height} \cdot n}{8},其中高度(height)和宽度(width)都以像素为单位。

另外需要注意的是这是一个近似值,对于n位的位图图像来说,尽管可能有最多2^n中颜色,一个特定的图像可能并不会使用这些所有的颜色。由于彩色调色板仅仅定义了图像所用的颜色,所以实际的彩色调色板将小于4 \cdot 2^n

如果想知道这些值是如何得到的,请参考下面文件格式的部分。

由于存储算法本身决定的因素,根据几个图像参数的不同计算出的大小与实际的文件大小将会有一些细小的差别。


Bitmap数据结构示例:以下示例:为2*2大小(宽:2,高:2)的24位深(位深:每个点(像素)使用多少字节存:1个字节即8位,24位即:24/8=3个字节)bmp位图文件信息。

Bitmap Data Example

I've been trying to add an example of the bitmap data, but it seems my efforts are not appreciated. What exactly is wrong with what I added? It is meant to be the pixel data after the 54 byte in the bitmap file. Dicklyon, I'd appreciate a more constructive comment than "this is wrong". While the text in the paragraph is somewhat clear, it doesn't go into detail about how the bits are stored in the file (i.e. what comes first blue, red, or green). I thought this example would be useful for someone who is writing a program that reads a bit map, which is what I've been doing.

Example of a 2x2 Pixel Bitmap, with 24 bits/pixel encoding
Bitmap Data Example using a 2x2 Pixel, 24-Bit Bitmap
Offset Size Hex Value Value Description
0 2 42 4D "BM" Magic Number (unsigned integer 66, 77)
2 4 46 00 00 00 70 Bytes Size of Bitmap
6 2 00 00 Unused Application Specific
8 2 00 00 Unused Application Specific
10 4 36 00 00 00 54 bytes The offset where the bitmap data (pixels) can be found.
14 4 28 00 00 00 40 bytes The number of bytes in the header (from this point).
18 4 02 00 00 00 2 pixels The width of the bitmap in pixels
22 4 02 00 00 00 2 pixels The height of the bitmap in pixels
26 2 01 00 1 plane Number of color planes being used.
28 2 18 00 00 00 24 bits The number of bits/pixel.
30 4 00 00 00 00 0 BI_RGB, No compression used
34 4 16 00 00 00 16 bytes The size of the raw BMP data (after this header)
38 4 13 0B 00 00 2,835 pixels/meter The horizontal resolution of the image
42 4 13 0B 00 00 2,835 pixels/meter The vertical resolution of the image
46 4 00 00 00 00 0 colors Number of colors in the pallet
50 4 00 00 00 00 0 important colors Means all colors are important
Start of Bitmap Data
54 3 00 00 FF 16,711,680 Red, Pixel (0,1)
57 3 FF FF FF 16,777,215 White, Pixel (1,1)
60 2 00 00 0 Padding for 8 bytes/row (Could be a value other than zero)
62 3 FF 00 00 255 Blue, Pixel (0,0)
65 3 00 FF 00 65,280 Green, Pixel (1,0)
68 2 00 00 0 Padding for 8 bytes/row (Could be a value other than zero)


附上一些微软目前使用的5个版本的bmp位图数据结构:

In addition to V5 Header, those are the current possible Microsoft header configurations with bit field masks:

All Windows Header configurations


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    文章分类
    最新评论