谁能写一个调用 Gif98a 类,实现播放 GIF 文件的的例子??附上 Gif89a 的类的源码!!
ddsft 2003-10-19 10:58:52 gif89a.cpp
------------------
#include <fstream.h>
#include <string.h>
#include "gif89a.h"
CGif89a::CGif89a()
{ opened = FALSE;
error = FALSE;
}
CGif89a::CGif89a(LPCSTR fileName,BOOL inMem)
{ opened = FALSE;
if(open(fileName,inMem))
{ opened = TRUE;
error = FALSE;
}
else
{ opened = FALSE;
error = TRUE;
}
}
CGif89a::~CGif89a()
{ close();
}
BOOL CGif89a::operator!()
{ return error;
}
BOOL CGif89a::open(LPCSTR fileName,BOOL b)
{ char cc[4];
BYTE be;
BOOL fileEnd = FALSE;
inMem = b;
allFrames = NULL;
curIndex = 0;
curFrame.pColorTable = NULL;
curFrame.dataBuf = NULL;
ctrlExt.active = FALSE;
if(opened)
return FALSE;
ifs.open(fileName,ios::binary|ios::nocreate);
if(!ifs)
{ return FALSE;
}
ifs.read(cc,3);
if(strncmp(cc,"GIF",3) != 0)
goto error;
ifs.read(version,3);
version[3] = 0;
if(strncmp(version,"89a",3) > 0)
goto error;
ifs.read((char*)&gInfo.scrWidth,2);
ifs.read((char*)&gInfo.scrHeight,2);
if(!ifs.good())
goto error;
ifs.read((char*)&be,1);
if((be&0x80) != 0)
gInfo.gFlag = TRUE;
else
gInfo.gFlag = FALSE;
gInfo.colorRes = ((be&0x70)>>4)+1;
if(gInfo.gFlag)
{ if((be&0x08) != 0)
gInfo.gSort = TRUE;
else
gInfo.gSort = FALSE;
gInfo.gSize = 1;
gInfo.gSize <<= ((be&0x07)+1);
}
ifs.read((char*)&be,1);
gInfo.BKColorIdx = be;
ifs.read((char*)&be,1);
gInfo.pixelAspectRatio = be;
if(!ifs.good())
goto error;
if(gInfo.gFlag)
{ ifs.read((char*)gColorTable,gInfo.gSize*3);
gInfo.gColorTable = gColorTable;
}
else
gInfo.gColorTable = NULL;
dataStart = ifs.tellg();
if((gInfo.frames = checkFrames(ifs)) == 0)
goto error;
if(inMem)
{ if((allFrames = new FRAME[gInfo.frames]) == NULL)
goto error;
ZeroMemory(allFrames,sizeof(FRAME)*gInfo.frames);
if(!getAllFrames(ifs))
{ delete[] allFrames;
allFrames = NULL;
goto error;
}
ifs.close();
}
opened = TRUE;
return TRUE;
error:
ifs.close();
return FALSE;
}
UINT CGif89a::checkFrames(ifstream& ifs)
{ BYTE be;
BOOL fileEnd = FALSE;
UINT frames=0;
streampos pos = ifs.tellg();
while(!ifs.eof() && !fileEnd)
{ ifs.read((char*)&be,1);
switch(be)
{ case 0x21:
ifs.read((char*)&be,1);
switch(be)
{ case 0xf9:
case 0xfe:
case 0x01:
case 0xff:
while(!ifs.eof())
{ ifs.read((char*)&be,1);
if(be == 0)
break;
ifs.seekg(be,ios::cur);
}
break;
default:
return 0;
}
break;
case 0x2c:
{ BYTE bp;
BOOL lFlag=FALSE;
UINT lSize=1;
frames++;
ifs.seekg(8,ios::cur);
ifs.read((char*)&bp,1);
if((bp&0x80) != 0)
lFlag = TRUE;
lSize <<= ((bp&0x07)+1);
if(lFlag)
ifs.seekg(lSize*3,ios::cur);
if(!ifs.good())
return 0;
ifs.read((char*)&be,1);
while(!ifs.eof())
{ ifs.read((char*)&be,1);
if(be == 0)
break;
ifs.seekg(be,ios::cur);
}
break;
}
case 0x3b:
fileEnd = TRUE;
break;
case 0x00:
break;
default:
return 0;
}
}
ifs.seekg(pos);
return frames;
}