diff --git a/.clang-format b/.clang-format index a95c3b3..792a015 100644 --- a/.clang-format +++ b/.clang-format @@ -1,19 +1,19 @@ --- -BasedOnStyle: Google -Language: Cpp -ColumnLimit: 120 +BasedOnStyle: Google +Language: Cpp +ColumnLimit: 120 AccessModifierOffset: -2 AlignAfterOpenBracket: Align AlignConsecutiveAssignments: false AlignConsecutiveDeclarations: false AlignEscapedNewlinesLeft: true -AlignOperands: true +AlignOperands: true AlignTrailingComments: true AllowAllParametersOfDeclarationOnNextLine: false -AllowShortBlocksOnASingleLine: true +AllowShortBlocksOnASingleLine: false AllowShortCaseLabelsOnASingleLine: false AllowShortFunctionsOnASingleLine: Inline -AllowShortIfStatementsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: true AllowShortLoopsOnASingleLine: false AlwaysBreakAfterReturnType: None AlwaysBreakBeforeMultilineStrings: false @@ -21,17 +21,17 @@ AlwaysBreakTemplateDeclarations: true BinPackArguments: true BinPackParameters: true BraceWrapping: - AfterNamespace: true - AfterClass: true - AfterEnum: true - AfterFunction: true - AfterStruct: true - AfterUnion: true - AfterControlStatement: true - AfterObjCDeclaration: true - BeforeCatch: true - BeforeElse: true - IndentBraces: false + AfterNamespace: true + AfterClass: true + AfterEnum: true + AfterFunction: true + AfterStruct: true + AfterUnion: true + AfterControlStatement: true + AfterObjCDeclaration: true + BeforeCatch: true + BeforeElse: true + IndentBraces: false BreakBeforeBraces: Custom BreakBeforeBinaryOperators: None BreakBeforeInheritanceComma: false @@ -40,31 +40,31 @@ BreakConstructorInitializersBeforeComma: true BreakConstructorInitializers: BeforeColon BreakAfterJavaFieldAnnotations: false BreakStringLiterals: true -CommentPragmas: '^ IWYU pragma:' +CommentPragmas: "^ IWYU pragma:" ConstructorInitializerAllOnOneLineOrOnePerLine: true ConstructorInitializerIndentWidth: 4 ContinuationIndentWidth: 4 Cpp11BracedListStyle: true DerivePointerAlignment: false -DisableFormat: false +DisableFormat: false ExperimentalAutoDetectBinPacking: false -ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] +ForEachMacros: [foreach, Q_FOREACH, BOOST_FOREACH] IncludeCategories: - - Regex: '^<.*\.h>' - Priority: 1 - - Regex: '^<.*' - Priority: 2 - - Regex: '.*' - Priority: 3 -IncludeIsMainRegex: '([-_](test|unittest))?$' + - Regex: '^<.*\.h>' + Priority: 1 + - Regex: "^<.*" + Priority: 2 + - Regex: ".*" + Priority: 3 +IncludeIsMainRegex: "([-_](test|unittest))?$" IndentCaseLabels: false -IndentWidth: 2 +IndentWidth: 2 IndentWrappedFunctionNames: false JavaScriptQuotes: Leave JavaScriptWrapImports: true KeepEmptyLinesAtTheStartOfBlocks: false -MacroBlockBegin: '' -MacroBlockEnd: '' +MacroBlockBegin: "" +MacroBlockEnd: "" MaxEmptyLinesToKeep: 1 NamespaceIndentation: None ObjCBlockIndentWidth: 2 @@ -77,21 +77,20 @@ PenaltyBreakString: 1000 PenaltyExcessCharacter: 1000000 PenaltyReturnTypeOnItsOwnLine: 200 PointerAlignment: Left -ReflowComments: true -SortIncludes: false +ReflowComments: true SpaceAfterCStyleCast: false SpaceBeforeAssignmentOperators: true SpaceBeforeParens: false SpaceInEmptyParentheses: false SpacesBeforeTrailingComments: 2 -SpacesInAngles: false +SpacesInAngles: false SpacesInContainerLiterals: true SpacesInCStyleCastParentheses: false -SortIncludes: true +SortIncludes: true SpacesInParentheses: true SpacesInSquareBrackets: false -Standard: Auto -TabWidth: 2 -UseTab: Never -... +Standard: Auto +TabWidth: 2 +UseTab: Never +--- diff --git a/.clang-tidy b/.clang-tidy index 79b09ae..85b8edb 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,12 +1,23 @@ --- -Checks: "clang-diagnostic-*,clang-analyzer-*,cppcoreguidelines-*,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-non-private-member-variables-in-classes, -cppcoreguidelines-owning-memory, -cppcoreguidelines-pro-bounds-constant-array-index, -readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers" +Checks: "*,-modernize-use-nodiscard,-llvmlibc-*,-fuchsia-trailing-return,-fuchsia*,-altera-*,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-implicit-widening-of-multiplication-result,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers, -readability-identifier-length" WarningsAsErrors: "" HeaderFilterRegex: "" -AnalyzeTemporaryDtors: false -FormatStyle: none -Diagnostics: - UnusedIncludes: Strict +FormatStyle: file CheckOptions: + - key: readability-identifier-naming.ClassCase + value: CamelCase + - key: readability-identifier-naming.ClassMemberCase + value: camelBack + - key: readability-identifier-naming.ClassMethodCase + value: camelBack + - key: readability-identifier-naming.ParameterCase + value: camelBack + - key: readability-identifier-naming.PrivateMemberCase + value: camelBack + - key: readability-identifier-naming.PrivateMemberPrefix + value: m_ + - key: readability-identifier-length.IgnoredVariableNames + value: "^(id|to)$" - key: llvm-else-after-return.WarnOnConditionVariables value: "0" - key: modernize-loop-convert.MinConfidence @@ -45,3 +56,11 @@ CheckOptions: value: "800" - key: readability-magic-numbers.IgnorePowersOf2IntegerValues value: "1" + - key: readability-braces-around-statements.ShortStatementLines + value: "1" + - key: google-readability-braces-around-statements.ShortStatementLines + value: "1" + - key: hicpp-braces-around-statements.ShortStatementLines + value: "1" + - key: misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic + value: "1" diff --git a/app/MainWindow.cpp b/app/MainWindow.cpp index 7633366..07aeb05 100644 --- a/app/MainWindow.cpp +++ b/app/MainWindow.cpp @@ -133,8 +133,7 @@ bool MainWindow::parseArgs( int argc, char* argv[] ) void MainWindow::about() { - if( !m_pcAboutDialog ) - m_pcAboutDialog = new AboutDialog( this ); + if( !m_pcAboutDialog ) m_pcAboutDialog = new AboutDialog( this ); m_pcAboutDialog->exec(); } @@ -209,8 +208,7 @@ void MainWindow::loadFile( QString fileName, std::optional stream if( !streamInfo.has_value() ) { auto idx = findCalypStreamInfo( m_aRecentFileStreamInfo, fileName ); - if( idx >= 0 ) - streamInfo = m_aRecentFileStreamInfo.at( idx ); + if( idx >= 0 ) streamInfo = m_aRecentFileStreamInfo.at( idx ); } try @@ -228,8 +226,7 @@ void MainWindow::loadFile( QString fileName, std::optional stream } } - if( !opened ) - opened = videoSubWindow->loadFile( fileName ); + if( !opened ) opened = videoSubWindow->loadFile( fileName ); if( opened ) { @@ -306,8 +303,7 @@ QStringList MainWindow::showFileDialog( bool bRead ) else { QString fileName = QFileDialog::getSaveFileName( this, tr( "Save File" ), m_cLastOpenPath, filter.join( ";;" ) ); - if( !fileName.isEmpty() ) - fileNameList.append( fileName ); + if( !fileName.isEmpty() ) fileNameList.append( fileName ); } return fileNameList; } @@ -488,8 +484,7 @@ void MainWindow::zoomToFitAll() videoSubWindow = qobject_cast( subWindowList.at( i ) ); videoSubWindow->zoomToFit(); } - if( m_pcCurrentSubWindow ) - updateZoomFactorSBox(); + if( m_pcCurrentSubWindow ) updateZoomFactorSBox(); } void MainWindow::scaleFrame( int ratio ) @@ -552,8 +547,7 @@ VideoStreamSubWindow* MainWindow::findVideoStreamSubWindow( const SubWindowHandl for( int i = 0; i < subWindowList.size(); i++ ) { auto* pcSubWindow = qobject_cast( subWindowList.at( i ) ); - if( pcSubWindow->getCurrentFileName() == canonicalFilePath ) - return pcSubWindow; + if( pcSubWindow->getCurrentFileName() == canonicalFilePath ) return pcSubWindow; } return nullptr; } @@ -724,22 +718,17 @@ void MainWindow::createActions() connect( m_arrayActions[EXIT_ACT], SIGNAL( triggered() ), qApp, SLOT( closeAllWindows() ) ); // ------------ View ------------ - mapperZoom = new QSignalMapper( this ); - connect( mapperZoom, SIGNAL( mapped( int ) ), this, SLOT( scaleFrame( int ) ) ); - m_arrayActions[ZOOM_IN_ACT] = new QAction( tr( "Zoom &In (+25%)" ), this ); m_arrayActions[ZOOM_IN_ACT]->setIcon( QIcon::fromTheme( "zoom-in" ) ); m_arrayActions[ZOOM_IN_ACT]->setShortcut( tr( "Ctrl++" ) ); m_arrayActions[ZOOM_IN_ACT]->setStatusTip( tr( "Scale the image up by 25%" ) ); - connect( m_arrayActions[ZOOM_IN_ACT], SIGNAL( triggered() ), mapperZoom, SLOT( map() ) ); - mapperZoom->setMapping( m_arrayActions[ZOOM_IN_ACT], 125 ); + connect( m_arrayActions[ZOOM_IN_ACT], &QAction::triggered, [this] { scaleFrame( 125 ); } ); m_arrayActions[ZOOM_OUT_ACT] = new QAction( tr( "Zoom &Out (-25%)" ), this ); m_arrayActions[ZOOM_OUT_ACT]->setIcon( QIcon::fromTheme( "zoom-out" ) ); m_arrayActions[ZOOM_OUT_ACT]->setShortcut( tr( "Ctrl+-" ) ); m_arrayActions[ZOOM_OUT_ACT]->setStatusTip( tr( "Scale the image down by 25%" ) ); - connect( m_arrayActions[ZOOM_OUT_ACT], SIGNAL( triggered() ), mapperZoom, SLOT( map() ) ); - mapperZoom->setMapping( m_arrayActions[ZOOM_OUT_ACT], 80 ); + connect( m_arrayActions[ZOOM_OUT_ACT], &QAction::triggered, [this] { scaleFrame( 80 ); } ); m_arrayActions[ZOOM_NORMAL_ACT] = new QAction( tr( "&Normal Size" ), this ); m_arrayActions[ZOOM_NORMAL_ACT]->setIcon( QIcon::fromTheme( "zoom-original" ) ); @@ -845,19 +834,17 @@ void MainWindow::createMenus() void MainWindow::createToolBars() { - m_arrayToolBars.resize( TOTAL_TOOLBAR ); + auto* fileToolbar = addToolBar( tr( "File" ) ); + fileToolbar->addAction( m_arrayActions[OPEN_ACT] ); + fileToolbar->addAction( m_arrayActions[SAVE_ACT] ); + fileToolbar->addAction( m_arrayActions[FORMAT_ACT] ); + fileToolbar->addAction( m_arrayActions[RELOAD_ACT] ); + fileToolbar->addAction( m_arrayActions[CLOSE_ACT] ); + fileToolbar->setMovable( false ); + addToolBar( Qt::TopToolBarArea, fileToolbar ); - m_arrayToolBars[FILE_TOOLBAR] = new QToolBar( tr( "File" ) ); - m_arrayToolBars[FILE_TOOLBAR]->addAction( m_arrayActions[OPEN_ACT] ); - m_arrayToolBars[FILE_TOOLBAR]->addAction( m_arrayActions[SAVE_ACT] ); - m_arrayToolBars[FILE_TOOLBAR]->addAction( m_arrayActions[FORMAT_ACT] ); - m_arrayToolBars[FILE_TOOLBAR]->addAction( m_arrayActions[RELOAD_ACT] ); - m_arrayToolBars[FILE_TOOLBAR]->addAction( m_arrayActions[CLOSE_ACT] ); + auto* viewToolBar = addToolBar( tr( "View" ) ); - m_arrayToolBars[FILE_TOOLBAR]->setMovable( false ); - addToolBar( Qt::TopToolBarArea, m_arrayToolBars[FILE_TOOLBAR] ); - - m_arrayToolBars[VIEW_TOOLBAR] = new QToolBar( tr( "View" ) ); m_pcZoomFactorSBox = new QDoubleSpinBox; m_pcZoomFactorSBox->setRange( 1.0, 10000.0 ); m_pcZoomFactorSBox->setDecimals( 0 ); @@ -865,14 +852,15 @@ void MainWindow::createToolBars() m_pcZoomFactorSBox->setValue( 100.0 ); m_pcZoomFactorSBox->setSuffix( "%" ); connect( m_pcZoomFactorSBox, SIGNAL( valueChanged( double ) ), this, SLOT( zoomFromSBox( double ) ) ); - m_arrayToolBars[VIEW_TOOLBAR]->addWidget( m_pcZoomFactorSBox ); - m_arrayToolBars[VIEW_TOOLBAR]->addAction( m_arrayActions[ZOOM_IN_ACT] ); - m_arrayToolBars[VIEW_TOOLBAR]->addAction( m_arrayActions[ZOOM_OUT_ACT] ); - m_arrayToolBars[VIEW_TOOLBAR]->addAction( m_arrayActions[ZOOM_NORMAL_ACT] ); - m_arrayToolBars[VIEW_TOOLBAR]->addAction( m_arrayActions[ZOOM_FIT_ACT] ); - m_arrayToolBars[FILE_TOOLBAR]->setMovable( false ); - addToolBar( Qt::TopToolBarArea, m_arrayToolBars[VIEW_TOOLBAR] ); + viewToolBar->addWidget( m_pcZoomFactorSBox ); + viewToolBar->addAction( m_arrayActions[ZOOM_IN_ACT] ); + viewToolBar->addAction( m_arrayActions[ZOOM_OUT_ACT] ); + viewToolBar->addAction( m_arrayActions[ZOOM_NORMAL_ACT] ); + viewToolBar->addAction( m_arrayActions[ZOOM_FIT_ACT] ); + + viewToolBar->setMovable( false ); + addToolBar( Qt::TopToolBarArea, viewToolBar ); addToolBar( Qt::TopToolBarArea, m_appModuleVideo->createToolBar() ); } @@ -895,8 +883,7 @@ void MainWindow::createStatusBar() void MainWindow::addStreamInfoToRecentList( CalypFileInfo streamInfo ) { int idx = findCalypStreamInfo( m_aRecentFileStreamInfo, streamInfo.m_cFilename ); - if( idx >= 0 ) - m_aRecentFileStreamInfo.remove( idx ); + if( idx >= 0 ) m_aRecentFileStreamInfo.remove( idx ); m_aRecentFileStreamInfo.prepend( streamInfo ); while( m_aRecentFileStreamInfo.size() > MAX_RECENT_FILES ) m_aRecentFileStreamInfo.remove( m_aRecentFileStreamInfo.size() - 1 ); diff --git a/app/MainWindow.h b/app/MainWindow.h index 13926e2..b7d7e3e 100644 --- a/app/MainWindow.h +++ b/app/MainWindow.h @@ -154,7 +154,6 @@ private: QDoubleSpinBox* m_pcZoomFactorSBox; - QSignalMapper* mapperZoom; QSignalMapper* mapperWindow; /** @@ -199,18 +198,6 @@ private: }; QVector m_arraySideBars; - /** - * Array of tool bars for the main app - */ - enum TOOLBAR_LIST - { - FILE_TOOLBAR = 0, - VIEW_TOOLBAR, - VIDEO_TOOLBAR, - TOTAL_TOOLBAR, - }; - QVector m_arrayToolBars; - /** * Array of actions for the main app */ diff --git a/app/VideoHandle.cpp b/app/VideoHandle.cpp index b64ee32..5712fe3 100644 --- a/app/VideoHandle.cpp +++ b/app/VideoHandle.cpp @@ -226,14 +226,14 @@ QMenu* VideoHandle::createImageMenu() return m_pcMenuImage; } -QToolBar* VideoHandle::createToolBar() +auto VideoHandle::createToolBar() -> QPointer { - m_toolbarVideo = new QToolBar( tr( "Video" ) ); + QPointer m_toolbarVideo = new QToolBar( tr( "Video" ) ); m_toolbarVideo->setAllowedAreas( Qt::TopToolBarArea | Qt::BottomToolBarArea ); m_toolbarVideo->addAction( m_arrayActions[PLAY_ACT] ); m_toolbarVideo->addAction( m_arrayActions[STOP_ACT] ); - QLabel* pcLockLabel = new QLabel( "PlayingLock" ); + QPointer pcLockLabel = new QLabel( "PlayingLock" ); pcLockLabel->setAlignment( Qt::AlignCenter ); m_arrayActions[VIDEO_LOCK_ACT] = m_toolbarVideo->addWidget( pcLockLabel ); m_arrayActions[VIDEO_LOCK_ACT]->setVisible( false ); @@ -242,6 +242,7 @@ QToolBar* VideoHandle::createToolBar() m_toolbarVideo->addWidget( m_pcFrameSlider ); m_toolbarVideo->addAction( m_arrayActions[VIDEO_FORWARD_ACT] ); m_toolbarVideo->addWidget( new QLabel ); + m_pcFrameNumInfo = new FrameNumberWidget; m_toolbarVideo->addWidget( m_pcFrameNumInfo ); @@ -383,8 +384,7 @@ void VideoHandle::readSettings() QSettings appSettings; m_arrayActions[VIDEO_REPEAT_ACT]->setChecked( appSettings.value( "VideoHandle/Repeat", false ).toBool() ); m_arrayActions[VIDEO_ZOOM_LOCK_ACT]->setChecked( appSettings.value( "VideoHandle/VideoZoomLock", false ).toBool() ); - if( !appSettings.value( "VideoHandle/FrameProperties", true ).toBool() ) - m_pcFramePropertiesDock->close(); + if( !appSettings.value( "VideoHandle/FrameProperties", true ).toBool() ) m_pcFramePropertiesDock->close(); m_uiViewTool = appSettings.value( "VideoHandle/SelectedTool", ViewArea::NavigationView ).toUInt(); setTool( m_uiViewTool ); @@ -630,8 +630,7 @@ unsigned long VideoHandle::getMaxFrameNumber() for( int i = 0; i < m_acPlayingSubWindows.size(); i++ ) { currFrames = m_acPlayingSubWindows.at( i )->getFrameNum(); - if( currFrames < maxFrames ) - maxFrames = currFrames; + if( currFrames < maxFrames ) maxFrames = currFrames; } } else @@ -682,8 +681,7 @@ void VideoHandle::play() { auto* videoStreamSubWindow = qobject_cast( m_pcCurrentVideoSubWindow ); - if( videoStreamSubWindow == nullptr ) - return; + if( videoStreamSubWindow == nullptr ) return; if( !videoStreamSubWindow->isPlaying() ) // Not playing { @@ -802,8 +800,7 @@ void VideoHandle::playEvent() stop(); m_pcCurrentVideoSubWindow->close(); } - if( bShouldRefreshUI ) - emit changed(); + if( bShouldRefreshUI ) emit changed(); // auto end = std::chrono::steady_clock::now(); // std::cout << "Elapsed time going to next frame: " @@ -861,8 +858,7 @@ void VideoHandle::seekVideo() void VideoHandle::seekSliderEvent( int new_frame_num ) { // TODO: Fix this slot - if( m_bIsPlaying ) - return; + if( m_bIsPlaying ) return; if( auto* videoStreamSubWindow = qobject_cast( m_pcCurrentVideoSubWindow ) ) { @@ -943,8 +939,7 @@ void VideoHandle::videoSelectionButtonEvent() void VideoHandle::setComponent( int component ) { - if( m_pcCurrentVideoSubWindow == nullptr ) - return; + if( m_pcCurrentVideoSubWindow == nullptr ) return; if( component < 0 ) { m_pcCurrentVideoSubWindow->getViewArea()->clearFilteredChannel(); diff --git a/app/VideoHandle.h b/app/VideoHandle.h index 92bd824..3edd87c 100644 --- a/app/VideoHandle.h +++ b/app/VideoHandle.h @@ -59,7 +59,7 @@ public: void createActions(); QMenu* createVideoMenu(); QMenu* createImageMenu(); - QToolBar* createToolBar(); + QPointer createToolBar(); QDockWidget* createDock(); QWidget* createStatusBarMessage(); void updateMenus(); @@ -113,7 +113,7 @@ private: QMenu* m_pcMenuVideo; QMenu* m_pcMenuImage; - QToolBar* m_toolbarVideo; + // QToolBar* m_toolbarVideo; QDockWidget* m_pcFramePropertiesDock; FramePropertiesDock* m_pcFramePropertiesSideBar; diff --git a/app/main.cpp b/app/main.cpp index 8dd03cb..569259e 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -51,6 +51,7 @@ int main( int argc, char* argv[] ) qRegisterMetaTypeStreamOperators(); qRegisterMetaTypeStreamOperators(); #endif + QApplication::setStyle( "macOS" ); QApplication application( argc, argv ); QApplication::setApplicationName( QStringLiteral( "Calyp" ) ); QApplication::setApplicationVersion( QStringLiteral( CALYP_VERSION_STRING ) ); diff --git a/app/widgets/FrameNumberWidget.h b/app/widgets/FrameNumberWidget.h index cca7ae1..dd58e51 100644 --- a/app/widgets/FrameNumberWidget.h +++ b/app/widgets/FrameNumberWidget.h @@ -35,7 +35,7 @@ class QLabel; class FrameNumberWidget : public QWidget { public: - FrameNumberWidget( QWidget* parent = 0 ); + explicit FrameNumberWidget( QWidget* parent = 0 ); void setCurrFrameNum( int number ); void setTotalFrameNum( int number ); void clear(); diff --git a/lib/CalypDefs.h b/lib/CalypDefs.h index b816545..f3a3ffc 100644 --- a/lib/CalypDefs.h +++ b/lib/CalypDefs.h @@ -27,6 +27,7 @@ #define __CALYPDEFS_H__ #include +#include #include #include #include @@ -58,20 +59,16 @@ inline std::string clpUppercase( const std::string& in ) struct CalypFailure : public std::exception { - std::string m_class_name; - std::string m_error_msg; - CalypFailure( std::string error_msg ) throw() - : m_error_msg( error_msg ) {} + CalypFailure( std::string error_msg ) throw() : m_error_msg{ std::move( error_msg ) } {} CalypFailure( std::string class_name, std::string error_msg ) throw() - : m_class_name( class_name ), m_error_msg( error_msg ) + : m_error_msg{ std::format( "[%s] %s", class_name, error_msg ) } { } ~CalypFailure() throw() {} - const char* what() const throw() - { - std::string* msg = new std::string( "[" + m_class_name + "] " + m_error_msg ); - return msg->c_str(); - } + auto what() const throw() -> const char* { return m_error_msg.c_str(); } + +private: + std::string m_error_msg; }; #endif // __CALYPDEFS_H__ diff --git a/lib/CalypFrame.cpp b/lib/CalypFrame.cpp index d415702..a22aacb 100644 --- a/lib/CalypFrame.cpp +++ b/lib/CalypFrame.cpp @@ -31,9 +31,11 @@ #include #include #include +#include #include #include "CalypDefs.h" +#include "PixelFormats.h" #include "config.h" #ifdef USE_OPENCV @@ -42,9 +44,6 @@ #include #endif -#include "PixelFormats.h" -#include "config.h" - #define DATA_ALIGN 1 ///< use 32-bit aligned malloc/free #if DATA_ALIGN && _WIN32 && ( _MSC_VER > 1300 ) #define xMalloc( len ) _aligned_malloc( len, 32 ) @@ -75,24 +74,15 @@ constexpr auto CalypFrame::numberOfFormats() -> std::size_t auto CalypFrame::findPixelFormat( const std::string& name ) -> std::optional { - for( const auto& [key, fmt] : g_CalypPixFmtDescriptorsMap ) - { - if( fmt.name.size() == name.size() && - std::equal( fmt.name.begin(), fmt.name.end(), name.begin(), - []( auto a, auto b ) { return std::tolower( a ) == std::tolower( b ); } ) ) - return key; - } - return {}; + return findPixelFormat( std::string_view{ name } ); } -auto CalypFrame::findPixelFormat( const std::string_view name ) -> std::optional +auto CalypFrame::findPixelFormat( std::string_view name ) -> std::optional { + auto ichar_equals = []( char lhs, char rhs ) { return std::tolower( lhs ) == std::tolower( rhs ); }; for( const auto& [key, fmt] : g_CalypPixFmtDescriptorsMap ) { - if( fmt.name.size() == name.size() && - std::equal( fmt.name.begin(), fmt.name.end(), name.begin(), - []( auto a, auto b ) { return std::tolower( a ) == std::tolower( b ); } ) ) - return key; + if( std::ranges::equal( fmt.name, name, ichar_equals ) ) return key; } return {}; } @@ -125,14 +115,13 @@ auto CalypFrame::supportedPixelFormatListNames( int colorSpace ) -> std::map const std::string_view +auto CalypFrame::pixelFormatName( ClpPixelFormats idx ) -> std::string_view { return g_CalypPixFmtDescriptorsMap.at( idx ).name; } -class CalypFrame::CalypFramePrivate +struct CalypFrame::CalypFramePrivate { -public: bool m_bInit{ false }; //! Struct with the pixel format description. @@ -149,6 +138,7 @@ public: ClpPel*** m_pppcInputPel{ nullptr }; bool m_bHasRGBPel{ false }; //!< Flag indicating that the ARGB buffer was computed + bool bHasRGBPel{ false }; //!< Flag indicating that the ARGB buffer was computed std::vector m_pcARGB32; //!< Buffer with the ARGB pixels used in Qt libs /** Histogram control variables **/ @@ -173,14 +163,14 @@ public: } void init( unsigned int width, unsigned int height, ClpPixelFormats pelFormat, unsigned bitsPixel, - bool has_negative_values ) + bool hasNegativeValues ) { m_uiWidth = width; m_uiHeight = height; m_iPixelFormat = pelFormat; m_uiBitsPel = bitsPixel < kMinBitsPerPixel ? kMinBitsPerPixel : bitsPixel; - m_uiHalfPelValue = 1 << ( m_uiBitsPel - 1 ); - m_bHasNegativeValues = has_negative_values; + m_uiHalfPelValue = 1UL << ( m_uiBitsPel - 1 ); + m_bHasNegativeValues = hasNegativeValues; if( m_uiWidth == 0 || m_uiHeight == 0 || m_iPixelFormat == ClpPixelFormats::Invalid || bitsPixel > kMaxBitsPerPixel ) @@ -227,19 +217,23 @@ public: m_bHasHistogram = false; m_bHistogramRunning = false; - m_uiHistoSegments = 1 << m_uiBitsPel; + m_uiHistoSegments = 1UL << m_uiBitsPel; if( m_pcPelFormat->colorSpace == CLP_COLOR_RGB || m_pcPelFormat->colorSpace == CLP_COLOR_RGBA ) + { m_uiHistoChannels = m_pcPelFormat->numberChannels + 1; + } else + { m_uiHistoChannels = m_pcPelFormat->numberChannels; + } m_puiHistogram.resize( m_uiHistoSegments * m_uiHistoChannels ); m_bInit = true; } - int getRealHistogramChannel( int channel ) + auto getRealHistogramChannel( int channel ) const -> int { if( channel < 0 ) { @@ -280,11 +274,12 @@ public: ~CalypFramePrivate() { while( m_bHistogramRunning ) - ; + { + std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) ); + } m_bHasHistogram = false; - if( m_pppcInputPel ) - xFreeMem( m_pppcInputPel ); // NOLINT + if( m_pppcInputPel ) xFreeMem( m_pppcInputPel ); // NOLINT } }; @@ -292,49 +287,34 @@ public: * \brief Constructors */ CalypFrame::CalypFrame( unsigned int width, unsigned int height, ClpPixelFormats pelFormat, unsigned bitsPixel ) - : d{ std::make_unique() } + : m_d{ std::make_unique() } { - d->init( width, height, pelFormat, bitsPixel ); + m_d->init( width, height, pelFormat, bitsPixel ); } CalypFrame::CalypFrame( unsigned int width, unsigned int height, ClpPixelFormats pelFormat, unsigned bitsPixel, - bool has_negative_values ) - : d{ std::make_unique() } + bool hasNegativeValues ) + : m_d{ std::make_unique() } { - d->init( width, height, pelFormat, bitsPixel, has_negative_values ); + m_d->init( width, height, pelFormat, bitsPixel, hasNegativeValues ); } -CalypFrame::CalypFrame( CalypFrame&& other ) noexcept : d{ std::move( other.d ) } {} - -CalypFrame& CalypFrame::operator=( CalypFrame&& other ) noexcept +CalypFrame::CalypFrame( const CalypFrame& other ) : m_d{ std::make_unique() } { - // Self-assignment detection - if( &other == this ) - return *this; - - // Transfer ownership of a.m_ptr to m_ptr - d = std::move( other.d ); - - return *this; -} - -CalypFrame::CalypFrame( const CalypFrame& other ) : d{ std::make_unique() } -{ - d->init( other.getWidth(), other.getHeight(), other.getPelFormat(), other.getBitsPel(), - other.getHasNegativeValues() ); + m_d->init( other.getWidth(), other.getHeight(), other.getPelFormat(), other.getBitsPel(), + other.getHasNegativeValues() ); copyFrom( &other ); } -CalypFrame& CalypFrame::operator=( const CalypFrame& other ) +auto CalypFrame::operator=( const CalypFrame& other ) -> CalypFrame& { // Self-assignment detection - if( &other == this ) - return *this; + if( &other == this ) return *this; // Copy the resource - d = std::make_unique(); - d->init( other.getWidth(), other.getHeight(), other.getPelFormat(), other.getBitsPel(), - other.getHasNegativeValues() ); + m_d = std::make_unique(); + m_d->init( other.getWidth(), other.getHeight(), other.getPelFormat(), other.getBitsPel(), + other.getHasNegativeValues() ); copyFrom( &other ); return *this; @@ -342,56 +322,47 @@ CalypFrame& CalypFrame::operator=( const CalypFrame& other ) CalypFrame::CalypFrame( const CalypFrame& other, unsigned int x, unsigned int y, unsigned int width, unsigned int height ) - : d{ std::make_unique() } + : m_d{ std::make_unique() } { const CalypPixelFormatDescriptor* pcPelFormat = &( g_CalypPixFmtDescriptorsMap.at( other.getPelFormat() ) ); - if( pcPelFormat->log2ChromaWidth ) + if( pcPelFormat->log2ChromaWidth > 0 ) { - if( x % ( 1 << pcPelFormat->log2ChromaWidth ) ) - x--; - if( ( x + width ) % ( 1 << pcPelFormat->log2ChromaWidth ) ) - width++; + if( x % ( 1UL << pcPelFormat->log2ChromaWidth ) != 0 ) x--; + if( ( x + width ) % ( 1UL << pcPelFormat->log2ChromaWidth ) != 0 ) width++; } - if( pcPelFormat->log2ChromaHeight ) + if( pcPelFormat->log2ChromaHeight > 0 ) { - if( y % ( 1 << pcPelFormat->log2ChromaHeight ) ) - y--; + if( y % ( 1UL << pcPelFormat->log2ChromaHeight ) != 0 ) y--; - if( ( y + height ) % ( 1 << pcPelFormat->log2ChromaHeight ) ) - height++; + if( ( y + height ) % ( 1UL << pcPelFormat->log2ChromaHeight ) != 0 ) height++; } - d->init( width, height, other.getPelFormat(), other.getBitsPel() ); + m_d->init( width, height, other.getPelFormat(), other.getBitsPel() ); copyFrom( other, x, y ); } CalypFrame::CalypFrame( const CalypFrame* other, unsigned int posX, unsigned int posY, unsigned int areaWidth, unsigned int areaHeight ) - : d{ std::make_unique() } + : m_d{ std::make_unique() } { - if( !other ) - return; + if( !other ) return; const CalypPixelFormatDescriptor* pcPelFormat = &( g_CalypPixFmtDescriptorsMap.at( other->getPelFormat() ) ); - if( pcPelFormat->log2ChromaWidth ) + if( pcPelFormat->log2ChromaWidth > 0 ) { - if( posX % ( 1 << pcPelFormat->log2ChromaWidth ) ) - posX--; - if( ( posX + areaWidth ) % ( 1 << pcPelFormat->log2ChromaWidth ) ) - areaWidth++; + if( posX % ( 1UL << pcPelFormat->log2ChromaWidth ) != 0 ) posX--; + if( ( posX + areaWidth ) % ( 1UL << pcPelFormat->log2ChromaWidth ) != 0 ) areaWidth++; } - if( pcPelFormat->log2ChromaHeight ) + if( pcPelFormat->log2ChromaHeight > 0 ) { - if( posY % ( 1 << pcPelFormat->log2ChromaHeight ) ) - posY--; + if( posY % ( 1UL << pcPelFormat->log2ChromaHeight ) != 0 ) posY--; - if( ( posY + areaHeight ) % ( 1 << pcPelFormat->log2ChromaHeight ) ) - areaHeight++; + if( ( posY + areaHeight ) % ( 1UL << pcPelFormat->log2ChromaHeight ) != 0 ) areaHeight++; } - d->init( areaWidth, areaHeight, other->getPelFormat(), other->getBitsPel() ); + m_d->init( areaWidth, areaHeight, other->getPelFormat(), other->getBitsPel() ); copyFrom( other, posX, posY ); } @@ -400,63 +371,54 @@ CalypFrame::~CalypFrame() = default; bool CalypFrame::haveSameFmt( const CalypFrame& other, unsigned int match ) const { bool bRet = true; - if( match & MATCH_COLOR_SPACE ) - bRet &= ( getColorSpace() == other.getColorSpace() ); - if( match & MATCH_RESOLUTION ) - bRet &= ( getWidth() == other.getWidth() ) && ( getHeight() == other.getHeight() ); - if( match & MATCH_PEL_FMT ) - bRet &= ( getPelFormat() == other.getPelFormat() ); - if( match & MATCH_BITS ) - bRet &= ( getBitsPel() == other.getBitsPel() ); + if( match & MATCH_COLOR_SPACE ) bRet &= ( getColorSpace() == other.getColorSpace() ); + if( match & MATCH_RESOLUTION ) bRet &= ( getWidth() == other.getWidth() ) && ( getHeight() == other.getHeight() ); + if( match & MATCH_PEL_FMT ) bRet &= ( getPelFormat() == other.getPelFormat() ); + if( match & MATCH_BITS ) bRet &= ( getBitsPel() == other.getBitsPel() ); if( match & MATCH_COLOR_SPACE_IGNORE_GRAY ) bRet &= ( getColorSpace() == CLP_COLOR_GRAY || getColorSpace() == other.getColorSpace() ); - if( match & MATCH_BYTES_PER_FRAME ) - bRet &= ( getBytesPerFrame() == other.getBytesPerFrame() ); + if( match & MATCH_BYTES_PER_FRAME ) bRet &= ( getBytesPerFrame() == other.getBytesPerFrame() ); return bRet; } -bool CalypFrame::haveSameFmt( const CalypFrame* other, unsigned int match ) const +auto CalypFrame::haveSameFmt( const CalypFrame* other, unsigned int match ) const -> bool { - bool bRet = false; - if( other ) - { - bRet = haveSameFmt( *other, match ); - } - return bRet; + if( other == nullptr ) return false; + return haveSameFmt( *other, match ); } auto CalypFrame::getPelFormat() const -> ClpPixelFormats { - return d->m_iPixelFormat; + return m_d->m_iPixelFormat; } auto CalypFrame::getPelFmtName() const -> std::string { - return std::string( d->m_pcPelFormat->name ); + return std::string( m_d->m_pcPelFormat->name ); } int CalypFrame::getColorSpace() const { - return d->m_pcPelFormat->colorSpace; + return m_d->m_pcPelFormat->colorSpace; } -unsigned int CalypFrame::getNumberChannels() const +auto CalypFrame::getNumberChannels() const -> unsigned int { - return d->m_pcPelFormat->numberChannels; + return m_d->m_pcPelFormat->numberChannels; } unsigned CalypFrame::getWidth( unsigned channel ) const { - return CHROMASHIFT( d->m_uiWidth, channel > 0 ? d->m_pcPelFormat->log2ChromaWidth : 0 ); + return CHROMASHIFT( m_d->m_uiWidth, channel > 0 ? m_d->m_pcPelFormat->log2ChromaWidth : 0 ); } unsigned CalypFrame::getHeight( unsigned channel ) const { - return CHROMASHIFT( d->m_uiHeight, channel > 0 ? d->m_pcPelFormat->log2ChromaHeight : 0 ); + return CHROMASHIFT( m_d->m_uiHeight, channel > 0 ? m_d->m_pcPelFormat->log2ChromaHeight : 0 ); } bool CalypFrame::getHasNegativeValues() const { - return d->m_bHasNegativeValues; + return m_d->m_bHasNegativeValues; } std::uint64_t CalypFrame::getPixels( unsigned channel ) const @@ -476,12 +438,12 @@ std::uint64_t CalypFrame::getTotalNumberOfPixels() const unsigned CalypFrame::getChromaWidthRatio() const { - return d->m_pcPelFormat->log2ChromaWidth; + return m_d->m_pcPelFormat->log2ChromaWidth; } unsigned CalypFrame::getChromaHeightRatio() const { - return d->m_pcPelFormat->log2ChromaHeight; + return m_d->m_pcPelFormat->log2ChromaHeight; } std::uint64_t CalypFrame::getChromaLength() const @@ -491,12 +453,12 @@ std::uint64_t CalypFrame::getChromaLength() const unsigned int CalypFrame::getBitsPel() const { - return d->m_uiBitsPel; + return m_d->m_uiBitsPel; } std::uint64_t CalypFrame::getBytesPerFrame() const { - return getBytesPerFrame( d->m_uiWidth, d->m_uiHeight, d->m_iPixelFormat, d->m_uiBitsPel ); + return getBytesPerFrame( m_d->m_uiWidth, m_d->m_uiHeight, m_d->m_iPixelFormat, m_d->m_uiBitsPel ); } std::uint64_t CalypFrame::getBytesPerFrame( unsigned int uiWidth, unsigned int uiHeight, ClpPixelFormats pelFormat, @@ -516,13 +478,13 @@ std::uint64_t CalypFrame::getBytesPerFrame( unsigned int uiWidth, unsigned int u void CalypFrame::reset() { - const ClpPel pelValue = 1 << ( d->m_uiBitsPel - 1 ); - for( std::size_t ch = 0; ch < d->m_pcPelFormat->numberChannels; ch++ ) + const ClpPel pelValue = 1UL << ( m_d->m_uiBitsPel - 1 ); + for( std::size_t ch = 0; ch < m_d->m_pcPelFormat->numberChannels; ch++ ) { - int ratioW = ch > 0 ? d->m_pcPelFormat->log2ChromaWidth : 0; - int ratioH = ch > 0 ? d->m_pcPelFormat->log2ChromaHeight : 0; - ClpPel* pPel = d->m_pppcInputPel[ch][0]; - for( std::size_t i = 0; i < CHROMASHIFT( d->m_uiHeight, ratioH ) * CHROMASHIFT( d->m_uiWidth, ratioW ); i++ ) + int ratioW = ch > 0 ? m_d->m_pcPelFormat->log2ChromaWidth : 0; + int ratioH = ch > 0 ? m_d->m_pcPelFormat->log2ChromaHeight : 0; + ClpPel* pPel = m_d->m_pppcInputPel[ch][0]; + for( std::size_t i = 0; i < CHROMASHIFT( m_d->m_uiHeight, ratioH ) * CHROMASHIFT( m_d->m_uiWidth, ratioW ); i++ ) { *pPel++ = pelValue; } @@ -531,35 +493,35 @@ void CalypFrame::reset() ClpPel*** CalypFrame::getPelBufferYUV() const { - return d->m_pppcInputPel; + return m_d->m_pppcInputPel; } ClpPel*** CalypFrame::getPelBufferYUV() { - d->m_bHasHistogram = false; - d->m_bHasRGBPel = false; - return d->m_pppcInputPel; + m_d->m_bHasHistogram = false; + m_d->m_bHasRGBPel = false; + return m_d->m_pppcInputPel; } auto CalypFrame::getRGBBuffer() const -> std::optional> { - if( !d->m_bHasRGBPel ) + if( !m_d->m_bHasRGBPel ) { return std::nullopt; } - return std::span{ d->m_pcARGB32 }; + return std::span{ m_d->m_pcARGB32 }; } ClpPel CalypFrame::operator()( unsigned int ch, unsigned int xPos, unsigned int yPos, bool absolute ) const { - int retValue = 0; - if( ch < d->m_pcPelFormat->numberChannels ) + if( ch >= m_d->m_pcPelFormat->numberChannels ) return 0; + + int pixel = m_d->m_pppcInputPel[ch][yPos][xPos]; + if( !absolute && m_d->m_bHasNegativeValues ) { - retValue = d->m_pppcInputPel[ch][yPos][xPos]; - if( !absolute && d->m_bHasNegativeValues ) - retValue = retValue - int( d->m_uiHalfPelValue ); + pixel = pixel - static_cast( m_d->m_uiHalfPelValue ); } - return ClpPel( retValue ); + return pixel; } CalypPixel CalypFrame::operator()( unsigned int xPos, unsigned int yPos ) const @@ -569,14 +531,14 @@ CalypPixel CalypFrame::operator()( unsigned int xPos, unsigned int yPos ) const CalypPixel CalypFrame::getPixel( unsigned int xPos, unsigned int yPos ) const { - assert( xPos < d->m_uiWidth ); - assert( yPos < d->m_uiHeight ); - CalypPixel PixelValue( d->m_pcPelFormat->colorSpace ); - for( unsigned int ch = 0; ch < d->m_pcPelFormat->numberChannels; ch++ ) + assert( xPos < m_d->m_uiWidth ); + assert( yPos < m_d->m_uiHeight ); + CalypPixel PixelValue( m_d->m_pcPelFormat->colorSpace ); + for( unsigned int ch = 0; ch < m_d->m_pcPelFormat->numberChannels; ch++ ) { - int ratioH = ch > 0 ? d->m_pcPelFormat->log2ChromaWidth : 0; - int ratioW = ch > 0 ? d->m_pcPelFormat->log2ChromaHeight : 0; - PixelValue[ch] = d->m_pppcInputPel[ch][( yPos >> ratioH )][( xPos >> ratioW )]; + int ratioH = ch > 0 ? m_d->m_pcPelFormat->log2ChromaWidth : 0; + int ratioW = ch > 0 ? m_d->m_pcPelFormat->log2ChromaHeight : 0; + PixelValue[ch] = m_d->m_pppcInputPel[ch][( yPos >> ratioH )][( xPos >> ratioW )]; } return PixelValue; } @@ -588,101 +550,94 @@ CalypPixel CalypFrame::getPixel( unsigned int xPos, unsigned int yPos, CalypColo void CalypFrame::setPixel( unsigned int xPos, unsigned int yPos, CalypPixel pixel ) { - for( unsigned int ch = 0; ch < d->m_pcPelFormat->numberChannels; ch++ ) + for( unsigned int ch = 0; ch < m_d->m_pcPelFormat->numberChannels; ch++ ) { - int ratioH = ch > 0 ? d->m_pcPelFormat->log2ChromaWidth : 0; - int ratioW = ch > 0 ? d->m_pcPelFormat->log2ChromaHeight : 0; - d->m_pppcInputPel[ch][( yPos >> ratioH )][( xPos >> ratioW )] = pixel[ch]; + int ratioH = ch > 0 ? m_d->m_pcPelFormat->log2ChromaWidth : 0; + int ratioW = ch > 0 ? m_d->m_pcPelFormat->log2ChromaHeight : 0; + m_d->m_pppcInputPel[ch][( yPos >> ratioH )][( xPos >> ratioW )] = pixel[ch]; } - d->m_bHasHistogram = false; - d->m_bHasRGBPel = false; + m_d->m_bHasHistogram = false; + m_d->m_bHasRGBPel = false; } void CalypFrame::copyFrom( const CalypFrame& other ) { - if( !haveSameFmt( other, MATCH_COLOR_SPACE | MATCH_BYTES_PER_FRAME | MATCH_BITS ) ) - return; - d->m_bHasRGBPel = false; - d->m_bHasHistogram = false; - memcpy( &( d->m_pppcInputPel[0][0][0] ), &( other.getPelBufferYUV()[0][0][0] ), + if( !haveSameFmt( other, MATCH_COLOR_SPACE | MATCH_BYTES_PER_FRAME | MATCH_BITS ) ) return; + m_d->m_bHasRGBPel = false; + m_d->m_bHasHistogram = false; + memcpy( &( m_d->m_pppcInputPel[0][0][0] ), &( other.getPelBufferYUV()[0][0][0] ), getTotalNumberOfPixels() * sizeof( ClpPel ) ); } void CalypFrame::copyFrom( const CalypFrame* other ) { - if( other ) - copyFrom( *other ); + if( other != nullptr ) copyFrom( *other ); } void CalypFrame::copyFrom( const CalypFrame& other, unsigned x, unsigned y ) { - if( !haveSameFmt( other, MATCH_COLOR_SPACE | MATCH_BITS ) ) - return; - // TODO: Protect width and height + if( !haveSameFmt( other, MATCH_COLOR_SPACE | MATCH_BITS ) ) return; + // TODO: @JC Protect width and height ClpPel*** pInput = other.getPelBufferYUV(); - for( unsigned int ch = 0; ch < d->m_pcPelFormat->numberChannels; ch++ ) + for( unsigned int ch = 0; ch < m_d->m_pcPelFormat->numberChannels; ch++ ) { - int ratioH = ch > 0 ? d->m_pcPelFormat->log2ChromaWidth : 0; - int ratioW = ch > 0 ? d->m_pcPelFormat->log2ChromaHeight : 0; - for( unsigned int i = 0; i < CHROMASHIFT( d->m_uiHeight, ratioH ); i++ ) + int ratioH = ch > 0 ? m_d->m_pcPelFormat->log2ChromaWidth : 0; + int ratioW = ch > 0 ? m_d->m_pcPelFormat->log2ChromaHeight : 0; + for( unsigned int i = 0; i < CHROMASHIFT( m_d->m_uiHeight, ratioH ); i++ ) { - memcpy( &( d->m_pppcInputPel[ch][i][0] ), &( pInput[ch][( y >> ratioH ) + i][( x >> ratioW )] ), - ( d->m_uiWidth >> ratioW ) * sizeof( ClpPel ) ); + memcpy( &( m_d->m_pppcInputPel[ch][i][0] ), &( pInput[ch][( y >> ratioH ) + i][( x >> ratioW )] ), + ( m_d->m_uiWidth >> ratioW ) * sizeof( ClpPel ) ); } } - d->m_bHasRGBPel = false; - d->m_bHasHistogram = false; + m_d->m_bHasRGBPel = false; + m_d->m_bHasHistogram = false; } void CalypFrame::copyFrom( const CalypFrame* other, unsigned x, unsigned y ) { - if( other ) - copyFrom( *other, x, y ); + if( other ) copyFrom( *other, x, y ); } void CalypFrame::copyTo( const CalypFrame& other, unsigned x, unsigned y ) const { - if( !haveSameFmt( other, MATCH_COLOR_SPACE | MATCH_PEL_FMT | MATCH_BITS ) ) - return; + if( !haveSameFmt( other, MATCH_COLOR_SPACE | MATCH_PEL_FMT | MATCH_BITS ) ) return; ClpPel*** pInput = other.getPelBufferYUV(); unsigned width = other.getWidth(); // TODO: Protect width and height - for( unsigned int ch = 0; ch < d->m_pcPelFormat->numberChannels; ch++ ) + for( unsigned int ch = 0; ch < m_d->m_pcPelFormat->numberChannels; ch++ ) { - int ratioH = ch > 0 ? d->m_pcPelFormat->log2ChromaWidth : 0; - int ratioW = ch > 0 ? d->m_pcPelFormat->log2ChromaHeight : 0; + int ratioH = ch > 0 ? m_d->m_pcPelFormat->log2ChromaWidth : 0; + int ratioW = ch > 0 ? m_d->m_pcPelFormat->log2ChromaHeight : 0; for( unsigned int i = 0; i < other.getHeight( ch ); i++ ) { - memcpy( &( d->m_pppcInputPel[ch][( y >> ratioH ) + i][( x >> ratioW )] ), &( pInput[ch][i][0] ), + memcpy( &( m_d->m_pppcInputPel[ch][( y >> ratioH ) + i][( x >> ratioW )] ), &( pInput[ch][i][0] ), ( width >> ratioW ) * sizeof( ClpPel ) ); } } - d->m_bHasRGBPel = false; - d->m_bHasHistogram = false; + m_d->m_bHasRGBPel = false; + m_d->m_bHasHistogram = false; } void CalypFrame::copyTo( const CalypFrame* other, unsigned x, unsigned y ) const { - if( other ) - copyTo( *other, x, y ); + if( other != nullptr ) copyTo( *other, x, y ); } -void CalypFrame::frameFromBuffer( std::span Buff, int iEndianness, unsigned long uiBuffSize ) +void CalypFrame::frameFromBuffer( std::span buff, int iEndianness, std::uint64_t uiBuffSize ) { - if( uiBuffSize != getBytesPerFrame() ) - return; + if( uiBuffSize != getBytesPerFrame() ) return; - frameFromBuffer( Buff, iEndianness ); + frameFromBuffer( buff, iEndianness ); } -void CalypFrame::frameFromBuffer( std::span Buff, int iEndianness ) +void CalypFrame::frameFromBuffer( std::span buff, int iEndianness ) { std::array ppBuff{ nullptr }; - int bytesPixel = ( d->m_uiBitsPel - 1 ) / kNumBitsInByte + 1; + int bytesPixel = ( m_d->m_uiBitsPel - 1 ) / kNumBitsInByte + 1; int startByte = 0; int endByte = bytesPixel; int incByte = 1; - int maxval = ( 1 << d->m_uiBitsPel ) - 1; + int maxval = ( 1UL << m_d->m_uiBitsPel ) - 1; if( iEndianness == CLP_BIG_ENDIAN ) { @@ -691,27 +646,28 @@ void CalypFrame::frameFromBuffer( std::span Buff, int iEndianness incByte = -1; } - ppBuff[0] = Buff.data(); + ppBuff[0] = buff.data(); for( std::size_t i = 1; i < CalypPixel::getMaxNumberOfComponents(); i++ ) { - int ratioW = i > 1 ? d->m_pcPelFormat->log2ChromaWidth : 0; - int ratioH = i > 1 ? d->m_pcPelFormat->log2ChromaHeight : 0; - ppBuff[i] = ppBuff[i - 1] + CHROMASHIFT( d->m_uiHeight, ratioH ) * CHROMASHIFT( d->m_uiWidth, ratioW ) * bytesPixel; + int ratioW = i > 1 ? m_d->m_pcPelFormat->log2ChromaWidth : 0; + int ratioH = i > 1 ? m_d->m_pcPelFormat->log2ChromaHeight : 0; + ppBuff[i] = + ppBuff[i - 1] + CHROMASHIFT( m_d->m_uiHeight, ratioH ) * CHROMASHIFT( m_d->m_uiWidth, ratioW ) * bytesPixel; } - memset( d->m_pppcInputPel[0][0], 0, getTotalNumberOfPixels() * sizeof( ClpPel ) ); + memset( m_d->m_pppcInputPel[0][0], 0, getTotalNumberOfPixels() * sizeof( ClpPel ) ); - for( std::size_t ch = 0; ch < d->m_pcPelFormat->numberChannels; ch++ ) + for( std::size_t ch = 0; ch < m_d->m_pcPelFormat->numberChannels; ch++ ) { - int ratioW = ch > 0 ? d->m_pcPelFormat->log2ChromaWidth : 0; - int ratioH = ch > 0 ? d->m_pcPelFormat->log2ChromaHeight : 0; - unsigned step = d->m_pcPelFormat->comp[ch].step_minus1 * bytesPixel; + int ratioW = ch > 0 ? m_d->m_pcPelFormat->log2ChromaWidth : 0; + int ratioH = ch > 0 ? m_d->m_pcPelFormat->log2ChromaHeight : 0; + unsigned step = m_d->m_pcPelFormat->comp[ch].step_minus1 * bytesPixel; - ClpPel* pPel = d->m_pppcInputPel[ch][0]; + ClpPel* pPel = m_d->m_pppcInputPel[ch][0]; const ClpByte* pTmpBuff = - ppBuff[d->m_pcPelFormat->comp[ch].plane] + ( d->m_pcPelFormat->comp[ch].offset_plus1 - 1 ) * bytesPixel; + ppBuff[m_d->m_pcPelFormat->comp[ch].plane] + ( m_d->m_pcPelFormat->comp[ch].offset_plus1 - 1 ) * bytesPixel; - for( std::size_t p = 0; p < CHROMASHIFT( d->m_uiHeight, ratioH ) * CHROMASHIFT( d->m_uiWidth, ratioW ); p++ ) + for( std::size_t p = 0; p < CHROMASHIFT( m_d->m_uiHeight, ratioH ) * CHROMASHIFT( m_d->m_uiWidth, ratioW ); p++ ) { for( int b = startByte; b != endByte; b += incByte ) { @@ -720,18 +676,17 @@ void CalypFrame::frameFromBuffer( std::span Buff, int iEndianness } // Check max value and bound it to "maxval" to prevent segfault when // calculating histogram - if( pPel[p] > maxval ) - pPel[p] = 0; + if( pPel[p] > maxval ) pPel[p] = 0; pTmpBuff += step; } } - d->m_bHasRGBPel = false; - d->m_bHasHistogram = false; + m_d->m_bHasRGBPel = false; + m_d->m_bHasHistogram = false; } -void CalypFrame::frameToBuffer( std::span output_buffer, int iEndianness ) const +void CalypFrame::frameToBuffer( std::span output, int iEndianness ) const { - const int bytesPixel = ( d->m_uiBitsPel - 1 ) / kNumBitsInByte + 1; + const int bytesPixel = ( m_d->m_uiBitsPel - 1 ) / kNumBitsInByte + 1; std::array ppBuff{ nullptr }; int startByte = 0; @@ -745,25 +700,26 @@ void CalypFrame::frameToBuffer( std::span output_buffer, int iEndiannes incByte = -1; } - ppBuff[0] = output_buffer.data(); + ppBuff[0] = output.data(); for( std::size_t i = 1; i < CalypPixel::getMaxNumberOfComponents(); i++ ) { - int ratioW = i > 1 ? d->m_pcPelFormat->log2ChromaWidth : 0; - int ratioH = i > 1 ? d->m_pcPelFormat->log2ChromaHeight : 0; - ppBuff[i] = ppBuff[i - 1] + CHROMASHIFT( d->m_uiHeight, ratioH ) * CHROMASHIFT( d->m_uiWidth, ratioW ) * bytesPixel; + int ratioW = i > 1 ? m_d->m_pcPelFormat->log2ChromaWidth : 0; + int ratioH = i > 1 ? m_d->m_pcPelFormat->log2ChromaHeight : 0; + ppBuff[i] = + ppBuff[i - 1] + CHROMASHIFT( m_d->m_uiHeight, ratioH ) * CHROMASHIFT( m_d->m_uiWidth, ratioW ) * bytesPixel; } - for( std::size_t ch = 0; ch < d->m_pcPelFormat->numberChannels; ch++ ) + for( std::size_t ch = 0; ch < m_d->m_pcPelFormat->numberChannels; ch++ ) { - int ratioW = ch > 0 ? d->m_pcPelFormat->log2ChromaWidth : 0; - int ratioH = ch > 0 ? d->m_pcPelFormat->log2ChromaHeight : 0; - int step = ( d->m_pcPelFormat->comp[ch].step_minus1 ) * bytesPixel; + int ratioW = ch > 0 ? m_d->m_pcPelFormat->log2ChromaWidth : 0; + int ratioH = ch > 0 ? m_d->m_pcPelFormat->log2ChromaHeight : 0; + int step = ( m_d->m_pcPelFormat->comp[ch].step_minus1 ) * bytesPixel; - ClpPel* pTmpPel = d->m_pppcInputPel[ch][0]; + ClpPel* pTmpPel = m_d->m_pppcInputPel[ch][0]; ClpByte* pTmpBuff = - ppBuff[d->m_pcPelFormat->comp[ch].plane] + ( d->m_pcPelFormat->comp[ch].offset_plus1 - 1 ) * bytesPixel; + ppBuff[m_d->m_pcPelFormat->comp[ch].plane] + ( m_d->m_pcPelFormat->comp[ch].offset_plus1 - 1 ) * bytesPixel; - for( std::size_t i = 0; i < CHROMASHIFT( d->m_uiHeight, ratioH ) * CHROMASHIFT( d->m_uiWidth, ratioW ); i++ ) + for( std::size_t i = 0; i < CHROMASHIFT( m_d->m_uiHeight, ratioH ) * CHROMASHIFT( m_d->m_uiWidth, ratioW ); i++ ) { for( int b = startByte; b != endByte; b += incByte ) { @@ -789,14 +745,12 @@ inline constexpr auto convert_to_pel_argb( const T r, const T g, const T b ) -> } template -inline constexpr void convert_yuv_to_rgb( const T iY, const T iU, const T iV, T& iR, T& iG, T& iB ) +constexpr void convert_yuv_to_rgb( const T iY, const T iU, const T iV, T& iR, T& iG, T& iB ) { - auto clamp_pel_value = []( auto X ) { - if( X < 0 ) - return 0; - if( X > 255 ) - return 255; - return X; + auto clamp_pel_value = []( auto x ) { + if( x < 0 ) return 0; + if( x > 255 ) return 255; + return x; }; iR = iY + ( ( 1436 * ( iV - 128 ) ) >> 10 ); @@ -841,15 +795,15 @@ void fillRGBBufferYUV420p( ClpPel*** pppInputPel, uint32_t* pARGB, unsigned uiWi void CalypFrame::fillRGBBuffer( std::optional channel ) const { - auto shiftBits = static_cast( d->m_uiBitsPel ) - 8; + auto shiftBits = static_cast( m_d->m_uiBitsPel ) - 8; - d->m_bHasRGBPel = true; + m_d->m_bHasRGBPel = true; // 4 bytes for A, R, G and B - uint32_t* pARGB = (uint32_t*)d->m_pcARGB32.data(); - if( d->m_pcPelFormat->colorSpace == CLP_COLOR_GRAY || ( channel.has_value() && *channel == 0 ) ) + auto* pARGB = reinterpret_cast( m_d->m_pcARGB32.data() ); + if( m_d->m_pcPelFormat->colorSpace == CLP_COLOR_GRAY || ( channel.has_value() && *channel == 0 ) ) { - ClpPel* pY = d->m_pppcInputPel[0][0]; - for( unsigned int i = 0; i < d->m_uiHeight * d->m_uiWidth; i++ ) + ClpPel* pY = m_d->m_pppcInputPel[0][0]; + for( unsigned int i = 0; i < m_d->m_uiHeight * m_d->m_uiWidth; i++ ) { unsigned char finalPel = ( *pY++ ) >> shiftBits; *pARGB++ = convert_to_pel_argb( finalPel, finalPel, finalPel ); @@ -857,72 +811,72 @@ void CalypFrame::fillRGBBuffer( std::optional channel ) const } else if( channel.has_value() && *channel > 0 ) { - ClpPel* pLine = d->m_pppcInputPel[*channel][0]; - for( unsigned y = 0; y < CHROMASHIFT( d->m_uiHeight, d->m_pcPelFormat->log2ChromaHeight ); y++ ) + ClpPel* pLine = m_d->m_pppcInputPel[*channel][0]; + for( unsigned y = 0; y < CHROMASHIFT( m_d->m_uiHeight, m_d->m_pcPelFormat->log2ChromaHeight ); y++ ) { - for( int i = 0; i < 1 << d->m_pcPelFormat->log2ChromaHeight; i++ ) + for( int i = 0; i < 1UL << m_d->m_pcPelFormat->log2ChromaHeight; i++ ) { ClpPel* pPel = pLine; - for( unsigned x = 0; x < CHROMASHIFT( d->m_uiWidth, d->m_pcPelFormat->log2ChromaWidth ); x++ ) + for( unsigned x = 0; x < CHROMASHIFT( m_d->m_uiWidth, m_d->m_pcPelFormat->log2ChromaWidth ); x++ ) { unsigned char finalPel = ( *pPel++ ) >> shiftBits; - for( int j = 0; j < ( 1 << d->m_pcPelFormat->log2ChromaWidth ); j++ ) + for( int j = 0; j < ( 1UL << m_d->m_pcPelFormat->log2ChromaWidth ); j++ ) { *pARGB++ = convert_to_pel_argb( finalPel, finalPel, finalPel ); } } } - pLine += CHROMASHIFT( d->m_uiWidth, d->m_pcPelFormat->log2ChromaWidth ); + pLine += CHROMASHIFT( m_d->m_uiWidth, m_d->m_pcPelFormat->log2ChromaWidth ); } } - else if( d->m_pcPelFormat->colorSpace == CLP_COLOR_RGB ) + else if( m_d->m_pcPelFormat->colorSpace == CLP_COLOR_RGB ) { - ClpPel* pR = d->m_pppcInputPel[CLP_COLOR_R][0]; - ClpPel* pG = d->m_pppcInputPel[CLP_COLOR_G][0]; - ClpPel* pB = d->m_pppcInputPel[CLP_COLOR_B][0]; - for( unsigned int i = 0; i < d->m_uiHeight * d->m_uiWidth; i++ ) + ClpPel* pR = m_d->m_pppcInputPel[CLP_COLOR_R][0]; + ClpPel* pG = m_d->m_pppcInputPel[CLP_COLOR_G][0]; + ClpPel* pB = m_d->m_pppcInputPel[CLP_COLOR_B][0]; + for( unsigned int i = 0; i < m_d->m_uiHeight * m_d->m_uiWidth; i++ ) *pARGB++ = convert_to_pel_argb( ( *pR++ ) >> shiftBits, ( *pG++ ) >> shiftBits, ( *pB++ ) >> shiftBits ); } - else if( d->m_pcPelFormat->colorSpace == CLP_COLOR_RGBA ) + else if( m_d->m_pcPelFormat->colorSpace == CLP_COLOR_RGBA ) { - ClpPel* pR = d->m_pppcInputPel[CLP_COLOR_R][0]; - ClpPel* pG = d->m_pppcInputPel[CLP_COLOR_G][0]; - ClpPel* pB = d->m_pppcInputPel[CLP_COLOR_B][0]; - ClpPel* pA = d->m_pppcInputPel[CLP_COLOR_A][0]; - for( unsigned int i = 0; i < d->m_uiHeight * d->m_uiWidth; i++ ) + ClpPel* pR = m_d->m_pppcInputPel[CLP_COLOR_R][0]; + ClpPel* pG = m_d->m_pppcInputPel[CLP_COLOR_G][0]; + ClpPel* pB = m_d->m_pppcInputPel[CLP_COLOR_B][0]; + ClpPel* pA = m_d->m_pppcInputPel[CLP_COLOR_A][0]; + for( unsigned int i = 0; i < m_d->m_uiHeight * m_d->m_uiWidth; i++ ) *pARGB++ = convert_to_pel_argb( ( *pA++ ) >> shiftBits, ( *pR++ ) >> shiftBits, ( *pG++ ) >> shiftBits, ( *pB++ ) >> shiftBits ); } - else if( d->m_pcPelFormat->colorSpace == CLP_COLOR_YUV ) + else if( m_d->m_pcPelFormat->colorSpace == CLP_COLOR_YUV ) { - if( d->m_iPixelFormat == ClpPixelFormats::YUV420p ) + if( m_d->m_iPixelFormat == ClpPixelFormats::YUV420p ) { - fillRGBBufferYUV420p( d->m_pppcInputPel, pARGB, d->m_uiWidth, d->m_uiHeight, shiftBits ); + fillRGBBufferYUV420p( m_d->m_pppcInputPel, pARGB, m_d->m_uiWidth, m_d->m_uiHeight, shiftBits ); return; } - ClpPel* pLineY = d->m_pppcInputPel[CLP_LUMA][0]; - ClpPel* pLineU = d->m_pppcInputPel[CLP_CHROMA_U][0]; - ClpPel* pLineV = d->m_pppcInputPel[CLP_CHROMA_V][0]; - unsigned int uiChromaStride = CHROMASHIFT( d->m_uiWidth, d->m_pcPelFormat->log2ChromaWidth ); + ClpPel* pLineY = m_d->m_pppcInputPel[CLP_LUMA][0]; + ClpPel* pLineU = m_d->m_pppcInputPel[CLP_CHROMA_U][0]; + ClpPel* pLineV = m_d->m_pppcInputPel[CLP_CHROMA_V][0]; + unsigned int uiChromaStride = CHROMASHIFT( m_d->m_uiWidth, m_d->m_pcPelFormat->log2ChromaWidth ); int iR, iG, iB; // NOLINT uint32_t* pARGBLine = pARGB; - for( unsigned y = 0; y < CHROMASHIFT( d->m_uiHeight, d->m_pcPelFormat->log2ChromaHeight ); y++ ) + for( unsigned y = 0; y < CHROMASHIFT( m_d->m_uiHeight, m_d->m_pcPelFormat->log2ChromaHeight ); y++ ) { - for( int i = 0; i < 1 << d->m_pcPelFormat->log2ChromaHeight; i++ ) + for( int i = 0; i < 1UL << m_d->m_pcPelFormat->log2ChromaHeight; i++ ) { ClpPel* pY = pLineY; ClpPel* pU = pLineU; ClpPel* pV = pLineV; uint32_t* pARGBAux = pARGBLine; - for( unsigned x = 0; x < CHROMASHIFT( d->m_uiWidth, d->m_pcPelFormat->log2ChromaWidth ); x++ ) + for( unsigned x = 0; x < CHROMASHIFT( m_d->m_uiWidth, m_d->m_pcPelFormat->log2ChromaWidth ); x++ ) { int iU = *pU++; iU >>= shiftBits; int iV = *pV++; iV >>= shiftBits; - for( int j = 0; j < ( 1 << d->m_pcPelFormat->log2ChromaWidth ); j++ ) + for( int j = 0; j < ( 1UL << m_d->m_pcPelFormat->log2ChromaWidth ); j++ ) { int iY = *pY++; iY >>= shiftBits; @@ -930,8 +884,8 @@ void CalypFrame::fillRGBBuffer( std::optional channel ) const *pARGBAux++ = convert_to_pel_argb( iR, iG, iB ); } } - pLineY += d->m_uiWidth; - pARGBLine += d->m_uiWidth; + pLineY += m_d->m_uiWidth; + pARGBLine += m_d->m_uiWidth; } pLineU += uiChromaStride; pLineV += uiChromaStride; @@ -941,8 +895,7 @@ void CalypFrame::fillRGBBuffer( std::optional channel ) const void CalypFrame::fillRGBBuffer() const { - if( d->m_bHasRGBPel ) - return; + if( m_d->m_bHasRGBPel ) return; fillRGBBuffer( {} ); } @@ -953,70 +906,67 @@ void CalypFrame::fillRGBBuffer() const void CalypFrame::calcHistogram() { - if( d->m_bHasHistogram || d->m_puiHistogram.empty() ) - return; + if( m_d->m_bHasHistogram || m_d->m_puiHistogram.empty() ) return; - d->m_bHistogramRunning = true; + m_d->m_bHistogramRunning = true; - std::fill( d->m_puiHistogram.begin(), d->m_puiHistogram.end(), 0 ); + std::fill( m_d->m_puiHistogram.begin(), m_d->m_puiHistogram.end(), 0 ); - unsigned int numberChannels = d->m_pcPelFormat->numberChannels; + unsigned int numberChannels = m_d->m_pcPelFormat->numberChannels; for( unsigned int ch = 0; ch < numberChannels; ch++ ) { - unsigned int size = CHROMASHIFT( d->m_uiWidth, ch > 0 ? d->m_pcPelFormat->log2ChromaWidth : 0 ) * - CHROMASHIFT( d->m_uiHeight, ch > 0 ? d->m_pcPelFormat->log2ChromaHeight : 0 ); + unsigned int size = CHROMASHIFT( m_d->m_uiWidth, ch > 0 ? m_d->m_pcPelFormat->log2ChromaWidth : 0 ) * + CHROMASHIFT( m_d->m_uiHeight, ch > 0 ? m_d->m_pcPelFormat->log2ChromaHeight : 0 ); - ClpPel* chPel = &( d->m_pppcInputPel[ch][0][0] ); + ClpPel* chPel = &( m_d->m_pppcInputPel[ch][0][0] ); for( unsigned int i = 0; i < size; i++ ) { - d->m_puiHistogram[*chPel + ch * d->m_uiHistoSegments]++; + m_d->m_puiHistogram[*chPel + ch * m_d->m_uiHistoSegments]++; chPel++; } } - if( d->m_pcPelFormat->colorSpace == CLP_COLOR_RGB || d->m_pcPelFormat->colorSpace == CLP_COLOR_RGBA ) + if( m_d->m_pcPelFormat->colorSpace == CLP_COLOR_RGB || m_d->m_pcPelFormat->colorSpace == CLP_COLOR_RGBA ) { - for( unsigned int y = 0; y < d->m_uiHeight; y++ ) - for( unsigned int x = 0; x < d->m_uiWidth; x++ ) + for( unsigned int y = 0; y < m_d->m_uiHeight; y++ ) + for( unsigned int x = 0; x < m_d->m_uiWidth; x++ ) { ClpPel luma = getPixel( x, y ).convertPixel( CLP_COLOR_YUV )[0]; - d->m_puiHistogram[luma + ( d->m_uiHistoChannels - 1 ) * d->m_uiHistoSegments]++; + m_d->m_puiHistogram[luma + ( m_d->m_uiHistoChannels - 1 ) * m_d->m_uiHistoSegments]++; } } - d->m_bHasHistogram = true; - d->m_bHistogramRunning = false; + m_d->m_bHasHistogram = true; + m_d->m_bHistogramRunning = false; } int CalypFrame::getNumHistogramSegment() const { - return d->m_uiHistoSegments; + return m_d->m_uiHistoSegments; } unsigned int CalypFrame::getMinimumPelValue( unsigned channel ) const { - if( !d->m_bHasHistogram ) - return 0; + if( !m_d->m_bHasHistogram ) return 0; - channel = d->getRealHistogramChannel( channel ); - if( channel < 0 ) - return 0; + channel = m_d->getRealHistogramChannel( channel ); + if( channel < 0 ) return 0; int indexStart; int indexEnd; if( channel == HIST_ALL_CHANNELS ) { indexStart = 0; - indexEnd = d->m_uiHistoChannels * d->m_uiHistoSegments; + indexEnd = m_d->m_uiHistoChannels * m_d->m_uiHistoSegments; } else { - indexStart = channel * d->m_uiHistoSegments; - indexEnd = indexStart + d->m_uiHistoSegments; + indexStart = channel * m_d->m_uiHistoSegments; + indexEnd = indexStart + m_d->m_uiHistoSegments; } for( int i = indexStart; i < indexEnd; i++ ) { - if( d->m_puiHistogram[i] > 0 ) + if( m_d->m_puiHistogram[i] > 0 ) { return i - indexStart; } @@ -1026,29 +976,27 @@ unsigned int CalypFrame::getMinimumPelValue( unsigned channel ) const unsigned int CalypFrame::getMaximumPelValue( unsigned channel ) const { - if( !d->m_bHasHistogram ) - return 0; + if( !m_d->m_bHasHistogram ) return 0; - channel = d->getRealHistogramChannel( channel ); - if( channel < 0 ) - return 0; + channel = m_d->getRealHistogramChannel( channel ); + if( channel < 0 ) return 0; int indexStart; int indexEnd; if( channel == HIST_ALL_CHANNELS ) { - indexStart = d->m_uiHistoChannels * d->m_uiHistoSegments; + indexStart = m_d->m_uiHistoChannels * m_d->m_uiHistoSegments; indexEnd = 0; } else { - indexStart = ( channel + 1 ) * d->m_uiHistoSegments - 1; - indexEnd = indexStart - d->m_uiHistoSegments; + indexStart = ( channel + 1 ) * m_d->m_uiHistoSegments - 1; + indexEnd = indexStart - m_d->m_uiHistoSegments; } for( int i = indexStart; i > indexEnd; i-- ) { - if( d->m_puiHistogram[i] > 0 ) + if( m_d->m_puiHistogram[i] > 0 ) { return i - indexEnd - 1; } @@ -1058,30 +1006,28 @@ unsigned int CalypFrame::getMaximumPelValue( unsigned channel ) const unsigned int CalypFrame::getNEBins( unsigned channel ) const { - if( !d->m_bHasHistogram ) - return 0; + if( !m_d->m_bHasHistogram ) return 0; - channel = d->getRealHistogramChannel( channel ); - if( channel < 0 ) - return 0; + channel = m_d->getRealHistogramChannel( channel ); + if( channel < 0 ) return 0; int indexStart; int indexEnd; if( channel == HIST_ALL_CHANNELS ) { indexStart = 0; - indexEnd = d->m_uiHistoChannels * d->m_uiHistoSegments; + indexEnd = m_d->m_uiHistoChannels * m_d->m_uiHistoSegments; } else { - indexStart = channel * d->m_uiHistoSegments; - indexEnd = indexStart + d->m_uiHistoSegments; + indexStart = channel * m_d->m_uiHistoSegments; + indexEnd = indexStart + m_d->m_uiHistoSegments; } int nEBins = 0; for( int i = indexStart; i < indexEnd; i++ ) { - if( d->m_puiHistogram[i] > 0 ) + if( m_d->m_puiHistogram[i] > 0 ) { nEBins++; } @@ -1091,12 +1037,10 @@ unsigned int CalypFrame::getNEBins( unsigned channel ) const unsigned int CalypFrame::getMaximum( unsigned channel ) const { - if( !d->m_bHasHistogram ) - return 0; + if( !m_d->m_bHasHistogram ) return 0; - channel = d->getRealHistogramChannel( channel ); - if( channel < 0 ) - return 0; + channel = m_d->getRealHistogramChannel( channel ); + if( channel < 0 ) return 0; unsigned int maxValue = 0; int indexStart{ 0 }; @@ -1104,19 +1048,19 @@ unsigned int CalypFrame::getMaximum( unsigned channel ) const if( channel == HIST_ALL_CHANNELS ) { indexStart = 0; - indexEnd = d->m_uiHistoChannels * d->m_uiHistoSegments; + indexEnd = m_d->m_uiHistoChannels * m_d->m_uiHistoSegments; } else { - indexStart = channel * d->m_uiHistoSegments; - indexEnd = indexStart + d->m_uiHistoSegments; + indexStart = channel * m_d->m_uiHistoSegments; + indexEnd = indexStart + m_d->m_uiHistoSegments; } for( int x = indexStart; x < indexEnd; x++ ) { - if( d->m_puiHistogram[x] > maxValue ) + if( m_d->m_puiHistogram[x] > maxValue ) { - maxValue = d->m_puiHistogram[x]; + maxValue = m_d->m_puiHistogram[x]; } } return maxValue; @@ -1124,11 +1068,11 @@ unsigned int CalypFrame::getMaximum( unsigned channel ) const unsigned int CalypFrame::getNumPixelsRange( unsigned channel, unsigned int start, unsigned int end ) const { - if( !d->m_bHasHistogram || start < 0 || end > d->m_uiHistoSegments - 1 || start > end ) + if( !m_d->m_bHasHistogram || start < 0 || end > m_d->m_uiHistoSegments - 1 || start > end ) { return 0; } - channel = d->getRealHistogramChannel( channel ); + channel = m_d->getRealHistogramChannel( channel ); if( channel < 0 ) { return 0; @@ -1136,31 +1080,31 @@ unsigned int CalypFrame::getNumPixelsRange( unsigned channel, unsigned int start unsigned int count = 0; int indexStart{ 0 }; - indexStart = channel * d->m_uiHistoSegments; + indexStart = channel * m_d->m_uiHistoSegments; for( unsigned int i = start; i <= end; i++ ) { - count += d->m_puiHistogram[indexStart + i]; + count += m_d->m_puiHistogram[indexStart + i]; } return count; } double CalypFrame::getMean( unsigned channel, unsigned int start, unsigned int end ) const { - if( !d->m_bHasHistogram || start < 0 || end > d->m_uiHistoSegments - 1 || start > end ) + if( !m_d->m_bHasHistogram || start < 0 || end > m_d->m_uiHistoSegments - 1 || start > end ) { return 0.0; } - channel = d->getRealHistogramChannel( channel ); + channel = m_d->getRealHistogramChannel( channel ); if( channel < 0 ) { return 0.0; } double mean{ 0.0 }; - int indexStart = channel * d->m_uiHistoSegments; + int indexStart = channel * m_d->m_uiHistoSegments; for( unsigned int i = start; i <= end; i++ ) { - mean += i * d->m_puiHistogram[indexStart + i]; + mean += i * m_d->m_puiHistogram[indexStart + i]; } auto count = getNumPixelsRange( channel, start, end ); @@ -1175,25 +1119,24 @@ double CalypFrame::getMean( unsigned channel, unsigned int start, unsigned int e int CalypFrame::getMedian( unsigned channel, unsigned int start, unsigned int end ) const { - if( !d->m_bHasHistogram || start < 0 || end > d->m_uiHistoSegments - 1 || start > end ) + if( !m_d->m_bHasHistogram || start < 0 || end > m_d->m_uiHistoSegments - 1 || start > end ) { return 0; } - channel = d->getRealHistogramChannel( channel ); + channel = m_d->getRealHistogramChannel( channel ); if( channel < 0 ) { return 0; } double sum = 0.0; - int indexStart = channel * d->m_uiHistoSegments; + int indexStart = channel * m_d->m_uiHistoSegments; double count = getNumPixelsRange( channel, start, end ); for( unsigned int i = start; i <= end; i++ ) { - sum += d->m_puiHistogram[indexStart + i]; - if( sum * 2 > count ) - return i; + sum += m_d->m_puiHistogram[indexStart + i]; + if( sum * 2 > count ) return i; } return 0; @@ -1201,23 +1144,22 @@ int CalypFrame::getMedian( unsigned channel, unsigned int start, unsigned int en double CalypFrame::getStdDev( unsigned channel, unsigned int start, unsigned int end ) const { - if( !d->m_bHasHistogram || start < 0 || end > d->m_uiHistoSegments - 1 || start > end ) + if( !m_d->m_bHasHistogram || start < 0 || end > m_d->m_uiHistoSegments - 1 || start > end ) { return 0.0; } - channel = d->getRealHistogramChannel( channel ); + channel = m_d->getRealHistogramChannel( channel ); if( channel < 0 ) { return 0.0; } - int indexStart = channel * d->m_uiHistoSegments; + int indexStart = channel * m_d->m_uiHistoSegments; double mean = getMean( channel, start, end ); double count = getNumPixelsRange( channel, start, end ); double dev = 0.0; - if( count == 0.0 ) - count = 1.0; + if( count == 0.0 ) count = 1.0; /*------------ original @@ -1232,7 +1174,7 @@ double CalypFrame::getStdDev( unsigned channel, unsigned int start, unsigned int for( unsigned int i = start; i <= end; i++ ) { - dev += ( i * i ) * d->m_puiHistogram[indexStart + i]; + dev += ( i * i ) * m_d->m_puiHistogram[indexStart + i]; } return sqrt( ( dev - count * mean * mean ) / ( count - 1 ) ); @@ -1240,38 +1182,34 @@ double CalypFrame::getStdDev( unsigned channel, unsigned int start, unsigned int double CalypFrame::getHistogramValue( unsigned channel, unsigned int bin ) const { - if( !d->m_bHasHistogram || bin < 0 || bin > d->m_uiHistoSegments - 1 ) - return 0.0; + if( !m_d->m_bHasHistogram || bin < 0 || bin > m_d->m_uiHistoSegments - 1 ) return 0.0; - channel = d->getRealHistogramChannel( channel ); + channel = m_d->getRealHistogramChannel( channel ); if( channel < 0 ) { return 0; } - int indexStart = channel * d->m_uiHistoSegments; - return d->m_puiHistogram[indexStart + bin]; + int indexStart = channel * m_d->m_uiHistoSegments; + return m_d->m_puiHistogram[indexStart + bin]; } double CalypFrame::getEntropy( unsigned channel, unsigned int start, unsigned int end ) const { - if( !d->m_bHasHistogram ) - return 0; + if( !m_d->m_bHasHistogram ) return 0; - channel = d->getRealHistogramChannel( channel ); - if( channel < 0 ) - return 0; + channel = m_d->getRealHistogramChannel( channel ); + if( channel < 0 ) return 0; - int indexStart = channel * d->m_uiHistoSegments; + int indexStart = channel * m_d->m_uiHistoSegments; double numValues = getNumPixelsRange( channel, start, end ); double entropy = 0.0; for( unsigned b = start; b <= end; b++ ) { - if( d->m_puiHistogram[indexStart + b] == 0 ) - continue; + if( m_d->m_puiHistogram[indexStart + b] == 0 ) continue; - double prob = d->m_puiHistogram[indexStart + b] / numValues; + double prob = m_d->m_puiHistogram[indexStart + b] / numValues; entropy -= prob * log2( prob ); } @@ -1290,15 +1228,14 @@ bool CalypFrame::toMat( cv::Mat& cvMat, bool convertToGray, bool scale, unsigned bool bRet = false; #ifdef USE_OPENCV if( convertToGray && - !( d->m_pcPelFormat->colorSpace == CLP_COLOR_YUV || d->m_pcPelFormat->colorSpace == CLP_COLOR_GRAY ) ) + !( m_d->m_pcPelFormat->colorSpace == CLP_COLOR_YUV || m_d->m_pcPelFormat->colorSpace == CLP_COLOR_GRAY ) ) { return bRet; } auto numBytes = getBitsPel() > kNumBitsInByte ? 2 : 1; auto numChannels = getNumberChannels(); - double scaleFactor = 1 << ( numBytes * kNumBitsInByte - getBitsPel() ); - if( scale == 0 ) - scaleFactor = 1; + double scaleFactor = 1UL << ( numBytes * kNumBitsInByte - getBitsPel() ); + if( scale == 0 ) scaleFactor = 1; if( convertToGray ) { @@ -1347,12 +1284,14 @@ bool CalypFrame::toMat( cv::Mat& cvMat, bool convertToGray, bool scale, unsigned else { unsigned char* cv_data = cvMat.data; - ClpPel* pel = getPelBufferYUV()[channel][0]; + auto* pel = getPelBufferYUV()[channel][0]; for( unsigned y = 0; y < imgHeight * imgWidth; y++ ) { - ClpPel currPel = static_cast( *pel++ * scaleFactor ); + auto currPel = static_cast( *pel++ * scaleFactor ); for( auto b = 0; b < numBytes; b++ ) + { *cv_data++ = currPel >> ( kNumBitsInByte * b ); + } } } bRet = true; @@ -1360,50 +1299,54 @@ bool CalypFrame::toMat( cv::Mat& cvMat, bool convertToGray, bool scale, unsigned return bRet; } -bool CalypFrame::fromMat( cv::Mat& cvMat, int channel ) +auto CalypFrame::fromMat( cv::Mat& cvMat, int channel ) -> bool { bool bRet = false; #ifdef USE_OPENCV - unsigned numBytes = getBitsPel() > kNumBitsInByte ? 2 : 1; - unsigned numChannels = getNumberChannels(); - unsigned int cvPrecision = getBitsPel() > kNumBitsInByte ? CV_16U : CV_8U; - if( !d->m_bInit ) + auto numBytes = getBitsPel() > kNumBitsInByte ? 2 : 1; + auto numChannels = getNumberChannels(); + auto cvPrecision = getBitsPel() > kNumBitsInByte ? CV_16U : CV_8U; + if( !m_d->m_bInit ) { uchar depth = cvMat.type() & CV_MAT_DEPTH_MASK; - if( d->m_iPixelFormat == ClpPixelFormats::Invalid ) + if( m_d->m_iPixelFormat == ClpPixelFormats::Invalid ) { switch( cvMat.channels() ) { case 1: - d->m_iPixelFormat = ClpPixelFormats::Gray; + m_d->m_iPixelFormat = ClpPixelFormats::Gray; break; case 3: - d->m_iPixelFormat = ClpPixelFormats::BGR24; + m_d->m_iPixelFormat = ClpPixelFormats::BGR24; break; default: assert( false ); return false; } } - d->m_uiBitsPel = depth == CV_8U ? kNumBitsInByte : kMinBitsPerPixel * 2; - d->init( cvMat.cols, cvMat.rows, d->m_iPixelFormat, d->m_uiBitsPel ); + m_d->m_uiBitsPel = depth == CV_8U ? kNumBitsInByte : kMinBitsPerPixel * 2; + m_d->init( cvMat.cols, cvMat.rows, m_d->m_iPixelFormat, m_d->m_uiBitsPel ); } - d->m_bHasRGBPel = false; - d->m_bHasHistogram = false; + m_d->m_bHasRGBPel = false; + m_d->m_bHasHistogram = false; if( channel >= 0 ) + { numChannels = 1; + } else + { channel = 0; + } - if( cvMat.channels() != int( numChannels ) ) + if( cvMat.channels() != static_cast( numChannels ) ) { return false; } - unsigned imgWidth = getWidth( channel ); - unsigned imgHeight = getHeight( channel ); + auto imgWidth = getWidth( channel ); + auto imgHeight = getHeight( channel ); if( numChannels > 1 ) { @@ -1414,8 +1357,8 @@ bool CalypFrame::fromMat( cv::Mat& cvMat, int channel ) cv::cvtColor( tmpMat, cvMat, cv::COLOR_RGB2YCrCb ); break; } - unsigned char* cv_data = tmpMat.data; - unsigned int cv_step = tmpMat.step; + auto* cv_data = tmpMat.data; + auto cv_step = tmpMat.step; CalypPixel currPel; for( unsigned int y = 0; y < imgHeight; y++ ) { @@ -1436,13 +1379,15 @@ bool CalypFrame::fromMat( cv::Mat& cvMat, int channel ) } else { - unsigned char* cv_data = cvMat.data; + auto* cv_data = cvMat.data; ClpPel* pel = getPelBufferYUV()[channel][0]; for( unsigned int y = 0; y < imgHeight * imgWidth; y++ ) { ClpPel curr_pel{ 0 }; for( unsigned b = 0; b < numBytes; b++ ) + { curr_pel += ( *cv_data++ ) << ( kNumBitsInByte * b ); + } *pel++ = curr_pel; } } @@ -1478,13 +1423,10 @@ std::vector CalypFrame::supportedQualityMetricsUnitsList() }; } -double CalypFrame::getQuality( int Metric, CalypFrame* Org, unsigned int component ) +double CalypFrame::getQuality( int metric, CalypFrame* Org, unsigned int component ) { - if( component >= getNumberChannels() ) - { - return 0; - } - switch( Metric ) + if( component >= getNumberChannels() ) return 0; + switch( metric ) { case PSNR_METRIC: return getPSNR( Org, component ); @@ -1504,11 +1446,11 @@ double CalypFrame::getQuality( int Metric, CalypFrame* Org, unsigned int compone return 0; } -double CalypFrame::getMSE( CalypFrame* Org, unsigned int component ) +auto CalypFrame::getMSE( CalypFrame* org, unsigned int component ) -> double { ClpPel* pPelYUV = getPelBufferYUV()[component][0]; - ClpPel* pOrgPelYUV = Org->getPelBufferYUV()[component][0]; - std::uint64_t numberOfPixels = Org->getHeight( component ) * Org->getWidth( component ); + ClpPel* pOrgPelYUV = org->getPelBufferYUV()[component][0]; + std::uint64_t numberOfPixels = org->getHeight( component ) * org->getWidth( component ); std::uint64_t ssd = 0; for( unsigned int i = 0; i < numberOfPixels; i++ ) { @@ -1522,18 +1464,17 @@ double CalypFrame::getMSE( CalypFrame* Org, unsigned int component ) return double( ssd ) / double( numberOfPixels ); } -double CalypFrame::getPSNR( CalypFrame* Org, unsigned int component ) +auto CalypFrame::getPSNR( CalypFrame* org, unsigned int component ) -> double { - std::uint64_t uiMaxValue = ( 1 << Org->getBitsPel() ) - 1; + std::uint64_t uiMaxValue = ( 1UL << org->getBitsPel() ) - 1; double dPSNR = 100; - double dMSE = getMSE( Org, component ); - if( dMSE != 0 ) - dPSNR = 10 * log10( double( uiMaxValue * uiMaxValue ) / dMSE ); + double dMSE = getMSE( org, component ); + if( dMSE != 0 ) dPSNR = 10 * log10( double( uiMaxValue * uiMaxValue ) / dMSE ); return dPSNR; } -float compute_ssim( ClpPel** refImg, ClpPel** encImg, int width, int height, int win_width, int win_height, - int max_pel_value_comp, int overlapSize ) +auto compute_ssim( ClpPel** refImg, ClpPel** encImg, int width, int height, int win_width, int win_height, + int max_pel_value_comp, int overlapSize ) -> float { static const float K1 = 0.01f, K2 = 0.03f; float max_pix_value_sqd; @@ -1599,26 +1540,26 @@ float compute_ssim( ClpPel** refImg, ClpPel** encImg, int width, int height, int return cur_distortion; } -double CalypFrame::getSSIM( CalypFrame* Org, unsigned int component ) +auto CalypFrame::getSSIM( CalypFrame* org, unsigned int component ) -> double { double dSSIM = 1; if( component == CLP_LUMA ) { - dSSIM = compute_ssim( d->m_pppcInputPel[component], Org->getPelBufferYUV()[component], d->m_uiWidth, d->m_uiHeight, - 8, 8, 255, 8 ); + dSSIM = compute_ssim( m_d->m_pppcInputPel[component], org->getPelBufferYUV()[component], m_d->m_uiWidth, + m_d->m_uiHeight, 8, 8, 255, 8 ); } else { - dSSIM = compute_ssim( d->m_pppcInputPel[component], Org->getPelBufferYUV()[component], getWidth( component ), + dSSIM = compute_ssim( m_d->m_pppcInputPel[component], org->getPelBufferYUV()[component], getWidth( component ), getHeight( component ), 4, 4, 255, 4 ); } return dSSIM; } -double CalypFrame::getWSPNR( CalypFrame* Org, unsigned int component ) +auto CalypFrame::getWSPNR( CalypFrame* org, unsigned int component ) -> double { ClpPel* pPelYUV = getPelBufferYUV()[component][0]; - ClpPel* pOrgPelYUV = Org->getPelBufferYUV()[component][0]; + ClpPel* pOrgPelYUV = org->getPelBufferYUV()[component][0]; unsigned height = getHeight( component ); unsigned width = getWidth( component ); @@ -1626,18 +1567,17 @@ double CalypFrame::getWSPNR( CalypFrame* Org, unsigned int component ) double weight_sum = 0; for( unsigned y = 0; y < height; y++ ) + { for( unsigned x = 0; x < width; x++ ) { - int diff = int( *pPelYUV++ ) - int( *pOrgPelYUV++ ); - double weight = cos( double( ( y + 0.5 - height / 2.0 ) * S_PI / height ) ); - ssd += (double)( diff * diff * 1000 ) * weight; + int diff = static_cast( *pPelYUV++ ) - static_cast( *pOrgPelYUV++ ); + double weight = std::cos( ( ( y + 0.5 - height / 2.0 ) * S_PI / height ) ); + ssd += static_cast( diff * diff * 1000 ) * weight; weight_sum += weight * 1000; } - - if( ssd == 0.0 ) - { - return 100.00; } - unsigned long uiMaxValue = ( 1 << Org->getBitsPel() ) - 1; - return 10 * log10( double( uiMaxValue * uiMaxValue ) * weight_sum / ssd ); + + if( ssd == 0.0 ) return 100.00; + auto uiMaxValue = ( 1UL << org->getBitsPel() ) - 1; + return 10 * std::log10( static_cast( uiMaxValue * uiMaxValue ) * weight_sum / ssd ); } diff --git a/lib/CalypFrame.h b/lib/CalypFrame.h index f3739bc..3184145 100644 --- a/lib/CalypFrame.h +++ b/lib/CalypFrame.h @@ -38,7 +38,7 @@ namespace cv { class Mat; -} +} // namespace cv using ClpPel = std::uint16_t; using ClpByte = std::uint8_t; @@ -100,7 +100,10 @@ enum CLP_Endianness CLP_LITTLE_ENDIAN = 1, }; -#define CHROMASHIFT( SIZE, SHIFT ) (unsigned int)( -( ( -( (int)( SIZE ) ) ) >> ( SHIFT ) ) ) +constexpr auto CHROMASHIFT( unsigned int size, unsigned int shift ) -> unsigned int +{ + return -( ( -( static_cast( size ) ) ) >> ( shift ) ); // NOLINT +} /** * \class CalypPixel @@ -118,9 +121,9 @@ public: static constexpr std::size_t getMaxNumberOfComponents() { return kMaxNumberOfComponents; } CalypPixel() = default; - CalypPixel( const int CalypColorSpace, const ClpPel c0 = 0 ); - CalypPixel( const int CalypColorSpace, const ClpPel c0, const ClpPel c1, const ClpPel c2 ); - CalypPixel( const int CalypColorSpace, const ClpPel c0, const ClpPel c1, const ClpPel c2, const ClpPel c3 ); + explicit CalypPixel( int calypColorSpace, ClpPel c0 = 0 ); + CalypPixel( int calypColorSpace, ClpPel c0, ClpPel c1, ClpPel c2 ); + CalypPixel( int calypColorSpace, ClpPel c0, ClpPel c1, ClpPel c2, ClpPel c3 ); CalypPixel( const CalypPixel& other ) noexcept = default; CalypPixel( CalypPixel&& other ) noexcept = default; auto operator=( const CalypPixel& other ) -> CalypPixel& = default; @@ -132,20 +135,20 @@ public: auto components() const -> const std::array& { return m_pelComp; }; auto components() -> std::array& { return m_pelComp; }; - ClpPel operator[]( const std::size_t channel ) const { return m_pelComp[channel]; } - ClpPel& operator[]( const std::size_t channel ) { return m_pelComp[channel]; } + auto operator[]( const std::size_t channel ) const -> ClpPel { return m_pelComp[channel]; } + auto operator[]( const std::size_t channel ) -> ClpPel& { return m_pelComp[channel]; } - CalypPixel& operator+=( const CalypPixel& ); - CalypPixel& operator-=( const CalypPixel& ); - CalypPixel& operator*=( const double ); + auto operator+=( const CalypPixel& ) -> CalypPixel&; + auto operator-=( const CalypPixel& ) -> CalypPixel&; + auto operator*=( double ) -> CalypPixel&; - CalypPixel operator+( const CalypPixel& ) const; - CalypPixel operator-( const CalypPixel& ) const; - CalypPixel operator*( const double ) const; + auto operator+( const CalypPixel& ) const -> CalypPixel; + auto operator-( const CalypPixel& ) const -> CalypPixel; + auto operator*( double ) const -> CalypPixel; auto operator==( const CalypPixel& other ) const -> bool; - friend std::ostream& operator<<( std::ostream& os, const CalypPixel& p ); + friend auto operator<<( std::ostream& os, const CalypPixel& p ) -> std::ostream&; /** * Convert a Pixel to a new color space @@ -153,10 +156,10 @@ public: * @param eOutputSpace output color space * @return converted pixel */ - CalypPixel convertPixel( CalypColorSpace eOutputSpace ) const; + auto convertPixel( CalypColorSpace eOutputSpace ) const -> CalypPixel; }; -std::ostream& operator<<( std::ostream& os, const CalypPixel& p ); +auto operator<<( std::ostream& stream, const CalypPixel& pixel ) -> std::ostream&; /** * \class CalypPlane @@ -222,11 +225,11 @@ public: */ static constexpr auto numberOfFormats() -> std::size_t; static auto findPixelFormat( const std::string& name ) -> std::optional; - static auto findPixelFormat( const std::string_view name ) -> std::optional; + static auto findPixelFormat( std::string_view name ) -> std::optional; static auto pelFormatColorSpace( ClpPixelFormats idx ) -> int; static auto supportedPixelFormatListNames() -> std::map; static auto supportedPixelFormatListNames( int colorSpace ) -> std::map; - static auto pixelFormatName( ClpPixelFormats idx ) -> const std::string_view; + static auto pixelFormatName( ClpPixelFormats idx ) -> std::string_view; /** * Creates a new frame using the following configuration @@ -249,21 +252,21 @@ public: * @note this function might misbehave if the pixel format enum is not correct */ CalypFrame( unsigned int width, unsigned int height, ClpPixelFormats pelFormat, unsigned bitsPixel, - bool has_negative_values ); + bool hasNegativeValues ); /** * Move contructor * * @param other existing frame to copy from */ - CalypFrame( CalypFrame&& other ) noexcept; + CalypFrame( CalypFrame&& other ) noexcept = default; /** * Move assignement contructor * * @param other existing frame to copy from */ - CalypFrame& operator=( CalypFrame&& other ) noexcept; + auto operator=( CalypFrame&& other ) noexcept -> CalypFrame& = default; /** * Copy contructor @@ -277,7 +280,7 @@ public: * * @param other existing frame to copy from */ - CalypFrame& operator=( const CalypFrame& other ); + auto operator=( const CalypFrame& other ) -> CalypFrame&; /** * Creates and new frame with the configuration of an existing one and copy @@ -314,14 +317,14 @@ public: * @param match matching conditions (use enum FormatMatching) * @return true if format matches */ - bool haveSameFmt( const CalypFrame& other, unsigned int match = MATCH_ALL ) const; - bool haveSameFmt( const CalypFrame* other, unsigned int match = MATCH_ALL ) const; + auto haveSameFmt( const CalypFrame& other, unsigned int match = MATCH_ALL ) const -> bool; + auto haveSameFmt( const CalypFrame* other, unsigned int match = MATCH_ALL ) const -> bool; /** * Get pixel format information * @return pixel format index */ - ClpPixelFormats getPelFormat() const; + auto getPelFormat() const -> ClpPixelFormats; /** * Get pixel format information @@ -333,91 +336,91 @@ public: * Get color space information * @return get color space index */ - int getColorSpace() const; + auto getColorSpace() const -> int; /** * Get the number of channels * @return number of channels */ - unsigned getNumberChannels() const; + auto getNumberChannels() const -> unsigned; /** * Get width of the frame * @param channel/component * @return number of pixels */ - unsigned int getWidth( unsigned channel = 0 ) const; + auto getWidth( unsigned channel = 0 ) const -> unsigned int; /** * Get height of the frame * @param channel/component * @return number of pixels */ - unsigned int getHeight( unsigned channel = 0 ) const; + auto getHeight( unsigned channel = 0 ) const -> unsigned int; /** * Check if frame has negative values * @return Boolean with the flag value */ - bool getHasNegativeValues() const; + auto getHasNegativeValues() const -> bool; /** * Get number of pixels of the frame * @param channel/component * @return number of pixels */ - std::uint64_t getPixels( unsigned channel = 0 ) const; + auto getPixels( unsigned channel = 0 ) const -> std::uint64_t; /** * Get the total number of pixels of the frame * @return number of pixels */ - std::uint64_t getTotalNumberOfPixels() const; + auto getTotalNumberOfPixels() const -> std::uint64_t; /** * Get chroma width ratio * @return ratio multiple of 2 */ - unsigned getChromaWidthRatio() const; + auto getChromaWidthRatio() const -> unsigned; /** * Get chroma height ratio * @return ratio multiple of 2 */ - unsigned getChromaHeightRatio() const; + auto getChromaHeightRatio() const -> unsigned; /** * Get number of pixels in each chroma channel * @return number of pixels */ - std::uint64_t getChromaLength() const; + auto getChromaLength() const -> std::uint64_t; /** * Get number of bits per pixel * @return number of bits */ - unsigned int getBitsPel() const; + auto getBitsPel() const -> unsigned int; /** * Get number of bytes per frame of an existing frame * @return number of bytes per frame */ - std::uint64_t getBytesPerFrame() const; + auto getBytesPerFrame() const -> std::uint64_t; /** * Get number of bytes per frame of a specific pixel format * @return number of bytes per frame */ - static std::uint64_t getBytesPerFrame( unsigned int uiWidth, unsigned int uiHeight, ClpPixelFormats pelFormat, - unsigned int bitsPixel ); + static auto getBytesPerFrame( unsigned int uiWidth, unsigned int uiHeight, ClpPixelFormats pelFormat, + unsigned int bitsPixel ) -> std::uint64_t; /** * Reset frame pixels to zero */ void reset(); - ClpPel*** getPelBufferYUV() const; - ClpPel*** getPelBufferYUV(); + auto getPelBufferYUV() const -> ClpPel***; + auto getPelBufferYUV() -> ClpPel***; auto getRGBBuffer() const -> std::optional>; @@ -429,7 +432,7 @@ public: * @param absolute whether it should return positive/negative values * @return pixel value */ - ClpPel operator()( unsigned int ch, unsigned int xPos, unsigned int yPos, bool absolute = true ) const; + auto operator()( unsigned int ch, unsigned int xPos, unsigned int yPos, bool absolute = true ) const -> ClpPel; /** * Get pixel value at coordinates @@ -437,7 +440,7 @@ public: * @param yPos position in Y axis * @return pixel value */ - CalypPixel operator()( unsigned int xPos, unsigned int yPos ) const; + auto operator()( unsigned int xPos, unsigned int yPos ) const -> CalypPixel; /** * Get pixel value at coordinates @@ -445,7 +448,7 @@ public: * @param yPos position in Y axis * @return pixel value */ - CalypPixel getPixel( unsigned int xPos, unsigned int yPos ) const; + auto getPixel( unsigned int xPos, unsigned int yPos ) const -> CalypPixel; /** * Get pixel value at coordinates @@ -455,7 +458,7 @@ public: * @param eColorSpace desired color space * @return pixel value */ - CalypPixel getPixel( unsigned int xPos, unsigned int yPos, CalypColorSpace eColorSpace ) const; + auto getPixel( unsigned int xPos, unsigned int yPos, CalypColorSpace eColorSpace ) const -> CalypPixel; /** * Set pixel value at coordinates to a given value @@ -491,7 +494,7 @@ public: void copyTo( const CalypFrame& other, unsigned x, unsigned y ) const; void copyTo( const CalypFrame* other, unsigned x, unsigned y ) const; - void frameFromBuffer( std::span, int iEndianness, unsigned long uiBuffSize ); + void frameFromBuffer( std::span, int iEndianness, std::uint64_t uiBuffSize ); void frameFromBuffer( std::span, int iEndianness ); void frameToBuffer( std::span, int iEndianness ) const; @@ -519,24 +522,24 @@ public: }; void calcHistogram(); - unsigned int getMinimumPelValue( unsigned channel ) const; - unsigned int getMaximumPelValue( unsigned channel ) const; + auto getMinimumPelValue( unsigned channel ) const -> unsigned int; + auto getMaximumPelValue( unsigned channel ) const -> unsigned int; - unsigned int getMaximum( unsigned channel ) const; - unsigned int getNumPixelsRange( unsigned channel, unsigned int start, unsigned int end ) const; - double getMean( unsigned channel, unsigned int start, unsigned int end ) const; - int getMedian( unsigned channel, unsigned int start, unsigned int end ) const; - double getStdDev( unsigned channel, unsigned int start, unsigned int end ) const; - double getHistogramValue( unsigned channel, unsigned int bin ) const; - unsigned int getNEBins( unsigned channel ) const; - int getNumHistogramSegment() const; - double getEntropy( unsigned channel, unsigned int start, unsigned int end ) const; + auto getMaximum( unsigned channel ) const -> unsigned int; + auto getNumPixelsRange( unsigned channel, unsigned int start, unsigned int end ) const -> unsigned int; + auto getMean( unsigned channel, unsigned int start, unsigned int end ) const -> double; + auto getMedian( unsigned channel, unsigned int start, unsigned int end ) const -> int; + auto getStdDev( unsigned channel, unsigned int start, unsigned int end ) const -> double; + auto getHistogramValue( unsigned channel, unsigned int bin ) const -> double; + auto getNEBins( unsigned channel ) const -> unsigned int; + auto getNumHistogramSegment() const -> int; + auto getEntropy( unsigned channel, unsigned int start, unsigned int end ) const -> double; /** * interface with OpenCV lib */ - bool toMat( cv::Mat& cvMat, bool convertToGray = false, bool scale = true, unsigned channel = 0 ) const; - bool fromMat( cv::Mat& cvMat, int iChannel = -1 ); + auto toMat( cv::Mat& cvMat, bool convertToGray = false, bool scale = true, unsigned channel = 0 ) const -> bool; + auto fromMat( cv::Mat& cvMat, int iChannel = -1 ) -> bool; /** * \ingroup CalypFrameGrp @@ -556,18 +559,18 @@ public: NUMBER_METRICS, }; - static std::vector supportedQualityMetricsList(); - static std::vector supportedQualityMetricsUnitsList(); - double getQuality( int Metric, CalypFrame* Org, unsigned int component ); - double getMSE( CalypFrame* Org, unsigned int component ); - double getPSNR( CalypFrame* Org, unsigned int component ); - double getSSIM( CalypFrame* Org, unsigned int component ); - double getWSPNR( CalypFrame* Org, unsigned int component ); + static auto supportedQualityMetricsList() -> std::vector; + static auto supportedQualityMetricsUnitsList() -> std::vector; + auto getQuality( int metric, CalypFrame* org, unsigned int component ) -> double; + auto getMSE( CalypFrame* org, unsigned int component ) -> double; + auto getPSNR( CalypFrame* org, unsigned int component ) -> double; + auto getSSIM( CalypFrame* org, unsigned int component ) -> double; + auto getWSPNR( CalypFrame* org, unsigned int component ) -> double; /** @} */ private: - class CalypFramePrivate; - std::unique_ptr d; + struct CalypFramePrivate; + std::unique_ptr m_d; }; #endif // __CALYPFRAME_H__ diff --git a/lib/CalypPixel.cpp b/lib/CalypPixel.cpp index a77e6fc..89de578 100644 --- a/lib/CalypPixel.cpp +++ b/lib/CalypPixel.cpp @@ -24,27 +24,23 @@ #include #include -#include -#include #include "CalypFrame.h" -#include "PixelFormats.h" -#include "config.h" namespace { - constexpr auto kMinPixelValue{ 0 }; constexpr auto kMaxPixelValue{ 255 }; -inline void yuvToRgb( int iY, int iU, int iV, int& iR, int& iG, int& iB ) +constexpr void yuvToRgb( int iY, int iU, int iV, int& iR, int& iG, int& iB ) { iR = std::clamp( iY + ( ( 1436 * ( iV - 128 ) ) >> 10 ), kMinPixelValue, kMaxPixelValue ); // NOLINT iG = std::clamp( iY - ( ( 352 * ( iU - 128 ) + 731 * ( iV - 128 ) ) >> 10 ), kMinPixelValue, kMaxPixelValue ); // NOLINT iB = std::clamp( iY + ( ( 1812 * ( iU - 128 ) ) >> 10 ), kMinPixelValue, kMaxPixelValue ); // NOLINT } -inline void rgbToYuv( int iR, int iG, int iB, int& iY, int& iU, int& iV ) + +constexpr void rgbToYuv( int iR, int iG, int iB, int& iY, int& iU, int& iV ) { iY = ( 299 * iR + 587 * iG + 114 * iB + 500 ) / 1000; // NOLINT iU = ( 1000 * ( iB - iY ) + 226816 ) / 1772; // NOLINT @@ -52,88 +48,83 @@ inline void rgbToYuv( int iR, int iG, int iB, int& iY, int& iU, int& iV ) } } // namespace -CalypPixel::CalypPixel( const int ColorSpace, const ClpPel c0 ) +CalypPixel::CalypPixel( int colorSpace, const ClpPel c0 ) : m_colorSpace( colorSpace ) { - m_colorSpace = ColorSpace; m_pelComp[0] = c0; } -CalypPixel::CalypPixel( const int ColorSpace, const ClpPel c0, const ClpPel c1, const ClpPel c2 ) +CalypPixel::CalypPixel( int colorSpace, const ClpPel c0, const ClpPel c1, const ClpPel c2 ) : m_colorSpace( colorSpace ) { - m_colorSpace = ColorSpace; m_pelComp[0] = c0; m_pelComp[1] = c1; m_pelComp[2] = c2; } -CalypPixel::CalypPixel( const int ColorSpace, const ClpPel c0, const ClpPel c1, const ClpPel c2, const ClpPel c3 ) +CalypPixel::CalypPixel( int colorSpace, const ClpPel c0, const ClpPel c1, const ClpPel c2, const ClpPel c3 ) + : m_colorSpace( colorSpace ) { - m_colorSpace = ColorSpace; m_pelComp[0] = c0; m_pelComp[1] = c1; m_pelComp[2] = c2; m_pelComp[3] = c3; } -CalypPixel& CalypPixel::operator+=( const CalypPixel& in ) +auto CalypPixel::operator+=( const CalypPixel& in ) -> CalypPixel& { assert( in.colorSpace() == m_colorSpace ); const auto& other_comp = in.components(); - std::transform( other_comp.begin(), other_comp.end(), m_pelComp.begin(), m_pelComp.begin(), - []( auto& p1, auto& p2 ) { return p1 + p2; } ); + std::ranges::transform( other_comp, m_pelComp, m_pelComp.begin(), []( auto& lhs, auto& rhs ) { return lhs + rhs; } ); return *this; } -CalypPixel& CalypPixel::operator-=( const CalypPixel& in ) +auto CalypPixel::operator-=( const CalypPixel& in ) -> CalypPixel& { assert( in.colorSpace() == m_colorSpace ); const auto& other_comp = in.components(); - std::transform( other_comp.begin(), other_comp.end(), m_pelComp.begin(), m_pelComp.begin(), - []( auto& p1, auto& p2 ) { return p1 - p2; } ); + std::ranges::transform( other_comp, m_pelComp, m_pelComp.begin(), []( auto& lhs, auto& rhs ) { return lhs - rhs; } ); return *this; } -CalypPixel& CalypPixel::operator*=( const double op ) +auto CalypPixel::operator*=( double op ) -> CalypPixel& { - std::transform( m_pelComp.begin(), m_pelComp.end(), m_pelComp.begin(), [&op]( auto& p1 ) { return p1 * op; } ); + std::ranges::transform( m_pelComp, m_pelComp.begin(), [&op]( auto& p1 ) { return p1 * op; } ); return *this; } -CalypPixel CalypPixel::operator+( const CalypPixel& in ) const +auto CalypPixel::operator+( const CalypPixel& in ) const -> CalypPixel { assert( in.colorSpace() == m_colorSpace ); const auto& other_comp = in.components(); CalypPixel result{ m_colorSpace }; - std::transform( other_comp.begin(), other_comp.end(), m_pelComp.begin(), result.components().begin(), - []( auto& p1, auto& p2 ) { return p1 + p2; } ); + std::ranges::transform( other_comp, m_pelComp, result.components().begin(), + []( auto& lhs, auto& rhs ) { return lhs + rhs; } ); return result; } -CalypPixel CalypPixel::operator-( const CalypPixel& in ) const +auto CalypPixel::operator-( const CalypPixel& input ) const -> CalypPixel { - assert( in.colorSpace() == m_colorSpace ); - const auto& other_comp = in.components(); + assert( input.colorSpace() == m_colorSpace ); + const auto& other_comp = input.components(); CalypPixel result{ m_colorSpace }; - std::transform( other_comp.begin(), other_comp.end(), m_pelComp.begin(), result.components().begin(), - []( auto& p1, auto& p2 ) { return p1 - p2; } ); + std::ranges::transform( other_comp, m_pelComp, result.components().begin(), + []( auto& lhs, auto& rhs ) { return lhs - rhs; } ); return result; } -CalypPixel CalypPixel::operator*( const double op ) const +auto CalypPixel::operator*( double op ) const -> CalypPixel { CalypPixel result{ m_colorSpace }; auto& comp = result.components(); - std::transform( comp.begin(), comp.end(), comp.begin(), [&op]( auto& p1 ) { return p1 * op; } ); + std::ranges::transform( comp, comp.begin(), [&op]( auto& p1 ) { return p1 * op; } ); return result; } auto CalypPixel::operator==( const CalypPixel& other ) const -> bool { - return colorSpace() == other.colorSpace() && - std::equal( m_pelComp.begin(), m_pelComp.end(), other.components().begin() ); + return colorSpace() == other.colorSpace() && std::ranges::equal( m_pelComp, other.components() ); } -std::ostream& operator<<( std::ostream& os, const CalypPixel& p ) +auto operator<<( std::ostream& stream, const CalypPixel& pixel ) -> std::ostream& { // #ifdef CLP_DEBUG // if( p.colorSpace() == CLP_COLOR_RGB || p.colorSpace() == CLP_COLOR_YUV ) @@ -145,18 +136,17 @@ std::ostream& operator<<( std::ostream& os, const CalypPixel& p ) // os << "(" << p[0] << ", " << p[1] << ", " << p[2] << ", " << p[3] << ")"; // } // #endif - return os; + return stream; } -CalypPixel CalypPixel::convertPixel( CalypColorSpace eOutputSpace ) const +auto CalypPixel::convertPixel( CalypColorSpace eOutputSpace ) const -> CalypPixel { - if( m_colorSpace == eOutputSpace ) - return *this; + if( m_colorSpace == eOutputSpace ) return *this; - int outA = 0; - int outB = 0; - int outC = 0; - int outD = 0; + int outA{ 0 }; + int outB{ 0 }; + int outC{ 0 }; + int outD{ 0 }; if( m_colorSpace == CLP_COLOR_YUV ) { switch( eOutputSpace ) @@ -189,5 +179,6 @@ CalypPixel CalypPixel::convertPixel( CalypColorSpace eOutputSpace ) const break; } } - return CalypPixel( eOutputSpace, outA, outB, outC, outD ); + return { eOutputSpace, static_cast( outA ), static_cast( outB ), static_cast( outC ), + static_cast( outD ) }; }