1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/mapeditor/BitmapHandler.cpp

161 lines
4.1 KiB
C++
Raw Normal View History

2022-09-24 22:55:05 +02:00
/*
* BitmapHandler.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
2022-10-12 23:51:55 +02:00
//code is copied from vcmiclient/CBitmapHandler.cpp with minimal changes
2022-09-18 01:23:17 +02:00
#include "StdInc.h"
#include "BitmapHandler.h"
#include "../lib/filesystem/Filesystem.h"
#include "../lib/vcmi_endian.h"
2022-09-18 01:23:17 +02:00
#include <QBitmap>
#include <QImage>
#include <QPixmap>
namespace BitmapHandler
{
QImage loadH3PCX(ui8 * data, size_t size);
2022-09-24 22:55:05 +02:00
QImage loadBitmapFromDir(const std::string & path, const std::string & fname, bool setKey=true);
2022-09-18 01:23:17 +02:00
2022-09-24 22:55:05 +02:00
bool isPCX(const ui8 * header)//check whether file can be PCX according to header
2022-09-18 01:23:17 +02:00
{
ui32 fSize = read_le_u32(header + 0);
ui32 width = read_le_u32(header + 4);
ui32 height = read_le_u32(header + 8);
2022-09-24 22:55:05 +02:00
return fSize == width * height || fSize == width * height * 3;
2022-09-18 01:23:17 +02:00
}
enum Epcxformat
{
PCX8B,
PCX24B
};
QImage loadH3PCX(ui8 * pcx, size_t size)
{
Epcxformat format;
2022-09-24 22:55:05 +02:00
int it = 0;
2022-09-18 01:23:17 +02:00
2022-09-24 22:55:05 +02:00
ui32 fSize = read_le_u32(pcx + it); it += 4;
ui32 width = read_le_u32(pcx + it); it += 4;
ui32 height = read_le_u32(pcx + it); it += 4;
2022-09-18 01:23:17 +02:00
2022-09-24 22:55:05 +02:00
if(fSize==width*height*3)
2022-09-18 01:23:17 +02:00
format=PCX24B;
2022-09-24 22:55:05 +02:00
else if(fSize==width*height)
2022-09-18 01:23:17 +02:00
format=PCX8B;
else
return QImage();
QSize qsize(width, height);
2022-09-24 22:55:05 +02:00
if(format==PCX8B)
2022-09-18 01:23:17 +02:00
{
it = 0xC;
//auto bitmap = QBitmap::fromData(qsize, pcx + it);
2022-11-18 22:09:28 +02:00
QImage image(pcx + it, width, height, width, QImage::Format_Indexed8);
2022-09-18 01:23:17 +02:00
//palette - last 256*3 bytes
QVector<QRgb> colorTable;
2022-09-24 22:55:05 +02:00
it = (int)size - 256 * 3;
for(int i = 0; i < 256; i++)
2022-09-18 01:23:17 +02:00
{
char bytes[3];
bytes[0] = pcx[it++];
bytes[1] = pcx[it++];
bytes[2] = pcx[it++];
colorTable.append(qRgb(bytes[0], bytes[1], bytes[2]));
}
image.setColorTable(colorTable);
return image;
}
else
{
2022-11-18 23:02:18 +02:00
QImage image(pcx + it, width, height, width * 3, QImage::Format_RGB888);
2022-09-18 01:23:17 +02:00
return image;
}
}
2022-09-24 22:55:05 +02:00
QImage loadBitmapFromDir(const std::string & path, const std::string & fname, bool setKey)
2022-09-18 01:23:17 +02:00
{
if(!fname.size())
{
logGlobal->warn("Call to loadBitmap with void fname!");
return QImage();
}
if(!CResourceHandler::get()->existsResource(ResourcePath(path + fname, EResType::IMAGE)))
2022-09-18 01:23:17 +02:00
{
return QImage();
}
auto fullpath = CResourceHandler::get()->getResourceName(ResourcePath(path + fname, EResType::IMAGE));
auto readFile = CResourceHandler::get()->load(ResourcePath(path + fname, EResType::IMAGE))->readAll();
2022-09-18 01:23:17 +02:00
2022-09-24 22:55:05 +02:00
if(isPCX(readFile.first.get()))
2022-09-18 01:23:17 +02:00
{//H3-style PCX
auto image = BitmapHandler::loadH3PCX(readFile.first.get(), readFile.second);
if(!image.isNull())
{
if(image.bitPlaneCount() == 1 && setKey)
{
QVector<QRgb> colorTable = image.colorTable();
colorTable[0] = qRgba(255, 255, 255, 0);
image.setColorTable(colorTable);
}
}
else
{
logGlobal->error("Failed to open %s as H3 PCX!", fname);
}
2022-11-18 23:52:21 +02:00
return image.copy(); //copy must be returned here because buffer readFile.first used to build QImage will be cleaned after this line
2022-09-18 01:23:17 +02:00
}
else
{ //loading via QImage
2022-09-18 01:23:17 +02:00
QImage image(QString::fromStdString(fullpath->make_preferred().string()));
if(!image.isNull())
{
if(image.bitPlaneCount() == 1)
{
//set correct value for alpha\unused channel
QVector<QRgb> colorTable = image.colorTable();
for(auto & c : colorTable)
c = qRgb(qRed(c), qGreen(c), qBlue(c));
image.setColorTable(colorTable);
}
return image;
2022-09-18 01:23:17 +02:00
}
else
{
logGlobal->error("Failed to open %s via QImage", fname);
return image;
}
}
return QImage();
2022-09-24 22:55:05 +02:00
// When modifying anything here please check use cases:
2022-09-18 01:23:17 +02:00
// 1) Vampire mansion in Necropolis (not 1st color is transparent)
// 2) Battle background when fighting on grass/dirt, topmost sky part (NO transparent color)
// 3) New objects that may use 24-bit images for icons (e.g. witchking arts)
}
2022-09-24 22:55:05 +02:00
QImage loadBitmap(const std::string & fname, bool setKey)
2022-09-18 01:23:17 +02:00
{
for(const auto dir : {"DATA/", "SPRITES/"})
2022-09-18 01:23:17 +02:00
{
auto image = loadBitmapFromDir(dir, fname, setKey);
if(!image.isNull())
return image;
2022-09-18 01:23:17 +02:00
}
logGlobal->error("Error: Failed to find file %s", fname);
return {};
2022-09-18 01:23:17 +02:00
}
}