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

标签 c++ c pixel 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);
}

最佳答案

这行得通吗?

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

关于c++ - C\C++ 中的简单翻转缓冲区(垂直)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14798604/

相关文章:

c++ - 在 C++ 中为 Fortran 程序员传递多维数组

c++ - 在同一台机器上运行客户端和服务器

c - 如何在 C 中扫描特定的字符串格式?

javascript - 当div距屏幕底部20%时显示div

c++ - 如何方便地打印出 std::stack 或 std::queue 中的所有元素

c++ - 如何创建具有拖放功能和单击信号功能的标签QT

c - 非零 TCP 窗口比例选项的原因

c - 取一些数字,看看它是不是质数?

Java在二维数组中绘制像素圆

android - 有没有办法访问视频的像素?