Krolyxon
Commit 66c3c55

Branches

Refs →

nsxivthumbs.c

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