Filter Library Camera Interface Physics

ScanFilterBinaryThreshold.cpp

00001 /*
00002  * ScanFilterBinaryThreshold.cpp - binary 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: ScanFilterBinaryThreshold.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/ScanFilterBinaryThreshold.h>      // this filter's header FIXME
00022 #include <cfl/FilterDlg_BinaryThreshold.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/BinaryThresholdImageFilter.h>     // BinaryThresholdImageFilter 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::BinaryThresholdImageFilter< Image > BinaryThresholdFilterType;
00048 
00049 #define CFL_SCHEMAVERSION_FILTERBINARYTHRESHOLD 0 ///< permanent storage schema version
00050                                         // this filter's name
00051 LPCTSTR CScanFilterBinaryThreshold::m_lpcsFilterName = _T("BinaryThreshold");
00052                                         // this filter's short name
00053 LPCTSTR CScanFilterBinaryThreshold::m_lpcsShortFilterName = CScanFilterBinaryThreshold::m_lpcsFilterName;
00054                                         // serialization
00055 IMPLEMENT_SERIAL(CScanFilterBinaryThreshold, CScanFilter, CFL_SCHEMAVERSION_FILTERBINARYTHRESHOLD)
00056 
00057 // /*
00058 //  * the default new X-size.
00059 //  */
00060 // const CScanFilterBinaryThreshold::DiffElementType CScanFilterBinaryThreshold::def_nSizeX = 3;
00061 // 
00062 // /*
00063 //  * the default new Y-size.
00064 //  */
00065 // const CScanFilterBinaryThreshold::DiffElementType CScanFilterBinaryThreshold::def_nSizeY = 3;
00066 // 
00067 /*
00068  * default constructor: setup filter to run without dialog;
00069  * see also CScanFilterNull::CScanFilterNull().
00070  */
00071 CScanFilterBinaryThreshold::CScanFilterBinaryThreshold()
00072 {
00073 //   Q_LOG( _T("CScanFilterBinaryThreshold::CScanFilterBinaryThreshold()") );
00074 
00075    m_csFilterName = m_lpcsFilterName;
00076 
00077    ReadFilterSettings();
00078 }
00079 
00080 /*
00081  * destructor.
00082  */
00083 CScanFilterBinaryThreshold::~CScanFilterBinaryThreshold()
00084 {
00085 //   Q_LOG( _T("CScanFilterBinaryThreshold::~CScanFilterBinaryThreshold()") );
00086 
00087    ; // do nothing
00088 }
00089 
00090 /*
00091  * store or retrieve the object's settings;
00092  * currently filter serialization is not used;
00093  * see also CScanFilterNull::Serialize().
00094  */
00095 void CScanFilterBinaryThreshold::Serialize(CArchive& ar)
00096 {
00097 //   Q_LOG( _T("CScanFilterBinaryThreshold::Serialize()") );
00098 
00099    CScanFilter::Serialize( ar );
00100 
00101    if ( ar.IsStoring() )
00102    {
00103        ar << m_lower << m_upper << m_inside << m_outside;
00104    }
00105    else
00106    {
00107        ar >> m_lower >> m_upper >> m_inside >> m_outside;
00108    }
00109 };
00110 
00111 /*
00112  * \brief configure filter with settings as provided by the application on the
00113  * filterlist window (e.g. via registry); see also CScanFilterNull::ReadFilterSettings().
00114  */
00115 void CScanFilterBinaryThreshold::ReadFilterSettings()
00116 {
00117 //   Q_LOG( _T("CScanFilterBinaryThreshold::ReadFilterSettings()") );
00118 
00119    CWinApp* pApp = AfxGetApp();
00120 
00121    if ( Q_INVALID( NULL == pApp ) )
00122       return ;
00123 
00124    /*
00125     * parameter string:
00126     */
00127    SetParameters( pApp->GetProfileString( gCflRegistrySubkey, m_lpcsFilterName, _T( "-30000,+30000,1,0" ) ) );
00128 }
00129 
00130 /*
00131  * \brief save filter settings e.g. for use with the filterlist window (e.g. via registry);
00132  * see also ReadFilterSettings().
00133  */
00134 void CScanFilterBinaryThreshold::WriteFilterSettings() const
00135 {
00136 //   Q_LOG( _T("CScanFilterBinaryThreshold::WriteFilterSettings()") );
00137 
00138    CWinApp* pApp = AfxGetApp();
00139 
00140    if ( Q_INVALID( NULL == pApp ) )
00141       return ;
00142 
00143    /*
00144     * parameter string:
00145     */
00146    LPCTSTR pStr = GetParameters();
00147 
00148    if ( pStr )
00149    {
00150       pApp->WriteProfileString( gCflRegistrySubkey, m_lpcsFilterName, pStr );
00151       delete const_cast<LPTSTR>( pStr );
00152    }
00153 }
00154 
00155 /*
00156  * provide current filter parameter(s) for filter-scripting capture
00157  * in the main application;
00158  * see also CScanFilterNull::GetParameters().
00159  */
00160 LPCTSTR CScanFilterBinaryThreshold::GetParameters() const
00161 {
00162 //   Q_LOG( _T("CScanFilterBinaryThreshold::GetParameters()") );
00163 
00164    /*
00165     * format parameters and return a c-string on the heap:
00166     */
00167    CString csParameters;
00168 
00169    csParameters.Format
00170       ( _T( "%d,%d,%d,%d" )
00171       , GetLowerThreshold()
00172       , GetUpperThreshold()
00173       , GetInsideValue()
00174       , GetOutsideValue()
00175    );
00176 
00177    return strcpy( new TCHAR[ csParameters.GetLength() + 1 ], csParameters );
00178 }
00179 
00180 /*
00181  * set parameters for filter-script execution of filter;
00182  * see also CScanFilterNull::SetParameters().
00183  */
00184 BOOL CScanFilterBinaryThreshold::SetParameters( LPCTSTR lpParameters )
00185 {
00186 //   Q_LOG( _T("CScanFilterBinaryThreshold::SetParameters()") );
00187 
00188    if ( Q_INVALID( NULL == lpParameters && "CScanFilterBinaryThreshold (SetParameters): parameters expected, none provided (NULL)." ) )
00189    {
00190       return FALSE;
00191    }
00192 
00193    /*
00194     * set defaults, read parameter string with factor and optional offset:
00195     */
00196    int lower   = lpm::NumericTraits< InputPixelType >::Min();
00197    int upper   = lpm::NumericTraits< InputPixelType >::Max();
00198    int inside  = 1;
00199    int outside = 0;
00200 
00201    int nfields = _stscanf
00202       ( lpParameters
00203       , _T( "%d,%d,%d,%d" )
00204       , &lower
00205       , &upper
00206       , &inside
00207       , &outside
00208    );
00209 
00210    m_lower   = static_cast< InputPixelType  >( lower   );
00211    m_upper   = static_cast< InputPixelType  >( upper   );
00212    m_inside  = static_cast< OutputPixelType >( inside  );
00213    m_outside = static_cast< OutputPixelType >( outside );
00214 
00215    if ( Q_INVALID( 4 > nfields && "CScanFilterBinaryThreshold (SetParameters): four parameters expected, got less." ) )
00216    {
00217       return FALSE;
00218    }
00219 
00220    return TRUE;
00221 }
00222 
00223 /*
00224  * create filter dialog and pre Apply() filter to view result;
00225  * see also CScanFilterNull::RunModeless().
00226  */
00227 BOOL CScanFilterBinaryThreshold::RunModeless( CWnd* pParentWnd, CDocument* pDoc )
00228 {
00229 //   Q_LOG( _T("CScanFilterBinaryThreshold::RunModeless()") );
00230 
00231    CFilterDlg_BinaryThresholdPtr pDlg = new CFilterDlg_BinaryThreshold();
00232 
00233    if ( Q_INVALID( NULL == pDlg ) )
00234       return FALSE;
00235 
00236    pDlg->SetParameters( this, pDoc );   // (void) method
00237 
00238    /*
00239     * Create returns non-zero if dialog was created and initialized succesfully.
00240     * Note: no pDlg->DestroyWindow() must be done.
00241     */
00242    pDlg->Create( IDD_FILTERDLG_BINARYTHRESHOLD, pParentWnd );
00243 
00244    m_pDlg = pDlg;
00245 
00246    BOOL bRet = Apply();
00247 
00248    if ( bRet )
00249    {
00250       pDlg->UpdateView();
00251    }
00252 
00253    return bRet;
00254 }
00255 
00256 /*
00257  * check parameters, take care of a properly sized output buffer, set its name
00258  * and copy filter parameters, CalculatePlane() and SubtractPlane();
00259  * see also CScanFilterNull::ApplyCore().
00260  */
00261 BOOL CScanFilterBinaryThreshold::Apply()
00262 {
00263 //   Q_LOG( _T("CScanFilterBinaryThreshold::Apply()") );
00264 
00265    if ( Q_INVALID( NULL == m_lpsbIn ) )
00266       return FALSE;
00267 
00268    if ( Q_INVALID( NULL == m_lpsbOut ) )
00269       return FALSE;
00270 
00271    if ( Q_INVALID( false == m_lpsbIn->IsCompleteFrame() ) )
00272       return FALSE;
00273 
00274    /*
00275     * copy the filter settings (not the data):
00276     */
00277    Q_RETURN( m_lpsbOut->CreateOutputBufferFor( *m_lpsbIn ) );
00278 
00279    m_lpsbOut->m_csBufferName.Format
00280       ( _T("%s - %s %d,%d,%d,%d")
00281       , m_lpsbIn->m_csBufferName
00282       , m_lpcsShortFilterName
00283       , GetLowerThreshold()
00284       , GetUpperThreshold()
00285       , GetInsideValue()
00286       , GetOutsideValue()
00287    );
00288 
00289    if ( IsInteractive() )
00290    {
00291       m_pDlg->SetDlgItemText( IDC_BUFFEROUTNAME, m_lpsbOut->m_csBufferName );
00292    }
00293 
00294    /*
00295     * apply filter to L2R part, use radius as specified by size (size 3x3 => radus 1x1):
00296     */
00297    BinaryThresholdFilterType filter;
00298    
00299    filter.SetLowerThreshold( GetLowerThreshold() );
00300    filter.SetUpperThreshold( GetUpperThreshold() );
00301    filter.SetInsideValue   ( GetInsideValue()    );
00302    filter.SetOutsideValue  ( GetOutsideValue()   );
00303 
00304    filter.Apply( ToImage( m_lpsbIn ), ToImage( m_lpsbOut ) );
00305 
00306    /*
00307     * if we have a double buffer, L2R and R2L, process that part too.
00308     */
00309    if ( m_lpsbIn->IsBidirectionalScan() && m_lpsbOut->IsBidirectionalScan() )
00310    {
00311       filter.Apply( ToImage( m_lpsbIn, true ), ToImage( m_lpsbOut, true ) );
00312    }
00313 
00314    return TRUE;
00315 }
00316 
00317 /*
00318  * end of file
00319  */
00320 

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