A Simple X Image Viewer
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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