彩色位图文件为黑白

时间:2013-05-23 18:17:56

标签: c++ c bitmap

我需要将彩色位图文件转换为黑白色。

据我所知,当我读取一行像素时,需要除以4.第一个问题,为什么? :)

如果它没有除以4,我需要添加零,直到它为止。

我遇到麻烦的主要问题是阅读那些零。谁能告诉我如何读取这些零?

此外,如果有任何指南,我很乐意看到它。

谢谢!

2 个答案:

答案 0 :(得分:0)

除法用于在特定边界上对齐图像的每一行,在本例中为32位。您可以使用模数学确定每行末尾的额外字节数。

int zero_padding_count = image->actual_width_in_bytes % 4;

这将产生0到3之间的值。要处理,您可以执行类似的操作。

char *source = image->buffer;
char *dest = some_buffer;

for(int row = 0; row < image->actual_height; row++)
{
    for(int column = 0; column < image->actual_width_in_bytes; column++)
    {
        // do your conversion here
        *source++ = dest++;
    }
    // Now adjust the source pointer for the number of padding bytes at the end
    // of the line
    source += zero_padding_count;
}

答案 1 :(得分:0)

如果您只想将彩色图像转换为黑白而不执行低级操作,您可能需要查看ImageMagick(http://www.imagemagick.org/script/command-line-options.php#grayscale)。它有很多有用的实用程序。

相关问题