#include <iostream>
#include <vector>
using namespace std;
// 代表图像中的像素,这里假设每个像素有三个通道(RGB)
struct Pixel {
unsigned char r, g, b;
};
// 图像平移函数
void translateImage(std::vector<Pixel>& srcPixels, std::vector<Pixel>& dstPixels, int width, int height, int dx, int dy) {
dstPixels.resize(srcPixels.size()); // 确保目标像素数组大小与源像素数组相同
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int srcIndex = y * width + x;
int dstX = x + dx;
int dstY = y + dy;
if (dstX >= 0 && dstX < width && dstY >= 0 && dstY < height) {
int dstIndex = dstY * width + dstX;
dstPixels[dstIndex] = srcPixels[srcIndex]; // 对图像的每个像素进行平移
}
}
}
}
int main() {
int width = 800;
int height = 600;
int dx = 50; // x轴平移距离
int dy = 50; // y轴平移距离
// 创建源图像像素数组,这里用一个示例颜色填充
std::vector<Pixel> srcPixels(width * height, {255, 0, 0}); // 红色
std::vector<Pixel> translatedPixels;
translateImage(srcPixels, translatedPixels, width, height, dx, dy); // 调用图像平移函数
// 在这里,你可以将translatedPixels转换回图像或进行其他处理
return 0;
}