2
0

CBitmapHandler.cpp 5.0 KB

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