A Simple X Image Viewer
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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