From b233f24fe103a745eb6487e236b7cb08269a6cb3 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 19 Oct 2020 11:02:26 +0200 Subject: Change thumbnail creation from nearest neighbor to linear interpolation, set body thumbnail size for 4chan and matrix (if available) --- src/AsyncImageLoader.cpp | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'src/AsyncImageLoader.cpp') diff --git a/src/AsyncImageLoader.cpp b/src/AsyncImageLoader.cpp index 8f771e7..00ca7ee 100644 --- a/src/AsyncImageLoader.cpp +++ b/src/AsyncImageLoader.cpp @@ -19,6 +19,7 @@ namespace QuickMedia { return sf::Vector2u(vec.x, vec.y); } + // Linear interpolation static void copy_resize(const sf::Image &source, sf::Image &destination, sf::Vector2u destination_size) { const sf::Vector2u source_size = source.getSize(); if(source_size.x == 0 || source_size.y == 0 || destination_size.x == 0 || destination_size.y == 0) @@ -33,14 +34,33 @@ namespace QuickMedia { sf::Uint32 *destination_pixel = destination_pixels; for(unsigned int y = 0; y < destination_size.y; ++y) { for(unsigned int x = 0; x < destination_size.x; ++x) { - int scaled_x = ((float)x / (float)destination_size.x) * source_size.x; - int scaled_y = ((float)y / (float)destination_size.y) * source_size.y; + int scaled_x_start = ((float)x / (float)destination_size.x) * source_size.x; + int scaled_y_start = ((float)y / (float)destination_size.y) * source_size.y; + int scaled_x_end = ((float)(x + 1) / (float)destination_size.x) * source_size.x; + int scaled_y_end = ((float)(y + 1) / (float)destination_size.y) * source_size.y; //float scaled_x = x * width_ratio; //float scaled_y = y * height_ratio; //sf::Uint32 *source_pixel = (sf::Uint32*)(source_pixels + (int)(scaled_x + scaled_y * source_size.x) * 4); - sf::Uint32 *source_pixel = (sf::Uint32*)(source_pixels + (scaled_x + scaled_y * source_size.x) * 4); - *destination_pixel = *source_pixel; + sf::Uint32 sum_red = 0; + sf::Uint32 sum_green = 0; + sf::Uint32 sum_blue = 0; + sf::Uint32 sum_alpha = 0; + sf::Uint32 num_colors = (scaled_x_end - scaled_x_start) * (scaled_y_end - scaled_y_start); + for(int yy = scaled_y_start; yy < scaled_y_end; ++yy) { + for(int xx = scaled_x_start; xx < scaled_x_end; ++xx) { + sf::Uint32 *source_pixel = (sf::Uint32*)(source_pixels + (xx + yy * source_size.x) * 4); + sum_red += (*source_pixel >> 24); + sum_green += ((*source_pixel >> 16) & 0xFF); + sum_blue += ((*source_pixel >> 8) & 0xFF); + sum_alpha += (*source_pixel & 0xFF); + } + } + sum_red /= num_colors; + sum_green /= num_colors; + sum_blue /= num_colors; + sum_alpha /= num_colors; + *destination_pixel = (sum_red << 24) | (sum_green << 16) | (sum_blue << 8) | sum_alpha; ++destination_pixel; } } -- cgit v1.2.3