CBitmapHandler.cpp 4.4 KB

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