C ++:.BMP以字节数组中的文件 [英] C++: .bmp to byte array in a file

查看:142
本文介绍了C ++:.BMP以字节数组中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我已经通过了与此相关的其他问题,但我发现他们没有太大的帮助。他们一定的帮助,但我还是有点困惑。所以在这里我需要做什么:

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个像素下面是白色那么这将是该阵列的第一个元素,该数字的十六进制,以使阵列看起来像这样:
数组[] = {} 0×01,然后将继续通过这些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.

我理解这种情况的报头格式,我已经阅读.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屋!

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