2
0

BitmapHandler.cpp 3.9 KB

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