1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-26 22:57:00 +02:00

* faster decompression with infs2

* minor changes (unused code removed, a few additional consts)
This commit is contained in:
mateuszb 2009-09-27 11:37:15 +00:00
parent 974acb3907
commit 07a769601e
6 changed files with 50 additions and 198 deletions

View File

@ -18,7 +18,7 @@
*/ */
boost::mutex bitmap_handler_mx; boost::mutex bitmap_handler_mx;
int readNormalNr (int pos, int bytCon, unsigned char * str); int readNormalNr (int pos, int bytCon, const unsigned char * str);
extern DLL_EXPORT CLodHandler *bitmaph; extern DLL_EXPORT CLodHandler *bitmaph;
@ -56,6 +56,7 @@ void CPCXConv::fromFile(std::string path)
is.read((char*)pcx, pcxs); // read map file to buffer is.read((char*)pcx, pcxs); // read map file to buffer
is.close(); is.close();
} }
void CPCXConv::saveBMP(std::string path) void CPCXConv::saveBMP(std::string path)
{ {
std::ofstream os; std::ofstream os;
@ -63,120 +64,8 @@ void CPCXConv::saveBMP(std::string path)
os.write((char*)bmp,bmps); os.write((char*)bmp,bmps);
os.close(); os.close();
} }
void CPCXConv::convert()
{
BMPHeader bh;
BMPPalette pal[256];
Epcxformat format;
int fSize,y;//,i; //TODO use me 'i'
bool check1, check2;
unsigned char add;
int it=0;
std::ostringstream out; SDL_Surface * CPCXConv::getSurface() const
fSize = readNormalNr(it,4,pcx);it+=4;
bh.x = readNormalNr(it,4,pcx);it+=4;
bh.y = readNormalNr(it,4,pcx);it+=4;
if (fSize==bh.x*bh.y*3)
check1=true;
else
check1=false;
if (fSize==bh.x*bh.y)
check2=true;
else
check2=false;
if (check1)
format=PCX24B;
else if (check2)
format=PCX8B;
else
return;
add = 4 - bh.x%4;
if (add==4)
add=0;
bh._h3=bh.x*bh.y;
if (format==PCX8B)
{
bh._c1=0x436;
bh._c2=0x28;
bh._c3=1;
bh._c4=8;
//bh.dataSize2=bh.dataSize1=maxx*maxy;
bh.dataSize1=bh.x;
bh.dataSize2=bh.y;
bh.fullSize = bh.dataSize1+436;
}
else
{
bh._c1=0x36;
bh._c2=0x28;
bh._c3=1;
bh._c4=0x18;
//bh.dataSize2=bh.dataSize1=0xB12;
bh.dataSize1=bh.x;
bh.dataSize2=bh.y;
bh.fullSize=(bh.x+add)*bh.y*3+36+18;
bh._h3*=3;
}
if (format==PCX8B)
{
it = pcxs-256*3;
for (int i=0;i<256;i++)
{
pal[i].R=pcx[it++];
pal[i].G=pcx[it++];
pal[i].B=pcx[it++];
pal[i].F='\0';
}
}
out<<"BM";
bh.print(out);
if (format==PCX8B)
{
for (int i=0;i<256;i++)
{
out<<pal[i].B;
out<<pal[i].G;
out<<pal[i].R;
out<<pal[i].F;
}
for (y=bh.y;y>0;y--)
{
it=0xC+(y-1)*bh.x;
for (int j=0;j<bh.x;j++)
out<<pcx[it+j];
if (add>0)
{
for (int j=0;j<add;j++)
out<<'\0'; //bylo z buforu, ale onnie byl incjalizowany (?!)
}
}
}
else
{
for (y=bh.y; y>0; y--)
{
it=0xC+(y-1)*bh.x*3;
for (int j=0;j<bh.x*3;j++)
out<<pcx[it+j];
if (add>0)
{
for (int j=0;j<add*3;j++)
out<<'\0'; //bylo z buforu, ale onnie byl incjalizowany (?!)
}
}
}
std::string temp = out.str();
bmp = new unsigned char[temp.length()];
bmps=temp.length();
for (size_t a=0;a<temp.length();++a)
{
bmp[a]=temp[a];
}
}
SDL_Surface * CPCXConv::getSurface()
{ {
SDL_Surface * ret; SDL_Surface * ret;
@ -362,7 +251,7 @@ SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
delete [] pcd; delete [] pcd;
} }
CPCXConv cp; CPCXConv cp;
cp.openPCX((char*)pcx,e->realSize); cp.openPCX((char*)pcx, e->realSize);
SDL_Surface * ret = cp.getSurface(); SDL_Surface * ret = cp.getSurface();
if(ret->format->BytesPerPixel == 1 && setKey) if(ret->format->BytesPerPixel == 1 && setKey)
{ {

View File

@ -23,14 +23,21 @@ struct BMPPalette
{ {
unsigned char R,G,B,F; unsigned char R,G,B,F;
}; };
struct BMPHeader struct BMPHeader
{ {
int fullSize, _h1, _h2, _h3, _c1, _c2, _c3, _c4, x, y, int fullSize, _h1, _h2, _h3, _c1, _c2, _c3, _c4, x, y,
dataSize1, dataSize2; //DataSize=X*Y+2*Y dataSize1, dataSize2; //DataSize=X*Y+2*Y
unsigned char _c5[8]; unsigned char _c5[8];
void print(std::ostream & out); void print(std::ostream & out);
BMPHeader(){_h1=_h2=0;for(int i=0;i<8;i++)_c5[i]=0;}; BMPHeader()
{
_h1=_h2=0;
for(int i=0;i<8;i++)
_c5[i]=0;
}
}; };
class CPCXConv class CPCXConv
{ {
public: public:
@ -39,12 +46,18 @@ public:
void fromFile(std::string path); void fromFile(std::string path);
void saveBMP(std::string path); void saveBMP(std::string path);
void openPCX(char * PCX, int len); void openPCX(char * PCX, int len);
void convert(); SDL_Surface * getSurface() const; //for standard H3 PCX
SDL_Surface * getSurface(); //for standard H3 PCX
//SDL_Surface * getSurfaceZ(); //for ZSoft PCX //SDL_Surface * getSurfaceZ(); //for ZSoft PCX
CPCXConv(){pcx=bmp=NULL;pcxs=bmps=0;}; //c-tor CPCXConv() //c-tor
~CPCXConv(){if (pcxs) delete[] pcx; if(bmps) delete[] bmp;} //d-tor : pcx(NULL), bmp(NULL), pcxs(0), bmps(0)
{}
~CPCXConv() //d-tor
{
if (pcxs) delete[] pcx;
if (bmps) delete[] bmp;
}
}; };
namespace BitmapHandler namespace BitmapHandler
{ {
SDL_Surface * loadBitmap(std::string fname, bool setKey=true); SDL_Surface * loadBitmap(std::string fname, bool setKey=true);

View File

@ -152,7 +152,7 @@ CBuildingHandler::~CBuildingHandler()
static std::string emptyStr = ""; static std::string emptyStr = "";
const std::string & CBuilding::Name() const std::string & CBuilding::Name() const
{ {
if(name.length()) if(name.length())
return name; return name;
@ -162,7 +162,7 @@ const std::string & CBuilding::Name()
return emptyStr; return emptyStr;
} }
const std::string & CBuilding::Description() const std::string & CBuilding::Description() const
{ {
if(description.length()) if(description.length())
return description; return description;

View File

@ -23,8 +23,8 @@ public:
std::string name; std::string name;
std::string description; std::string description;
const std::string &Name(); const std::string &Name() const;
const std::string &Description(); const std::string &Description() const;
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
{ {

View File

@ -24,7 +24,7 @@
* *
*/ */
DLL_EXPORT int readNormalNr (int pos, int bytCon, unsigned char * str) DLL_EXPORT int readNormalNr (int pos, int bytCon, const unsigned char * str)
{ {
int ret=0; int ret=0;
int amp=1; int amp=1;
@ -166,8 +166,6 @@ DLL_EXPORT int CLodHandler::infs2(unsigned char * in, int size, int realSize, un
int ret; int ret;
unsigned have; unsigned have;
z_stream strm; z_stream strm;
unsigned char inx[NLoadHandlerHelp::fCHUNK];
unsigned char outx[NLoadHandlerHelp::fCHUNK];
out = new unsigned char [realSize]; out = new unsigned char [realSize];
int latPosOut = 0; int latPosOut = 0;
@ -183,27 +181,26 @@ DLL_EXPORT int CLodHandler::infs2(unsigned char * in, int size, int realSize, un
int chunkNumber = 0; int chunkNumber = 0;
do do
{ {
int readBytes = 0; if(size < chunkNumber * NLoadHandlerHelp::fCHUNK)
for(int i=0; i<NLoadHandlerHelp::fCHUNK && (chunkNumber * NLoadHandlerHelp::fCHUNK + i)<size; ++i) break;
{ strm.avail_in = std::min(NLoadHandlerHelp::fCHUNK, size - chunkNumber * NLoadHandlerHelp::fCHUNK);
inx[i] = in[chunkNumber * NLoadHandlerHelp::fCHUNK + i];
++readBytes;
}
++chunkNumber;
strm.avail_in = readBytes;
if (strm.avail_in == 0) if (strm.avail_in == 0)
break; break;
strm.next_in = inx; strm.next_in = in + chunkNumber * NLoadHandlerHelp::fCHUNK;
/* run inflate() on input until output buffer not full */ /* run inflate() on input until output buffer not full */
do do
{ {
strm.avail_out = NLoadHandlerHelp::fCHUNK; strm.avail_out = realSize - latPosOut;
strm.next_out = outx; strm.next_out = out + latPosOut;
ret = inflate(&strm, Z_NO_FLUSH); ret = inflate(&strm, Z_NO_FLUSH);
//assert(ret != Z_STREAM_ERROR); /* state not clobbered */ //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
bool breakLoop = false;
switch (ret) switch (ret)
{ {
case Z_STREAM_END:
breakLoop = true;
break;
case Z_NEED_DICT: case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */ ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR: case Z_DATA_ERROR:
@ -211,14 +208,15 @@ DLL_EXPORT int CLodHandler::infs2(unsigned char * in, int size, int realSize, un
(void)inflateEnd(&strm); (void)inflateEnd(&strm);
return ret; return ret;
} }
have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
for(int oo=0; oo<have; ++oo) if(breakLoop)
{ break;
out[latPosOut] = outx[oo];
++latPosOut; have = realSize - latPosOut - strm.avail_out;
} latPosOut += have;
} while (strm.avail_out == 0); } while (strm.avail_out == 0);
++chunkNumber;
/* done when inflate() says it's done */ /* done when inflate() says it's done */
} while (ret != Z_STREAM_END); } while (ret != Z_STREAM_END);
@ -227,53 +225,6 @@ DLL_EXPORT int CLodHandler::infs2(unsigned char * in, int size, int realSize, un
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
} }
void CLodHandler::extract(std::string FName)
{
std::ofstream FOut;
for (int i=0;i<totalFiles;i++)
{
fseek(FLOD, entries[i].offset, 0);
std::string bufff = (DATA_DIR + FName.substr(0, FName.size()-4) + PATHSEPARATOR + entries[i].nameStr);
unsigned char * outp;
if (entries[i].size==0) //file is not compressed
{
outp = new unsigned char[entries[i].realSize];
fread((char*)outp, 1, entries[i].realSize, FLOD);
std::ofstream out;
out.open(bufff.c_str(), std::ios::binary);
if(!out.is_open())
{
tlog1<<"Unable to create "<<bufff;
}
else
{
for(int hh=0; hh<entries[i].realSize; ++hh)
{
out<<*(outp+hh);
}
out.close();
}
}
else
{
outp = new unsigned char[entries[i].size];
fread((char*)outp, 1, entries[i].size, FLOD);
fseek(FLOD, 0, 0);
std::ofstream destin;
destin.open(bufff.c_str(), std::ios::binary);
//int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
int decRes = infs(outp, entries[i].size, entries[i].realSize, destin);
destin.close();
if(decRes!=0)
{
tlog1<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
}
}
delete[] outp;
}
fclose(FLOD);
}
void CLodHandler::extractFile(std::string FName, std::string name) void CLodHandler::extractFile(std::string FName, std::string name)
{ {
std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper); std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper);

View File

@ -65,7 +65,6 @@ public:
int infs2(unsigned char * in, int size, int realSize, unsigned char*& out, int wBits=15); //zlib fast handler int infs2(unsigned char * in, int size, int realSize, unsigned char*& out, int wBits=15); //zlib fast handler
unsigned char * giveFile(std::string defName, int * length=NULL); //returns pointer to the decompressed data - it must be deleted when no longer needed! unsigned char * giveFile(std::string defName, int * length=NULL); //returns pointer to the decompressed data - it must be deleted when no longer needed!
std::string getTextFile(std::string name); //extracts one file std::string getTextFile(std::string name); //extracts one file
void extract(std::string FName);
void extractFile(std::string FName, std::string name); //extracts a specific file void extractFile(std::string FName, std::string name); //extracts a specific file
void init(std::string lodFile, std::string dirName); void init(std::string lodFile, std::string dirName);
}; };