CBitmapHandler.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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;
  51. int it=0;
  52. fSize = read_le_u32(pcx + it); it+=4;
  53. width = read_le_u32(pcx + it); it+=4;
  54. height = read_le_u32(pcx + it); it+=4;
  55. if (fSize==width*height*3)
  56. format=PCX24B;
  57. else if (fSize==width*height)
  58. format=PCX8B;
  59. else
  60. return NULL;
  61. if (format==PCX8B)
  62. {
  63. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8, 0, 0, 0, 0);
  64. it = 0xC;
  65. for (int i=0; i<height; i++)
  66. {
  67. memcpy((char*)ret->pixels + ret->pitch * i, pcx + it, width);
  68. it+= width;
  69. }
  70. it = pcxs-256*3;
  71. for (int i=0;i<256;i++)
  72. {
  73. SDL_Color tp;
  74. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  75. tp.b = pcx[it++];
  76. tp.g = pcx[it++];
  77. tp.r = pcx[it++];
  78. #else
  79. tp.r = pcx[it++];
  80. tp.g = pcx[it++];
  81. tp.b = pcx[it++];
  82. #endif
  83. tp.unused = 0;
  84. ret->format->palette->colors[i] = tp;
  85. }
  86. }
  87. else
  88. {
  89. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  90. int bmask = 0xff0000;
  91. int gmask = 0x00ff00;
  92. int rmask = 0x0000ff;
  93. #else
  94. int bmask = 0x0000ff;
  95. int gmask = 0x00ff00;
  96. int rmask = 0xff0000;
  97. #endif
  98. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 24, rmask, gmask, bmask, 0);
  99. it = 0xC;
  100. for (int i=0; i<height; i++)
  101. {
  102. memcpy((char*)ret->pixels + ret->pitch * i, pcx + it, width*3);
  103. it+= width*3;
  104. }
  105. }
  106. return ret;
  107. }
  108. bool isPCX(const unsigned char *header)//check whether file can be PCX according to 1st 12 bytes
  109. {
  110. int fSize = read_le_u32(header + 0);
  111. int width = read_le_u32(header + 4);
  112. int height = read_le_u32(header + 8);
  113. return fSize == width*height || fSize == width*height*3;
  114. }
  115. SDL_Surface * BitmapHandler::loadBitmapFromLod(CLodHandler *lod, std::string fname, bool setKey)
  116. {
  117. if(!fname.size())
  118. {
  119. tlog2 << "Call to loadBitmap with void fname!\n";
  120. return NULL;
  121. }
  122. if (!lod->haveFile(fname, FILE_GRAPHICS))
  123. {
  124. tlog2<<"Entry for file "<<fname<<" was not found"<<std::endl;
  125. return NULL;
  126. }
  127. SDL_Surface * ret=NULL;
  128. int size;
  129. unsigned char * file = 0;
  130. file = lod->giveFile(fname, FILE_GRAPHICS, &size);
  131. if (isPCX(file))
  132. {//H3-style PCX
  133. CPCXConv cp;
  134. cp.openPCX((char*)file,size);
  135. ret = cp.getSurface();
  136. if (!ret)
  137. tlog1<<"Failed to open "<<fname<<" as H3 PCX!\n";
  138. if(ret->format->BytesPerPixel == 1 && setKey)
  139. {
  140. const SDL_Color &c = ret->format->palette->colors[0];
  141. SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format, c.r, c.g, c.b));
  142. }
  143. }
  144. else
  145. { //loading via SDL_Image
  146. std::string filename = lod->getFileName(fname, FILE_GRAPHICS);
  147. std::string ext;
  148. lod->convertName(filename, &ext);
  149. if (ext == ".TGA")//Special case - targa can't be loaded by IMG_Load_RW (no magic constants in header)
  150. {
  151. SDL_RWops *rw = SDL_RWFromMem((void*)file, size);
  152. ret = IMG_LoadTGA_RW( rw );
  153. SDL_FreeRW(rw);
  154. }
  155. else
  156. ret = IMG_Load_RW( SDL_RWFromMem((void*)file, size), 1);
  157. if (!ret)
  158. tlog1<<"Failed to open "<<fname<<" via SDL_Image\n";
  159. delete [] file;
  160. }
  161. return ret;
  162. }
  163. SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
  164. {
  165. SDL_Surface *bitmap;
  166. if (!(bitmap = loadBitmapFromLod(bitmaph, fname, setKey)) &&
  167. !(bitmap = loadBitmapFromLod(bitmaph_ab, fname, setKey)) &&
  168. !(bitmap = loadBitmapFromLod(spriteh, fname, setKey)))
  169. tlog1<<"Failed to find file "<<fname<<"\n";
  170. return bitmap;
  171. }