Split img_fit into internal/external function

This commit is contained in:
Bert 2011-02-03 16:55:24 +01:00
parent c6556f8cc7
commit 414fa567ce
3 changed files with 25 additions and 19 deletions

40
image.c
View File

@ -99,7 +99,7 @@ int img_load(img_t *img, const char *filename) {
} }
void img_check_pan(img_t *img, win_t *win) { void img_check_pan(img_t *img, win_t *win) {
if (!img) if (!img || !win)
return; return;
if (img->w * img->zoom > win->w) { if (img->w * img->zoom > win->w) {
@ -120,6 +120,23 @@ void img_check_pan(img_t *img, win_t *win) {
} }
} }
int img_fit(img_t *img, win_t *win) {
float oz, zw, zh;
if (!img || !win)
return 0;
oz = img->zoom;
zw = (float) win->w / (float) img->w;
zh = (float) win->h / (float) img->h;
img->zoom = MIN(zw, zh);
img->zoom = MAX(img->zoom, zoom_min);
img->zoom = MIN(img->zoom, zoom_max);
return oz != img->zoom;
}
void img_render(img_t *img, win_t *win) { void img_render(img_t *img, win_t *win) {
int sx, sy, sw, sh; int sx, sy, sw, sh;
int dx, dy, dw, dh; int dx, dy, dw, dh;
@ -128,7 +145,7 @@ void img_render(img_t *img, win_t *win) {
return; return;
if (img->scalemode != SCALE_ZOOM) { if (img->scalemode != SCALE_ZOOM) {
img_fit(img, win, 0); img_fit(img, win);
if (img->scalemode == SCALE_DOWN && img->zoom > 1.0) if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
img->zoom = 1.0; img->zoom = 1.0;
} }
@ -176,24 +193,12 @@ void img_render(img_t *img, win_t *win) {
win_draw(win); win_draw(win);
} }
int img_fit(img_t *img, win_t *win, unsigned char set) { int img_fit_win(img_t *img, win_t *win) {
float oz, zw, zh;
if (!img || !win) if (!img || !win)
return 0; return 0;
oz = img->zoom; img->scalemode = SCALE_FIT;
zw = (float) win->w / (float) img->w; return img_fit(img, win);
zh = (float) win->h / (float) img->h;
img->zoom = MIN(zw, zh);
img->zoom = MAX(img->zoom, zoom_min);
img->zoom = MIN(img->zoom, zoom_max);
if (set)
img->scalemode = SCALE_FIT;
return oz != img->zoom;
} }
int img_center(img_t *img, win_t *win) { int img_center(img_t *img, win_t *win) {
@ -217,6 +222,7 @@ int img_zoom(img_t *img, float z) {
z = MAX(z, zoom_min); z = MAX(z, zoom_min);
z = MIN(z, zoom_max); z = MIN(z, zoom_max);
img->scalemode = SCALE_ZOOM; img->scalemode = SCALE_ZOOM;
if (z != img->zoom) { if (z != img->zoom) {

View File

@ -54,7 +54,7 @@ int img_load(img_t*, const char*);
void img_render(img_t*, win_t*); void img_render(img_t*, win_t*);
int img_fit(img_t*, win_t*, unsigned char); int img_fit_win(img_t*, win_t*);
int img_center(img_t*, win_t*); int img_center(img_t*, win_t*);
int img_zoom(img_t*, float); int img_zoom(img_t*, float);

2
main.c
View File

@ -288,7 +288,7 @@ void on_keypress(XKeyEvent *kev) {
changed = img_zoom(&img, 1.0); changed = img_zoom(&img, 1.0);
break; break;
case XK_w: case XK_w:
if ((changed = img_fit(&img, &win, 1))) if ((changed = img_fit_win(&img, &win)))
img_center(&img, &win); img_center(&img, &win);
break; break;