CBitmapHandler.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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, const unsigned char * str);
  20. extern DLL_EXPORT CLodHandler *bitmaph;
  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. SDL_Surface * CPCXConv::getSurface() const
  63. {
  64. SDL_Surface * ret;
  65. int width = -1, height = -1;
  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. fSize = readNormalNr(it,4,pcx);it+=4;
  72. width = readNormalNr(it,4,pcx);it+=4;
  73. height = readNormalNr(it,4,pcx);it+=4;
  74. if (fSize==width*height*3)
  75. check1=true;
  76. else
  77. check1=false;
  78. if (fSize==width*height)
  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 NULL;
  88. add = 4 - width%4;
  89. if (add==4)
  90. add=0;
  91. if (format==PCX8B)
  92. {
  93. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width+add, height, 8, 0, 0, 0, 0);
  94. }
  95. else
  96. {
  97. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  98. int bmask = 0xff0000;
  99. int gmask = 0x00ff00;
  100. int rmask = 0x0000ff;
  101. #else
  102. int bmask = 0x0000ff;
  103. int gmask = 0x00ff00;
  104. int rmask = 0xff0000;
  105. #endif
  106. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width+add, height, 24, rmask, gmask, bmask, 0);
  107. }
  108. if (format==PCX8B)
  109. {
  110. it = pcxs-256*3;
  111. for (int i=0;i<256;i++)
  112. {
  113. SDL_Color tp;
  114. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  115. tp.b = pcx[it++];
  116. tp.g = pcx[it++];
  117. tp.r = pcx[it++];
  118. #else
  119. tp.r = pcx[it++];
  120. tp.g = pcx[it++];
  121. tp.b = pcx[it++];
  122. #endif
  123. tp.unused = 0;
  124. *(ret->format->palette->colors+i) = tp;
  125. }
  126. for (y=height;y>0;y--)
  127. {
  128. it=0xC+(y-1)*width;
  129. for (int j=0;j<width;j++)
  130. {
  131. *((char*)ret->pixels + ret->pitch * (y-1) + ret->format->BytesPerPixel * j) = pcx[it+j];
  132. }
  133. if (add>0)
  134. {
  135. for (int j=0;j<add;j++)
  136. {
  137. *((char*)ret->pixels + ret->pitch * (y-1) + ret->format->BytesPerPixel * (j+width)) = 0;
  138. }
  139. }
  140. }
  141. }
  142. else
  143. {
  144. for (y=height; y>0; y--)
  145. {
  146. it=0xC+(y-1)*width*3;
  147. for (int j=0;j<width*3;j++)
  148. {
  149. *((char*)ret->pixels + ret->pitch * (y-1) + j) = pcx[it+j];
  150. }
  151. if (add>0)
  152. {
  153. for (int j=0;j<add*3;j++)
  154. {
  155. *((char*)ret->pixels + ret->pitch * (y-1) + (j+width*3)) = 0;
  156. }
  157. }
  158. }
  159. }
  160. return ret;
  161. }
  162. SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
  163. {
  164. if(!fname.size())
  165. {
  166. tlog2 << "Call to loadBitmap with void fname!\n";
  167. return NULL;
  168. }
  169. unsigned char * pcx;
  170. std::transform(fname.begin(),fname.end(),fname.begin(),toupper);
  171. fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".PCX");
  172. Entry *e = bitmaph->entries.znajdz(fname);
  173. if(!e)
  174. {
  175. tlog2<<"File "<<fname<<" not found"<<std::endl;
  176. return NULL;
  177. }
  178. if(e->offset<0)
  179. {
  180. fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".BMP");
  181. fname = "Data/"+fname;
  182. FILE * f = fopen(fname.c_str(),"r");
  183. if(f)
  184. {
  185. fclose(f);
  186. return SDL_LoadBMP(fname.c_str());
  187. }
  188. else //file .bmp not present, check .pcx
  189. {
  190. char sign[3];
  191. fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".PCX");
  192. f = fopen(fname.c_str(),"r");
  193. if(!f)
  194. {
  195. tlog1 << "Cannot open " << fname << " - not present as bmp nor as pcx.\n";
  196. return NULL;
  197. }
  198. fread(sign,1,3,f);
  199. if(sign[0]=='B' && sign[1]=='M') //BMP named as PCX - people (eg. Kulex) sometimes use such files
  200. {
  201. fclose(f);
  202. return SDL_LoadBMP(fname.c_str());
  203. }
  204. else //PCX - but we don't know which
  205. {
  206. if((sign[0]==10) && (sign[1]<6) && (sign[2]==1)) //ZSoft PCX
  207. {
  208. fclose(f);
  209. return IMG_Load(fname.c_str());
  210. }
  211. else //H3-style PCX
  212. {
  213. CPCXConv cp;
  214. pcx = new unsigned char[e->realSize];
  215. memcpy(pcx,sign,3);
  216. int res = fread((char*)pcx+3, 1, e->realSize-3, f); //TODO use me
  217. fclose(f);
  218. cp.openPCX((char*)pcx,e->realSize);
  219. return cp.getSurface();
  220. }
  221. }
  222. }
  223. }
  224. bitmap_handler_mx.lock();
  225. fseek(bitmaph->FLOD, e->offset, 0);
  226. if (e->size==0) //file is not compressed
  227. {
  228. pcx = new unsigned char[e->realSize];
  229. fread((char*)pcx, 1, e->realSize, bitmaph->FLOD);
  230. bitmap_handler_mx.unlock();
  231. }
  232. else
  233. {
  234. unsigned char * pcd = new unsigned char[e->size];
  235. fread((char*)pcd, 1, e->size, bitmaph->FLOD);
  236. bitmap_handler_mx.unlock();
  237. int res=bitmaph->infs2(pcd,e->size,e->realSize,pcx);
  238. if(res!=0)
  239. {
  240. tlog2<<"an error "<<res<<" occurred while extracting file "<<fname<<std::endl;
  241. }
  242. delete [] pcd;
  243. }
  244. CPCXConv cp;
  245. cp.openPCX((char*)pcx, e->realSize);
  246. SDL_Surface * ret = cp.getSurface();
  247. if(ret->format->BytesPerPixel == 1 && setKey)
  248. {
  249. const SDL_Color &c = ret->format->palette->colors[0];
  250. SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format, c.r, c.g, c.b));
  251. }
  252. return ret;
  253. }