From 4eaa1c89aefb4ebb0cc5aa40613a555e6d250ed0 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Sat, 16 Feb 2019 17:54:22 +0000 Subject: Add customisable image resizing behaviour Two new flags: -w and -W enable resizing and recentering, respectively. Also add an 'autoresize' config variable to control this behaviour. Fixes #122 --- doc/imv.1.txt | 6 ++++++ doc/imv.5.txt | 5 +++++ src/imv.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/doc/imv.1.txt b/doc/imv.1.txt index 07167fd..6c2b45c 100644 --- a/doc/imv.1.txt +++ b/doc/imv.1.txt @@ -60,6 +60,12 @@ Options *-u* :: Set upscaling method used by imv. +*-w*:: + Enable automatic resizing of the window to fit each image. + +*-W*:: + Enable automatic resizing and recentering of the window to fit each image. + *-x*:: Disable looping of input paths. diff --git a/doc/imv.5.txt b/doc/imv.5.txt index 36be08d..e323995 100644 --- a/doc/imv.5.txt +++ b/doc/imv.5.txt @@ -24,6 +24,11 @@ Options The *[options]* section accepts the following settings: +*autoresize* = :: + Sets the resizing behaviour when changing image. 'none' does nothing, and + is the default. 'resize' will resize the window to fit the image. + 'recenter' will resize and recenter the window. + *background* = :: Set the background in imv. Can either be a 6-digit hexadecimal colour code, or 'checks' for a chequered background. Defaults to '000000' diff --git a/src/imv.c b/src/imv.c index 2af5878..29e3a20 100644 --- a/src/imv.c +++ b/src/imv.c @@ -53,6 +53,13 @@ enum background_type { BACKGROUND_TYPE_COUNT }; +/* window behaviour on image change */ +enum resize_mode { + RESIZE_NONE, /* do nothing */ + RESIZE_ONLY, /* resize to fit the new image */ + RESIZE_CENTER /* resize to fit the new image and recenter */ +}; + struct backend_chain { const struct imv_backend *backend; struct backend_chain *next; @@ -84,6 +91,10 @@ struct imv { /* dirty state flags */ bool need_redraw; bool need_rescale; + bool need_resize; + + /* mode for resizing the window on image change */ + enum resize_mode resize_mode; /* traverse sub-directories for more images */ bool recursive_load; @@ -555,6 +566,26 @@ static bool parse_upscaling_method(struct imv *imv, const char *method) return false; } +static bool parse_resizing_mode(struct imv *imv, const char *method) +{ + if (!strcmp(method, "none")) { + imv->upscaling_method = RESIZE_NONE; + return true; + } + + if (!strcmp(method, "resize")) { + imv->upscaling_method = RESIZE_ONLY; + return true; + } + + if (!strcmp(method, "recenter")) { + imv->upscaling_method = RESIZE_CENTER; + return true; + } + + return false; +} + static int load_paths_from_stdin(void *data) { struct imv *imv = data; @@ -613,11 +644,13 @@ bool imv_parse_args(struct imv *imv, int argc, char **argv) int o; - while((o = getopt(argc, argv, "frdxhlu:s:n:b:t:")) != -1) { + while((o = getopt(argc, argv, "frdwWxhlu:s:n:b:t:")) != -1) { switch(o) { case 'f': imv->fullscreen = true; break; case 'r': imv->recursive_load = true; break; case 'd': imv->overlay_enabled = true; break; + case 'w': imv->resize_mode = RESIZE_ONLY; break; + case 'W': imv->resize_mode = RESIZE_CENTER; break; case 'x': imv->loop_input = false; break; case 'l': imv->list_files_at_exit = true; break; case 'n': imv->starting_path = optarg; break; @@ -986,6 +1019,17 @@ static void handle_new_image(struct imv *imv, struct imv_bitmap *bitmap, int fra imv_bitmap_free(bitmap); imv->need_redraw = true; imv->need_rescale = true; + /* If autoresizing on every image is enabled, make sure we do so */ + if (imv->resize_mode != RESIZE_NONE) { + imv->need_resize = true; + } + if (imv->need_resize) { + imv->need_resize = false; + SDL_SetWindowSize(imv->window, imv->current_image.width, imv->current_image.height); + if (imv->resize_mode == RESIZE_CENTER) { + SDL_SetWindowPosition(imv->window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); + } + } imv->loading = false; imv->next_frame_due = frametime ? SDL_GetTicks() + frametime : 0; imv->next_frame_duration = 0; @@ -1282,6 +1326,10 @@ static int handle_ini_value(void *user, const char *section, const char *name, return 1; } + if(!strcmp(name, "autoresize")) { + return parse_resizing_mode(imv, value); + } + if(!strcmp(name, "upscaling_method")) { return parse_upscaling_method(imv, value); } -- cgit v1.2.3