Krolyxon
Commit 33b8148

Branches

Refs →

nsxivthumbs.c

609 lines · 13.7 KB · C

1/* Copyright 2011 Bert Muennich
2 *
3 * This file is part of sxiv.
4 *
5 * sxiv is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 2 of the License,
8 * or (at your option) any later version.
9 *
10 * sxiv is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#define _POSIX_C_SOURCE 200112L
20#define _THUMBS_CONFIG
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <unistd.h>
28#include <utime.h>
29
30#include "thumbs.h"
31#include "util.h"
32#include "config.h"
33
34#if HAVE_LIBEXIF
35#include <libexif/exif-data.h>
36void exif_auto_orientate(const fileinfo_t*);
37#endif
38
39static char *cache_dir;
40
41char* tns_cache_filepath(const char *filepath)
42{
43 size_t len;
44 char *cfile = NULL;
45
46 if (cache_dir == NULL || filepath == NULL || *filepath != '/')
47 return NULL;
48
49 if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
50 /* don't cache images inside the cache directory! */
51 len = strlen(cache_dir) + strlen(filepath) + 6;
52 cfile = (char*) s_malloc(len);
53 snprintf(cfile, len, "%s/%s.jpg", cache_dir, filepath + 1);
54 }
55 return cfile;
56}
57
58Imlib_Image tns_cache_load(const char *filepath, bool *outdated)
59{
60 char *cfile;
61 struct stat cstats, fstats;
62 Imlib_Image im = NULL;
63
64 if (filepath == NULL)
65 return NULL;
66 if (stat(filepath, &fstats) < 0)
67 return NULL;
68
69 if ((cfile = tns_cache_filepath(filepath)) != NULL) {
70 if (stat(cfile, &cstats) == 0) {
71 if (cstats.st_mtime == fstats.st_mtime)
72 im = imlib_load_image(cfile);
73 else
74 *outdated = true;
75 }
76 free(cfile);
77 }
78 return im;
79}
80
81void tns_cache_write(Imlib_Image im, const char *filepath, bool force)
82{
83 char *cfile, *dirend;
84 struct stat cstats, fstats;
85 struct utimbuf times;
86 Imlib_Load_Error err = 0;
87
88 if (im == NULL || filepath == NULL)
89 return;
90 if (stat(filepath, &fstats) < 0)
91 return;
92
93 if ((cfile = tns_cache_filepath(filepath)) != NULL) {
94 if (force || stat(cfile, &cstats) < 0 ||
95 cstats.st_mtime != fstats.st_mtime)
96 {
97 if ((dirend = strrchr(cfile, '/')) != NULL) {
98 *dirend = '\0';
99 err = r_mkdir(cfile);
100 *dirend = '/';
101 }
102 if (err == 0) {
103 imlib_context_set_image(im);
104 imlib_image_set_format("jpg");
105 imlib_image_attach_data_value("quality", NULL, 90, NULL);
106 imlib_save_image_with_error_return(cfile, &err);
107 }
108 if (err == 0) {
109 times.actime = fstats.st_atime;
110 times.modtime = fstats.st_mtime;
111 utime(cfile, &times);
112 }
113 }
114 free(cfile);
115 }
116}
117
118void tns_clean_cache(tns_t *tns)
119{
120 int dirlen;
121 bool delete;
122 char *cfile, *filename, *tpos;
123 r_dir_t dir;
124
125 if (cache_dir == NULL)
126 return;
127
128 if (r_opendir(&dir, cache_dir) < 0) {
129 warn("could not open thumbnail cache directory: %s", cache_dir);
130 return;
131 }
132
133 dirlen = strlen(cache_dir);
134
135 while ((cfile = r_readdir(&dir)) != NULL) {
136 filename = cfile + dirlen;
137 delete = false;
138
139 if ((tpos = strrchr(filename, '.')) != NULL) {
140 *tpos = '\0';
141 if (access(filename, F_OK) < 0)
142 delete = true;
143 *tpos = '.';
144 }
145 if (delete) {
146 if (unlink(cfile) < 0)
147 warn("could not delete cache file: %s", cfile);
148 }
149 free(cfile);
150 }
151 r_closedir(&dir);
152}
153
154
155void tns_init(tns_t *tns, const fileinfo_t *files, const int *cnt, int *sel,
156 win_t *win)
157{
158 int len;
159 const char *homedir, *dsuffix = "";
160
161 if (tns == NULL)
162 return;
163
164 if (*cnt > 0) {
165 tns->thumbs = (thumb_t*) s_malloc(*cnt * sizeof(thumb_t));
166 memset(tns->thumbs, 0, *cnt * sizeof(thumb_t));
167 } else {
168 tns->thumbs = NULL;
169 }
170 tns->files = files;
171 tns->cnt = cnt;
172 tns->loadnext = tns->first = tns->end = tns->r_first = tns->r_end = 0;
173 tns->sel = sel;
174 tns->win = win;
175 tns->dirty = false;
176
177 tns->zl = 0;
178 tns_zoom(tns, 1);
179
180 if ((homedir = getenv("XDG_CACHE_HOME")) == NULL || homedir[0] == '\0') {
181 homedir = getenv("HOME");
182 dsuffix = "/.cache";
183 }
184 if (homedir != NULL) {
185 free(cache_dir);
186 len = strlen(homedir) + strlen(dsuffix) + 6;
187 cache_dir = (char*) s_malloc(len);
188 snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix);
189 } else {
190 warn("could not locate thumbnail cache directory");
191 }
192}
193
194void tns_free(tns_t *tns)
195{
196 int i;
197
198 if (tns == NULL)
199 return;
200
201 if (tns->thumbs != NULL) {
202 for (i = 0; i < *tns->cnt; i++) {
203 if (tns->thumbs[i].im != NULL) {
204 imlib_context_set_image(tns->thumbs[i].im);
205 imlib_free_image();
206 }
207 }
208 free(tns->thumbs);
209 tns->thumbs = NULL;
210 }
211
212 free(cache_dir);
213 cache_dir = NULL;
214}
215
216Imlib_Image tns_scale_down(Imlib_Image im, int dim)
217{
218 int w, h;
219 float z, zw, zh;
220
221 imlib_context_set_image(im);
222 w = imlib_image_get_width();
223 h = imlib_image_get_height();
224 zw = (float) dim / (float) w;
225 zh = (float) dim / (float) h;
226 z = MIN(zw, zh);
227 z = MIN(z, 1.0);
228
229 if (z < 1.0) {
230 imlib_context_set_anti_alias(1);
231 im = imlib_create_cropped_scaled_image(0, 0, w, h,
232 MAX(z * w, 1), MAX(z * h, 1));
233 if (im == NULL)
234 die("could not allocate memory");
235 imlib_free_image_and_decache();
236 }
237 return im;
238}
239
240bool tns_load(tns_t *tns, int n, bool force)
241{
242 int w, h;
243 bool cache_hit = false;
244 float zw, zh;
245 thumb_t *t;
246 Imlib_Image im = NULL;
247 const fileinfo_t *file;
248
249 if (tns == NULL || tns->thumbs == NULL)
250 return false;
251 if (n < 0 || n >= *tns->cnt)
252 return false;
253 file = &tns->files[n];
254 if (file->name == NULL || file->path == NULL)
255 return false;
256
257 t = &tns->thumbs[n];
258
259 if (t->im != NULL) {
260 imlib_context_set_image(t->im);
261 imlib_free_image();
262 }
263
264 if (!force && (im = tns_cache_load(file->path, &force)) != NULL) {
265 cache_hit = true;
266 } else {
267#if HAVE_LIBEXIF
268 if (!force) {
269 int pw = 0, ph = 0, x = 0, y = 0;
270 bool err;
271 ExifData *ed;
272 ExifEntry *entry;
273 ExifContent *ifd;
274 ExifByteOrder byte_order;
275 int tmpfd;
276 char tmppath[] = "/tmp/sxiv-XXXXXX";
277 Imlib_Image tmpim;
278
279 if ((ed = exif_data_new_from_file(file->path)) != NULL &&
280 ed->data != NULL && ed->size > 0)
281 {
282 if ((tmpfd = mkstemp(tmppath)) >= 0) {
283 err = write(tmpfd, ed->data, ed->size) != ed->size;
284 close(tmpfd);
285
286 if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) {
287 byte_order = exif_data_get_byte_order(ed);
288 ifd = ed->ifd[EXIF_IFD_EXIF];
289 entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION);
290 if (entry != NULL)
291 pw = exif_get_long(entry->data, byte_order);
292 entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION);
293 if (entry != NULL)
294 ph = exif_get_long(entry->data, byte_order);
295
296 imlib_context_set_image(tmpim);
297 w = imlib_image_get_width();
298 h = imlib_image_get_height();
299
300 if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) {
301 zw = (float) pw / (float) w;
302 zh = (float) ph / (float) h;
303 if (zw < zh) {
304 pw /= zh;
305 x = (w - pw) / 2;
306 w = pw;
307 } else if (zw > zh) {
308 ph /= zw;
309 y = (h - ph) / 2;
310 h = ph;
311 }
312 }
313 if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL)
314 die("could not allocate memory");
315 imlib_free_image_and_decache();
316 }
317 unlink(tmppath);
318 }
319 exif_data_unref(ed);
320 }
321 }
322#endif
323 if (im == NULL && (access(file->path, R_OK) < 0 ||
324 (im = imlib_load_image(file->path)) == NULL))
325 {
326 if (file->warn)
327 warn("could not open image: %s", file->name);
328 return false;
329 }
330 }
331
332 if (!cache_hit) {
333#if HAVE_LIBEXIF
334 imlib_context_set_image(im);
335 exif_auto_orientate(file);
336#endif
337 im = tns_scale_down(im, thumb_sizes[ARRLEN(thumb_sizes)-1]);
338 tns_cache_write(im, file->path, true);
339 }
340
341 t->im = tns_scale_down(im, thumb_sizes[tns->zl]);
342 imlib_context_set_image(t->im);
343 t->w = imlib_image_get_width();
344 t->h = imlib_image_get_height();
345
346 tns->dirty = true;
347 return true;
348}
349
350void tns_unload(tns_t *tns, int n)
351{
352 thumb_t *t;
353
354 if (tns == NULL || tns->thumbs == NULL)
355 return;
356 if (n < 0 || n >= *tns->cnt)
357 return;
358
359 t = &tns->thumbs[n];
360
361 if (t->im != NULL) {
362 imlib_context_set_image(t->im);
363 imlib_free_image();
364 t->im = NULL;
365 }
366}
367
368void tns_check_view(tns_t *tns, bool scrolled)
369{
370 int r;
371
372 if (tns == NULL)
373 return;
374
375 tns->first -= tns->first % tns->cols;
376 r = *tns->sel % tns->cols;
377
378 if (scrolled) {
379 /* move selection into visible area */
380 if (*tns->sel >= tns->first + tns->cols * tns->rows)
381 *tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
382 else if (*tns->sel < tns->first)
383 *tns->sel = tns->first + r;
384 } else {
385 /* scroll to selection */
386 if (tns->first + tns->cols * tns->rows <= *tns->sel) {
387 tns->first = *tns->sel - r - tns->cols * (tns->rows - 1);
388 tns->dirty = true;
389 } else if (tns->first > *tns->sel) {
390 tns->first = *tns->sel - r;
391 tns->dirty = true;
392 }
393 }
394}
395
396void tns_render(tns_t *tns)
397{
398 thumb_t *t;
399 win_t *win;
400 int i, cnt, r, x, y;
401
402 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
403 return;
404 if (!tns->dirty)
405 return;
406
407 win = tns->win;
408 win_clear(win);
409 imlib_context_set_drawable(win->buf.pm);
410
411 tns->cols = MAX(1, win->w / tns->dim);
412 tns->rows = MAX(1, win->h / tns->dim);
413
414 if (*tns->cnt < tns->cols * tns->rows) {
415 tns->first = 0;
416 cnt = *tns->cnt;
417 } else {
418 tns_check_view(tns, false);
419 cnt = tns->cols * tns->rows;
420 if ((r = tns->first + cnt - *tns->cnt) >= tns->cols)
421 tns->first -= r - r % tns->cols;
422 if (r > 0)
423 cnt -= r % tns->cols;
424 }
425 r = cnt % tns->cols ? 1 : 0;
426 tns->x = x = (win->w - MIN(cnt, tns->cols) * tns->dim) / 2 + tns->bw + 3;
427 tns->y = y = (win->h - (cnt / tns->cols + r) * tns->dim) / 2 + tns->bw + 3;
428 tns->loadnext = *tns->cnt;
429 tns->end = tns->first + cnt;
430
431 for (i = tns->r_first; i < tns->r_end; i++) {
432 if ((i < tns->first || i >= tns->end) && tns->thumbs[i].im != NULL)
433 tns_unload(tns, i);
434 }
435 tns->r_first = tns->first;
436 tns->r_end = tns->end;
437
438 for (i = tns->first; i < tns->end; i++) {
439 t = &tns->thumbs[i];
440 if (t->im != NULL) {
441 t->x = x + (thumb_sizes[tns->zl] - t->w) / 2;
442 t->y = y + (thumb_sizes[tns->zl] - t->h) / 2;
443 imlib_context_set_image(t->im);
444 imlib_render_image_on_drawable_at_size(t->x, t->y, t->w, t->h);
445 if (tns->files[i].marked)
446 tns_mark(tns, i, true);
447 } else {
448 tns->loadnext = MIN(tns->loadnext, i);
449 }
450 if ((i + 1) % tns->cols == 0) {
451 x = tns->x;
452 y += tns->dim;
453 } else {
454 x += tns->dim;
455 }
456 }
457 tns->dirty = false;
458 tns_highlight(tns, *tns->sel, true);
459}
460
461void tns_mark(tns_t *tns, int n, bool mark)
462{
463 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
464 return;
465
466 if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
467 win_t *win = tns->win;
468 thumb_t *t = &tns->thumbs[n];
469 unsigned long col = win->fullscreen ? win->fscol : win->bgcol;
470 int x = t->x + t->w, y = t->y + t->h;
471
472 win_draw_rect(win, x - 1, y + 1, 1, tns->bw, true, 1, col);
473 win_draw_rect(win, x + 1, y - 1, tns->bw, 1, true, 1, col);
474
475 if (mark)
476 col = win->selcol;
477
478 win_draw_rect(win, x, y, tns->bw + 2, tns->bw + 2, true, 1, col);
479
480 if (!mark && n == *tns->sel)
481 tns_highlight(tns, n, true);
482 }
483}
484
485void tns_highlight(tns_t *tns, int n, bool hl)
486{
487 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
488 return;
489
490 if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
491 win_t *win = tns->win;
492 thumb_t *t = &tns->thumbs[n];
493 unsigned long col;
494 int oxy = (tns->bw + 1) / 2 + 1, owh = tns->bw + 2;
495
496 if (hl)
497 col = win->selcol;
498 else
499 col = win->fullscreen ? win->fscol : win->bgcol;
500
501 win_draw_rect(win, t->x - oxy, t->y - oxy, t->w + owh, t->h + owh,
502 false, tns->bw, col);
503
504 if (tns->files[n].marked)
505 tns_mark(tns, n, true);
506 }
507}
508
509bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
510{
511 int old, max;
512
513 if (tns == NULL || tns->thumbs == NULL)
514 return false;
515
516 old = *tns->sel;
517 cnt = cnt > 1 ? cnt : 1;
518
519 switch (dir) {
520 case DIR_UP:
521 *tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols);
522 break;
523 case DIR_DOWN:
524 max = tns->cols * ((*tns->cnt - 1) / tns->cols) +
525 MIN((*tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
526 *tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
527 break;
528 case DIR_LEFT:
529 *tns->sel = MAX(*tns->sel - cnt, 0);
530 break;
531 case DIR_RIGHT:
532 *tns->sel = MIN(*tns->sel + cnt, *tns->cnt - 1);
533 break;
534 }
535
536 if (*tns->sel != old) {
537 tns_highlight(tns, old, false);
538 tns_check_view(tns, false);
539 if (!tns->dirty)
540 tns_highlight(tns, *tns->sel, true);
541 }
542 return *tns->sel != old;
543}
544
545bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
546{
547 int d, max, old;
548
549 if (tns == NULL)
550 return false;
551
552 old = tns->first;
553 d = tns->cols * (screen ? tns->rows : 1);
554
555 if (dir == DIR_DOWN) {
556 max = *tns->cnt - tns->cols * tns->rows;
557 if (*tns->cnt % tns->cols != 0)
558 max += tns->cols - *tns->cnt % tns->cols;
559 tns->first = MIN(tns->first + d, max);
560 } else if (dir == DIR_UP) {
561 tns->first = MAX(tns->first - d, 0);
562 }
563
564 if (tns->first != old) {
565 tns_check_view(tns, true);
566 tns->dirty = true;
567 }
568 return tns->first != old;
569}
570
571bool tns_zoom(tns_t *tns, int d)
572{
573 int i, oldzl;
574
575 if (tns == NULL || tns->thumbs == NULL)
576 return false;
577
578 oldzl = tns->zl;
579 tns->zl += -(d < 0) + (d > 0);
580 tns->zl = MAX(tns->zl, 0);
581 tns->zl = MIN(tns->zl, ARRLEN(thumb_sizes)-1);
582
583 tns->bw = ((thumb_sizes[tns->zl] - 1) >> 5) + 1;
584 tns->dim = thumb_sizes[tns->zl] + 2 * tns->bw + 6;
585
586 if (tns->zl != oldzl) {
587 for (i = 0; i < *tns->cnt; i++)
588 tns_unload(tns, i);
589 tns->dirty = true;
590 }
591 return tns->zl != oldzl;
592}
593
594int tns_translate(tns_t *tns, int x, int y)
595{
596 int n;
597
598 if (tns == NULL || tns->thumbs == NULL)
599 return -1;
600 if (x < tns->x || y < tns->y)
601 return -1;
602
603 n = tns->first + (y - tns->y) / tns->dim * tns->cols +
604 (x - tns->x) / tns->dim;
605 if (n >= *tns->cnt)
606 n = -1;
607
608 return n;
609}