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.
 
 
 
 
 
 

190 líneas
4.1 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 <stdlib.h>
  19. #include <stdio.h>
  20. #include <Imlib2.h>
  21. #include "sxiv.h"
  22. #include "image.h"
  23. void imlib_init(win_t *win) {
  24. if (!win)
  25. return;
  26. imlib_context_set_display(win->env.dpy);
  27. imlib_context_set_visual(win->env.vis);
  28. imlib_context_set_colormap(win->env.cmap);
  29. }
  30. void imlib_destroy() {
  31. if (imlib_context_get_image())
  32. imlib_free_image();
  33. }
  34. int img_load(img_t *img, const char *filename) {
  35. Imlib_Image *im;
  36. if (!img || !filename)
  37. return -1;
  38. if (imlib_context_get_image())
  39. imlib_free_image();
  40. if (!(im = imlib_load_image(filename))) {
  41. WARN("could not open image: %s", filename);
  42. return -1;
  43. }
  44. imlib_context_set_image(im);
  45. img->w = imlib_image_get_width();
  46. img->h = imlib_image_get_height();
  47. return 0;
  48. }
  49. void img_display(img_t *img, win_t *win) {
  50. float zw, zh;
  51. if (!img || !win || !imlib_context_get_image())
  52. return;
  53. /* set zoom level to fit image into window */
  54. if (img->scalemode != SCALE_ZOOM) {
  55. zw = (float) win->w / (float) img->w;
  56. zh = (float) win->h / (float) img->h;
  57. img->zoom = MIN(zw, zh);
  58. if (img->zoom * 100.0 < ZOOM_MIN)
  59. img->zoom = ZOOM_MIN / 100.0;
  60. else if (img->zoom * 100.0 > ZOOM_MAX)
  61. img->zoom = ZOOM_MAX / 100.0;
  62. if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
  63. img->zoom = 1.0;
  64. }
  65. /* center image in window */
  66. img->x = (win->w - img->w * img->zoom) / 2;
  67. img->y = (win->h - img->h * img->zoom) / 2;
  68. img_render(img, win);
  69. }
  70. void img_check_pan(img_t *img, win_t *win) {
  71. if (!img)
  72. return;
  73. if (img->w * img->zoom > win->w) {
  74. if (img->x > 0 && img->x + img->w * img->zoom > win->w)
  75. img->x = 0;
  76. if (img->x < 0 && img->x + img->w * img->zoom < win->w)
  77. img->x = win->w - img->w * img->zoom;
  78. } else {
  79. img->x = (win->w - img->w * img->zoom) / 2;
  80. }
  81. if (img->h * img->zoom > win->h) {
  82. if (img->y > 0 && img->y + img->h * img->zoom > win->h)
  83. img->y = 0;
  84. if (img->y < 0 && img->y + img->h * img->zoom < win->h)
  85. img->y = win->h - img->h * img->zoom;
  86. } else {
  87. img->y = (win->h - img->h * img->zoom) / 2;
  88. }
  89. }
  90. void img_render(img_t *img, win_t *win) {
  91. int sx, sy, sw, sh;
  92. int dx, dy, dw, dh;
  93. if (!img || !win || !imlib_context_get_image())
  94. return;
  95. img_check_pan(img, win);
  96. if (img->x < 0) {
  97. sx = -img->x / img->zoom;
  98. sw = win->w / img->zoom;
  99. dx = 0;
  100. dw = win->w;
  101. } else {
  102. sx = 0;
  103. sw = img->w;
  104. dx = img->x;
  105. dw = img->w * img->zoom;
  106. }
  107. if (img->y < 0) {
  108. sy = -img->y / img->zoom;
  109. sh = win->h / img->zoom;
  110. dy = 0;
  111. dh = win->h;
  112. } else {
  113. sy = 0;
  114. sh = img->h;
  115. dy = img->y;
  116. dh = img->h * img->zoom;
  117. }
  118. win_clear(win);
  119. imlib_context_set_drawable(win->pm);
  120. imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
  121. win_draw(win);
  122. }
  123. int img_zoom(img_t *img, int d) {
  124. int ad, iz;
  125. float z;
  126. if (!img)
  127. return 0;
  128. ad = ABS(d);
  129. iz = (int) (img->zoom * 1000.0) + d;
  130. if (iz % ad > ad / 2)
  131. iz += ad - iz % ad;
  132. else
  133. iz -= iz % ad;
  134. z = (float) iz / 1000.0;
  135. if (z * 100.0 < ZOOM_MIN)
  136. z = ZOOM_MIN / 100.0;
  137. else if (z * 100.0 > ZOOM_MAX)
  138. z = ZOOM_MAX / 100.0;
  139. if (z != img->zoom) {
  140. img->x -= (img->w * z - img->w * img->zoom) / 2;
  141. img->y -= (img->h * z - img->h * img->zoom) / 2;
  142. img->zoom = z;
  143. return 1;
  144. } else {
  145. return 0;
  146. }
  147. }
  148. int img_zoom_in(img_t *img) {
  149. return img_zoom(img, 125);
  150. }
  151. int img_zoom_out(img_t *img) {
  152. return img_zoom(img, -125);
  153. }