From 5be1bf8dc7d218368730ff09e597223bd36884d7 Mon Sep 17 00:00:00 2001 From: Dario Sassi Date: Mon, 13 Feb 2017 08:20:31 +0000 Subject: [PATCH] EgtGraphics 1.8b2 : - aggiunta in interfaccia funzione GetImage. --- EgtGraphics.rc | Bin 11604 -> 11604 bytes EgtGraphics.vcxproj | 1 + EgtGraphics.vcxproj.filters | 3 + Scene.h | 4 ++ SceneBasic.cpp | 11 +++- SceneImage.cpp | 123 ++++++++++++++++++++++++++++++++++++ SceneTexture.cpp | 2 +- 7 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 SceneImage.cpp diff --git a/EgtGraphics.rc b/EgtGraphics.rc index d1fbaac42a1c61b5ca64210dee1e9bcfbd09ea83..8b799b5c2a9cab95a83a17754a39c16f9168502e 100644 GIT binary patch delta 103 zcmcZ-btP)UFE&P_%?J7RGfm>rnVhGn#%MJ8ptSDhKaxI-o7=dpn8C8TKng4@BhrnVhGn#%MVCptSDhKaxI-o7=dpn8C8TKng4@Bh + diff --git a/EgtGraphics.vcxproj.filters b/EgtGraphics.vcxproj.filters index 47e21ec..2224580 100644 --- a/EgtGraphics.vcxproj.filters +++ b/EgtGraphics.vcxproj.filters @@ -134,6 +134,9 @@ File di origine + + File di origine + diff --git a/Scene.h b/Scene.h index 524676d..dea1be9 100644 --- a/Scene.h +++ b/Scene.h @@ -178,6 +178,9 @@ class Scene : public IEGrScene bool GetTexturePixels( const std::string& sName, int& nWidth, int& nHeight) const override ; bool GetTextureDimensions( const std::string& sName, double& dDimX, double& dDimY) const override ; bool ChangeTextureDimensions( const std::string& sName, double dDimX, double dDimY) override ; + // Image + bool GetImage( int nShowMode, bool bShowGrid, bool bShowFrame, Color colBackTop, Color colBackBottom, + int nDestWidth, int nDestHeight, const std::string& sFile) override ; public : // Basic @@ -202,6 +205,7 @@ class Scene : public IEGrScene bool CalcDimViewFromExtView( void) ; bool AdjustDimView( double dHalfWidth, double dHalfHeight) ; bool CalcClippingPlanesFromExtView( void) ; + bool MyDraw( bool bSwapBF) ; bool Background( void) ; bool Prepare( void) ; // Camera diff --git a/SceneBasic.cpp b/SceneBasic.cpp index ee1e8a2..e776f0e 100644 --- a/SceneBasic.cpp +++ b/SceneBasic.cpp @@ -683,6 +683,13 @@ Scene::Prepare( void) //---------------------------------------------------------------------------- bool Scene::Draw( void) +{ + return MyDraw( true) ; +} + +//---------------------------------------------------------------------------- +bool +Scene::MyDraw( bool bSwapBF) { // sistemo lo sfondo e lo Zbuffer if ( ! Background()) @@ -811,7 +818,7 @@ Scene::Draw( void) } // scambio i buffer back e front ( eseguita solo se previsti) - if ( ! SwapBuffers( m_hDC)) + if ( bSwapBF && ! SwapBuffers( m_hDC)) LOG_INFO( GetEGrLogger(), "Draw SwapBuffers error") return true ; @@ -877,7 +884,7 @@ Scene::Destroy( void) // cancellazione textures if ( ! m_TextMgr.Clear()) - LOG_ERROR( GetEGrLogger(), "Error uloading textures from OpenGl") ; + LOG_ERROR( GetEGrLogger(), "Error unloading textures from OpenGl") ; // cancellazione rendering context wglMakeCurrent( nullptr, nullptr) ; diff --git a/SceneImage.cpp b/SceneImage.cpp new file mode 100644 index 0000000..351ed62 --- /dev/null +++ b/SceneImage.cpp @@ -0,0 +1,123 @@ +//---------------------------------------------------------------------------- +// 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 ; +} diff --git a/SceneTexture.cpp b/SceneTexture.cpp index 7f94c0f..b6751bb 100644 --- a/SceneTexture.cpp +++ b/SceneTexture.cpp @@ -2,7 +2,7 @@ // EgalTech 2015-2015 //---------------------------------------------------------------------------- // File : SceneTexture.cpp Data : 27.09.15 Versione : 1.6i8 -// Contenuto : Implementazione della gestione texturesa della classe scena. +// Contenuto : Implementazione della gestione textures della classe scena. // // //