CBitmapHandler.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "StdInc.h"
  2. #include "../lib/filesystem/Filesystem.h"
  3. #include "SDL.h"
  4. #include "SDL_image.h"
  5. #include "CBitmapHandler.h"
  6. #include "gui/SDL_Extensions.h"
  7. #include "../lib/vcmi_endian.h"
  8. /*
  9. * CBitmapHandler.cpp, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  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); 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(SDL_SWSURFACE, width, height, 8, 0, 0, 0, 0);
  51. it = 0xC;
  52. for (int i=0; i<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 = 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(SDL_SWSURFACE, width, height, 24, rmask, gmask, bmask, 0);
  81. //it == 0xC;
  82. for (int i=0; i<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, bool setKey)
  91. {
  92. if(!fname.size())
  93. {
  94. logGlobal->warnStream() << "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. if(ret->format->BytesPerPixel == 1 && setKey)
  109. {
  110. CSDL_Ext::setColorKey(ret,ret->format->palette->colors[0]);
  111. }
  112. }
  113. else
  114. {
  115. logGlobal->errorStream()<<"Failed to open "<<fname<<" as H3 PCX!";
  116. return nullptr;
  117. }
  118. }
  119. else
  120. { //loading via SDL_Image
  121. ret = IMG_Load_RW(
  122. //create SDL_RW with our data (will be deleted by SDL)
  123. SDL_RWFromConstMem((void*)readFile.first.get(), readFile.second),
  124. 1); // mark it for auto-deleting
  125. if (ret)
  126. {
  127. if (ret->format->palette)
  128. {
  129. //set correct value for alpha\unused channel
  130. for (int i=0; i < ret->format->palette->ncolors; i++)
  131. ret->format->palette->colors[i].a = SDL_ALPHA_OPAQUE;
  132. }
  133. }
  134. else
  135. {
  136. logGlobal->errorStream() << "Failed to open " << fname << " via SDL_Image";
  137. logGlobal->errorStream() << "Reason: " << IMG_GetError();
  138. return nullptr;
  139. }
  140. }
  141. // When modifying anything here please check two use cases:
  142. // 1) Vampire mansion in Necropolis (not 1st color is transparent)
  143. // 2) Battle background when fighting on grass/dirt, topmost sky part (NO transparent color)
  144. // 3) New objects that may use 24-bit images for icons (e.g. witchking arts)
  145. if (ret->format->palette)
  146. {
  147. CSDL_Ext::setDefaultColorKeyPresize(ret);
  148. }
  149. else // always set
  150. {
  151. CSDL_Ext::setDefaultColorKey(ret);
  152. }
  153. return ret;
  154. }
  155. SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
  156. {
  157. SDL_Surface *bitmap;
  158. if (!(bitmap = loadBitmapFromDir("DATA/", fname, setKey)) &&
  159. !(bitmap = loadBitmapFromDir("SPRITES/", fname, setKey)))
  160. {
  161. logGlobal->errorStream() << "Error: Failed to find file " << fname;
  162. }
  163. return bitmap;
  164. }