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.
 
 
 
 
 
 

327 line
6.4 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 <Imlib2.h>
  19. #include "config.h"
  20. #include "image.h"
  21. #include "options.h"
  22. #include "util.h"
  23. int zl_cnt;
  24. float zoom_min;
  25. float zoom_max;
  26. void img_init(img_t *img, win_t *win) {
  27. zl_cnt = sizeof(zoom_levels) / sizeof(zoom_levels[0]);
  28. zoom_min = zoom_levels[0] / 100.0;
  29. zoom_max = zoom_levels[zl_cnt - 1] / 100.0;
  30. if (img) {
  31. img->zoom = options->zoom;
  32. img->zoom = MAX(img->zoom, zoom_min);
  33. img->zoom = MIN(img->zoom, zoom_max);
  34. img->aa = options->aa;
  35. }
  36. if (win) {
  37. imlib_context_set_display(win->env.dpy);
  38. imlib_context_set_visual(win->env.vis);
  39. imlib_context_set_colormap(win->env.cmap);
  40. }
  41. }
  42. void img_free(img_t* img) {
  43. if (imlib_context_get_image())
  44. imlib_free_image();
  45. }
  46. int _imlib_load_image(const char *filename) {
  47. Imlib_Image *im;
  48. if (!filename)
  49. return 0;
  50. if (!(im = imlib_load_image(filename))) {
  51. warn("could not open image: %s", filename);
  52. return 0;
  53. }
  54. imlib_context_set_image(im);
  55. imlib_image_set_changes_on_disk();
  56. return 1;
  57. }
  58. int img_check(const char *filename) {
  59. int ret;
  60. if ((ret = _imlib_load_image(filename)))
  61. imlib_free_image();
  62. return ret;
  63. }
  64. int img_load(img_t *img, const char *filename) {
  65. if (!img || !filename)
  66. return 0;
  67. if (imlib_context_get_image())
  68. imlib_free_image();
  69. if (!_imlib_load_image(filename))
  70. return 0;
  71. imlib_context_set_anti_alias(img->aa);
  72. img->re = 0;
  73. img->checkpan = 0;
  74. img->zoomed = 0;
  75. img->w = imlib_image_get_width();
  76. img->h = imlib_image_get_height();
  77. return 1;
  78. }
  79. void img_check_pan(img_t *img, win_t *win) {
  80. if (!img)
  81. return;
  82. if (img->w * img->zoom > win->w) {
  83. if (img->x > 0 && img->x + img->w * img->zoom > win->w)
  84. img->x = 0;
  85. if (img->x < 0 && img->x + img->w * img->zoom < win->w)
  86. img->x = win->w - img->w * img->zoom;
  87. } else {
  88. img->x = (win->w - img->w * img->zoom) / 2;
  89. }
  90. if (img->h * img->zoom > win->h) {
  91. if (img->y > 0 && img->y + img->h * img->zoom > win->h)
  92. img->y = 0;
  93. if (img->y < 0 && img->y + img->h * img->zoom < win->h)
  94. img->y = win->h - img->h * img->zoom;
  95. } else {
  96. img->y = (win->h - img->h * img->zoom) / 2;
  97. }
  98. }
  99. void img_render(img_t *img, win_t *win) {
  100. int sx, sy, sw, sh;
  101. int dx, dy, dw, dh;
  102. if (!img || !win || !imlib_context_get_image())
  103. return;
  104. if (!img->zoomed && options->scalemode != SCALE_ZOOM) {
  105. img_fit(img, win);
  106. if (options->scalemode == SCALE_DOWN && img->zoom > 1.0)
  107. img->zoom = 1.0;
  108. }
  109. if (!img->re) {
  110. /* rendered for the first time */
  111. img->re = 1;
  112. img_center(img, win);
  113. }
  114. if (img->checkpan) {
  115. img_check_pan(img, win);
  116. img->checkpan = 0;
  117. }
  118. /* calculate source and destination offsets */
  119. if (img->x < 0) {
  120. sx = -img->x / img->zoom;
  121. sw = win->w / img->zoom;
  122. dx = 0;
  123. dw = win->w;
  124. } else {
  125. sx = 0;
  126. sw = img->w;
  127. dx = img->x;
  128. dw = img->w * img->zoom;
  129. }
  130. if (img->y < 0) {
  131. sy = -img->y / img->zoom;
  132. sh = win->h / img->zoom;
  133. dy = 0;
  134. dh = win->h;
  135. } else {
  136. sy = 0;
  137. sh = img->h;
  138. dy = img->y;
  139. dh = img->h * img->zoom;
  140. }
  141. win_clear(win);
  142. imlib_context_set_drawable(win->pm);
  143. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  144. win_draw(win);
  145. }
  146. int img_fit(img_t *img, win_t *win) {
  147. float oz, zw, zh;
  148. if (!img || !win)
  149. return 0;
  150. oz = img->zoom;
  151. zw = (float) win->w / (float) img->w;
  152. zh = (float) win->h / (float) img->h;
  153. img->zoom = MIN(zw, zh);
  154. img->zoom = MAX(img->zoom, zoom_min);
  155. img->zoom = MIN(img->zoom, zoom_max);
  156. return oz != img->zoom;
  157. }
  158. int img_center(img_t *img, win_t *win) {
  159. int ox, oy;
  160. if (!img || !win)
  161. return 0;
  162. ox = img->x;
  163. oy = img->y;
  164. img->x = (win->w - img->w * img->zoom) / 2;
  165. img->y = (win->h - img->h * img->zoom) / 2;
  166. return ox != img->x || oy != img->y;
  167. }
  168. int img_zoom(img_t *img, float z) {
  169. if (!img)
  170. return 0;
  171. z = MAX(z, zoom_min);
  172. z = MIN(z, zoom_max);
  173. if (z != img->zoom) {
  174. img->x -= (img->w * z - img->w * img->zoom) / 2;
  175. img->y -= (img->h * z - img->h * img->zoom) / 2;
  176. img->zoom = z;
  177. img->checkpan = 1;
  178. img->zoomed = 1;
  179. return 1;
  180. } else {
  181. return 0;
  182. }
  183. }
  184. int img_zoom_in(img_t *img) {
  185. int i;
  186. if (!img)
  187. return 0;
  188. for (i = 1; i < zl_cnt; ++i) {
  189. if (zoom_levels[i] > img->zoom * 100.0)
  190. return img_zoom(img, zoom_levels[i] / 100.0);
  191. }
  192. return 0;
  193. }
  194. int img_zoom_out(img_t *img) {
  195. int i;
  196. if (!img)
  197. return 0;
  198. for (i = zl_cnt - 2; i >= 0; --i) {
  199. if (zoom_levels[i] < img->zoom * 100.0)
  200. return img_zoom(img, zoom_levels[i] / 100.0);
  201. }
  202. return 0;
  203. }
  204. int img_move(img_t *img, win_t *win, int dx, int dy) {
  205. int ox, oy;
  206. if (!img || !win)
  207. return 0;
  208. ox = img->x;
  209. oy = img->y;
  210. img->x += dx;
  211. img->y += dy;
  212. img_check_pan(img, win);
  213. return ox != img->x || oy != img->y;
  214. }
  215. int img_pan(img_t *img, win_t *win, pandir_t dir) {
  216. if (!img || !win)
  217. return 0;
  218. switch (dir) {
  219. case PAN_LEFT:
  220. return img_move(img, win, win->w / 5, 0);
  221. case PAN_RIGHT:
  222. return img_move(img, win, win->w / 5 * -1, 0);
  223. case PAN_UP:
  224. return img_move(img, win, 0, win->h / 5);
  225. case PAN_DOWN:
  226. return img_move(img, win, 0, win->h / 5 * -1);
  227. }
  228. return 0;
  229. }
  230. void img_rotate(img_t *img, win_t *win, int d) {
  231. int ox, oy, tmp;
  232. if (!img || !win)
  233. return;
  234. ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom;
  235. oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom;
  236. imlib_image_orientate(d);
  237. img->x = oy + (win->w - win->h) / 2;
  238. img->y = ox + (win->h - win->w) / 2;
  239. tmp = img->w;
  240. img->w = img->h;
  241. img->h = tmp;
  242. img->checkpan = 1;
  243. }
  244. void img_rotate_left(img_t *img, win_t *win) {
  245. img_rotate(img, win, 3);
  246. }
  247. void img_rotate_right(img_t *img, win_t *win) {
  248. img_rotate(img, win, 1);
  249. }
  250. void img_toggle_antialias(img_t *img) {
  251. if (!img)
  252. return;
  253. img->aa ^= 1;
  254. imlib_context_set_anti_alias(img->aa);
  255. }