Krolyxon
Commit eb5feb4

Branches

Refs →

nsxivthumbs.c

618 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#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 float zw, zh;
246 thumb_t *t;
247 Imlib_Image im = NULL;
248 const fileinfo_t *file;
249
250 if (tns == NULL || tns->thumbs == NULL)
251 return false;
252 if (n < 0 || n >= *tns->cnt)
253 return false;
254 file = &tns->files[n];
255 if (file->name == NULL || file->path == NULL)
256 return false;
257
258 t = &tns->thumbs[n];
259
260 if (t->im != NULL) {
261 imlib_context_set_image(t->im);
262 imlib_free_image();
263 }
264
265 if (!force) {
266 if ((im = tns_cache_load(file->path, &force)) != NULL) {
267 cache_hit = true;
268#if HAVE_LIBEXIF
269 } else {
270 int pw = 0, ph = 0, x = 0, y = 0;
271 bool err;
272 ExifData *ed;
273 ExifEntry *entry;
274 ExifContent *ifd;
275 ExifByteOrder byte_order;
276 int tmpfd;
277 char tmppath[] = "/tmp/sxiv-XXXXXX";
278 Imlib_Image tmpim;
279
280 if ((ed = exif_data_new_from_file(file->path)) != NULL) {
281 if (ed->data != NULL && ed->size > 0 &&
282 (tmpfd = mkstemp(tmppath)) >= 0)
283 {
284 err = write(tmpfd, ed->data, ed->size) != ed->size;
285 close(tmpfd);
286
287 if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) {
288 byte_order = exif_data_get_byte_order(ed);
289 ifd = ed->ifd[EXIF_IFD_EXIF];
290 entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION);
291 if (entry != NULL)
292 pw = exif_get_long(entry->data, byte_order);
293 entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION);
294 if (entry != NULL)
295 ph = exif_get_long(entry->data, byte_order);
296
297 imlib_context_set_image(tmpim);
298 w = imlib_image_get_width();
299 h = imlib_image_get_height();
300
301 if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) {
302 zw = (float) pw / (float) w;
303 zh = (float) ph / (float) h;
304 if (zw < zh) {
305 pw /= zh;
306 x = (w - pw) / 2;
307 w = pw;
308 } else if (zw > zh) {
309 ph /= zw;
310 y = (h - ph) / 2;
311 h = ph;
312 }
313 }
314 if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL)
315 die("could not allocate memory");
316 imlib_free_image_and_decache();
317 }
318 unlink(tmppath);
319 }
320 exif_data_unref(ed);
321 }
322#endif
323 }
324 }
325 if (im != NULL) {
326 imlib_context_set_image(im);
327 if (imlib_image_get_width() < maxwh && imlib_image_get_height() < maxwh) {
328 imlib_free_image_and_decache();
329 im = NULL;
330 cache_hit = false;
331 }
332 }
333 if (im == NULL && (access(file->path, R_OK) < 0 ||
334 (im = imlib_load_image(file->path)) == NULL))
335 {
336 if (file->warn)
337 warn("could not open image: %s", file->name);
338 return false;
339 }
340
341 if (!cache_hit) {
342#if HAVE_LIBEXIF
343 imlib_context_set_image(im);
344 exif_auto_orientate(file);
345#endif
346 im = tns_scale_down(im, maxwh);
347 tns_cache_write(im, file->path, true);
348 }
349
350 t->im = tns_scale_down(im, thumb_sizes[tns->zl]);
351 imlib_context_set_image(t->im);
352 t->w = imlib_image_get_width();
353 t->h = imlib_image_get_height();
354
355 tns->dirty = true;
356 return true;
357}
358
359void tns_unload(tns_t *tns, int n)
360{
361 thumb_t *t;
362
363 if (tns == NULL || tns->thumbs == NULL)
364 return;
365 if (n < 0 || n >= *tns->cnt)
366 return;
367
368 t = &tns->thumbs[n];
369
370 if (t->im != NULL) {
371 imlib_context_set_image(t->im);
372 imlib_free_image();
373 t->im = NULL;
374 }
375}
376
377void tns_check_view(tns_t *tns, bool scrolled)
378{
379 int r;
380
381 if (tns == NULL)
382 return;
383
384 tns->first -= tns->first % tns->cols;
385 r = *tns->sel % tns->cols;
386
387 if (scrolled) {
388 /* move selection into visible area */
389 if (*tns->sel >= tns->first + tns->cols * tns->rows)
390 *tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
391 else if (*tns->sel < tns->first)
392 *tns->sel = tns->first + r;
393 } else {
394 /* scroll to selection */
395 if (tns->first + tns->cols * tns->rows <= *tns->sel) {
396 tns->first = *tns->sel - r - tns->cols * (tns->rows - 1);
397 tns->dirty = true;
398 } else if (tns->first > *tns->sel) {
399 tns->first = *tns->sel - r;
400 tns->dirty = true;
401 }
402 }
403}
404
405void tns_render(tns_t *tns)
406{
407 thumb_t *t;
408 win_t *win;
409 int i, cnt, r, x, y;
410
411 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
412 return;
413 if (!tns->dirty)
414 return;
415
416 win = tns->win;
417 win_clear(win);
418 imlib_context_set_drawable(win->buf.pm);
419
420 tns->cols = MAX(1, win->w / tns->dim);
421 tns->rows = MAX(1, win->h / tns->dim);
422
423 if (*tns->cnt < tns->cols * tns->rows) {
424 tns->first = 0;
425 cnt = *tns->cnt;
426 } else {
427 tns_check_view(tns, false);
428 cnt = tns->cols * tns->rows;
429 if ((r = tns->first + cnt - *tns->cnt) >= tns->cols)
430 tns->first -= r - r % tns->cols;
431 if (r > 0)
432 cnt -= r % tns->cols;
433 }
434 r = cnt % tns->cols ? 1 : 0;
435 tns->x = x = (win->w - MIN(cnt, tns->cols) * tns->dim) / 2 + tns->bw + 3;
436 tns->y = y = (win->h - (cnt / tns->cols + r) * tns->dim) / 2 + tns->bw + 3;
437 tns->loadnext = *tns->cnt;
438 tns->end = tns->first + cnt;
439
440 for (i = tns->r_first; i < tns->r_end; i++) {
441 if ((i < tns->first || i >= tns->end) && tns->thumbs[i].im != NULL)
442 tns_unload(tns, i);
443 }
444 tns->r_first = tns->first;
445 tns->r_end = tns->end;
446
447 for (i = tns->first; i < tns->end; i++) {
448 t = &tns->thumbs[i];
449 if (t->im != NULL) {
450 t->x = x + (thumb_sizes[tns->zl] - t->w) / 2;
451 t->y = y + (thumb_sizes[tns->zl] - t->h) / 2;
452 imlib_context_set_image(t->im);
453 imlib_render_image_on_drawable_at_size(t->x, t->y, t->w, t->h);
454 if (tns->files[i].marked)
455 tns_mark(tns, i, true);
456 } else {
457 tns->loadnext = MIN(tns->loadnext, i);
458 }
459 if ((i + 1) % tns->cols == 0) {
460 x = tns->x;
461 y += tns->dim;
462 } else {
463 x += tns->dim;
464 }
465 }
466 tns->dirty = false;
467 tns_highlight(tns, *tns->sel, true);
468}
469
470void tns_mark(tns_t *tns, int n, bool mark)
471{
472 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
473 return;
474
475 if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
476 win_t *win = tns->win;
477 thumb_t *t = &tns->thumbs[n];
478 unsigned long col = win->fullscreen ? win->fscol : win->bgcol;
479 int x = t->x + t->w, y = t->y + t->h;
480
481 win_draw_rect(win, x - 1, y + 1, 1, tns->bw, true, 1, col);
482 win_draw_rect(win, x + 1, y - 1, tns->bw, 1, true, 1, col);
483
484 if (mark)
485 col = win->selcol;
486
487 win_draw_rect(win, x, y, tns->bw + 2, tns->bw + 2, true, 1, col);
488
489 if (!mark && n == *tns->sel)
490 tns_highlight(tns, n, true);
491 }
492}
493
494void tns_highlight(tns_t *tns, int n, bool hl)
495{
496 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
497 return;
498
499 if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
500 win_t *win = tns->win;
501 thumb_t *t = &tns->thumbs[n];
502 unsigned long col;
503 int oxy = (tns->bw + 1) / 2 + 1, owh = tns->bw + 2;
504
505 if (hl)
506 col = win->selcol;
507 else
508 col = win->fullscreen ? win->fscol : win->bgcol;
509
510 win_draw_rect(win, t->x - oxy, t->y - oxy, t->w + owh, t->h + owh,
511 false, tns->bw, col);
512
513 if (tns->files[n].marked)
514 tns_mark(tns, n, true);
515 }
516}
517
518bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
519{
520 int old, max;
521
522 if (tns == NULL || tns->thumbs == NULL)
523 return false;
524
525 old = *tns->sel;
526 cnt = cnt > 1 ? cnt : 1;
527
528 switch (dir) {
529 case DIR_UP:
530 *tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols);
531 break;
532 case DIR_DOWN:
533 max = tns->cols * ((*tns->cnt - 1) / tns->cols) +
534 MIN((*tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
535 *tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
536 break;
537 case DIR_LEFT:
538 *tns->sel = MAX(*tns->sel - cnt, 0);
539 break;
540 case DIR_RIGHT:
541 *tns->sel = MIN(*tns->sel + cnt, *tns->cnt - 1);
542 break;
543 }
544
545 if (*tns->sel != old) {
546 tns_highlight(tns, old, false);
547 tns_check_view(tns, false);
548 if (!tns->dirty)
549 tns_highlight(tns, *tns->sel, true);
550 }
551 return *tns->sel != old;
552}
553
554bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
555{
556 int d, max, old;
557
558 if (tns == NULL)
559 return false;
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 if (tns == NULL || tns->thumbs == NULL)
585 return false;
586
587 oldzl = tns->zl;
588 tns->zl += -(d < 0) + (d > 0);
589 tns->zl = MAX(tns->zl, 0);
590 tns->zl = MIN(tns->zl, ARRLEN(thumb_sizes)-1);
591
592 tns->bw = ((thumb_sizes[tns->zl] - 1) >> 5) + 1;
593 tns->dim = thumb_sizes[tns->zl] + 2 * tns->bw + 6;
594
595 if (tns->zl != oldzl) {
596 for (i = 0; i < *tns->cnt; i++)
597 tns_unload(tns, i);
598 tns->dirty = true;
599 }
600 return tns->zl != oldzl;
601}
602
603int tns_translate(tns_t *tns, int x, int y)
604{
605 int n;
606
607 if (tns == NULL || tns->thumbs == NULL)
608 return -1;
609 if (x < tns->x || y < tns->y)
610 return -1;
611
612 n = tns->first + (y - tns->y) / tns->dim * tns->cols +
613 (x - tns->x) / tns->dim;
614 if (n >= *tns->cnt)
615 n = -1;
616
617 return n;
618}