#include "../stdafx.h" #include "CLodHandler.h" #include "../SDL_Extensions.h" #include "CDefHandler.h" #include #include #include #include #include "boost/filesystem.hpp" // includes all needed Boost.Filesystem declarations int readNormalNr (int pos, int bytCon, unsigned char * str) { int ret=0; int amp=1; if (str) { for (int i=0; i open(path.c_str(),std::ios::binary); is->seekg(0,std::ios::end); // to the end pcxs = is->tellg(); // read length is->seekg(0,std::ios::beg); // wracamy na poczatek pcx = new unsigned char[pcxs]; // allocate memory is->read((char*)pcx, pcxs); // read map file to buffer is->close(); delete is; } void CPCXConv::saveBMP(std::string path) { std::ofstream os; os.open(path.c_str(), std::ios::binary); os.write((char*)bmp,bmps); os.close(); } void CPCXConv::convert() { BMPHeader bh; BMPPalette pal[256]; Epcxformat format; int fSize,i,y; bool check1, check2; unsigned char add; int it=0; std::stringstream out; 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<0;y--) { it=0xC+(y-1)*bh.x; for (int j=0;j0) { for (int j=0;j0; y--) { it=0xC+(y-1)*bh.x*3; for (int j=0;j0) { for (int j=0;jformat->palette->colors+i) = tp; } for (y=bh.y;y>0;y--) { it=0xC+(y-1)*bh.x; for (int j=0;jpixels + ret->pitch * (y-1) + ret->format->BytesPerPixel * j) = pcx[it+j]; } if (add>0) { for (int j=0;jpixels + ret->pitch * (y-1) + ret->format->BytesPerPixel * (j+bh.x)) = 0; } } } } else { for (y=bh.y; y>0; y--) { it=0xC+(y-1)*bh.x*3; for (int j=0;jpixels + ret->pitch * (y-1) + j) = pcx[it+j]; } if (add>0) { for (int j=0;jpixels + ret->pitch * (y-1) + (j+bh.x*3)) = 0; } } } } return ret; } SDL_Surface * CLodHandler::loadBitmap(std::string fname) { if(!fname.size()) return NULL; unsigned char * pcx; std::transform(fname.begin(),fname.end(),fname.begin(),toupper); fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".PCX"); Entry *e = entries.znajdz(fname); if(!e) { std::cout<<"File "<offset<0) { fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".BMP"); fname = "Data/"+fname; return SDL_LoadBMP(fname.c_str()); } fseek(FLOD, e->offset, 0); if (e->size==0) //file is not compressed { pcx = new unsigned char[e->realSize]; fread((char*)pcx, 1, e->realSize, FLOD); } else { unsigned char * pcd = new unsigned char[e->size]; fread((char*)pcd, 1, e->size, FLOD); int res=infs2(pcd,e->size,e->realSize,pcx); if(res!=0) { std::cout<<"an error "<realSize); return cp.getSurface(); } int CLodHandler::decompress (unsigned char * source, int size, int realSize, std::string & dest) { std::ofstream lb; lb.open("lodbuf\\buf.gz", std::ios::out|std::ios::binary); for(int i=0; ioffset, 0); unsigned char * outp; if (ourEntry->size==0) //file is not compressed { outp = new unsigned char[ourEntry->realSize]; fread((char*)outp, 1, ourEntry->realSize, FLOD); CDefHandler * nh = new CDefHandler(); nh->openFromMemory(outp, ourEntry->realSize, std::string((char*)ourEntry->name)); nh->alphaTransformed = false; ret = nh; } else //we will decompress file { outp = new unsigned char[ourEntry->size]; fread((char*)outp, 1, ourEntry->size, FLOD); fseek(FLOD, 0, 0); unsigned char * decomp = NULL; int decRes = infs2(outp, ourEntry->size, ourEntry->realSize, decomp); CDefHandler * nh = new CDefHandler(); nh->openFromMemory(decomp, ourEntry->realSize, std::string((char*)ourEntry->name)); nh->alphaTransformed = false; ret = nh; delete[] decomp; } delete[] outp; return ret; } CDefEssential * CLodHandler::giveDefEss(std::string defName) { CDefEssential * ret; CDefHandler * temp = giveDef(defName); ret = temp->essentialize(); delete temp; return ret; } std::vector CLodHandler::extractManyFiles(std::vector defNamesIn) { std::vector ret(defNamesIn.size()); for(int hh=0; hhoffset, 0); unsigned char * outp; if (e->size==0) //file is not compressed { outp = new unsigned char[e->realSize]; fread((char*)outp, 1, e->realSize, FLOD); CDefHandler * nh = new CDefHandler; nh->openFromMemory(outp, e->realSize, std::string((char*)e->name)); nh->alphaTransformed = false; ret[hh] = nh; } else //we will decompressing file { outp = new unsigned char[e->size]; fread((char*)outp, 1, e->size, FLOD); fseek(FLOD, 0, 0); unsigned char * decomp = NULL; int decRes = infs2(outp, e->size, e->realSize, decomp); CDefHandler * nh = new CDefHandler; nh->openFromMemory(decomp, e->realSize, std::string((char*)e->name)); nh->alphaTransformed = false; delete [] decomp; ret[hh] = nh; } delete[] outp; } //int i; //std::vector found(defNamesIn.size(), 0); //for (int i=0;iopenFromMemory(outp, entries[i].realSize, std::string((char*)entries[i].name)); // nh->alphaTransformed = false; // ret[curDef] = nh; // } // else //we will decompressing file // { // outp = new unsigned char[entries[i].size]; // fread((char*)outp, 1, entries[i].size, FLOD); // fseek(FLOD, 0, 0); // unsigned char * decomp = NULL; // int decRes = infs2(outp, entries[i].size, entries[i].realSize, decomp); // CDefHandler * nh = new CDefHandler; // nh->openFromMemory(decomp, entries[i].realSize, std::string((char*)entries[i].name)); // nh->alphaTransformed = false; // delete [] decomp; // ret[curDef] = nh; // } // delete[] outp; //} ////std::cout<<'\r'<<"Reading defs: 100% "<=amp/2) { ret = ret-amp; } return ret; } void CLodHandler::init(std::string lodFile) { std::string Ts; FLOD = fopen(lodFile.c_str(), "rb"); fseek(FLOD, 8, 0); unsigned char temp[4]; fread((char*)temp, 1, 4, FLOD); totalFiles = readNormalNr(temp,4); fseek(FLOD, 0x5c, 0); for (int i=0; ioffset<0) { char * outp = new char[e->realSize]; char name[120];for(int i=0;i<120;i++) name[i]='\0'; strcat(name,"Data/"); strcat(name,(char*)e->name); FILE * f = fopen(name,"rb"); int result = fread(outp,1,e->realSize,f); if(result<0) {std::cout<<"Error in file reading: "<realSize;itr++) ret0+= *(outp+itr); delete[] outp; return ret0; } fseek(FLOD, e->offset, 0); unsigned char * outp; if (e->size==0) //file is not compressed { outp = new unsigned char[e->realSize]; fread((char*)outp, 1, e->realSize, FLOD); std::string ret = std::string((char*)outp); delete[] outp; return ret; } else //we will decompressing file { outp = new unsigned char[e->size]; fread((char*)outp, 1, e->size, FLOD); fseek(FLOD, 0, 0); unsigned char * decomp = NULL; int decRes = infs2(outp, e->size, e->realSize, decomp); std::string ret; for (int itr=0;itrrealSize;itr++) ret+= *((char*)decomp+itr); delete[] outp; delete[] decomp; return ret; } return ret0; }