Skip to content

Commit

Permalink
Merge pull request #566 from IENT/feature/fixRGBCustomFormatSelectionBug
Browse files Browse the repository at this point in the history
Fix RGB custom format selection bug
  • Loading branch information
ChristianFeldmann committed Feb 27, 2024
2 parents 50e6941 + 5975a30 commit 62c1520
Show file tree
Hide file tree
Showing 23 changed files with 135 additions and 200 deletions.
2 changes: 1 addition & 1 deletion YUViewApp/YUViewApp.pro
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ QT += core gui widgets opengl xml concurrent network

TARGET = YUView
TEMPLATE = app
CONFIG += c++11
CONFIG += c++17
CONFIG -= debug_and_release

SOURCES += $$files(src/*.cpp, false)
Expand Down
2 changes: 1 addition & 1 deletion YUViewLib/YUViewLib.pro
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ QT += core gui widgets opengl xml concurrent network

TEMPLATE = lib
CONFIG += staticlib
CONFIG += c++1z
CONFIG += c++17
CONFIG -= debug_and_release
CONFIG += object_parallel_to_source

Expand Down
8 changes: 5 additions & 3 deletions YUViewLib/src/common/Typedef.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <cstring>
#include <iterator>
#include <map>
#include <optional>
#include <sstream>
#include <string>
#include <type_traits>
Expand Down Expand Up @@ -231,12 +232,13 @@ template <typename T, size_t N1, size_t N2> using array2d = std::array<std::arra
template <typename T, size_t N1, size_t N2, size_t N3>
using array3d = std::array<std::array<std::array<T, N3>, N2>, N1>;

template <typename T> int indexInVec(const std::vector<T> &vec, const T &item)
template <typename T>
std::optional<std::size_t> vectorIndexOf(const std::vector<T> &vec, const T &item)
{
auto it = std::find(vec.begin(), vec.end(), item);
if (it == vec.end())
return -1;
return int(std::distance(vec.begin(), it));
return {};
return static_cast<std::size_t>(std::distance(vec.begin(), it));
}

template <typename T> int vectorContains(const std::vector<T> &vec, const T &item)
Expand Down
3 changes: 2 additions & 1 deletion YUViewLib/src/playlistitem/playlistItemCompressedVideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,8 @@ void playlistItemCompressedVideo::createPropertiesWidget()
// Add decoders we can use
for (auto e : possibleDecoders)
ui.comboBoxDecoder->addItem(QString::fromStdString(DecoderEngineMapper.getName(e)));
ui.comboBoxDecoder->setCurrentIndex(indexInVec(possibleDecoders, this->decoderEngine));
if (const auto index = vectorIndexOf(possibleDecoders, this->decoderEngine))
ui.comboBoxDecoder->setCurrentIndex(static_cast<int>(index.value()));

// Connect signals/slots
this->connect(ui.comboBoxDisplaySignal,
Expand Down
5 changes: 2 additions & 3 deletions YUViewLib/src/statistics/StatisticsType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,8 @@ void StatisticsType::savePlaylist(YUViewDomElement &root) const
newChild.setAttribute("mapVectorToColor", mapVectorToColor);
if (init.arrowHead != arrowHead)
{
auto idx = indexInVec(stats::AllArrowHeads, arrowHead);
if (idx >= 0)
newChild.setAttribute("renderarrowHead", idx);
if (const auto index = vectorIndexOf(stats::AllArrowHeads, arrowHead))
newChild.setAttribute("renderarrowHead", static_cast<int>(*index));
}
if (init.renderGrid != renderGrid)
newChild.setAttribute("renderGrid", renderGrid);
Expand Down
12 changes: 6 additions & 6 deletions YUViewLib/src/ui/Statisticsstylecontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ void StatisticsStyleControl::setStatsItem(stats::StatisticsType *item)
{
this->ui.groupBoxVector->show();

auto penStyleIndex = indexInVec(stats::AllPatterns, this->currentItem->vectorStyle.pattern);
if (penStyleIndex != -1)
this->ui.comboBoxVectorLineStyle->setCurrentIndex(penStyleIndex);
if (const auto penStyleIndex =
vectorIndexOf(stats::AllPatterns, this->currentItem->vectorStyle.pattern))
this->ui.comboBoxVectorLineStyle->setCurrentIndex(static_cast<int>(*penStyleIndex));
this->ui.doubleSpinBoxVectorLineWidth->setValue(this->currentItem->vectorStyle.width);
this->ui.checkBoxVectorScaleToZoom->setChecked(this->currentItem->scaleVectorToZoom);
this->ui.comboBoxVectorHeadStyle->setCurrentIndex(int(this->currentItem->arrowHead));
Expand All @@ -130,9 +130,9 @@ void StatisticsStyleControl::setStatsItem(stats::StatisticsType *item)
this->ui.doubleSpinBoxGridLineWidth->setValue(this->currentItem->gridStyle.width);
this->ui.checkBoxGridScaleToZoom->setChecked(this->currentItem->scaleGridToZoom);

auto penStyleIndex = indexInVec(stats::AllPatterns, this->currentItem->vectorStyle.pattern);
if (penStyleIndex != -1)
this->ui.comboBoxGridLineStyle->setCurrentIndex(penStyleIndex);
if (const auto penStyleIndex =
vectorIndexOf(stats::AllPatterns, this->currentItem->vectorStyle.pattern))
this->ui.comboBoxGridLineStyle->setCurrentIndex(static_cast<int>(*penStyleIndex));

this->resize(sizeHint());
}
Expand Down
110 changes: 39 additions & 71 deletions YUViewLib/src/video/rgb/videoHandlerRGB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,41 +84,13 @@ const auto componentShowMapper =
#endif
#endif

/* The default constructor of the RGBFormatList will fill the list with all supported RGB file
* formats. Don't forget to implement actual support for all of them in the conversion functions.
*/
videoHandlerRGB::RGBFormatList::RGBFormatList()
{
append(PixelFormatRGB(8, DataLayout::Packed, ChannelOrder::RGB));
append(PixelFormatRGB(10, DataLayout::Packed, ChannelOrder::RGB));
append(PixelFormatRGB(8, DataLayout::Packed, ChannelOrder::RGB, AlphaMode::First));
append(PixelFormatRGB(8, DataLayout::Packed, ChannelOrder::BRG));
append(PixelFormatRGB(10, DataLayout::Packed, ChannelOrder::BRG));
append(PixelFormatRGB(10, DataLayout::Planar, ChannelOrder::RGB));
}

/* Put all the names of the RGB formats into a list and return it
*/
std::vector<std::string> videoHandlerRGB::RGBFormatList::getFormattedNames() const
{
std::vector<std::string> l;
for (int i = 0; i < count(); i++)
l.push_back(at(i).getName());
return l;
}

PixelFormatRGB videoHandlerRGB::RGBFormatList::getFromName(const std::string &name) const
{
for (int i = 0; i < count(); i++)
if (at(i).getName() == name)
return at(i);

// If the format could not be found, we return the "Unknown Pixel Format" format
return {};
}

// Initialize the static rgbPresetList
videoHandlerRGB::RGBFormatList videoHandlerRGB::rgbPresetList;
std::vector<rgb::PixelFormatRGB> videoHandlerRGB::formatPresetList = {
PixelFormatRGB(8, DataLayout::Packed, ChannelOrder::RGB),
PixelFormatRGB(10, DataLayout::Packed, ChannelOrder::RGB),
PixelFormatRGB(8, DataLayout::Packed, ChannelOrder::RGB, AlphaMode::First),
PixelFormatRGB(8, DataLayout::Packed, ChannelOrder::BRG),
PixelFormatRGB(10, DataLayout::Packed, ChannelOrder::BRG),
PixelFormatRGB(10, DataLayout::Planar, ChannelOrder::RGB)};

videoHandlerRGB::videoHandlerRGB() : videoHandler()
{
Expand Down Expand Up @@ -254,22 +226,22 @@ QLayout *videoHandlerRGB::createVideoHandlerControls(bool isSizeFixed)

ui.setupUi();

// Set all the values of the properties widget to the values of this class
ui.rgbFormatComboBox->addItems(functions::toQStringList(rgbPresetList.getFormattedNames()));
ui.rgbFormatComboBox->addItem("Custom...");
ui.rgbFormatComboBox->setEnabled(!isSizeFixed);
int idx = rgbPresetList.indexOf(srcPixelFormat);
if (idx == -1 && srcPixelFormat.isValid())
for (const auto &format : videoHandlerRGB::formatPresetList)
ui.rgbFormatComboBox->addItem(QString::fromStdString(format.getName()));

const auto currentFormatInPresetList =
vectorContains(videoHandlerRGB::formatPresetList, this->srcPixelFormat);
if (!currentFormatInPresetList && this->srcPixelFormat.isValid())
{
// Custom pixel format (but a known pixel format). Add and select it.
rgbPresetList.append(srcPixelFormat);
int nrItems = ui.rgbFormatComboBox->count();
ui.rgbFormatComboBox->insertItem(nrItems - 1, QString::fromStdString(srcPixelFormat.getName()));
idx = rgbPresetList.indexOf(srcPixelFormat);
ui.rgbFormatComboBox->setCurrentIndex(idx);
videoHandlerRGB::formatPresetList.push_back(this->srcPixelFormat);
ui.rgbFormatComboBox->addItem(QString::fromStdString(this->srcPixelFormat.getName()));
}
else if (idx > 0)
ui.rgbFormatComboBox->setCurrentIndex(idx);
ui.rgbFormatComboBox->addItem("Custom...");
ui.rgbFormatComboBox->setEnabled(!isSizeFixed);

if (const auto presetIndex =
vectorIndexOf(videoHandlerRGB::formatPresetList, this->srcPixelFormat))
ui.rgbFormatComboBox->setCurrentIndex(static_cast<int>(*presetIndex));

ui.RScaleSpinBox->setValue(componentScale[0]);
ui.RScaleSpinBox->setMaximum(1000);
Expand Down Expand Up @@ -395,44 +367,40 @@ void videoHandlerRGB::updateControlsForNewPixelFormat()
}
}

void videoHandlerRGB::slotRGBFormatControlChanged()
void videoHandlerRGB::slotRGBFormatControlChanged(int selectionIndex)
{
auto selectionIdx = ui.rgbFormatComboBox->currentIndex();
auto nrBytesOldFormat = getBytesPerFrame();
const auto nrBytesOldFormat = getBytesPerFrame();

if (selectionIdx == this->rgbPresetList.count())
const auto customFormatSelected = (selectionIndex == videoHandlerRGB::formatPresetList.size());
if (customFormatSelected)
{
DEBUG_RGB("videoHandlerRGB::slotRGBFormatControlChanged custom format");

// The user selected the "custom format..." option
videoHandlerRGBCustomFormatDialog dialog(this->srcPixelFormat);
if (dialog.exec() == QDialog::Accepted)
if (dialog.exec() == QDialog::Accepted && dialog.getSelectedRGBFormat().isValid())
this->srcPixelFormat = dialog.getSelectedRGBFormat();

// Check if the custom format it in the presets list. If not, add it
auto idx = this->rgbPresetList.indexOf(this->srcPixelFormat);
if (idx == -1 && this->srcPixelFormat.isValid())
const auto isInPresetList =
vectorContains(videoHandlerRGB::formatPresetList, this->srcPixelFormat);
if (!isInPresetList && this->srcPixelFormat.isValid())
{
// Valid pixel format which is not in the list. Add it...
this->rgbPresetList.append(this->srcPixelFormat);
int nrItems = ui.rgbFormatComboBox->count();
const QSignalBlocker blocker(ui.rgbFormatComboBox);
ui.rgbFormatComboBox->insertItem(nrItems - 1,
videoHandlerRGB::formatPresetList.push_back(this->srcPixelFormat);
const QSignalBlocker blocker(this->ui.rgbFormatComboBox);
const auto insertPositionBeforeCustom = (this->ui.rgbFormatComboBox->count() - 1);
ui.rgbFormatComboBox->insertItem(insertPositionBeforeCustom,
QString::fromStdString(this->srcPixelFormat.getName()));
idx = this->rgbPresetList.indexOf(this->srcPixelFormat);
}

if (idx > 0)
if (const auto presetIndex =
vectorIndexOf(videoHandlerRGB::formatPresetList, this->srcPixelFormat))
{
// Format found. Set it without another call to this function.
const QSignalBlocker blocker(ui.rgbFormatComboBox);
ui.rgbFormatComboBox->setCurrentIndex(idx);
const QSignalBlocker blocker(this->ui.rgbFormatComboBox);
selectionIndex = static_cast<int>(*presetIndex);
ui.rgbFormatComboBox->setCurrentIndex(selectionIndex);
}

selectionIdx = idx;
}

this->setSrcPixelFormat(rgbPresetList.at(selectionIdx));
this->setSrcPixelFormat(videoHandlerRGB::formatPresetList.at(selectionIndex));

// Set the current frame in the buffer to be invalid and clear the cache.
// Emit that this item needs redraw and the cache needs updating.
Expand Down
19 changes: 3 additions & 16 deletions YUViewLib/src/video/rgb/videoHandlerRGB.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,10 @@ class videoHandlerRGB : public videoHandler
protected:
ComponentDisplayMode componentDisplayMode{ComponentDisplayMode::RGBA};

// A (static) convenience QList class that handles the preset PixelFormatRGBs
class RGBFormatList : public QList<rgb::PixelFormatRGB>
{
public:
// Default constructor. Fill the list with all the supported YUV formats.
RGBFormatList();
// Get all the YUV formats as a formatted list (for the drop-down control)
std::vector<std::string> getFormattedNames() const;
// Get the PixelFormatYUV with the given name
rgb::PixelFormatRGB getFromName(const std::string &name) const;
};
static RGBFormatList rgbPresetList;
static std::vector<PixelFormatRGB> formatPresetList;

// The currently selected RGB format
rgb::PixelFormatRGB srcPixelFormat;
PixelFormatRGB srcPixelFormat;

// Parameters for the RGBA transformation (like scaling, invert)
int componentScale[4]{1, 1, 1, 1};
Expand Down Expand Up @@ -215,9 +204,7 @@ class videoHandlerRGB : public videoHandler

private slots:

// One of the controls for the RGB format changed.
void slotRGBFormatControlChanged();
// One of the controls for the RGB display settings changed.
void slotRGBFormatControlChanged(int selectionIndex);
void slotDisplayOptionsChanged();
};

Expand Down
Loading

0 comments on commit 62c1520

Please sign in to comment.