Krolyxon
Commit 01ed483

Branches

Refs →

nsxivthumbs.c

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