Added support for multi-frame images

This commit is contained in:
Bert 2011-08-17 00:56:18 +02:00
parent a85707ef63
commit 86a6f00112
7 changed files with 206 additions and 5 deletions

View File

@ -7,7 +7,7 @@ DESTDIR =
PREFIX = /usr/local PREFIX = /usr/local
CFLAGS = -Wall -pedantic -O2 -DVERSION=\"$(VERSION)\" CFLAGS = -Wall -pedantic -O2 -DVERSION=\"$(VERSION)\"
LDFLAGS = LDFLAGS =
LIBS = -lX11 -lImlib2 LIBS = -lX11 -lImlib2 -lgif
SRC = commands.c image.c main.c options.c thumbs.c util.c window.c SRC = commands.c image.c main.c options.c thumbs.c util.c window.c
OBJ = $(SRC:.c=.o) OBJ = $(SRC:.c=.o)

View File

@ -147,6 +147,10 @@ int it_last(arg_t a) {
} }
} }
int i_navigate_frame(arg_t a) {
return img_change_frame(&img, (int) a);
}
int it_move(arg_t a) { int it_move(arg_t a) {
direction_t dir = (direction_t) a; direction_t dir = (direction_t) a;

View File

@ -47,6 +47,7 @@ int it_remove_image(arg_t);
int i_navigate(arg_t); int i_navigate(arg_t);
int it_first(arg_t); int it_first(arg_t);
int it_last(arg_t); int it_last(arg_t);
int i_navigate_frame(arg_t);
int it_move(arg_t); int it_move(arg_t);
int i_pan_screen(arg_t); int i_pan_screen(arg_t);
int i_pan_edge(arg_t); int i_pan_edge(arg_t);

View File

@ -57,6 +57,9 @@ static const keymap_t keys[] = {
{ False, XK_g, it_first, (arg_t) None }, { False, XK_g, it_first, (arg_t) None },
{ False, XK_G, it_last, (arg_t) None }, { False, XK_G, it_last, (arg_t) None },
{ False, XK_N, i_navigate_frame, (arg_t) +1 },
{ False, XK_P, i_navigate_frame, (arg_t) -1 },
{ False, XK_h, it_move, (arg_t) DIR_LEFT }, { False, XK_h, it_move, (arg_t) DIR_LEFT },
{ False, XK_Left, it_move, (arg_t) DIR_LEFT }, { False, XK_Left, it_move, (arg_t) DIR_LEFT },
{ False, XK_j, it_move, (arg_t) DIR_DOWN }, { False, XK_j, it_move, (arg_t) DIR_DOWN },

176
image.c
View File

@ -18,7 +18,11 @@
#define _IMAGE_CONFIG #define _IMAGE_CONFIG
#include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h>
#include <gif_lib.h>
#include "image.h" #include "image.h"
#include "options.h" #include "options.h"
@ -36,6 +40,7 @@ void img_init(img_t *img, win_t *win) {
if (img) { if (img) {
img->im = NULL; img->im = NULL;
img->multi.cap = img->multi.cnt = 0;
img->zoom = options->zoom; img->zoom = options->zoom;
img->zoom = MAX(img->zoom, zoom_min); img->zoom = MAX(img->zoom, zoom_min);
img->zoom = MIN(img->zoom, zoom_max); img->zoom = MIN(img->zoom, zoom_max);
@ -50,7 +55,153 @@ void img_init(img_t *img, win_t *win) {
} }
} }
int img_load_gif(img_t *img, const fileinfo_t *file) {
DATA32 *data, *ptr;
DATA32 *prev_frame = NULL;
Imlib_Image *im;
GifFileType *gif;
GifRowType *rows = NULL;
GifRecordType rec;
ColorMapObject *cmap;
int i, j;
int bg, r, g, b;
int w = 0, h = 0;
int intoffset[] = { 0, 4, 2, 1 };
int intjump[] = { 8, 8, 4, 2 };
int transp = -1;
int err = 0;
if (img->multi.cap == 0) {
img->multi.cap = 8;
img->multi.frames = (Imlib_Image**)
s_malloc(sizeof(Imlib_Image*) * img->multi.cap);
}
img->multi.cnt = 0;
img->multi.cur = 0;
gif = DGifOpenFileName(file->path);
if (!gif) {
warn("could not open gif file: %s", file->name);
return 0;
}
do {
if (DGifGetRecordType(gif, &rec) == GIF_ERROR) {
warn("could not open gif file: %s", file->name);
err = 1;
break;
}
if (rec == EXTENSION_RECORD_TYPE) {
int ext_code;
GifByteType *ext = NULL;
DGifGetExtension(gif, &ext_code, &ext);
while (ext) {
if ((ext_code == 0xf9) && (ext[1] & 1) && (transp < 0))
transp = (int) ext[4];
ext = NULL;
DGifGetExtensionNext(gif, &ext);
}
} else if (rec == IMAGE_DESC_RECORD_TYPE) {
if (DGifGetImageDesc(gif) == GIF_ERROR) {
warn("could not open gif frame # %d: %s", img->multi.cnt, file->name);
err = 1;
break;
}
w = gif->Image.Width;
h = gif->Image.Height;
rows = (GifRowType*) s_malloc(h * sizeof(GifRowType));
for (i = 0; i < h; i++)
rows[i] = (GifRowType) s_malloc(w * sizeof(GifPixelType));
if (gif->Image.Interlace) {
for (i = 0; i < 4; i++) {
for (j = intoffset[i]; j < h; j += intjump[i])
DGifGetLine(gif, rows[j], w);
}
} else {
for (i = 0; i < h; i++)
DGifGetLine(gif, rows[i], w);
}
bg = gif->SBackGroundColor;
cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
ptr = data = (DATA32*) s_malloc(sizeof(DATA32) * w * h);
if (img->multi.cnt) {
imlib_context_set_image(img->multi.frames[img->multi.cnt - 1]);
prev_frame = imlib_image_get_data_for_reading_only();
}
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
if (rows[i][j] == transp) {
if (prev_frame) {
*ptr++ = prev_frame[i * w + j];
} else {
r = cmap->Colors[bg].Red;
g = cmap->Colors[bg].Green;
b = cmap->Colors[bg].Blue;
*ptr++ = 0x00ffffff & ((r << 16) | (g << 8) | b);
}
} else {
r = cmap->Colors[rows[i][j]].Red;
g = cmap->Colors[rows[i][j]].Green;
b = cmap->Colors[rows[i][j]].Blue;
*ptr++ = (0xff << 24) | (r << 16) | (g << 8) | b;
}
}
}
im = imlib_create_image_using_copied_data(w, h, data);
for (i = 0; i < h; i++)
free(rows[i]);
free(rows);
free(data);
if (!im) {
warn("could not open gif frame # %d: %s", img->multi.cnt, file->name);
err = 1;
break;
}
imlib_context_set_image(im);
imlib_image_set_format("gif");
if (transp >= 0)
imlib_image_set_has_alpha(1);
if (img->multi.cnt == img->multi.cap) {
img->multi.cap *= 2;
img->multi.frames = (Imlib_Image**)
s_realloc(img->multi.frames,
img->multi.cap * sizeof(Imlib_Image*));
}
img->multi.frames[img->multi.cnt++] = im;
}
} while (rec != TERMINATE_RECORD_TYPE);
DGifCloseFile(gif);
if (!err && img->multi.cnt > 1) {
imlib_context_set_image(img->im);
imlib_free_image();
img->im = img->multi.frames[0];
} else {
for (i = 0; i < img->multi.cnt; i++) {
imlib_context_set_image(img->multi.frames[i]);
imlib_free_image();
}
img->multi.cnt = 0;
}
imlib_context_set_image(img->im);
return !err;
}
int img_load(img_t *img, const fileinfo_t *file) { int img_load(img_t *img, const fileinfo_t *file) {
char *fmt;
if (!img || !file || !file->name || !file->path) if (!img || !file || !file->name || !file->path)
return 0; return 0;
@ -63,6 +214,10 @@ int img_load(img_t *img, const fileinfo_t *file) {
imlib_image_set_changes_on_disk(); imlib_image_set_changes_on_disk();
imlib_context_set_anti_alias(img->aa); imlib_context_set_anti_alias(img->aa);
fmt = imlib_image_format();
if (!strcmp(fmt, "gif"))
img_load_gif(img, file);
img->scalemode = options->scalemode; img->scalemode = options->scalemode;
img->re = 0; img->re = 0;
img->checkpan = 0; img->checkpan = 0;
@ -191,6 +346,27 @@ void img_render(img_t *img, win_t *win) {
win_draw(win); win_draw(win);
} }
int img_change_frame(img_t *img, int d) {
if (!img || !img->multi.cnt || !d)
return 0;
d += img->multi.cur;
if (d < 0)
d = 0;
else if (d >= img->multi.cnt)
d = img->multi.cnt - 1;
img->multi.cur = d;
img->im = img->multi.frames[d];
imlib_context_set_image(img->im);
img->w = imlib_image_get_width();
img->h = imlib_image_get_height();
img->checkpan = 1;
return 1;
}
int img_fit_win(img_t *img, win_t *win) { int img_fit_win(img_t *img, win_t *win) {
if (!img || !img->im || !win) if (!img || !img->im || !win)
return 0; return 0;

10
image.h
View File

@ -24,8 +24,16 @@
#include "types.h" #include "types.h"
#include "window.h" #include "window.h"
typedef struct {
int cap;
int cnt;
int cur;
Imlib_Image **frames;
} multi_img_t;
typedef struct { typedef struct {
Imlib_Image *im; Imlib_Image *im;
multi_img_t multi;
float zoom; float zoom;
scalemode_t scalemode; scalemode_t scalemode;
@ -48,6 +56,8 @@ void img_close(img_t*, int);
void img_render(img_t*, win_t*); void img_render(img_t*, win_t*);
int img_change_frame(img_t*, int);
int img_fit_win(img_t*, win_t*); int img_fit_win(img_t*, win_t*);
int img_center(img_t*, win_t*); int img_center(img_t*, win_t*);

15
main.c
View File

@ -154,10 +154,17 @@ void update_title() {
} else { } else {
size = filesize; size = filesize;
size_readable(&size, &unit); size_readable(&size, &unit);
n = snprintf(win_title, TITLE_LEN, if (img.multi.cnt)
"sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) %s", n = snprintf(win_title, TITLE_LEN,
fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w, img.h, "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) {%d/%d} %s",
size, unit, files[fileidx].name); fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
img.h, size, unit, img.multi.cur + 1, img.multi.cnt,
files[fileidx].name);
else
n = snprintf(win_title, TITLE_LEN,
"sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) %s",
fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
img.h, size, unit, files[fileidx].name);
} }
if (n >= TITLE_LEN) { if (n >= TITLE_LEN) {