C语言实现BMP图像的移动

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),效果如下:
在这里插入图片描述
感兴趣的同学,可以自己动手调试下!

  • 8
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值