C \ C ++中的简单翻转缓冲区(垂直)问题

时间:2013-02-10 14:11:31

标签: c++ c pixels memcpy flip

我正在尝试翻转缓冲区,但缓冲区未完全处理。 是一个像素的缓冲区,我基本上需要垂直翻转它。 谁能发现我做错了什么?提前谢谢。

void flipVertically(unsigned int* buffer, const unsigned int width, const unsigned int height)
{
    const unsigned int rowWidth = width; // Length of a row
    const unsigned int rows = height / 2; // Iterate only half the buffer to get a full flip
    unsigned int* tempRow = (unsigned int*)malloc(rowWidth);

    for (int rowIndex = 0; rowIndex < rows; rowIndex++)
    {
        memcpy(tempRow, buffer + (rowIndex * rowWidth), rowWidth);
        memcpy(buffer + (rowIndex * rowWidth), buffer + (height - rowIndex - 1) * rowWidth, rowWidth);
        memcpy(buffer + (height - rowIndex - 1) * rowWidth, tempRow, rowWidth);
    }

    free(tempRow);
}

1 个答案:

答案 0 :(得分:7)

这会有用吗?

void flip(unsigned* buffer, unsigned width, unsigned height)
{
    unsigned rows = height / 2; // Iterate only half the buffer to get a full flip
    unsigned* tempRow = (unsigned*)malloc(width * sizeof(unsigned));

    for (unsigned rowIndex = 0; rowIndex < rows; rowIndex++)
    {
        memcpy(tempRow, buffer + rowIndex * width, width * sizeof(unsigned));
        memcpy(buffer + rowIndex * width, buffer + (height - rowIndex - 1) * width, width * sizeof(unsigned));
        memcpy(buffer + (height - rowIndex - 1) * width, tempRow, width * sizeof(unsigned));
    }

    free(tempRow);
}