2017-04-02 83 views
0

下面的代码是我的老师给我的。我只是不明白这是如何缩放bmp图像。我知道关于bmp图像的基础知识(wikipedia上的信息)。我知道这种方法应该用任何比例乘以新图像的行和列。我试图手动运行代码,但它让我更加困惑。任何帮助都感激不尽。谢谢!不明白这段代码是如何缩放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; 

有额外的代码,但我不认为这是需要这个问题。

+0

内两个环路填写与像素的副本放大的版本的方通过'O'指向。 'o'只是您在所有像素上循环的上下文中的“当前像素”。 – harold

+0

它在每个像素处做了什么? – name

回答

1
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; 

在这里,他只是将源像素复制到目标像素。

实际上,他在目标图像中将每个源像素“扩展”为比例x比例尺正方形。

+1

这是一个很好的解释。非常感谢你! – name