Krolyxon
Commit 7b91e10

Branches

Refs →

nsxivthumbs.c

600 lines · 13.4 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
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <unistd.h>
27#include <utime.h>
28
29#include "thumbs.h"
30#include "util.h"
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;
39static const int thumb_size[] = { 32, 64, 96, 128, 160 };
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->zl = 1;
174 tns->dirty = false;
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*) s_malloc(len);
184 snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix);
185 } else {
186 warn("could not locate thumbnail cache directory");
187 }
188}
189
190void tns_free(tns_t *tns)
191{
192 int i;
193
194 if (tns == NULL)
195 return;
196
197 if (tns->thumbs != NULL) {
198 for (i = 0; i < tns->cnt; i++) {
199 if (tns->thumbs[i].im != NULL) {
200 imlib_context_set_image(tns->thumbs[i].im);
201 imlib_free_image();
202 }
203 }
204 free(tns->thumbs);
205 tns->thumbs = NULL;
206 }
207
208 free(cache_dir);
209 cache_dir = NULL;
210}
211
212Imlib_Image tns_scale_down(Imlib_Image im, int dim)
213{
214 int w, h;
215 float z, zw, zh;
216
217 imlib_context_set_image(im);
218 w = imlib_image_get_width();
219 h = imlib_image_get_height();
220 zw = (float) dim / (float) w;
221 zh = (float) dim / (float) h;
222 z = MIN(zw, zh);
223 z = MIN(z, 1.0);
224
225 if (z < 1.0) {
226 imlib_context_set_anti_alias(1);
227 im = imlib_create_cropped_scaled_image(0, 0, w, h, z * w, z * h);
228 if (im == NULL)
229 die("could not allocate memory");
230 imlib_free_image_and_decache();
231 }
232 return im;
233}
234
235bool tns_load(tns_t *tns, int n, bool force)
236{
237 int w, h;
238 bool cache_hit = false;
239 float zw, zh;
240 thumb_t *t;
241 Imlib_Image im = NULL;
242 const fileinfo_t *file;
243
244 if (tns == NULL || tns->thumbs == NULL)
245 return false;
246 if (n < 0 || n >= tns->cnt)
247 return false;
248 file = &tns->files[n];
249 if (file->name == NULL || file->path == NULL)
250 return false;
251
252 t = &tns->thumbs[n];
253
254 if (t->im != NULL) {
255 imlib_context_set_image(t->im);
256 imlib_free_image();
257 }
258
259 if (!force && (im = tns_cache_load(file->path, &force)) != NULL) {
260 cache_hit = true;
261 } else {
262#if HAVE_LIBEXIF
263 if (!force) {
264 int pw = 0, ph = 0, x = 0, y = 0;
265 bool err;
266 ExifData *ed;
267 ExifEntry *entry;
268 ExifContent *ifd;
269 ExifByteOrder byte_order;
270 int tmpfd;
271 char tmppath[] = "/tmp/sxiv-XXXXXX";
272 Imlib_Image tmpim;
273
274 if ((ed = exif_data_new_from_file(file->path)) != NULL &&
275 ed->data != NULL && ed->size > 0)
276 {
277 if ((tmpfd = mkstemp(tmppath)) >= 0) {
278 err = write(tmpfd, ed->data, ed->size) != ed->size;
279 close(tmpfd);
280
281 if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) {
282 byte_order = exif_data_get_byte_order(ed);
283 ifd = ed->ifd[EXIF_IFD_EXIF];
284 entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION);
285 if (entry != NULL)
286 pw = exif_get_long(entry->data, byte_order);
287 entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION);
288 if (entry != NULL)
289 ph = exif_get_long(entry->data, byte_order);
290
291 imlib_context_set_image(tmpim);
292 w = imlib_image_get_width();
293 h = imlib_image_get_height();
294
295 if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) {
296 zw = (float) pw / (float) w;
297 zh = (float) ph / (float) h;
298 if (zw < zh) {
299 pw /= zh;
300 x = (w - pw) / 2;
301 w = pw;
302 } else if (zw > zh) {
303 ph /= zw;
304 y = (h - ph) / 2;
305 h = ph;
306 }
307 }
308 if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL)
309 die("could not allocate memory");
310 imlib_free_image_and_decache();
311 }
312 unlink(tmppath);
313 }
314 exif_data_unref(ed);
315 }
316 }
317#endif
318 if (im == NULL && (access(file->path, R_OK) < 0 ||
319 (im = imlib_load_image(file->path)) == NULL))
320 {
321 warn("could not open image: %s", file->name);
322 return false;
323 }
324 }
325
326 if (!cache_hit) {
327#if HAVE_LIBEXIF
328 imlib_context_set_image(im);
329 exif_auto_orientate(file);
330#endif
331 im = tns_scale_down(im, thumb_size[ARRLEN(thumb_size)-1]);
332 tns_cache_write(im, file->path, true);
333 }
334
335 t->im = tns_scale_down(im, thumb_size[tns->zl]);
336 imlib_context_set_image(t->im);
337 t->w = imlib_image_get_width();
338 t->h = imlib_image_get_height();
339
340 tns->dirty = true;
341 return true;
342}
343
344void tns_unload(tns_t *tns, int n)
345{
346 thumb_t *t;
347
348 if (tns == NULL || tns->thumbs == NULL)
349 return;
350 if (n < 0 || n >= tns->cnt)
351 return;
352
353 t = &tns->thumbs[n];
354
355 if (t->im != NULL) {
356 imlib_context_set_image(t->im);
357 imlib_free_image();
358 t->im = NULL;
359 }
360}
361
362void tns_check_view(tns_t *tns, bool scrolled)
363{
364 int r;
365
366 if (tns == NULL)
367 return;
368
369 tns->first -= tns->first % tns->cols;
370 r = *tns->sel % tns->cols;
371
372 if (scrolled) {
373 /* move selection into visible area */
374 if (*tns->sel >= tns->first + tns->cols * tns->rows)
375 *tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
376 else if (*tns->sel < tns->first)
377 *tns->sel = tns->first + r;
378 } else {
379 /* scroll to selection */
380 if (tns->first + tns->cols * tns->rows <= *tns->sel) {
381 tns->first = *tns->sel - r - tns->cols * (tns->rows - 1);
382 tns->dirty = true;
383 } else if (tns->first > *tns->sel) {
384 tns->first = *tns->sel - r;
385 tns->dirty = true;
386 }
387 }
388}
389
390void tns_render(tns_t *tns)
391{
392 thumb_t *t;
393 win_t *win;
394 int i, cnt, r, x, y;
395 int thumb_dim;
396
397 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
398 return;
399 if (!tns->dirty)
400 return;
401
402 win = tns->win;
403 win_clear(win);
404 imlib_context_set_drawable(win->buf.pm);
405
406 thumb_dim = thumb_size[tns->zl] + 10;
407 tns->cols = MAX(1, win->w / thumb_dim);
408 tns->rows = MAX(1, win->h / thumb_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) * thumb_dim) / 2 + 5;
423 tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
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_size[tns->zl] - t->w) / 2;
438 t->y = y + (thumb_size[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 += thumb_dim;
449 } else {
450 x += thumb_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, 2, true, 1, col);
469 win_draw_rect(win, x + 1, y - 1, 2, 1, true, 1, col);
470
471 if (mark)
472 col = win->selcol;
473
474 win_draw_rect(win, x, y, 4, 4, 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
491 if (hl)
492 col = win->selcol;
493 else
494 col = win->fullscreen ? win->fscol : win->bgcol;
495
496 win_draw_rect(win, t->x - 2, t->y - 2, t->w + 4, t->h + 4, false, 2, col);
497
498 if (tns->files[n].marked)
499 tns_mark(tns, n, true);
500 }
501}
502
503bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
504{
505 int old, max;
506
507 if (tns == NULL || tns->thumbs == NULL)
508 return false;
509
510 old = *tns->sel;
511 cnt = cnt > 1 ? cnt : 1;
512
513 switch (dir) {
514 case DIR_UP:
515 *tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols);
516 break;
517 case DIR_DOWN:
518 max = tns->cols * ((tns->cnt - 1) / tns->cols) +
519 MIN((tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
520 *tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
521 break;
522 case DIR_LEFT:
523 *tns->sel = MAX(*tns->sel - cnt, 0);
524 break;
525 case DIR_RIGHT:
526 *tns->sel = MIN(*tns->sel + cnt, tns->cnt - 1);
527 break;
528 }
529
530 if (*tns->sel != old) {
531 tns_highlight(tns, old, false);
532 tns_check_view(tns, false);
533 if (!tns->dirty)
534 tns_highlight(tns, *tns->sel, true);
535 }
536 return *tns->sel != old;
537}
538
539bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
540{
541 int d, max, old;
542
543 if (tns == NULL)
544 return false;
545
546 old = tns->first;
547 d = tns->cols * (screen ? tns->rows : 1);
548
549 if (dir == DIR_DOWN) {
550 max = tns->cnt - tns->cols * tns->rows;
551 if (tns->cnt % tns->cols != 0)
552 max += tns->cols - tns->cnt % tns->cols;
553 tns->first = MIN(tns->first + d, max);
554 } else if (dir == DIR_UP) {
555 tns->first = MAX(tns->first - d, 0);
556 }
557
558 if (tns->first != old) {
559 tns_check_view(tns, true);
560 tns->dirty = true;
561 }
562 return tns->first != old;
563}
564
565bool tns_zoom(tns_t *tns, int d)
566{
567 int i, oldzl;
568
569 if (tns == NULL || tns->thumbs == NULL)
570 return false;
571
572 oldzl = tns->zl;
573 tns->zl += -(d < 0) + (d > 0);
574 tns->zl = MAX(tns->zl, 0);
575 tns->zl = MIN(tns->zl, ARRLEN(thumb_size)-1);
576
577 if (tns->zl != oldzl) {
578 for (i = 0; i < tns->cnt; i++)
579 tns_unload(tns, i);
580 tns->dirty = true;
581 }
582 return tns->zl != oldzl;
583}
584
585int tns_translate(tns_t *tns, int x, int y)
586{
587 int n;
588
589 if (tns == NULL || tns->thumbs == NULL)
590 return -1;
591 if (x < tns->x || y < tns->y)
592 return -1;
593
594 n = tns->first + (y - tns->y) / (thumb_size[tns->zl] + 10) * tns->cols +
595 (x - tns->x) / (thumb_size[tns->zl] + 10);
596 if (n >= tns->cnt)
597 n = -1;
598
599 return n;
600}