【问题标题】:Simple Flip buffer (Vertically) issue in C\C++C\C++ 中的简单翻转缓冲区(垂直)问题
【发布时间】:2013-01-25 18:08:08
【问题描述】:

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

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);
}

【问题讨论】:

  • 你能澄清flip的意思吗?你的意思是反向吗?你的意思是转置?这是 C 还是 C++?如果是C++,为什么要使用指针?
  • bufferPixels 是什么?
  • 到底出了什么问题?
  • 注意:您将rowWidth 传递给memcpy,而您可能需要rowWidth * sizeof(*buffer)
  • @AlexChamberlain 我认为这是一个在内存中按顺序存储的二维数组,需要垂直翻转(row[0] row[w-1], row[1] row [w-2] 等)。

标签: c++ c pixels memcpy flip


【解决方案1】:

这行得通吗?

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);
}

【讨论】:

    猜你喜欢
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多