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

How I can resize a bitmap properly?

我想将 BMP 文件的位图缩减采样因子 M。我想获得没有锯齿的图像。因此,为了实现它,我以这种方式计算 MxM 像素的平均值:

当我尝试调整非正方形图像的大小时出现问题,因为它只计算正方形的平均值。例如,如果最终图像为 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;

这是绝对正确的,我正在用 old_heigth 排列 old_width。