不明白这段代码是如何缩放 bmp 图像的
Don't understand how this code is scaling a bmp image
下面的代码是我的导师给我的。我只是不明白这是如何缩放 bmp 图像的。我知道有关 bmp 图像的基础知识(维基百科上的信息)。我知道这种方法应该将新图像的行和列乘以任何比例。我尝试手动 运行 代码,但它让我更加困惑。任何帮助都感激不尽。谢谢!
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 的结构。
typedef struct {
unsigned char r;
unsigned char g;
unsigned char b;
} PIXEL;
还有额外的代码,但我认为这个问题不需要。
PIXEL* o = original + (row * cols) + col;
他在这里检索指向原始图像中源像素的指针;它只是简单的指针算法,基于位图中的行在内存中是连续的这一事实。通常,在 C 风格的矩阵 width
范围内,元素 (x, y) 的地址是 beginning + (y * width) + x
.
然后,他在目标图像中遍历 scale
x scale
宽的正方形。
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
指针指向目标图像中的目标像素;如果您从源图像匹配上面的公式并重新排列一些术语,您会看到他正在访问新图像,位置
(scale * col + sx, scale * row + sy)
(请记住,新图像的宽度为 *newcols
)。
*n = *o;
这里他只是将源像素复制到目标像素。
实际上,他 "expanding" 每个源像素在目标图像中 scale x scale 正方形。
下面的代码是我的导师给我的。我只是不明白这是如何缩放 bmp 图像的。我知道有关 bmp 图像的基础知识(维基百科上的信息)。我知道这种方法应该将新图像的行和列乘以任何比例。我尝试手动 运行 代码,但它让我更加困惑。任何帮助都感激不尽。谢谢!
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 的结构。
typedef struct {
unsigned char r;
unsigned char g;
unsigned char b;
} PIXEL;
还有额外的代码,但我认为这个问题不需要。
PIXEL* o = original + (row * cols) + col;
他在这里检索指向原始图像中源像素的指针;它只是简单的指针算法,基于位图中的行在内存中是连续的这一事实。通常,在 C 风格的矩阵 width
范围内,元素 (x, y) 的地址是 beginning + (y * width) + x
.
然后,他在目标图像中遍历 scale
x scale
宽的正方形。
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
指针指向目标图像中的目标像素;如果您从源图像匹配上面的公式并重新排列一些术语,您会看到他正在访问新图像,位置
(scale * col + sx, scale * row + sy)
(请记住,新图像的宽度为 *newcols
)。
*n = *o;
这里他只是将源像素复制到目标像素。
实际上,他 "expanding" 每个源像素在目标图像中 scale x scale 正方形。