【发布时间】:2017-04-02 00:42:03
【问题描述】:
以下代码是我的导师给我的。我只是不明白这是如何缩放 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;
还有其他代码,但我认为这个问题不需要。
【问题讨论】:
-
内部的两个循环用
o指向的像素副本填充放大版本中的一个正方形。o只是您在所有像素上循环的上下文中的“当前像素”。 -
每个像素的作用是什么?