A Simple X Image Viewer
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

327 行
6.3 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. void img_center(img_t *img, win_t *win) {
  159. if (!img || !win)
  160. return;
  161. img->x = (win->w - img->w * img->zoom) / 2;
  162. img->y = (win->h - img->h * img->zoom) / 2;
  163. }
  164. int img_zoom(img_t *img, float z) {
  165. if (!img)
  166. return 0;
  167. z = MAX(z, zoom_min);
  168. z = MIN(z, zoom_max);
  169. if (z != img->zoom) {
  170. img->x -= (img->w * z - img->w * img->zoom) / 2;
  171. img->y -= (img->h * z - img->h * img->zoom) / 2;
  172. img->zoom = z;
  173. img->checkpan = 1;
  174. img->zoomed = 1;
  175. return 1;
  176. } else {
  177. return 0;
  178. }
  179. }
  180. int img_zoom_in(img_t *img) {
  181. int i;
  182. if (!img)
  183. return 0;
  184. for (i = 1; i < zl_cnt; ++i) {
  185. if (zoom_levels[i] > img->zoom * 100.0)
  186. return img_zoom(img, zoom_levels[i] / 100.0);
  187. }
  188. return 0;
  189. }
  190. int img_zoom_out(img_t *img) {
  191. int i;
  192. if (!img)
  193. return 0;
  194. for (i = zl_cnt - 2; i >= 0; --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_move(img_t *img, win_t *win, int dx, int dy) {
  201. int ox, oy;
  202. if (!img || !win)
  203. return 0;
  204. ox = img->x;
  205. oy = img->y;
  206. img->x += dx;
  207. img->y += dy;
  208. img_check_pan(img, win);
  209. return ox != img->x || oy != img->y;
  210. }
  211. int img_pan(img_t *img, win_t *win, pandir_t dir) {
  212. if (!img || !win)
  213. return 0;
  214. switch (dir) {
  215. case PAN_LEFT:
  216. return img_move(img, win, win->w / 5, 0);
  217. case PAN_RIGHT:
  218. return img_move(img, win, win->w / 5 * -1, 0);
  219. case PAN_UP:
  220. return img_move(img, win, 0, win->h / 5);
  221. case PAN_DOWN:
  222. return img_move(img, win, 0, win->h / 5 * -1);
  223. }
  224. return 0;
  225. }
  226. int img_rotate(img_t *img, win_t *win, int d) {
  227. int ox, oy, tmp;
  228. if (!img || !win)
  229. return 0;
  230. ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom;
  231. oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom;
  232. imlib_image_orientate(d);
  233. img->x = oy + (win->w - win->h) / 2;
  234. img->y = ox + (win->h - win->w) / 2;
  235. tmp = img->w;
  236. img->w = img->h;
  237. img->h = tmp;
  238. img->checkpan = 1;
  239. return 1;
  240. }
  241. int img_rotate_left(img_t *img, win_t *win) {
  242. return img_rotate(img, win, 3);
  243. }
  244. int img_rotate_right(img_t *img, win_t *win) {
  245. return img_rotate(img, win, 1);
  246. }
  247. int img_toggle_antialias(img_t *img) {
  248. if (!img)
  249. return 0;
  250. img->aa ^= 1;
  251. imlib_context_set_anti_alias(img->aa);
  252. return 1;
  253. }