diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 72eb6d6..f8771e9 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -17,6 +17,11 @@ IF( USE_FFMPEG ) LIST( APPEND plaYUver_Lib_SRCS LibAvContextHandle.cpp ) ENDIF() +IF( USE_OPENCV ) + LIST( APPEND plaYUver_Lib_SRCS LibOpenCVHandler.cpp ) +ENDIF() + + set(plaYUver_Lib_HEADERS PlaYUVerFrame.h PlaYUVerStream.h diff --git a/lib/LibOpenCVHandler.cpp b/lib/LibOpenCVHandler.cpp new file mode 100644 index 0000000..05c72af --- /dev/null +++ b/lib/LibOpenCVHandler.cpp @@ -0,0 +1,143 @@ +/* This file is a part of plaYUVer project + * Copyright (C) 2014-2015 by Luis Lucas (luisfrlucas@gmail.com) + * Joao Carreira (jfmcarreira@gmail.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/** + * \file LibOpenCVHandler.cpp + * \brief Interface with opencv lib + */ + +#include +#include "LibOpenCVHandler.h" +#include "PlaYUVerFrame.h" +#include "PlaYUVerFramePixelFormats.h" +#include "LibMemory.h" + +namespace plaYUVer +{ + +std::vector LibOpenCVHandler::supportedReadFormatsExt() +{ + std::vector formatsExt; + formatsExt.push_back( "pgm" ); + formatsExt.push_back( "png" ); + formatsExt.push_back( "bmp" ); + formatsExt.push_back( "jpg" ); + return formatsExt; +} + +std::vector LibOpenCVHandler::supportedReadFormatsName() +{ + std::vector formatsName; + formatsName.push_back( "Portable Grayscale Map" ); + formatsName.push_back( "Portable Network Graphics" ); + formatsName.push_back( "Windows Bitmap" ); + formatsName.push_back( "Joint Photographic Experts Group" ); + return formatsName; +} + +std::vector LibOpenCVHandler::supportedWriteFormatsExt() +{ + std::vector formatsExt; + return formatsExt; +} + +std::vector LibOpenCVHandler::supportedWriteFormatsName() +{ + std::vector formatsName; + return formatsName; +} + +std::vector LibOpenCVHandler::supportedSaveFormatsExt() +{ + std::vector formatsExt; + formatsExt.push_back( "bmp" ); + formatsExt.push_back( "bmp" ); + formatsExt.push_back( "jpg" ); + formatsExt.push_back( "png" ); + return formatsExt; +} + +std::vector LibOpenCVHandler::supportedSaveFormatsName() +{ + std::vector formatsName; + formatsName.push_back( "Windows Bitmap" ); + formatsName.push_back( "Windows Bitmap" ); + formatsName.push_back( "Joint Photographic Experts Group" ); + formatsName.push_back( "Portable Network Graphics" ); + return formatsName; +} + +Bool LibOpenCVHandler::loadFrame( PlaYUVerFrame* pcFrame, std::string filename ) +{ + cv::Mat pcCvFrame = cv::imread( filename ); + + std::string pelFormatName; + Int iPelFormat; + switch( pcCvFrame.channels() ) + { + case 1: + pelFormatName = "GRAY"; + break; + case 4: + pelFormatName = "RGB8"; + break; + default: + return false; + } + + for( UInt i = 0; i < PlaYUVerFrame::supportedPixelFormatListNames().size(); i++ ) + { + if( PlaYUVerFrame::supportedPixelFormatListNames()[i] == pelFormatName ) + { + iPelFormat = i; + break; + } + } + return true; +} + +Bool LibOpenCVHandler::saveFrame( const PlaYUVerFrame* pcFrame, std::string filename ) +{ + + Int cvType = CV_8UC3; + switch( pcFrame->getNumberChannels() ) + { + case 3: + cvType = CV_8UC4; + break; + case 1: + cvType = CV_8UC1; + break; + default: + return false; + break; + } + cv::Mat pcCvFrame( pcFrame->getHeight(), pcFrame->getWidth(), cvType ); + if( pcFrame->getColorSpace() != PlaYUVerFrame::COLOR_GRAY ) + { + memcpy( pcCvFrame.data, pcFrame->getRGBBuffer(), pcFrame->getHeight() * pcFrame->getWidth() * 4 * sizeof(UChar) ); + } + else + { + memcpy( pcCvFrame.data, pcFrame->getPelBufferYUV()[LUMA][0], pcFrame->getHeight() * pcFrame->getWidth() * sizeof(Pel) ); + } + return cv::imwrite( filename, pcCvFrame ); +} + +} // NAMESPACE diff --git a/lib/LibOpenCVHandler.h b/lib/LibOpenCVHandler.h new file mode 100644 index 0000000..2b155b8 --- /dev/null +++ b/lib/LibOpenCVHandler.h @@ -0,0 +1,63 @@ +/* This file is a part of plaYUVer project + * Copyright (C) 2014-2015 by Luis Lucas (luisfrlucas@gmail.com) + * Joao Carreira (jfmcarreira@gmail.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/** + * \file LibOpenCVHandler.h + * \brief Interface with opencv lib + */ + +#ifndef __LIBOPENCVHANDLER_H__ +#define __LIBOPENCVHANDLER_H__ + +#include +#include +#include +#include +#include "PlaYUVerDefs.h" + +namespace plaYUVer +{ + +class PlaYUVerFrame; + +class LibOpenCVHandler +{ +public: + + static std::vector supportedReadFormatsExt(); + static std::vector supportedReadFormatsName(); + static std::vector supportedWriteFormatsExt(); + static std::vector supportedWriteFormatsName(); + static std::vector supportedSaveFormatsExt(); + static std::vector supportedSaveFormatsName(); + + LibOpenCVHandler() + { + } + + static Bool loadFrame( PlaYUVerFrame* pcFrame, std::string filename ); + static Bool saveFrame( const PlaYUVerFrame* pcFrame, std::string filename ); + +private: + std::string m_cFilename; +}; + +} // NAMESPACE + +#endif // __LIBOPENCVHANDLER_H__ diff --git a/lib/PlaYUVerFrame.cpp b/lib/PlaYUVerFrame.cpp index 6a229a0..0e4889a 100644 --- a/lib/PlaYUVerFrame.cpp +++ b/lib/PlaYUVerFrame.cpp @@ -32,7 +32,7 @@ #include "LibAvContextHandle.h" #endif #ifdef USE_OPENCV -#include +#include "LibOpenCVHandler.h" #endif #ifdef USE_PIXFC #include "pixfc-sse.h" @@ -324,25 +324,19 @@ Void PlaYUVerFrame::getCvMat( Void** ppCvFrame ) { #ifdef USE_OPENCV Int cvType = CV_8UC3; - switch( m_iPixelFormat ) + switch( m_pcPelFormat->numberChannels ) { - case YUV420p: - case YUV444p: - case YUV422p: - case YUYV422: - cvType = CV_8UC4; - break; - case GRAY: - cvType = CV_8UC1; - break; - case RGB8: + case 3: cvType = CV_8UC3; break; + case 1: + cvType = CV_8UC1; + break; default: break; } cv::Mat *pcCvFrame = new cv::Mat( m_uiHeight, m_uiWidth, cvType ); - if( m_iPixelFormat != GRAY ) + if( m_pcPelFormat->colorSpace != PlaYUVerFrame::COLOR_GRAY ) { fillRGBBuffer(); memcpy( pcCvFrame->data, m_pcRGB32, m_uiWidth * m_uiHeight * 4 * sizeof(UChar) ); diff --git a/lib/PlaYUVerFrame.h b/lib/PlaYUVerFrame.h index d0562a4..b767312 100644 --- a/lib/PlaYUVerFrame.h +++ b/lib/PlaYUVerFrame.h @@ -55,7 +55,7 @@ public: } Pixel( Int ColorSpace, Pel c0, Pel c1, Pel c2 ) { - m_iColorSpace = ColorSpace == PlaYUVerFrame::GRAY ? PlaYUVerFrame::COLOR_YUV : ColorSpace; + m_iColorSpace = ColorSpace == PlaYUVerFrame::COLOR_GRAY ? PlaYUVerFrame::COLOR_YUV : ColorSpace; PixelComponents[0] = c0; PixelComponents[1] = c1; PixelComponents[2] = c2; diff --git a/lib/PlaYUVerStream.cpp b/lib/PlaYUVerStream.cpp index e374e2f..d17dd2a 100644 --- a/lib/PlaYUVerStream.cpp +++ b/lib/PlaYUVerStream.cpp @@ -31,7 +31,7 @@ #include "LibAvContextHandle.h" #endif #ifdef USE_OPENCV -#include +#include "LibOpenCVHandler.h" #endif namespace plaYUVer @@ -41,8 +41,14 @@ std::vector PlaYUVerStream::supportedReadFormatsExt() { std::vector formatsExt; formatsExt.push_back( "yuv" ); +#ifdef USE_FFMPEG std::vector libAvFmt = LibAvContextHandle::supportedReadFormatsExt(); formatsExt.insert( formatsExt.end(), libAvFmt.begin(), libAvFmt.end() ); +#endif +#ifdef USE_OPENCV + std::vector opencvFmt = LibOpenCVHandler::supportedReadFormatsExt(); + formatsExt.insert( formatsExt.end(), opencvFmt.begin(), opencvFmt.end() ); +#endif return formatsExt; } @@ -50,8 +56,14 @@ std::vector PlaYUVerStream::supportedReadFormatsName() { std::vector formatsName; formatsName.push_back( "Raw video" ); +#ifdef USE_FFMPEG std::vector libAvFmt = LibAvContextHandle::supportedReadFormatsName(); formatsName.insert( formatsName.end(), libAvFmt.begin(), libAvFmt.end() ); +#endif +#ifdef USE_OPENCV + std::vector opencvFmt = LibOpenCVHandler::supportedReadFormatsName(); + formatsName.insert( formatsName.end(), opencvFmt.begin(), opencvFmt.end() ); +#endif return formatsName; } @@ -74,10 +86,10 @@ std::vector PlaYUVerStream::supportedSaveFormatsExt() std::vector formatsExt; std::vector writeExt = supportedWriteFormatsExt(); formatsExt.insert( formatsExt.begin(), writeExt.begin(), writeExt.end() ); - formatsExt.push_back( "bmp" ); - formatsExt.push_back( "bmp" ); - formatsExt.push_back( "jpeg" ); - formatsExt.push_back( "png" ); +#ifdef USE_OPENCV + std::vector opencvFmt = LibOpenCVHandler::supportedSaveFormatsExt(); + formatsExt.insert( formatsExt.end(), opencvFmt.begin(), opencvFmt.end() ); +#endif sort( formatsExt.begin(), formatsExt.end() ); formatsExt.erase( unique( formatsExt.begin(), formatsExt.end() ), formatsExt.end() ); return formatsExt; @@ -88,10 +100,10 @@ std::vector PlaYUVerStream::supportedSaveFormatsName() std::vector formatsName; std::vector writeName = supportedWriteFormatsName(); formatsName.insert( formatsName.begin(), writeName.begin(), writeName.end() ); - formatsName.push_back( "Windows Bitmap" ); - formatsName.push_back( "Windows Bitmap" ); - formatsName.push_back( "Joint Photographic Experts Group" ); - formatsName.push_back( "Portable Network Graphics" ); +#ifdef USE_OPENCV + std::vector opencvFmt = LibOpenCVHandler::supportedSaveFormatsName(); + formatsName.insert( formatsName.end(), opencvFmt.begin(), opencvFmt.end() ); +#endif sort( formatsName.begin(), formatsName.end() ); formatsName.erase( unique( formatsName.begin(), formatsName.end() ), formatsName.end() ); return formatsName; @@ -224,6 +236,12 @@ Bool PlaYUVerStream::open( std::string filename, UInt width, UInt height, Int in { m_uiStreamHandler = YUV_IO; } +//#ifdef USE_OPENCV +// else if ( m_cFormatName == "png" ) +// { +// m_uiStreamHandler = OPENCV_HANDLER; +// } +//#endif #ifdef USE_FFMPEG else { @@ -481,7 +499,7 @@ Void PlaYUVerStream::readFrame() if( !m_bInit || !m_bIsInput || m_bLoadAll ) return; - if( m_uiCurrFrameFileIdx >= Int64( m_uiTotalFrameNum ) ) + if( m_uiCurrFrameFileIdx >= m_uiTotalFrameNum ) { m_pcNextFrame = NULL; return; @@ -591,11 +609,8 @@ Bool PlaYUVerStream::saveFrame( const std::string& filename, PlaYUVerFrame *save #ifdef USE_OPENCV else { - cv::Mat* image; - saveFrame->getCvMat( ( Void** )&image ); - return cv::imwrite( filename, *image ); + return LibOpenCVHandler::saveFrame( saveFrame, filename ); } - #endif } return false; diff --git a/lib/PlaYUVerStream.h b/lib/PlaYUVerStream.h index f675dc2..841beb6 100644 --- a/lib/PlaYUVerStream.h +++ b/lib/PlaYUVerStream.h @@ -35,6 +35,7 @@ namespace plaYUVer { class LibAvContextHandle; +class LibOpenCVHandler; typedef struct { @@ -57,6 +58,7 @@ private: { INVALID_HANDLER = -1, YUV_IO, + OPENCV_HANDLER, FFMPEG, TOTAL_HANDLERR };