From 8919204a2e666e12216240a792bfff7a391d4d43 Mon Sep 17 00:00:00 2001 From: Bert Date: Wed, 16 Feb 2011 18:16:00 +0100 Subject: [PATCH] Render thumbnails --- image.c | 47 ++++++++++++++++++++++++++--------------------- image.h | 2 ++ main.c | 33 +++++++++++++++++++++++++++++++++ options.c | 10 +++++++--- window.c | 7 +++++++ window.h | 1 + 6 files changed, 76 insertions(+), 24 deletions(-) diff --git a/image.c b/image.c index cc664b6..d299013 100644 --- a/image.c +++ b/image.c @@ -110,6 +110,32 @@ int img_load(img_t *img, const char *filename) { return 1; } +int img_load_thumb(thumb_t *tn, const char *filename) { + int w, h; + float z, zw, zh; + + if (!tn) + return 0; + + if (!_imlib_load_image(filename)) + return 0; + + w = imlib_image_get_width(); + h = imlib_image_get_height(); + zw = (float) THUMB_SIZE / (float) w; + zh = (float) THUMB_SIZE / (float) h; + z = MIN(zw, zh); + tn->w = z * w; + tn->h = z * h; + + imlib_context_set_drawable(tn->pm); + imlib_render_image_part_on_drawable_at_size(0, 0, w, h, + 0, 0, tn->w, tn->h); + imlib_free_image(); + + return 1; +} + void img_check_pan(img_t *img, win_t *win) { if (!img || !win) return; @@ -149,27 +175,6 @@ int img_fit(img_t *img, win_t *win) { return oz != img->zoom; } -int img_load_thumb(thumb_t *tn, const char *filename) { - int w; - int h; - - if (!tn) - return 0; - - if (!_imlib_load_image(filename)) - return 0; - - w = imlib_image_get_width(); - h = imlib_image_get_height(); - - imlib_context_set_drawable(tn->pm); - imlib_render_image_part_on_drawable_at_size(0, 0, w, h, - 0, 0, THUMB_SIZE, THUMB_SIZE); - imlib_free_image(); - - return 1; -} - void img_render(img_t *img, win_t *win) { int sx, sy, sw, sh; int dx, dy, dw, dh; diff --git a/image.h b/image.h index d0ebbf1..49ce70e 100644 --- a/image.h +++ b/image.h @@ -52,6 +52,8 @@ typedef struct img_s { typedef struct thumb_s { int x; int y; + int w; + int h; Pixmap pm; } thumb_t; diff --git a/main.c b/main.c index 602cc60..f4afcf0 100644 --- a/main.c +++ b/main.c @@ -27,6 +27,7 @@ #include #include +#include "config.h" #include "image.h" #include "options.h" #include "util.h" @@ -38,6 +39,7 @@ typedef enum appmode_e { } appmode_t; void update_title(); +void render_thumbs(); int check_append(const char*); void read_dir_rec(const char*); void run(); @@ -53,6 +55,7 @@ int filecnt, fileidx; size_t filesize; thumb_t *thumbs; +int tvis, tcols, trows; #define TITLE_LEN 256 char win_title[TITLE_LEN]; @@ -137,6 +140,7 @@ int main(int argc, char **argv) { if (options->thumbnails == 2) { mode = MODE_THUMBS; + render_thumbs(); } else { mode = MODE_NORMAL; load_image(); @@ -175,6 +179,35 @@ void update_title() { win_set_title(&win, win_title); } +void render_thumbs() { + int i, cnt, x, y; + + tcols = win.w / (THUMB_SIZE + 10); + trows = win.h / (THUMB_SIZE + 10); + + x = win.w - tcols * (THUMB_SIZE + 10) + 5; + y = win.h - trows * (THUMB_SIZE + 10) + 5; + cnt = MIN(tcols * trows, filecnt); + + win_clear(&win); + + i = 0; + while (i < cnt) { + thumbs[i].x = x + (THUMB_SIZE - thumbs[i].w) / 2; + thumbs[i].y = y + (THUMB_SIZE - thumbs[i].h) / 2; + win_draw_pixmap(&win, thumbs[i].pm, thumbs[i].x, thumbs[i].y, thumbs[i].w, + thumbs[i].h); + if (++i % tcols == 0) { + x = win.w - tcols * (THUMB_SIZE + 10) + 5; + y += THUMB_SIZE + 10; + } else { + x += THUMB_SIZE + 10; + } + } + + win_draw(&win); +} + int check_append(const char *filename) { if (!filename) return 0; diff --git a/options.c b/options.c index 2291ded..9d53c6c 100644 --- a/options.c +++ b/options.c @@ -25,12 +25,13 @@ #include "config.h" #include "options.h" +#include "util.h" options_t _options; const options_t *options = (const options_t*) &_options; void print_usage() { - printf("usage: sxiv [-dFfhpqrstvZ] [-g GEOMETRY] [-z ZOOM] FILES...\n"); + printf("usage: sxiv [-dFfhpqrsTtvZ] [-g GEOMETRY] [-z ZOOM] FILES...\n"); } void print_version() { @@ -54,7 +55,7 @@ void parse_options(int argc, char **argv) { _options.quiet = 0; _options.recursive = 0; - while ((opt = getopt(argc, argv, "dFfg:hpqrstvZz:")) != -1) { + while ((opt = getopt(argc, argv, "dFfg:hpqrsTtvZz:")) != -1) { switch (opt) { case '?': print_usage(); @@ -86,8 +87,11 @@ void parse_options(int argc, char **argv) { case 's': _options.scalemode = SCALE_FIT; break; + case 'T': + _options.thumbnails = 2; + break; case 't': - _options.thumbnails = 1; + _options.thumbnails = MAX(_options.thumbnails, 1); break; case 'v': print_version(); diff --git a/window.c b/window.c index 1be14c1..d585c55 100644 --- a/window.c +++ b/window.c @@ -219,6 +219,13 @@ Pixmap win_create_pixmap(win_t *win) { win->env.depth); } +void win_draw_pixmap(win_t *win, Pixmap pm, int x, int y, int w, int h) { + if (!win) + return; + + XCopyArea(win->env.dpy, pm, win->pm, bgc, 0, 0, w, h, x, y); +} + void win_clear(win_t *win) { win_env_t *e; XGCValues gcval; diff --git a/window.h b/window.h index 212272f..5ca4eba 100644 --- a/window.h +++ b/window.h @@ -64,6 +64,7 @@ int win_moveresize(win_t*, int, int, unsigned int, unsigned int); void win_toggle_fullscreen(win_t*); Pixmap win_create_pixmap(win_t*); +void win_draw_pixmap(win_t*, Pixmap, int, int, int, int); void win_clear(win_t*); void win_draw(win_t*);