指向纹理数据的指针-垂直翻转数据
pointer to texture data - flip data vertically
我使用一个库来加载纹理,它返回一个指向数据、图像宽度和高度的指针。我想垂直翻转纹理数据,所以我创建了以下功能:
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]);
}
}
}
将内部循环分解为标准算法,并使用简化的指针算法:
#include <cstdint>
#include <algorithm>
void FlipYTexture(unsigned width, unsigned height, uint8_t* data)
{
auto row_offset = [&](unsigned row)
{
return row * height;
};
// pointer arithmetic once up front
auto first_row = data + row_offset(0);
auto last_row = data + row_offset(height / 2);
auto other_row = data + row_offset(height);
// swap each row
for ( ; first_row != last_row ; first_row += width )
{
other_row -= width;
std::swap_ranges(first_row, first_row + width, other_row);
}
}
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 指向纹理数据的指针-垂直翻转数据
- 如何翻转像素数据的Y轴
- stb_image在加载 opengl 纹理的图像时不会垂直翻转
- C++中的字节翻转数据只返回零
- 我应该垂直翻转加载了stb_image的图像的线条以在OpenGL中使用吗
- 2D 阵列垂直翻转
- 垂直翻转图像
- glm::lookAt 垂直相机翻转时 z <= 0
- QImage垂直翻转
- 如何在YUV到RGB转换期间垂直翻转缓冲区
- 使用GL_ARRAY_BUFFER更新垂直数据
- QTableView垂直调整未刷新的数据大小