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),效果如下:

感興趣的同學,可以自己動手除錯下!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/273615.html
標籤:其他
上一篇:新大陸lora二維碼
