//---------------------------------------------------------------------------- // EgalTech 2017-2017 //---------------------------------------------------------------------------- // File : SceneImage.cpp Data : 11.02.17 Versione : 1.8b2 // Contenuto : Implementazione della gestione immagini della classe scena. // // // // Modifiche : 11.02.17 DS Creazione modulo. // // //---------------------------------------------------------------------------- //--------------------------- Include ---------------------------------------- #include "stdafx.h" #include "Scene.h" #include "\EgtDev\Include\EgtStringConverter.h" #include "\EgtDev\Extern\FreeImage\Include\FreeImage.h" using namespace std ; //---------------------------------------------------------------------------- bool Scene::GetImage( int nShowMode, bool bShowGrid, bool bShowFrame, Color colBackTop, Color colBackBottom, int nDestWidth, int nDestHeight, const string& sFile) { // Cerco e verifico il formato del file da scrivere a partire dall'estensione FREE_IMAGE_FORMAT fif = FreeImage_GetFIFFromFilenameU( stringtoW( sFile)) ; if ( fif == FIF_UNKNOWN || ! FreeImage_FIFSupportsWriting( fif)) return false ; // Dimensioni della vista int nWidth = m_nViewportW ; int nHeight = m_nViewportH ; if ( nWidth == 0 || nHeight == 0) return false ; // Coefficienti di scalatura if ( nDestWidth == 0 || nDestHeight == 0) return false ; double dCoeffW = double( nDestWidth) / nWidth ; double dCoeffH = double( nDestHeight) / nHeight ; // Alloco il buffer di lettura dei pixel della vista OpenGL ( fattore 4 perché RGBA) BYTE* pBuffer = new BYTE[ 4 * nWidth * nHeight] ; if ( pBuffer == nullptr) return false ; // Imposto attributi di visualizzazione int nCurrShowMode = m_nShowMode ; m_nShowMode = nShowMode ; bool bCurrShowGrid = m_bShowGrid ; m_bShowGrid = bShowGrid ; bool bCurrShowFrame = m_bShowFrame ; m_bShowFrame = bShowFrame ; Color colCurrBackTop = m_colBackTop ; Color colCurrBackBottom = m_colBackBottom ; m_colBackTop = colBackTop ; m_colBackBottom = colBackBottom ; // Imposto zoom double dCurrHalfWidth = m_dHalfWidth ; double dCurrHalfHeight = m_dHalfHeight ; double dCoeff = max( dCoeffW, dCoeffH) / min( dCoeffW, dCoeffH) ; m_dHalfWidth *= dCoeff ; m_dHalfHeight *= dCoeff ; // Eseguo disegno sul back buffer if ( ! MyDraw( false)) return false ; // Ripristino attributi di visualizzazione originali m_nShowMode = nCurrShowMode ; m_bShowGrid = bCurrShowGrid ; m_bShowFrame = bCurrShowFrame ; m_colBackBottom = colCurrBackBottom ; m_colBackTop = colCurrBackTop ; // Ripristino zoom m_dHalfWidth = dCurrHalfWidth ; m_dHalfHeight = dCurrHalfHeight ; // Leggo l'immagine nel buffer glReadBuffer( GL_BACK) ; glReadPixels( 0, 0, nWidth, nHeight, GL_BGRA, GL_UNSIGNED_BYTE, pBuffer) ; // Converto al formato di FreeImage FIBITMAP* pDib = FreeImage_ConvertFromRawBits( pBuffer, nWidth, nHeight, 4 * nWidth, 32, 0x0000FF00, 0xFF000000, 0x00FF0000, false) ; // converto l'immagine a 24 bit per pixel FIBITMAP* pTmpDib = pDib ; pDib = FreeImage_ConvertTo24Bits( pTmpDib) ; FreeImage_Unload( pTmpDib) ; // Eseguo scalatura della bitmap int nLeft ; int nTop ; int nRight ; int nBottom ; if ( dCoeffW >= dCoeffH) { nLeft = 0 ; nRight = nWidth - 1 ; int nDeltaH = nHeight - int( nDestHeight / dCoeffW) ; nTop = nDeltaH / 2 ; nBottom = nHeight - nTop ; } else { nTop = 0 ; nBottom = nHeight ; int nDeltaW = nWidth - int( nDestWidth / dCoeffH) ; nLeft = nDeltaW / 2 ; nRight = nWidth - nLeft ; } FIBITMAP* pTmp2Dib = pDib ; pDib = FreeImage_RescaleRect( pTmp2Dib, nDestWidth, nDestHeight, nLeft, nTop, nRight, nBottom, FILTER_BSPLINE) ; FreeImage_Unload( pTmp2Dib) ; // Salvataggio int nFlag = 0 ; bool bOk = ( FreeImage_SaveU( fif, pDib, stringtoW( sFile), nFlag) != FALSE) ; // Libero le risorse FreeImage_Unload( pDib) ; delete pBuffer ; return bOk ; }