From 23435ac30b25cd1a0a8d52c4a792d90f38300344 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 17 Apr 2021 03:32:56 +0200 Subject: Fit image to window in continuous scrolling mode, use n 3 for waifu2x --- src/ImageViewer.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++------- src/QuickMedia.cpp | 4 ++-- 2 files changed, 53 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/ImageViewer.cpp b/src/ImageViewer.cpp index 271d69f..dbd2284 100644 --- a/src/ImageViewer.cpp +++ b/src/ImageViewer.cpp @@ -3,6 +3,7 @@ #include "../include/Storage.hpp" #include "../include/SfmlFixes.hpp" #include "../include/ResourceLoader.hpp" +#include "../include/Scale.hpp" #include #include #include @@ -10,7 +11,23 @@ #include namespace QuickMedia { - ImageViewer::ImageViewer(sf::RenderWindow *window, int num_pages, const std::string &content_title, const std::string &chapter_title, int current_page, const Path &chapter_cache_dir) : + static sf::Vector2 get_no_image_size_scaled(sf::Vector2 window_size, bool fit_image_to_window) { + sf::Vector2 no_image_page_size(720.0, 1280.0); + sf::Vector2f no_image_page_size_f(no_image_page_size.x, no_image_page_size.y); + sf::Vector2f content_size(window_size.x, window_size.y); + + sf::Vector2f image_scale; + if(fit_image_to_window) + image_scale = get_ratio(no_image_page_size_f, wrap_to_size_x(no_image_page_size_f, content_size)); + else + image_scale = get_ratio(no_image_page_size_f, clamp_to_size_x(no_image_page_size_f, content_size)); + + no_image_page_size.x *= image_scale.x; + no_image_page_size.y *= image_scale.y; + return no_image_page_size; + } + + ImageViewer::ImageViewer(sf::RenderWindow *window, int num_pages, const std::string &content_title, const std::string &chapter_title, int current_page, const Path &chapter_cache_dir, bool *fit_image_to_window) : window(window), current_page(current_page), num_pages(num_pages), @@ -19,7 +36,8 @@ namespace QuickMedia { chapter_cache_dir(chapter_cache_dir), focused_page(current_page), font(FontLoader::get_font(FontLoader::FontType::LATIN)), - page_text("", *FontLoader::get_font(FontLoader::FontType::LATIN), 14) + page_text("", *FontLoader::get_font(FontLoader::FontType::LATIN), 14), + fit_image_to_window(fit_image_to_window) { current_page = std::min(current_page, num_pages); image_data.resize(num_pages); @@ -87,6 +105,17 @@ namespace QuickMedia { page_image_data->visible_on_screen = true; if(page_image_data->image_status == ImageStatus::APPLIED_TO_TEXTURE) { + auto texture_size = page_image_data->sprite.getTexture()->getSize(); + sf::Vector2f texture_size_f(texture_size.x, texture_size.y); + sf::Vector2f content_size(window_size.x, window_size.y); + + sf::Vector2f image_scale; + if(*fit_image_to_window) + image_scale = get_ratio(texture_size_f, wrap_to_size_x(texture_size_f, content_size)); + else + image_scale = get_ratio(texture_size_f, clamp_to_size_x(texture_size_f, content_size)); + + page_image_data->sprite.setScale(image_scale); page_image_data->sprite.setPosition(render_pos.x, render_pos.y); window.draw(page_image_data->sprite); } else { @@ -198,6 +227,9 @@ namespace QuickMedia { if(event.key.code == sf::Keyboard::I) return ImageViewerAction::SWITCH_TO_SINGLE_IMAGE_MODE; + + if(event.key.code == sf::Keyboard::F) + *fit_image_to_window = !*fit_image_to_window; } else if(event.type == sf::Event::KeyReleased) { if(event.key.code == sf::Keyboard::Up || (event.key.control && event.key.code == sf::Keyboard::K)) up_pressed = false; @@ -267,9 +299,10 @@ namespace QuickMedia { double height_before = get_page_size(page_i).y; page_data->image_status = ImageStatus::APPLIED_TO_TEXTURE; page_data->sprite.setTexture(page_data->texture, true); - page_size[page_i].size = get_page_size(page_i); + sf::Vector2u texture_size = image_data[page_i]->texture.getSize(); + page_size[page_i].size = sf::Vector2(texture_size.x, texture_size.y); page_size[page_i].loaded = true; - double height_after = page_size[page_i].size.y; + double height_after = get_page_size(page_i).y; double height_diff = height_before - height_after; if(scroll_speed <= 0.0 && page_i < current_page) { @@ -358,17 +391,28 @@ namespace QuickMedia { } sf::Vector2 ImageViewer::get_page_size(int page) { - const sf::Vector2 no_image_page_size(720.0, 1280.0); + sf::Vector2 no_image_page_size = get_no_image_size_scaled(window_size, *fit_image_to_window); if(page < 0 || page >= (int)image_data.size()) return no_image_page_size; - if(page_size[page].loaded) - return page_size[page].size; + if(page_size[page].loaded) { + sf::Vector2f texture_size_f(page_size[page].size.x, page_size[page].size.y); + sf::Vector2f content_size(window_size.x, window_size.y); + + sf::Vector2f image_scale; + if(*fit_image_to_window) + image_scale = get_ratio(texture_size_f, wrap_to_size_x(texture_size_f, content_size)); + else + image_scale = get_ratio(texture_size_f, clamp_to_size_x(texture_size_f, content_size)); + + return sf::Vector2(page_size[page].size.x * image_scale.x, page_size[page].size.y * image_scale.y); + } if(!image_data[page] || image_data[page]->image_status != ImageStatus::APPLIED_TO_TEXTURE) return no_image_page_size; + // Do not scale here, because this will be used to set page_size[page].size sf::Vector2u texture_size = image_data[page]->texture.getSize(); return sf::Vector2(texture_size.x, texture_size.y); } diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 0f69f85..44901e8 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -548,7 +548,7 @@ namespace QuickMedia { tmp_file.append(".tmp.png"); fprintf(stderr, "Upscaling %s\n", copy_op.source.data.c_str()); - const char *args[] = { "waifu2x-ncnn-vulkan", "-i", copy_op.source.data.c_str(), "-o", tmp_file.data.c_str(), nullptr }; + const char *args[] = { "waifu2x-ncnn-vulkan", "-n", "3", "-i", copy_op.source.data.c_str(), "-o", tmp_file.data.c_str(), nullptr }; if(exec_program(args, nullptr, nullptr) != 0) { fprintf(stderr, "Warning: failed to upscale %s with waifu2x-ncnn-vulkan\n", copy_op.source.data.c_str()); // No conversion, but we need the file to have the destination name to see that the operation completed (and read it) @@ -2784,7 +2784,7 @@ namespace QuickMedia { json_chapter = Json::Value(Json::objectValue); } - ImageViewer image_viewer(&window, num_manga_pages, images_page->manga_name, images_page->get_chapter_name(), image_index, content_cache_dir); + ImageViewer image_viewer(&window, num_manga_pages, images_page->manga_name, images_page->get_chapter_name(), image_index, content_cache_dir, &fit_image_to_window); json_chapter["current"] = std::min(latest_read, image_viewer.get_num_pages()); json_chapter["total"] = image_viewer.get_num_pages(); -- cgit v1.2.3