/* This file is a part of plaYUVer project * Copyright (C) 2014-2017 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 StreamHandlerPortableMap.cpp * \brief Handling portable pixmap formats */ #include "StreamHandlerPortableMap.h" #include "LibMemory.h" #include "PlaYUVerFrame.h" #include "PlaYUVerFramePixelFormats.h" #include #include std::vector StreamHandlerPortableMap::supportedReadFormats() { INI_REGIST_PLAYUVER_SUPPORTED_FMT; REGIST_PLAYUVER_SUPPORTED_FMT( &StreamHandlerPortableMap::Create, "Portable BitMap ", "pbm" ); REGIST_PLAYUVER_SUPPORTED_FMT( &StreamHandlerPortableMap::Create, "Portable GrayMap ", "pgm" ); REGIST_PLAYUVER_SUPPORTED_FMT( &StreamHandlerPortableMap::Create, "Portable PixMap ", "ppm" ); END_REGIST_PLAYUVER_SUPPORTED_FMT; } std::vector StreamHandlerPortableMap::supportedWriteFormats() { INI_REGIST_PLAYUVER_SUPPORTED_FMT; REGIST_PLAYUVER_SUPPORTED_FMT( &StreamHandlerPortableMap::Create, "Portable BitMap ", "pbm" ); REGIST_PLAYUVER_SUPPORTED_FMT( &StreamHandlerPortableMap::Create, "Portable GrayMap ", "pgm" ); END_REGIST_PLAYUVER_SUPPORTED_FMT; } Bool StreamHandlerPortableMap::openHandler( String strFilename, Bool bInput ) { m_bIsInput = bInput; m_pFile = NULL; m_pFile = fopen( strFilename.c_str(), bInput ? "rb" : "wb" ); if( m_pFile == NULL ) { return false; } m_strFormatName = "PGM"; m_strCodecName = "Raw Video"; if( m_bIsInput ) { char line[101]; while( fgets( line, 100, m_pFile ) && line[0] == '#' ) ; sscanf( line, "P%d", &m_iMagicNumber ); while( fgets( line, 100, m_pFile ) && line[0] == '#' ) ; sscanf( line, "%u %u", &m_uiWidth, &m_uiHeight ); if( m_iMagicNumber == 1 || m_iMagicNumber == 4 ) { m_uiBitsPerPixel = 1; m_iMaxValue = 1; m_iPixelFormat = PlaYUVerFrame::GRAY; } else { while( fgets( line, 100, m_pFile ) && line[0] == '#' ) ; sscanf( line, "%d", &m_iMaxValue ); m_uiBitsPerPixel = log( m_iMaxValue + 1 ) / log( 2 ); m_iPixelFormat = m_iMagicNumber == 2 || m_iMagicNumber == 5 ? PlaYUVerFrame::GRAY : PlaYUVerFrame::RGB24; } } else { m_iMaxValue = ( 1 << m_uiBitsPerPixel ) - 1; if( m_uiBitsPerPixel == 1 ) { m_iMagicNumber = 4; } else if( m_iPixelFormat == PlaYUVerFrame::GRAY ) { m_iMagicNumber = 5; } else if( m_iPixelFormat == PlaYUVerFrame::RGB24 ) { m_iMagicNumber = 6; } else { closeHandler(); return false; } } m_iEndianness = 0; m_dFrameRate = 1; return true; } Void StreamHandlerPortableMap::closeHandler() { if( m_pFile ) fclose( m_pFile ); if( m_pStreamBuffer ) freeMem1D( m_pStreamBuffer ); } Bool StreamHandlerPortableMap::configureBuffer( PlaYUVerFrame* pcFrame ) { return getMem1D( &m_pStreamBuffer, pcFrame->getBytesPerFrame() ); } UInt64 StreamHandlerPortableMap::calculateFrameNumber() { return 1; } Bool StreamHandlerPortableMap::seek( UInt64 iFrameNum ) { // m_uiCurrFrameFileIdx = return true; } Bool StreamHandlerPortableMap::read( PlaYUVerFrame* pcFrame ) { UInt64 processed_bytes = fread( m_pStreamBuffer, sizeof( Byte ), m_uiNBytesPerFrame, m_pFile ); if( processed_bytes != m_uiNBytesPerFrame ) return false; pcFrame->frameFromBuffer( m_pStreamBuffer, PLAYUVER_BIG_ENDIAN ); m_uiCurrFrameFileIdx++; return true; } Bool StreamHandlerPortableMap::write( PlaYUVerFrame* 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, PLAYUVER_BIG_ENDIAN ); UInt64 processed_bytes = fwrite( m_pStreamBuffer, sizeof( Byte ), m_uiNBytesPerFrame, m_pFile ); if( processed_bytes != m_uiNBytesPerFrame ) return false; return true; }