Krolyxon
Commit e15dabd

Branches

Refs →

nsxivthumbs.c

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