A Simple X Image Viewer
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

thumbs.c 9.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /* sxiv: thumbs.c
  2. * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #define _POSIX_C_SOURCE 200112L
  19. #define _FEATURE_CONFIG
  20. #define _THUMBS_CONFIG
  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. #include "thumbs.h"
  28. #include "util.h"
  29. #include "config.h"
  30. #if EXIF_SUPPORT
  31. void exif_auto_orientate(const fileinfo_t*);
  32. #endif
  33. const int thumb_dim = THUMB_SIZE + 10;
  34. char *cache_dir = NULL;
  35. bool tns_cache_enabled() {
  36. struct stat stats;
  37. return cache_dir && !stat(cache_dir, &stats) && S_ISDIR(stats.st_mode) &&
  38. !access(cache_dir, W_OK);
  39. }
  40. char* tns_cache_filepath(const char *filepath) {
  41. size_t len;
  42. char *cfile = NULL;
  43. if (!cache_dir || !filepath || *filepath != '/')
  44. return NULL;
  45. if (strncmp(filepath, cache_dir, strlen(cache_dir))) {
  46. len = strlen(cache_dir) + strlen(filepath) + 6;
  47. cfile = (char*) s_malloc(len);
  48. snprintf(cfile, len, "%s/%s.png", cache_dir, filepath + 1);
  49. }
  50. return cfile;
  51. }
  52. Imlib_Image* tns_cache_load(const char *filepath) {
  53. char *cfile;
  54. struct stat cstats, fstats;
  55. Imlib_Image *im = NULL;
  56. if (!filepath)
  57. return NULL;
  58. if (stat(filepath, &fstats))
  59. return NULL;
  60. if ((cfile = tns_cache_filepath(filepath))) {
  61. if (!stat(cfile, &cstats) && cstats.st_mtime == fstats.st_mtime)
  62. im = imlib_load_image(cfile);
  63. free(cfile);
  64. }
  65. return im;
  66. }
  67. void tns_cache_write(thumb_t *t, bool force) {
  68. char *cfile, *dirend;
  69. struct stat cstats, fstats;
  70. struct utimbuf times;
  71. Imlib_Load_Error err = 0;
  72. if (!t || !t->im || !t->file || !t->file->name || !t->file->path)
  73. return;
  74. if (stat(t->file->path, &fstats))
  75. return;
  76. if ((cfile = tns_cache_filepath(t->file->path))) {
  77. if (force || stat(cfile, &cstats) || cstats.st_mtime != fstats.st_mtime) {
  78. if ((dirend = strrchr(cfile, '/'))) {
  79. *dirend = '\0';
  80. err = r_mkdir(cfile);
  81. *dirend = '/';
  82. }
  83. if (!err) {
  84. imlib_context_set_image(t->im);
  85. imlib_image_set_format("png");
  86. imlib_save_image_with_error_return(cfile, &err);
  87. }
  88. if (err) {
  89. warn("could not cache thumbnail: %s", t->file->name);
  90. } else {
  91. times.actime = fstats.st_atime;
  92. times.modtime = fstats.st_mtime;
  93. utime(cfile, &times);
  94. }
  95. }
  96. free(cfile);
  97. }
  98. }
  99. void tns_clean_cache(tns_t *tns) {
  100. int dirlen;
  101. bool delete;
  102. char *cfile, *filename, *tpos;
  103. r_dir_t dir;
  104. if (!cache_dir)
  105. return;
  106. if (r_opendir(&dir, cache_dir)) {
  107. warn("could not open thumbnail cache directory: %s", cache_dir);
  108. return;
  109. }
  110. dirlen = strlen(cache_dir);
  111. while ((cfile = r_readdir(&dir))) {
  112. filename = cfile + dirlen;
  113. delete = false;
  114. if ((tpos = strrchr(filename, '.'))) {
  115. *tpos = '\0';
  116. if (access(filename, F_OK))
  117. delete = true;
  118. *tpos = '.';
  119. }
  120. if (delete && unlink(cfile))
  121. warn("could not delete cache file: %s", cfile);
  122. free(cfile);
  123. }
  124. r_closedir(&dir);
  125. }
  126. void tns_init(tns_t *tns, int cnt, win_t *win) {
  127. int len;
  128. char *homedir;
  129. if (!tns)
  130. return;
  131. if (cnt) {
  132. tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
  133. memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
  134. } else {
  135. tns->thumbs = NULL;
  136. }
  137. tns->cap = cnt;
  138. tns->cnt = tns->first = tns->sel = 0;
  139. tns->win = win;
  140. tns->alpha = true;
  141. tns->dirty = false;
  142. if ((homedir = getenv("HOME"))) {
  143. if (cache_dir)
  144. free(cache_dir);
  145. len = strlen(homedir) + 10;
  146. cache_dir = (char*) s_malloc(len * sizeof(char));
  147. snprintf(cache_dir, len, "%s/.sxiv", homedir);
  148. } else {
  149. warn("could not locate thumbnail cache directory");
  150. }
  151. }
  152. void tns_free(tns_t *tns) {
  153. int i;
  154. if (!tns)
  155. return;
  156. if (tns->thumbs) {
  157. for (i = 0; i < tns->cnt; i++) {
  158. if (tns->thumbs[i].im) {
  159. imlib_context_set_image(tns->thumbs[i].im);
  160. imlib_free_image();
  161. }
  162. }
  163. free(tns->thumbs);
  164. tns->thumbs = NULL;
  165. }
  166. if (cache_dir) {
  167. free(cache_dir);
  168. cache_dir = NULL;
  169. }
  170. }
  171. bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
  172. bool force, bool silent)
  173. {
  174. int w, h;
  175. bool use_cache, cache_hit = false;
  176. float z, zw, zh;
  177. thumb_t *t;
  178. Imlib_Image *im;
  179. const char *fmt;
  180. if (!tns || !tns->thumbs || !file || !file->name || !file->path)
  181. return false;
  182. if (n < 0 || n >= tns->cap)
  183. return false;
  184. t = &tns->thumbs[n];
  185. t->file = file;
  186. if (t->im) {
  187. imlib_context_set_image(t->im);
  188. imlib_free_image();
  189. }
  190. if ((use_cache = tns_cache_enabled())) {
  191. if (!force && (im = tns_cache_load(file->path)))
  192. cache_hit = true;
  193. }
  194. if (!cache_hit &&
  195. (access(file->path, R_OK) || !(im = imlib_load_image(file->path))))
  196. {
  197. if (!silent)
  198. warn("could not open image: %s", file->name);
  199. return false;
  200. }
  201. imlib_context_set_image(im);
  202. imlib_context_set_anti_alias(1);
  203. fmt = imlib_image_format();
  204. /* avoid unused-but-set-variable warning */
  205. (void) fmt;
  206. #if EXIF_SUPPORT
  207. if (!cache_hit && streq(fmt, "jpeg"))
  208. exif_auto_orientate(file);
  209. #endif
  210. w = imlib_image_get_width();
  211. h = imlib_image_get_height();
  212. zw = (float) THUMB_SIZE / (float) w;
  213. zh = (float) THUMB_SIZE / (float) h;
  214. z = MIN(zw, zh);
  215. t->w = z * w;
  216. t->h = z * h;
  217. if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
  218. die("could not allocate memory");
  219. imlib_free_image_and_decache();
  220. if (use_cache && !cache_hit)
  221. tns_cache_write(t, false);
  222. tns->dirty = true;
  223. return true;
  224. }
  225. void tns_check_view(tns_t *tns, bool scrolled) {
  226. int r;
  227. if (!tns)
  228. return;
  229. tns->first -= tns->first % tns->cols;
  230. r = tns->sel % tns->cols;
  231. if (scrolled) {
  232. /* move selection into visible area */
  233. if (tns->sel >= tns->first + tns->cols * tns->rows)
  234. tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  235. else if (tns->sel < tns->first)
  236. tns->sel = tns->first + r;
  237. } else {
  238. /* scroll to selection */
  239. if (tns->first + tns->cols * tns->rows <= tns->sel) {
  240. tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
  241. tns->dirty = true;
  242. } else if (tns->first > tns->sel) {
  243. tns->first = tns->sel - r;
  244. tns->dirty = true;
  245. }
  246. }
  247. }
  248. void tns_render(tns_t *tns) {
  249. thumb_t *t;
  250. win_t *win;
  251. int i, cnt, r, x, y;
  252. if (!tns || !tns->thumbs || !tns->win)
  253. return;
  254. if (!tns->dirty)
  255. return;
  256. win = tns->win;
  257. win_clear(win);
  258. imlib_context_set_drawable(win->pm);
  259. tns->cols = MAX(1, win->w / thumb_dim);
  260. tns->rows = MAX(1, win->h / thumb_dim);
  261. if (tns->cnt < tns->cols * tns->rows) {
  262. tns->first = 0;
  263. cnt = tns->cnt;
  264. } else {
  265. tns_check_view(tns, false);
  266. cnt = tns->cols * tns->rows;
  267. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  268. tns->first -= r - r % tns->cols;
  269. if (r > 0)
  270. cnt -= r % tns->cols;
  271. }
  272. r = cnt % tns->cols ? 1 : 0;
  273. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  274. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  275. for (i = 0; i < cnt; i++) {
  276. t = &tns->thumbs[tns->first + i];
  277. t->x = x + (THUMB_SIZE - t->w) / 2;
  278. t->y = y + (THUMB_SIZE - t->h) / 2;
  279. imlib_context_set_image(t->im);
  280. if (imlib_image_has_alpha() && !tns->alpha)
  281. win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, true, 0, win->white);
  282. imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
  283. t->x, t->y, t->w, t->h);
  284. if ((i + 1) % tns->cols == 0) {
  285. x = tns->x;
  286. y += thumb_dim;
  287. } else {
  288. x += thumb_dim;
  289. }
  290. }
  291. tns->dirty = false;
  292. tns_highlight(tns, tns->sel, true);
  293. }
  294. void tns_highlight(tns_t *tns, int n, bool hl) {
  295. thumb_t *t;
  296. win_t *win;
  297. int x, y;
  298. unsigned long col;
  299. if (!tns || !tns->thumbs || !tns->win)
  300. return;
  301. win = tns->win;
  302. if (n >= 0 && n < tns->cnt) {
  303. t = &tns->thumbs[n];
  304. if (hl)
  305. col = win->selcol;
  306. else if (win->fullscreen)
  307. col = win->black;
  308. else
  309. col = win->bgcol;
  310. x = t->x - (THUMB_SIZE - t->w) / 2;
  311. y = t->y - (THUMB_SIZE - t->h) / 2;
  312. win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
  313. false, 2, col);
  314. }
  315. win_draw(win);
  316. }
  317. bool tns_move_selection(tns_t *tns, direction_t dir) {
  318. int old;
  319. if (!tns || !tns->thumbs)
  320. return false;
  321. old = tns->sel;
  322. switch (dir) {
  323. case DIR_LEFT:
  324. if (tns->sel > 0)
  325. tns->sel--;
  326. break;
  327. case DIR_RIGHT:
  328. if (tns->sel < tns->cnt - 1)
  329. tns->sel++;
  330. break;
  331. case DIR_UP:
  332. if (tns->sel >= tns->cols)
  333. tns->sel -= tns->cols;
  334. break;
  335. case DIR_DOWN:
  336. if (tns->sel + tns->cols < tns->cnt)
  337. tns->sel += tns->cols;
  338. break;
  339. }
  340. if (tns->sel != old) {
  341. tns_highlight(tns, old, false);
  342. tns_check_view(tns, false);
  343. if (!tns->dirty)
  344. tns_highlight(tns, tns->sel, true);
  345. }
  346. return tns->sel != old;
  347. }
  348. bool tns_scroll(tns_t *tns, direction_t dir) {
  349. int old;
  350. if (!tns)
  351. return false;
  352. old = tns->first;
  353. if (dir == DIR_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
  354. tns->first += tns->cols;
  355. tns_check_view(tns, true);
  356. tns->dirty = true;
  357. } else if (dir == DIR_UP && tns->first >= tns->cols) {
  358. tns->first -= tns->cols;
  359. tns_check_view(tns, true);
  360. tns->dirty = true;
  361. }
  362. return tns->first != old;
  363. }
  364. int tns_translate(tns_t *tns, int x, int y) {
  365. int n;
  366. if (!tns || !tns->thumbs)
  367. return -1;
  368. if (x < tns->x || y < tns->y)
  369. return -1;
  370. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  371. (x - tns->x) / thumb_dim;
  372. if (n >= tns->cnt)
  373. n = -1;
  374. return n;
  375. }