我如何正确调整位图的大小?

时间:2017-12-14 19:27:35

标签: c bitmap image-resizing bmp

我想通过因子M对BMP文件的位图进行下采样。我想在没有锯齿的情况下使用图像。所以为了实现它,我用这种方式计算MxM像素的平均值:

Resize in good way

当我尝试调整非平方图像的大小时,问题出现了,因为它只能在正方形中计算平均值。例如,如果最终图像为300x150,则平均值为150x150像素。如果我有previous_mean - > new_mean = (previous_mean+value)/2

这就是我实际计算它的方式:

for (i = 0; i < new_height; i++) {
for (j = 0; j < new_width; j++) {
  mean.r = bitmap[i*factor][j*factor].r;
  mean.g = bitmap[i*factor][j*factor].g;
  mean.b = bitmap[i*factor][j*factor].b;
  for(k = i*factor; (k < i*factor+factor)&&(k<old_height); k++){
    for(l = j*factor; (l < j*factor+factor)&&(l<old_width); l++){
      mean.r = (mean.r + bitmap[k][l].r)/2;
      mean.g = (mean.g + bitmap[k][l].g)/2;
      mean.b = (mean.b + bitmap[k][l].b)/2;
    }
  }
  new_bitmap[i][j] = mean;
  mean.r = 0;
  mean.g = 0;
  mean.b = 0;
}

}

new_bitmapbitmapPIXELS的二维数组,为PIXELS

typedef struct __attribute__((__packed__)){
  unsigned char b;
  unsigned char g;
  unsigned char r;
} PIXELS;

1 个答案:

答案 0 :(得分:0)

这是绝对正确的,我用old_heig来置换old_width。