Filter Library Camera Interface Physics

ScanFilterDifferential.cpp

00001 /*
00002  * ScanFilterDifferential.cpp - frame by frame differentiation.
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: ScanFilterDifferential.cpp 358 2006-03-06 14:49:02Z moene $
00013  */
00014 
00015 /*
00016  * configuration defines:
00017  */
00018 
00019 //#define NO_RETRACE_LINE               // if the buffers do not contain a retrace line (nacra)
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/ScanFilterDifferential.h> // this filter's header
00033 #include <cfl/FilterDlg_Differential.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_FILTERDIFFERENTIAL 0  // permanent storage schema version
00042                                                 // this filter's name
00043 LPCTSTR CScanFilterDifferential::m_lpcsFilterName = _T("Differential-Plane");
00044                                         // this filter's short name
00045 LPCTSTR CScanFilterDifferential::m_lpcsShortFilterName = _T( "Diff" );
00046                                                 // serialization
00047 IMPLEMENT_SERIAL( CScanFilterDifferential, CScanFilter, CFL_SCHEMAVERSION_FILTERDIFFERENTIAL )
00048 
00049 /*
00050  * default angle: 0.
00051  */
00052 const int CScanFilterDifferential::def_nAngle  = 0;
00053 
00054 /*
00055  * constructor.
00056  */
00057 CScanFilterDifferential::CScanFilterDifferential() :
00058    m_nAngle( def_nAngle )
00059 {
00060    m_csFilterName = m_lpcsFilterName;
00061 
00062    ReadFilterSettings();                 // get filter settings to run without dialog
00063 }
00064 
00065 /*
00066  * destructor.
00067  */
00068 CScanFilterDifferential::~CScanFilterDifferential()
00069 {
00070    ; // do nothing
00071 }
00072 
00073 /*
00074  * store or retrieve the object's settings;
00075  * see also CScanFilterNull::Serialize().
00076  */
00077 void CScanFilterDifferential::Serialize(CArchive& ar)
00078 {
00079    CScanFilter::Serialize( ar );
00080 
00081    if ( ar.IsStoring() )
00082    {
00083       ar << m_nAngle;
00084    }
00085    else
00086    {
00087       ar >> m_nAngle;
00088    }
00089 };
00090 
00091 /*
00092  * \brief configure filter with settings as provided by the application on the
00093  * filterlist window (e.g. via registry); see also CScanFilterNull::ReadFilterSettings().
00094  */
00095 void CScanFilterDifferential::ReadFilterSettings( )
00096 {
00097 //   Q_LOG( _T("CScanFilterDifferential::ReadFilterSettings()") );
00098 
00099    CWinApp* pApp = AfxGetApp();
00100 
00101    if ( Q_INVALID( NULL == pApp ) )
00102       return ;
00103 
00104    /*
00105     * parameter string: x-size,y-size,parameter,factor
00106     */
00107    SetParameters( pApp->GetProfileString( gCflRegistrySubkey, m_lpcsFilterName, _T( "0" ) ) );
00108 }
00109 
00110 /*
00111  * \brief save filter settings e.g. for use with the filterlist window (e.g. via registry);
00112  * see also ReadFilterSettings().
00113  */
00114 void CScanFilterDifferential::WriteFilterSettings() const
00115 {
00116 //   Q_LOG( _T("CScanFilterDifferential::ReadFilterSettings()") );
00117 
00118    CWinApp* pApp = AfxGetApp();
00119 
00120    if ( Q_INVALID( NULL == pApp ) )
00121       return ;
00122 
00123    /*
00124     * parameter string:
00125     */
00126    LPCTSTR pStr = GetParameters();
00127 
00128    if ( pStr )
00129    {
00130       pApp->WriteProfileString( gCflRegistrySubkey, m_lpcsFilterName, pStr );
00131       delete const_cast<LPTSTR>( pStr );
00132    }
00133 }
00134 
00135 /*
00136  * provide current filter parameter(s) for filter-scripting capture
00137  * in the main application;
00138  * see also CScanFilterNull::GetParameters().
00139  */
00140 LPCTSTR CScanFilterDifferential::GetParameters( ) const
00141 {
00142 //   Q_LOG( _T("CScanFilterDifferential::GetParameters()") );
00143 
00144    /*
00145     * format parameters and return a c-string on the heap:
00146     */
00147    CString csParameters;
00148 
00149    csParameters.Format( _T( "%d" ), m_nAngle );
00150 
00151    return strcpy( new TCHAR[ csParameters.GetLength() + 1 ], csParameters );
00152 }
00153 
00154 /*
00155  * set parameters for filter-script execution of filter;
00156  * see also CScanFilterNull::SetParameters().
00157  */
00158 BOOL CScanFilterDifferential::SetParameters( LPCTSTR lpParameters )
00159 {
00160 //   Q_LOG( _T("CScanFilterDifferential::SetParameters()") );
00161 
00162    if ( Q_INVALID( NULL == lpParameters ) &&
00163            "CScanFilterDifferential (SetParameters): parameters expected, none provided (NULL)." )
00164    {
00165       return FALSE;
00166    }
00167 
00168    /*
00169     * set defaults, read parameter string:
00170     */
00171    m_nAngle = def_nAngle;
00172 
00173    int nfields = _stscanf( lpParameters, _T( "%d" ), &m_nAngle );
00174 
00175    if ( Q_INVALID( 1 > nfields && "CScanFilterDifferential (SetParameters): angle expected, got none." ) )
00176    {
00177       return FALSE;
00178    }
00179 
00180    if ( Q_INVALID( ( m_nAngle < 0 || m_nAngle > 360 ) && "CScanFilterDifferential (SetParameters): invalid angle." ) )
00181    {
00182       return FALSE;
00183    }
00184 
00185    return TRUE;
00186 }
00187 
00188 /*
00189  * start filter dialog to edit parameters; no filter operations;
00190  * see also CScanFilterNull::EditModeless().
00191  */
00192 BOOL CScanFilterDifferential::EditModeless( CWnd *pParentWnd )
00193 {
00194 //   Q_LOG( _T("CScanFilterDifferential::EditModeless()") );
00195 
00196    CFilterDlg_DifferentialPtr pDlg = new CFilterDlg_Differential();
00197 
00198    if ( Q_INVALID( NULL == pDlg ) )
00199       return FALSE;
00200 
00201    /*
00202     * set filter, no doc (edit modeless):
00203     */
00204    pDlg->SetParameters( this, NULL );   // (void) method
00205 
00206    /*
00207     * Create returns non-zero if dialog was created and initialized succesfully.
00208     * Note: no pDlg->DestroyWindow() must be done.
00209     */
00210    BOOL bRet = pDlg->Create( IDD_FILTERDLG_DIFFERENTIAL, pParentWnd );
00211    m_pDlg    = pDlg;
00212 
00213    /*
00214     * gray-out buffers
00215     */
00216    pDlg->DisableBufferControls();
00217 
00218    return bRet;
00219 }
00220 
00221 /*
00222  * create filter dialog and pre Apply() filter to view result;
00223  * see also CScanFilterNull::RunModeless().
00224  */
00225 BOOL CScanFilterDifferential::RunModeless( CWnd* pParentWnd, CDocument* pDoc )
00226 {
00227 //   Q_LOG( _T("CScanFilterDifferential::RunModeless()") );
00228 
00229    CFilterDlg_Differential *pDlg = new CFilterDlg_Differential();
00230 
00231    if ( Q_INVALID( NULL == pDlg ) )
00232       return FALSE;
00233 
00234    pDlg->SetParameters( this, pDoc );   // (void) method
00235 
00236    /*
00237     * Create returns non-zero if dialog was created and initialized succesfully.
00238     * Note: no pDlg->DestroyWindow() must be done.
00239     */
00240    pDlg->Create( IDD_FILTERDLG_DIFFERENTIAL, pParentWnd );
00241 
00242    m_pDlg = pDlg;
00243 
00244    BOOL bRet = Apply();
00245 
00246    if ( bRet )
00247    {
00248       pDlg->UpdateView();
00249    }
00250 
00251    return bRet;
00252 }
00253 
00254 /*
00255  * check parameters, take care of a properly sized output buffer,
00256  * set its name and copy filter parameters and process;
00257  * see also CScanFilterNull::ApplyCore().
00258  */
00259 BOOL CScanFilterDifferential::Apply()
00260 {
00261    if ( Q_INVALID( NULL == m_lpsbIn && "ensure valid input buffer" ) )
00262       return FALSE;
00263 
00264    if ( Q_INVALID( NULL == m_lpsbOut && "ensure valid output buffer" ) )
00265       return FALSE;
00266 
00267    /*
00268     * copy the filter settings (not the data):
00269     */
00270    Q_RETURN( m_lpsbOut->CreateOutputBufferFor( *m_lpsbIn ) );
00271 
00272    m_lpsbOut->m_csBufferName.Format( _T( "%s - %s:%d" ), m_lpsbIn->m_csBufferName, m_lpcsShortFilterName, m_nAngle );
00273 
00274    if ( IsInteractive() )
00275    {
00276       m_pDlg->SetDlgItemText( IDC_BUFFEROUTNAME, m_lpsbOut->m_csBufferName );
00277    }
00278 
00279    // Process first part (L2R)
00280    Differentiate( 0 );
00281 
00282    // If we have a double buffer, L2R and R2L, process that part too.
00283    if ( m_lpsbIn->IsBidirectionalScan() && m_lpsbOut->IsBidirectionalScan() )
00284    {
00285       Differentiate( m_lpsbIn->m_dwSizeX * m_lpsbIn->m_dwSizeY );
00286    }
00287 
00288    return TRUE;
00289 }
00290 
00291 /*
00292  * differentiate frame part at given offset.
00293  */
00294 void CScanFilterDifferential::Differentiate( DWORD dwOffset )
00295 {
00296    int clox, chix, cloy, chiy, dx, dy, cybx;
00297 
00298    double coslight = cos( m_nAngle * g_d2PI / 360.0 );
00299    double sinlight = sin( m_nAngle * g_d2PI / 360.0 );
00300 
00301    int nSizeX = m_lpsbIn->Columns();
00302    int nSizeY = m_lpsbIn->Rows();
00303    int nCount = 0;
00304 
00305    for ( int nCountY = 0; nCountY < nSizeY; ++nCountY )
00306    {
00307       cloy = max(   0           , ( nCountY - 1 ) );
00308       chiy = min( ( nSizeY - 1 ), ( nCountY + 1 ) );
00309       dy   = chiy - cloy;
00310       cloy *= nSizeX;
00311       chiy *= nSizeX;
00312       cybx = nCount = nCountY * nSizeX;
00313 
00314       for ( int nCountX = 0; nCountX < nSizeX; ++nCountX, ++nCount )
00315       {
00316          clox = max(   0        , ( nCountX-1 ) );
00317          chix = min( ( nSizeX-1), ( nCountX+1 ) );
00318          dx   = chix - clox;
00319 
00320          m_lpsbOut->m_pwData[ nCount + dwOffset ] = (short)(
00321             coslight * ( m_lpsbIn->m_pwData[ chix    + cybx + dwOffset] - m_lpsbIn->m_pwData[ clox    + cybx + dwOffset ] ) / dx +
00322             sinlight * ( m_lpsbIn->m_pwData[ nCountX + chiy + dwOffset] - m_lpsbIn->m_pwData[ nCountX + cloy + dwOffset ] ) / dy
00323          );
00324       }
00325    }
00326 }
00327 
00328 /*
00329  * end of file
00330  */

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