CBitmapHandler.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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(std::string path, std::string fname);
  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(std::string path, std::string fname)
  90. {
  91. if(!fname.size())
  92. {
  93. logGlobal->warn("Call to loadBitmap with void fname!");
  94. return nullptr;
  95. }
  96. if (!CResourceHandler::get()->existsResource(ResourceID(path + fname, EResType::IMAGE)))
  97. {
  98. return nullptr;
  99. }
  100. SDL_Surface * ret=nullptr;
  101. auto readFile = CResourceHandler::get()->load(ResourceID(path + fname, EResType::IMAGE))->readAll();
  102. if (isPCX(readFile.first.get()))
  103. {//H3-style PCX
  104. ret = loadH3PCX(readFile.first.get(), readFile.second);
  105. if (!ret)
  106. {
  107. logGlobal->error("Failed to open %s as H3 PCX!", fname);
  108. return nullptr;
  109. }
  110. }
  111. else
  112. { //loading via SDL_Image
  113. ret = IMG_Load_RW(
  114. //create SDL_RW with our data (will be deleted by SDL)
  115. SDL_RWFromConstMem((void*)readFile.first.get(), (int)readFile.second),
  116. 1); // mark it for auto-deleting
  117. if (ret)
  118. {
  119. if (ret->format->palette)
  120. {
  121. // set correct value for alpha\unused channel
  122. // NOTE: might be unnecessary with SDL2
  123. for (int i=0; i < ret->format->palette->ncolors; i++)
  124. ret->format->palette->colors[i].a = SDL_ALPHA_OPAQUE;
  125. }
  126. }
  127. else
  128. {
  129. logGlobal->error("Failed to open %s via SDL_Image", fname);
  130. logGlobal->error("Reason: %s", IMG_GetError());
  131. return nullptr;
  132. }
  133. }
  134. // When modifying anything here please check two use cases:
  135. // 1) Vampire mansion in Necropolis (not 1st color is transparent)
  136. // 2) Battle background when fighting on grass/dirt, topmost sky part (NO transparent color)
  137. // 3) New objects that may use 24-bit images for icons (e.g. witchking arts)
  138. // 4) special case - there are 2 .bmp images that have semi-transparency (CCELLGRD.BMP & CCELLSHD.BMP)
  139. if (ret->format->palette &&
  140. ret->format->palette->colors[0].r == 255 &&
  141. ret->format->palette->colors[0].g == 0 &&
  142. ret->format->palette->colors[0].b == 255 )
  143. {
  144. static SDL_Color shadow[3] =
  145. {
  146. { 0, 0, 0, 0},// 100% - transparency
  147. { 0, 0, 0, 32},// 75% - shadow border,
  148. { 0, 0, 0, 128},// 50% - shadow body
  149. };
  150. CSDL_Ext::setColorKey(ret, ret->format->palette->colors[0]);
  151. ret->format->palette->colors[0] = shadow[0];
  152. ret->format->palette->colors[1] = shadow[1];
  153. ret->format->palette->colors[4] = shadow[2];
  154. }
  155. else if (ret->format->palette)
  156. {
  157. CSDL_Ext::setDefaultColorKeyPresize(ret);
  158. }
  159. else if (ret->format->Amask)
  160. {
  161. SDL_SetSurfaceBlendMode(ret, SDL_BLENDMODE_BLEND);
  162. }
  163. else // always set
  164. {
  165. CSDL_Ext::setDefaultColorKey(ret);
  166. }
  167. return ret;
  168. }
  169. SDL_Surface * BitmapHandler::loadBitmap(std::string fname)
  170. {
  171. SDL_Surface * bitmap = nullptr;
  172. if (!(bitmap = loadBitmapFromDir("DATA/", fname)) &&
  173. !(bitmap = loadBitmapFromDir("SPRITES/", fname)))
  174. {
  175. logGlobal->error("Error: Failed to find file %s", fname);
  176. }
  177. return bitmap;
  178. }