BitmapHandler.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * BitmapHandler.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. //code is copied from vcmiclient/CBitmapHandler.cpp with minimal changes
  11. #include "StdInc.h"
  12. #include "BitmapHandler.h"
  13. #include "../lib/filesystem/Filesystem.h"
  14. #include <QBitmap>
  15. #include <QImage>
  16. #include <QPixmap>
  17. namespace BitmapHandler
  18. {
  19. QImage loadH3PCX(ui8 * data, size_t size);
  20. QImage loadBitmapFromDir(const std::string & path, const std::string & fname, bool setKey=true);
  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. QImage 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 QImage();
  47. QSize qsize(width, height);
  48. if(format==PCX8B)
  49. {
  50. it = 0xC;
  51. //auto bitmap = QBitmap::fromData(qsize, pcx + it);
  52. QImage image(pcx + it, width, height, QImage::Format_Indexed8);
  53. //palette - last 256*3 bytes
  54. QVector<QRgb> colorTable;
  55. it = (int)size - 256 * 3;
  56. for(int i = 0; i < 256; i++)
  57. {
  58. char bytes[3];
  59. bytes[0] = pcx[it++];
  60. bytes[1] = pcx[it++];
  61. bytes[2] = pcx[it++];
  62. colorTable.append(qRgb(bytes[0], bytes[1], bytes[2]));
  63. }
  64. image.setColorTable(colorTable);
  65. return image;
  66. }
  67. else
  68. {
  69. QImage image(pcx + it, width, height, QImage::Format_RGB32);
  70. return image;
  71. }
  72. }
  73. QImage loadBitmapFromDir(const std::string & path, const std::string & fname, bool setKey)
  74. {
  75. if(!fname.size())
  76. {
  77. logGlobal->warn("Call to loadBitmap with void fname!");
  78. return QImage();
  79. }
  80. if(!CResourceHandler::get()->existsResource(ResourceID(path + fname, EResType::IMAGE)))
  81. {
  82. return QImage();
  83. }
  84. auto fullpath = CResourceHandler::get()->getResourceName(ResourceID(path + fname, EResType::IMAGE));
  85. auto readFile = CResourceHandler::get()->load(ResourceID(path + fname, EResType::IMAGE))->readAll();
  86. if(isPCX(readFile.first.get()))
  87. {//H3-style PCX
  88. auto image = BitmapHandler::loadH3PCX(readFile.first.get(), readFile.second);
  89. if(!image.isNull())
  90. {
  91. if(image.bitPlaneCount() == 1 && setKey)
  92. {
  93. QVector<QRgb> colorTable = image.colorTable();
  94. colorTable[0] = qRgba(255, 255, 255, 0);
  95. image.setColorTable(colorTable);
  96. }
  97. }
  98. else
  99. {
  100. logGlobal->error("Failed to open %s as H3 PCX!", fname);
  101. }
  102. return image;
  103. }
  104. else
  105. { //loading via QImage
  106. QImage image(QString::fromStdString(fullpath->make_preferred().string()));
  107. if(!image.isNull())
  108. {
  109. if(image.bitPlaneCount() == 1)
  110. {
  111. //set correct value for alpha\unused channel
  112. QVector<QRgb> colorTable = image.colorTable();
  113. for(auto & c : colorTable)
  114. c = qRgb(qRed(c), qGreen(c), qBlue(c));
  115. image.setColorTable(colorTable);
  116. }
  117. }
  118. else
  119. {
  120. logGlobal->error("Failed to open %s via QImage", fname);
  121. return image;
  122. }
  123. }
  124. return QImage();
  125. // When modifying anything here please check use cases:
  126. // 1) Vampire mansion in Necropolis (not 1st color is transparent)
  127. // 2) Battle background when fighting on grass/dirt, topmost sky part (NO transparent color)
  128. // 3) New objects that may use 24-bit images for icons (e.g. witchking arts)
  129. }
  130. QImage loadBitmap(const std::string & fname, bool setKey)
  131. {
  132. for(const auto dir : {"DATA/", "SPRITES/"})
  133. {
  134. auto image = loadBitmapFromDir(dir, fname, setKey);
  135. if(!image.isNull())
  136. return image;
  137. }
  138. logGlobal->error("Error: Failed to find file %s", fname);
  139. return {};
  140. }
  141. }