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