Filter Library Camera Interface Physics

ScanFilterThreshold.cpp

00001 /*
00002  * ScanFilterThreshold.cpp - general threshold 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, Leiden Probe Microscopy.
00008  * Copyright (C) 2004, Universiteit Leiden.
00009  *
00010  * Authors: Martin J. Moene
00011  *
00012  * $Id: ScanFilterThreshold.cpp 400 2006-09-28 13:34:02Z moene $
00013  */
00014 
00015 #include "stdafx.h"                     // for common (pre-compiled) headers
00016 //#include <cfl/stdafx.h>                 // for common (pre-compiled) headers
00017 #include <cfl/resource.h>               // for messages and other resources
00018 
00019 #include <Camera/InterfaceDll.h>        // Camera--Filter Library interface
00020 
00021 #include <cfl/ScanFilterThreshold.h>      // this filter's header FIXME
00022 #include <cfl/FilterDlg_Threshold.h>      // this filter's dialog
00023 #include <cfl/ToolCilImage.h>                   // for Image type and ToImage conversion shim
00024 
00025 #include <cil/Traits.h>                         // type traits
00026 #include <cil/ThresholdImageFilter.h>     // ThresholdImageFilter image filter
00027 
00028 /*
00029  * configuration defines:
00030  */
00031 
00032 // none
00033 
00034 /*
00035  * end of configuration defines.
00036  */
00037 
00038 #ifdef _DEBUG
00039 #undef THIS_FILE
00040 static char THIS_FILE[]=__FILE__;
00041 #define new DEBUG_NEW
00042 #endif
00043 
00044 /*
00045  * convenience types.
00046  */
00047 typedef cil::ThresholdImageFilter< Image > ThresholdFilterType;
00048 
00049 #define CFL_SCHEMAVERSION_FILTERTHRESHOLD 0 ///< permanent storage schema version
00050                                         // this filter's name
00051 LPCTSTR CScanFilterThreshold::m_lpcsFilterName = _T("Threshold");
00052                                         // this filter's short name
00053 LPCTSTR CScanFilterThreshold::m_lpcsShortFilterName = CScanFilterThreshold::m_lpcsFilterName;
00054                                         // serialization
00055 IMPLEMENT_SERIAL(CScanFilterThreshold, CScanFilter, CFL_SCHEMAVERSION_FILTERTHRESHOLD)
00056 
00057 // /*
00058 //  * the default new X-size.
00059 //  */
00060 // const CScanFilterThreshold::DiffElementType CScanFilterThreshold::def_nSizeX = 3;
00061 // 
00062 // /*
00063 //  * the default new Y-size.
00064 //  */
00065 // const CScanFilterThreshold::DiffElementType CScanFilterThreshold::def_nSizeY = 3;
00066 // 
00067 /*
00068  * default constructor: setup filter to run without dialog;
00069  * see also CScanFilterNull::CScanFilterNull().
00070  */
00071 CScanFilterThreshold::CScanFilterThreshold()
00072 {
00073 //   Q_LOG( _T("CScanFilterThreshold::CScanFilterThreshold()") );
00074 
00075    /*
00076     * we define our own threshold mode values to decouple this filter's header
00077     * from the Camera Imaging Library (CIL). Nonetheless, we want to use the
00078     * mode values as if they were from the CIL Threshld filter. Here we check
00079     * at compile-time if the values do match.
00080     */
00081    cil_static_assert( tmBelow   == ThresholdFilterType::tmBelow  , tmBelow_values_must_match   );
00082    cil_static_assert( tmAbove   == ThresholdFilterType::tmAbove  , tmAbove_values_must_match   );
00083    cil_static_assert( tmOutside == ThresholdFilterType::tmOutside, tmOutside_values_must_match );
00084 
00085    m_csFilterName = m_lpcsFilterName;
00086 
00087    ReadFilterSettings();
00088 }
00089 
00090 /*
00091  * destructor.
00092  */
00093 CScanFilterThreshold::~CScanFilterThreshold()
00094 {
00095 //   Q_LOG( _T("CScanFilterThreshold::~CScanFilterThreshold()") );
00096 
00097    ; // do nothing
00098 }
00099 
00100 /*
00101  * store or retrieve the object's settings;
00102  * currently filter serialization is not used;
00103  * see also CScanFilterNull::Serialize().
00104  */
00105 void CScanFilterThreshold::Serialize(CArchive& ar)
00106 {
00107 //   Q_LOG( _T("CScanFilterThreshold::Serialize()") );
00108 
00109    CScanFilter::Serialize( ar );
00110 
00111    if ( ar.IsStoring() )
00112    {
00113        ar << m_mode << m_lower << m_upper << m_outside;
00114    }
00115    else
00116    {
00117       int mode;
00118       
00119       ar >> mode >> m_lower >> m_upper >> m_outside;
00120       
00121       m_mode = static_cast< ThresholdMode >( mode );
00122    }
00123 };
00124 
00125 /*
00126  * \brief configure filter with settings as provided by the application on the
00127  * filterlist window (e.g. via registry); see also CScanFilterNull::ReadFilterSettings().
00128  */
00129 void CScanFilterThreshold::ReadFilterSettings()
00130 {
00131 //   Q_LOG( _T("CScanFilterThreshold::ReadFilterSettings()") );
00132 
00133    CWinApp* pApp = AfxGetApp();
00134 
00135    if ( Q_INVALID( NULL == pApp ) )
00136       return ;
00137 
00138    /*
00139     * parameter string:
00140     */
00141    SetParameters( pApp->GetProfileString( gCflRegistrySubkey, m_lpcsFilterName, _T( "0,-30000,+30000,0" ) ) );
00142 }
00143 
00144 /*
00145  * \brief save filter settings e.g. for use with the filterlist window (e.g. via registry);
00146  * see also ReadFilterSettings().
00147  */
00148 void CScanFilterThreshold::WriteFilterSettings() const
00149 {
00150 //   Q_LOG( _T("CScanFilterThreshold::WriteFilterSettings()") );
00151 
00152    CWinApp* pApp = AfxGetApp();
00153 
00154    if ( Q_INVALID( NULL == pApp ) )
00155       return ;
00156 
00157    /*
00158     * parameter string:
00159     */
00160    LPCTSTR pStr = GetParameters();
00161 
00162    if ( pStr )
00163    {
00164       pApp->WriteProfileString( gCflRegistrySubkey, m_lpcsFilterName, pStr );
00165       delete const_cast<LPTSTR>( pStr );
00166    }
00167 }
00168 
00169 /*
00170  * provide current filter parameter(s) for filter-scripting capture
00171  * in the main application;
00172  * see also CScanFilterNull::GetParameters().
00173  */
00174 LPCTSTR CScanFilterThreshold::GetParameters() const
00175 {
00176 //   Q_LOG( _T("CScanFilterThreshold::GetParameters()") );
00177 
00178    /*
00179     * format parameters and return a c-string on the heap:
00180     */
00181    CString csParameters;
00182 
00183    csParameters.Format
00184       ( _T( "%d,%d,%d,%d" )
00185       , GetThresholdMode()
00186       , GetLowerThreshold()
00187       , GetUpperThreshold()
00188       , GetOutsideValue()
00189    );
00190 
00191    return strcpy( new TCHAR[ csParameters.GetLength() + 1 ], csParameters );
00192 }
00193 
00194 /*
00195  * set parameters for filter-script execution of filter;
00196  * see also CScanFilterNull::SetParameters().
00197  */
00198 BOOL CScanFilterThreshold::SetParameters( LPCTSTR lpParameters )
00199 {
00200 //   Q_LOG( _T("CScanFilterThreshold::SetParameters()") );
00201 
00202    if ( Q_INVALID( NULL == lpParameters && "CScanFilterThreshold (SetParameters): parameters expected, none provided (NULL)." ) )
00203    {
00204       return FALSE;
00205    }
00206 
00207    /*
00208     * set defaults, read parameter string with factor and optional offset:
00209     */
00210    int mode    = tmBelow; 
00211    int lower   = lpm::NumericTraits< InputPixelType >::Min();
00212    int upper   = lpm::NumericTraits< InputPixelType >::Max();
00213    int outside = 0;
00214 
00215    int nfields = _stscanf
00216       ( lpParameters
00217       , _T( "%d,%d,%d,%d" )
00218       , &mode
00219       , &lower
00220       , &upper
00221       , &outside
00222    );
00223 
00224    m_mode    = static_cast< ThresholdMode   >( mode    );
00225    m_lower   = static_cast< InputPixelType  >( lower   );
00226    m_upper   = static_cast< InputPixelType  >( upper   );
00227    m_outside = static_cast< OutputPixelType >( outside );
00228 
00229    if ( Q_INVALID( 4 > nfields && "CScanFilterThreshold (SetParameters): four parameters expected, got less." ) )
00230    {
00231       return FALSE;
00232    }
00233 
00234    return TRUE;
00235 }
00236 
00237 /*
00238  * create filter dialog and pre Apply() filter to view result;
00239  * see also CScanFilterNull::RunModeless().
00240  */
00241 BOOL CScanFilterThreshold::RunModeless( CWnd* pParentWnd, CDocument* pDoc )
00242 {
00243 //   Q_LOG( _T("CScanFilterThreshold::RunModeless()") );
00244 
00245    CFilterDlg_ThresholdPtr pDlg = new CFilterDlg_Threshold();
00246 
00247    if ( Q_INVALID( NULL == pDlg ) )
00248       return FALSE;
00249 
00250    pDlg->SetParameters( this, pDoc );   // (void) method
00251 
00252    /*
00253     * Create returns non-zero if dialog was created and initialized succesfully.
00254     * Note: no pDlg->DestroyWindow() must be done.
00255     */
00256    pDlg->Create( IDD_FILTERDLG_THRESHOLD, pParentWnd );
00257 
00258    m_pDlg = pDlg;
00259 
00260    BOOL bRet = Apply();
00261 
00262    if ( bRet )
00263    {
00264       pDlg->UpdateView();
00265    }
00266 
00267    return bRet;
00268 }
00269 
00270 /*
00271  * check parameters, take care of a properly sized output buffer, set its name
00272  * and copy filter parameters, CalculatePlane() and SubtractPlane();
00273  * see also CScanFilterNull::ApplyCore().
00274  */
00275 BOOL CScanFilterThreshold::Apply()
00276 {
00277 //   Q_LOG( _T("CScanFilterThreshold::Apply()") );
00278 
00279    if ( Q_INVALID( NULL == m_lpsbIn ) )
00280       return FALSE;
00281 
00282    if ( Q_INVALID( NULL == m_lpsbOut ) )
00283       return FALSE;
00284 
00285    if ( Q_INVALID( false == m_lpsbIn->IsCompleteFrame() ) )
00286       return FALSE;
00287 
00288    /*
00289     * copy the filter settings (not the data):
00290     */
00291    Q_RETURN( m_lpsbOut->CreateOutputBufferFor( *m_lpsbIn ) );
00292 
00293    m_lpsbOut->m_csBufferName.Format
00294       ( _T("%s - %s %d,%d,%d,%d")
00295       , m_lpsbIn->m_csBufferName
00296       , m_lpcsShortFilterName
00297       , GetThresholdMode()
00298       , GetLowerThreshold()
00299       , GetUpperThreshold()
00300       , GetOutsideValue()
00301    );
00302 
00303    if ( IsInteractive() )
00304    {
00305       m_pDlg->SetDlgItemText( IDC_BUFFEROUTNAME, m_lpsbOut->m_csBufferName );
00306    }
00307 
00308    /*
00309     * apply filter to L2R part, use radius as specified by size (size 3x3 => radus 1x1):
00310     */
00311    ThresholdFilterType filter;
00312    
00313    filter.SetOutsideValue ( GetOutsideValue()  );
00314 
00315    switch ( GetThresholdMode() )
00316    {
00317       case tmBelow:
00318          filter.ThresholdBelow( GetLowerThreshold() );
00319          break;
00320       case tmAbove:
00321          filter.ThresholdAbove( GetUpperThreshold() );
00322          break;
00323       case tmOutside:
00324          filter.ThresholdOutside( GetLowerThreshold(), GetUpperThreshold() );
00325          break;
00326    };
00327 
00328    filter.Apply( ToImage( m_lpsbIn ), ToImage( m_lpsbOut ) );
00329 
00330    /*
00331     * if we have a double buffer, L2R and R2L, process that part too.
00332     */
00333    if ( m_lpsbIn->IsBidirectionalScan() && m_lpsbOut->IsBidirectionalScan() )
00334    {
00335       filter.Apply( ToImage( m_lpsbIn, true ), ToImage( m_lpsbOut, true ) );
00336    }
00337 
00338    return TRUE;
00339 }
00340 
00341 /*
00342  * end of file
00343  */
00344 

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