Krolyxon
Commit 0e4db69

Branches

Refs →

nsxivthumbs.c

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