Krolyxon
Commit b4268fb

Branches

Refs →

nsxivthumbs.c

591 lines · 13.6 KB · C

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