Krolyxon
Commit ec47ecb

Branches

Refs →

nsxivthumbs.c

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