C ++:.bmp到文件中的字节数组 [英] C++: .bmp to byte array in a file
问题描述
是的,我已经通过与这个有关的其他问题,但我发现他们没有太多的帮助。他们是一些帮助,但我仍然有点困惑。所以这里我需要做什么:
Yes i have been through the other questions that are related to this, but i found them not much help. They were some help but i am still a bit confused. So here what what i need to do:
我们有一个132x65的屏幕。我有一个132x65 .bmp。我想通过.bmp,并将其分成小的1x8列,以获得该32位列的二进制。然后做那132遍,做那9次下来。任何不是白色的东西都应该算作一点。示例:
We have a 132x65 screen. I have a 132x65 .bmp. I want to go through the .bmp and separate it into little 1x8 columns to get the binary of that 32-bit column. Then do that 132 times across, and do that 9 times down. Anything that is not white should be counted as a bit. example:
如果图片的左上角的像素是不是白色的任何颜色,并且下面的7个像素是白色,那么这将是数组的第一个元素,该数字的十六进制,所以数组看起来像这样:
array [] = {0x01},然后它将继续填充这132列,然后再次为9部分的行。并且文件结果将只在一个单独的文件中的数组。
If the top left pixel of the picture is any color that is not white and the 7 pixels below that are white then that would be the first element of the array, the hex of that number, so the array would look like this: array [] = { 0x01 } and then it would continue to fill through those 132 columns and then do it again for 9 "sections" of rows. And the file result would be ONLY that array in a separate file.
我理解头文件格式,我已经阅读wiki文章.bmp文件格式,我主要问题是我真的不知道如何与.bmp交互,当我真的想要它进入和与图像中的每个像素进行交互。我真的不需要整个事情,但也许只是一个例子,抓住每个像素从.bmp和输出的像素的颜色到一个文件或东西。我的c ++有点生锈(最近一直做java和javscript)。
I understand the header format for this, i have read the wiki article on .bmp file formats, my main problem is i don't really know how to interact with the .bmp when i actually want it to go inside and interact with each pixel from the image. I really dont need the whole thing, but maybe just an example of grabbing each pixel from the .bmp and outputting the color of the pixel into a file or something. My c++ is a little rusty (been doing java and javscript lately).
推荐答案
如果你想读一个已知的格式BMP和不关心它是如何做的(即,内部唯一的东西),你可以只是采取BMP,忽略标题,并将其用作像素阵列。它从左下方开始逐行存储。有一些细节,如果它包装,但在我的经验,如果你采取一个32bpp图像,它可以完全忽略。
If you want to read a known format BMP and don't care about how it's done (ie, internal-only thing) you can just take the BMP, ignore the header and use it as a pixel array. It is stored line by line starting at the bottom left. There are some detail snags for how it's packed but in my experience if you take a 32bpp image it can be completely ignored.
作为一个很简单的例子:
As a really simple example:
unsigned int *buffer;
void readfile() {
FILE *f = fopen("file.bmp", "rb");
buffer = new unsigned int[132*65];
fseek(f, 54);
fread(buffer, 132*65*4, 1, f);
fclose(f);
}
unsigned int getpixel(int x, int y) {
//assuming your x/y starts from top left, like I usually do
return buffer[(64 - y) * 132 + x];
}
这篇关于C ++:.bmp到文件中的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!