⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gif.c

📁 一个简单的C语言写的GIF算法
💻 C
字号:
我也很想帮你,但是我这里只有一个写入GIF的,可以运行的,你看看它的代码    读取是写的逆操作,应该对你有帮助       /*--GIF动画生成.      程序中type的值为2则按16色的格式生成动画;type的值为3则按256色      mode的值为0按顺序方式压缩数据,mode的值为1则按交叉方式压缩      数据,生其的动画文件可直接在浏览器中播放.    TC20   small模式下编译通过--*/    #include<stdio.h>    #include<graphics.h>       #define   MAXCODES   4096    #define   TABLESIZES   4999       int   left,top,width,right,bottom,type,mode;    unsigned   char   blockbuf[256],colorbits;    int   clearcode,eofcode,encode;    int   encodetable[TABLESIZES];    int   initbits,runbits,shiftbits;    int   maxcodesize,bytecount;    unsigned   long   tempcodes;    int   linestart[]={0,4,2,1};    int   rowcount[]={8,8,4,2};       void   getcolortable(int   colortable[16][3])/*   设置16色颜色表   */    {int   i,j;      int   color[16]={0,1,2,3,4,5,22,7,56,57,58,59,60,61,62,63};      for(i=0;i<16;i++)      {j=color[i];        if(i==6)j=20;        outportb(0x3c7,((unsigned   char)j));        colortable[i][0]=inportb(0x3c9);        colortable[i][1]=inportb(0x3c9);        colortable[i][2]=inportb(0x3c9);      }    }       void   globalheader(FILE   *   fp)    {int   i,colornum,colortable[16][3];      unsigned   char   pal[768];      struct   globalhead{                unsigned   char   signature[6];                unsigned   int   width;                unsigned   int   height;                unsigned   char   flag;                unsigned   char   color;                unsigned   char   ratio;      }gif;      memcpy(gif.signature,"GIF89a",6);/*   Gif   数字签名   GIF89a   */      gif.width=right-left;      gif.height=bottom-top;         if(type==2)        colorbits=4;      else   colorbits=8;/*   色深   */      gif.flag=(colorbits-1<<4)|(colorbits-1);      if(type!=1)gif.flag|=0x80;         gif.color=0;gif.ratio=0;      fwrite(&gif,1,13,fp);/*   GIF   文件头:13BYTE   */         if(gif.flag&0x80){          for(i=0;i<768;i++)pal[i]=0;          getcolortable(colortable);          for(i=0;i<16;i++)              {pal[3*i]=colortable[i][0]<<2;                pal[3*i+1]=colortable[i][1]<<2;                pal[3*i+2]=colortable[i][2]<<2;              }          colornum=3*(1<<colorbits);          fwrite(&pal,1,colornum,fp);      }    }       void   localheader(FILE   *   fp)    {struct   localhead{          unsigned   char   label;          unsigned   int   left;          unsigned   int   top;          unsigned   int   width;          unsigned   int   height;          unsigned   char   flag;        }image;      image.label=0x2c;image.left=0;image.top=0;      image.width=right-left;image.height=bottom-top;      image.flag=colorbits-1;      if(mode)image.flag|=0x40;      fwrite(&image,1,10,fp);    }       void   cleartable()    {int   count;      encode=eofcode+1;runbits=initbits+1;maxcodesize=1<<runbits;      for(count=0;count<TABLESIZES;count++)encodetable[count]=0;    }       void   savecode(FILE   *   fp)    {blockbuf[0]=bytecount;bytecount++;      fwrite(blockbuf,1,bytecount,fp);      bytecount=0;    }       void   fillblockbuf(FILE   *   fp,int   code)    {tempcodes|=(unsigned   long)code<<shiftbits;shiftbits+=runbits;      while(shiftbits>=8)      {blockbuf[++bytecount]=tempcodes&0x00ff;        if(bytecount==255)savecode(fp);tempcodes>>=8;shiftbits-=8;      }    }       void   LZWimage(FILE   *   fp)   /*   写入LZW数据   */    {unsigned   char   suffixtable[TABLESIZES],bytebuf[640],now;      int   i,prefixtable[TABLESIZES],prefixcode,suffixcode,index,off;      unsigned   int   width,height,row,dot,online;      width=right-left;height=bottom-top;initbits=colorbits;         if(initbits>8)exit(1);/*   不支持高于256色   */         fputc(initbits,fp);      clearcode=1<<initbits;      eofcode=clearcode+1;      bytecount=shiftbits=0;      tempcodes=0;      cleartable();/*   清空索引表   */         fillblockbuf(fp,clearcode);      for(row=0,online=0,now=0;row<height;row++)/*   处理所有行   */      {        for(i=0;i<width;i++)          bytebuf[i]=getpixel(left+i,top+online);          if(mode){online+=rowcount[now];            if(online>=height){now++;online=linestart[now];}            }          else   online++;          if(row==0){prefixcode=bytebuf[0];dot=1;}          else   dot=0;             while(dot<width)/*   压缩一行信息   */          {suffixcode=bytebuf[dot++];            index=prefixcode^(suffixcode<<4);            if(index==0)off=1;else   off=TABLESIZES-index;            while(1)            {if(encodetable[index]==0)              {fillblockbuf(fp,prefixcode);                if(encode==MAXCODES)                {fillblockbuf(fp,clearcode);cleartable();}                else                {if(encode==maxcodesize)                  {maxcodesize<<=1;runbits++;                }                prefixtable[index]=prefixcode;                suffixtable[index]=suffixcode;encodetable[index]=encode++;              }              prefixcode=suffixcode;break;            }            if(prefixtable[index]==prefixcode&&suffixtable[index]==suffixcode)            {prefixcode=encodetable[index];break;}            else            {index-=off;if(index<0)index+=TABLESIZES;}          }        }      }      fillblockbuf(fp,prefixcode);fillblockbuf(fp,eofcode);      if(shiftbits>0||bytecount>0)      {blockbuf[++bytecount]=tempcodes&0x00ff;savecode(fp);}      fputc(0,fp);    }       void   applyblock(FILE   *   fp)    {      struct      {unsigned   char   extension;        unsigned   char   label;        unsigned   char   size;        char   identifier[8];        char   code[3];        char   data[4];        unsigned   char   teminator;      }application;         application.extension=0x21;      application.label=0xff;application.size=11;      memcpy(application.identifier,"NETSCAPE",8);      memcpy(application.code,"2.0",3);application.data[0]=3;      application.data[1]=1;application.data[2]=0xe8;application.data[3]=3;      application.teminator=0;      fwrite(&application,1,19,fp);    }       void   controlblock(FILE   *   fp,int   delaytime)    {struct{unsigned   char   extension;    unsigned   char   label;    unsigned   char   size;    unsigned   char   field;    unsigned   int   delaytime;    unsigned   char   colorindex;    unsigned   char   terminator;                  }control;      control.extension=0x21;control.label=0xf9;      control.size=4;control.field=0;      control.delaytime=delaytime;control.colorindex=0;      control.terminator=0;fwrite(&control,1,8,fp);    }          void   formgif()/*   使用GIF函数,输出GIF图象   */    {int   i,charsize=4;      char   *   text="GIF   Animator!";      FILE   *   fp;      settextstyle(TRIPLEX_FONT,HORIZ_DIR,charsize);      left=220;top=200;right=420;bottom=280;type=3;mode=0;      if((fp=fopen("animator.gif","wb"))==NULL)            {printf("Can   not   create   Gif   file!\n");exit(1);}         globalheader(fp);   /*   写入全局头   */      applyblock(fp);   /*   写入应用程序声明头   */         for(i=1;i<22;i++)        {setcolor(GREEN);outtextxy(right-20*i,top+20,text);             controlblock(fp,20);   /*   写入控制块   */          localheader(fp);   /*   写入局部头   */          LZWimage(fp);   /*   写入LZW数据   */             delay(200);setcolor(BLACK);outtextxy(right-20*i,top+20,text);        }      fputc(';',fp);fclose(fp);    }    main()    {int   gd,gm,gerror;      detectgraph(&gd,&gm);initgraph(&gd,&gm,"");      cleardevice();formgif();closegraph();    }    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -