A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

464 lines
9.7 KiB

  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) {
  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->cnt = tns->first = tns->sel = 0;
  138. tns->cap = cnt;
  139. tns->alpha = true;
  140. tns->dirty = false;
  141. if ((homedir = getenv("HOME"))) {
  142. if (cache_dir)
  143. free(cache_dir);
  144. len = strlen(homedir) + 10;
  145. cache_dir = (char*) s_malloc(len * sizeof(char));
  146. snprintf(cache_dir, len, "%s/.sxiv", homedir);
  147. } else {
  148. warn("could not locate thumbnail cache directory");
  149. }
  150. }
  151. void tns_free(tns_t *tns) {
  152. int i;
  153. if (!tns)
  154. return;
  155. if (tns->thumbs) {
  156. for (i = 0; i < tns->cnt; i++) {
  157. if (tns->thumbs[i].im) {
  158. imlib_context_set_image(tns->thumbs[i].im);
  159. imlib_free_image();
  160. }
  161. }
  162. free(tns->thumbs);
  163. tns->thumbs = NULL;
  164. }
  165. if (cache_dir) {
  166. free(cache_dir);
  167. cache_dir = NULL;
  168. }
  169. }
  170. bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
  171. bool force, bool silent)
  172. {
  173. int w, h;
  174. bool use_cache, cache_hit = false;
  175. float z, zw, zh;
  176. thumb_t *t;
  177. Imlib_Image *im;
  178. const char *fmt;
  179. if (!tns || !tns->thumbs || !file || !file->name || !file->path)
  180. return false;
  181. if (n < 0 || n >= tns->cap)
  182. return false;
  183. t = &tns->thumbs[n];
  184. t->file = file;
  185. if (t->im) {
  186. imlib_context_set_image(t->im);
  187. imlib_free_image();
  188. }
  189. if ((use_cache = tns_cache_enabled())) {
  190. if (!force && (im = tns_cache_load(file->path)))
  191. cache_hit = true;
  192. }
  193. if (!cache_hit &&
  194. (access(file->path, R_OK) || !(im = imlib_load_image(file->path))))
  195. {
  196. if (!silent)
  197. warn("could not open image: %s", file->name);
  198. return false;
  199. }
  200. imlib_context_set_image(im);
  201. imlib_context_set_anti_alias(1);
  202. fmt = imlib_image_format();
  203. /* avoid unused-but-set-variable warning */
  204. (void) fmt;
  205. #if EXIF_SUPPORT
  206. if (!cache_hit && !strcmp(fmt, "jpeg"))
  207. exif_auto_orientate(file);
  208. #endif
  209. w = imlib_image_get_width();
  210. h = imlib_image_get_height();
  211. zw = (float) THUMB_SIZE / (float) w;
  212. zh = (float) THUMB_SIZE / (float) h;
  213. z = MIN(zw, zh);
  214. t->w = z * w;
  215. t->h = z * h;
  216. if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
  217. die("could not allocate memory");
  218. imlib_free_image_and_decache();
  219. if (use_cache && !cache_hit)
  220. tns_cache_write(t, false);
  221. tns->dirty = true;
  222. return true;
  223. }
  224. void tns_check_view(tns_t *tns, bool scrolled) {
  225. int r;
  226. if (!tns)
  227. return;
  228. tns->first -= tns->first % tns->cols;
  229. r = tns->sel % tns->cols;
  230. if (scrolled) {
  231. /* move selection into visible area */
  232. if (tns->sel >= tns->first + tns->cols * tns->rows)
  233. tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  234. else if (tns->sel < tns->first)
  235. tns->sel = tns->first + r;
  236. } else {
  237. /* scroll to selection */
  238. if (tns->first + tns->cols * tns->rows <= tns->sel) {
  239. tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
  240. tns->dirty = true;
  241. } else if (tns->first > tns->sel) {
  242. tns->first = tns->sel - r;
  243. tns->dirty = true;
  244. }
  245. }
  246. }
  247. void tns_render(tns_t *tns, win_t *win) {
  248. int i, cnt, r, x, y;
  249. thumb_t *t;
  250. if (!tns || !tns->thumbs || !win)
  251. return;
  252. if (!tns->dirty)
  253. return;
  254. win_clear(win);
  255. imlib_context_set_drawable(win->pm);
  256. tns->cols = MAX(1, win->w / thumb_dim);
  257. tns->rows = MAX(1, win->h / thumb_dim);
  258. if (tns->cnt < tns->cols * tns->rows) {
  259. tns->first = 0;
  260. cnt = tns->cnt;
  261. } else {
  262. tns_check_view(tns, false);
  263. cnt = tns->cols * tns->rows;
  264. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  265. tns->first -= r - r % tns->cols;
  266. if (r > 0)
  267. cnt -= r % tns->cols;
  268. }
  269. r = cnt % tns->cols ? 1 : 0;
  270. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  271. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  272. for (i = 0; i < cnt; i++) {
  273. t = &tns->thumbs[tns->first + i];
  274. t->x = x + (THUMB_SIZE - t->w) / 2;
  275. t->y = y + (THUMB_SIZE - t->h) / 2;
  276. imlib_context_set_image(t->im);
  277. if (imlib_image_has_alpha() && !tns->alpha)
  278. win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, true, 0, win->white);
  279. imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
  280. t->x, t->y, t->w, t->h);
  281. if ((i + 1) % tns->cols == 0) {
  282. x = tns->x;
  283. y += thumb_dim;
  284. } else {
  285. x += thumb_dim;
  286. }
  287. }
  288. tns->dirty = false;
  289. tns_highlight(tns, win, tns->sel, true);
  290. }
  291. void tns_highlight(tns_t *tns, win_t *win, int n, bool hl) {
  292. thumb_t *t;
  293. int x, y;
  294. unsigned long col;
  295. if (!tns || !tns->thumbs || !win)
  296. return;
  297. if (n >= 0 && n < tns->cnt) {
  298. t = &tns->thumbs[n];
  299. if (hl)
  300. col = win->selcol;
  301. else if (win->fullscreen)
  302. col = win->black;
  303. else
  304. col = win->bgcol;
  305. x = t->x - (THUMB_SIZE - t->w) / 2;
  306. y = t->y - (THUMB_SIZE - t->h) / 2;
  307. win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
  308. false, 2, col);
  309. }
  310. win_draw(win);
  311. }
  312. bool tns_move_selection(tns_t *tns, win_t *win, direction_t dir) {
  313. int old;
  314. if (!tns || !tns->thumbs || !win)
  315. return false;
  316. old = tns->sel;
  317. switch (dir) {
  318. case DIR_LEFT:
  319. if (tns->sel > 0)
  320. tns->sel--;
  321. break;
  322. case DIR_RIGHT:
  323. if (tns->sel < tns->cnt - 1)
  324. tns->sel++;
  325. break;
  326. case DIR_UP:
  327. if (tns->sel >= tns->cols)
  328. tns->sel -= tns->cols;
  329. break;
  330. case DIR_DOWN:
  331. if (tns->sel + tns->cols < tns->cnt)
  332. tns->sel += tns->cols;
  333. break;
  334. }
  335. if (tns->sel != old) {
  336. tns_highlight(tns, win, old, false);
  337. tns_check_view(tns, false);
  338. if (!tns->dirty)
  339. tns_highlight(tns, win, tns->sel, true);
  340. }
  341. return tns->sel != old;
  342. }
  343. bool tns_scroll(tns_t *tns, direction_t dir) {
  344. int old;
  345. if (!tns)
  346. return false;
  347. old = tns->first;
  348. if (dir == DIR_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
  349. tns->first += tns->cols;
  350. tns_check_view(tns, true);
  351. tns->dirty = true;
  352. } else if (dir == DIR_UP && tns->first >= tns->cols) {
  353. tns->first -= tns->cols;
  354. tns_check_view(tns, true);
  355. tns->dirty = true;
  356. }
  357. return tns->first != old;
  358. }
  359. int tns_translate(tns_t *tns, int x, int y) {
  360. int n;
  361. if (!tns || !tns->thumbs)
  362. return -1;
  363. if (x < tns->x || y < tns->y)
  364. return -1;
  365. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  366. (x - tns->x) / thumb_dim;
  367. if (n >= tns->cnt)
  368. n = -1;
  369. return n;
  370. }