Krolyxon
Commit e0d0892

Branches

Refs →

nsxivthumbs.c

150 lines · 3.4 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#include <pthread.h>
22
23#include <Imlib2.h>
24
25#include "config.h"
26#include "thumbs.h"
27#include "util.h"
28
29typedef struct tload_s {
30 const char **filenames;
31 tns_t *tns;
32 win_t *win;
33} tload_t;
34
35const int thumb_dim = THUMB_SIZE + 10;
36
37pthread_t loader;
38tload_t tinfo;
39
40void* thread_load(void *arg) {
41 int i, w, h;
42 float z, zw, zh;
43 tload_t *tl;
44 thumb_t *t;
45 Imlib_Image *im;
46
47 tl = (tload_t*) arg;
48
49 for (i = 0; i < tl->tns->cnt; ++i) {
50 if (!(im = imlib_load_image(tl->filenames[i])))
51 continue;
52
53 imlib_context_set_image(im);
54
55 w = imlib_image_get_width();
56 h = imlib_image_get_height();
57 zw = (float) THUMB_SIZE / (float) w;
58 zh = (float) THUMB_SIZE / (float) h;
59 z = MIN(zw, zh);
60
61 t = &tl->tns->thumbs[i];
62 t->w = z * w;
63 t->h = z * h;
64
65 t->pm = win_create_pixmap(tl->win, t->w, t->h);
66 imlib_context_set_drawable(t->pm);
67 imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
68 0, 0, t->w, t->h);
69 t->loaded = 1;
70 imlib_free_image();
71 }
72
73 tl->tns->loaded = 1;
74
75 return 0;
76}
77
78void tns_load(tns_t *tns, win_t *win, const char **fnames, int fcnt) {
79 pthread_attr_t tattr;
80
81 if (!tns || !win || !fnames || !fcnt)
82 return;
83
84 tns->thumbs = (thumb_t*) s_malloc(fcnt * sizeof(thumb_t));
85 memset(tns->thumbs, 0, fcnt * sizeof(thumb_t));
86 tns->cnt = fcnt;
87 tns->loaded = 0;
88
89 tinfo.filenames = fnames;
90 tinfo.tns = tns;
91 tinfo.win = win;
92
93 pthread_attr_init(&tattr);
94 pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE);
95
96 if (pthread_create(&loader, &tattr, thread_load, (void*) &tinfo))
97 die("could not create thread");
98}
99
100void tns_free(tns_t *tns, win_t *win) {
101 int i;
102
103 if (!tns)
104 return;
105
106 for (i = 0; i < tns->cnt; ++i)
107 win_free_pixmap(win, tns->thumbs[i].pm);
108
109 free(tns->thumbs);
110 tns->thumbs = NULL;
111}
112
113void tns_render(tns_t *tns, win_t *win) {
114 int i, cnt, x, y;
115
116 if (!tns || !win)
117 return;
118
119 tns->cols = win->w / thumb_dim;
120 tns->rows = win->h / thumb_dim;
121
122 cnt = tns->cols * tns->rows;
123 if (tns->first && tns->first + cnt > tns->cnt)
124 tns->first = MAX(0, tns->cnt - cnt);
125 cnt = MIN(tns->first + cnt, tns->cnt);
126
127 win_clear(win);
128
129 x = y = 5;
130 i = tns->first;
131
132 while (i < cnt) {
133 if (!tns->thumbs[i].loaded)
134 continue;
135
136 tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2;
137 tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2;
138 win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x,
139 tns->thumbs[i].y, tns->thumbs[i].w, tns->thumbs[i].h);
140 if (++i % tns->cols == 0) {
141 x = 5;
142 y += thumb_dim;
143 } else {
144 x += thumb_dim;
145 }
146 }
147
148 win_draw(win);
149}
150