From a528b686ecd5d1c938a752a5f5a7786f137ba1e3 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Mon, 19 May 2014 00:16:30 +0100 Subject: [PATCH] Add support to copy only a region of a PlaYUVerFrame and implement histogram from the selection --- source/app/PropertiesSidebar.cpp | 35 +++++---- source/app/PropertiesSidebar.h | 1 - source/app/plaYUVerApp.cpp | 11 +-- source/lib/PlaYUVerFrame.cpp | 124 +++++++++++++++++++++++-------- source/lib/PlaYUVerFrame.h | 21 ++---- 5 files changed, 120 insertions(+), 72 deletions(-) diff --git a/source/app/PropertiesSidebar.cpp b/source/app/PropertiesSidebar.cpp index 4822d61..9ceafeb 100755 --- a/source/app/PropertiesSidebar.cpp +++ b/source/app/PropertiesSidebar.cpp @@ -171,7 +171,6 @@ FramePropertiesSideBar::FramePropertiesSideBar( QWidget* parent, Bool *pbIsPlayi { // -------------- Variables definition -------------- m_pcFrame = NULL; - m_pcFrameSelection = NULL; m_iLastFrameType = -1; m_pbIsPlaying = pbIsPlaying; @@ -484,11 +483,9 @@ Void FramePropertiesSideBar::setSelection( const QRect &selectionArea ) if( selectionArea.isValid() ) { - histogramWidget->stopHistogramComputation(); - - m_pcFrameSelection->copyFrom( m_pcFrame ); - histogramWidget->updateSelectionData( m_pcFrameSelection ); + //histogramWidget->updateSelectionData( new PlaYUVerFrame(m_pcFrame selectionArea)); + histogramWidget->updateSelectionData( new PlaYUVerFrame(m_pcFrame)); fullImageButton->show(); selectionImageButton->show(); selectionImageButton->click(); @@ -547,23 +544,26 @@ Void FramePropertiesSideBar::updateStatistiques() else histogram = histogramWidget->m_selectionHistogram; - double mean = histogram->getMean( channel, min, max ); - labelMeanValue->setText( value.setNum( mean, 'f', 1 ) ); + if( histogram ) + { + double mean = histogram->getMean( channel, min, max ); + labelMeanValue->setText( value.setNum( mean, 'f', 1 ) ); - double pixels = histogram->getPixels(); - labelPixelsValue->setText( value.setNum( ( float )pixels, 'f', 0 ) ); + double pixels = histogram->getPixels(); + labelPixelsValue->setText( value.setNum( ( float )pixels, 'f', 0 ) ); - double stddev = histogram->getStdDev( channel, min, max ); - labelStdDevValue->setText( value.setNum( stddev, 'f', 1 ) ); + double stddev = histogram->getStdDev( channel, min, max ); + labelStdDevValue->setText( value.setNum( stddev, 'f', 1 ) ); - double counts = histogram->getCount( channel, min, max ); - labelCountValue->setText( value.setNum( ( float )counts, 'f', 0 ) ); + double counts = histogram->getCount( channel, min, max ); + labelCountValue->setText( value.setNum( ( float )counts, 'f', 0 ) ); - double median = histogram->getMedian( channel, min, max ); - labelMedianValue->setText( value.setNum( median, 'f', 1 ) ); + double median = histogram->getMedian( channel, min, max ); + labelMedianValue->setText( value.setNum( median, 'f', 1 ) ); - double percentile = ( pixels > 0 ? ( 100.0 * counts / pixels ) : 0.0 ); - labelPercentileValue->setText( value.setNum( percentile, 'f', 1 ) ); + double percentile = ( pixels > 0 ? ( 100.0 * counts / pixels ) : 0.0 ); + labelPercentileValue->setText( value.setNum( percentile, 'f', 1 ) ); + } } Void FramePropertiesSideBar::stopHistogram() @@ -601,7 +601,6 @@ Void FramePropertiesSideBar::slotRefreshOptions( bool /*depth*/) Void FramePropertiesSideBar::slotHistogramComputationFailed() { m_pcFrame = NULL; - m_pcFrameSelection = NULL; } Void FramePropertiesSideBar::slotChannelChanged( Int channel ) diff --git a/source/app/PropertiesSidebar.h b/source/app/PropertiesSidebar.h index 08ebc1d..f35e5c5 100755 --- a/source/app/PropertiesSidebar.h +++ b/source/app/PropertiesSidebar.h @@ -94,7 +94,6 @@ private: }; PlaYUVerFrame *m_pcFrame; - PlaYUVerFrame *m_pcFrameSelection; Bool* m_pbIsPlaying; Int m_iLastFrameType; diff --git a/source/app/plaYUVerApp.cpp b/source/app/plaYUVerApp.cpp index c894b0f..e7891cb 100644 --- a/source/app/plaYUVerApp.cpp +++ b/source/app/plaYUVerApp.cpp @@ -158,8 +158,8 @@ Void plaYUVerApp::loadFile( QString fileName ) interfaceChild->show(); connect( interfaceChild->getViewArea(), SIGNAL( positionChanged(const QPoint &, PlaYUVerFrame *) ), this, SLOT( updatePixelValueStatusBar(const QPoint &, PlaYUVerFrame *) ) ); -// connect( interfaceChild->getViewArea(), SIGNAL( selectionChanged( QRect ) ), this, -// SLOT( updatePixelValueStatusBar(const QPoint &, PlaYUVerFrame *) ) ); + connect( interfaceChild->getViewArea(), SIGNAL( selectionChanged( QRect ) ), this, + SLOT( updatePropertiesSelectedArea( QRect ) ) ); interfaceChild->zoomToFit(); interfaceChild->getViewArea()->setTool( m_appTool ); @@ -282,12 +282,7 @@ Void plaYUVerApp::updatePropertiesSelectedArea( QRect area ) { if( m_pcCurrentSubWindow ) { - if( area.isNull() ) - { - m_pcFrameProperties->setData( m_pcCurrentSubWindow->getInputStream()->getCurrFrame() ); - m_pcFrameProperties->setSelection( area ); - } - else + if( area.isValid() ) { m_pcFrameProperties->setSelection( area ); } diff --git a/source/lib/PlaYUVerFrame.cpp b/source/lib/PlaYUVerFrame.cpp index fb7e2eb..b272f4b 100755 --- a/source/lib/PlaYUVerFrame.cpp +++ b/source/lib/PlaYUVerFrame.cpp @@ -41,6 +41,12 @@ PlaYUVerFrame::PlaYUVerFrame( PlaYUVerFrame *other ) copyFrom( other ); } +PlaYUVerFrame::PlaYUVerFrame( PlaYUVerFrame *other, QRect area ) +{ + init( area.width(), area.height(), other->getPelFormat() ); + copyFrom( other, area.x(), area.y() ); +} + PlaYUVerFrame::~PlaYUVerFrame() { if( m_pppcInputPel ) @@ -357,12 +363,66 @@ Void PlaYUVerFrame::FrameFromBuffer( Pel *input_buffer, Int pel_format ) } Void PlaYUVerFrame::copyFrom( PlaYUVerFrame* input_frame ) +{ + if( m_iPixelFormat != input_frame->getPelFormat() && m_uiWidth == input_frame->getWidth() && m_uiHeight == input_frame->getHeight() ) + return; + m_bHasRGBPel = false; + memcpy( *m_pppcInputPel[LUMA], input_frame->getPelBufferYUV()[LUMA][0], getBytesPerFrame() * sizeof(Pel) ); +} + +Void PlaYUVerFrame::copyFrom( PlaYUVerFrame* input_frame, UInt xPos, UInt yPos ) { if( m_iPixelFormat != input_frame->getPelFormat() ) return; m_bHasRGBPel = false; - UInt64 num_bytes = getBytesPerFrame(); - memcpy( *m_pppcInputPel[LUMA], *( input_frame->getPelBufferYUV() )[LUMA], num_bytes * sizeof(Pel) ); + + switch( m_iPixelFormat ) + { + case YUV420p: + for( UInt i = 0; i < m_uiHeight / 2; i++ ) + { + memcpy( m_pppcInputPel[LUMA][0], &( input_frame->getPelBufferYUV()[LUMA][yPos + ( i << 1 )][xPos] ), m_uiWidth * sizeof(Pel) ); + memcpy( m_pppcInputPel[LUMA][0], &( input_frame->getPelBufferYUV()[LUMA][yPos + ( i << 1 ) + 1][xPos] ), m_uiWidth * sizeof(Pel) ); + memcpy( m_pppcInputPel[CHROMA_U][0], &( input_frame->getPelBufferYUV()[CHROMA_U][yPos / 2 + i][xPos / 2] ), m_uiWidth / 2 * sizeof(Pel) ); + memcpy( m_pppcInputPel[CHROMA_V][0], &( input_frame->getPelBufferYUV()[CHROMA_V][yPos / 2 + i][xPos / 2] ), m_uiWidth / 2 * sizeof(Pel) ); + } + break; + case YUV444p: + for( UInt i = 0; i < m_uiHeight; i++ ) + { + memcpy( m_pppcInputPel[LUMA][0], &( input_frame->getPelBufferYUV()[LUMA][yPos + i][xPos] ), m_uiWidth * sizeof(Pel) ); + memcpy( m_pppcInputPel[CHROMA_U][0], &( input_frame->getPelBufferYUV()[CHROMA_U][yPos + i][xPos] ), m_uiWidth * sizeof(Pel) ); + memcpy( m_pppcInputPel[CHROMA_V][0], &( input_frame->getPelBufferYUV()[CHROMA_V][yPos + i][xPos] ), m_uiWidth * sizeof(Pel) ); + } + break; + case YUV422p: + case YUYV422: + for( UInt i = 0; i < m_uiHeight / 2; i++ ) + { + memcpy( m_pppcInputPel[LUMA][0], &( input_frame->getPelBufferYUV()[LUMA][yPos + ( i << 1 )][xPos] ), m_uiWidth * sizeof(Pel) ); + memcpy( m_pppcInputPel[LUMA][0], &( input_frame->getPelBufferYUV()[LUMA][yPos + ( i << 1 ) + 1][xPos] ), m_uiWidth * sizeof(Pel) ); + memcpy( m_pppcInputPel[CHROMA_U][0], &( input_frame->getPelBufferYUV()[CHROMA_U][yPos + i][xPos / 2] ), m_uiWidth / 2 * sizeof(Pel) ); + memcpy( m_pppcInputPel[CHROMA_V][0], &( input_frame->getPelBufferYUV()[CHROMA_V][yPos + i][xPos / 2] ), m_uiWidth / 2 * sizeof(Pel) ); + } + break; + case GRAY: + for( UInt i = 0; i < m_uiHeight; i++ ) + { + memcpy( m_pppcInputPel[LUMA][0], &( input_frame->getPelBufferYUV()[LUMA][yPos + i][xPos] ), m_uiWidth * sizeof(Pel) ); + } + break; + case RGB8: + for( UInt i = 0; i < m_uiHeight; i++ ) + { + memcpy( m_pppcInputPel[COLOR_R][0], &( input_frame->getPelBufferYUV()[LUMA][yPos + i][xPos] ), m_uiWidth * sizeof(Pel) ); + memcpy( m_pppcInputPel[COLOR_G][0], &( input_frame->getPelBufferYUV()[CHROMA_U][yPos + i][xPos] ), m_uiWidth * sizeof(Pel) ); + memcpy( m_pppcInputPel[COLOR_B][0], &( input_frame->getPelBufferYUV()[CHROMA_V][yPos + i][xPos] ), m_uiWidth * sizeof(Pel) ); + } + break; + default: + Q_ASSERT( 0 ); + } + } UInt64 PlaYUVerFrame::getBytesPerFrame() @@ -417,36 +477,6 @@ UInt PlaYUVerFrame::getChromaLength() const return 0; } -Pixel PlaYUVerFrame::ConvertPixel( Pixel sInputPixel, Int eOutputSpace ) -{ - Int outA, outB, outC; - Pixel sOutputPixel = - { - COLOR_INVALID, - 0, - 0, - 0 }; - - if( sInputPixel.color_space == eOutputSpace ) - return sInputPixel; - - if( eOutputSpace == COLOR_RGB ) - { - yuvToRgb( sInputPixel.Luma, sInputPixel.ChromaU, sInputPixel.ChromaV, outA, outB, outC ); - sOutputPixel.ColorR = outA; - sOutputPixel.ColorG = outB; - sOutputPixel.ColorB = outC; - } - if( eOutputSpace == COLOR_YUV ) - { - rgbToYuv( sInputPixel.ColorR, sInputPixel.ColorG, sInputPixel.ColorB, outA, outB, outC ); - sOutputPixel.Luma = outA; - sOutputPixel.ChromaU = outB; - sOutputPixel.ChromaV = outC; - } - return sOutputPixel; -} - Pixel PlaYUVerFrame::getPixelValue( const QPoint &pos, Int color_space ) { Pixel pixel_value; @@ -489,6 +519,36 @@ Pixel PlaYUVerFrame::getPixelValue( const QPoint &pos, Int color_space ) return pixel_value; } +Pixel PlaYUVerFrame::ConvertPixel( Pixel sInputPixel, Int eOutputSpace ) +{ + Int outA, outB, outC; + Pixel sOutputPixel = + { + COLOR_INVALID, + 0, + 0, + 0 }; + + if( sInputPixel.color_space == eOutputSpace ) + return sInputPixel; + + if( eOutputSpace == COLOR_RGB ) + { + yuvToRgb( sInputPixel.Luma, sInputPixel.ChromaU, sInputPixel.ChromaV, outA, outB, outC ); + sOutputPixel.ColorR = outA; + sOutputPixel.ColorG = outB; + sOutputPixel.ColorB = outC; + } + if( eOutputSpace == COLOR_YUV ) + { + rgbToYuv( sInputPixel.ColorR, sInputPixel.ColorG, sInputPixel.ColorB, outA, outB, outC ); + sOutputPixel.Luma = outA; + sOutputPixel.ChromaU = outB; + sOutputPixel.ChromaV = outC; + } + return sOutputPixel; +} + // Format conversion #ifdef USE_OPENCV diff --git a/source/lib/PlaYUVerFrame.h b/source/lib/PlaYUVerFrame.h index d47cf6d..2105d61 100755 --- a/source/lib/PlaYUVerFrame.h +++ b/source/lib/PlaYUVerFrame.h @@ -44,20 +44,12 @@ class QImage; namespace plaYUVer { -static const QString gInputStreamFormatsNames[] = -{ - "YUV420p", - "YUV444p", - "YUV422p", - "YUV411p", - "YUV410p", - "GRAY" }; - class PlaYUVerFrame { public: PlaYUVerFrame( UInt width = 0, UInt height = 0, Int pel_format = 0 ); PlaYUVerFrame( PlaYUVerFrame *other ); + PlaYUVerFrame( PlaYUVerFrame *other, QRect area ); ~PlaYUVerFrame(); enum ColorSpace @@ -117,11 +109,13 @@ public: Void FrametoRGB8(); - Void FrameFromBuffer( Pel *input_buffer, Int pel_format ); + Void FrameFromBuffer( Pel*, Int ); Void copyFrom( PlaYUVerFrame* ); + Void copyFrom( PlaYUVerFrame*, UInt, UInt); UInt64 getBytesPerFrame(); + UInt getChromaLength() const; Pel*** getPelBufferYUV() const @@ -135,17 +129,18 @@ public: return m_pppcInputPel; } - static Pixel ConvertPixel( Pixel sInputPixel, Int eOutputSpace ); - Pixel getPixelValue( const QPoint &pos, Int color ); + Pixel getPixelValue( const QPoint&, Int ); UChar* getQImageBuffer() const { return m_pcRGBPelInterlaced; } + static Pixel ConvertPixel( Pixel, Int ); + #ifdef USE_OPENCV cv::Mat getCvMat(); - Void copyFrom( cv::Mat* input_frame ); + Void copyFrom( cv::Mat* ); #endif UInt getWidth() const