CBitmapHandler.cpp 8.1 KB

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