Skip to content

Commit

Permalink
App: fix support of TGA texture files in "Inspect XDE" tool
Browse files Browse the repository at this point in the history
Relates to GitHub #272
  • Loading branch information
HuguesDelorme committed Apr 18, 2024
1 parent ffc8cb0 commit 185694d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
52 changes: 40 additions & 12 deletions src/app/dialog_inspect_xde.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "qtwidgets_utils.h"
#include "ui_dialog_inspect_xde.h"

#include <Image_AlienPixMap.hxx>
#include <TDF_AttributeIterator.hxx>
#include <TDF_ChildIterator.hxx>
#include <TDF_LabelSequence.hxx>
Expand Down Expand Up @@ -295,6 +296,42 @@ static void loadLabelMaterialProperties(

#if OCC_VERSION_HEX >= OCC_VERSION_CHECK(7, 4, 0)

// Load pixmap from file
static QPixmap loadPixmap(const FilePath& filePath)
{
QPixmap pixmap;
bool okLoad = pixmap.load(filepathTo<QString>(filePath));
if (!okLoad || pixmap.isNull()) {
// QPixmap::load() failed, try with OpenCascade Image_AlienPixMap::Load()
Image_AlienPixMap occPixmap;
okLoad = occPixmap.Load(filepathTo<TCollection_AsciiString>(filePath));
if (okLoad)
pixmap = QtGuiUtils::toQPixmap(occPixmap);
}

return pixmap;
}

// Load pixmap from data buffer
static QPixmap loadPixmap(const QByteArray& fileData)
{
QPixmap pixmap;
bool okLoad = pixmap.loadFromData(fileData);
if (!okLoad || pixmap.isNull()) {
// QPixmap::loadFromData() failed, try with OpenCascade Image_AlienPixMap::Load()
Image_AlienPixMap occPixmap;
okLoad = occPixmap.Load(
reinterpret_cast<const Standard_Byte*>(fileData.constData()),
fileData.size(),
TCollection_AsciiString{}
);
if (okLoad)
pixmap = QtGuiUtils::toQPixmap(occPixmap);
}

return pixmap;
}

// Provides a QTreeWidgetItem specialized to display an image file with a tooltip
// QTreeWidgetItem::setToolTip() could be used but it forces all image files to be loaded on
// tree item construction
Expand Down Expand Up @@ -324,18 +361,9 @@ class ImageFileTreeWidgetItem : public QTreeWidgetItem {
return {};

if (ptrItem->strToolTip.isEmpty()) {
QPixmap pixmap;
uintmax_t imageSize = 0;

if (!ptrItem->filePath.empty()) {
pixmap.load(filepathTo<QString>(ptrItem->filePath));
imageSize = filepathFileSize(ptrItem->filePath);
}
else {
pixmap.loadFromData(ptrItem->fileData);
imageSize = ptrItem->fileData.size();
}

const bool isFilePathDefined = !ptrItem->filePath.empty();
const QPixmap pixmap = isFilePathDefined ? loadPixmap(ptrItem->filePath) : loadPixmap(ptrItem->fileData);
const uintmax_t imageSize = isFilePathDefined ? filepathFileSize(ptrItem->filePath) : ptrItem->fileData.size();
if (!pixmap.isNull()) {
QBuffer bufferPixmap;
const int pixmapWidth = std::min(pixmap.width(), int(400 * qGuiApp->devicePixelRatio()));
Expand Down
1 change: 1 addition & 0 deletions src/app/qtgui_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ QPixmap toQPixmap(const Image_PixMap& pixmap)
auto fnToQImageFormat = [](Image_Format occFormat) {
switch (occFormat) {
case Image_Format_RGB: return QImage::Format_RGB888;
case Image_Format_BGR: return QImage::Format_BGR888;
case Image_Format_RGBA: return QImage::Format_ARGB32;
case Image_Format_RGBF: return QImage::Format_RGB444;
case Image_Format_Gray: return QImage::Format_Grayscale8;
Expand Down

0 comments on commit 185694d

Please sign in to comment.