Krolyxon
Commit 727939b

Branches

Refs →

nsxivthumbs.c

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