Filter Library Camera Interface Physics

ScanFilterClad.cpp

00001 /*
00002  * ScanFilterClad.cpp - clad filter.
00003  *
00004  * This file is part of the Camera Filter Library.
00005  * Computer Aided Measurement Environment for Realtime Atomic imaging (Camera)
00006  *
00007  * Copyright (C) 2004-2005, Leiden Probe Microscopy.
00008  * Copyright (C) 2004-2005, Universiteit Leiden.
00009  *
00010  * Authors: M. Seynen (original), Martin J. Moene
00011  *
00012  * $Id: ScanFilterClad.cpp 358 2006-03-06 14:49:02Z moene $
00013  */
00014 
00015 /*
00016  * configuration defines:
00017  */
00018 
00019 // currently nothing
00020 
00021 /*
00022  * end of configuration defines.
00023  */
00024 
00025 #include "stdafx.h"                     // for common (pre-compiled) headers
00026 //#include <cfl/stdafx.h>                 // for common (pre-compiled) headers
00027 #include <cfl/resource.h>               // for messages and other resources
00028 
00029 #include <Camera/InterfaceDll.h>        // Camera--Filter Library interface
00030 //#include <Camera/CameraGeneralDefines.h>// for g_d2PI
00031 
00032 #include <cfl/ScanFilterClad.h>         // this filter's header
00033 #include <cfl/FilterDlg_Clad.h>         // this filter's dialog
00034 
00035 #ifdef _DEBUG
00036 #undef THIS_FILE
00037 static char THIS_FILE[]=__FILE__;
00038 #define new DEBUG_NEW
00039 #endif
00040 
00041 #define CFL_SCHEMAVERSION_FILTERCLAD 0  // permanent storage schema version
00042                                         // this filter's name
00043 LPCTSTR CScanFilterClad::m_lpcsFilterName = _T("Clad");
00044                                         // this filter's short name
00045 LPCTSTR CScanFilterClad::m_lpcsShortFilterName = CScanFilterClad::m_lpcsFilterName;
00046                                         // serialization
00047 IMPLEMENT_SERIAL( CScanFilterClad, CScanFilter, CFL_SCHEMAVERSION_FILTERCLAD )
00048 
00049 /*
00050  * the default new X-size.
00051  */
00052 const CScanFilterClad::SizeElementType CScanFilterClad::def_nWidth = 220;
00053 
00054 /*
00055  * the default new Y-size.
00056  */
00057 const CScanFilterClad::SizeElementType CScanFilterClad::def_nHeight = 120;
00058 
00059 /*
00060  * constructor.
00061  */
00062 CScanFilterClad::CScanFilterClad() :
00063    m_cSize( CSize( def_nWidth, def_nHeight ) )
00064 {
00065 //   Q_LOG( _T("CScanFilterClad::CScanFilterClad()") );
00066 
00067    m_csFilterName = m_lpcsFilterName;
00068 
00069    ReadFilterSettings();
00070 }
00071 
00072 /*
00073  * destructor.
00074  */
00075 CScanFilterClad::~CScanFilterClad()
00076 {
00077 //   Q_LOG( _T("CScanFilterClad::~CScanFilterClad()") );
00078 
00079    ; // do nothing
00080 }
00081 
00082 /*
00083  * store or retrieve the object's settings;
00084  * see also CScanFilterNull::Serialize().
00085  */
00086 void CScanFilterClad::Serialize( CArchive& ar )
00087 {
00088 //   Q_LOG( _T("CScanFilterClad::Serialize()") );
00089 
00090    CScanFilter::Serialize( ar );
00091 
00092    if ( ar.IsStoring() )
00093    {
00094       ar << m_cSize.cx << m_cSize.cy;
00095    }
00096    else
00097    {
00098       ar >> m_cSize.cx >> m_cSize.cy;
00099    }
00100 };
00101 
00102 /*
00103  * \brief configure filter with settings as provided by the application on the
00104  * filterlist window (e.g. via registry); see also CScanFilterNull::ReadFilterSettings().
00105  */
00106 void CScanFilterClad::ReadFilterSettings()
00107 {
00108 //   Q_LOG( _T("CScanFilterClad::ReadFilterSettings()") );
00109 
00110    CWinApp* pApp = AfxGetApp();
00111 
00112    if ( Q_INVALID( NULL == pApp ) )
00113       return ;
00114 
00115    /*
00116     * parameter string:
00117     */
00118    SetParameters( pApp->GetProfileString( gCflRegistrySubkey, m_lpcsFilterName, _T( "220,120" ) ) );
00119 }
00120 
00121 /*
00122  * \brief save filter settings e.g. for use with the filterlist window (e.g. via registry);
00123  * see also ReadFilterSettings().
00124  */
00125 void CScanFilterClad::WriteFilterSettings() const
00126 {
00127 //   Q_LOG( _T("CScanFilterClad::WriteFilterSettings()") );
00128 
00129    CWinApp* pApp = AfxGetApp();
00130 
00131    if ( Q_INVALID( NULL == pApp ) )
00132       return ;
00133 
00134    /*
00135     * parameter string:
00136     */
00137    LPCTSTR pStr = GetParameters();
00138 
00139    if ( pStr )
00140    {
00141       pApp->WriteProfileString( gCflRegistrySubkey, m_lpcsFilterName, pStr );
00142       delete const_cast<LPTSTR>( pStr );
00143    }
00144 }
00145 
00146 /*
00147  * provide current filter parameter(s) for filter-scripting capture
00148  * in the main application;
00149  * see also CScanFilterNull::GetParameters().
00150  */
00151 LPCTSTR CScanFilterClad::GetParameters() const
00152 {
00153 //   Q_LOG( _T("CScanFilterClad::GetInitParameters()") );
00154 
00155    /*
00156     * format parameters and return a c-string on the heap:
00157     */
00158    CString csParameters;
00159 
00160    csParameters.Format( _T( "%d,%d" ), m_cSize.cx, m_cSize.cy );
00161 
00162    return strcpy( new TCHAR[ csParameters.GetLength() + 1 ], csParameters );
00163 }
00164 
00165 /*
00166  * set parameters for filter-script execution of filter;
00167  * see also CScanFilterNull::SetParameters().
00168  */
00169 BOOL CScanFilterClad::SetParameters( LPCTSTR lpParameters )
00170 {
00171 //   Q_LOG( _T("CScanFilterDZ::SetParameters()") );
00172 
00173    if ( Q_INVALID( NULL == lpParameters &&
00174            "CScanFilterClad (SetParameters): parameters expected, none provided (NULL)." ) )
00175    {
00176       return FALSE;
00177    }
00178 
00179    /*
00180     * set defaults, read parameter string with factor and optional offset:
00181     */
00182    m_cSize = SizeType( def_nWidth, def_nHeight );
00183 
00184    int nfields = _stscanf( lpParameters, _T( "%d,%d" ), &m_cSize.cx, &m_cSize.cy );
00185 
00186    if ( Q_INVALID( 2 > nfields && "CScanFilterClad (SetParameters): x- and y-size expected, got none." ) )
00187    {
00188       return FALSE;
00189    }
00190 
00191    return TRUE;
00192 }
00193 
00194 /*
00195  * create filter dialog and pre Apply() filter to view result;
00196  * see also CScanFilterNull::RunModeless().
00197  */
00198 BOOL CScanFilterClad::RunModeless( CWnd* pParentWnd, CDocument* pDoc )
00199 {
00200 //   Q_LOG( _T("CScanFilterClad::RunModeless()") );
00201 
00202    CFilterDlg_CladPtr pDlg = new CFilterDlg_Clad();
00203 
00204    if ( Q_INVALID( NULL == pDlg ) )
00205       return FALSE;
00206 
00207    pDlg->SetParameters( this, pDoc );   // (void) method
00208 
00209    /*
00210     * Create returns non-zero if dialog was created and initialized succesfully.
00211     * Note: no pDlg->DestroyWindow() must be done.
00212     */
00213    pDlg->Create( IDD_FILTERDLG_CLAD, pParentWnd );
00214 
00215    m_pDlg = pDlg;
00216 
00217    BOOL bRet = Apply();
00218 
00219    if ( bRet )
00220    {
00221       pDlg->UpdateView();
00222    }
00223 
00224    return bRet;
00225 }
00226 
00227 #pragma warning( push )
00228 #pragma warning( disable : 4100 )       // unreferenced formal parameter
00229 
00230 /**
00231  * the supported dragmethod(s).
00232  */
00233 DragSupportType CScanFilterClad::SupportDragInput() const 
00234 { 
00235    return SFDS_RECT; 
00236 }
00237 
00238 /*
00239  * apply the filter to the selected area.
00240  */
00241 void CScanFilterClad::SetDragInput( BOOL bL2R, CRect rcIn )
00242 {
00243 //   Q_LOG( _T("CScanFilterClad::SetDragInput()") );
00244 
00245    /* 
00246     * Note: use NormalizeRect() because otherwise width and height of rcIn may wel be negative;
00247     * minimally retain image size.
00248     */
00249    rcIn.NormalizeRect();
00250    SetWidth ( max( rcIn.Width() , static_cast< int >( m_lpsbIn->Columns() ) ) );
00251    SetHeight( max( rcIn.Height(), static_cast< int >( m_lpsbIn->Rows   () ) ) );
00252 
00253    if ( IsInteractive() )
00254    {
00255       m_pDlg->SetDlgItemInt( IDC_CLAD_SIZEX, GetWidth() );
00256       m_pDlg->SetDlgItemInt( IDC_CLAD_SIZEY, GetHeight() );
00257    }
00258 
00259   if ( Q_FAILED( Apply() ) )
00260    {
00261       return ;
00262    }
00263 
00264    CFilterDlg_CladPtr pDlg = dynamic_cast<CFilterDlg_CladPtr>( m_pDlg );
00265 
00266    if ( Q_ASSERT( NULL != pDlg ) )
00267    {
00268       pDlg->UpdateView();
00269    }
00270 }
00271 
00272 #pragma warning( pop )
00273 
00274 /*
00275  * check parameters, take care of a properly sized output buffer,
00276  * set its name and copy filter parameters and process;
00277  * see also CScanFilterNull::ApplyCore().
00278  */
00279 BOOL CScanFilterClad::Apply()
00280 {
00281 //   Q_LOG( _T("CScanFilterClad::Apply()") );
00282 
00283    if ( Q_INVALID( NULL == m_lpsbIn ) )
00284       return FALSE;
00285 
00286    if ( Q_INVALID( NULL == m_lpsbOut ) )
00287       return FALSE;
00288 
00289    /*
00290     * minimally retain image size:
00291     */
00292    SetWidth ( max( GetWidth() , m_lpsbIn->Columns() ) );
00293    SetHeight( max( GetHeight(), m_lpsbIn->Rows   () ) );
00294 
00295    /*
00296     * extra number of elements to allocate in data buffer:
00297     */
00298    SizeElementType extra = GetWidth() * GetHeight();
00299 
00300    /*
00301     * create output buffer:
00302     */
00303    Q_RETURN( m_lpsbOut->CreateOutputBufferFor( *m_lpsbIn, extra ) );
00304 
00305    /*
00306     * resize data buffer to requested size; also for first-time to show correct view:
00307     */
00308    Q_RETURN( m_lpsbOut->ResizeDataBuffer( GetSize(), extra ) );
00309 
00310    m_lpsbOut->m_csBufferName.Format( _T("%s - %s %d,%d"), 
00311       m_lpsbIn->m_csBufferName, m_lpcsShortFilterName, GetWidth(), GetHeight() );
00312 
00313    if ( IsInteractive() )
00314    {
00315       m_pDlg->SetDlgItemText( IDC_BUFFEROUTNAME, m_lpsbOut->m_csBufferName );
00316    }
00317 
00318    /*
00319     * process first part (L2R):
00320     */
00321    Clad( FALSE );
00322 
00323    /*
00324     * if we have a double buffer, L2R and R2L, process that part too.
00325     */
00326    if ( m_lpsbIn->IsBidirectionalScan() && m_lpsbOut->IsBidirectionalScan() )
00327    {
00328       Clad( TRUE );
00329    }
00330 
00331    return TRUE;
00332 }
00333 
00334 /*
00335  * process the frame part at the given offset (L2R, R2L frame part).
00336  */
00337 void CScanFilterClad::Clad( BOOL bDoR2L )
00338 {
00339    CScanBaseBuffer::ConstPointer pInpData = m_lpsbIn->Data()  + ( bDoR2L ? m_lpsbIn->GetFrameSize() : 0 );
00340    CScanBaseBuffer::Pointer      pOutData = m_lpsbOut->Data() + ( bDoR2L ? m_lpsbOut->GetFrameSize() : 0 );
00341 
00342    SizeElementType newWidth    = m_lpsbOut->Columns();
00343    SizeElementType newHeight   = m_lpsbOut->Rows();
00344 
00345    SizeElementType oldWidth    = m_lpsbIn->Columns();
00346    SizeElementType oldHeight   = m_lpsbIn->Rows();
00347 
00348    SizeElementType extraWidth  = newWidth  - oldWidth;
00349    SizeElementType extraHeight = newHeight - oldHeight;
00350 
00351    /*
00352     * copy the image data into the new area, clear the unused block at the right:
00353     */
00354    for ( SizeElementType y = 0; y < oldHeight; ++y )
00355    {
00356       //      dst       src       count
00357       memcpy( pOutData, pInpData, oldWidth * sizeof( CScanBaseBuffer::ValueType ) );
00358 
00359       //      dst                val  count
00360       memset( pOutData + oldWidth, 0, extraWidth * sizeof( CScanBaseBuffer::ValueType ) );
00361 
00362       pInpData += oldWidth;
00363       pOutData += newWidth;
00364    }
00365 
00366    /*
00367     * clear the unused block at the bottom:
00368     */
00369 
00370    //      dst     val  count
00371    memset( pOutData, 0, extraHeight * newWidth * sizeof( CScanBaseBuffer::ValueType ) );
00372 
00373 }
00374 
00375 /*
00376  * end of file
00377  */
00378 

Camera Filter Library documentation © 2004-2007 by Leiden Probe Microscopy