Krolyxon
Commit b4096c5

Branches

Refs →

nsxivthumbs.c

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