Krolyxon
Commit a3838c1

Branches

Refs →

nsxivthumbs.c

645 lines · 14.6 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) + 2;
52 cfile = (char*) s_malloc(len);
53 snprintf(cfile, len, "%s/%s", 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 if (imlib_image_has_alpha()) {
105 imlib_image_set_format("png");
106 } else {
107 imlib_image_set_format("jpg");
108 imlib_image_attach_data_value("quality", NULL, 90, NULL);
109 }
110 imlib_save_image_with_error_return(cfile, &err);
111 }
112 if (err == 0) {
113 times.actime = fstats.st_atime;
114 times.modtime = fstats.st_mtime;
115 utime(cfile, &times);
116 }
117 }
118 free(cfile);
119 }
120}
121
122void tns_clean_cache(tns_t *tns)
123{
124 int dirlen;
125 bool delete;
126 char *cfile, *filename, *tpos;
127 r_dir_t dir;
128
129 if (cache_dir == NULL)
130 return;
131
132 if (r_opendir(&dir, cache_dir) < 0) {
133 warn("could not open thumbnail cache directory: %s", cache_dir);
134 return;
135 }
136
137 dirlen = strlen(cache_dir);
138
139 while ((cfile = r_readdir(&dir)) != NULL) {
140 filename = cfile + dirlen;
141 delete = false;
142
143 if ((tpos = strrchr(filename, '.')) != NULL) {
144 *tpos = '\0';
145 if (access(filename, F_OK) < 0)
146 delete = true;
147 *tpos = '.';
148 }
149 if (delete) {
150 if (unlink(cfile) < 0)
151 warn("could not delete cache file: %s", cfile);
152 }
153 free(cfile);
154 }
155 r_closedir(&dir);
156}
157
158
159void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel,
160 win_t *win)
161{
162 int len;
163 const char *homedir, *dsuffix = "";
164
165 if (tns == NULL)
166 return;
167
168 if (cnt != NULL && *cnt > 0) {
169 tns->thumbs = (thumb_t*) s_malloc(*cnt * sizeof(thumb_t));
170 memset(tns->thumbs, 0, *cnt * sizeof(thumb_t));
171 } else {
172 tns->thumbs = NULL;
173 }
174 tns->files = files;
175 tns->cnt = cnt;
176 tns->initnext = tns->loadnext = 0;
177 tns->first = tns->end = tns->r_first = tns->r_end = 0;
178 tns->sel = sel;
179 tns->win = win;
180 tns->dirty = false;
181
182 tns->zl = 0;
183 tns_zoom(tns, 1);
184
185 if ((homedir = getenv("XDG_CACHE_HOME")) == NULL || homedir[0] == '\0') {
186 homedir = getenv("HOME");
187 dsuffix = "/.cache";
188 }
189 if (homedir != NULL) {
190 free(cache_dir);
191 len = strlen(homedir) + strlen(dsuffix) + 6;
192 cache_dir = (char*) s_malloc(len);
193 snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix);
194 } else {
195 warn("could not locate thumbnail cache directory");
196 }
197}
198
199void tns_free(tns_t *tns)
200{
201 int i;
202
203 if (tns == NULL)
204 return;
205
206 if (tns->thumbs != NULL) {
207 for (i = 0; i < *tns->cnt; i++) {
208 if (tns->thumbs[i].im != NULL) {
209 imlib_context_set_image(tns->thumbs[i].im);
210 imlib_free_image();
211 }
212 }
213 free(tns->thumbs);
214 tns->thumbs = NULL;
215 }
216
217 free(cache_dir);
218 cache_dir = NULL;
219}
220
221Imlib_Image tns_scale_down(Imlib_Image im, int dim)
222{
223 int w, h;
224 float z, zw, zh;
225
226 imlib_context_set_image(im);
227 w = imlib_image_get_width();
228 h = imlib_image_get_height();
229 zw = (float) dim / (float) w;
230 zh = (float) dim / (float) h;
231 z = MIN(zw, zh);
232 z = MIN(z, 1.0);
233
234 if (z < 1.0) {
235 imlib_context_set_anti_alias(1);
236 im = imlib_create_cropped_scaled_image(0, 0, w, h,
237 MAX(z * w, 1), MAX(z * h, 1));
238 if (im == NULL)
239 die("could not allocate memory");
240 imlib_free_image_and_decache();
241 }
242 return im;
243}
244
245bool tns_load(tns_t *tns, int n, bool force, bool cache_only)
246{
247 int w, h;
248 int maxwh = thumb_sizes[ARRLEN(thumb_sizes)-1];
249 bool cache_hit = false;
250 char *cfile;
251 float zw, zh;
252 thumb_t *t;
253 fileinfo_t *file;
254 Imlib_Image im = NULL;
255
256 if (tns == NULL || tns->thumbs == NULL)
257 return false;
258 if (n < 0 || n >= *tns->cnt)
259 return false;
260 file = &tns->files[n];
261 if (file->name == NULL || file->path == NULL)
262 return false;
263
264 t = &tns->thumbs[n];
265
266 if (t->im != NULL) {
267 imlib_context_set_image(t->im);
268 imlib_free_image();
269 t->im = NULL;
270 }
271
272 if (!force) {
273 if ((im = tns_cache_load(file->path, &force)) != NULL) {
274 imlib_context_set_image(im);
275 if (imlib_image_get_width() < maxwh &&
276 imlib_image_get_height() < maxwh)
277 {
278 if ((cfile = tns_cache_filepath(file->path)) != NULL) {
279 unlink(cfile);
280 free(cfile);
281 }
282 imlib_free_image_and_decache();
283 im = NULL;
284 } else {
285 cache_hit = true;
286 }
287#if HAVE_LIBEXIF
288 } else if (!force) {
289 int pw = 0, ph = 0, x = 0, y = 0;
290 bool err;
291 ExifData *ed;
292 ExifEntry *entry;
293 ExifContent *ifd;
294 ExifByteOrder byte_order;
295 int tmpfd;
296 char tmppath[] = "/tmp/sxiv-XXXXXX";
297 Imlib_Image tmpim;
298
299 if ((ed = exif_data_new_from_file(file->path)) != NULL) {
300 if (ed->data != NULL && ed->size > 0 &&
301 (tmpfd = mkstemp(tmppath)) >= 0)
302 {
303 err = write(tmpfd, ed->data, ed->size) != ed->size;
304 close(tmpfd);
305
306 if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) {
307 byte_order = exif_data_get_byte_order(ed);
308 ifd = ed->ifd[EXIF_IFD_EXIF];
309 entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION);
310 if (entry != NULL)
311 pw = exif_get_long(entry->data, byte_order);
312 entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION);
313 if (entry != NULL)
314 ph = exif_get_long(entry->data, byte_order);
315
316 imlib_context_set_image(tmpim);
317 w = imlib_image_get_width();
318 h = imlib_image_get_height();
319
320 if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) {
321 zw = (float) pw / (float) w;
322 zh = (float) ph / (float) h;
323 if (zw < zh) {
324 pw /= zh;
325 x = (w - pw) / 2;
326 w = pw;
327 } else if (zw > zh) {
328 ph /= zw;
329 y = (h - ph) / 2;
330 h = ph;
331 }
332 }
333 if (w >= maxwh || h >= maxwh) {
334 if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL)
335 die("could not allocate memory");
336 }
337 imlib_free_image_and_decache();
338 }
339 unlink(tmppath);
340 }
341 exif_data_unref(ed);
342 }
343#endif
344 }
345 }
346
347 if (im == NULL && (access(file->path, R_OK) < 0 ||
348 (im = imlib_load_image(file->path)) == NULL))
349 {
350 if (file->flags & FF_WARN)
351 warn("could not open image: %s", file->name);
352 return false;
353 }
354 imlib_context_set_image(im);
355
356 if (!cache_hit) {
357#if HAVE_LIBEXIF
358 exif_auto_orientate(file);
359#endif
360 im = tns_scale_down(im, maxwh);
361 imlib_context_set_image(im);
362 if (imlib_image_get_width() == maxwh || imlib_image_get_height() == maxwh)
363 tns_cache_write(im, file->path, true);
364 }
365
366 if (cache_only) {
367 imlib_free_image_and_decache();
368 } else {
369 t->im = tns_scale_down(im, thumb_sizes[tns->zl]);
370 imlib_context_set_image(t->im);
371 t->w = imlib_image_get_width();
372 t->h = imlib_image_get_height();
373 tns->dirty = true;
374 }
375 file->flags |= FF_TN_INIT;
376
377 if (n == tns->initnext)
378 while (++tns->initnext < *tns->cnt && ((++file)->flags & FF_TN_INIT));
379 if (n == tns->loadnext && !cache_only)
380 while (++tns->loadnext < tns->end && (++t)->im != NULL);
381
382 return true;
383}
384
385void tns_unload(tns_t *tns, int n)
386{
387 thumb_t *t;
388
389 if (tns == NULL || tns->thumbs == NULL)
390 return;
391 if (n < 0 || n >= *tns->cnt)
392 return;
393
394 t = &tns->thumbs[n];
395
396 if (t->im != NULL) {
397 imlib_context_set_image(t->im);
398 imlib_free_image();
399 t->im = NULL;
400 }
401}
402
403void tns_check_view(tns_t *tns, bool scrolled)
404{
405 int r;
406
407 if (tns == NULL)
408 return;
409
410 tns->first -= tns->first % tns->cols;
411 r = *tns->sel % tns->cols;
412
413 if (scrolled) {
414 /* move selection into visible area */
415 if (*tns->sel >= tns->first + tns->cols * tns->rows)
416 *tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
417 else if (*tns->sel < tns->first)
418 *tns->sel = tns->first + r;
419 } else {
420 /* scroll to selection */
421 if (tns->first + tns->cols * tns->rows <= *tns->sel) {
422 tns->first = *tns->sel - r - tns->cols * (tns->rows - 1);
423 tns->dirty = true;
424 } else if (tns->first > *tns->sel) {
425 tns->first = *tns->sel - r;
426 tns->dirty = true;
427 }
428 }
429}
430
431void tns_render(tns_t *tns)
432{
433 thumb_t *t;
434 win_t *win;
435 int i, cnt, r, x, y;
436
437 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
438 return;
439 if (!tns->dirty)
440 return;
441
442 win = tns->win;
443 win_clear(win);
444 imlib_context_set_drawable(win->buf.pm);
445
446 tns->cols = MAX(1, win->w / tns->dim);
447 tns->rows = MAX(1, win->h / tns->dim);
448
449 if (*tns->cnt < tns->cols * tns->rows) {
450 tns->first = 0;
451 cnt = *tns->cnt;
452 } else {
453 tns_check_view(tns, false);
454 cnt = tns->cols * tns->rows;
455 if ((r = tns->first + cnt - *tns->cnt) >= tns->cols)
456 tns->first -= r - r % tns->cols;
457 if (r > 0)
458 cnt -= r % tns->cols;
459 }
460 r = cnt % tns->cols ? 1 : 0;
461 tns->x = x = (win->w - MIN(cnt, tns->cols) * tns->dim) / 2 + tns->bw + 3;
462 tns->y = y = (win->h - (cnt / tns->cols + r) * tns->dim) / 2 + tns->bw + 3;
463 tns->loadnext = *tns->cnt;
464 tns->end = tns->first + cnt;
465
466 for (i = tns->r_first; i < tns->r_end; i++) {
467 if ((i < tns->first || i >= tns->end) && tns->thumbs[i].im != NULL)
468 tns_unload(tns, i);
469 }
470 tns->r_first = tns->first;
471 tns->r_end = tns->end;
472
473 for (i = tns->first; i < tns->end; i++) {
474 t = &tns->thumbs[i];
475 if (t->im != NULL) {
476 t->x = x + (thumb_sizes[tns->zl] - t->w) / 2;
477 t->y = y + (thumb_sizes[tns->zl] - t->h) / 2;
478 imlib_context_set_image(t->im);
479 imlib_render_image_on_drawable_at_size(t->x, t->y, t->w, t->h);
480 if (tns->files[i].flags & FF_MARK)
481 tns_mark(tns, i, true);
482 } else {
483 tns->loadnext = MIN(tns->loadnext, i);
484 }
485 if ((i + 1) % tns->cols == 0) {
486 x = tns->x;
487 y += tns->dim;
488 } else {
489 x += tns->dim;
490 }
491 }
492 tns->dirty = false;
493 tns_highlight(tns, *tns->sel, true);
494}
495
496void tns_mark(tns_t *tns, int n, bool mark)
497{
498 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
499 return;
500
501 if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
502 win_t *win = tns->win;
503 thumb_t *t = &tns->thumbs[n];
504 unsigned long col = win->fullscreen ? win->fscol : win->bgcol;
505 int x = t->x + t->w, y = t->y + t->h;
506
507 win_draw_rect(win, x - 1, y + 1, 1, tns->bw, true, 1, col);
508 win_draw_rect(win, x + 1, y - 1, tns->bw, 1, true, 1, col);
509
510 if (mark)
511 col = win->selcol;
512
513 win_draw_rect(win, x, y, tns->bw + 2, tns->bw + 2, true, 1, col);
514
515 if (!mark && n == *tns->sel)
516 tns_highlight(tns, n, true);
517 }
518}
519
520void tns_highlight(tns_t *tns, int n, bool hl)
521{
522 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
523 return;
524
525 if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
526 win_t *win = tns->win;
527 thumb_t *t = &tns->thumbs[n];
528 unsigned long col;
529 int oxy = (tns->bw + 1) / 2 + 1, owh = tns->bw + 2;
530
531 if (hl)
532 col = win->selcol;
533 else
534 col = win->fullscreen ? win->fscol : win->bgcol;
535
536 win_draw_rect(win, t->x - oxy, t->y - oxy, t->w + owh, t->h + owh,
537 false, tns->bw, col);
538
539 if (tns->files[n].flags & FF_MARK)
540 tns_mark(tns, n, true);
541 }
542}
543
544bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
545{
546 int old, max;
547
548 if (tns == NULL || tns->thumbs == NULL)
549 return false;
550
551 old = *tns->sel;
552 cnt = cnt > 1 ? cnt : 1;
553
554 switch (dir) {
555 case DIR_UP:
556 *tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols);
557 break;
558 case DIR_DOWN:
559 max = tns->cols * ((*tns->cnt - 1) / tns->cols) +
560 MIN((*tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
561 *tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
562 break;
563 case DIR_LEFT:
564 *tns->sel = MAX(*tns->sel - cnt, 0);
565 break;
566 case DIR_RIGHT:
567 *tns->sel = MIN(*tns->sel + cnt, *tns->cnt - 1);
568 break;
569 }
570
571 if (*tns->sel != old) {
572 tns_highlight(tns, old, false);
573 tns_check_view(tns, false);
574 if (!tns->dirty)
575 tns_highlight(tns, *tns->sel, true);
576 }
577 return *tns->sel != old;
578}
579
580bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
581{
582 int d, max, old;
583
584 if (tns == NULL)
585 return false;
586
587 old = tns->first;
588 d = tns->cols * (screen ? tns->rows : 1);
589
590 if (dir == DIR_DOWN) {
591 max = *tns->cnt - tns->cols * tns->rows;
592 if (*tns->cnt % tns->cols != 0)
593 max += tns->cols - *tns->cnt % tns->cols;
594 tns->first = MIN(tns->first + d, max);
595 } else if (dir == DIR_UP) {
596 tns->first = MAX(tns->first - d, 0);
597 }
598
599 if (tns->first != old) {
600 tns_check_view(tns, true);
601 tns->dirty = true;
602 }
603 return tns->first != old;
604}
605
606bool tns_zoom(tns_t *tns, int d)
607{
608 int i, oldzl;
609
610 if (tns == NULL || tns->thumbs == NULL)
611 return false;
612
613 oldzl = tns->zl;
614 tns->zl += -(d < 0) + (d > 0);
615 tns->zl = MAX(tns->zl, 0);
616 tns->zl = MIN(tns->zl, ARRLEN(thumb_sizes)-1);
617
618 tns->bw = ((thumb_sizes[tns->zl] - 1) >> 5) + 1;
619 tns->bw = MIN(tns->bw, 4);
620 tns->dim = thumb_sizes[tns->zl] + 2 * tns->bw + 6;
621
622 if (tns->zl != oldzl) {
623 for (i = 0; i < *tns->cnt; i++)
624 tns_unload(tns, i);
625 tns->dirty = true;
626 }
627 return tns->zl != oldzl;
628}
629
630int tns_translate(tns_t *tns, int x, int y)
631{
632 int n;
633
634 if (tns == NULL || tns->thumbs == NULL)
635 return -1;
636 if (x < tns->x || y < tns->y)
637 return -1;
638
639 n = tns->first + (y - tns->y) / tns->dim * tns->cols +
640 (x - tns->x) / tns->dim;
641 if (n >= *tns->cnt)
642 n = -1;
643
644 return n;
645}