CBitmapHandler.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include "../stdafx.h"
  2. #include "SDL.h"
  3. #include "SDL_image.h"
  4. #include "CBitmapHandler.h"
  5. #include "../hch/CDefHandler.h"
  6. #include "../hch/CLodHandler.h"
  7. #include <sstream>
  8. #include <boost/thread.hpp>
  9. boost::mutex bitmap_handler_mx;
  10. int readNormalNr (int pos, int bytCon, unsigned char * str);
  11. CLodHandler * BitmapHandler::bitmaph = NULL;
  12. void BMPHeader::print(std::ostream & out)
  13. {
  14. CDefHandler::print(out,fullSize,4);
  15. CDefHandler::print(out,_h1,4);
  16. CDefHandler::print(out,_c1,4);
  17. CDefHandler::print(out,_c2,4);
  18. CDefHandler::print(out,x,4);
  19. CDefHandler::print(out,y,4);
  20. CDefHandler::print(out,_c3,2);
  21. CDefHandler::print(out,_c4,2);
  22. CDefHandler::print(out,_h2,4);
  23. CDefHandler::print(out,_h3,4);
  24. CDefHandler::print(out,dataSize1,4);
  25. CDefHandler::print(out,dataSize2,4);
  26. for (int i=0;i<8;i++)
  27. out << _c5[i];
  28. out.flush();
  29. }
  30. void CPCXConv::openPCX(char * PCX, int len)
  31. {
  32. pcxs=len;
  33. pcx=(unsigned char*)PCX;
  34. }
  35. void CPCXConv::fromFile(std::string path)
  36. {
  37. std::ifstream * is = new std::ifstream();
  38. is -> open(path.c_str(),std::ios::binary);
  39. is->seekg(0,std::ios::end); // to the end
  40. pcxs = is->tellg(); // read length
  41. is->seekg(0,std::ios::beg); // wracamy na poczatek
  42. pcx = new unsigned char[pcxs]; // allocate memory
  43. is->read((char*)pcx, pcxs); // read map file to buffer
  44. is->close();
  45. delete is;
  46. }
  47. void CPCXConv::saveBMP(std::string path)
  48. {
  49. std::ofstream os;
  50. os.open(path.c_str(), std::ios::binary);
  51. os.write((char*)bmp,bmps);
  52. os.close();
  53. }
  54. void CPCXConv::convert()
  55. {
  56. BMPHeader bh;
  57. BMPPalette pal[256];
  58. Epcxformat format;
  59. int fSize,i,y;
  60. bool check1, check2;
  61. unsigned char add;
  62. int it=0;
  63. std::stringstream out;
  64. fSize = readNormalNr(it,4,pcx);it+=4;
  65. bh.x = readNormalNr(it,4,pcx);it+=4;
  66. bh.y = readNormalNr(it,4,pcx);it+=4;
  67. if (fSize==bh.x*bh.y*3)
  68. check1=true;
  69. else
  70. check1=false;
  71. if (fSize==bh.x*bh.y)
  72. check2=true;
  73. else
  74. check2=false;
  75. if (check1)
  76. format=PCX24B;
  77. else if (check2)
  78. format=PCX8B;
  79. else
  80. return;
  81. add = 4 - bh.x%4;
  82. if (add==4)
  83. add=0;
  84. bh._h3=bh.x*bh.y;
  85. if (format==PCX8B)
  86. {
  87. bh._c1=0x436;
  88. bh._c2=0x28;
  89. bh._c3=1;
  90. bh._c4=8;
  91. //bh.dataSize2=bh.dataSize1=maxx*maxy;
  92. bh.dataSize1=bh.x;
  93. bh.dataSize2=bh.y;
  94. bh.fullSize = bh.dataSize1+436;
  95. }
  96. else
  97. {
  98. bh._c1=0x36;
  99. bh._c2=0x28;
  100. bh._c3=1;
  101. bh._c4=0x18;
  102. //bh.dataSize2=bh.dataSize1=0xB12;
  103. bh.dataSize1=bh.x;
  104. bh.dataSize2=bh.y;
  105. bh.fullSize=(bh.x+add)*bh.y*3+36+18;
  106. bh._h3*=3;
  107. }
  108. if (format==PCX8B)
  109. {
  110. it = pcxs-256*3;
  111. for (int i=0;i<256;i++)
  112. {
  113. pal[i].R=pcx[it++];
  114. pal[i].G=pcx[it++];
  115. pal[i].B=pcx[it++];
  116. pal[i].F='\0';
  117. }
  118. }
  119. out<<"BM";
  120. bh.print(out);
  121. if (format==PCX8B)
  122. {
  123. for (int i=0;i<256;i++)
  124. {
  125. out<<pal[i].B;
  126. out<<pal[i].G;
  127. out<<pal[i].R;
  128. out<<pal[i].F;
  129. }
  130. for (y=bh.y;y>0;y--)
  131. {
  132. it=0xC+(y-1)*bh.x;
  133. for (int j=0;j<bh.x;j++)
  134. out<<pcx[it+j];
  135. if (add>0)
  136. {
  137. for (int j=0;j<add;j++)
  138. out<<'\0'; //bylo z buforu, ale onnie byl incjalizowany (?!)
  139. }
  140. }
  141. }
  142. else
  143. {
  144. for (y=bh.y; y>0; y--)
  145. {
  146. it=0xC+(y-1)*bh.x*3;
  147. for (int j=0;j<bh.x*3;j++)
  148. out<<pcx[it+j];
  149. if (add>0)
  150. {
  151. for (int j=0;j<add*3;j++)
  152. out<<'\0'; //bylo z buforu, ale onnie byl incjalizowany (?!)
  153. }
  154. }
  155. }
  156. std::string temp = out.str();
  157. bmp = new unsigned char[temp.length()];
  158. bmps=temp.length();
  159. for (int a=0;a<temp.length();a++)
  160. {
  161. bmp[a]=temp[a];
  162. }
  163. }
  164. SDL_Surface * CPCXConv::getSurface()
  165. {
  166. SDL_Surface * ret;
  167. BMPHeader bh;
  168. Epcxformat format;
  169. int fSize,i,y;
  170. bool check1, check2;
  171. unsigned char add;
  172. int it=0;
  173. fSize = readNormalNr(it,4,pcx);it+=4;
  174. bh.x = readNormalNr(it,4,pcx);it+=4;
  175. bh.y = readNormalNr(it,4,pcx);it+=4;
  176. if (fSize==bh.x*bh.y*3)
  177. check1=true;
  178. else
  179. check1=false;
  180. if (fSize==bh.x*bh.y)
  181. check2=true;
  182. else
  183. check2=false;
  184. if (check1)
  185. format=PCX24B;
  186. else if (check2)
  187. format=PCX8B;
  188. else
  189. return NULL;
  190. add = 4 - bh.x%4;
  191. if (add==4)
  192. add=0;
  193. if (format==PCX8B)
  194. {
  195. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, bh.x+add, bh.y, 8, 0, 0, 0, 0);
  196. }
  197. else
  198. {
  199. int bmask = 0x0000ff;
  200. int gmask = 0x00ff00;
  201. int rmask = 0xff0000;
  202. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, bh.x+add, bh.y, 24, rmask, gmask, bmask, 0);
  203. }
  204. if (format==PCX8B)
  205. {
  206. it = pcxs-256*3;
  207. for (int i=0;i<256;i++)
  208. {
  209. SDL_Color tp;
  210. tp.r = pcx[it++];
  211. tp.g = pcx[it++];
  212. tp.b = pcx[it++];
  213. tp.unused = 0;
  214. *(ret->format->palette->colors+i) = tp;
  215. }
  216. for (y=bh.y;y>0;y--)
  217. {
  218. it=0xC+(y-1)*bh.x;
  219. for (int j=0;j<bh.x;j++)
  220. {
  221. *((char*)ret->pixels + ret->pitch * (y-1) + ret->format->BytesPerPixel * j) = pcx[it+j];
  222. }
  223. if (add>0)
  224. {
  225. for (int j=0;j<add;j++)
  226. {
  227. *((char*)ret->pixels + ret->pitch * (y-1) + ret->format->BytesPerPixel * (j+bh.x)) = 0;
  228. }
  229. }
  230. }
  231. }
  232. else
  233. {
  234. for (y=bh.y; y>0; y--)
  235. {
  236. it=0xC+(y-1)*bh.x*3;
  237. for (int j=0;j<bh.x*3;j++)
  238. {
  239. *((char*)ret->pixels + ret->pitch * (y-1) + j) = pcx[it+j];
  240. }
  241. if (add>0)
  242. {
  243. for (int j=0;j<add*3;j++)
  244. {
  245. *((char*)ret->pixels + ret->pitch * (y-1) + (j+bh.x*3)) = 0;
  246. }
  247. }
  248. }
  249. }
  250. return ret;
  251. }
  252. SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
  253. {
  254. if(!fname.size())
  255. return NULL;
  256. unsigned char * pcx;
  257. std::transform(fname.begin(),fname.end(),fname.begin(),toupper);
  258. fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".PCX");
  259. Entry *e = bitmaph->entries.znajdz(fname);
  260. if(!e)
  261. {
  262. std::cout<<"File "<<fname<<" not found"<<std::endl;
  263. return NULL;
  264. }
  265. if(e->offset<0)
  266. {
  267. fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".BMP");
  268. fname = "Data/"+fname;
  269. FILE * f = fopen(fname.c_str(),"r");
  270. if(f)
  271. {
  272. fclose(f);
  273. return SDL_LoadBMP(fname.c_str());
  274. }
  275. else //file .bmp not present, check .pcx
  276. {
  277. char sign[3];
  278. fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".PCX");
  279. f = fopen(fname.c_str(),"r");
  280. if(!f)
  281. return NULL;
  282. fread(sign,1,3,f);
  283. if(sign[0]=='B' && sign[1]=='M') //BMP named as PCX - people (eg. Kulex) sometimes use such files
  284. {
  285. fclose(f);
  286. return SDL_LoadBMP(fname.c_str());
  287. }
  288. else //PCX - but we don't know which
  289. {
  290. if((sign[0]==10) && (sign[1]<6) && (sign[2]==1)) //ZSoft PCX
  291. {
  292. fclose(f);
  293. return IMG_Load(fname.c_str());
  294. }
  295. else //H3-style PCX
  296. {
  297. CPCXConv cp;
  298. pcx = new unsigned char[e->realSize];
  299. memcpy(pcx,sign,3);
  300. int res = fread((char*)pcx+3, 1, e->realSize-3, f);
  301. fclose(f);
  302. cp.openPCX((char*)pcx,e->realSize);
  303. return cp.getSurface();
  304. }
  305. }
  306. }
  307. }
  308. bitmap_handler_mx.lock();
  309. fseek(bitmaph->FLOD, e->offset, 0);
  310. if (e->size==0) //file is not compressed
  311. {
  312. pcx = new unsigned char[e->realSize];
  313. fread((char*)pcx, 1, e->realSize, bitmaph->FLOD);
  314. bitmap_handler_mx.unlock();
  315. }
  316. else
  317. {
  318. unsigned char * pcd = new unsigned char[e->size];
  319. fread((char*)pcd, 1, e->size, bitmaph->FLOD);
  320. bitmap_handler_mx.unlock();
  321. int res=bitmaph->infs2(pcd,e->size,e->realSize,pcx);
  322. if(res!=0)
  323. {
  324. std::cout<<"an error "<<res<<" occured while extracting file "<<fname<<std::endl;
  325. }
  326. delete [] pcd;
  327. }
  328. CPCXConv cp;
  329. cp.openPCX((char*)pcx,e->realSize);
  330. SDL_Surface * ret = cp.getSurface();
  331. if(setKey)
  332. SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format,0,255,255));
  333. return ret;
  334. }