further performance improvement

This commit is contained in:
Anakin 2017-02-02 11:14:16 +01:00
parent a14229aa71
commit 5372838420
1 changed files with 14 additions and 29 deletions

View File

@ -1,15 +1,13 @@
#pragma once
#include <fstream>
#include "OutputDevice.h"
#include <QImage>
#include <QColor>
#include <QVector>
#include <QFile>
#include "Profiler.h"
QImage loadTga(QString filePath, bool &success)
{
TIC("start");
QImage img;
success = true;
@ -50,34 +48,23 @@ QImage loadTga(QString filePath, bool &success)
// jump to the data block
file.seek(ui32IDLength + ui32PaletteLength + 18);
img = QImage(ui32Width, ui32Height, QImage::Format_RGBA8888);
// uncompressed
if (ui32PicType == 2 && (ui32BpP == 24 || ui32BpP == 32))
{
QVector<quint8> vui8Pixels;
vui8Pixels.resize(ui32Size);
file.read(reinterpret_cast<char*>(vui8Pixels.data()), ui32Size);
img = QImage(ui32Width, ui32Height, ui32BpP == 32 ? QImage::Format_RGBA8888 : QImage::Format_RGB888);
int lineWidth = ui32Width * ui32BpP / 8;
for (unsigned int y = 0; y < ui32Height; y++)
{
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);
for (int i = ui32Height - 1; i >= 0; --i)
file.read(reinterpret_cast<char*>(img.scanLine(i)), lineWidth);
imgLine[x] = QColor(valr, valg, valb, vala).rgba();
}
}
}
// else if compressed 24 or 32 bit
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 tempData[5];
unsigned int tmp_pixelIndex = 0;
@ -97,9 +84,9 @@ QImage loadTga(QString filePath, bool &success)
QColor color;
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
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());
tmp_pixelIndex++;
@ -117,9 +104,9 @@ QImage loadTga(QString filePath, bool &success)
QColor color;
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
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());
tmp_pixelIndex++;
@ -134,10 +121,8 @@ QImage loadTga(QString filePath, bool &success)
}
}
TOC("end");
if (file.isOpen())
file.close();
return img;
return qMove(img).rgbSwapped();
}