Search code examples
cbitmapimage-resizingbmp

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:

Resize in good 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;

Solution

  • This is absolutely correct, I were permutating the old_width with the old_heigth.