A Simple X Image Viewer
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

336 lines
6.5 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->scalemode = options->scalemode;
  73. img->re = 0;
  74. img->checkpan = 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 || !win)
  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. int img_fit(img_t *img, win_t *win) {
  100. float oz, zw, zh;
  101. if (!img || !win)
  102. return 0;
  103. oz = img->zoom;
  104. zw = (float) win->w / (float) img->w;
  105. zh = (float) win->h / (float) img->h;
  106. img->zoom = MIN(zw, zh);
  107. img->zoom = MAX(img->zoom, zoom_min);
  108. img->zoom = MIN(img->zoom, zoom_max);
  109. return oz != img->zoom;
  110. }
  111. void img_render(img_t *img, win_t *win) {
  112. int sx, sy, sw, sh;
  113. int dx, dy, dw, dh;
  114. if (!img || !win || !imlib_context_get_image())
  115. return;
  116. if (img->scalemode != SCALE_ZOOM) {
  117. img_fit(img, win);
  118. if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
  119. img->zoom = 1.0;
  120. }
  121. if (!img->re) {
  122. /* rendered for the first time */
  123. img->re = 1;
  124. img_center(img, win);
  125. }
  126. if (img->checkpan) {
  127. img_check_pan(img, win);
  128. img->checkpan = 0;
  129. }
  130. /* calculate source and destination offsets */
  131. if (img->x < 0) {
  132. sx = -img->x / img->zoom;
  133. sw = win->w / img->zoom;
  134. dx = 0;
  135. dw = win->w;
  136. } else {
  137. sx = 0;
  138. sw = img->w;
  139. dx = img->x;
  140. dw = img->w * img->zoom;
  141. }
  142. if (img->y < 0) {
  143. sy = -img->y / img->zoom;
  144. sh = win->h / img->zoom;
  145. dy = 0;
  146. dh = win->h;
  147. } else {
  148. sy = 0;
  149. sh = img->h;
  150. dy = img->y;
  151. dh = img->h * img->zoom;
  152. }
  153. win_clear(win);
  154. imlib_context_set_drawable(win->pm);
  155. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  156. win_draw(win);
  157. }
  158. int img_fit_win(img_t *img, win_t *win) {
  159. if (!img || !win)
  160. return 0;
  161. img->scalemode = SCALE_FIT;
  162. return img_fit(img, win);
  163. }
  164. int img_center(img_t *img, win_t *win) {
  165. int ox, oy;
  166. if (!img || !win)
  167. return 0;
  168. ox = img->x;
  169. oy = img->y;
  170. img->x = (win->w - img->w * img->zoom) / 2;
  171. img->y = (win->h - img->h * img->zoom) / 2;
  172. return ox != img->x || oy != img->y;
  173. }
  174. int img_zoom(img_t *img, float z) {
  175. if (!img)
  176. return 0;
  177. z = MAX(z, zoom_min);
  178. z = MIN(z, zoom_max);
  179. img->scalemode = SCALE_ZOOM;
  180. if (z != img->zoom) {
  181. img->x -= (img->w * z - img->w * img->zoom) / 2;
  182. img->y -= (img->h * z - img->h * img->zoom) / 2;
  183. img->zoom = z;
  184. img->checkpan = 1;
  185. return 1;
  186. } else {
  187. return 0;
  188. }
  189. }
  190. int img_zoom_in(img_t *img) {
  191. int i;
  192. if (!img)
  193. return 0;
  194. for (i = 1; i < zl_cnt; ++i) {
  195. if (zoom_levels[i] > img->zoom * 100.0)
  196. return img_zoom(img, zoom_levels[i] / 100.0);
  197. }
  198. return 0;
  199. }
  200. int img_zoom_out(img_t *img) {
  201. int i;
  202. if (!img)
  203. return 0;
  204. for (i = zl_cnt - 2; i >= 0; --i) {
  205. if (zoom_levels[i] < img->zoom * 100.0)
  206. return img_zoom(img, zoom_levels[i] / 100.0);
  207. }
  208. return 0;
  209. }
  210. int img_move(img_t *img, win_t *win, int dx, int dy) {
  211. int ox, oy;
  212. if (!img || !win)
  213. return 0;
  214. ox = img->x;
  215. oy = img->y;
  216. img->x += dx;
  217. img->y += dy;
  218. img_check_pan(img, win);
  219. return ox != img->x || oy != img->y;
  220. }
  221. int img_pan(img_t *img, win_t *win, pandir_t dir) {
  222. if (!img || !win)
  223. return 0;
  224. switch (dir) {
  225. case PAN_LEFT:
  226. return img_move(img, win, win->w / 5, 0);
  227. case PAN_RIGHT:
  228. return img_move(img, win, win->w / 5 * -1, 0);
  229. case PAN_UP:
  230. return img_move(img, win, 0, win->h / 5);
  231. case PAN_DOWN:
  232. return img_move(img, win, 0, win->h / 5 * -1);
  233. }
  234. return 0;
  235. }
  236. void img_rotate(img_t *img, win_t *win, int d) {
  237. int ox, oy, tmp;
  238. if (!img || !win)
  239. return;
  240. ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom;
  241. oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom;
  242. imlib_image_orientate(d);
  243. img->x = oy + (win->w - win->h) / 2;
  244. img->y = ox + (win->h - win->w) / 2;
  245. tmp = img->w;
  246. img->w = img->h;
  247. img->h = tmp;
  248. img->checkpan = 1;
  249. }
  250. void img_rotate_left(img_t *img, win_t *win) {
  251. img_rotate(img, win, 3);
  252. }
  253. void img_rotate_right(img_t *img, win_t *win) {
  254. img_rotate(img, win, 1);
  255. }
  256. void img_toggle_antialias(img_t *img) {
  257. if (!img)
  258. return;
  259. img->aa ^= 1;
  260. imlib_context_set_anti_alias(img->aa);
  261. }