further performance improvement
This commit is contained in:
parent
a14229aa71
commit
5372838420
|
@ -1,15 +1,13 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <fstream>
|
#include "OutputDevice.h"
|
||||||
#include <QImage>
|
#include <QImage>
|
||||||
#include <QColor>
|
#include <QColor>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
|
||||||
#include "Profiler.h"
|
|
||||||
|
|
||||||
QImage loadTga(QString filePath, bool &success)
|
QImage loadTga(QString filePath, bool &success)
|
||||||
{
|
{
|
||||||
TIC("start");
|
|
||||||
QImage img;
|
QImage img;
|
||||||
success = true;
|
success = true;
|
||||||
|
|
||||||
|
@ -50,34 +48,23 @@ QImage loadTga(QString filePath, bool &success)
|
||||||
// jump to the data block
|
// jump to the data block
|
||||||
file.seek(ui32IDLength + ui32PaletteLength + 18);
|
file.seek(ui32IDLength + ui32PaletteLength + 18);
|
||||||
|
|
||||||
img = QImage(ui32Width, ui32Height, QImage::Format_RGBA8888);
|
|
||||||
|
|
||||||
// uncompressed
|
// uncompressed
|
||||||
if (ui32PicType == 2 && (ui32BpP == 24 || ui32BpP == 32))
|
if (ui32PicType == 2 && (ui32BpP == 24 || ui32BpP == 32))
|
||||||
{
|
{
|
||||||
QVector<quint8> vui8Pixels;
|
img = QImage(ui32Width, ui32Height, ui32BpP == 32 ? QImage::Format_RGBA8888 : QImage::Format_RGB888);
|
||||||
vui8Pixels.resize(ui32Size);
|
int lineWidth = ui32Width * ui32BpP / 8;
|
||||||
file.read(reinterpret_cast<char*>(vui8Pixels.data()), ui32Size);
|
|
||||||
|
|
||||||
for (unsigned int y = 0; y < ui32Height; y++)
|
for (int i = ui32Height - 1; i >= 0; --i)
|
||||||
{
|
file.read(reinterpret_cast<char*>(img.scanLine(i)), lineWidth);
|
||||||
QRgb* imgLine = reinterpret_cast<QRgb*>(img.scanLine(ui32Height - y - 1));
|
|
||||||
for (unsigned int x = 0; x < ui32Width; x++)
|
|
||||||
{
|
|
||||||
int valr = vui8Pixels.at(y * ui32Width * ui32BpP / 8 + x * ui32BpP / 8);
|
|
||||||
int valg = vui8Pixels.at(y * ui32Width * ui32BpP / 8 + x * ui32BpP / 8 + 1);
|
|
||||||
int valb = vui8Pixels.at(y * ui32Width * ui32BpP / 8 + x * ui32BpP / 8 + 2);
|
|
||||||
int vala = 255;
|
|
||||||
if (ui32BpP == 32)
|
|
||||||
vala = vui8Pixels.at(y * ui32Width * ui32BpP / 8 + x * ui32BpP / 8 + 3);
|
|
||||||
|
|
||||||
imgLine[x] = QColor(valr, valg, valb, vala).rgba();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// else if compressed 24 or 32 bit
|
// else if compressed 24 or 32 bit
|
||||||
else if (ui32PicType == 10 && (ui32BpP == 24 || ui32BpP == 32)) // compressed
|
else if (ui32PicType == 10 && (ui32BpP == 24 || ui32BpP == 32)) // compressed
|
||||||
{
|
{
|
||||||
|
OutputDevice::getInstance()->print("compressed tga is not supported by SWBF", 1);
|
||||||
|
|
||||||
|
img = QImage(ui32Width, ui32Height, QImage::Format_RGBA8888);
|
||||||
|
|
||||||
quint8 tempChunkHeader;
|
quint8 tempChunkHeader;
|
||||||
quint8 tempData[5];
|
quint8 tempData[5];
|
||||||
unsigned int tmp_pixelIndex = 0;
|
unsigned int tmp_pixelIndex = 0;
|
||||||
|
@ -97,9 +84,9 @@ QImage loadTga(QString filePath, bool &success)
|
||||||
QColor color;
|
QColor color;
|
||||||
|
|
||||||
if (ui32BpP == 32)
|
if (ui32BpP == 32)
|
||||||
color.setRgba(qRgba(tempData[2], tempData[1], tempData[0], tempData[3]));
|
color.setRgba(qRgba(tempData[0], tempData[1], tempData[2], tempData[3]));
|
||||||
else
|
else
|
||||||
color.setRgba(qRgba(tempData[2], tempData[1], tempData[0], 255));
|
color.setRgba(qRgba(tempData[0], tempData[1], tempData[2], 255));
|
||||||
|
|
||||||
img.setPixel(tmp_pixelIndex % ui32Width, ui32Height - 1 - (tmp_pixelIndex / ui32Width), color.rgba());
|
img.setPixel(tmp_pixelIndex % ui32Width, ui32Height - 1 - (tmp_pixelIndex / ui32Width), color.rgba());
|
||||||
tmp_pixelIndex++;
|
tmp_pixelIndex++;
|
||||||
|
@ -117,9 +104,9 @@ QImage loadTga(QString filePath, bool &success)
|
||||||
QColor color;
|
QColor color;
|
||||||
|
|
||||||
if (ui32BpP == 32)
|
if (ui32BpP == 32)
|
||||||
color.setRgba(qRgba(tempData[2], tempData[1], tempData[0], tempData[3]));
|
color.setRgba(qRgba(tempData[0], tempData[1], tempData[2], tempData[3]));
|
||||||
else
|
else
|
||||||
color.setRgba(qRgba(tempData[2], tempData[1], tempData[0], 255));
|
color.setRgba(qRgba(tempData[0], tempData[1], tempData[2], 255));
|
||||||
|
|
||||||
img.setPixel(tmp_pixelIndex % ui32Width, ui32Height - 1 - (tmp_pixelIndex / ui32Width), color.rgba());
|
img.setPixel(tmp_pixelIndex % ui32Width, ui32Height - 1 - (tmp_pixelIndex / ui32Width), color.rgba());
|
||||||
tmp_pixelIndex++;
|
tmp_pixelIndex++;
|
||||||
|
@ -134,10 +121,8 @@ QImage loadTga(QString filePath, bool &success)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TOC("end");
|
|
||||||
|
|
||||||
if (file.isOpen())
|
if (file.isOpen())
|
||||||
file.close();
|
file.close();
|
||||||
return img;
|
return qMove(img).rgbSwapped();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue