diff --git a/lib/CalypFrame.cpp b/lib/CalypFrame.cpp index a370890..f7008d7 100644 --- a/lib/CalypFrame.cpp +++ b/lib/CalypFrame.cpp @@ -81,6 +81,11 @@ int CalypFrame::findPixelFormat( const ClpString& name ) return -1; } +int CalypFrame::pelformatColorSpace( const int idx ) +{ + return g_CalypPixFmtDescriptorsMap.at( idx ).colorSpace; +} + struct CalypFramePrivate { public: @@ -346,6 +351,8 @@ bool CalypFrame::haveSameFmt( const CalypFrame& other, unsigned int match ) cons 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() ); return bRet; } @@ -423,7 +430,7 @@ unsigned int CalypFrame::getBitsPel() const return d->m_uiBitsPel; } -ClpULong CalypFrame::getBytesPerFrame() +ClpULong CalypFrame::getBytesPerFrame() const { return getBytesPerFrame( d->m_uiWidth, d->m_uiHeight, d->m_iPixelFormat, d->m_uiBitsPel ); } @@ -432,10 +439,10 @@ ClpULong CalypFrame::getBytesPerFrame( unsigned int uiWidth, unsigned int uiHeig { const CalypPixelFormatDescriptor* pcPelFormat = &( g_CalypPixFmtDescriptorsMap.at( iPixelFormat ) ); unsigned int bytesPerPixel = ( bitsPixel - 1 ) / 8 + 1; - unsigned long long int numberBytes = uiWidth * uiHeight; + ClpULong numberBytes = uiWidth * uiHeight; if( pcPelFormat->numberChannels > 1 ) { - unsigned long long int numberBytesChroma = + ClpULong numberBytesChroma = CHROMASHIFT( uiWidth, pcPelFormat->log2ChromaWidth ) * CHROMASHIFT( uiHeight, pcPelFormat->log2ChromaHeight ); numberBytes += ( pcPelFormat->numberChannels - 1 ) * numberBytesChroma; } @@ -524,11 +531,11 @@ void CalypFrame::setPixel( unsigned int xPos, unsigned int yPos, CalypPixel pixe void CalypFrame::copyFrom( const CalypFrame& other ) { - if( !haveSameFmt( other, MATCH_ALL ) ) + 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[CLP_LUMA][0][0]), &(other.getPelBufferYUV()[CLP_LUMA][0][0]), getTotalNumberOfPixels() * sizeof( ClpPel ) ); + memcpy( &( d->m_pppcInputPel[0][0][0]), &(other.getPelBufferYUV()[0][0][0]), getTotalNumberOfPixels() * sizeof( ClpPel ) ); } void CalypFrame::copyFrom( const CalypFrame* other ) @@ -539,7 +546,7 @@ void CalypFrame::copyFrom( const CalypFrame* other ) void CalypFrame::copyFrom( const CalypFrame& other, unsigned int xPos, unsigned int yPos ) { - if( !haveSameFmt( other, MATCH_COLOR_SPACE | MATCH_PEL_FMT | MATCH_BITS ) ) + if( !haveSameFmt( other, MATCH_COLOR_SPACE | MATCH_BYTES_PER_FRAME | MATCH_BITS ) ) return; // TODO: Protect width and height ClpPel*** pInput = other.getPelBufferYUV(); @@ -699,7 +706,7 @@ void CalypFrame::frameToBuffer( ClpByte* output_buffer, int iEndianness ) step = ( d->m_pcPelFormat->comp[ch].step_minus1 ) * bytesPixel; pTmpPel = d->m_pppcInputPel[ch][0]; - pTmpBuff = ppBuff[d->m_pcPelFormat->comp[ch].plane]; + pTmpBuff = ppBuff[d->m_pcPelFormat->comp[ch].plane] + ( d->m_pcPelFormat->comp[ch].offset_plus1 - 1 ) * bytesPixel; for( i = 0; i < CHROMASHIFT( d->m_uiHeight, ratioH ) * CHROMASHIFT( d->m_uiWidth, ratioW ); i++ ) { diff --git a/lib/CalypFrame.h b/lib/CalypFrame.h index cdcb2b8..7036f19 100644 --- a/lib/CalypFrame.h +++ b/lib/CalypFrame.h @@ -100,6 +100,7 @@ public: static std::vector supportedPixelFormatListNames( int colorSpace ); static int numberOfFormats(); static int findPixelFormat( const ClpString& name ); + static int pelformatColorSpace( const int idx ); /** * Creates a new frame using the following configuration @@ -145,6 +146,7 @@ public: MATCH_PEL_FMT = 4, MATCH_BITS = 8, MATCH_COLOR_SPACE_IGNORE_GRAY = 16, + MATCH_BYTES_PER_FRAME = 32, MATCH_ALL = 0xFFFF, }; @@ -236,7 +238,7 @@ public: * Get number of bytes per frame of an existing frame * @return number of bytes per frame */ - ClpULong getBytesPerFrame(); + ClpULong getBytesPerFrame() const; /** * Get number of bytes per frame of a specific pixel format diff --git a/lib/StreamHandlerPortableMap.cpp b/lib/StreamHandlerPortableMap.cpp index 717c1df..20d092c 100644 --- a/lib/StreamHandlerPortableMap.cpp +++ b/lib/StreamHandlerPortableMap.cpp @@ -86,18 +86,20 @@ bool StreamHandlerPortableMap::openHandler( ClpString strFilename, bool bInput ) } else { + int colorSpace = CalypFrame::pelformatColorSpace( m_iPixelFormat ); m_iMaxValue = ( 1 << m_uiBitsPerPixel ) - 1; if( m_uiBitsPerPixel == 1 ) { m_iMagicNumber = 4; } - else if( m_iPixelFormat == CLP_GRAY ) + else if( colorSpace == CLP_COLOR_GRAY ) { m_iMagicNumber = 5; } - else if( m_iPixelFormat == CLP_RGB24 ) + else if( colorSpace == CLP_COLOR_RGB ) { m_iMagicNumber = 6; + m_iPixelFormat = CalypFrame::findPixelFormat("RGB"); } else { @@ -105,7 +107,7 @@ bool StreamHandlerPortableMap::openHandler( ClpString strFilename, bool bInput ) return false; } } - m_iEndianness = 0; + m_iEndianness = CLP_BIG_ENDIAN; m_dFrameRate = 1; m_uiTotalNumberFrames = 1; return true; @@ -143,13 +145,16 @@ bool StreamHandlerPortableMap::read( CalypFrame* pcFrame ) bool StreamHandlerPortableMap::write( CalypFrame* pcFrame ) { + CalypFrame pcRGBFrame( pcFrame->getWidth(), pcFrame->getHeight(), + m_iPixelFormat, pcFrame->getBitsPel() ); + pcRGBFrame.copyFrom( pcFrame ); fseek( m_pFile, 0, SEEK_SET ); fprintf( m_pFile, "P%d\n%d %d\n", m_iMagicNumber, m_uiWidth, m_uiHeight ); if( m_iMagicNumber > 4 ) { fprintf( m_pFile, "%d\n", m_iMaxValue ); } - pcFrame->frameToBuffer( m_pStreamBuffer, CLP_BIG_ENDIAN ); + pcRGBFrame.frameToBuffer( m_pStreamBuffer, m_iEndianness ); unsigned long long int processed_bytes = fwrite( m_pStreamBuffer, sizeof( ClpByte ), m_uiNBytesPerFrame, m_pFile ); if( processed_bytes != m_uiNBytesPerFrame ) return false;