Krolyxon
Commit c2560f8

Branches

Refs →

nsxivthumbs.c

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