C 不理解此代码是如何缩放bmp图像的
下面的代码是我的导师给我的。我只是不明白这是如何缩放bmp图像的。我知道关于bmp图像的基本知识和维基百科上的信息。我知道这个方法应该是将新图像的行和列乘以任何比例。我试图手动运行代码,但这让我更加困惑。任何帮助都将不胜感激。谢谢C 不理解此代码是如何缩放bmp图像的,c,bmp,C,Bmp,下面的代码是我的导师给我的。我只是不明白这是如何缩放bmp图像的。我知道关于bmp图像的基本知识和维基百科上的信息。我知道这个方法应该是将新图像的行和列乘以任何比例。我试图手动运行代码,但这让我更加困惑。任何帮助都将不胜感激。谢谢 int enlarge(PIXEL* original, int rows, int cols, int scale, PIXEL** new, int* newrows, int* newcols) { //scaling the new
int enlarge(PIXEL* original, int rows, int cols, int scale,
PIXEL** new, int* newrows, int* newcols)
{
//scaling the new rows & cols
*newcols = cols * scale;
*newrows = rows * scale;
//memory allocated for enlaged bmp
*new = (PIXEL*)malloc(*newrows * *newcols * sizeof(PIXEL));
int row, col, sx, sy;
//transverse through every row
for (row = 0; row < rows; row++ )
//transvere through every col
for (col = 0; col < cols; col++ ){
//im unsure what this is for
PIXEL* o = original + (row * cols) + col;
for(sy = 0; sy < scale; sy++ )
for(sx = 0; sx < scale; sx++ )
{
//im unsure what this is for
PIXEL* n = *new + (scale * row) * *newcols + (scale * col) + (sy * *newcols) + sx;
*n = *o;
}
}
return 0;
}
有额外的代码,但我认为这个问题不需要这些代码
PIXEL* o = original + (row * cols) + col;
在这里,他正在检索指向原始图像中源像素的指针;这只是一个微不足道的指针算法,基于位图中的行在内存中是连续的这一事实。通常,在宽度为元素x的地址的C样式矩阵中,y是开始+y*宽度+x
然后,他在目标图像中的一个正方形比例x比例宽上循环
for(sy = 0; sy < scale; sy++ )
for(sx = 0; sx < scale; sx++ )
{
//im unsure what this is for
PIXEL* n = *new + (scale * row) * *newcols + (scale * col) + (sy * *newcols) + sx;
请记住,新图像是*newcols宽的
在这里,他只是将源像素复制到目标像素
实际上,他将每个源像素扩展为目标图像中的x比例正方形。内部两个循环用o指向的像素副本填充放大版本中的正方形。o只是在所有像素上循环的上下文中的当前像素。它在每个像素上做什么?这是一个很好的解释。非常感谢你!
for(sy = 0; sy < scale; sy++ )
for(sx = 0; sx < scale; sx++ )
{
//im unsure what this is for
PIXEL* n = *new + (scale * row) * *newcols + (scale * col) + (sy * *newcols) + sx;
(scale * col + sx, scale * row + sy)
*n = *o;