#include "define.h"
#include "malloc.h"
#include "stdio.h"
#include "alj.h"
#include "lzw.h"
#include "stream.h"
#define FILENAME "d:\\1.gif"
int file_load(uint8_t ** buf)
{
FILE * fp;
long len;
fp=fopen(FILENAME,"rb+");
if(fp==NULL)
{
printf("The file is not exeict!\n");
getch();
return -1;
}
fseek(fp,0L,SEEK_END);
len=ftell(fp)-1;
*buf=(uint8_t *)malloc(len);
if(*buf==NULL)
{
printf("Ram is out!\n");
getch();
return -1;
}
fseek(fp,0L,SEEK_SET);
fread(*buf,1L,len,fp);
fclose(fp);
#ifdef debug
printf("The file is copied to Ram!\n");
getch();
#endif
return len;
}
#undef FILENAME
void file_close(GifState * s)
{
free(s->bytestream);
free(s->outdata);
}
int gif_head(GifState * s)
{
char sig[6];
uint8_t gif87a_sig[6] = "GIF87a";
uint8_t gif89a_sig[6] = "GIF89a";
bytestream_get_buffer(&s->bytestream, sig, 6);
if(*sig==*gif87a_sig|*sig==*gif89a_sig)
return 0;
return -1;
}
int far gif_init(GifState *s)
{
uint8_t tmp;
int n;
unsigned int len;
ff_lzw_decode_open(&s->lzw);
s->transparent_color_index=0;
s->gce_delay=0;
s->gce_disposal=0;
s->screen_width=bytestream_get_2byte(&s->bytestream);
s->screen_height=bytestream_get_2byte(&s->bytestream);
tmp=bytestream_get_1byte(&s->bytestream);
s->background_color_index=bytestream_get_1byte(&s->bytestream);
bytestream_get_1byte(&s->bytestream);
len=s->screen_width * s->screen_height;
s->outdata=(uint8_t *)malloc(len);
if(s->outdata==NULL)
{
printf("RAM is out!\n");
getch();
return -1;
}
s->color_resolution=((tmp&0x70)>>4)+1;
s->bits_per_pixel=(tmp&0x7)+1;
if((tmp&0x80)==0x80)
{
n=1<<s->bits_per_pixel;
bytestream_get_buffer(&s->bytestream,s->global_palette,3*n);
}
return 0;
}
int far gif_read_extension(GifState *s)
{
int ext_code,ext_len,i, gce_flags, gce_transparent_index;
ext_code=bytestream_get_1byte(&s->bytestream);
ext_len=bytestream_get_1byte(&s->bytestream);
switch(ext_code) {
case 0xf9:
if (ext_len != 4)
goto discard_ext;
s->transparent_color_index = -1;
gce_flags = bytestream_get_1byte(&s->bytestream);
s->gce_delay = bytestream_get_2byte(&s->bytestream);
gce_transparent_index = bytestream_get_1byte(&s->bytestream);
if (gce_flags & 0x01)
s->transparent_color_index = gce_transparent_index;
else
s->transparent_color_index = -1;
s->gce_disposal = (gce_flags >> 2) & 0x7;
#ifdef DEBUG
dprintf(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
gce_flags, s->gce_delay,
s->transparent_color_index, s->gce_disposal);
#endif
ext_len = bytestream_get_1byte(&s->bytestream);
break;
}
/* NOTE: many extension blocks can come after */
discard_ext:
while (ext_len != 0) {
for (i = 0; i < ext_len; i++)
bytestream_get_1byte(&s->bytestream);
ext_len = bytestream_get_1byte(&s->bytestream);
#ifdef DEBUG
dprintf(s->avctx, "gif: ext_len1=%d\n", ext_len);
#endif
}
return 0;
}
#define RL24(x) ((((const uint8_t*)(x))[0] << 16) | \
(((const uint8_t*)(x))[1] << 8) | \
((const uint8_t*)(x))[2])
int far gif_read_image(GifState *s)
{
uint16_t left, top, height, width;
uint8_t tmp,has_local_palette, is_interleaved;
uint8_t bits_per_pixel,linesize;
uint8_t * palette,*ptr,*ptr1;
int n,i,code_size, y, y1, pass;
left=bytestream_get_2byte(&s->bytestream);
top=bytestream_get_2byte(&s->bytestream);
width=bytestream_get_2byte(&s->bytestream);
height=bytestream_get_2byte(&s->bytestream);
tmp=bytestream_get_1byte(&s->bytestream);
bits_per_pixel=(tmp&0x7)+1;
is_interleaved=tmp&0x40;
has_local_palette=tmp&0x80;
if(has_local_palette==0x80)
{
bytestream_get_buffer(&s->bytestream,s->local_palette,3*(1<<bits_per_pixel));
palette=s->local_palette;
}
else
{
palette=s->global_palette;
bits_per_pixel = s->bits_per_pixel;
}
if((top+height)>s->screen_height||(l<input type="image" src="">eft+width)>s->screen_width)
{
printf("The image is wrong!\n");
return -1;
}
s->image_palette1=s->global_palette;
n=(1<<bits_per_pixel);
for (i=0;i<n;i++)
{
s->image_palette[i]=(0xff<<24)|RL24(palette);
palette+=3;
}
for(;i<255;i++)
{
s->image_palette[i]=(0xff<<24);
}
if(s->transparent_color_index>=0)
s->image_palette[s->transparent_color_index]=0;
code_size=bytestream_get_1byte(&s->bytestream);
ff_lzw_decode_init(s->lzw, code_size, s->bytestream,s->bytestream_end - s->bytestream, FF_LZW_GIF);
linesize=s->screen_width;
ptr1=s->outdata +top*linesize+ left;
ptr=ptr1;
y1=0;
pass=0;
for(y=0;y<height;y++)
{
ff_lzw_decode(s->lzw, ptr, width);
if (is_interleaved) {
switch(pass) {
default:
case 0:
case 1:
y1 += 8;
ptr += linesize * 8;
if (y1 >= height) {
y1 = pass ? 2 : 4;
ptr = ptr1 + linesize * y1;
pass++;
}
break;
case 2:
y1 += 4;
ptr += linesize * 4;
if (y1 >= height) {
y1 = 1;
ptr = ptr1 + linesize;
pass++;
}
break;
case 3:
y1 += 2;
ptr += linesize * 2;
break;
}
} else {
ptr += linesize;
}
}
ff_lzw_decode_tail(s->lzw);
s->bytestream = ff_lzw_cur_ptr(s->lzw);
return 2;
}
没有合适的资源?快使用搜索试试~ 我知道了~
GIF图片/动画解码程序
共11个文件
h:5个
c:5个
bgi:1个
3星 · 超过75%的资源 需积分: 9 9 下载量 97 浏览量
2009-09-02
13:55:44
上传
评论
收藏 11KB RAR 举报
温馨提示
可直接运行的GIF解码源代码,在TC2.0上编译成功,目前解码黑白图片及黑白的GIF动画没有问题,解码彩色回出乱码,怀疑与TC2.0的图形显示设置有关。
资源推荐
资源详情
资源评论
收起资源包目录
gif.rar (11个子文件)
stream.c 579B
Svga256.bgi 6KB
lzw.h 2KB
lzw.c 6KB
define.h 281B
ALJ.C 1KB
stream.h 197B
GIF.C 6KB
Svga256.h 1KB
alj.h 1KB
DISPLAY.C 814B
共 11 条
- 1
资源评论
- leogo07052012-06-08代码并没有运行起来!
kongrong
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功