2017-01-05 10:31:36 +00:00
|
|
|
#pragma once
|
2017-02-02 10:14:16 +00:00
|
|
|
#include "OutputDevice.h"
|
2017-01-05 10:31:36 +00:00
|
|
|
#include <QImage>
|
|
|
|
#include <QColor>
|
2017-02-01 16:41:29 +00:00
|
|
|
#include <QVector>
|
|
|
|
#include <QFile>
|
2017-01-05 10:31:36 +00:00
|
|
|
|
|
|
|
|
2017-01-07 14:59:16 +00:00
|
|
|
QImage loadTga(QString filePath, bool &success)
|
2017-01-05 10:31:36 +00:00
|
|
|
{
|
|
|
|
QImage img;
|
2017-02-01 16:41:29 +00:00
|
|
|
success = true;
|
2017-01-05 10:31:36 +00:00
|
|
|
|
2017-01-30 15:00:14 +00:00
|
|
|
// open the file
|
2017-02-01 16:41:29 +00:00
|
|
|
QFile file(filePath);
|
2017-01-05 10:31:36 +00:00
|
|
|
|
2017-02-01 16:41:29 +00:00
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
2017-01-30 15:00:14 +00:00
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
}
|
2017-02-01 16:41:29 +00:00
|
|
|
else
|
2017-01-30 15:00:14 +00:00
|
|
|
{
|
2017-01-05 13:46:01 +00:00
|
|
|
|
2017-02-01 16:41:29 +00:00
|
|
|
// read in the header
|
|
|
|
quint8 ui8x18Header[19] = { 0 };
|
|
|
|
file.read(reinterpret_cast<char*>(&ui8x18Header), sizeof(ui8x18Header) - 1);
|
|
|
|
|
|
|
|
//get variables
|
|
|
|
quint32 ui32BpP;
|
|
|
|
quint32 ui32Width;
|
|
|
|
quint32 ui32Height;
|
|
|
|
quint32 ui32IDLength;
|
|
|
|
quint32 ui32PicType;
|
|
|
|
quint32 ui32PaletteLength;
|
|
|
|
quint32 ui32Size;
|
|
|
|
|
|
|
|
// extract all information from header
|
|
|
|
ui32IDLength = ui8x18Header[0];
|
|
|
|
ui32PicType = ui8x18Header[2];
|
|
|
|
ui32PaletteLength = ui8x18Header[6] * 0x100 + ui8x18Header[5];
|
|
|
|
ui32Width = ui8x18Header[13] * 0x100 + ui8x18Header[12];
|
|
|
|
ui32Height = ui8x18Header[15] * 0x100 + ui8x18Header[14];
|
|
|
|
ui32BpP = ui8x18Header[16];
|
|
|
|
|
|
|
|
// calculate some more information
|
|
|
|
ui32Size = ui32Width * ui32Height * ui32BpP / 8;
|
|
|
|
|
|
|
|
// jump to the data block
|
|
|
|
file.seek(ui32IDLength + ui32PaletteLength + 18);
|
|
|
|
|
|
|
|
// uncompressed
|
|
|
|
if (ui32PicType == 2 && (ui32BpP == 24 || ui32BpP == 32))
|
2017-01-30 15:00:14 +00:00
|
|
|
{
|
2017-02-02 10:14:16 +00:00
|
|
|
img = QImage(ui32Width, ui32Height, ui32BpP == 32 ? QImage::Format_RGBA8888 : QImage::Format_RGB888);
|
|
|
|
int lineWidth = ui32Width * ui32BpP / 8;
|
|
|
|
|
|
|
|
for (int i = ui32Height - 1; i >= 0; --i)
|
|
|
|
file.read(reinterpret_cast<char*>(img.scanLine(i)), lineWidth);
|
|
|
|
|
2017-01-05 10:31:36 +00:00
|
|
|
}
|
2017-02-01 16:41:29 +00:00
|
|
|
// else if compressed 24 or 32 bit
|
|
|
|
else if (ui32PicType == 10 && (ui32BpP == 24 || ui32BpP == 32)) // compressed
|
|
|
|
{
|
2017-02-02 10:14:16 +00:00
|
|
|
OutputDevice::getInstance()->print("compressed tga is not supported by SWBF", 1);
|
|
|
|
|
|
|
|
img = QImage(ui32Width, ui32Height, QImage::Format_RGBA8888);
|
|
|
|
|
2017-02-01 16:41:29 +00:00
|
|
|
quint8 tempChunkHeader;
|
|
|
|
quint8 tempData[5];
|
|
|
|
unsigned int tmp_pixelIndex = 0;
|
2017-01-05 10:31:36 +00:00
|
|
|
|
2017-02-01 16:41:29 +00:00
|
|
|
do {
|
|
|
|
file.read(reinterpret_cast<char*>(&tempChunkHeader), sizeof(tempChunkHeader));
|
2017-01-05 10:31:36 +00:00
|
|
|
|
2017-02-01 16:41:29 +00:00
|
|
|
if (tempChunkHeader >> 7) // repeat count
|
|
|
|
{
|
|
|
|
// just use the first 7 bits
|
|
|
|
tempChunkHeader = (quint8(tempChunkHeader << 1) >> 1);
|
2017-01-05 10:31:36 +00:00
|
|
|
|
2017-02-01 16:41:29 +00:00
|
|
|
file.read(reinterpret_cast<char*>(&tempData), ui32BpP / 8);
|
2017-01-05 10:31:36 +00:00
|
|
|
|
2017-02-01 16:41:29 +00:00
|
|
|
for (int i = 0; i <= tempChunkHeader; i++)
|
|
|
|
{
|
|
|
|
QColor color;
|
2017-01-05 13:46:01 +00:00
|
|
|
|
2017-02-01 16:41:29 +00:00
|
|
|
if (ui32BpP == 32)
|
2017-02-02 10:14:16 +00:00
|
|
|
color.setRgba(qRgba(tempData[0], tempData[1], tempData[2], tempData[3]));
|
2017-02-01 16:41:29 +00:00
|
|
|
else
|
2017-02-02 10:14:16 +00:00
|
|
|
color.setRgba(qRgba(tempData[0], tempData[1], tempData[2], 255));
|
2017-01-05 13:46:01 +00:00
|
|
|
|
2017-02-01 16:41:29 +00:00
|
|
|
img.setPixel(tmp_pixelIndex % ui32Width, ui32Height - 1 - (tmp_pixelIndex / ui32Width), color.rgba());
|
|
|
|
tmp_pixelIndex++;
|
|
|
|
}
|
2017-01-05 10:31:36 +00:00
|
|
|
}
|
2017-02-01 16:41:29 +00:00
|
|
|
else // data count
|
2017-01-30 15:00:14 +00:00
|
|
|
{
|
2017-02-01 16:41:29 +00:00
|
|
|
// just use the first 7 bits
|
|
|
|
tempChunkHeader = (uint8_t(tempChunkHeader << 1) >> 1);
|
2017-01-05 10:31:36 +00:00
|
|
|
|
2017-02-01 16:41:29 +00:00
|
|
|
for (int i = 0; i <= tempChunkHeader; i++)
|
|
|
|
{
|
|
|
|
file.read(reinterpret_cast<char*>(&tempData), ui32BpP / 8);
|
2017-01-05 13:46:01 +00:00
|
|
|
|
2017-02-01 16:41:29 +00:00
|
|
|
QColor color;
|
2017-01-05 13:46:01 +00:00
|
|
|
|
2017-02-01 16:41:29 +00:00
|
|
|
if (ui32BpP == 32)
|
2017-02-02 10:14:16 +00:00
|
|
|
color.setRgba(qRgba(tempData[0], tempData[1], tempData[2], tempData[3]));
|
2017-02-01 16:41:29 +00:00
|
|
|
else
|
2017-02-02 10:14:16 +00:00
|
|
|
color.setRgba(qRgba(tempData[0], tempData[1], tempData[2], 255));
|
2017-02-01 16:41:29 +00:00
|
|
|
|
|
|
|
img.setPixel(tmp_pixelIndex % ui32Width, ui32Height - 1 - (tmp_pixelIndex / ui32Width), color.rgba());
|
|
|
|
tmp_pixelIndex++;
|
|
|
|
}
|
2017-01-05 10:31:36 +00:00
|
|
|
}
|
2017-02-01 16:41:29 +00:00
|
|
|
} while (tmp_pixelIndex < (ui32Width * ui32Height));
|
|
|
|
}
|
|
|
|
// not useable format
|
|
|
|
else
|
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
}
|
2017-01-05 10:31:36 +00:00
|
|
|
}
|
2017-01-30 15:00:14 +00:00
|
|
|
|
2017-02-01 16:41:29 +00:00
|
|
|
if (file.isOpen())
|
|
|
|
file.close();
|
2017-02-02 10:14:16 +00:00
|
|
|
return qMove(img).rgbSwapped();
|
2017-01-30 15:54:35 +00:00
|
|
|
|
2017-01-05 10:31:36 +00:00
|
|
|
}
|