CBitmapHandler.cpp 5.0 KB

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