我如何读取BMP像素值到数组? [英] How can I read BMP pixel values into an array?

查看:534
本文介绍了我如何读取BMP像素值到数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++(在Windows上)编写代码,我试图提取灰度bmp的像素值。我不在乎保留任何元数据,只想将像素值存储在字符数组中。我没有能够找到一个标准或典型的方式这样做手动,所以我想知道是否有一个简单的库,人们用来加载位图到内存。

I'm writing code in C++ (on Windows) and I'm trying to extract the pixel values of a grayscale bmp. I don't care about keeping any of the metadata, and just want to store the pixel values in a char array. I haven't been able to find a standard or "typical" way of doing this manually, so I'm wondering if there's perhaps a simple library that people use to load bitmaps into memory.

提前感谢!

推荐答案

将整个文件读入内存。

Read the entire file into memory. There will be a small header at the front, and the rest of it will be the pixel values.

第一部分将是一个 BITMAPFILEHEADER 结构。你关心的唯一部分是bfOffBits,它给出从文件开始到像素值的字节数。

The first part will be a BITMAPFILEHEADER structure. The only part you care about is the bfOffBits, which gives the number of bytes from the start of the file to the pixel values.

<$ c后面的部分$ c> BITMAPFILEHEADER 将是 BITMAPINFOHEADER 。这将有助于确定像素的格式。

The next part after the BITMAPFILEHEADER will be a BITMAPINFOHEADER. This will be useful to determine the format of the pixels.

这将是一个调色板,如果位深度需要一个。

This will be followed by a palette, if the bit depth requires one.

有几个问题与像素值。首先是顺序是(蓝色,绿色,红色),正好相反的方式每个人都这样做。第二是行从图像的底部到顶部,再次从其他人向后。最后,行中的字节数将始终填充到下一个4的倍数。

There are a couple of gotchas with the pixel values. First is that the order is (blue,green,red), just opposite of the way everybody else does it. Second is that the rows go from bottom to top of the image, again backwards from everybody else. Finally, the number of bytes in a row will always be padded up to the next multiple of 4.

我几乎忘了提及,可能是JPEG或PNG文件被编码为BMP,但这不常见。看看 BITMAPINFOHEADER 的biCompression字段,如果它只是BI_RGB,你需要更多的帮助。

I almost forgot to mention, it is possible for a JPEG or PNG file to be encoded as a BMP, but this is not common. Have a look at the biCompression field of the BITMAPINFOHEADER, if it's anything but BI_RGB you'll need a little more help.

这篇关于我如何读取BMP像素值到数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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