BMP图像的移动
将BMP图像向各个方位移动,下面我用纯C语言实现这个功能;
建模
将图像放在直角坐标系中,图像的左下角与坐标原点重合,以此为模型,(X,Y)为移动的方向以及移动的距离,例(+,+)向右上角移动,(-,+)向左上角移动,(-,-)向左下角移动,(+,-)向右下角移动;
代码实现
移动函数
int moveimage(unsigned char* pBmpBuf, unsigned char* imgout,int width, int height,int x,int y)
{
int i = 0;
int j = 0;
//右上
if (x > 0 && y > 0 && x < width && y < height)
{
for (i = 0; i < height-y; i++)
{
for (j = 0; j < width*3-x*3; j++)
{
imgout[y*width*3+x*3+i*width*3+j] = pBmpBuf[i * width * 3 + j];
}
}
}
//左上
if(x < 0 && y > 0 && x < width && y < height)
{
x = -x;
for (i = 0; i < height - y; i++)
{
for (j = 0; j < width * 3 - x * 3; j++)
{
imgout[y * width * 3 + i * width * 3 + j] = pBmpBuf[x*3+ i * width * 3 + j];
}
}
}
//左下
if (x < 0 && y < 0 && x < width && y < height)
{
x = -x;
y = -y;
for (i = 0; i < height - y; i++)
{
for (j = 0; j < width * 3 - x * 3; j++)
{
imgout[i * width * 3 + j] = pBmpBuf[y*width*3+x*3+ i * width * 3 + j];
}
}
}
//右下
if (x > 0 && y < 0 && x < width && y < height)
{
y = -y;
for (i = 0; i < height - y; i++)
{
for (j = 0; j < width * 3 - x * 3; j++)
{
imgout[x*3+ i * width * 3 + j] = pBmpBuf[y * width * 3 + i * width * 3 + j];
}
}
}
return 0;
}
main()函数
int main()
{
long int i = 0;
int width, height, bitCount = 0;
unsigned char* pBmpBuf = (unsigned char*)malloc(1000 * 1000 * 3);
if (pBmpBuf == NULL)
{
return -1;
}
else
{
for (i = 0; i < 768 * 768 * 3; i++)
{
pBmpBuf[i] = 0;
}
}
const char* path = "D:\\test\\read_bmp_image\\1-B.bmp";
read_bmp(path, pBmpBuf, &width, &height, &bitCount);
printf("width:%d height:%d bitCount:%d\n", width, height, bitCount);
unsigned char* imgout= NULL;
//imgout= (unsigned char*)malloc(sizeof(unsigned char) * width * height * 3);
imgout = new unsigned char[(sizeof(BYTE) * width * height * 3)];
for (i = 0; i < width * height * 3; i++)
{
imgout[i] = 0;
}
int x=0, y = 0;
scanf_s("%d %d", &x, &y);
moveimage(pBmpBuf, imgout,width, height,x,y);
write_bmp(imgout, &width, &height, &bitCount);
delete []imgout;
}
write_bmp()、read_bmp()在我之前的文档中写过,就不在这里赘述,可以自行查阅https://blog.csdn.net/weixin_47364956/article/details/115420772;
效果展示
原图
(X,Y)=(100,100),效果如下:
(X,Y)=(-100,100),效果如下:
(X,Y)=(-100,-100),效果如下:
(X,Y)=(100,-100),效果如下:
感兴趣的同学,可以自己动手调试下!