Filter Library Camera Interface Physics

ScanFilterSwapQuadrants.cpp

00001 /*
00002  * ScanFilterSwapQuadrants.cpp - swap quadrants.
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: ScanFilterSwapQuadrants.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/ScanFilterSwapQuadrants.h>// this filter's header
00022 
00023 #pragma warning( disable: 4018 4100 4116 4146 4244 4663)
00024 #include <cil/Math.h>                   // for cil::min()
00025 #pragma warning( default: 4018 4100 4116 4146 4244 4663)
00026 
00027 // #pragma warning( disable : 4663 )       // template<> Class<type>
00028 // #include <limits>                       // for numeric_limits<>
00029 // #pragma warning( default : 4663 )
00030 
00031 #undef min                              // use cil::min instead of macro
00032 #undef max                              // use cil::max instead of macro
00033 
00034 /*
00035  * configuration defines:
00036  */
00037 
00038 // currently none
00039 
00040 /*
00041  * end of configuration defines.
00042  */
00043 
00044 #ifdef _DEBUG
00045 #undef THIS_FILE
00046 static char THIS_FILE[]=__FILE__;
00047 #define new DEBUG_NEW
00048 #endif
00049 
00050 #define CFL_SCHEMAVERSION_FILTERIFFT 0  // permanent storage schema version
00051                                         // this filter's name
00052 LPCTSTR CScanFilterSwapQuadrants::m_lpcsFilterName = _T("Swap-Quadrants");
00053                                         // this filter's short name
00054 LPCTSTR CScanFilterSwapQuadrants::m_lpcsShortFilterName = _T( "SwapQ" );
00055                                         // serialization
00056 IMPLEMENT_SERIAL( CScanFilterSwapQuadrants, CScanFilter, CFL_SCHEMAVERSION_FILTERIFFT)
00057 
00058 /**
00059  * constructor.
00060  */
00061 CScanFilterSwapQuadrants::CScanFilterSwapQuadrants()
00062 {
00063    m_csFilterName = m_lpcsFilterName;
00064 }
00065 
00066 /**
00067  * destructor.
00068  */
00069 CScanFilterSwapQuadrants::~CScanFilterSwapQuadrants()
00070 {
00071    ; // do nothing
00072 }
00073 
00074 /**
00075  * no dialog.
00076  */
00077 BOOL CScanFilterSwapQuadrants::CanDoDialogEntry() const
00078 {
00079    return FALSE;
00080 }
00081 
00082 /**
00083  * store or retrieve the object's settings;
00084  * see also CScanFilterNull::Serialize().
00085  */
00086 void CScanFilterSwapQuadrants::Serialize(CArchive& ar)
00087 {
00088    CScanFilter::Serialize( ar );
00089 
00090    if ( ar.IsStoring() )
00091    {
00092       ; // do nothing
00093    }
00094    else
00095    {
00096       ; // do nothing
00097    }
00098 };
00099 
00100 /**
00101  * no parameters.
00102  */
00103 #pragma warning( push )
00104 #pragma warning( disable : 4100 )       // unreferenced formal parameter
00105 
00106 BOOL CScanFilterSwapQuadrants::SetParameters(LPCTSTR lpParameters)
00107 {
00108    return TRUE;
00109 }
00110 
00111 #pragma warning( pop )
00112 
00113 /*
00114  * no parameters.
00115  */
00116 LPCTSTR CScanFilterSwapQuadrants::GetParameters() const
00117 {
00118    return NULL;
00119 }
00120 
00121 /**
00122  * check parameters, take care of a properly sized output buffer,
00123  * set its name and copy filter parameters and process;
00124  * see also CScanFilterNull::ApplyCore().
00125  */
00126 BOOL CScanFilterSwapQuadrants::Apply()
00127 {
00128 //   Q_LOG( _T("CScanFilterCrossCorrelate::Apply()") );
00129 
00130    if ( Q_INVALID( NULL == m_lpsbIn && "ensure input buffer") )
00131       return FALSE;
00132 
00133    if ( Q_INVALID( NULL == m_lpsbOut && "ensure output buffer" ) )
00134       return FALSE;
00135 
00136    if ( Q_INVALID( !m_lpsbIn->IsCompleteFrame() && "ensure complete frame" ) )
00137       return FALSE;
00138 
00139    /*
00140     * copy the filter settings (not the data):
00141     */
00142    Q_RETURN( m_lpsbOut->CreateOutputBufferFor( *m_lpsbIn ) );
00143 
00144    m_lpsbOut->m_csBufferName = m_lpsbIn->m_csBufferName + _T(" - ") + m_lpcsShortFilterName;
00145    m_lpsbOut->m_csDataName.Format(_T("%s(%s)"), m_lpcsShortFilterName, m_lpsbIn->m_csDataName );
00146 
00147    return Compute();
00148 }
00149 
00150 /**
00151  * compute the desired result.
00152  */
00153 BOOL CScanFilterSwapQuadrants::Compute()
00154 {
00155 //   Q_LOG( _T("CScanFilterSwapQuadrants::Compute()") );
00156 
00157    ASSERT( m_lpsbIn );
00158    ASSERT( m_lpsbOut );
00159 
00160    /*
00161     * perform the left-right and right-left transformations:
00162     */
00163    if ( m_lpsbIn->HasLeftToRightScan() )
00164    {
00165       CopyPart   ( m_lpsbIn->Data() , m_lpsbOut->Data(), m_lpsbOut->GetFrameSize() );
00166       ComputePart( m_lpsbOut->Data(), m_lpsbOut->Rows(), m_lpsbOut->Columns()     );
00167    }
00168 
00169    if ( m_lpsbIn->HasRightToLeftScan() )
00170    {
00171       ConstPointer pIn  = m_lpsbIn->Data()  + m_lpsbIn->GetFrameSize() ;
00172       Pointer      pOut = m_lpsbOut->Data() + m_lpsbOut->GetFrameSize();
00173 
00174       CopyPart   ( pIn , pOut, m_lpsbOut->GetFrameSize()         );
00175       ComputePart( pOut, m_lpsbOut->Rows(), m_lpsbOut->Columns() );
00176    }
00177 
00178    return TRUE;
00179 }
00180 
00181 /**
00182  * copy a frame.
00183  */
00184 void CScanFilterSwapQuadrants::CopyPart( ConstPointer pIn, Pointer pOut, SizeType elements )
00185 {
00186    CopyMemory( pOut, pIn, elements * sizeof( ValueType ) );
00187 }
00188 
00189 /**
00190  * compute a frame in-place; intended to only swap even x even matrices.
00191  */
00192 void CScanFilterSwapQuadrants::ComputePart( Pointer pFrame, const SizeType& nrow, const SizeType& ncol )
00193 {
00194    /*
00195     * ensure even x even:
00196     */
00197 //   assert( 0 == nrow % 2 );
00198 //   assert( 0 == ncol % 2 );
00199 
00200    SizeType hrow = nrow / 2;           // half row
00201    SizeType hcol = ncol / 2;           // half column
00202    SizeType hsiz = hrow * ncol;        // half size
00203 
00204    for ( SizeType y = 0; y < hrow; ++y )
00205    {
00206       Pointer ptop = pFrame + y * ncol;
00207       Pointer pbot = ptop   + hsiz;
00208 
00209       for ( SizeType left = 0; left < hcol; ++left )
00210       {
00211          SizeType right = hcol + left;
00212 
00213          //          top--left  bottom--right
00214          lpm::swap( ptop[ left ], pbot[ right ] );
00215          lpm::swap( pbot[ left ], ptop[ right ] );
00216       }  //        bottom--left     top--right
00217    }
00218 }
00219 
00220 /*
00221  * end of file
00222  */
00223 

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