windows C++ 读取和编辑位图图像的像素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2750988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:23:38  来源:igfitidea点击:

C++ Reading and Editing pixels of a bitmap image

c++windowsimage

提问by BettyD

I'm trying to create a program which reads an image in (starting with bitmaps because they're the easiest), searches through the image for a particular parameter (i.e. one pixel of (255, 0, 0) followed by one of (0, 0, 255)) and changes the pixels in some way (only within the program, not saving to the original file.) and then displays it.

我正在尝试创建一个程序来读取图像(从位图开始,因为它们是最简单的),在图像中搜索特定参数(即 (255, 0, 0) 的一个像素,然后是 ( 0, 0, 255)) 并以某种方式更改像素(仅在程序内,不保存到原始文件。)然后显示它。

I'm using Windows, simple GDI for preference and I want to avoid custom libraries just because I want to really understand what I'm doing for this test program.

我正在使用 Windows,偏好使用简单的 GDI,我想避免使用自定义库,只是因为我想真正了解我正在为这个测试程序做什么。

Can anyone help? In any way? A website? Advice? Example code?

任何人都可以帮忙吗?以任何方式?一个网站?建议?示例代码?

Thanks

谢谢

回答by Taco de Wolff

But if by "bitmap" you really meant a bitmap (that is, a raw image file with nothing but uncompressed pixel data), it's rather easy:

但是,如果您所说的“位图”实际上是指位图(即只有未压缩像素数据的原始图像文件),则很容易:

char *filename = "filename";
FILE *input = fopen(filename, "rb+");
if (!input)
{
    printf("Could not open input file '%s'\n", filename);
    system("PAUSE");
    return 1;
}

fseek(input, 0, SEEK_END);
size_t size = ftell(input);
rewind(input);

char *buffer = (char *) malloc(size);
if (!buffer)
{
    printf("Out of memory, could not allocate %u bytes\n", size);
    system("PAUSE");
    return 1;
}

if (fread(buffer, 1, size, input) != size)
{
    printf("Could not read from input file '%s'\n", filename);
    free(buffer);
    fclose(input);
    system("PAUSE");
    return 1;
}
fclose(input);

// we loaded the file from disk into memory now

for (size_t i = 0; (i + 3) < size; i += 4)
{
    if (buffer[i + 0] == 0xFF && buffer[i + 1] == 0x00 && buffer[i + 2] == 0x00 && buffer[i + 3] == 0x00)
    {
        printf("Pixel %i is red\n", (i / 4));
    }
}

free(buffer);

Sorry, this is C style, but you can easily change the allocation to C++ style. Personally I like the C way of reading files though. Typically you will want the if statement to check if the pixel is red to be a macro / function. This will make the if statement like "if (compare_4bytes(buffer, 0xFF000000))".

抱歉,这是 C 风格,但您可以轻松地将分配更改为 C++ 风格。我个人喜欢 C 方式读取文件。通常,您会希望 if 语句检查像素是否为红色作为宏/函数。这将使 if 语句类似于“if (compare_4bytes(buffer, 0xFF000000))”。

Also, this assumes the file loaded consists entirely of raw pixel data. If this is not the case, ie. you read a .bmp / .png / .jpg or something else, you must first load and then convert the file in memory to raw pixel data. This would be done at the comment saying "we loaded the file from disk into memory now".

此外,这假设加载的文件完全由原始像素数据组成。如果不是这种情况,即。您读取 .bmp / .png / .jpg 或其他文件时,您必须先加载然后将内存中的文件转换为原始像素数据。这将在“我们现在将文件从磁盘加载到内存中”的评论中完成。

BMP is certainly doable (it's almost raw pixel data already), but most other types need A LOT of work / research. They use often complex compression algorithms. I suggest you'd use a library for this, just for converting it to raw pixel data.

BMP当然是可行的(它已经几乎是原始像素数据),但大多数其他类型需要大量的工作/研究。他们通常使用复杂的压缩算法。我建议您为此使用一个库,仅用于将其转换为原始像素数据。

Corona is a good (small) one (http://corona.sourceforge.net/), or DevIL (http://openil.sourceforge.net/) or thousand others :)

Corona 是一个很好的(小)(http://corona.sourceforge.net/),或 DevIL(http://openil.sourceforge.net/)或其他数千个 :)

回答by msw

If by "bitmap" you mean BMP, Wikipedia has a pretty comprehensive descriptionof the bits and bytes

如果“位图”是指 BMP,维基百科对位和字节有非常全面的描述