Krolyxon
Commit 4cf17d2

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 INCLUDE_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) < 0)
96 goto end;
97 *dirend = '/';
98 }
99 imlib_context_set_image(im);
100 if (imlib_image_has_alpha()) {
101 imlib_image_set_format("png");
102 } else {
103 imlib_image_set_format("jpg");
104 imlib_image_attach_data_value("quality", NULL, 90, NULL);
105 }
106 imlib_save_image_with_error_return(cfile, &err);
107 if (err)
108 goto end;
109 times.actime = fstats.st_atime;
110 times.modtime = fstats.st_mtime;
111 utime(cfile, &times);
112 }
113end:
114 free(cfile);
115 }
116}
117
118void tns_clean_cache(void)
119{
120 int dirlen;
121 char *cfile, *filename;
122 r_dir_t dir;
123
124 if (r_opendir(&dir, cache_dir, true) < 0) {
125 error(0, errno, "%s", cache_dir);
126 return;
127 }
128
129 dirlen = strlen(cache_dir);
130
131 while ((cfile = r_readdir(&dir, false)) != NULL) {
132 filename = cfile + dirlen;
133 if (access(filename, F_OK) < 0) {
134 if (unlink(cfile) < 0)
135 error(0, errno, "%s", cfile);
136 }
137 free(cfile);
138 }
139 r_closedir(&dir);
140}
141
142void tns_init(tns_t *tns, fileinfo_t *tns_files, const int *cnt, int *sel, win_t *win)
143{
144 int len;
145 const char *homedir, *dsuffix = "";
146
147 if (cnt != NULL && *cnt > 0)
148 tns->thumbs = ecalloc(*cnt, sizeof(thumb_t));
149 else
150 tns->thumbs = NULL;
151 tns->files = tns_files;
152 tns->cnt = cnt;
153 tns->initnext = tns->loadnext = 0;
154 tns->first = tns->end = tns->r_first = tns->r_end = 0;
155 tns->sel = sel;
156 tns->win = win;
157 tns->dirty = false;
158
159 tns->zl = THUMB_SIZE;
160 tns_zoom(tns, 0);
161
162 if ((homedir = getenv("XDG_CACHE_HOME")) == NULL || homedir[0] == '\0') {
163 homedir = getenv("HOME");
164 dsuffix = "/.cache";
165 }
166 if (homedir != NULL) {
167 const char *s = "/nsxiv";
168 free(cache_dir);
169 len = strlen(homedir) + strlen(dsuffix) + strlen(s) + 1;
170 cache_dir = emalloc(len);
171 snprintf(cache_dir, len, "%s%s%s", homedir, dsuffix, s);
172 } else {
173 error(0, 0, "Cache directory not found");
174 }
175}
176
177CLEANUP void tns_free(tns_t *tns)
178{
179 int i;
180
181 if (tns->thumbs != NULL) {
182 for (i = 0; i < *tns->cnt; i++) {
183 if (tns->thumbs[i].im != NULL) {
184 imlib_context_set_image(tns->thumbs[i].im);
185 imlib_free_image();
186 }
187 }
188 free(tns->thumbs);
189 tns->thumbs = NULL;
190 }
191
192 free(cache_dir);
193 cache_dir = NULL;
194}
195
196static Imlib_Image tns_scale_down(Imlib_Image im, int dim)
197{
198 int w, h;
199 float z, zw, zh;
200
201 imlib_context_set_image(im);
202 w = imlib_image_get_width();
203 h = imlib_image_get_height();
204 zw = (float) dim / (float) w;
205 zh = (float) dim / (float) h;
206 z = MIN(zw, zh);
207 z = MIN(z, 1.0);
208
209 if (z < 1.0) {
210 imlib_context_set_anti_alias(1);
211 im = imlib_create_cropped_scaled_image(0, 0, w, h,
212 MAX(z * w, 1), MAX(z * h, 1));
213 if (im == NULL)
214 error(EXIT_FAILURE, ENOMEM, NULL);
215 imlib_free_image_and_decache();
216 }
217 return im;
218}
219
220bool tns_load(tns_t *tns, int n, bool force, bool cache_only)
221{
222 int maxwh = thumb_sizes[ARRLEN(thumb_sizes)-1];
223 bool cache_hit = false;
224 char *cfile;
225 thumb_t *t;
226 fileinfo_t *file;
227 Imlib_Image im = NULL;
228
229 if (n < 0 || n >= *tns->cnt)
230 return false;
231 file = &tns->files[n];
232 if (file->name == NULL || file->path == NULL)
233 return false;
234
235 t = &tns->thumbs[n];
236
237 if (t->im != NULL) {
238 imlib_context_set_image(t->im);
239 imlib_free_image();
240 t->im = NULL;
241 }
242
243 if (!force) {
244 if ((im = tns_cache_load(file->path, &force)) != NULL) {
245 imlib_context_set_image(im);
246 if (imlib_image_get_width() < maxwh &&
247 imlib_image_get_height() < maxwh)
248 {
249 if ((cfile = tns_cache_filepath(file->path)) != NULL) {
250 unlink(cfile);
251 free(cfile);
252 }
253 imlib_free_image_and_decache();
254 im = NULL;
255 } else {
256 cache_hit = true;
257 }
258#if HAVE_LIBEXIF
259 } else if (!force && !options->private_mode) {
260 int pw = 0, ph = 0, w, h, x = 0, y = 0;
261 bool err;
262 float zw, zh;
263 ExifData *ed;
264 ExifEntry *entry;
265 ExifContent *ifd;
266 ExifByteOrder byte_order;
267 int tmpfd;
268 char tmppath[] = "/tmp/nsxiv-XXXXXX";
269 Imlib_Image tmpim;
270
271 if ((ed = exif_data_new_from_file(file->path)) != NULL) {
272 if (ed->data != NULL && ed->size > 0 &&
273 (tmpfd = mkstemp(tmppath)) >= 0)
274 {
275 err = write(tmpfd, ed->data, ed->size) != ed->size;
276 close(tmpfd);
277
278 if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) {
279 byte_order = exif_data_get_byte_order(ed);
280 ifd = ed->ifd[EXIF_IFD_EXIF];
281 entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION);
282 if (entry != NULL)
283 pw = exif_get_long(entry->data, byte_order);
284 entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION);
285 if (entry != NULL)
286 ph = exif_get_long(entry->data, byte_order);
287
288 imlib_context_set_image(tmpim);
289 w = imlib_image_get_width();
290 h = imlib_image_get_height();
291
292 if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) {
293 zw = (float) pw / (float) w;
294 zh = (float) ph / (float) h;
295 if (zw < zh) {
296 pw /= zh;
297 x = (w - pw) / 2;
298 w = pw;
299 } else if (zw > zh) {
300 ph /= zw;
301 y = (h - ph) / 2;
302 h = ph;
303 }
304 }
305 if (w >= maxwh || h >= maxwh) {
306 if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL)
307 error(0, 0, "%s: error generating thumbnail", file->name);
308 }
309 imlib_free_image_and_decache();
310 }
311 unlink(tmppath);
312 }
313 exif_data_unref(ed);
314 }
315#endif /* HAVE_LIBEXIF */
316 }
317 }
318
319 if (im == NULL) {
320 if ((im = img_open(file)) == NULL)
321 return false;
322 }
323 imlib_context_set_image(im);
324
325 if (!cache_hit) {
326#if HAVE_LIBEXIF
327 exif_auto_orientate(file);
328#endif
329 im = tns_scale_down(im, maxwh);
330 imlib_context_set_image(im);
331 if (imlib_image_get_width() == maxwh || imlib_image_get_height() == maxwh)
332 tns_cache_write(im, file->path, true);
333 }
334
335 if (cache_only) {
336 imlib_free_image_and_decache();
337 } else {
338 t->im = tns_scale_down(im, thumb_sizes[tns->zl]);
339 imlib_context_set_image(t->im);
340 t->w = imlib_image_get_width();
341 t->h = imlib_image_get_height();
342 tns->dirty = true;
343 }
344 file->flags |= FF_TN_INIT;
345
346 if (n == tns->initnext)
347 while (++tns->initnext < *tns->cnt && ((++file)->flags & FF_TN_INIT));
348 if (n == tns->loadnext && !cache_only)
349 while (++tns->loadnext < tns->end && (++t)->im != NULL);
350
351 return true;
352}
353
354void tns_unload(tns_t *tns, int n)
355{
356 thumb_t *t;
357
358 if (n < 0 || n >= *tns->cnt)
359 return;
360
361 t = &tns->thumbs[n];
362
363 if (t->im != NULL) {
364 imlib_context_set_image(t->im);
365 imlib_free_image();
366 t->im = NULL;
367 }
368}
369
370static void tns_check_view(tns_t *tns, bool scrolled)
371{
372 int r;
373
374 if (tns == NULL)
375 return;
376
377 tns->first -= tns->first % tns->cols;
378 r = *tns->sel % tns->cols;
379
380 if (scrolled) {
381 /* move selection into visible area */
382 if (*tns->sel >= tns->first + tns->cols * tns->rows)
383 *tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
384 else if (*tns->sel < tns->first)
385 *tns->sel = tns->first + r;
386 } else {
387 /* scroll to selection */
388 if (tns->first + tns->cols * tns->rows <= *tns->sel) {
389 tns->first = *tns->sel - r - tns->cols * (tns->rows - 1);
390 tns->dirty = true;
391 } else if (tns->first > *tns->sel) {
392 tns->first = *tns->sel - r;
393 tns->dirty = true;
394 }
395 }
396}
397
398void tns_render(tns_t *tns)
399{
400 thumb_t *t;
401 win_t *win;
402 int i, cnt, r, x, y;
403
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 (win->bar.top ? win->bar.h : 0);
429 tns->loadnext = *tns->cnt;
430 tns->end = tns->first + cnt;
431
432 for (i = tns->r_first; i < tns->r_end; i++) {
433 if ((i < tns->first || i >= tns->end) && tns->thumbs[i].im != NULL)
434 tns_unload(tns, i);
435 }
436 tns->r_first = tns->first;
437 tns->r_end = tns->end;
438
439 for (i = tns->first; i < tns->end; i++) {
440 t = &tns->thumbs[i];
441 if (t->im != NULL) {
442 t->x = x + (thumb_sizes[tns->zl] - t->w) / 2;
443 t->y = y + (thumb_sizes[tns->zl] - t->h) / 2;
444 imlib_context_set_image(t->im);
445 imlib_render_image_on_drawable_at_size(t->x, t->y, t->w, t->h);
446 if (tns->files[i].flags & FF_MARK)
447 tns_mark(tns, i, true);
448 } else {
449 tns->loadnext = MIN(tns->loadnext, i);
450 }
451 if ((i + 1) % tns->cols == 0) {
452 x = tns->x;
453 y += tns->dim;
454 } else {
455 x += tns->dim;
456 }
457 }
458 tns->dirty = false;
459 tns_highlight(tns, *tns->sel, true);
460 title_dirty = true;
461}
462
463void tns_mark(tns_t *tns, int n, bool mark)
464{
465 if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
466 win_t *win = tns->win;
467 thumb_t *t = &tns->thumbs[n];
468 unsigned long col = win->win_bg.pixel;
469 int x = t->x + t->w, y = t->y + t->h;
470
471 win_draw_rect(win, x - 1, y + 1, 1, tns->bw, true, 1, col);
472 win_draw_rect(win, x + 1, y - 1, tns->bw, 1, true, 1, col);
473
474 if (mark)
475 col = win->mrk_fg.pixel;
476
477 win_draw_rect(win, x, y, tns->bw + 2, tns->bw + 2, true, 1, col);
478
479 if (!mark && n == *tns->sel)
480 tns_highlight(tns, n, true);
481 }
482}
483
484void tns_highlight(tns_t *tns, int n, bool hl)
485{
486 if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
487 win_t *win = tns->win;
488 thumb_t *t = &tns->thumbs[n];
489 unsigned long col = hl ? win->win_fg.pixel : win->win_bg.pixel;
490 int oxy = (tns->bw + 1) / 2 + 1, owh = tns->bw + 2;
491
492 win_draw_rect(win, t->x - oxy, t->y - oxy, t->w + owh, t->h + owh,
493 false, tns->bw, col);
494
495 if (tns->files[n].flags & FF_MARK)
496 tns_mark(tns, n, true);
497 }
498}
499
500bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
501{
502 int old, max;
503
504 old = *tns->sel;
505 cnt = cnt > 1 ? cnt : 1;
506
507 switch (dir) {
508 case DIR_UP:
509 *tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols);
510 break;
511 case DIR_DOWN:
512 max = tns->cols * ((*tns->cnt - 1) / tns->cols) +
513 MIN((*tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
514 *tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
515 break;
516 case DIR_LEFT:
517 *tns->sel = MAX(*tns->sel - cnt, 0);
518 break;
519 case DIR_RIGHT:
520 *tns->sel = MIN(*tns->sel + cnt, *tns->cnt - 1);
521 break;
522 }
523
524 if (*tns->sel != old) {
525 tns_highlight(tns, old, false);
526 tns_check_view(tns, false);
527 if (!tns->dirty)
528 tns_highlight(tns, *tns->sel, true);
529 title_dirty = 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}