aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-04-17 03:32:56 +0200
committerdec05eba <dec05eba@protonmail.com>2021-04-17 03:32:56 +0200
commit23435ac30b25cd1a0a8d52c4a792d90f38300344 (patch)
treeb032a66dfcd30b005e2ff9a506b93f30a013e512 /src
parente183c47ec2db4e3bdd82a3fbbe81d2d61c64107a (diff)
Fit image to window in continuous scrolling mode, use n 3 for waifu2x
Diffstat (limited to 'src')
-rw-r--r--src/ImageViewer.cpp58
-rw-r--r--src/QuickMedia.cpp4
2 files changed, 53 insertions, 9 deletions
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 <cmath>
#include <malloc.h>
#include <SFML/Window/Event.hpp>
@@ -10,7 +11,23 @@
#include <SFML/Graphics/RectangleShape.hpp>
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<double> get_no_image_size_scaled(sf::Vector2<double> window_size, bool fit_image_to_window) {
+ sf::Vector2<double> 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<double>(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<double> ImageViewer::get_page_size(int page) {
- const sf::Vector2<double> no_image_page_size(720.0, 1280.0);
+ sf::Vector2<double> 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<double>(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<double>(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();