CBitmapHandler.cpp 7.9 KB

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