在C ++中将像素阵列旋转90度

Per*_*abs 2 c++ rotation pixels

我已经编写了下一个函数来旋转一个无符号字符像素数组,该数组将RGB图像保持90度。我面临的问题是旋转的输出全部乱码。

void rotate90(unsigned char *buffer, const unsigned int width, const unsigned int height)
{
    const unsigned int sizeBuffer = width * height * 3; 
    unsigned char *tempBuffer = new unsigned char[sizeBuffer];

    for (int y = 0, destinationColumn = height - 1; y < height; ++y, --destinationColumn)
    {
        int offset = y * width;

        for (int x = 0; x < width; x++)
        {
            tempBuffer[(x * height) + destinationColumn] = buffer[offset + x];
        }
    }

    // Copy rotated pixels

    memcpy(buffer, tempBuffer, sizeBuffer);
    delete[] tempBuffer;
}
Run Code Online (Sandbox Code Playgroud)

nev*_*boy 5

将最里面的循环中的行替换为:

for (int i = 0; i < 3; i++)
    tempBuffer[(x * height + destinationColumn) * 3 + i] = buffer[(offset + x) * 3 + i];
Run Code Online (Sandbox Code Playgroud)