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.
 
 
 
 
 
 

348 lines
7.0 KiB

  1. /* sxiv: image.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
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <unistd.h>
  19. #include <Imlib2.h>
  20. #include "config.h"
  21. #include "icon.h"
  22. #include "image.h"
  23. #include "options.h"
  24. #include "util.h"
  25. int zl_cnt;
  26. float zoom_min;
  27. float zoom_max;
  28. Imlib_Image *im_warn;
  29. void img_init(img_t *img, win_t *win) {
  30. zl_cnt = sizeof(zoom_levels) / sizeof(zoom_levels[0]);
  31. zoom_min = zoom_levels[0] / 100.0;
  32. zoom_max = zoom_levels[zl_cnt - 1] / 100.0;
  33. im_warn = imlib_create_image_using_data(32, 32, icon_warn);
  34. if (img) {
  35. img->zoom = options->zoom;
  36. img->zoom = MAX(img->zoom, zoom_min);
  37. img->zoom = MIN(img->zoom, zoom_max);
  38. img->aa = options->aa;
  39. }
  40. if (win) {
  41. imlib_context_set_display(win->env.dpy);
  42. imlib_context_set_visual(win->env.vis);
  43. imlib_context_set_colormap(win->env.cmap);
  44. }
  45. }
  46. void img_free(img_t* img) {
  47. if (img && img->valid && imlib_context_get_image())
  48. imlib_free_image();
  49. imlib_context_set_image(im_warn);
  50. imlib_free_image();
  51. }
  52. int _imlib_load_image(const char *filename) {
  53. Imlib_Image *im;
  54. if (!filename)
  55. return 0;
  56. if (access(filename, F_OK) || !(im = imlib_load_image(filename))) {
  57. warn("could not open file: %s", filename);
  58. return 0;
  59. }
  60. imlib_context_set_image(im);
  61. imlib_image_set_changes_on_disk();
  62. return 1;
  63. }
  64. int img_check(const char *filename) {
  65. int ret;
  66. if ((ret = _imlib_load_image(filename)))
  67. imlib_free_image();
  68. return ret;
  69. }
  70. int img_load(img_t *img, const char *filename) {
  71. if (!img || !filename)
  72. return 0;
  73. if (img->valid && imlib_context_get_image())
  74. imlib_free_image();
  75. if ((img->valid = _imlib_load_image(filename))) {
  76. imlib_context_set_anti_alias(img->aa);
  77. img->scalemode = options->scalemode;
  78. } else {
  79. imlib_context_set_image(im_warn);
  80. imlib_image_set_has_alpha(1);
  81. imlib_context_set_anti_alias(0);
  82. img->scalemode = SCALE_DOWN;
  83. }
  84. img->re = 0;
  85. img->checkpan = 0;
  86. img->w = imlib_image_get_width();
  87. img->h = imlib_image_get_height();
  88. return 1;
  89. }
  90. void img_check_pan(img_t *img, win_t *win) {
  91. if (!img || !win)
  92. return;
  93. if (img->w * img->zoom > win->w) {
  94. if (img->x > 0 && img->x + img->w * img->zoom > win->w)
  95. img->x = 0;
  96. if (img->x < 0 && img->x + img->w * img->zoom < win->w)
  97. img->x = win->w - img->w * img->zoom;
  98. } else {
  99. img->x = (win->w - img->w * img->zoom) / 2;
  100. }
  101. if (img->h * img->zoom > win->h) {
  102. if (img->y > 0 && img->y + img->h * img->zoom > win->h)
  103. img->y = 0;
  104. if (img->y < 0 && img->y + img->h * img->zoom < win->h)
  105. img->y = win->h - img->h * img->zoom;
  106. } else {
  107. img->y = (win->h - img->h * img->zoom) / 2;
  108. }
  109. }
  110. int img_fit(img_t *img, win_t *win) {
  111. float oz, zw, zh;
  112. if (!img || !win)
  113. return 0;
  114. oz = img->zoom;
  115. zw = (float) win->w / (float) img->w;
  116. zh = (float) win->h / (float) img->h;
  117. img->zoom = MIN(zw, zh);
  118. img->zoom = MAX(img->zoom, zoom_min);
  119. img->zoom = MIN(img->zoom, zoom_max);
  120. return oz != img->zoom;
  121. }
  122. void img_render(img_t *img, win_t *win) {
  123. int sx, sy, sw, sh;
  124. int dx, dy, dw, dh;
  125. if (!img || !win || !imlib_context_get_image())
  126. return;
  127. if (img->scalemode != SCALE_ZOOM) {
  128. img_fit(img, win);
  129. if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
  130. img->zoom = 1.0;
  131. }
  132. if (!img->re) {
  133. /* rendered for the first time */
  134. img->re = 1;
  135. img_center(img, win);
  136. }
  137. if (img->checkpan) {
  138. img_check_pan(img, win);
  139. img->checkpan = 0;
  140. }
  141. /* calculate source and destination offsets */
  142. if (img->x < 0) {
  143. sx = -img->x / img->zoom;
  144. sw = win->w / img->zoom;
  145. dx = 0;
  146. dw = win->w;
  147. } else {
  148. sx = 0;
  149. sw = img->w;
  150. dx = img->x;
  151. dw = img->w * img->zoom;
  152. }
  153. if (img->y < 0) {
  154. sy = -img->y / img->zoom;
  155. sh = win->h / img->zoom;
  156. dy = 0;
  157. dh = win->h;
  158. } else {
  159. sy = 0;
  160. sh = img->h;
  161. dy = img->y;
  162. dh = img->h * img->zoom;
  163. }
  164. win_clear(win);
  165. imlib_context_set_drawable(win->pm);
  166. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  167. win_draw(win);
  168. }
  169. int img_fit_win(img_t *img, win_t *win) {
  170. if (!img || !img->valid || !win)
  171. return 0;
  172. img->scalemode = SCALE_FIT;
  173. return img_fit(img, win);
  174. }
  175. int img_center(img_t *img, win_t *win) {
  176. int ox, oy;
  177. if (!img || !win)
  178. return 0;
  179. ox = img->x;
  180. oy = img->y;
  181. img->x = (win->w - img->w * img->zoom) / 2;
  182. img->y = (win->h - img->h * img->zoom) / 2;
  183. return ox != img->x || oy != img->y;
  184. }
  185. int img_zoom(img_t *img, float z) {
  186. if (!img || !img->valid)
  187. return 0;
  188. z = MAX(z, zoom_min);
  189. z = MIN(z, zoom_max);
  190. img->scalemode = SCALE_ZOOM;
  191. if (z != img->zoom) {
  192. img->x -= (img->w * z - img->w * img->zoom) / 2;
  193. img->y -= (img->h * z - img->h * img->zoom) / 2;
  194. img->zoom = z;
  195. img->checkpan = 1;
  196. return 1;
  197. } else {
  198. return 0;
  199. }
  200. }
  201. int img_zoom_in(img_t *img) {
  202. int i;
  203. if (!img || !img->valid)
  204. return 0;
  205. for (i = 1; i < zl_cnt; ++i) {
  206. if (zoom_levels[i] > img->zoom * 100.0)
  207. return img_zoom(img, zoom_levels[i] / 100.0);
  208. }
  209. return 0;
  210. }
  211. int img_zoom_out(img_t *img) {
  212. int i;
  213. if (!img || !img->valid)
  214. return 0;
  215. for (i = zl_cnt - 2; i >= 0; --i) {
  216. if (zoom_levels[i] < img->zoom * 100.0)
  217. return img_zoom(img, zoom_levels[i] / 100.0);
  218. }
  219. return 0;
  220. }
  221. int img_move(img_t *img, win_t *win, int dx, int dy) {
  222. int ox, oy;
  223. if (!img || !img->valid || !win)
  224. return 0;
  225. ox = img->x;
  226. oy = img->y;
  227. img->x += dx;
  228. img->y += dy;
  229. img_check_pan(img, win);
  230. return ox != img->x || oy != img->y;
  231. }
  232. int img_pan(img_t *img, win_t *win, pandir_t dir) {
  233. if (!img || !img->valid || !win)
  234. return 0;
  235. switch (dir) {
  236. case PAN_LEFT:
  237. return img_move(img, win, win->w / 5, 0);
  238. case PAN_RIGHT:
  239. return img_move(img, win, win->w / 5 * -1, 0);
  240. case PAN_UP:
  241. return img_move(img, win, 0, win->h / 5);
  242. case PAN_DOWN:
  243. return img_move(img, win, 0, win->h / 5 * -1);
  244. }
  245. return 0;
  246. }
  247. void img_rotate(img_t *img, win_t *win, int d) {
  248. int ox, oy, tmp;
  249. if (!img || !img->valid || !win)
  250. return;
  251. ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom;
  252. oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom;
  253. imlib_image_orientate(d);
  254. img->x = oy + (win->w - win->h) / 2;
  255. img->y = ox + (win->h - win->w) / 2;
  256. tmp = img->w;
  257. img->w = img->h;
  258. img->h = tmp;
  259. img->checkpan = 1;
  260. }
  261. void img_rotate_left(img_t *img, win_t *win) {
  262. img_rotate(img, win, 3);
  263. }
  264. void img_rotate_right(img_t *img, win_t *win) {
  265. img_rotate(img, win, 1);
  266. }
  267. void img_toggle_antialias(img_t *img) {
  268. if (!img || !img->valid)
  269. return;
  270. img->aa ^= 1;
  271. imlib_context_set_anti_alias(img->aa);
  272. }