Created
November 23, 2016 09:39
-
-
Save EddyLuten/103fcdec46d6ca3aaa67aba9d57d05ff to your computer and use it in GitHub Desktop.
Nearest neighbor 3-component image resizing function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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