From 44e36b8b0dffd05e9e6b8c586be5b88dd9a600f1 Mon Sep 17 00:00:00 2001 From: Anakin Date: Thu, 5 Jan 2017 14:46:01 +0100 Subject: [PATCH] trying to fix the 2xEmit bug, write directly mirrored to the image, --- QtMeshViewer/Header/FileInterface.h | 2 +- QtMeshViewer/Header/MainWindow.h | 2 +- QtMeshViewer/Header/tga.h | 83 +++++++++++++------------ QtMeshViewer/Source/GeometryEngine.cpp | 15 +++-- QtMeshViewer/Source/MainWindow.cpp | 2 +- QtMeshViewer/Source/OglViewerWidget.cpp | 2 +- 6 files changed, 55 insertions(+), 51 deletions(-) diff --git a/QtMeshViewer/Header/FileInterface.h b/QtMeshViewer/Header/FileInterface.h index 83cbcf0..de60f45 100644 --- a/QtMeshViewer/Header/FileInterface.h +++ b/QtMeshViewer/Header/FileInterface.h @@ -54,7 +54,7 @@ public: MainWindow* tmp = dynamic_cast(parent->parent()->parent()); if(tmp != NULL) - connect(this, SIGNAL(sendMessage(QString, int)), tmp, SLOT(showMessage(QString, int))); + connect(this, SIGNAL(sendMessage(QString, int)), tmp, SLOT(printMessage(QString, int))); }; diff --git a/QtMeshViewer/Header/MainWindow.h b/QtMeshViewer/Header/MainWindow.h index 10eb092..a2081ad 100644 --- a/QtMeshViewer/Header/MainWindow.h +++ b/QtMeshViewer/Header/MainWindow.h @@ -21,7 +21,7 @@ private slots: void aboutTool(); public slots: - void showMessage(QString message, int severity); + void printMessage(QString message, int severity); signals: void loadFile(const char*); diff --git a/QtMeshViewer/Header/tga.h b/QtMeshViewer/Header/tga.h index 957f110..7011a2d 100644 --- a/QtMeshViewer/Header/tga.h +++ b/QtMeshViewer/Header/tga.h @@ -22,19 +22,14 @@ QImage loadTga(const char* filePath, bool &success) return img; } - // some variables - std::vector* vui8Pixels; - std::uint32_t ui32BpP; - std::uint32_t ui32Width; - std::uint32_t ui32Height; - // read in the header std::uint8_t ui8x18Header[19] = { 0 }; fsPicture.read(reinterpret_cast(&ui8x18Header), sizeof(ui8x18Header) - 1); //get variables - vui8Pixels = new std::vector; - bool bCompressed; + std::uint32_t ui32BpP; + std::uint32_t ui32Width; + std::uint32_t ui32Height; std::uint32_t ui32IDLength; std::uint32_t ui32PicType; std::uint32_t ui32PaletteLength; @@ -50,22 +45,38 @@ QImage loadTga(const char* filePath, bool &success) // calculate some more information ui32Size = ui32Width * ui32Height * ui32BpP / 8; - bCompressed = ui32PicType == 9 || ui32PicType == 10; - vui8Pixels->resize(ui32Size); // jump to the data block fsPicture.seekg(ui32IDLength + ui32PaletteLength, std::ios_base::cur); + img = QImage(ui32Width, ui32Height, ui32BpP == 32? QImage::Format_RGBA8888 : QImage::Format_RGB888); + + // uncompressed if (ui32PicType == 2 && (ui32BpP == 24 || ui32BpP == 32)) { - fsPicture.read(reinterpret_cast(vui8Pixels->data()), ui32Size); + std::vector vui8Pixels; + vui8Pixels.resize(ui32Size); + fsPicture.read(reinterpret_cast(vui8Pixels.data()), ui32Size); + + for (unsigned int y = 0; y < ui32Height; y++) + { + for (unsigned int x = 0; x < ui32Width; x++) + { + int valr = vui8Pixels.at(y * ui32Width * ui32BpP / 8 + x * ui32BpP / 8 + 2); + int valg = vui8Pixels.at(y * ui32Width * ui32BpP / 8 + x * ui32BpP / 8 + 1); + int valb = vui8Pixels.at(y * ui32Width * ui32BpP / 8 + x * ui32BpP / 8); + + QColor value(valr, valg, valb); + img.setPixelColor(x, ui32Width - 1 - y, value); + } + } } // else if compressed 24 or 32 bit else if (ui32PicType == 10 && (ui32BpP == 24 || ui32BpP == 32)) // compressed { std::uint8_t tempChunkHeader; std::uint8_t tempData[5]; - unsigned int tempByteIndex = 0; + unsigned int tmp_pixelIndex = 0; do { fsPicture.read(reinterpret_cast(&tempChunkHeader), sizeof(tempChunkHeader)); @@ -79,10 +90,15 @@ QImage loadTga(const char* filePath, bool &success) for (int i = 0; i <= tempChunkHeader; i++) { - vui8Pixels->at(tempByteIndex++) = tempData[0]; - vui8Pixels->at(tempByteIndex++) = tempData[1]; - vui8Pixels->at(tempByteIndex++) = tempData[2]; - if (ui32BpP == 32) vui8Pixels->at(tempByteIndex++) = tempData[3]; + QColor color; + + if (ui32BpP == 32) + color.setRgba(qRgba(tempData[2], tempData[1], tempData[0], tempData[3])); + else + color.setRgb(qRgb(tempData[2], tempData[1], tempData[0])); + + img.setPixelColor(tmp_pixelIndex % ui32Width, ui32Height - 1 - (tmp_pixelIndex / ui32Width), color); + tmp_pixelIndex++; } } else // data count @@ -94,13 +110,18 @@ QImage loadTga(const char* filePath, bool &success) { fsPicture.read(reinterpret_cast(&tempData), ui32BpP / 8); - vui8Pixels->at(tempByteIndex++) = tempData[0]; - vui8Pixels->at(tempByteIndex++) = tempData[1]; - vui8Pixels->at(tempByteIndex++) = tempData[2]; - if (ui32BpP == 32) vui8Pixels->at(tempByteIndex++) = tempData[3]; + QColor color; + + if (ui32BpP == 32) + color.setRgba(qRgba(tempData[2], tempData[1], tempData[0], tempData[3])); + else + color.setRgb(qRgb(tempData[2], tempData[1], tempData[0])); + + img.setPixelColor(tmp_pixelIndex % ui32Width, ui32Height - 1 - (tmp_pixelIndex / ui32Width), color); + tmp_pixelIndex++; } } - } while (tempByteIndex < ui32Size); + } while (tmp_pixelIndex < (ui32Width * ui32Height)); } // not useable format else @@ -113,26 +134,6 @@ QImage loadTga(const char* filePath, bool &success) } fsPicture.close(); - - img = QImage(ui32Width, ui32Height, QImage::Format_RGB888); - - int pixelSize = ui32BpP == 32 ? 4 : 3; - //TODO: write direct into img - for (unsigned int x = 0; x < ui32Width; x++) - { - for (unsigned int y = 0; y < ui32Height; y++) - { - int valr = vui8Pixels->at(y * ui32Width * pixelSize + x * pixelSize + 2); - int valg = vui8Pixels->at(y * ui32Width * pixelSize + x * pixelSize + 1); - int valb = vui8Pixels->at(y * ui32Width * pixelSize + x * pixelSize); - - QColor value(valr, valg, valb); - img.setPixelColor(x, y, value); - } - } - - img = img.mirrored(); - } success = true; return img; diff --git a/QtMeshViewer/Source/GeometryEngine.cpp b/QtMeshViewer/Source/GeometryEngine.cpp index b30fff2..d2a8a2b 100644 --- a/QtMeshViewer/Source/GeometryEngine.cpp +++ b/QtMeshViewer/Source/GeometryEngine.cpp @@ -100,6 +100,8 @@ void GeometryEngine::loadFile(const char* filePath) for(auto& it : *textureNames) loadTexture(path.c_str(), it.c_str()); + loadTexture("", ""); + emit requestUpdate(); emit sendMessage("done..", 0); } @@ -114,12 +116,13 @@ void GeometryEngine::loadTexture(const char* filePath, const char* fileName) bool loadSuccess(false); QImage img; - //if (!strcmp(fileName, "")) - //{ - // img = QImage(1, 1, QImage::Format_RGB32); - // img.fill(Qt::red); - //} - //else + if (!strcmp(fileName, "")) + { + loadSuccess = true; + img = QImage(1, 1, QImage::Format_RGB32); + img.fill(Qt::red); + } + else img = loadTga((std::string(filePath) + std::string(fileName)).c_str(), loadSuccess); //TODO: emit if not successfull diff --git a/QtMeshViewer/Source/MainWindow.cpp b/QtMeshViewer/Source/MainWindow.cpp index cd5afa6..33c9607 100644 --- a/QtMeshViewer/Source/MainWindow.cpp +++ b/QtMeshViewer/Source/MainWindow.cpp @@ -74,7 +74,7 @@ void MainWindow::aboutTool() dialog->exec(); } -void MainWindow::showMessage(QString message, int severity) +void MainWindow::printMessage(QString message, int severity) { if (severity < m_curSeverity) return; diff --git a/QtMeshViewer/Source/OglViewerWidget.cpp b/QtMeshViewer/Source/OglViewerWidget.cpp index adc9085..64f6f5d 100644 --- a/QtMeshViewer/Source/OglViewerWidget.cpp +++ b/QtMeshViewer/Source/OglViewerWidget.cpp @@ -197,7 +197,7 @@ void OglViewerWidget::setConnections() connect(m_dataEngine, &GeometryEngine::requestResetView, this, &OglViewerWidget::resetView); connect(parentWidget(), SIGNAL(loadFile(const char*)), m_dataEngine, SLOT(loadFile(const char*))); connect(this, SIGNAL(loadFile(const char*)), m_dataEngine, SLOT(loadFile(const char*))); - connect(m_dataEngine, SIGNAL(sendMessage(QString, int)), parentWidget(), SLOT(showMessage(QString, int))); + connect(m_dataEngine, SIGNAL(sendMessage(QString, int)), parentWidget(), SLOT(printMessage(QString, int))); connect(m_dataEngine, SIGNAL(requestUpdate()), this, SLOT(update())); }