CBitmapHandler.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include "../stdafx.h"
  2. #include "SDL.h"
  3. #include "SDL_image.h"
  4. #include "CBitmapHandler.h"
  5. #include "CDefHandler.h"
  6. #include "../lib/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. extern DLL_EXPORT CLodHandler *bitmaph;
  19. extern DLL_EXPORT CLodHandler *bitmaph_ab;
  20. extern DLL_EXPORT CLodHandler *spriteh;
  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;
  50. bool check1, check2;
  51. unsigned char add;
  52. int it=0;
  53. fSize = readNormalNr(pcx, it); it+=4;
  54. width = readNormalNr(pcx, it); it+=4;
  55. height = readNormalNr(pcx, it); 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. bool isPCX(const unsigned char *header)//check whether file can be PCX according to 1st 12 bytes
  133. {
  134. int fSize = readNormalNr(header, 0);
  135. int width = readNormalNr(header, 4);
  136. int height = readNormalNr(header, 8);
  137. return fSize == width*height || fSize == width*height*3;
  138. }
  139. SDL_Surface * BitmapHandler::loadBitmapFromLod(CLodHandler *lod, std::string fname, bool setKey)
  140. {
  141. if(!fname.size())
  142. {
  143. tlog2 << "Call to loadBitmap with void fname!\n";
  144. return NULL;
  145. }
  146. if (!lod->haveFile(fname, FILE_GRAPHICS))
  147. {
  148. tlog2<<"Entry for file "<<fname<<" was not found"<<std::endl;
  149. return NULL;
  150. }
  151. SDL_Surface * ret=NULL;
  152. int size;
  153. unsigned char * file = 0;
  154. file = lod->giveFile(fname, FILE_GRAPHICS, &size);
  155. if (isPCX(file))
  156. {//H3-style PCX
  157. CPCXConv cp;
  158. cp.openPCX((char*)file,size);
  159. ret = cp.getSurface();
  160. if (!ret)
  161. tlog1<<"Failed to open "<<fname<<" as H3 PCX!\n";
  162. if(ret->format->BytesPerPixel == 1 && setKey)
  163. {
  164. const SDL_Color &c = ret->format->palette->colors[0];
  165. SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format, c.r, c.g, c.b));
  166. }
  167. }
  168. else
  169. { //loading via SDL_Image
  170. std::string filename = lod->getFileName(fname, FILE_GRAPHICS);
  171. std::string ext;
  172. lod->convertName(filename, &ext);
  173. if (ext == ".TGA")//Special case - targa can't be loaded by IMG_Load_RW (no magic constants in header)
  174. {
  175. SDL_RWops *rw = SDL_RWFromMem((void*)file, size);
  176. ret = IMG_LoadTGA_RW( rw );
  177. SDL_FreeRW(rw);
  178. }
  179. else
  180. ret = IMG_Load_RW( SDL_RWFromMem((void*)file, size), 1);
  181. if (!ret)
  182. tlog1<<"Failed to open "<<fname<<" via SDL_Image\n";
  183. delete [] file;
  184. }
  185. return ret;
  186. }
  187. SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
  188. {
  189. if (bitmaph->haveFile(fname, FILE_GRAPHICS))
  190. return loadBitmapFromLod(bitmaph, fname, setKey);
  191. if (bitmaph_ab->haveFile(fname, FILE_GRAPHICS))
  192. return loadBitmapFromLod(bitmaph_ab, fname, setKey);
  193. if (spriteh->haveFile(fname, FILE_GRAPHICS))
  194. return loadBitmapFromLod(spriteh, fname, setKey);
  195. tlog1<<"Failed to find file "<<fname<<"\n";
  196. return NULL;
  197. }