SysTools Logo SysTools


C, ANSI C: Fast integer-only image resizing/scaling


/* fast integer-only image resizing/scaling algorithm
   assuming image pixels stored from left to right,
   from top to bottom and without any row padding
   declare "my_pixel" type as you see fit:
   uint8_t (byte) / uint16_t (word) / uint32_t (dword) / etc. */
void fast_resize_image(
  my_pixel *simg, uint16_t swidth, uint16_t sheight,
  my_pixel *dimg, uint16_t dwidth, uint16_t dheight
) {
uint32_t i, j, cx, cy, x, y;
my_pixel *row;
  /* sanity checks */
  if (simg && swidth && sheight && dimg && dwidth && dheight) {
    cx = (swidth << 16) / dwidth;
    cy = (sheight << 16) / dheight;
    y = 0;
    for (j = 0; j < dheight; j++) {
      x = 0;
      row = &simg[(y >> 16) * swidth];
      for (i = 0; i < dwidth; i++) {
        *dimg = row[x >> 16];
        dimg++;
        x += cx;
      }
      y += cy;
    }
  }
}

2025.11.20


[ Код ]