Skip to content

Commit

Permalink
Refactoring of default RGB formats list
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianFeldmann committed Feb 26, 2024
1 parent 38b518e commit 0084766
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 92 deletions.
7 changes: 4 additions & 3 deletions YUViewLib/src/common/Typedef.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,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", *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
105 changes: 46 additions & 59 deletions YUViewLib/src/video/rgb/videoHandlerRGB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ const auto componentShowMapper =
{ComponentDisplayMode::B, "B", "Blue Only"},
{ComponentDisplayMode::A, "A", "Alpha Only"}});

QStringList getFormattedNames(const std::vector<rgb::PixelFormatRGB> &rgbFormatList)
{
QStringList formattedNames;
for (size_t i = 0; i < rgbFormatList.size(); i++)
formattedNames.append(QString::fromStdString(rgbFormatList.at(i).getName()));
return formattedNames;
}

} // namespace

// Activate this if you want to know when which buffer is loaded/converted to image and so on.
Expand Down Expand Up @@ -84,41 +92,23 @@ 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()
{
this->append(PixelFormatRGB(8, DataLayout::Packed, ChannelOrder::RGB));
this->append(PixelFormatRGB(10, DataLayout::Packed, ChannelOrder::RGB));
this->append(PixelFormatRGB(8, DataLayout::Packed, ChannelOrder::RGB, AlphaMode::First));
this->append(PixelFormatRGB(8, DataLayout::Packed, ChannelOrder::BRG));
this->append(PixelFormatRGB(10, DataLayout::Packed, ChannelOrder::BRG));
this->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);
// 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 {};
}
// // 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::rgbPresetList = {
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 @@ -255,21 +245,20 @@ 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->addItems(getFormattedNames(this->rgbPresetList));
ui.rgbFormatComboBox->addItem("Custom...");
ui.rgbFormatComboBox->setEnabled(!isSizeFixed);
int idx = rgbPresetList.indexOf(srcPixelFormat);
if (idx == -1 && srcPixelFormat.isValid())

if (!vectorContains(videoHandlerRGB::rgbPresetList, this->srcPixelFormat) &&
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);
this->rgbPresetList.push_back(this->srcPixelFormat);
const auto nrItems = ui.rgbFormatComboBox->count();
ui.rgbFormatComboBox->insertItem(nrItems - 1,
QString::fromStdString(this->srcPixelFormat.getName()));
}
else if (idx > 0)
ui.rgbFormatComboBox->setCurrentIndex(idx);
if (const auto presetIndex = vectorIndexOf(videoHandlerRGB::rgbPresetList, this->srcPixelFormat))
ui.rgbFormatComboBox->setCurrentIndex(static_cast<int>(*presetIndex));

ui.RScaleSpinBox->setValue(componentScale[0]);
ui.RScaleSpinBox->setMaximum(1000);
Expand Down Expand Up @@ -400,7 +389,7 @@ void videoHandlerRGB::slotRGBFormatControlChanged()
auto selectionIdx = ui.rgbFormatComboBox->currentIndex();
auto nrBytesOldFormat = getBytesPerFrame();

const auto customFormatSelected = selectionIdx == this->rgbPresetList.count();
const auto customFormatSelected = (selectionIdx == this->rgbPresetList.size());
if (customFormatSelected)
{
DEBUG_RGB("videoHandlerRGB::slotRGBFormatControlChanged custom format");
Expand All @@ -409,27 +398,25 @@ void videoHandlerRGB::slotRGBFormatControlChanged()
if (dialog.exec() == QDialog::Accepted)
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::rgbPresetList, 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 = this->ui.rgbFormatComboBox->count();
this->rgbPresetList.push_back(this->srcPixelFormat);
const auto nrItems = ui.rgbFormatComboBox->count();

const QSignalBlocker blocker(this->ui.rgbFormatComboBox);
this->ui.rgbFormatComboBox->insertItem(
nrItems - 1, QString::fromStdString(this->srcPixelFormat.getName()));
idx = this->rgbPresetList.indexOf(this->srcPixelFormat);
ui.rgbFormatComboBox->insertItem(nrItems - 1,
QString::fromStdString(this->srcPixelFormat.getName()));
}

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

selectionIdx = idx;
}

this->setSrcPixelFormat(this->rgbPresetList.at(selectionIdx));
Expand Down
13 changes: 1 addition & 12 deletions YUViewLib/src/video/rgb/videoHandlerRGB.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,7 @@ 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<rgb::PixelFormatRGB> rgbPresetList;

// The currently selected RGB format
rgb::PixelFormatRGB srcPixelFormat;
Expand Down
13 changes: 5 additions & 8 deletions YUViewLib/src/video/yuv/videoHandlerYUVCustomFormatDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ videoHandlerYUVCustomFormatDialog::videoHandlerYUVCustomFormatDialog(
for (auto bitDepth : BitDepthList)
this->ui.comboBoxBitDepth->addItem(QString("%1").arg(bitDepth));
{
auto idx = indexInVec(BitDepthList, yuvFormat.getBitsPerSample());
if (idx == -1)
idx = 0;
this->ui.comboBoxBitDepth->setCurrentIndex(idx);
this->ui.comboBoxEndianness->setEnabled(idx != 0);
const auto idx = vectorIndexOf(BitDepthList, yuvFormat.getBitsPerSample());
this->ui.comboBoxBitDepth->setCurrentIndex(idx ? static_cast<int>(*idx) : 0);
this->ui.comboBoxEndianness->setEnabled(idx.has_value());
}

// Endianness
Expand All @@ -95,9 +93,8 @@ videoHandlerYUVCustomFormatDialog::videoHandlerYUVCustomFormatDialog(
// Set the packing order
this->ui.groupBoxPacked->setChecked(true);
auto supportedPackingFormats = getSupportedPackingFormats(yuvFormat.getSubsampling());
auto idx = indexInVec(supportedPackingFormats, yuvFormat.getPackingOrder());
if (idx != -1)
this->ui.comboBoxPackingOrder->setCurrentIndex(idx);
if (const auto idx = vectorIndexOf(supportedPackingFormats, yuvFormat.getPackingOrder()))
this->ui.comboBoxPackingOrder->setCurrentIndex(static_cast<int>(*idx));
this->ui.checkBoxBytePacking->setChecked(yuvFormat.isBytePacking());
}
}
Expand Down

0 comments on commit 0084766

Please sign in to comment.