A Simple X Image Viewer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

image.c 5.7 KiB

il y a 14 ans
il y a 14 ans
il y a 14 ans
il y a 14 ans
il y a 14 ans
il y a 14 ans
il y a 14 ans
il y a 14 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 <stdlib.h>
  19. #include <stdio.h>
  20. #include <Imlib2.h>
  21. #include "sxiv.h"
  22. #include "image.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 = 1.0;
  32. img->aa = 1;
  33. }
  34. if (win) {
  35. imlib_context_set_display(win->env.dpy);
  36. imlib_context_set_visual(win->env.vis);
  37. imlib_context_set_colormap(win->env.cmap);
  38. }
  39. }
  40. void img_free(img_t* img) {
  41. if (imlib_context_get_image())
  42. imlib_free_image();
  43. }
  44. int img_load(img_t *img, const char *filename) {
  45. Imlib_Image *im;
  46. if (!img || !filename)
  47. return -1;
  48. if (imlib_context_get_image())
  49. imlib_free_image();
  50. if (!(im = imlib_load_image(filename))) {
  51. WARN("could not open image: %s", filename);
  52. return -1;
  53. }
  54. imlib_context_set_image(im);
  55. imlib_context_set_anti_alias(img->aa);
  56. img->re = 0;
  57. img->checkpan = 0;
  58. img->zoomed = 0;
  59. img->w = imlib_image_get_width();
  60. img->h = imlib_image_get_height();
  61. return 0;
  62. }
  63. void img_check_pan(img_t *img, win_t *win) {
  64. if (!img)
  65. return;
  66. if (img->w * img->zoom > win->w) {
  67. if (img->x > 0 && img->x + img->w * img->zoom > win->w)
  68. img->x = 0;
  69. if (img->x < 0 && img->x + img->w * img->zoom < win->w)
  70. img->x = win->w - img->w * img->zoom;
  71. } else {
  72. img->x = (win->w - img->w * img->zoom) / 2;
  73. }
  74. if (img->h * img->zoom > win->h) {
  75. if (img->y > 0 && img->y + img->h * img->zoom > win->h)
  76. img->y = 0;
  77. if (img->y < 0 && img->y + img->h * img->zoom < win->h)
  78. img->y = win->h - img->h * img->zoom;
  79. } else {
  80. img->y = (win->h - img->h * img->zoom) / 2;
  81. }
  82. }
  83. void img_render(img_t *img, win_t *win) {
  84. int sx, sy, sw, sh;
  85. int dx, dy, dw, dh;
  86. float zw, zh;
  87. if (!img || !win || !imlib_context_get_image())
  88. return;
  89. if (!img->zoomed && SCALE_MODE != SCALE_ZOOM) {
  90. /* set zoom level to fit image into window */
  91. zw = (float) win->w / (float) img->w;
  92. zh = (float) win->h / (float) img->h;
  93. img->zoom = MIN(zw, zh);
  94. if (img->zoom < zoom_min)
  95. img->zoom = zoom_min;
  96. else if (img->zoom > zoom_max)
  97. img->zoom = zoom_max;
  98. if (SCALE_MODE == SCALE_DOWN && img->zoom > 1.0)
  99. img->zoom = 1.0;
  100. }
  101. if (!img->re) {
  102. /* rendered for the first time */
  103. img->re = 1;
  104. /* center image in window */
  105. img->x = (win->w - img->w * img->zoom) / 2;
  106. img->y = (win->h - img->h * img->zoom) / 2;
  107. }
  108. if (img->checkpan) {
  109. img_check_pan(img, win);
  110. img->checkpan = 0;
  111. }
  112. /* calculate source and destination offsets */
  113. if (img->x < 0) {
  114. sx = -img->x / img->zoom;
  115. sw = win->w / img->zoom;
  116. dx = 0;
  117. dw = win->w;
  118. } else {
  119. sx = 0;
  120. sw = img->w;
  121. dx = img->x;
  122. dw = img->w * img->zoom;
  123. }
  124. if (img->y < 0) {
  125. sy = -img->y / img->zoom;
  126. sh = win->h / img->zoom;
  127. dy = 0;
  128. dh = win->h;
  129. } else {
  130. sy = 0;
  131. sh = img->h;
  132. dy = img->y;
  133. dh = img->h * img->zoom;
  134. }
  135. win_clear(win);
  136. imlib_context_set_drawable(win->pm);
  137. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  138. win_draw(win);
  139. }
  140. int img_zoom(img_t *img, float z) {
  141. if (!img)
  142. return 0;
  143. if (z < zoom_min)
  144. z = zoom_min;
  145. else if (z > zoom_max)
  146. z = zoom_max;
  147. if (z != img->zoom) {
  148. img->x -= (img->w * z - img->w * img->zoom) / 2;
  149. img->y -= (img->h * z - img->h * img->zoom) / 2;
  150. img->zoom = z;
  151. img->checkpan = 1;
  152. img->zoomed = 1;
  153. return 1;
  154. } else {
  155. return 0;
  156. }
  157. }
  158. int img_zoom_in(img_t *img) {
  159. int i;
  160. if (!img)
  161. return 0;
  162. for (i = 1; i < zl_cnt; ++i) {
  163. if (zoom_levels[i] > img->zoom * 100.0)
  164. return img_zoom(img, zoom_levels[i] / 100.0);
  165. }
  166. return 0;
  167. }
  168. int img_zoom_out(img_t *img) {
  169. int i;
  170. if (!img)
  171. return 0;
  172. for (i = zl_cnt - 2; i >= 0; --i) {
  173. if (zoom_levels[i] < img->zoom * 100.0)
  174. return img_zoom(img, zoom_levels[i] / 100.0);
  175. }
  176. return 0;
  177. }
  178. int img_pan(img_t *img, win_t *win, pandir_t dir) {
  179. int ox, oy;
  180. if (!img || !win)
  181. return 0;
  182. ox = img->x;
  183. oy = img->y;
  184. switch (dir) {
  185. case PAN_LEFT:
  186. img->x += win->w / 5;
  187. break;
  188. case PAN_RIGHT:
  189. img->x -= win->w / 5;
  190. break;
  191. case PAN_UP:
  192. img->y += win->h / 5;
  193. break;
  194. case PAN_DOWN:
  195. img->y -= win->h / 5;
  196. break;
  197. }
  198. img_check_pan(img, win);
  199. return ox != img->x || oy != img->y;
  200. }
  201. int img_rotate(img_t *img, win_t *win, int d) {
  202. int ox, oy, tmp;
  203. if (!img || !win)
  204. return 0;
  205. ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom;
  206. oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom;
  207. imlib_image_orientate(d);
  208. img->x = oy + (win->w - win->h) / 2;
  209. img->y = ox + (win->h - win->w) / 2;
  210. tmp = img->w;
  211. img->w = img->h;
  212. img->h = tmp;
  213. img->checkpan = 1;
  214. return 1;
  215. }
  216. int img_rotate_left(img_t *img, win_t *win) {
  217. return img_rotate(img, win, 3);
  218. }
  219. int img_rotate_right(img_t *img, win_t *win) {
  220. return img_rotate(img, win, 1);
  221. }
  222. int img_toggle_antialias(img_t *img) {
  223. if (!img)
  224. return 0;
  225. img->aa ^= 1;
  226. imlib_context_set_anti_alias(img->aa);
  227. return 1;
  228. }