CBitmapHandler.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * CBitmapHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "../lib/filesystem/Filesystem.h"
  12. #include "SDL.h"
  13. #include "SDL_image.h"
  14. #include "CBitmapHandler.h"
  15. #include "gui/SDL_Extensions.h"
  16. #include "../lib/vcmi_endian.h"
  17. namespace BitmapHandler
  18. {
  19. SDL_Surface * loadH3PCX(ui8 * data, size_t size);
  20. SDL_Surface * loadBitmapFromDir(std::string path, std::string fname, bool setKey = true);
  21. }
  22. bool isPCX(const ui8 * header) //check whether file can be PCX according to header
  23. {
  24. int fSize = read_le_u32(header + 0);
  25. int width = read_le_u32(header + 4);
  26. int height = read_le_u32(header + 8);
  27. return fSize == width * height || fSize == width * height * 3;
  28. }
  29. enum Epcxformat
  30. {
  31. PCX8B,
  32. PCX24B
  33. };
  34. SDL_Surface * BitmapHandler::loadH3PCX(ui8 * pcx, size_t size)
  35. {
  36. SDL_Surface * ret;
  37. Epcxformat format;
  38. int it = 0;
  39. ui32 fSize = read_le_u32(pcx + it);
  40. it += 4;
  41. ui32 width = read_le_u32(pcx + it);
  42. it += 4;
  43. ui32 height = read_le_u32(pcx + it);
  44. it += 4;
  45. if(fSize == width * height * 3)
  46. format = PCX24B;
  47. else if(fSize == width * height)
  48. format = PCX8B;
  49. else
  50. return nullptr;
  51. if(format == PCX8B)
  52. {
  53. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8, 0, 0, 0, 0);
  54. it = 0xC;
  55. for(int i = 0; i < height; i++)
  56. {
  57. memcpy((char *)ret->pixels + ret->pitch * i, pcx + it, width);
  58. it += width;
  59. }
  60. //palette - last 256*3 bytes
  61. it = size - 256 * 3;
  62. for(int i = 0; i < 256; i++)
  63. {
  64. SDL_Color tp;
  65. tp.r = pcx[it++];
  66. tp.g = pcx[it++];
  67. tp.b = pcx[it++];
  68. tp.a = SDL_ALPHA_OPAQUE;
  69. ret->format->palette->colors[i] = tp;
  70. }
  71. }
  72. else
  73. {
  74. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  75. int bmask = 0xff0000;
  76. int gmask = 0x00ff00;
  77. int rmask = 0x0000ff;
  78. #else
  79. int bmask = 0x0000ff;
  80. int gmask = 0x00ff00;
  81. int rmask = 0xff0000;
  82. #endif
  83. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 24, rmask, gmask, bmask, 0);
  84. //it == 0xC;
  85. for(int i = 0; i < height; i++)
  86. {
  87. memcpy((char *)ret->pixels + ret->pitch * i, pcx + it, width * 3);
  88. it += width * 3;
  89. }
  90. }
  91. return ret;
  92. }
  93. SDL_Surface * BitmapHandler::loadBitmapFromDir(std::string path, std::string fname, bool setKey)
  94. {
  95. if(!fname.size())
  96. {
  97. logGlobal->warnStream() << "Call to loadBitmap with void fname!";
  98. return nullptr;
  99. }
  100. if(!CResourceHandler::get()->existsResource(ResourceID(path + fname, EResType::IMAGE)))
  101. {
  102. return nullptr;
  103. }
  104. SDL_Surface * ret = nullptr;
  105. auto readFile = CResourceHandler::get()->load(ResourceID(path + fname, EResType::IMAGE))->readAll();
  106. if(isPCX(readFile.first.get()))
  107. { //H3-style PCX
  108. ret = loadH3PCX(readFile.first.get(), readFile.second);
  109. if(ret)
  110. {
  111. if(ret->format->BytesPerPixel == 1 && setKey)
  112. {
  113. CSDL_Ext::setColorKey(ret, ret->format->palette->colors[0]);
  114. }
  115. }
  116. else
  117. {
  118. logGlobal->errorStream() << "Failed to open " << fname << " as H3 PCX!";
  119. return nullptr;
  120. }
  121. }
  122. else
  123. { //loading via SDL_Image
  124. ret = IMG_Load_RW(
  125. //create SDL_RW with our data (will be deleted by SDL)
  126. SDL_RWFromConstMem((void *)readFile.first.get(), readFile.second),
  127. 1); // mark it for auto-deleting
  128. if(ret)
  129. {
  130. if(ret->format->palette)
  131. {
  132. //set correct value for alpha\unused channel
  133. for(int i = 0; i < ret->format->palette->ncolors; i++)
  134. ret->format->palette->colors[i].a = SDL_ALPHA_OPAQUE;
  135. }
  136. }
  137. else
  138. {
  139. logGlobal->errorStream() << "Failed to open " << fname << " via SDL_Image";
  140. logGlobal->errorStream() << "Reason: " << IMG_GetError();
  141. return nullptr;
  142. }
  143. }
  144. // When modifying anything here please check two use cases:
  145. // 1) Vampire mansion in Necropolis (not 1st color is transparent)
  146. // 2) Battle background when fighting on grass/dirt, topmost sky part (NO transparent color)
  147. // 3) New objects that may use 24-bit images for icons (e.g. witchking arts)
  148. if(ret->format->palette)
  149. {
  150. CSDL_Ext::setDefaultColorKeyPresize(ret);
  151. }
  152. else if(ret->format->Amask)
  153. {
  154. SDL_SetSurfaceBlendMode(ret, SDL_BLENDMODE_BLEND);
  155. }
  156. else // always set
  157. {
  158. CSDL_Ext::setDefaultColorKey(ret);
  159. }
  160. return ret;
  161. }
  162. SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
  163. {
  164. SDL_Surface * bitmap;
  165. if(!(bitmap = loadBitmapFromDir("DATA/", fname, setKey)) &&
  166. !(bitmap = loadBitmapFromDir("SPRITES/", fname, setKey)))
  167. {
  168. logGlobal->errorStream() << "Error: Failed to find file " << fname;
  169. }
  170. return bitmap;
  171. }