nsxiv56142bd
Refactored merged rotation code
+20 −30
5 files changed
- M commands.c +5 −12
- M config.def.h +3 −3
- M image.c +7 −10
- M image.h +1 −1
- M types.h +4 −4
| @@ -397,21 +397,14 @@ bool i_fit_to_img(arg_t a) | |||
| 397 | 397 | ||
| 398 | 398 | bool i_rotate(arg_t a) | |
| 399 | 399 | { | |
| 400 | − | rotate_t rot = (rotate_t) a; | |
| 400 | + | degree_t degree = (degree_t) a; | |
| 401 | 401 | ||
| 402 | 402 | if (mode == MODE_IMAGE) { | |
| 403 | − | if (rot == ROTATE_90) { | |
| 404 | − | img_rotate(&img, 1); | |
| 405 | − | return true; | |
| 406 | − | } else if (rot == ROTATE_270) { | |
| 407 | − | img_rotate(&img, 3); | |
| 408 | − | return true; | |
| 409 | − | } else if (rot == ROTATE_180) { | |
| 410 | − | img_rotate(&img, 2); | |
| 411 | − | return true; | |
| 412 | − | } | |
| 403 | + | img_rotate(&img, degree); | |
| 404 | + | return true; | |
| 405 | + | } else { | |
| 406 | + | return false; | |
| 413 | 407 | } | |
| 414 | − | return false; | |
| 415 | 408 | } | |
| 416 | 409 | ||
| 417 | 410 | bool i_flip(arg_t a) | |
| @@ -114,9 +114,9 @@ static const keymap_t keys[] = { | |||
| 114 | 114 | { false, XK_E, i_fit_to_win, (arg_t) SCALE_HEIGHT }, | |
| 115 | 115 | { false, XK_W, i_fit_to_img, (arg_t) None }, | |
| 116 | 116 | ||
| 117 | − | { false, XK_less, i_rotate, (arg_t) ROTATE_270 }, | |
| 118 | − | { false, XK_greater, i_rotate, (arg_t) ROTATE_90 }, | |
| 119 | − | { false, XK_question, i_rotate, (arg_t) ROTATE_180 }, | |
| 117 | + | { false, XK_less, i_rotate, (arg_t) DEGREE_270 }, | |
| 118 | + | { false, XK_greater, i_rotate, (arg_t) DEGREE_90 }, | |
| 119 | + | { false, XK_question, i_rotate, (arg_t) DEGREE_180 }, | |
| 120 | 120 | ||
| 121 | 121 | { false, XK_backslash, i_flip, (arg_t) FLIP_HORIZONTAL }, | |
| 122 | 122 | { false, XK_bar, i_flip, (arg_t) FLIP_VERTICAL }, | |
| @@ -650,25 +650,22 @@ bool img_pan_edge(img_t *img, direction_t dir) | |||
| 650 | 650 | } | |
| 651 | 651 | } | |
| 652 | 652 | ||
| 653 | − | void img_rotate(img_t *img, int d) | |
| 653 | + | void img_rotate(img_t *img, degree_t d) | |
| 654 | 654 | { | |
| 655 | − | win_t *win; | |
| 656 | 655 | int ox, oy, tmp; | |
| 657 | 656 | ||
| 658 | 657 | if (img == NULL || img->im == NULL || img->win == NULL) | |
| 659 | 658 | return; | |
| 660 | 659 | ||
| 661 | − | win = img->win; | |
| 662 | − | ox = d == 1 ? img->x : win->w - img->x - img->w * img->zoom; | |
| 663 | − | oy = d == 3 ? img->y : win->h - img->y - img->h * img->zoom; | |
| 664 | − | ||
| 665 | 660 | imlib_context_set_image(img->im); | |
| 666 | − | /* rotates by `90 * d` degrees in the clockwise direction */ | |
| 667 | 661 | imlib_image_orientate(d); | |
| 668 | 662 | ||
| 669 | − | if (d == 1 || d == 3) { | |
| 670 | − | img->x = oy + (win->w - win->h) / 2; | |
| 671 | − | img->y = ox + (win->h - win->w) / 2; | |
| 663 | + | if (d == DEGREE_90 || d == DEGREE_270) { | |
| 664 | + | ox = d == DEGREE_90 ? img->x : img->win->w - img->x - img->w * img->zoom; | |
| 665 | + | oy = d == DEGREE_270 ? img->y : img->win->h - img->y - img->h * img->zoom; | |
| 666 | + | ||
| 667 | + | img->x = oy + (img->win->w - img->win->h) / 2; | |
| 668 | + | img->y = ox + (img->win->h - img->win->w) / 2; | |
| 672 | 669 | ||
| 673 | 670 | tmp = img->w; | |
| 674 | 671 | img->w = img->h; | |
| @@ -76,7 +76,7 @@ bool img_move(img_t*, float, float); | |||
| 76 | 76 | bool img_pan(img_t*, direction_t, int); | |
| 77 | 77 | bool img_pan_edge(img_t*, direction_t); | |
| 78 | 78 | ||
| 79 | − | void img_rotate(img_t*, int); | |
| 79 | + | void img_rotate(img_t*, degree_t); | |
| 80 | 80 | void img_flip(img_t*, flipdir_t); | |
| 81 | 81 | ||
| 82 | 82 | void img_toggle_antialias(img_t*); | |
| @@ -39,10 +39,10 @@ typedef enum { | |||
| 39 | 39 | } direction_t; | |
| 40 | 40 | ||
| 41 | 41 | typedef enum { | |
| 42 | − | ROTATE_90, | |
| 43 | − | ROTATE_270, | |
| 44 | − | ROTATE_180 | |
| 45 | − | } rotate_t; | |
| 42 | + | DEGREE_90 = 1, | |
| 43 | + | DEGREE_180 = 2, | |
| 44 | + | DEGREE_270 = 3 | |
| 45 | + | } degree_t; | |
| 46 | 46 | ||
| 47 | 47 | typedef enum { | |
| 48 | 48 | FLIP_HORIZONTAL, | |