CBitmapHandler.cpp 7.7 KB

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