From 86a6f00112576f3fc1ca565c2b5ad11d784cab8d Mon Sep 17 00:00:00 2001 From: Bert Date: Wed, 17 Aug 2011 00:56:18 +0200 Subject: [PATCH] Added support for multi-frame images --- Makefile | 2 +- commands.c | 4 ++ commands.h | 1 + config.h | 3 + image.c | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++++ image.h | 10 +++ main.c | 15 +++-- 7 files changed, 206 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 852a966..58368a4 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ DESTDIR = PREFIX = /usr/local CFLAGS = -Wall -pedantic -O2 -DVERSION=\"$(VERSION)\" LDFLAGS = -LIBS = -lX11 -lImlib2 +LIBS = -lX11 -lImlib2 -lgif SRC = commands.c image.c main.c options.c thumbs.c util.c window.c OBJ = $(SRC:.c=.o) diff --git a/commands.c b/commands.c index 7dbd019..50a3c2b 100644 --- a/commands.c +++ b/commands.c @@ -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) { direction_t dir = (direction_t) a; diff --git a/commands.h b/commands.h index c249d29..ee2e07a 100644 --- a/commands.h +++ b/commands.h @@ -47,6 +47,7 @@ int it_remove_image(arg_t); int i_navigate(arg_t); int it_first(arg_t); int it_last(arg_t); +int i_navigate_frame(arg_t); int it_move(arg_t); int i_pan_screen(arg_t); int i_pan_edge(arg_t); diff --git a/config.h b/config.h index 442b606..a55ea18 100644 --- a/config.h +++ b/config.h @@ -57,6 +57,9 @@ static const keymap_t keys[] = { { False, XK_g, it_first, (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_Left, it_move, (arg_t) DIR_LEFT }, { False, XK_j, it_move, (arg_t) DIR_DOWN }, diff --git a/image.c b/image.c index 3f6c75c..ef78542 100644 --- a/image.c +++ b/image.c @@ -18,7 +18,11 @@ #define _IMAGE_CONFIG +#include +#include #include +#include +#include #include "image.h" #include "options.h" @@ -36,6 +40,7 @@ void img_init(img_t *img, win_t *win) { if (img) { img->im = NULL; + img->multi.cap = img->multi.cnt = 0; img->zoom = options->zoom; img->zoom = MAX(img->zoom, zoom_min); 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) { + char *fmt; + if (!img || !file || !file->name || !file->path) return 0; @@ -63,6 +214,10 @@ int img_load(img_t *img, const fileinfo_t *file) { imlib_image_set_changes_on_disk(); 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->re = 0; img->checkpan = 0; @@ -191,6 +346,27 @@ void img_render(img_t *img, win_t *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) { if (!img || !img->im || !win) return 0; diff --git a/image.h b/image.h index 0dbde1e..37db7c8 100644 --- a/image.h +++ b/image.h @@ -24,8 +24,16 @@ #include "types.h" #include "window.h" +typedef struct { + int cap; + int cnt; + int cur; + Imlib_Image **frames; +} multi_img_t; + typedef struct { Imlib_Image *im; + multi_img_t multi; float zoom; scalemode_t scalemode; @@ -48,6 +56,8 @@ void img_close(img_t*, int); void img_render(img_t*, win_t*); +int img_change_frame(img_t*, int); + int img_fit_win(img_t*, win_t*); int img_center(img_t*, win_t*); diff --git a/main.c b/main.c index 44b3e69..eb00c67 100644 --- a/main.c +++ b/main.c @@ -154,10 +154,17 @@ void update_title() { } else { size = filesize; size_readable(&size, &unit); - 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 (img.multi.cnt) + n = snprintf(win_title, TITLE_LEN, + "sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) {%d/%d} %s", + 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) {