From 53728384205b393170e1bf3229165a8aa4d6d4b6 Mon Sep 17 00:00:00 2001 From: Anakin Date: Thu, 2 Feb 2017 11:14:16 +0100 Subject: [PATCH] further performance improvement --- QtMeshViewer/Header/tga.h | 43 +++++++++++++-------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/QtMeshViewer/Header/tga.h b/QtMeshViewer/Header/tga.h index f111448..3520f78 100644 --- a/QtMeshViewer/Header/tga.h +++ b/QtMeshViewer/Header/tga.h @@ -1,15 +1,13 @@ #pragma once -#include +#include "OutputDevice.h" #include #include #include #include -#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 vui8Pixels; - vui8Pixels.resize(ui32Size); - file.read(reinterpret_cast(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(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(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(); }