gpt4 book ai didi

c++ - 指向纹理数据的指针 - 垂直翻转数据

转载 作者:行者123 更新时间:2023-11-28 04:04:55 26 4
gpt4 key购买 nike

我使用一个库来加载纹理,它返回一个指向数据、图像宽度和高度的指针。我想垂直翻转纹理数据,所以我创建了以下函数:

void FlipYTexture(const unsigned int width, const unsigned int height, uint8_t* data)
{
const unsigned int rowsSwapCount = height / 2;
const unsigned int maxRowIndex = height - 1;

for (int i = 0; i < rowsSwapCount; ++i)
{
for (int j = 0; j < width; ++j)
{
const unsigned int currentDataIndex = width * i + j;
const unsigned int swapDataIndex = width * (maxRowIndex - i) + j;

uint8_t temp = data[currentDataIndex];
data[currentDataIndex] = data[swapDataIndex];
data[swapDataIndex] = temp;
}
}
}

有什么方法可以优化该功能和/或更简单地实现它吗?

最佳答案

如果您可以将数据参数从指向数组引用的指针更改,那么这将是一个更简单的选项,尽管我不知道如果不使用典型输入数据对其进行测试它会更快。

#include <algorithm>

template<typename T, size_t N>
void FlipYTexture(const unsigned int width, const unsigned int height, T(&data)[N]) {
std::reverse(std::begin(data), std::end(data));
}

如果您必须将数据参数保留为指针,那么您至少可以使用 std::swap:

#include <utility>

void FlipYTexture(const unsigned int width, const unsigned int height, uint8_t* data) {
const unsigned int rowsSwapCount = height / 2;
const unsigned int maxRowIndex = height - 1;

for (unsigned int i = 0; i < rowsSwapCount; ++i) {
for (unsigned int j = 0; j < width; ++j) {
const unsigned int currentDataIndex = width * i + j;
const unsigned int swapDataIndex = width * (maxRowIndex - i) + j;
std::swap(data[currentDataIndex], data[swapDataIndex]);
}
}
}

关于c++ - 指向纹理数据的指针 - 垂直翻转数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58905321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com