lel

Fork of Hiltjo's Farbfeld image viewer. (It was just for fun, not much use)
Log | Files | Refs | README | LICENSE

commit 4953d8cb575ba1363490443473a44f1bd5331f88
parent eff5c89f690c55745e345cb81382fe8e3ee39606
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Tue, 20 Apr 2021 18:56:40 -0700

Make the view system function based

While it's also more efficient, I reimplemented it this way because
I like it more.

Diffstat:
Mlel.c | 105++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
1 file changed, 59 insertions(+), 46 deletions(-)

diff --git a/lel.c b/lel.c @@ -23,9 +23,6 @@ /* Image status flags. */ enum { NONE = 0, LOADED = 1, SCALED = 2, DRAWN = 4 }; -/* View mode. */ -enum { ASPECT, FULL_ASPECT, FULL_STRETCH, FULL_WIDTH, FULL_HEIGHT, FULL_IMAGE }; - struct img { int index; int state; @@ -67,9 +64,9 @@ static int done; static int lastimg; static int nimgs; static int screen, xfd; -static int viewmode; static int winwidth, winheight; static struct img img; +static void (*view)(unsigned int *, unsigned int *); static void warn(char *s) @@ -339,42 +336,57 @@ ximage(unsigned int newwidth, unsigned int newheight) } static void +view_stretch(unsigned int *w, unsigned int *h) +{ + *w = winwidth; + *h = winheight; +} + +static void +view_height(unsigned int *w, unsigned int *h) +{ + *w = img.width * winheight / img.height; + *h = winheight; +} + +static void +view_width(unsigned int *w, unsigned int *h) +{ + *w = winwidth; + *h = img.height * winwidth / img.width; +} + +static void +view_full(unsigned int *w, unsigned int *h) +{ + *w = img.width; + *h = img.height; +} + +static void +view_aspect(unsigned int *w, unsigned int *h) +{ + if (winwidth * img.height > winheight * img.width) + view_width(w, h); + else + view_height(w, h); +} + +static void +view_fit(unsigned int *w, unsigned int *h) +{ + if (winwidth * img.height > winheight * img.width) + view_height(w, h); + else + view_width(w, h); +} + +static void scaleview(void) { unsigned int w, h; - switch(viewmode) { - case FULL_STRETCH: - w = winwidth; - h = winheight; - break; - case FULL_ASPECT: - if (winwidth * img.height > winheight * img.width) - goto width; - else - goto height; - case FULL_IMAGE: - if (winwidth * img.height > winheight * img.width) - goto height; - else - goto width; - case FULL_WIDTH: -width: - w = winwidth; - h = img.height * winwidth / img.width; - break; - case FULL_HEIGHT: -height: - w = img.width * winheight / img.height; - h = winheight; - break; - case ASPECT: - default: - w = img.width; - h = img.height; - break; - } - + view(&w, &h); ximage(w * img.view.zoomfact, h * img.view.zoomfact); img.state |= SCALED; } @@ -413,11 +425,11 @@ update(void) } static void -setview(int mode) +setview(void (*newview)(unsigned int *, unsigned int *)) { - if (viewmode == mode) + if (view == newview) return; - viewmode = mode; + view = newview; img.view.panyoffset = 0; img.view.panxoffset = 0; img.state &= ~(DRAWN | SCALED); @@ -526,22 +538,22 @@ keypress(XEvent *ev) deleteimg(); break; case XK_a: - setview(FULL_ASPECT); + setview(view_aspect); break; case XK_s: - setview(FULL_STRETCH); + setview(view_stretch); break; case XK_d: - setview(ASPECT); + setview(view_full); break; case XK_f: - setview(FULL_IMAGE); + setview(view_fit); break; case XK_w: - setview(FULL_WIDTH); + setview(view_width); break; case XK_e: - setview(FULL_HEIGHT); + setview(view_height); break; case XK_Escape: case XK_q: @@ -628,10 +640,11 @@ main(int argc, char *argv[]) { int c; + view = view_full; while ((c = getopt(argc, argv, "f")) != -1) { switch (c) { case 'f': - viewmode = FULL_IMAGE; + view = view_fit; break; default: fprintf(stderr, "usage: %s [-f] [file...]\n",