Result.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. //-----------------------------------------------------------------------------
  2. // Basler pylon SDK
  3. // Copyright (c) 2006-2021 Basler AG
  4. // http://www.baslerweb.com
  5. // Author: Hartmut Nebelung
  6. //-----------------------------------------------------------------------------
  7. /*!
  8. \file
  9. \brief Low Level API: Definition of classes that contain grabbed data.
  10. */
  11. #pragma once
  12. #ifndef RESULT_H__
  13. #define RESULT_H__
  14. #include <limits.h>
  15. #include <memory.h> // for memset
  16. #include <Base/GCTypes.h>
  17. #include <Base/GCException.h>
  18. #include <pylon/Platform.h>
  19. #include <pylon/StreamGrabber.h>
  20. #include <pylon/PixelType.h>
  21. #include <pylon/PayloadType.h>
  22. #include <pylon/ResultImage.h>
  23. #pragma pack(push, PYLON_PACKING)
  24. // Microsoft Visual Studio defines SIZE_MAX (see limits.h)
  25. // But VS 2003 does not define SIZE_MAX. In this case, we have to define SIZE_MAX
  26. // Because of this, the following lines must be AFTER #include limits.h
  27. #ifndef SIZE_MAX
  28. #define SIZE_MAX UINT_MAX
  29. #endif
  30. namespace Pylon
  31. {
  32. // Forward declaration
  33. class CPylonDataContainer;
  34. class CPylonDataComponent;
  35. class GrabResult;
  36. typedef CGrabResultImageT<const GrabResult&> CGrabResultImageRef;
  37. ///////////////////////////////////////////////////////////////////////
  38. /// \brief Low Level API: Possible grab status values
  39. ///
  40. /// \ingroup Pylon_LowLevelApi
  41. enum EGrabStatus
  42. {
  43. GrabStatus_Undefined = -1,
  44. _UndefinedGrabStatus = GrabStatus_Undefined, // Consider using GrabStatus_Undefined instead.
  45. GrabStatus_Idle, //!< Currently not used.
  46. Idle = GrabStatus_Idle, //!< Currently not used. For backward compatibility only.
  47. GrabStatus_Queued, //!< Grab request is in the input queue.
  48. Queued = GrabStatus_Queued, //!< Grab request is in the input queue. For backward compatibility only. Consider using GrabStatus_Queued instead.
  49. GrabStatus_Grabbed, //!< Grab request terminated successfully. Buffer is filled with data.
  50. Grabbed = GrabStatus_Grabbed, //!< Grab request terminated successfully. Buffer is filled with data. For backward compatibility only. Consider using GrabStatus_Grabbed instead.
  51. GrabStatus_Canceled, //!< Grab request was canceled. Buffer doesn't contain valid data.
  52. Canceled = GrabStatus_Canceled, //!< Grab request was canceled. Buffer doesn't contain valid data. For backward compatibility only. Consider using GrabStatus_Canceled instead.
  53. GrabStatus_Failed, //!< Grab request failed. Buffer doesn't contain valid data.
  54. Failed = GrabStatus_Failed //!< Grab request failed. Buffer doesn't contain valid data. For backward compatibility only. Consider using GrabStatus_Queued instead.
  55. };
  56. /// Retrieve the number of bits per pixel for a given pixel type
  57. #define BIT_PER_PIXEL( pixelType ) ( ( (pixelType) >> 16 ) & 0xff )
  58. ///////////////////////////////////////////////////////////////////////
  59. /// \brief Low Level API: A grab result that combines the used image buffer and status information.
  60. ///
  61. /// Note that depending on the used interface technology, the specific camera and
  62. /// the situation some of the attributes are not meaningful, e. g. timestamp in case
  63. /// of an canceled grab.
  64. ///
  65. /// \ingroup Pylon_LowLevelApi
  66. class PYLONBASE_API GrabResult
  67. {
  68. public:
  69. ///////////////////////////////////////////////////////////////////////
  70. //
  71. GrabResult()
  72. : m_pContext( NULL )
  73. , m_hBuffer( NULL )
  74. , m_pBuffer( NULL )
  75. , m_BufferSize( 0 )
  76. , m_Status( GrabStatus_Undefined )
  77. , m_PayloadType( PayloadType_Undefined )
  78. , m_PixelType( PixelType_Undefined )
  79. , m_TimeStamp( 0 )
  80. , m_SizeX( -1 )
  81. , m_SizeY( -1 )
  82. , m_OffsetX( -1 )
  83. , m_OffsetY( -1 )
  84. , m_PaddingX( -1 )
  85. , m_PaddingY( -1 )
  86. , m_PayloadSize( (uint64_t) -1 )
  87. , m_ErrorCode( 0 )
  88. , m_ErrorDescription( "" )
  89. , m_BlockID( GC_UINT64_MAX )
  90. {
  91. }
  92. ///////////////////////////////////////////////////////////////////////
  93. //
  94. ~GrabResult()
  95. {
  96. }
  97. ///////////////////////////////////////////////////////////////////////
  98. /// True if status is grabbed.
  99. bool Succeeded() const
  100. {
  101. return m_Status == GrabStatus_Grabbed;
  102. }
  103. ///////////////////////////////////////////////////////////////////////
  104. /// Get the buffer handle.
  105. StreamBufferHandle Handle() const
  106. {
  107. return m_hBuffer;
  108. }
  109. ///////////////////////////////////////////////////////////////////////
  110. /// Get the pointer to the buffer.
  111. void* Buffer() const
  112. {
  113. return const_cast<void*>(m_pBuffer);
  114. }
  115. ///////////////////////////////////////////////////////////////////////
  116. /// Get the grab status.
  117. EGrabStatus Status() const
  118. {
  119. return m_Status;
  120. }
  121. ///////////////////////////////////////////////////////////////////////
  122. /// Get the pointer the user provided context.
  123. const void* Context() const
  124. {
  125. return m_pContext;
  126. }
  127. ///////////////////////////////////////////////////////////////////////
  128. /// Get the actual payload type.
  129. EPayloadType GetPayloadType() const
  130. {
  131. return m_PayloadType;
  132. }
  133. ///////////////////////////////////////////////////////////////////////
  134. /// Get the actual pixel type. This is only defined in case of image data.
  135. EPixelType GetPixelType() const
  136. {
  137. return m_PixelType;
  138. }
  139. ///////////////////////////////////////////////////////////////////////
  140. /// \brief Get the camera specific tick count.
  141. ///
  142. /// In case of GigE-Vision this describes when the image exposure was started.
  143. /// Cameras that do not support this feature return zero. If supported this
  144. /// may be used to determine which ROIs were acquired simultaneously.
  145. uint64_t GetTimeStamp() const
  146. {
  147. return m_TimeStamp;
  148. }
  149. ///////////////////////////////////////////////////////////////////////
  150. /// Get the actual number of columns in pixel. This is only defined in case of image data.
  151. int32_t GetSizeX() const
  152. {
  153. return m_SizeX;
  154. }
  155. ///////////////////////////////////////////////////////////////////////
  156. /// Get the actual number of rows in pixel. This is only defined in case of image data.
  157. int32_t GetSizeY() const
  158. {
  159. return m_SizeY;
  160. }
  161. ///////////////////////////////////////////////////////////////////////
  162. /// Get the actual starting column. This is only defined in case of image data.
  163. int32_t GetOffsetX() const
  164. {
  165. return m_OffsetX;
  166. }
  167. ///////////////////////////////////////////////////////////////////////
  168. /// Get the actual starting row. This is only defined in case of image data.
  169. int32_t GetOffsetY() const
  170. {
  171. return m_OffsetY;
  172. }
  173. ///////////////////////////////////////////////////////////////////////
  174. /// Get the number of extra data at the end of each row in bytes. This is only defined in case of image data.
  175. int32_t GetPaddingX() const
  176. {
  177. return m_PaddingX;
  178. }
  179. ///////////////////////////////////////////////////////////////////////
  180. /// Get the number of extra data at the end of the image data in bytes. This is only defined in case of image data.
  181. int32_t GetPaddingY() const
  182. {
  183. return m_PaddingY;
  184. }
  185. ///////////////////////////////////////////////////////////////////////
  186. /// Get the number of valid bytes in the buffer returned by Buffer().
  187. int64_t GetPayloadSize() const
  188. {
  189. return m_PayloadSize;
  190. }
  191. ///////////////////////////////////////////////////////////////////////
  192. /// Get the number of valid bytes in the buffer returned by Buffer() as size_t.
  193. size_t GetPayloadSize_t() const
  194. {
  195. #if SIZE_MAX >= 0xffffffffffffffffULL
  196. return static_cast<size_t>(GetPayloadSize());
  197. #else
  198. if (m_PayloadSize > SIZE_MAX)
  199. {
  200. throw OUT_OF_RANGE_EXCEPTION( "PayloadSize too big" );
  201. }
  202. return static_cast<size_t>(m_PayloadSize & SIZE_MAX);
  203. #endif
  204. }
  205. ///////////////////////////////////////////////////////////////////////
  206. /// Get a description of the current error.
  207. String_t GetErrorDescription() const
  208. {
  209. return m_ErrorDescription;
  210. }
  211. ///////////////////////////////////////////////////////////////////////
  212. /// Get the current error code.
  213. uint32_t GetErrorCode() const
  214. {
  215. return m_ErrorCode;
  216. }
  217. ///////////////////////////////////////////////////////////////////////
  218. /// \brief Provides an adapter from the grab result to Pylon::IImage interface.
  219. ///
  220. /// This returned adapter allows passing the grab result to saving functions or image format converter.
  221. ///
  222. /// \attention The returned reference is only valid as long the grab result is not destroyed.
  223. CGrabResultImageRef GetImage() const
  224. {
  225. return CGrabResultImageRef( *this, false );
  226. }
  227. ///////////////////////////////////////////////////////////////////////
  228. /// \brief Get the block ID of the grabbed frame (camera device specific).
  229. ///
  230. /// \par IEEE 1394 Camera Devices
  231. /// The value of block ID is always UINT64_MAX.
  232. ///
  233. /// \par GigE Camera Devices
  234. /// The sequence number starts with 1 and
  235. /// wraps at 65535. The value 0 has a special meaning and indicates
  236. /// that this feature is not supported by the camera.
  237. ///
  238. /// \par USB Camera Devices
  239. /// The sequence number starts with 0 and uses the full 64 Bit range.
  240. ///
  241. /// \attention A block ID of value UINT64_MAX indicates that the Block ID is invalid and must not be used.
  242. uint64_t GetBlockID() const
  243. {
  244. return m_BlockID;
  245. }
  246. ///////////////////////////////////////////////////////////////////////////////
  247. /// \copydoc Pylon::CGrabResultData::GetDataContainer()
  248. CPylonDataContainer GetDataContainer() const;
  249. ///////////////////////////////////////////////////////////////////////////////
  250. /// \copydoc Pylon::CPylonDataContainer::GetDataComponentCount()
  251. size_t GetDataComponentCount() const;
  252. ///////////////////////////////////////////////////////////////////////////////
  253. /// \copydoc Pylon::CPylonDataContainer::GetDataComponent()
  254. CPylonDataComponent GetDataComponent( size_t index );
  255. ///////////////////////////////////////////////////////////////////////
  256. /// Get the size of the buffer returned by Buffer().
  257. size_t GetBufferSize() const
  258. {
  259. return m_BufferSize;
  260. }
  261. protected:
  262. const void* m_pContext;
  263. StreamBufferHandle m_hBuffer;
  264. const void* m_pBuffer;
  265. size_t m_BufferSize;
  266. EGrabStatus m_Status;
  267. EPayloadType m_PayloadType;
  268. EPixelType m_PixelType;
  269. uint64_t m_TimeStamp;
  270. int32_t m_SizeX;
  271. int32_t m_SizeY;
  272. int32_t m_OffsetX;
  273. int32_t m_OffsetY;
  274. int32_t m_PaddingX;
  275. int32_t m_PaddingY;
  276. uint64_t m_PayloadSize;
  277. uint32_t m_ErrorCode;
  278. String_t m_ErrorDescription;
  279. uint64_t m_BlockID;
  280. };
  281. ///////////////////////////////////////////////////////////////////////
  282. /// \brief Low Level API: An event result
  283. /// \ingroup Pylon_LowLevelApi
  284. class EventResult
  285. {
  286. public:
  287. ///////////////////////////////////////////////////////////////////////
  288. //
  289. EventResult()
  290. : m_ReturnCode( 0 )
  291. , m_Message()
  292. {
  293. // prevent uninitialized member warning
  294. memset( Buffer, 0, sizeof( Buffer ) );
  295. }
  296. ///////////////////////////////////////////////////////////////////////
  297. //
  298. ~EventResult()
  299. {
  300. }
  301. ///////////////////////////////////////////////////////////////////////
  302. //
  303. bool Succeeded() const
  304. {
  305. return 0 == m_ReturnCode;
  306. }
  307. ///////////////////////////////////////////////////////////////////////
  308. //
  309. String_t ErrorDescription() const
  310. {
  311. return m_Message;
  312. }
  313. ///////////////////////////////////////////////////////////////////////
  314. //
  315. unsigned long ErrorCode() const
  316. {
  317. return m_ReturnCode;
  318. }
  319. protected:
  320. unsigned long m_ReturnCode;
  321. String_t m_Message;
  322. public:
  323. unsigned char Buffer[576];
  324. };
  325. ///////////////////////////////////////////////////////////////////////
  326. /// \brief Low Level API: Adapts a copy of a grab result to pylon image.
  327. ///
  328. /// \attention The referenced grab result must not be destroyed and the result's buffer must not be queued for grabbing again during the lifetime of this object.
  329. ///
  330. /// \ingroup Pylon_LowLevelApi
  331. typedef CGrabResultImageT<GrabResult> CGrabResultImage;
  332. }
  333. #pragma pack(pop)
  334. #endif // RESULT_H__