aboutsummaryrefslogtreecommitdiff
path: root/src/AsyncImageLoader.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-10-19 11:02:26 +0200
committerdec05eba <dec05eba@protonmail.com>2020-10-19 11:02:26 +0200
commitb233f24fe103a745eb6487e236b7cb08269a6cb3 (patch)
treefbbc54060727a12769048ab5741d6f234686dbcd /src/AsyncImageLoader.cpp
parent386d16a95a1306decabf6e9764309caf0d2818e3 (diff)
Change thumbnail creation from nearest neighbor to linear interpolation, set body thumbnail size for 4chan and matrix (if available)
Diffstat (limited to 'src/AsyncImageLoader.cpp')
-rw-r--r--src/AsyncImageLoader.cpp28
1 files changed, 24 insertions, 4 deletions
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;
}
}