CBitmapHandler.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "../lib/vcmi_endian.h"
  8. #include <sstream>
  9. #include <boost/thread.hpp>
  10. /*
  11. * CBitmapHandler.cpp, part of VCMI engine
  12. *
  13. * Authors: listed in file AUTHORS in main folder
  14. *
  15. * License: GNU General Public License v2.0 or later
  16. * Full text of license available in license.txt file, in main folder
  17. *
  18. */
  19. extern DLL_EXPORT CLodHandler *bitmaph;
  20. extern DLL_EXPORT CLodHandler *bitmaph_ab;
  21. extern DLL_EXPORT CLodHandler *spriteh;
  22. void CPCXConv::openPCX(char * PCX, int len)
  23. {
  24. pcxs=len;
  25. pcx=(unsigned char*)PCX;
  26. }
  27. void CPCXConv::fromFile(std::string path)
  28. {
  29. std::ifstream is;
  30. is.open(path.c_str(),std::ios::binary);
  31. is.seekg(0,std::ios::end); // to the end
  32. pcxs = is.tellg(); // read length
  33. is.seekg(0,std::ios::beg); // wracamy na poczatek
  34. pcx = new unsigned char[pcxs]; // allocate memory
  35. is.read((char*)pcx, pcxs); // read map file to buffer
  36. is.close();
  37. }
  38. void CPCXConv::saveBMP(std::string path) const
  39. {
  40. std::ofstream os;
  41. os.open(path.c_str(), std::ios::binary);
  42. os.write(reinterpret_cast<const char*>(bmp), bmps);
  43. os.close();
  44. }
  45. SDL_Surface * CPCXConv::getSurface() const
  46. {
  47. SDL_Surface * ret;
  48. int width = -1, height = -1;
  49. Epcxformat format;
  50. int fSize,y;
  51. unsigned char add;
  52. int it=0;
  53. fSize = read_le_u32(pcx + it); it+=4;
  54. width = read_le_u32(pcx + it); it+=4;
  55. height = read_le_u32(pcx + it); it+=4;
  56. if (fSize==width*height*3)
  57. format=PCX24B;
  58. else if (fSize==width*height)
  59. format=PCX8B;
  60. else
  61. return NULL;
  62. add = 4 - width%4;
  63. if (add==4)
  64. add=0;
  65. if (format==PCX8B)
  66. {
  67. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width+add, height, 8, 0, 0, 0, 0);
  68. it = pcxs-256*3;
  69. for (int i=0;i<256;i++)
  70. {
  71. SDL_Color tp;
  72. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  73. tp.b = pcx[it++];
  74. tp.g = pcx[it++];
  75. tp.r = pcx[it++];
  76. #else
  77. tp.r = pcx[it++];
  78. tp.g = pcx[it++];
  79. tp.b = pcx[it++];
  80. #endif
  81. tp.unused = 0;
  82. *(ret->format->palette->colors+i) = tp;
  83. }
  84. for (y=height; y>0; --y)
  85. {
  86. it = 0xC + (y-1)*width;
  87. memcpy((char*)ret->pixels + ret->pitch * (y-1), pcx + it, width);
  88. if (add>0)
  89. {
  90. memset((char*)ret->pixels + ret->pitch * (y-1) + width, 0, add);
  91. }
  92. }
  93. }
  94. else
  95. {
  96. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  97. int bmask = 0xff0000;
  98. int gmask = 0x00ff00;
  99. int rmask = 0x0000ff;
  100. #else
  101. int bmask = 0x0000ff;
  102. int gmask = 0x00ff00;
  103. int rmask = 0xff0000;
  104. #endif
  105. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width+add, height, 24, rmask, gmask, bmask, 0);
  106. for (y=height; y>0; y--)
  107. {
  108. it = 0xC + (y-1)*width*3;
  109. memcpy((char*)ret->pixels + ret->pitch * (y-1), pcx + it, width*3);
  110. if (add>0)
  111. {
  112. memset((char*)ret->pixels + ret->pitch * (y-1) + width*3, 0, add*3);
  113. }
  114. }
  115. }
  116. return ret;
  117. }
  118. bool isPCX(const unsigned char *header)//check whether file can be PCX according to 1st 12 bytes
  119. {
  120. int fSize = read_le_u32(header + 0);
  121. int width = read_le_u32(header + 4);
  122. int height = read_le_u32(header + 8);
  123. return fSize == width*height || fSize == width*height*3;
  124. }
  125. SDL_Surface * BitmapHandler::loadBitmapFromLod(CLodHandler *lod, std::string fname, bool setKey)
  126. {
  127. if(!fname.size())
  128. {
  129. tlog2 << "Call to loadBitmap with void fname!\n";
  130. return NULL;
  131. }
  132. if (!lod->haveFile(fname, FILE_GRAPHICS))
  133. {
  134. tlog2<<"Entry for file "<<fname<<" was not found"<<std::endl;
  135. return NULL;
  136. }
  137. SDL_Surface * ret=NULL;
  138. int size;
  139. unsigned char * file = 0;
  140. file = lod->giveFile(fname, FILE_GRAPHICS, &size);
  141. if (isPCX(file))
  142. {//H3-style PCX
  143. CPCXConv cp;
  144. cp.openPCX((char*)file,size);
  145. ret = cp.getSurface();
  146. if (!ret)
  147. tlog1<<"Failed to open "<<fname<<" as H3 PCX!\n";
  148. if(ret->format->BytesPerPixel == 1 && setKey)
  149. {
  150. const SDL_Color &c = ret->format->palette->colors[0];
  151. SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format, c.r, c.g, c.b));
  152. }
  153. }
  154. else
  155. { //loading via SDL_Image
  156. std::string filename = lod->getFileName(fname, FILE_GRAPHICS);
  157. std::string ext;
  158. lod->convertName(filename, &ext);
  159. if (ext == ".TGA")//Special case - targa can't be loaded by IMG_Load_RW (no magic constants in header)
  160. {
  161. SDL_RWops *rw = SDL_RWFromMem((void*)file, size);
  162. ret = IMG_LoadTGA_RW( rw );
  163. SDL_FreeRW(rw);
  164. }
  165. else
  166. ret = IMG_Load_RW( SDL_RWFromMem((void*)file, size), 1);
  167. if (!ret)
  168. tlog1<<"Failed to open "<<fname<<" via SDL_Image\n";
  169. delete [] file;
  170. }
  171. return ret;
  172. }
  173. SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
  174. {
  175. SDL_Surface *bitmap;
  176. if (!(bitmap = loadBitmapFromLod(bitmaph, fname, setKey)) &&
  177. !(bitmap = loadBitmapFromLod(bitmaph_ab, fname, setKey)) &&
  178. !(bitmap = loadBitmapFromLod(spriteh, fname, setKey)))
  179. tlog1<<"Failed to find file "<<fname<<"\n";
  180. return bitmap;
  181. }