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.
 
 
 
 
 
 

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