A Simple X Image Viewer
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

364 líneas
7.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. #define _IMAGE_CONFIG
  19. #include <unistd.h>
  20. #include "image.h"
  21. #include "options.h"
  22. #include "util.h"
  23. #include "config.h"
  24. int zl_cnt;
  25. float zoom_min;
  26. float zoom_max;
  27. void img_init(img_t *img, win_t *win) {
  28. zl_cnt = sizeof(zoom_levels) / sizeof(zoom_levels[0]);
  29. zoom_min = zoom_levels[0] / 100.0;
  30. zoom_max = zoom_levels[zl_cnt - 1] / 100.0;
  31. if (img) {
  32. img->im = NULL;
  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. img->alpha = 1;
  38. }
  39. if (win) {
  40. imlib_context_set_display(win->env.dpy);
  41. imlib_context_set_visual(win->env.vis);
  42. imlib_context_set_colormap(win->env.cmap);
  43. }
  44. }
  45. int img_load(img_t *img, const char *filename) {
  46. if (!img || !filename)
  47. return 0;
  48. if (access(filename, R_OK) || !(img->im = imlib_load_image(filename))) {
  49. warn("could not open image: %s", filename);
  50. return 0;
  51. }
  52. imlib_context_set_image(img->im);
  53. imlib_image_set_changes_on_disk();
  54. imlib_context_set_anti_alias(img->aa);
  55. img->scalemode = options->scalemode;
  56. img->re = 0;
  57. img->checkpan = 0;
  58. img->w = imlib_image_get_width();
  59. img->h = imlib_image_get_height();
  60. return 1;
  61. }
  62. void img_close(img_t *img, int decache) {
  63. if (img && img->im) {
  64. imlib_context_set_image(img->im);
  65. if (decache)
  66. imlib_free_image_and_decache();
  67. else
  68. imlib_free_image();
  69. img->im = NULL;
  70. }
  71. }
  72. void img_check_pan(img_t *img, win_t *win) {
  73. if (!img || !win)
  74. return;
  75. if (img->w * img->zoom > win->w) {
  76. if (img->x > 0 && img->x + img->w * img->zoom > win->w)
  77. img->x = 0;
  78. if (img->x < 0 && img->x + img->w * img->zoom < win->w)
  79. img->x = win->w - img->w * img->zoom;
  80. } else {
  81. img->x = (win->w - img->w * img->zoom) / 2;
  82. }
  83. if (img->h * img->zoom > win->h) {
  84. if (img->y > 0 && img->y + img->h * img->zoom > win->h)
  85. img->y = 0;
  86. if (img->y < 0 && img->y + img->h * img->zoom < win->h)
  87. img->y = win->h - img->h * img->zoom;
  88. } else {
  89. img->y = (win->h - img->h * img->zoom) / 2;
  90. }
  91. }
  92. int img_fit(img_t *img, win_t *win) {
  93. float oz, zw, zh;
  94. if (!img || !win)
  95. return 0;
  96. oz = img->zoom;
  97. zw = (float) win->w / (float) img->w;
  98. zh = (float) win->h / (float) img->h;
  99. img->zoom = MIN(zw, zh);
  100. img->zoom = MAX(img->zoom, zoom_min);
  101. img->zoom = MIN(img->zoom, zoom_max);
  102. return oz != img->zoom;
  103. }
  104. void img_render(img_t *img, win_t *win) {
  105. int sx, sy, sw, sh;
  106. int dx, dy, dw, dh;
  107. if (!img || !img->im || !win)
  108. return;
  109. if (img->scalemode != SCALE_ZOOM) {
  110. img_fit(img, win);
  111. if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
  112. img->zoom = 1.0;
  113. }
  114. if (!img->re) {
  115. /* rendered for the first time */
  116. img->re = 1;
  117. if (img->zoom * img->w <= win->w)
  118. img->x = (win->w - img->w * img->zoom) / 2;
  119. else
  120. img->x = 0;
  121. if (img->zoom * img->h <= win->h)
  122. img->y = (win->h - img->h * img->zoom) / 2;
  123. else
  124. img->y = 0;
  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_image(img->im);
  155. if (imlib_image_has_alpha() && !img->alpha)
  156. win_draw_rect(win, win->pm, dx, dy, dw, dh, True, 0, win->white);
  157. imlib_context_set_drawable(win->pm);
  158. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  159. win_draw(win);
  160. }
  161. int img_fit_win(img_t *img, win_t *win) {
  162. if (!img || !img->im || !win)
  163. return 0;
  164. img->scalemode = SCALE_FIT;
  165. return img_fit(img, win);
  166. }
  167. int img_center(img_t *img, win_t *win) {
  168. int ox, oy;
  169. if (!img || !win)
  170. return 0;
  171. ox = img->x;
  172. oy = img->y;
  173. img->x = (win->w - img->w * img->zoom) / 2;
  174. img->y = (win->h - img->h * img->zoom) / 2;
  175. return ox != img->x || oy != img->y;
  176. }
  177. int img_zoom(img_t *img, win_t *win, float z) {
  178. if (!img || !img->im || !win)
  179. return 0;
  180. z = MAX(z, zoom_min);
  181. z = MIN(z, zoom_max);
  182. img->scalemode = SCALE_ZOOM;
  183. if (z != img->zoom) {
  184. img->x = win->w / 2 - (win->w / 2 - img->x) * z / img->zoom;
  185. img->y = win->h / 2 - (win->h / 2 - img->y) * z / img->zoom;
  186. img->zoom = z;
  187. img->checkpan = 1;
  188. return 1;
  189. } else {
  190. return 0;
  191. }
  192. }
  193. int img_zoom_in(img_t *img, win_t *win) {
  194. int i;
  195. if (!img || !img->im || !win)
  196. return 0;
  197. for (i = 1; i < zl_cnt; i++) {
  198. if (zoom_levels[i] > img->zoom * 100.0)
  199. return img_zoom(img, win, zoom_levels[i] / 100.0);
  200. }
  201. return 0;
  202. }
  203. int img_zoom_out(img_t *img, win_t *win) {
  204. int i;
  205. if (!img || !img->im || !win)
  206. return 0;
  207. for (i = zl_cnt - 2; i >= 0; i--) {
  208. if (zoom_levels[i] < img->zoom * 100.0)
  209. return img_zoom(img, win, zoom_levels[i] / 100.0);
  210. }
  211. return 0;
  212. }
  213. int img_move(img_t *img, win_t *win, int dx, int dy) {
  214. int ox, oy;
  215. if (!img || !img->im || !win)
  216. return 0;
  217. ox = img->x;
  218. oy = img->y;
  219. img->x += dx;
  220. img->y += dy;
  221. img_check_pan(img, win);
  222. return ox != img->x || oy != img->y;
  223. }
  224. int img_pan(img_t *img, win_t *win, direction_t dir, int page) {
  225. if (!img || !img->im || !win)
  226. return 0;
  227. switch (dir) {
  228. case DIR_LEFT:
  229. return img_move(img, win, win->w / (page ? 1 : 5), 0);
  230. case DIR_RIGHT:
  231. return img_move(img, win, win->w / (page ? 1 : 5) * -1, 0);
  232. case DIR_UP:
  233. return img_move(img, win, 0, win->h / (page ? 1 : 5));
  234. case DIR_DOWN:
  235. return img_move(img, win, 0, win->h / (page ? 1 : 5) * -1);
  236. }
  237. return 0;
  238. }
  239. int img_pan_edge(img_t *img, win_t *win, direction_t dir) {
  240. int ox, oy;
  241. if (!img || !img->im || !win)
  242. return 0;
  243. ox = img->x;
  244. oy = img->y;
  245. switch (dir) {
  246. case DIR_LEFT:
  247. img->x = 0;
  248. break;
  249. case DIR_RIGHT:
  250. img->x = win->w - img->w * img->zoom;
  251. break;
  252. case DIR_UP:
  253. img->y = 0;
  254. break;
  255. case DIR_DOWN:
  256. img->y = win->h - img->h * img->zoom;
  257. break;
  258. }
  259. img_check_pan(img, win);
  260. return ox != img->x || oy != img->y;
  261. }
  262. void img_rotate(img_t *img, win_t *win, int d) {
  263. int ox, oy, tmp;
  264. if (!img || !img->im || !win)
  265. return;
  266. ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom;
  267. oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom;
  268. imlib_context_set_image(img->im);
  269. imlib_image_orientate(d);
  270. img->x = oy + (win->w - win->h) / 2;
  271. img->y = ox + (win->h - win->w) / 2;
  272. tmp = img->w;
  273. img->w = img->h;
  274. img->h = tmp;
  275. img->checkpan = 1;
  276. }
  277. void img_rotate_left(img_t *img, win_t *win) {
  278. img_rotate(img, win, 3);
  279. }
  280. void img_rotate_right(img_t *img, win_t *win) {
  281. img_rotate(img, win, 1);
  282. }
  283. void img_toggle_antialias(img_t *img) {
  284. if (img && img->im) {
  285. img->aa ^= 1;
  286. imlib_context_set_image(img->im);
  287. imlib_context_set_anti_alias(img->aa);
  288. }
  289. }