Krolyxon
Commit 919ada1

Branches

Refs →

nsxivthumbs.c

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