浏览代码

Render image on window

master
Bert 13 年前
父节点
当前提交
79c7e6178e
共有 6 个文件被更改,包括 75 次插入7 次删除
  1. +3
    -1
      app.c
  2. +58
    -3
      image.c
  3. +2
    -2
      image.h
  4. +4
    -1
      sxiv.h
  5. +7
    -0
      window.c
  6. +1
    -0
      window.h

+ 3
- 1
app.c 查看文件

@@ -28,7 +28,7 @@ void app_init(app_t *app) {

app->fileidx = 0;

app->img.zoom = 100;
app->img.zoom = 1.0;
app->img.scalemode = SCALE_MODE;

app->win.w = WIN_WIDTH;
@@ -52,4 +52,6 @@ void app_load_image(app_t *app) {
return;

img_load(&app->img, app->filenames[app->fileidx]);

img_render(&app->img, &app->win);
}

+ 58
- 3
image.c 查看文件

@@ -25,7 +25,7 @@
void imlib_init(win_t *win) {
if (!win)
return;
imlib_context_set_display(win->env.dpy);
imlib_context_set_visual(win->env.vis);
imlib_context_set_colormap(win->env.cmap);
@@ -41,9 +41,64 @@ void img_load(img_t *img, char *filename) {

if (!(img->im = imlib_load_image(filename)))
DIE("could not open image: %s", filename);
imlib_context_set_image(img->im);
img->w = imlib_image_get_width();
img->h = imlib_image_get_height();
}

void img_render(img_t *img, win_t *win) {
float zw, zh;
unsigned int sx, sy, sw, sh;
unsigned int dx, dy, dw, dh;

if (!img || !win)
return;

/* set zoom level to fit image into window */
if (img->scalemode != SCALE_ZOOM) {
zw = (float) win->w / (float) img->w;
zh = (float) win->h / (float) img->h;
img->zoom = MIN(zw, zh);

if (img->zoom * 100.0 < ZOOM_MIN)
img->zoom = ZOOM_MIN / 100.0;
else if (img->zoom * 100.0 > ZOOM_MAX)
img->zoom = ZOOM_MAX / 100.0;

if (img->scalemode == SCALE_DOWN && img->zoom > 1.0)
img->zoom = 1.0;
}

/* center image in window */
img->x = (win->w - img->w * img->zoom) / 2;
img->y = (win->h - img->h * img->zoom) / 2;

if (img->x < 0) {
sx = -img->x / img->zoom;
sw = (img->x + win->w) / img->zoom;
dx = 0;
dw = win->w;
} else {
sx = 0;
sw = img->w;
dx = img->x;
dw = img->w * img->zoom;
}
if (img->y < 0) {
sy = -img->y / img->zoom;
sh = (img->y + win->h) / img->zoom;
dy = 0;
dh = win->h;
} else {
sy = 0;
sh = img->h;
dy = img->y;
dh = img->h * img->zoom;
}

win_clear(win);

imlib_render_image_part_on_drawable_at_size(sx, sy, sw, sh, dx, dy, dw, dh);
}

+ 2
- 2
image.h 查看文件

@@ -30,7 +30,7 @@ typedef enum scalemode_e {
} scalemode_t;

typedef struct img_s {
int zoom;
float zoom;
scalemode_t scalemode;
int w;
int h;
@@ -43,6 +43,6 @@ typedef struct img_s {
void imlib_init(win_t*);

void img_load(img_t*, char*);
void img_display(img_t*, win_t*);
void img_render(img_t*, win_t*);

#endif /* IMAGE_H */

+ 4
- 1
sxiv.h 查看文件

@@ -23,6 +23,9 @@

#define VERSION "git-20110117"

#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))

#define WARN(...) \
do { \
fprintf(stderr, "sxiv: %s:%d: warning: ", __FILE__, __LINE__); \
@@ -30,7 +33,7 @@
fprintf(stderr, "\n"); \
} while (0)

#define DIE(...) \
#define DIE(...) \
do { \
fprintf(stderr, "sxiv: %s:%d: error: ", __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \


+ 7
- 0
window.c 查看文件

@@ -106,3 +106,10 @@ int win_configure(win_t *win, XConfigureEvent *cev) {
win->bw = cev->border_width;
return changed;
}

void win_clear(win_t *win) {
if (!win)
return;

XClearWindow(win->env.dpy, win->xwin);
}

+ 1
- 0
window.h 查看文件

@@ -47,5 +47,6 @@ void win_open(win_t*);
void win_close(win_t*);

int win_configure(win_t*, XConfigureEvent*);
void win_clear(win_t*);

#endif /* WINDOW_H */

正在加载...
取消
保存