bmp2gif的代码,高手帮着优化一下~~感谢

geagle 2001-08-17 06:23:45
void BmpToGif(CString filein, CString fileout) //filein只可以是16色或者256的位图文件名,fileout是输出文件名
{
BITMAPFILEHEADER bm1;
BITMAPINFOHEADER bm2;

const char gifhead[]="GIF89a";
CFile fp,wp;
CString tt;
BYTE c,color[4],*data,*tdata,kk[256];
WORD w,h,d,*bb[4099],bbl[4099],cur[4099],out[200000];
int i,j,k,bblen,curl=0,outl,colorcount;
DWORD wt;

fp.Open(filein,CFile::modeRead);
wp.Open(fileout,CFile::modeReadWrite¦CFile::modeCreate);
fp.Read(&bm1,sizeof(bm1));
fp.Read(&bm2,sizeof(bm2));

if(bm2.biBitCount==8 ¦¦ bm2.biBitCount==4)
{
if(bm2.biBitCount==8) { colorcount=0x100; c=0xf7; }
if(bm2.biBitCount==4) { colorcount=16; c=0xb3; }
wp.Write(gifhead,6);
w=(WORD)bm2.biWidth;
wp.Write(&w,2);
h=(WORD)bm2.biHeight;
wp.Write(&h,2);

wp.Write(&c,1);
d=0;
wp.Write(&d,2);

for(i=0;i<colorcount;i++)
{
fp.Read(color,4);
wp.Write(&color[2],1);
wp.Write(&color[1],1);
wp.Write(&color[0],1);
}
data=new BYTE[w*h];
switch(colorcount)
{
case 256:
d=4-w%4; if(d==4) d=0;
tdata=0;
break;
case 16:
d=w/2; while(d%4>0) d++;
tdata=new BYTE[d];
break;
}
for(i=0;i<h;i++)
{
switch(colorcount)
{
case 256:
fp.Read(&data[w*(h-i-1)],w);
fp.Seek(d,CFile::current);
break;
case 16:
fp.Read(tdata,d);
for(j=0;j<w;j++)
{
if(j%2) data[w*(h-i-1)+j]=tdata[j/2]&0xf;
else data[w*(h-i-1)+j]=(tdata[j/2]>>4)&0xf;
}
break;
}
}
if(tdata) delete[] tdata;
c=0x2c;
wp.Write(&c,1);
d=0;
wp.Write(&d,2);
wp.Write(&d,2);
wp.Write(&w,2);
wp.Write(&h,2);
c=0;
wp.Write(&c,1);
c=(BYTE)bm2.biBitCount;
wp.Write(&c,1);

for(j=0;j<colorcount;j++)//初始化编译表
{
bb[j]=new WORD[1];
*bb[j]=j;
bbl[j]=1;
}
bbl[colorcount]=bbl[colorcount+1]=0;

bblen=colorcount+2;
out[0]=colorcount;
outl=1; curl=0; int e=bm2.biBitCount+1;

for(i=0;i<w*h;i++)
{
cur[curl]=(WORD)data[i];
curl++;
if(found(cur,bb,bbl,bblen,curl)<0)
{
if((bblen-1)&((WORD)0xffff<<e)) { e++; out[outl]=0xffff; outl++; }
out[outl]=found(cur,bb,bbl,bblen,curl-1);
outl++;
bb[bblen]=new WORD[curl];
bbl[bblen]=curl;
for(j=0;j<curl;j++)
{
*(bb[bblen]+j)=cur[j];
}
bblen++;
if(bblen>=4096)
{
for(j=colorcount+2;j<bblen;j++) delete[] bb[j];
bblen=colorcount+2;
out[outl]=colorcount;
outl++;
out[outl]=0xfffe;
outl++;
e=bm2.biBitCount+1;
}
cur[0]=cur[curl-1];
curl=1;
}
if(i==w*h-1)
{
out[outl]=found(cur,bb,bbl,bblen,curl);
outl++;
}
}

out[outl]=colorcount+1;
outl++;

j=0;
k=1;
wt=0;
e=bm2.biBitCount+1;
for(i=0;i<outl;i++)
{
if(out[i]==0xffff)
{
e++;
continue;
}
if(out[i]==0xfffe)
{
e=bm2.biBitCount+1;
continue;
}
wt¦=((DWORD)out[i]<<j);
j+=e;
while(j>8)
{
c=(BYTE)(wt&0x000000ff);
kk[k]=c;
k++;
if(k==256)
{
kk[0]=0xff;
k=1;
wp.Write(kk,256);
}
wt>>=8;
j-=8;
}
}
if(j!=0)
{
c=(BYTE)(wt&0x000000ff);
kk[k]=c;
k++;
}
if(k!=1)
{
kk[0]=k-1;
wp.Write(kk,k);
}
c=0;
wp.Write(&c,1);
c=0x3b;
wp.Write(&c,1);
delete data;
for(j=0;j<bblen;j++) if(j!=colorcount&&j!=colorcount+1) delete[] bb[j];
}
wp.Close();
fp.Close();
}

int found(WORD cur[], WORD *bb[], WORD bbl[], int bblen, int curl)
{
int i,j,k,r=-1;
for(i=0;i<bblen;i++)
{
if(curl==bbl[i])
{
k=0;
for(j=0;j<curl;j++)
{
if(cur[j]!=*(bb[i]+j)) k=1;
}
if(k==0) { r=i; break; }
}
}
return r;
}
...全文
92 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
geagle 2001-08-22
  • 打赏
  • 举报
回复
有人吗?(东东东...)
geagle 2001-08-21
  • 打赏
  • 举报
回复
哪位大哥拉小弟一把。。。感谢~~~
geagle 2001-08-17
  • 打赏
  • 举报
回复
附:

读取大的BMP文件会造成内存错误,

读个小的也特慢,

小弟刚涉及VC编程,

各位仁兄帮帮小弟~~

1,451

社区成员

发帖
与我相关
我的任务
社区描述
多媒体/设计/Flash/Silverlight 开发 图象工具使用
社区管理员
  • 图象工具使用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧