aboutsummaryrefslogtreecommitdiff
path: root/src/viewport.c
diff options
context:
space:
mode:
authorSebastian Parborg <darkdefende@gmail.com>2019-08-25 13:58:49 +0200
committerHarry Jeffery <harry@exec64.co.uk>2019-08-25 21:35:15 +0100
commit498c35c28831cf89cc60a773a5c2513c97bc08a3 (patch)
treea706d16e5deaf2019d24830269986d437a61300b /src/viewport.c
parentcd67aca62d1c1169bb35e580e3f4d9ddf708235d (diff)
downloadimv-498c35c28831cf89cc60a773a5c2513c97bc08a3.tar.gz
Added crop scaling method
Added a method that scales and crop the image so that the image will fill the whole window. Also made viewport update respect the current scaling mode.
Diffstat (limited to 'src/viewport.c')
-rw-r--r--src/viewport.c37
1 files changed, 35 insertions, 2 deletions
diff --git a/src/viewport.c b/src/viewport.c
index ce9fa54..5b8b3c7 100644
--- a/src/viewport.c
+++ b/src/viewport.c
@@ -220,15 +220,48 @@ void imv_viewport_scale_to_window(struct imv_viewport *view, const struct imv_im
view->locked = 0;
}
+void imv_viewport_crop_to_window(struct imv_viewport *view, const struct imv_image *image)
+{
+ const int image_width = imv_image_width(image);
+ const int image_height = imv_image_height(image);
+ const double window_aspect = (double)view->buffer.width / (double)view->buffer.height;
+ const double image_aspect = (double)image_width / (double)image_height;
+
+ /* Scale the image so that it fills the whole window */
+ if(window_aspect > image_aspect) {
+ view->scale = (double)view->buffer.width / (double)image_width;
+ } else {
+ view->scale = (double)view->buffer.height / (double)image_height;
+ }
+
+ imv_viewport_center(view, image);
+ view->locked = 0;
+}
+
void imv_viewport_set_redraw(struct imv_viewport *view)
{
view->redraw = 1;
}
+void imv_viewport_rescale(struct imv_viewport *view, const struct imv_image *image,
+ enum scaling_mode scaling_mode) {
+ if (scaling_mode == SCALING_NONE ||
+ (scaling_mode == SCALING_DOWN
+ && view->buffer.width > imv_image_width(image)
+ && view->buffer.height > imv_image_height(image))) {
+ imv_viewport_scale_to_actual(view, image);
+ } else if (scaling_mode == SCALING_CROP) {
+ imv_viewport_crop_to_window(view, image);
+ } else {
+ imv_viewport_scale_to_window(view, image);
+ }
+}
+
void imv_viewport_update(struct imv_viewport *view,
int window_width, int window_height,
int buffer_width, int buffer_height,
- struct imv_image *image)
+ struct imv_image *image,
+ enum scaling_mode scaling_mode)
{
view->window.width = window_width;
view->window.height = window_height;
@@ -241,7 +274,7 @@ void imv_viewport_update(struct imv_viewport *view,
}
imv_viewport_center(view, image);
- imv_viewport_scale_to_window(view, image);
+ imv_viewport_rescale(view, image, scaling_mode);
}
int imv_viewport_needs_redraw(struct imv_viewport *view)