00001 /*============================================================================= 00002 Copyright (C) 2012 Allied Vision Technologies. All Rights Reserved. 00003 00004 Redistribution of this file, in original or modified form, without 00005 prior written consent of Allied Vision Technologies is prohibited. 00006 00007 ------------------------------------------------------------------------------- 00008 00009 File: Frame.cpp 00010 00011 Description: Implementation of class AVT::VmbAPI::Frame. 00012 00013 ------------------------------------------------------------------------------- 00014 00015 THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED 00016 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE, 00017 NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00018 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 00019 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00020 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00021 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00022 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 00023 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00024 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00025 00026 =============================================================================*/ 00027 00028 #include <VimbaCPP/Include/Frame.h> 00029 00030 #include <VimbaCPP/Include/LoggerDefines.h> 00031 #include <VimbaCPP/Include/VimbaSystem.h> 00032 #include <VimbaCPP/Source/ConditionHelper.h> 00033 #include <VimbaCPP/Include/SharedPointerDefines.h> 00034 #include <VimbaCPP/Source/FrameImpl.h> 00035 00036 namespace AVT { 00037 namespace VmbAPI { 00038 00039 Frame::Frame() 00040 { 00041 // No default ctor 00042 } 00043 00044 Frame::Frame( Frame& ) 00045 { 00046 // No copy ctor 00047 } 00048 00049 Frame& Frame::operator=( const Frame& ) 00050 { 00051 // No assignment operator 00052 return *this; 00053 } 00054 00055 Frame::Frame( VmbInt64_t nBufferSize ) 00056 : m_pImpl( new Impl() ) 00057 { 00058 m_pImpl->m_bAlreadyAnnounced = false; 00059 m_pImpl->m_bAlreadyQueued = false; 00060 m_pImpl->m_bIsUserBuffer = false; 00061 SP_SET( m_pImpl->m_pObserverMutex, new Mutex() ); 00062 m_pImpl->Init(); 00063 m_pImpl->m_pBuffer = new VmbUchar_t[ (VmbUint32_t)nBufferSize ]; 00064 m_pImpl->m_frame.bufferSize = (VmbUint32_t)nBufferSize; 00065 m_pImpl->m_frame.buffer = m_pImpl->m_pBuffer; 00066 } 00067 00068 Frame::Frame( VmbUchar_t *pBuffer, VmbInt64_t nBufferSize ) 00069 : m_pImpl( new Impl() ) 00070 { 00071 m_pImpl->m_bAlreadyAnnounced = false; 00072 m_pImpl->m_bAlreadyQueued = false; 00073 m_pImpl->m_bIsUserBuffer = true; 00074 m_pImpl->m_pBuffer = NULL; 00075 SP_SET( m_pImpl->m_pObserverMutex, new Mutex()); 00076 m_pImpl->Init(); 00077 if ( NULL != pBuffer ) 00078 { 00079 m_pImpl->m_pBuffer = pBuffer; 00080 m_pImpl->m_frame.bufferSize = (VmbUint32_t)nBufferSize; 00081 m_pImpl->m_frame.buffer = m_pImpl->m_pBuffer; 00082 } 00083 else 00084 { 00085 // Do some logging 00086 LOG_FREE_TEXT( "No valid buffer passed when constructing frame." ) 00087 } 00088 } 00089 00090 void Frame::Impl::Init() 00091 { 00092 m_frame.ancillarySize = 0; 00093 m_frame.buffer = NULL; 00094 m_frame.bufferSize = 0; 00095 for ( int i=0; i<4; ++i) 00096 { 00097 m_frame.context[i] = NULL; 00098 } 00099 m_frame.frameID = 0; 00100 m_frame.height = 0; 00101 m_frame.imageSize = 0; 00102 m_frame.offsetX = 0; 00103 m_frame.offsetY = 0; 00104 m_frame.pixelFormat = 0; 00105 m_frame.receiveFlags = VmbFrameFlagsNone; 00106 m_frame.receiveStatus = VmbFrameStatusInvalid; 00107 m_frame.timestamp = 0; 00108 m_frame.width = 0; 00109 } 00110 00111 Frame::~Frame() 00112 { 00113 UnregisterObserver(); 00114 if ( false == m_pImpl->m_bIsUserBuffer 00115 && NULL != m_pImpl->m_pBuffer ) 00116 { 00117 delete [] m_pImpl->m_pBuffer; 00118 } 00119 00120 delete m_pImpl; 00121 } 00122 00123 VmbErrorType Frame::RegisterObserver( const IFrameObserverPtr &rObserver ) 00124 { 00125 if ( SP_ISNULL( rObserver )) 00126 { 00127 return VmbErrorBadParameter; 00128 } 00129 00130 // Begin exclusive write lock observer 00131 if ( true == m_pImpl->m_observerConditionHelper.EnterWriteLock( m_pImpl->m_pObserverMutex, true )) 00132 { 00133 m_pImpl->m_pObserver = rObserver; 00134 00135 // End write lock observer 00136 m_pImpl->m_observerConditionHelper.ExitWriteLock( m_pImpl->m_pObserverMutex ); 00137 00138 return VmbErrorSuccess; 00139 } 00140 else 00141 { 00142 LOG_FREE_TEXT( "Could not lock frame observer.") 00143 return VmbErrorResources; 00144 } 00145 } 00146 00147 VmbErrorType Frame::UnregisterObserver() 00148 { 00149 VmbErrorType res = VmbErrorSuccess; 00150 00151 // Begin exclusive write lock observer 00152 if ( true == m_pImpl->m_observerConditionHelper.EnterWriteLock( m_pImpl->m_pObserverMutex, true )) 00153 { 00154 if ( SP_ISNULL( m_pImpl->m_pObserver )) 00155 { 00156 res = VmbErrorNotFound; 00157 } 00158 else 00159 { 00160 SP_RESET( m_pImpl->m_pObserver ); 00161 } 00162 00163 // End exclusive write lock observer 00164 m_pImpl->m_observerConditionHelper.ExitWriteLock( m_pImpl->m_pObserverMutex ); 00165 } 00166 else 00167 { 00168 LOG_FREE_TEXT( "Could not lock frame observer.") 00169 res = VmbErrorResources; 00170 } 00171 00172 return res; 00173 } 00174 00175 bool Frame::GetObserver( IFrameObserverPtr &rObserver ) const 00176 { 00177 if ( SP_ISNULL( m_pImpl->m_pObserver )) 00178 { 00179 return false; 00180 } 00181 00182 // Begin read lock observer 00183 if ( true == m_pImpl->m_observerConditionHelper.EnterReadLock( m_pImpl->m_pObserverMutex )) 00184 { 00185 rObserver = m_pImpl->m_pObserver; 00186 // End read lock observer 00187 m_pImpl->m_observerConditionHelper.ExitReadLock( m_pImpl->m_pObserverMutex ); 00188 return true; 00189 } 00190 else 00191 { 00192 LOG_FREE_TEXT( "Could not lock frame observer.") 00193 } 00194 00195 return false; 00196 } 00197 00198 VmbErrorType Frame::GetAncillaryData( AncillaryDataPtr &rAncillaryData ) 00199 { 00200 if ( m_pImpl->m_frame.ancillarySize == 0 ) 00201 { 00202 return VmbErrorNotFound; 00203 } 00204 00205 SP_SET( rAncillaryData, new AncillaryData( &m_pImpl->m_frame )); 00206 00207 return VmbErrorSuccess; 00208 } 00209 00210 VmbErrorType Frame::GetAncillaryData( ConstAncillaryDataPtr &rAncillaryData ) const 00211 { 00212 if ( m_pImpl->m_frame.ancillarySize == 0 ) 00213 { 00214 return VmbErrorNotFound; 00215 } 00216 00217 SP_SET( rAncillaryData, new AncillaryData( &m_pImpl->m_frame )); 00218 00219 return VmbErrorSuccess; 00220 } 00221 00222 VmbErrorType Frame::GetBuffer( VmbUchar_t* &rpBuffer ) 00223 { 00224 rpBuffer = m_pImpl->m_pBuffer; 00225 00226 return VmbErrorSuccess; 00227 } 00228 00229 VmbErrorType Frame::GetBuffer( const VmbUchar_t* &rpBuffer ) const 00230 { 00231 rpBuffer = m_pImpl->m_pBuffer; 00232 00233 return VmbErrorSuccess; 00234 } 00235 00236 VmbErrorType Frame::GetImage( VmbUchar_t* &rpBuffer ) 00237 { 00238 // HINT: On Allied Vision cameras image data always is at the beginning of the buffer 00239 rpBuffer = m_pImpl->m_pBuffer; 00240 00241 return VmbErrorSuccess; 00242 } 00243 00244 VmbErrorType Frame::GetImage( const VmbUchar_t* &rpBuffer ) const 00245 { 00246 // HINT: On Allied Vision cameras image data always is at the beginning of the buffer 00247 rpBuffer = m_pImpl->m_pBuffer; 00248 00249 return VmbErrorSuccess; 00250 } 00251 00252 VmbErrorType Frame::GetReceiveStatus( VmbFrameStatusType &rStatus ) const 00253 { 00254 rStatus = (VmbFrameStatusType)m_pImpl->m_frame.receiveStatus; 00255 00256 return VmbErrorSuccess; 00257 } 00258 00259 VmbErrorType Frame::GetImageSize( VmbUint32_t &rnImageSize ) const 00260 { 00261 rnImageSize = m_pImpl->m_frame.imageSize; 00262 00263 return VmbErrorSuccess; 00264 } 00265 00266 VmbErrorType Frame::GetAncillarySize( VmbUint32_t &rnAncillarySize ) const 00267 { 00268 rnAncillarySize = m_pImpl->m_frame.ancillarySize; 00269 00270 return VmbErrorSuccess; 00271 } 00272 00273 VmbErrorType Frame::GetBufferSize( VmbUint32_t &rnBufferSize ) const 00274 { 00275 rnBufferSize =m_pImpl-> m_frame.bufferSize; 00276 00277 return VmbErrorSuccess; 00278 } 00279 00280 VmbErrorType Frame::GetPixelFormat( VmbPixelFormatType &rPixelFormat ) const 00281 { 00282 rPixelFormat = (VmbPixelFormatType)m_pImpl->m_frame.pixelFormat; 00283 00284 return VmbErrorSuccess; 00285 } 00286 00287 VmbErrorType Frame::GetWidth( VmbUint32_t &rnWidth ) const 00288 { 00289 rnWidth = m_pImpl->m_frame.width; 00290 00291 return VmbErrorSuccess; 00292 } 00293 00294 VmbErrorType Frame::GetHeight( VmbUint32_t &rnHeight ) const 00295 { 00296 rnHeight = m_pImpl->m_frame.height; 00297 00298 return VmbErrorSuccess; 00299 } 00300 00301 VmbErrorType Frame::GetOffsetX( VmbUint32_t &rnOffsetX ) const 00302 { 00303 rnOffsetX = m_pImpl->m_frame.offsetX; 00304 00305 return VmbErrorSuccess; 00306 } 00307 00308 VmbErrorType Frame::GetOffsetY( VmbUint32_t &rnOffsetY ) const 00309 { 00310 rnOffsetY = m_pImpl->m_frame.offsetY; 00311 00312 return VmbErrorSuccess; 00313 } 00314 00315 VmbErrorType Frame::GetFrameID( VmbUint64_t &rnFrameID ) const 00316 { 00317 rnFrameID = m_pImpl->m_frame.frameID; 00318 00319 return VmbErrorSuccess; 00320 } 00321 00322 VmbErrorType Frame::GetTimestamp( VmbUint64_t &rnTimestamp ) const 00323 { 00324 rnTimestamp = m_pImpl->m_frame.timestamp; 00325 00326 return VmbErrorSuccess; 00327 } 00328 00329 }} // namespace AVT::VmbAPI