Refactored img_load_gif()
- Corrected handling of transparency settings for individual frames - Corrected handling of different frame dimensions & offsets
This commit is contained in:
parent
86a6f00112
commit
79bd86defb
98
image.c
98
image.c
|
@ -56,20 +56,18 @@ void img_init(img_t *img, win_t *win) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int img_load_gif(img_t *img, const fileinfo_t *file) {
|
int img_load_gif(img_t *img, const fileinfo_t *file) {
|
||||||
DATA32 *data, *ptr;
|
|
||||||
DATA32 *prev_frame = NULL;
|
|
||||||
Imlib_Image *im;
|
|
||||||
GifFileType *gif;
|
GifFileType *gif;
|
||||||
GifRowType *rows = NULL;
|
GifRowType *rows = NULL;
|
||||||
GifRecordType rec;
|
GifRecordType rec;
|
||||||
ColorMapObject *cmap;
|
ColorMapObject *cmap;
|
||||||
int i, j;
|
DATA32 bgpixel, *data, *ptr;
|
||||||
int bg, r, g, b;
|
DATA32 *prev_frame = NULL;
|
||||||
int w = 0, h = 0;
|
Imlib_Image *im;
|
||||||
|
int i, j, bg, r, g, b;
|
||||||
|
int x, y, w, h, sw, sh;
|
||||||
int intoffset[] = { 0, 4, 2, 1 };
|
int intoffset[] = { 0, 4, 2, 1 };
|
||||||
int intjump[] = { 8, 8, 4, 2 };
|
int intjump[] = { 8, 8, 4, 2 };
|
||||||
int transp = -1;
|
int err = 0, transp = -1;
|
||||||
int err = 0;
|
|
||||||
|
|
||||||
if (img->multi.cap == 0) {
|
if (img->multi.cap == 0) {
|
||||||
img->multi.cap = 8;
|
img->multi.cap = 8;
|
||||||
|
@ -77,13 +75,16 @@ int img_load_gif(img_t *img, const fileinfo_t *file) {
|
||||||
s_malloc(sizeof(Imlib_Image*) * img->multi.cap);
|
s_malloc(sizeof(Imlib_Image*) * img->multi.cap);
|
||||||
}
|
}
|
||||||
img->multi.cnt = 0;
|
img->multi.cnt = 0;
|
||||||
img->multi.cur = 0;
|
img->multi.sel = 0;
|
||||||
|
|
||||||
gif = DGifOpenFileName(file->path);
|
gif = DGifOpenFileName(file->path);
|
||||||
if (!gif) {
|
if (!gif) {
|
||||||
warn("could not open gif file: %s", file->name);
|
warn("could not open gif file: %s", file->name);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
bg = gif->SBackGroundColor;
|
||||||
|
sw = gif->SWidth;
|
||||||
|
sh = gif->SHeight;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (DGifGetRecordType(gif, &rec) == GIF_ERROR) {
|
if (DGifGetRecordType(gif, &rec) == GIF_ERROR) {
|
||||||
|
@ -97,8 +98,12 @@ int img_load_gif(img_t *img, const fileinfo_t *file) {
|
||||||
|
|
||||||
DGifGetExtension(gif, &ext_code, &ext);
|
DGifGetExtension(gif, &ext_code, &ext);
|
||||||
while (ext) {
|
while (ext) {
|
||||||
if ((ext_code == 0xf9) && (ext[1] & 1) && (transp < 0))
|
if (ext_code == 0xf9) {
|
||||||
transp = (int) ext[4];
|
if (ext[1] & 1)
|
||||||
|
transp = (int) ext[4];
|
||||||
|
else
|
||||||
|
transp = -1;
|
||||||
|
}
|
||||||
ext = NULL;
|
ext = NULL;
|
||||||
DGifGetExtensionNext(gif, &ext);
|
DGifGetExtensionNext(gif, &ext);
|
||||||
}
|
}
|
||||||
|
@ -108,8 +113,11 @@ int img_load_gif(img_t *img, const fileinfo_t *file) {
|
||||||
err = 1;
|
err = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
x = gif->Image.Left;
|
||||||
|
y = gif->Image.Top;
|
||||||
w = gif->Image.Width;
|
w = gif->Image.Width;
|
||||||
h = gif->Image.Height;
|
h = gif->Image.Height;
|
||||||
|
|
||||||
rows = (GifRowType*) s_malloc(h * sizeof(GifRowType));
|
rows = (GifRowType*) s_malloc(h * sizeof(GifRowType));
|
||||||
for (i = 0; i < h; i++)
|
for (i = 0; i < h; i++)
|
||||||
rows[i] = (GifRowType) s_malloc(w * sizeof(GifPixelType));
|
rows[i] = (GifRowType) s_malloc(w * sizeof(GifPixelType));
|
||||||
|
@ -123,36 +131,36 @@ int img_load_gif(img_t *img, const fileinfo_t *file) {
|
||||||
DGifGetLine(gif, rows[i], w);
|
DGifGetLine(gif, rows[i], w);
|
||||||
}
|
}
|
||||||
|
|
||||||
bg = gif->SBackGroundColor;
|
ptr = data = (DATA32*) s_malloc(sizeof(DATA32) * sw * sh);
|
||||||
cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
|
cmap = gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap;
|
||||||
ptr = data = (DATA32*) s_malloc(sizeof(DATA32) * w * h);
|
r = cmap->Colors[bg].Red;
|
||||||
|
g = cmap->Colors[bg].Green;
|
||||||
|
b = cmap->Colors[bg].Blue;
|
||||||
|
bgpixel = 0x00ffffff & (r << 16 | g << 8 | b);
|
||||||
|
|
||||||
if (img->multi.cnt) {
|
for (i = 0; i < sh; i++) {
|
||||||
imlib_context_set_image(img->multi.frames[img->multi.cnt - 1]);
|
for (j = 0; j < sw; j++) {
|
||||||
prev_frame = imlib_image_get_data_for_reading_only();
|
if (i < y || i >= y + h || j < x || j >= x + w) {
|
||||||
}
|
if (transp >= 0 && prev_frame)
|
||||||
|
*ptr = prev_frame[i * sw + j];
|
||||||
for (i = 0; i < h; i++) {
|
else
|
||||||
for (j = 0; j < w; j++) {
|
*ptr = bgpixel;
|
||||||
if (rows[i][j] == transp) {
|
} else if (rows[i-y][j-x] == transp) {
|
||||||
if (prev_frame) {
|
if (prev_frame)
|
||||||
*ptr++ = prev_frame[i * w + j];
|
*ptr = prev_frame[i * sw + j];
|
||||||
} else {
|
else
|
||||||
r = cmap->Colors[bg].Red;
|
*ptr = bgpixel;
|
||||||
g = cmap->Colors[bg].Green;
|
|
||||||
b = cmap->Colors[bg].Blue;
|
|
||||||
*ptr++ = 0x00ffffff & ((r << 16) | (g << 8) | b);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
r = cmap->Colors[rows[i][j]].Red;
|
r = cmap->Colors[rows[i-y][j-x]].Red;
|
||||||
g = cmap->Colors[rows[i][j]].Green;
|
g = cmap->Colors[rows[i-y][j-x]].Green;
|
||||||
b = cmap->Colors[rows[i][j]].Blue;
|
b = cmap->Colors[rows[i-y][j-x]].Blue;
|
||||||
*ptr++ = (0xff << 24) | (r << 16) | (g << 8) | b;
|
*ptr = 0xff << 24 | r << 16 | g << 8 | b;
|
||||||
}
|
}
|
||||||
|
ptr++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
im = imlib_create_image_using_copied_data(w, h, data);
|
im = imlib_create_image_using_copied_data(sw, sh, data);
|
||||||
|
|
||||||
for (i = 0; i < h; i++)
|
for (i = 0; i < h; i++)
|
||||||
free(rows[i]);
|
free(rows[i]);
|
||||||
|
@ -166,6 +174,8 @@ int img_load_gif(img_t *img, const fileinfo_t *file) {
|
||||||
}
|
}
|
||||||
|
|
||||||
imlib_context_set_image(im);
|
imlib_context_set_image(im);
|
||||||
|
prev_frame = imlib_image_get_data_for_reading_only();
|
||||||
|
|
||||||
imlib_image_set_format("gif");
|
imlib_image_set_format("gif");
|
||||||
if (transp >= 0)
|
if (transp >= 0)
|
||||||
imlib_image_set_has_alpha(1);
|
imlib_image_set_has_alpha(1);
|
||||||
|
@ -200,7 +210,7 @@ int img_load_gif(img_t *img, const fileinfo_t *file) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int img_load(img_t *img, const fileinfo_t *file) {
|
int img_load(img_t *img, const fileinfo_t *file) {
|
||||||
char *fmt;
|
const char *fmt;
|
||||||
|
|
||||||
if (!img || !file || !file->name || !file->path)
|
if (!img || !file || !file->name || !file->path)
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -229,7 +239,19 @@ int img_load(img_t *img, const fileinfo_t *file) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void img_close(img_t *img, int decache) {
|
void img_close(img_t *img, int decache) {
|
||||||
if (img && img->im) {
|
int i;
|
||||||
|
|
||||||
|
if (!img)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (img->multi.cnt) {
|
||||||
|
for (i = 0; i < img->multi.cnt; i++) {
|
||||||
|
imlib_context_set_image(img->multi.frames[i]);
|
||||||
|
imlib_free_image();
|
||||||
|
}
|
||||||
|
img->multi.cnt = 0;
|
||||||
|
img->im = NULL;
|
||||||
|
} else if (img->im) {
|
||||||
imlib_context_set_image(img->im);
|
imlib_context_set_image(img->im);
|
||||||
if (decache)
|
if (decache)
|
||||||
imlib_free_image_and_decache();
|
imlib_free_image_and_decache();
|
||||||
|
@ -350,13 +372,13 @@ int img_change_frame(img_t *img, int d) {
|
||||||
if (!img || !img->multi.cnt || !d)
|
if (!img || !img->multi.cnt || !d)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
d += img->multi.cur;
|
d += img->multi.sel;
|
||||||
if (d < 0)
|
if (d < 0)
|
||||||
d = 0;
|
d = 0;
|
||||||
else if (d >= img->multi.cnt)
|
else if (d >= img->multi.cnt)
|
||||||
d = img->multi.cnt - 1;
|
d = img->multi.cnt - 1;
|
||||||
|
|
||||||
img->multi.cur = d;
|
img->multi.sel = d;
|
||||||
img->im = img->multi.frames[d];
|
img->im = img->multi.frames[d];
|
||||||
|
|
||||||
imlib_context_set_image(img->im);
|
imlib_context_set_image(img->im);
|
||||||
|
|
2
image.h
2
image.h
|
@ -27,7 +27,7 @@
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int cap;
|
int cap;
|
||||||
int cnt;
|
int cnt;
|
||||||
int cur;
|
int sel;
|
||||||
Imlib_Image **frames;
|
Imlib_Image **frames;
|
||||||
} multi_img_t;
|
} multi_img_t;
|
||||||
|
|
||||||
|
|
2
main.c
2
main.c
|
@ -158,7 +158,7 @@ void update_title() {
|
||||||
n = snprintf(win_title, TITLE_LEN,
|
n = snprintf(win_title, TITLE_LEN,
|
||||||
"sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) {%d/%d} %s",
|
"sxiv: [%d/%d] <%d%%> <%dx%d> (%.2f%s) {%d/%d} %s",
|
||||||
fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
|
fileidx + 1, filecnt, (int) (img.zoom * 100.0), img.w,
|
||||||
img.h, size, unit, img.multi.cur + 1, img.multi.cnt,
|
img.h, size, unit, img.multi.sel + 1, img.multi.cnt,
|
||||||
files[fileidx].name);
|
files[fileidx].name);
|
||||||
else
|
else
|
||||||
n = snprintf(win_title, TITLE_LEN,
|
n = snprintf(win_title, TITLE_LEN,
|
||||||
|
|
Loading…
Reference in a new issue