Krolyxon
Commit 591be8c

Branches

Refs →

nsxivthumbs.c

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