将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),效果如下:
感兴趣的同学,可以自己动手调试下!
XML/HTML Code 复制内容到剪贴板 input id = username name = username type = t...
本文介绍了JSP编程技术实现一个简单的购物车程序,具体如下: 1 问题描述 利用JS...
由于工作所需,最近花时间研究了html转换为pdf的功能。html转换为pdf的关键技术...
第二题 2021年4月4日 腾讯笔试编程题第二题 描述 给出一个有0-9的数字组成的字符...
Coonamd 对象定义了将对数据源执行的命令,可以用于查询数据库表并返回一个记录...
常见信号介绍 SIGINT 2 CtrlC时OS送给前台进程组中每个进程 SIGABRT 6 调用abort...
1.有时候,那些清晨时最坚强的人,正是那些夜里哭着哭着睡着的人。 2.总有一个...
作者个人研发的在高并发场景下,提供的简单、稳定、可扩展的延迟消息队列框架,...
ASP.Net Core的跨域设置比较简单 官方都整合了 具体的参见微软官方文档: https:...
当我们学习surface命令时,已经看到了三维作图的一些端倪。在matlab中我么可以调...