CBitmapHandler.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. extern DLL_EXPORT CLodHandler *bitmaph;
  20. void CPCXConv::openPCX(char * PCX, int len)
  21. {
  22. pcxs=len;
  23. pcx=(unsigned char*)PCX;
  24. }
  25. void CPCXConv::fromFile(std::string path)
  26. {
  27. std::ifstream is;
  28. is.open(path.c_str(),std::ios::binary);
  29. is.seekg(0,std::ios::end); // to the end
  30. pcxs = is.tellg(); // read length
  31. is.seekg(0,std::ios::beg); // wracamy na poczatek
  32. pcx = new unsigned char[pcxs]; // allocate memory
  33. is.read((char*)pcx, pcxs); // read map file to buffer
  34. is.close();
  35. }
  36. void CPCXConv::saveBMP(std::string path) const
  37. {
  38. std::ofstream os;
  39. os.open(path.c_str(), std::ios::binary);
  40. os.write(reinterpret_cast<const char*>(bmp), bmps);
  41. os.close();
  42. }
  43. SDL_Surface * CPCXConv::getSurface() const
  44. {
  45. SDL_Surface * ret;
  46. int width = -1, height = -1;
  47. Epcxformat format;
  48. int fSize,y;//,i; //TODO use me 'i'
  49. bool check1, check2;
  50. unsigned char add;
  51. int it=0;
  52. fSize = readNormalNr(pcx, it); it+=4;
  53. width = readNormalNr(pcx, it); it+=4;
  54. height = readNormalNr(pcx, it); it+=4;
  55. if (fSize==width*height*3)
  56. check1=true;
  57. else
  58. check1=false;
  59. if (fSize==width*height)
  60. check2=true;
  61. else
  62. check2=false;
  63. if (check1)
  64. format=PCX24B;
  65. else if (check2)
  66. format=PCX8B;
  67. else
  68. return NULL;
  69. add = 4 - width%4;
  70. if (add==4)
  71. add=0;
  72. if (format==PCX8B)
  73. {
  74. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width+add, height, 8, 0, 0, 0, 0);
  75. }
  76. else
  77. {
  78. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  79. int bmask = 0xff0000;
  80. int gmask = 0x00ff00;
  81. int rmask = 0x0000ff;
  82. #else
  83. int bmask = 0x0000ff;
  84. int gmask = 0x00ff00;
  85. int rmask = 0xff0000;
  86. #endif
  87. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width+add, height, 24, rmask, gmask, bmask, 0);
  88. }
  89. if (format==PCX8B)
  90. {
  91. it = pcxs-256*3;
  92. for (int i=0;i<256;i++)
  93. {
  94. SDL_Color tp;
  95. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  96. tp.b = pcx[it++];
  97. tp.g = pcx[it++];
  98. tp.r = pcx[it++];
  99. #else
  100. tp.r = pcx[it++];
  101. tp.g = pcx[it++];
  102. tp.b = pcx[it++];
  103. #endif
  104. tp.unused = 0;
  105. *(ret->format->palette->colors+i) = tp;
  106. }
  107. for (y=height; y>0; --y)
  108. {
  109. it = 0xC + (y-1)*width;
  110. memcpy((char*)ret->pixels + ret->pitch * (y-1), pcx + it, width);
  111. if (add>0)
  112. {
  113. memset((char*)ret->pixels + ret->pitch * (y-1) + width, 0, add);
  114. }
  115. }
  116. }
  117. else
  118. {
  119. for (y=height; y>0; y--)
  120. {
  121. it = 0xC + (y-1)*width*3;
  122. memcpy((char*)ret->pixels + ret->pitch * (y-1), pcx + it, width*3);
  123. if (add>0)
  124. {
  125. memset((char*)ret->pixels + ret->pitch * (y-1) + width*3, 0, add*3);
  126. }
  127. }
  128. }
  129. return ret;
  130. }
  131. bool isPCX(const unsigned char *header)//check whether file can be PCX according to 1st 12 bytes
  132. {
  133. int fSize = readNormalNr(header, 0);
  134. int width = readNormalNr(header, 4);
  135. int height = readNormalNr(header, 8);
  136. return fSize == width*height || fSize == width*height*3;
  137. }
  138. SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
  139. {
  140. if(!fname.size())
  141. {
  142. tlog2 << "Call to loadBitmap with void fname!\n";
  143. return NULL;
  144. }
  145. SDL_Surface * ret=NULL;
  146. int size;
  147. unsigned char * file = bitmaph->giveFile(fname, FILE_GRAPHICS, &size);
  148. if (!file)
  149. {
  150. tlog2<<"Entry for file "<<fname<<" was not found"<<std::endl;
  151. return NULL;
  152. }
  153. if (isPCX(file))
  154. {//H3-style PCX
  155. CPCXConv cp;
  156. cp.openPCX((char*)file,size);
  157. ret = cp.getSurface();
  158. if (!ret)
  159. tlog1<<"Failed to open "<<fname<<" as H3 PCX!\n";
  160. if(ret->format->BytesPerPixel == 1 && setKey)
  161. {
  162. const SDL_Color &c = ret->format->palette->colors[0];
  163. SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format, c.r, c.g, c.b));
  164. }
  165. }
  166. else
  167. { //loading via SDL_Image
  168. ret = IMG_Load_RW( SDL_RWFromMem((void*)file, size), 1);
  169. if (!ret)
  170. tlog1<<"Failed to open "<<fname<<" via SDL_Image\n";
  171. }
  172. return ret;
  173. }