CBitmapHandler.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "StdInc.h"
  2. #include "../lib/Filesystem/CResourceLoader.h"
  3. #include "../lib/Filesystem/CFileInfo.h"
  4. #include "SDL.h"
  5. #include "SDL_image.h"
  6. #include "CBitmapHandler.h"
  7. #include "CDefHandler.h"
  8. #include "UIFramework/SDL_Extensions.h"
  9. #include "../lib/vcmi_endian.h"
  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. bool isPCX(const ui8 *header)//check whether file can be PCX according to header
  20. {
  21. int fSize = read_le_u32(header + 0);
  22. int width = read_le_u32(header + 4);
  23. int height = read_le_u32(header + 8);
  24. return fSize == width*height || fSize == width*height*3;
  25. }
  26. enum Epcxformat
  27. {
  28. PCX8B,
  29. PCX24B
  30. };
  31. SDL_Surface * BitmapHandler::loadH3PCX(ui8 * pcx, size_t size)
  32. {
  33. SDL_Surface * ret;
  34. Epcxformat format;
  35. int it=0;
  36. ui32 fSize = read_le_u32(pcx + it); it+=4;
  37. ui32 width = read_le_u32(pcx + it); it+=4;
  38. ui32 height = read_le_u32(pcx + it); it+=4;
  39. if (fSize==width*height*3)
  40. format=PCX24B;
  41. else if (fSize==width*height)
  42. format=PCX8B;
  43. else
  44. return nullptr;
  45. if (format==PCX8B)
  46. {
  47. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8, 0, 0, 0, 0);
  48. it = 0xC;
  49. for (size_t i=0; i<height; ++i)
  50. {
  51. memcpy((char*)ret->pixels + ret->pitch * i, pcx + it, width);
  52. it+= width;
  53. }
  54. //palette - last 256*3 bytes
  55. it = size-256*3;
  56. for (int i=0;i<256;i++)
  57. {
  58. SDL_Color tp;
  59. tp.r = pcx[it++];
  60. tp.g = pcx[it++];
  61. tp.b = pcx[it++];
  62. tp.unused = SDL_ALPHA_OPAQUE;
  63. ret->format->palette->colors[i] = tp;
  64. }
  65. }
  66. else
  67. {
  68. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  69. int bmask = 0xff0000;
  70. int gmask = 0x00ff00;
  71. int rmask = 0x0000ff;
  72. #else
  73. int bmask = 0x0000ff;
  74. int gmask = 0x00ff00;
  75. int rmask = 0xff0000;
  76. #endif
  77. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 24, rmask, gmask, bmask, 0);
  78. //it == 0xC;
  79. for (size_t i=0; i<height; ++i)
  80. {
  81. memcpy((char*)ret->pixels + ret->pitch * i, pcx + it, width*3);
  82. it+= width*3;
  83. }
  84. }
  85. return ret;
  86. }
  87. SDL_Surface * BitmapHandler::loadBitmapFromDir(std::string path, std::string fname, bool setKey)
  88. {
  89. if(!fname.size())
  90. {
  91. tlog2 << "Call to loadBitmap with void fname!\n";
  92. return NULL;
  93. }
  94. if (!CResourceHandler::get()->existsResource(ResourceID(path + fname, EResType::IMAGE)))
  95. {
  96. return nullptr;
  97. }
  98. SDL_Surface * ret=NULL;
  99. auto readFile = CResourceHandler::get()->loadData(
  100. ResourceID(path + fname, EResType::IMAGE));
  101. if (isPCX(readFile.first.get()))
  102. {//H3-style PCX
  103. ret = loadH3PCX(readFile.first.get(), readFile.second);
  104. if (ret)
  105. {
  106. if(ret->format->BytesPerPixel == 1 && setKey)
  107. {
  108. const SDL_Color &c = ret->format->palette->colors[0];
  109. SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format, c.r, c.g, c.b));
  110. }
  111. }
  112. else
  113. tlog1<<"Failed to open "<<fname<<" as H3 PCX!\n";
  114. }
  115. else
  116. { //loading via SDL_Image
  117. CFileInfo info(CResourceHandler::get()->getResourceName(ResourceID(path + fname, EResType::IMAGE)));
  118. ret = IMG_LoadTyped_RW(
  119. //create SDL_RW with our data (will be deleted by SDL)
  120. SDL_RWFromConstMem((void*)readFile.first.get(), readFile.second),
  121. 1, // mark it for auto-deleting
  122. &info.getExtension()[0] + 1); //pass extension without dot (+1 character)
  123. if (ret)
  124. {
  125. if (ret->format->palette)
  126. {
  127. //set correct value for alpha\unused channel
  128. for (int i=0; i< ret->format->palette->ncolors; i++)
  129. ret->format->palette->colors[i].unused = 255;
  130. }
  131. }
  132. else
  133. {
  134. tlog1<<"Failed to open "<<fname<<" via SDL_Image\n";
  135. }
  136. }
  137. SDL_SetColorKey(ret, SDL_SRCCOLORKEY, SDL_MapRGB(ret->format, 0, 255, 255));
  138. return ret;
  139. }
  140. SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
  141. {
  142. SDL_Surface *bitmap;
  143. if (!(bitmap = loadBitmapFromDir("DATA/", fname, setKey)) &&
  144. !(bitmap = loadBitmapFromDir("SPRITES/", fname, setKey)))
  145. tlog1<<"Error: Failed to find file "<<fname<<"\n";
  146. return bitmap;
  147. }