简体   繁体   中英

How I can resize a bitmap properly?

I want to downsample the bitmap of a BMP file by a factor M . I want to obatain the image without aliasing. So in order to achieve it I compute the mean of the MxM pixels in this way:

调整大小

The problem apears when I try to resize non-squared images because it only compute the mean proprely in a square. For example, if the final image is 300x150, the mean is right until 150x150 pixel. If I had the previous_mean -> new_mean = (previous_mean+value)/2

This is how I actually compute it:

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_bitmap and bitmap are 2-D array of PIXELS , being PIXELS :

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

这是绝对正确的,我用old_heigth替换了old_width。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM