2
0

CBitmapHandler.cpp 4.4 KB

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