CBitmapHandler.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  200. int bmask = 0xff0000;
  201. int gmask = 0x00ff00;
  202. int rmask = 0x0000ff;
  203. #else
  204. int bmask = 0x0000ff;
  205. int gmask = 0x00ff00;
  206. int rmask = 0xff0000;
  207. #endif
  208. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, bh.x+add, bh.y, 24, rmask, gmask, bmask, 0);
  209. }
  210. if (format==PCX8B)
  211. {
  212. it = pcxs-256*3;
  213. for (int i=0;i<256;i++)
  214. {
  215. SDL_Color tp;
  216. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  217. tp.b = pcx[it++];
  218. tp.g = pcx[it++];
  219. tp.r = pcx[it++];
  220. #else
  221. tp.r = pcx[it++];
  222. tp.g = pcx[it++];
  223. tp.b = pcx[it++];
  224. #endif
  225. tp.unused = 0;
  226. *(ret->format->palette->colors+i) = tp;
  227. }
  228. for (y=bh.y;y>0;y--)
  229. {
  230. it=0xC+(y-1)*bh.x;
  231. for (int j=0;j<bh.x;j++)
  232. {
  233. *((char*)ret->pixels + ret->pitch * (y-1) + ret->format->BytesPerPixel * j) = pcx[it+j];
  234. }
  235. if (add>0)
  236. {
  237. for (int j=0;j<add;j++)
  238. {
  239. *((char*)ret->pixels + ret->pitch * (y-1) + ret->format->BytesPerPixel * (j+bh.x)) = 0;
  240. }
  241. }
  242. }
  243. }
  244. else
  245. {
  246. for (y=bh.y; y>0; y--)
  247. {
  248. it=0xC+(y-1)*bh.x*3;
  249. for (int j=0;j<bh.x*3;j++)
  250. {
  251. *((char*)ret->pixels + ret->pitch * (y-1) + j) = pcx[it+j];
  252. }
  253. if (add>0)
  254. {
  255. for (int j=0;j<add*3;j++)
  256. {
  257. *((char*)ret->pixels + ret->pitch * (y-1) + (j+bh.x*3)) = 0;
  258. }
  259. }
  260. }
  261. }
  262. return ret;
  263. }
  264. SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
  265. {
  266. if(!fname.size())
  267. return NULL;
  268. unsigned char * pcx;
  269. std::transform(fname.begin(),fname.end(),fname.begin(),toupper);
  270. fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".PCX");
  271. Entry *e = bitmaph->entries.znajdz(fname);
  272. if(!e)
  273. {
  274. tlog2<<"File "<<fname<<" not found"<<std::endl;
  275. return NULL;
  276. }
  277. if(e->offset<0)
  278. {
  279. fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".BMP");
  280. fname = "Data/"+fname;
  281. FILE * f = fopen(fname.c_str(),"r");
  282. if(f)
  283. {
  284. fclose(f);
  285. return SDL_LoadBMP(fname.c_str());
  286. }
  287. else //file .bmp not present, check .pcx
  288. {
  289. char sign[3];
  290. fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".PCX");
  291. f = fopen(fname.c_str(),"r");
  292. if(!f)
  293. return NULL;
  294. fread(sign,1,3,f);
  295. if(sign[0]=='B' && sign[1]=='M') //BMP named as PCX - people (eg. Kulex) sometimes use such files
  296. {
  297. fclose(f);
  298. return SDL_LoadBMP(fname.c_str());
  299. }
  300. else //PCX - but we don't know which
  301. {
  302. if((sign[0]==10) && (sign[1]<6) && (sign[2]==1)) //ZSoft PCX
  303. {
  304. fclose(f);
  305. return IMG_Load(fname.c_str());
  306. }
  307. else //H3-style PCX
  308. {
  309. CPCXConv cp;
  310. pcx = new unsigned char[e->realSize];
  311. memcpy(pcx,sign,3);
  312. int res = fread((char*)pcx+3, 1, e->realSize-3, f);
  313. fclose(f);
  314. cp.openPCX((char*)pcx,e->realSize);
  315. return cp.getSurface();
  316. }
  317. }
  318. }
  319. }
  320. bitmap_handler_mx.lock();
  321. fseek(bitmaph->FLOD, e->offset, 0);
  322. if (e->size==0) //file is not compressed
  323. {
  324. pcx = new unsigned char[e->realSize];
  325. fread((char*)pcx, 1, e->realSize, bitmaph->FLOD);
  326. bitmap_handler_mx.unlock();
  327. }
  328. else
  329. {
  330. unsigned char * pcd = new unsigned char[e->size];
  331. fread((char*)pcd, 1, e->size, bitmaph->FLOD);
  332. bitmap_handler_mx.unlock();
  333. int res=bitmaph->infs2(pcd,e->size,e->realSize,pcx);
  334. if(res!=0)
  335. {
  336. tlog2<<"an error "<<res<<" occured while extracting file "<<fname<<std::endl;
  337. }
  338. delete [] pcd;
  339. }
  340. CPCXConv cp;
  341. cp.openPCX((char*)pcx,e->realSize);
  342. SDL_Surface * ret = cp.getSurface();
  343. if(setKey)
  344. SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format,0,255,255));
  345. return ret;
  346. }