Krolyxon
Commit 1439b84

Branches

Refs →

nsxivthumbs.c

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