aboutsummaryrefslogtreecommitdiff
path: root/src/Body.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Body.cpp')
-rw-r--r--src/Body.cpp122
1 files changed, 2 insertions, 120 deletions
diff --git a/src/Body.cpp b/src/Body.cpp
index 13e3d7d..843a1b1 100644
--- a/src/Body.cpp
+++ b/src/Body.cpp
@@ -1,8 +1,6 @@
#include "../include/Body.hpp"
#include "../include/QuickMedia.hpp"
#include "../include/Scale.hpp"
-#include "../include/base64_url.hpp"
-#include "../include/ImageUtils.hpp"
#include "../plugins/Plugin.hpp"
#include <SFML/OpenGL.hpp>
#include <assert.h>
@@ -72,7 +70,6 @@ namespace QuickMedia {
wrap_around(false),
line_seperator_color(sf::Color(32, 37, 43, 255)),
program(program),
- loading_thumbnail(false),
selected_item(0),
prev_selected_item(0),
page_scroll(0.0f),
@@ -271,115 +268,6 @@ namespace QuickMedia {
//page_scroll = 0.0f;
}
- static sf::Vector2f to_vec2f(const sf::Vector2u &vec) {
- return sf::Vector2f(vec.x, vec.y);
- }
-
- static sf::Vector2f to_vec2f(const sf::Vector2i &vec) {
- return sf::Vector2f(vec.x, vec.y);
- }
-
- static sf::Vector2u to_vec2u(const sf::Vector2f &vec) {
- return sf::Vector2u(vec.x, vec.y);
- }
-
- 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)
- return;
-
- //float width_ratio = (float)source_size.x / (float)destination_size.x;
- //float height_ratio = (float)source_size.y / (float)destination_size.y;
-
- const sf::Uint8 *source_pixels = source.getPixelsPtr();
- // TODO: Remove this somehow. Right now we need to allocate this and also allocate the same array in the destination image
- sf::Uint32 *destination_pixels = new sf::Uint32[destination_size.x * destination_size.y];
- 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;
- //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;
- ++destination_pixel;
- }
- }
- destination.create(destination_size.x, destination_size.y, (sf::Uint8*)destination_pixels);
- delete []destination_pixels;
- }
-
- static bool save_image_as_thumbnail_atomic(const sf::Image &image, const Path &thumbnail_path, const char *ext) {
- Path tmp_path = thumbnail_path;
- tmp_path.append(".tmp");
- const char *thumbnail_path_ext = thumbnail_path.ext();
- if(is_image_ext(ext))
- tmp_path.append(ext);
- else if(is_image_ext(thumbnail_path_ext))
- tmp_path.append(thumbnail_path_ext);
- else
- tmp_path.append(".png");
- return image.saveToFile(tmp_path.data) && (rename(tmp_path.data.c_str(), thumbnail_path.data.c_str()) == 0);
- }
-
- // Returns empty string if no extension
- static const char* get_ext(const std::string &path) {
- size_t index = path.rfind('.');
- if(index == std::string::npos)
- return "";
- return path.c_str() + index;
- }
-
- // TODO: Do not load thumbnails for images larger than 30mb.
- // TODO: Load the thumbnail embedded in the file instead.
- void Body::load_thumbnail_from_url(const std::string &url, bool local, sf::Vector2i thumbnail_resize_target_size, std::shared_ptr<ThumbnailData> thumbnail_data) {
- assert(!loading_thumbnail);
- loading_thumbnail = true;
-
- load_thumbnail_future = std::async(std::launch::async, [this, url, local, thumbnail_resize_target_size, thumbnail_data]() mutable {
- // TODO: Use sha256 instead of base64_url encoding
- Path thumbnail_path = get_cache_dir().join("thumbnails").join(base64_url::encode(url));
-
- thumbnail_data->image = std::make_unique<sf::Image>();
- if(thumbnail_data->image->loadFromFile(thumbnail_path.data)) {
- fprintf(stderr, "Loaded %s from thumbnail cache\n", url.c_str());
- thumbnail_data->loading_state = LoadingState::FINISHED_LOADING;
- return;
- } else {
- if(local) {
- if(!thumbnail_data->image->loadFromFile(url)) {
- thumbnail_data->loading_state = LoadingState::FINISHED_LOADING;
- return;
- }
- } else {
- std::string texture_data;
- if(download_to_string_cache(url, texture_data, {}, program->get_current_plugin()->use_tor, true) != DownloadResult::OK || !thumbnail_data->image->loadFromMemory(texture_data.data(), texture_data.size())) {
- thumbnail_data->loading_state = LoadingState::FINISHED_LOADING;
- return;
- }
- }
- }
-
- if(thumbnail_resize_target_size.x != 0 && thumbnail_resize_target_size.y != 0) {
- sf::Vector2u new_image_size = to_vec2u(clamp_to_size(to_vec2f(thumbnail_data->image->getSize()), to_vec2f(thumbnail_resize_target_size)));
- if(new_image_size.x < thumbnail_data->image->getSize().x || new_image_size.y < thumbnail_data->image->getSize().y) {
- auto destination_image = std::make_unique<sf::Image>();
- copy_resize(*thumbnail_data->image, *destination_image, new_image_size);
- thumbnail_data->image = std::move(destination_image);
- save_image_as_thumbnail_atomic(*thumbnail_data->image, thumbnail_path, get_ext(url));
- thumbnail_data->loading_state = LoadingState::FINISHED_LOADING;
- return;
- }
- }
-
- thumbnail_data->loading_state = LoadingState::FINISHED_LOADING;
- return;
- });
- }
-
void Body::draw(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size) {
draw(window, pos, size, Json::nullValue);
}
@@ -404,11 +292,6 @@ namespace QuickMedia {
num_visible_items = 0;
last_item_fully_visible = true;
- if(loading_thumbnail && load_thumbnail_future.valid() && load_thumbnail_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
- load_thumbnail_future.get();
- loading_thumbnail = false;
- }
-
int num_items = items.size();
if(num_items == 0 || size.y <= 0.0f) {
for(auto it = item_thumbnail_textures.begin(); it != item_thumbnail_textures.end();) {
@@ -570,9 +453,8 @@ namespace QuickMedia {
item_thumbnail->referenced = true;
if(draw_thumbnails) {
- if(!loading_thumbnail && !item->thumbnail_url.empty() && item_thumbnail->loading_state == LoadingState::NOT_LOADED) {
- item_thumbnail->loading_state = LoadingState::LOADING;
- load_thumbnail_from_url(item->thumbnail_url, item->thumbnail_is_local, thumbnail_resize_target_size, item_thumbnail);
+ if(!item->thumbnail_url.empty() && item_thumbnail->loading_state == LoadingState::NOT_LOADED) {
+ async_image_loader.load_thumbnail(item->thumbnail_url, item->thumbnail_is_local, thumbnail_resize_target_size, program->get_current_plugin()->use_tor, item_thumbnail);
}
if(item_thumbnail->loading_state == LoadingState::FINISHED_LOADING) {