BitmapHandler.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "../lib/vcmi_endian.h"
  15. #include <QBitmap>
  16. #include <QImage>
  17. #include <QPixmap>
  18. namespace BitmapHandler
  19. {
  20. QImage loadH3PCX(ui8 * data, size_t size);
  21. QImage loadBitmapFromDir(const std::string & path, const std::string & fname, bool setKey=true);
  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. QImage loadH3PCX(ui8 * pcx, size_t size)
  35. {
  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, width, 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. std::array<uint8_t, 4> bytes;
  59. bytes[0] = pcx[it++];
  60. bytes[1] = pcx[it++];
  61. bytes[2] = pcx[it++];
  62. bytes[3] = (bytes[0] == 0 && bytes[1] == 255 && bytes[2] == 255) ? 0 : 255;
  63. colorTable.append(qRgba(bytes[0], bytes[1], bytes[2], bytes[3]));
  64. }
  65. image.setColorTable(colorTable);
  66. return image;
  67. }
  68. else
  69. {
  70. QImage image(pcx + it, width, height, width * 3, QImage::Format_BGR888);
  71. return image;
  72. }
  73. }
  74. QImage loadBitmapFromDir(const std::string & path, const std::string & fname, bool setKey)
  75. {
  76. if(!fname.size())
  77. {
  78. logGlobal->warn("Call to loadBitmap with void fname!");
  79. return QImage();
  80. }
  81. if(!CResourceHandler::get()->existsResource(ResourcePath(path + fname, EResType::IMAGE)))
  82. {
  83. return QImage();
  84. }
  85. auto fullpath = CResourceHandler::get()->getResourceName(ResourcePath(path + fname, EResType::IMAGE));
  86. auto readFile = CResourceHandler::get()->load(ResourcePath(path + fname, EResType::IMAGE))->readAll();
  87. if(isPCX(readFile.first.get()))
  88. {//H3-style PCX
  89. auto image = BitmapHandler::loadH3PCX(readFile.first.get(), readFile.second);
  90. if(!image.isNull())
  91. {
  92. if(image.bitPlaneCount() == 1 && setKey)
  93. {
  94. QVector<QRgb> colorTable = image.colorTable();
  95. colorTable[0] = qRgba(255, 255, 255, 0);
  96. image.setColorTable(colorTable);
  97. }
  98. }
  99. else
  100. {
  101. logGlobal->error("Failed to open %s as H3 PCX!", fname);
  102. }
  103. return image.copy(); //copy must be returned here because buffer readFile.first used to build QImage will be cleaned after this line
  104. }
  105. else
  106. { //loading via QImage
  107. QImage image(QString::fromStdString(fullpath->make_preferred().string()));
  108. if(!image.isNull())
  109. {
  110. if(image.bitPlaneCount() == 1)
  111. {
  112. //set correct value for alpha\unused channel
  113. QVector<QRgb> colorTable = image.colorTable();
  114. for(auto & c : colorTable)
  115. c = qRgb(qRed(c), qGreen(c), qBlue(c));
  116. image.setColorTable(colorTable);
  117. }
  118. return image;
  119. }
  120. else
  121. {
  122. logGlobal->error("Failed to open %s via QImage", fname);
  123. return image;
  124. }
  125. }
  126. return QImage();
  127. // When modifying anything here please check use cases:
  128. // 1) Vampire mansion in Necropolis (not 1st color is transparent)
  129. // 2) Battle background when fighting on grass/dirt, topmost sky part (NO transparent color)
  130. // 3) New objects that may use 24-bit images for icons (e.g. witchking arts)
  131. }
  132. QImage loadBitmap(const std::string & fname, bool setKey)
  133. {
  134. for(const auto dir : {"DATA/", "SPRITES/"})
  135. {
  136. auto image = loadBitmapFromDir(dir, fname, setKey);
  137. if(!image.isNull())
  138. return image;
  139. }
  140. logGlobal->error("Error: Failed to find file %s", fname);
  141. return {};
  142. }
  143. }