Corrected zoom level handling

This commit is contained in:
Bert Münnich 2012-08-16 13:09:44 +02:00
parent 4ce323213d
commit 8a652a786c
3 changed files with 21 additions and 9 deletions

View file

@ -1,4 +1,4 @@
VERSION = git-20120807 VERSION = git-20120816
PREFIX = /usr/local PREFIX = /usr/local
MANPREFIX = $(PREFIX)/share/man MANPREFIX = $(PREFIX)/share/man

25
image.c
View file

@ -36,9 +36,14 @@ enum { MIN_GIF_DELAY = 50 };
float zoom_min; float zoom_min;
float zoom_max; float zoom_max;
bool zoomdiff(float z1, float z2) { int zoomdiff(float z1, float z2) {
float d = z1 - z2;
const float mindelta = 0.001; const float mindelta = 0.001;
return (z1 - z2 > mindelta) || (z1 - z2 < mindelta);
if (ABS(d) < mindelta)
return 0;
else
return d < 0 ? -1 : 1;
} }
void img_init(img_t *img, win_t *win) { void img_init(img_t *img, win_t *win) {
@ -380,7 +385,7 @@ bool img_fit(img_t *img) {
z = MAX(z, zoom_min); z = MAX(z, zoom_min);
z = MIN(z, zmax); z = MIN(z, zmax);
if (zoomdiff(z, img->zoom)) { if (zoomdiff(z, img->zoom) != 0) {
img->zoom = z; img->zoom = z;
img->dirty = true; img->dirty = true;
return true; return true;
@ -496,7 +501,7 @@ bool img_zoom(img_t *img, float z) {
img->scalemode = SCALE_ZOOM; img->scalemode = SCALE_ZOOM;
if (zoomdiff(z, img->zoom)) { if (zoomdiff(z, img->zoom) != 0) {
img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * z / img->zoom; img->x = img->win->w / 2 - (img->win->w / 2 - img->x) * z / img->zoom;
img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * z / img->zoom; img->y = img->win->h / 2 - (img->win->h / 2 - img->y) * z / img->zoom;
img->zoom = z; img->zoom = z;
@ -510,26 +515,30 @@ bool img_zoom(img_t *img, float z) {
bool img_zoom_in(img_t *img) { bool img_zoom_in(img_t *img) {
int i; int i;
float z;
if (img == NULL || img->im == NULL) if (img == NULL || img->im == NULL)
return false; return false;
for (i = 1; i < ARRLEN(zoom_levels); i++) { for (i = 1; i < ARRLEN(zoom_levels); i++) {
if (zoom_levels[i] > img->zoom * 100.0) z = zoom_levels[i] / 100.0;
return img_zoom(img, zoom_levels[i] / 100.0); if (zoomdiff(z, img->zoom) > 0)
return img_zoom(img, z);
} }
return false; return false;
} }
bool img_zoom_out(img_t *img) { bool img_zoom_out(img_t *img) {
int i; int i;
float z;
if (img == NULL || img->im == NULL) if (img == NULL || img->im == NULL)
return false; return false;
for (i = ARRLEN(zoom_levels) - 2; i >= 0; i--) { for (i = ARRLEN(zoom_levels) - 2; i >= 0; i--) {
if (zoom_levels[i] < img->zoom * 100.0) z = zoom_levels[i] / 100.0;
return img_zoom(img, zoom_levels[i] / 100.0); if (zoomdiff(z, img->zoom) < 0)
return img_zoom(img, z);
} }
return false; return false;
} }

3
util.h
View file

@ -27,6 +27,9 @@
#include "types.h" #include "types.h"
#ifndef ABS
#define ABS(a) ((a) < 0 ? -(a) : (a))
#endif
#ifndef MIN #ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif #endif