您没有很好地解释您的目标,但我认为您希望“环绕”以每行为基础。因此,每行左侧的 n 个像素应该最终流到该行的右侧。
只是在整个图像周围移动字节,您最终会在 previous 行中得到这些字节,因为这些字节是一维序列,并且您没有构建任何逻辑来注意图像的名义“宽度”。
如果你想要二维逻辑,你需要编写二维代码。
这是一个完整的例子:
#include <iostream>
int main()
{
// Some constants
const size_t width = 6;
const size_t height = 4;
const size_t bitmapBytes = width*height;
// Actual data
char oldBitmapBytes[bitmapBytes] =
{
'1', '2', '3', '4', '5', '6',
'a', 'b', 'c', 'd', 'e', 'f',
'.', ',', '/', '~', '#', '$',
'!', '-', '!', '+', '!', '?'
};
char newBitmapBytes[bitmapBytes];
// Variables
const size_t x_offset = 2;
// Some utilities
auto convertCoordsToIndex = [&](const size_t x, const size_t y)
{
return y*width + x;
};
auto printBitmap = [&](const char* bitmapBytes)
{
for (size_t row = 0; row < height; row++)
{
for (size_t col = 0; col < width; col++)
std::cout << bitmapBytes[convertCoordsToIndex(col, row)];
std::cout << '\n';
}
};
// Display original bitmap
printBitmap(oldBitmapBytes);
// Shift by x_offset
for (size_t row = 0; row < height; row++)
{
for (size_t col = 0; col < width; col++)
{
const size_t adjustedCol = (col + x_offset) % width;
const size_t oldIndex = convertCoordsToIndex(col, row);
const size_t newIndex = convertCoordsToIndex(adjustedCol, row);
newBitmapBytes[newIndex] = oldBitmapBytes[oldIndex];
}
}
// Display shifted bitmap
std::cout << '\n';
printBitmap(newBitmapBytes);
}
// Output:
// 123456
// abcdef
// .,/~#$
// !-!+!?
//
// 561234
// efabcd
// #$.,/~
// !?!-!+
有趣的位在底部附近,我以行感知和列感知的方式循环,将字节从旧位图复制到新位图,但使用 addition (+)和 模 (%) 来抵消我在每种情况下复制的列。
最终,这只是数学!
我们也可以就地执行此操作(即不需要第二个数组),doing it with std::rotate 可能会使事情稍微简单一些。
请注意,这通常不适用于实际的位图,因为许多格式(包括 BMP)不仅有标题,还有行填充。你必须为这些东西增加一些津贴。