Krolyxon
Commit dc04bdc

Branches

Refs →

nsxivthumbs.c

117 lines · 2.7 KB · C

1/* sxiv: thumbs.c
2 * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#include <stdlib.h>
20#include <string.h>
21
22#include <Imlib2.h>
23
24#include "config.h"
25#include "thumbs.h"
26#include "util.h"
27
28const int thumb_dim = THUMB_SIZE + 10;
29
30void tns_init(tns_t *tns, int cnt) {
31 if (!tns)
32 return;
33
34 tns->cnt = tns->first = tns->sel = 0;
35 tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
36}
37
38void tns_free(tns_t *tns, win_t *win) {
39 int i;
40
41 if (!tns)
42 return;
43
44 for (i = 0; i < tns->cnt; ++i)
45 win_free_pixmap(win, tns->thumbs[i].pm);
46
47 free(tns->thumbs);
48 tns->thumbs = NULL;
49}
50
51void tns_load(tns_t *tns, win_t *win, const char *filename) {
52 int w, h;
53 float z, zw, zh;
54 thumb_t *t;
55 Imlib_Image *im;
56
57 if (!tns || !win || !filename)
58 return;
59
60 if (!(im = imlib_load_image(filename)))
61 return;
62
63 imlib_context_set_image(im);
64
65 w = imlib_image_get_width();
66 h = imlib_image_get_height();
67 zw = (float) THUMB_SIZE / (float) w;
68 zh = (float) THUMB_SIZE / (float) h;
69 z = MIN(zw, zh);
70
71 t = &tns->thumbs[tns->cnt++];
72 t->filename = filename;
73 t->w = z * w;
74 t->h = z * h;
75
76 t->pm = win_create_pixmap(win, t->w, t->h);
77 imlib_context_set_drawable(t->pm);
78 imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
79 0, 0, t->w, t->h);
80 imlib_free_image();
81}
82
83void tns_render(tns_t *tns, win_t *win) {
84 int i, cnt, x, y;
85
86 if (!tns || !win)
87 return;
88
89 tns->cols = win->w / thumb_dim;
90 tns->rows = win->h / thumb_dim;
91
92 cnt = tns->cols * tns->rows;
93 if (tns->first && tns->first + cnt > tns->cnt)
94 tns->first = MAX(0, tns->cnt - cnt);
95 cnt = MIN(tns->first + cnt, tns->cnt);
96
97 win_clear(win);
98
99 x = y = 5;
100 i = tns->first;
101
102 while (i < cnt) {
103 tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2;
104 tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2;
105 win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x,
106 tns->thumbs[i].y, tns->thumbs[i].w, tns->thumbs[i].h);
107 if (++i % tns->cols == 0) {
108 x = 5;
109 y += thumb_dim;
110 } else {
111 x += thumb_dim;
112 }
113 }
114
115 win_draw(win);
116}
117