CBitmapHandler.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 CPCXConv::openPCX(char * PCX, int len)
  22. {
  23. pcxs=len;
  24. pcx=(unsigned char*)PCX;
  25. }
  26. void CPCXConv::fromFile(std::string path)
  27. {
  28. std::ifstream is;
  29. is.open(path.c_str(),std::ios::binary);
  30. is.seekg(0,std::ios::end); // to the end
  31. pcxs = is.tellg(); // read length
  32. is.seekg(0,std::ios::beg); // wracamy na poczatek
  33. pcx = new unsigned char[pcxs]; // allocate memory
  34. is.read((char*)pcx, pcxs); // read map file to buffer
  35. is.close();
  36. }
  37. void CPCXConv::saveBMP(std::string path) const
  38. {
  39. std::ofstream os;
  40. os.open(path.c_str(), std::ios::binary);
  41. os.write(reinterpret_cast<const char*>(bmp), bmps);
  42. os.close();
  43. }
  44. SDL_Surface * CPCXConv::getSurface() const
  45. {
  46. SDL_Surface * ret;
  47. int width = -1, height = -1;
  48. Epcxformat format;
  49. int fSize,y;//,i; //TODO use me 'i'
  50. bool check1, check2;
  51. unsigned char add;
  52. int it=0;
  53. fSize = readNormalNr(it,4,pcx);it+=4;
  54. width = readNormalNr(it,4,pcx);it+=4;
  55. height = readNormalNr(it,4,pcx);it+=4;
  56. if (fSize==width*height*3)
  57. check1=true;
  58. else
  59. check1=false;
  60. if (fSize==width*height)
  61. check2=true;
  62. else
  63. check2=false;
  64. if (check1)
  65. format=PCX24B;
  66. else if (check2)
  67. format=PCX8B;
  68. else
  69. return NULL;
  70. add = 4 - width%4;
  71. if (add==4)
  72. add=0;
  73. if (format==PCX8B)
  74. {
  75. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width+add, height, 8, 0, 0, 0, 0);
  76. }
  77. else
  78. {
  79. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  80. int bmask = 0xff0000;
  81. int gmask = 0x00ff00;
  82. int rmask = 0x0000ff;
  83. #else
  84. int bmask = 0x0000ff;
  85. int gmask = 0x00ff00;
  86. int rmask = 0xff0000;
  87. #endif
  88. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width+add, height, 24, rmask, gmask, bmask, 0);
  89. }
  90. if (format==PCX8B)
  91. {
  92. it = pcxs-256*3;
  93. for (int i=0;i<256;i++)
  94. {
  95. SDL_Color tp;
  96. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  97. tp.b = pcx[it++];
  98. tp.g = pcx[it++];
  99. tp.r = pcx[it++];
  100. #else
  101. tp.r = pcx[it++];
  102. tp.g = pcx[it++];
  103. tp.b = pcx[it++];
  104. #endif
  105. tp.unused = 0;
  106. *(ret->format->palette->colors+i) = tp;
  107. }
  108. for (y=height; y>0; --y)
  109. {
  110. it = 0xC + (y-1)*width;
  111. memcpy((char*)ret->pixels + ret->pitch * (y-1), pcx + it, width);
  112. if (add>0)
  113. {
  114. memset((char*)ret->pixels + ret->pitch * (y-1) + width, 0, add);
  115. }
  116. }
  117. }
  118. else
  119. {
  120. for (y=height; y>0; y--)
  121. {
  122. it = 0xC + (y-1)*width*3;
  123. memcpy((char*)ret->pixels + ret->pitch * (y-1), pcx + it, width*3);
  124. if (add>0)
  125. {
  126. memset((char*)ret->pixels + ret->pitch * (y-1) + width*3, 0, add*3);
  127. }
  128. }
  129. }
  130. return ret;
  131. }
  132. SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
  133. {
  134. if(!fname.size())
  135. {
  136. tlog2 << "Call to loadBitmap with void fname!\n";
  137. return NULL;
  138. }
  139. unsigned char * pcx;
  140. std::transform(fname.begin(),fname.end(),fname.begin(),toupper);
  141. fname.replace(fname.find_last_of('.'), fname.find_last_of('.')+4, ".PCX");
  142. Entry *e = bitmaph->entries.znajdz(fname);
  143. if(!e)
  144. {
  145. tlog2<<"File "<<fname<<" not found"<<std::endl;
  146. return NULL;
  147. }
  148. if(e->offset<0)
  149. {
  150. fname.replace(fname.find_last_of('.'),fname.find_last_of('.')+4,".BMP");
  151. fname = DATA_DIR "/Data/" + fname;
  152. FILE * f = fopen(fname.c_str(),"r");
  153. if(f)
  154. {
  155. fclose(f);
  156. return SDL_LoadBMP(fname.c_str());
  157. }
  158. else //file .bmp not present, check .pcx
  159. {
  160. char sign[3];
  161. fname.replace(fname.find_last_of('.'),fname.find_last_of('.')+4,".PCX");
  162. f = fopen(fname.c_str(),"r");
  163. if(!f)
  164. {
  165. tlog1 << "Cannot open " << fname << " - not present as bmp nor as pcx.\n";
  166. return NULL;
  167. }
  168. fread(sign,1,3,f);
  169. if(sign[0]=='B' && sign[1]=='M') //BMP named as PCX - people (eg. Kulex) sometimes use such files
  170. {
  171. fclose(f);
  172. return SDL_LoadBMP(fname.c_str());
  173. }
  174. else //PCX - but we don't know which
  175. {
  176. if((sign[0]==10) && (sign[1]<6) && (sign[2]==1)) //ZSoft PCX
  177. {
  178. fclose(f);
  179. return IMG_Load(fname.c_str());
  180. }
  181. else //H3-style PCX
  182. {
  183. CPCXConv cp;
  184. pcx = new unsigned char[e->realSize];
  185. memcpy(pcx,sign,3);
  186. int res = fread((char*)pcx+3, 1, e->realSize-3, f); //TODO use me
  187. fclose(f);
  188. cp.openPCX((char*)pcx,e->realSize);
  189. return cp.getSurface();
  190. }
  191. }
  192. }
  193. }
  194. pcx = bitmaph->giveFile(e->nameStr, NULL);
  195. CPCXConv cp;
  196. cp.openPCX((char*)pcx, e->realSize);
  197. SDL_Surface * ret = cp.getSurface();
  198. if(ret->format->BytesPerPixel == 1 && setKey)
  199. {
  200. const SDL_Color &c = ret->format->palette->colors[0];
  201. SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format, c.r, c.g, c.b));
  202. }
  203. return ret;
  204. }