Skip to content

Instantly share code, notes, and snippets.

@EddyLuten
Created November 23, 2016 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EddyLuten/103fcdec46d6ca3aaa67aba9d57d05ff to your computer and use it in GitHub Desktop.
Save EddyLuten/103fcdec46d6ca3aaa67aba9d57d05ff to your computer and use it in GitHub Desktop.
Nearest neighbor 3-component image resizing function
static uint8_t* resize_nearest(
uint8_t* source_data,
uint32_t source_width,
uint32_t source_height,
uint32_t resized_width,
uint32_t resized_height
)
{
uint32_t x, y, di, si;
double px, py;
const double x_ratio = source_width / (double)resized_width;
const double y_ratio = source_height / (double)resized_height;
uint8_t* destination = malloc(resized_width * resized_height * 3);
for (y = 0; y < resized_height; ++y) {
for (x = 0; x < resized_width; ++x) {
px = floor(x * x_ratio);
py = floor(y * y_ratio);
di = (y * resized_width) + x;
si = (uint32_t)((py * source_width) + px);
memcpy(destination + (3 * di), source_data + (3 * si), 3);
}
}
return destination;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment