PylonImage.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. //------------------------------------------------------------------------------
  2. // Basler pylon SDK
  3. // Copyright (c) 2010-2021 Basler AG
  4. // http://www.baslerweb.com
  5. // Author: Andreas Gau
  6. //------------------------------------------------------------------------------
  7. /*!
  8. \file
  9. \brief Contains an image class.
  10. */
  11. #ifndef INCLUDED_PYLONIMAGE_H_6241114
  12. #define INCLUDED_PYLONIMAGE_H_6241114
  13. #include <pylon/Platform.h>
  14. #ifdef _MSC_VER
  15. # pragma pack(push, PYLON_PACKING)
  16. #endif /* _MSC_VER */
  17. #include <pylon/stdinclude.h>
  18. #include <pylon/PylonUtility.h>
  19. #include <pylon/PylonImageBase.h>
  20. #include <pylon/PylonImageUserBufferEventHandler.h>
  21. namespace Pylon
  22. {
  23. // Forward references.
  24. class CGrabResultPtr;
  25. /** \addtogroup Pylon_ImageHandlingSupport
  26. * @{
  27. */
  28. /*!
  29. \class CPylonImage
  30. \brief Describes an image.
  31. <ul>
  32. <li> Automatically handles size and lifetime of the image buffer.
  33. <li> Allows to take over a buffer of grab result which is preventing its reuse as long as required.
  34. <li> Allows to connect user buffers or buffers provided by third party software packages.
  35. <li> Provides methods for loading and saving an image in different file formats.
  36. <li> Serves as the main target format for the image format converter \c CImageFormatConverter.
  37. <li> Eases working with planar images.
  38. <li> Eases extraction of AOIs, e.g. for thumbnail images of defects.
  39. </ul>
  40. \par Buffer Handling:
  41. The buffer that is automatically created by the CPylonImage class or a hosted grab result buffer are replaced by a larger buffer if required.
  42. The size of the allocated buffer is never decreased.
  43. Referenced user buffers are never automatically replaced by a larger buffer.
  44. Referenced grab result buffers are never reused. See the Reset() method for more details.
  45. The Release() method can be used to detach a user buffer, release a hosted grab result buffer or to free an allocated buffer.
  46. \threading
  47. The CPylonImage class is not thread-safe.
  48. */
  49. class PYLONUTILITY_API CPylonImage : public CPylonImageBase
  50. {
  51. public:
  52. typedef void (*DeleteNotifyFunction_t) (void*);
  53. public:
  54. /*!
  55. \brief Creates an invalid image.
  56. See Pylon::IImage on how the properties of an invalid image are returned.
  57. \error
  58. Does not throw C++ exceptions.
  59. */
  60. CPylonImage();
  61. /*!
  62. \brief Copies the image properties and creates a reference to the buffer of the source image.
  63. \param[in] source The source image.
  64. \post
  65. <ul>
  66. <li> Another reference to the source image buffer is created.
  67. <li> Creates an invalid image if the source image is invalid.
  68. </ul>
  69. \error
  70. Does not throw C++ exceptions.
  71. */
  72. CPylonImage( const CPylonImage& source );
  73. /*!
  74. \brief Destroys a pylon image object.
  75. \error
  76. Does not throw C++ exceptions.
  77. */
  78. virtual ~CPylonImage();
  79. /*!
  80. \brief Copies the image properties and creates a reference to the buffer of the source image.
  81. \param[in] source The source image.
  82. \post
  83. <ul>
  84. <li> Another reference to the source image buffer is created.
  85. <li> Creates an invalid image if the source image is invalid.
  86. </ul>
  87. \error
  88. Does not throw C++ exceptions.
  89. */
  90. virtual CPylonImage& operator= ( const CPylonImage& source );
  91. /*!
  92. \brief Creates an image and allocates a buffer for it.
  93. \param[in] pixelType The pixel type of the new image.
  94. \param[in] width The number of pixels in a row in the new image.
  95. \param[in] height The number of rows in the new image.
  96. \param[in] paddingX The number of extra data bytes at the end of each row.
  97. \param[in] orientation The vertical orientation of the image in the image buffer.
  98. \pre
  99. <ul>
  100. <li> The pixel type must be valid.
  101. <li> The \c width value must be >= 0 and < _I32_MAX.
  102. <li> The \c height value must be >= 0 and < _I32_MAX.
  103. </ul>
  104. \error
  105. Throws an exception when the parameters are invalid.
  106. Throws an exception when no buffer with the required size could be allocated.
  107. */
  108. static CPylonImage Create( EPixelType pixelType, uint32_t width, uint32_t height, size_t paddingX = 0, EImageOrientation orientation = ImageOrientation_TopDown );
  109. /*!
  110. \brief Copies the image data from a different image.
  111. This method is used for making a full copy of an image.
  112. Calls the Reset() method to set the same image properties as the source image and
  113. copies the image data.
  114. \param[in] image The source image, e.g. a CPylonImage, CPylonBitmapImage, or Grab Result Smart Pointer object.
  115. \pre
  116. The preconditions of the Reset() method must be met.
  117. \post
  118. <ul>
  119. <li> The image contains a copy of the image data contained by the source image.
  120. <li> Creates an invalid image if the source image is invalid.
  121. </ul>
  122. \error
  123. Throws an exception when no buffer with the required size could be allocated.
  124. Throws an exception when the preconditions of the Reset() method are not met.
  125. */
  126. virtual void CopyImage( const IImage& image );
  127. /*!
  128. \brief Copies the image data from a different image and changes the padding while copying.
  129. This method is used for making a full copy of an image except for changing the padding.
  130. Calls the Reset() method to set the same image properties as the source image and
  131. copies the image data.
  132. This method is useful in combination with the GetAoi() method.
  133. \param[in] image The source image, e.g. a CPylonImage, CPylonBitmapImage, or Grab Result Smart Pointer object.
  134. \param[in] newPaddingX The number of extra data bytes at the end of each row.
  135. \pre
  136. <ul>
  137. <li> The preconditions of the Reset() method must be met.
  138. <li> The rows of the source image must be byte aligned. This may not be the case for packed pixel types. See Pylon::IsPacked().
  139. <li> The rows of the newly created image must be byte aligned. This may not be the case for packed pixel types. See Pylon::IsPacked().
  140. </ul>
  141. \post
  142. <ul>
  143. <li> The image contains a copy of the image data contained by the source image.
  144. <li> The line padding is adjusted.
  145. <li> The byte aligned row padding area is set to zero.
  146. <li> Creates an invalid image if the source image is invalid.
  147. </ul>
  148. \error
  149. Throws an exception when no buffer with the required size could be allocated.
  150. Throws an exception when the preconditions of the Reset() method are not met.
  151. */
  152. virtual void CopyImage( const IImage& image, size_t newPaddingX );
  153. /*!
  154. \brief Copies the image data from a provided buffer.
  155. This method is used for making a full copy of an image.
  156. Calls the Reset() method to set the same image properties as the source image and
  157. copies the image data.
  158. \param[in] pBuffer The pointer to the buffer of the source image.
  159. \param[in] bufferSizeBytes The size of the buffer of the source image.
  160. \param[in] pixelType The pixel type of the source image.
  161. \param[in] width The number of pixels in a row in the source image.
  162. \param[in] height The number of rows in the source image.
  163. \param[in] paddingX The number of extra data bytes at the end of each row.
  164. \param[in] orientation The vertical orientation of the image in the image buffer.
  165. \pre
  166. <ul>
  167. <li> The pixel type must be valid.
  168. <li> The \c width value must be >= 0 and < _I32_MAX.
  169. <li> The \c height value must be >= 0 and < _I32_MAX.
  170. <li> The pointer to the source buffer must not be NULL.
  171. <li> The source buffer must be large enough to hold the image described by the parameters.
  172. <li> The preconditions of the Reset() method must be met.
  173. </ul>
  174. \post A copy of the image contained by the source image buffer is made.
  175. \error
  176. Throws an exception when no buffer with the required size could be allocated.
  177. Throws an exception when the preconditions of the Reset() method are not met.
  178. */
  179. virtual void CopyImage(
  180. void* pBuffer,
  181. size_t bufferSizeBytes,
  182. EPixelType pixelType,
  183. uint32_t width,
  184. uint32_t height,
  185. size_t paddingX,
  186. EImageOrientation orientation = ImageOrientation_TopDown );
  187. /*!
  188. \brief Attaches a grab result buffer using additional hints.
  189. This allows to display grabbed data in a user-defined way.
  190. \param[in] grabResult The source buffer.
  191. \param[in] pixelType The pixel type of the source image.
  192. \param[in] width The number of pixels in a row in the source image.
  193. \param[in] height The number of rows in the source image.
  194. \param[in] paddingX The number of extra data bytes at the end of each row.
  195. \param[in] orientation The vertical orientation of the image in the image buffer.
  196. \pre
  197. <ul>
  198. <li> The \c grabResult must be valid.
  199. <li> The \c pixelType must be valid.
  200. <li> The \c width value must be >= 0 and < _I32_MAX.
  201. <li> The \c height value must be >= 0 and < _I32_MAX.
  202. </ul>
  203. \post
  204. <ul>
  205. <li> The image properties are taken from the \c width, \c height, \c pixelType, \c paddingX and \c orientation parameters.
  206. <li> The grab result buffer is used by the image class.
  207. <li> Another reference to the grab result buffer is created. This prevents the buffer's reuse for grabbing.
  208. </ul>
  209. \error
  210. Throws an exception if the preconditions are not met.
  211. Throws an exception if the buffer size of the grabresult is too small for an image with the given parameters.
  212. */
  213. virtual void AttachGrabResultBufferWithUserHints(
  214. const CGrabResultPtr& grabResult,
  215. EPixelType pixelType,
  216. uint32_t width,
  217. uint32_t height,
  218. size_t paddingX,
  219. EImageOrientation orientation = ImageOrientation_TopDown );
  220. /*!
  221. \brief Attaches a grab result buffer.
  222. \param[in] grabResult The source image represented by a grab result.
  223. \post
  224. <ul>
  225. <li> The image properties are taken from the grab result.
  226. <li> The grab result buffer is used by the image class.
  227. <li> Another reference to the grab result buffer is created. This prevents the buffer's reuse for grabbing.
  228. <li> Creates an invalid image if the \c grabResult is invalid.
  229. <li> Creates an invalid image if the grab was not successful. See CGrabResultData::GrabSucceeded().
  230. </ul>
  231. \error
  232. Throws an exception when no buffer with the required size could be allocated.
  233. Throws an exception when the preconditions of the Reset() method are not met.
  234. */
  235. virtual void AttachGrabResultBuffer( const CGrabResultPtr& grabResult );
  236. /*!
  237. \brief Attaches a user buffer.
  238. \param[in] pBuffer The pointer to the buffer of the source image. CPylonImage will never free any user buffers.
  239. \param[in] bufferSizeBytes The size of the buffer of the source image.
  240. \param[in] pixelType The pixel type of the source image.
  241. \param[in] width The number of pixels in a row in the source image.
  242. \param[in] height The number of rows in the source image.
  243. \param[in] paddingX The number of extra data bytes at the end of each row.
  244. \param[in] orientation The vertical orientation of the image in the image buffer.
  245. \param[in] pEventHandler A pointer to an optional CPylonImageUserBufferEventHandler-derived object
  246. called when the user-supplied buffer is not used anymore.
  247. You can use this to free the user-supplied buffer.
  248. In case the function throws an exception, the handler will not be called.
  249. When attaching a user buffer and passing a pEventHandler, the user is responsible for ensuring the object is valid
  250. until CPylonImageUserBufferEventHandler::OnPylonImageUserBufferDetached() has been called.
  251. The user is also responsible to free the handler object after CPylonImageUserBufferEventHandler::OnPylonImageUserBufferDetached()
  252. has been called. After the function has returned, CPylonImage won't access the object anymore.
  253. See \c CPylonImageUserBufferEventHandler::OnPylonImageUserBufferDetached() for a sample.
  254. \pre
  255. <ul>
  256. <li> The pixel type must be valid.
  257. <li> The \c width value must be >= 0 and < _I32_MAX.
  258. <li> The \c height value must be >= 0 and < _I32_MAX.
  259. <li> The pointer to the source buffer must not be NULL.
  260. <li> The source buffer must be large enough to hold the image described by the parameters.
  261. <li> The buffer passed in \c pBuffer must not be currently attached.
  262. </ul>
  263. \post
  264. <ul>
  265. <li> The image properties are taken from the parameters passed.
  266. <li> The user buffer is used by the image class.
  267. <li> The user buffer must not be freed while being attached.
  268. </ul>
  269. \error
  270. Throws an exception if the preconditions are not met. In this case the an optional handler passed in \ pEventHandler will not be called.
  271. */
  272. virtual void AttachUserBuffer(
  273. void* pBuffer,
  274. size_t bufferSizeBytes,
  275. EPixelType pixelType,
  276. uint32_t width,
  277. uint32_t height,
  278. size_t paddingX,
  279. EImageOrientation orientation = ImageOrientation_TopDown,
  280. CPylonImageUserBufferEventHandler* pEventHandler = NULL);
  281. // Implements Pylon::IImage.
  282. virtual bool IsValid() const;
  283. // Implements Pylon::IImage.
  284. virtual EPixelType GetPixelType() const;
  285. // Implements Pylon::IImage.
  286. virtual uint32_t GetWidth() const;
  287. // Implements Pylon::IImage.
  288. virtual uint32_t GetHeight() const;
  289. // Implements Pylon::IImage.
  290. virtual size_t GetPaddingX() const;
  291. // Implements Pylon::IImage.
  292. virtual EImageOrientation GetOrientation() const;
  293. // Implements Pylon::IImage.
  294. virtual void* GetBuffer();
  295. // Implements Pylon::IImage.
  296. virtual const void* GetBuffer() const;
  297. // Implements Pylon::IImage.
  298. virtual size_t GetImageSize() const;
  299. // Implements Pylon::IImage.
  300. virtual bool IsUnique() const;
  301. // Implements Pylon::IImage.
  302. virtual bool GetStride( size_t& strideBytes ) const;
  303. // Implements Pylon::IReusableImage.
  304. virtual bool IsSupportedPixelType( EPixelType pixelType ) const;
  305. // Implements Pylon::IReusableImage, always returns true.
  306. virtual bool IsAdditionalPaddingSupported() const;
  307. //Implements Pylon::IReusableImage
  308. /*!
  309. \brief Resets the image properties and allocates a new buffer if required.
  310. \param[in] pixelType The pixel type of the new image.
  311. \param[in] width The number of pixels in a row in the new image.
  312. \param[in] height The number of rows in the new image.
  313. \param[in] orientation The vertical orientation of the image in the image buffer.
  314. \pre
  315. <ul>
  316. <li> The \c width value must be >= 0 and < _I32_MAX.
  317. <li> The \c height value must be >= 0 and < _I32_MAX.
  318. <li> If a user buffer is referenced then this buffer must not be referenced by another pylon image. See the IsUnique() and IsUserBufferAttached() methods.
  319. <li> If a user buffer is referenced then this buffer must be large enough to hold the destination image. See the GetAllocatedBufferSize() and IsUserBufferAttached() methods.
  320. </ul>
  321. \post
  322. <ul>
  323. <li> If the previously referenced buffer is a grab result buffer, a new buffer has been allocated.
  324. <li> If the previously referenced buffer is also referenced by another pylon image, a new buffer has been allocated.
  325. <li> If the previously referenced buffer is not large enough to hold an image with the given properties, a new buffer has been allocated.
  326. <li> If no buffer has been allocated before, a buffer has been allocated.
  327. </ul>
  328. \error
  329. Throws an exception when the preconditions are not met.
  330. Throws an exception when no buffer with the required size could be allocated.
  331. */
  332. virtual void Reset( EPixelType pixelType, uint32_t width, uint32_t height, EImageOrientation orientation = ImageOrientation_TopDown );
  333. // Implements Pylon::IReusableImage, user defined padding is always supported.
  334. /*!
  335. \brief Extends the Reset( EPixelType, uint32_t, uint32_t, EImageOrientation) method by settable paddingX.
  336. \copydetails Reset( EPixelType, uint32_t, uint32_t, EImageOrientation)
  337. \param[in] paddingX The number of extra data bytes at the end of each row.
  338. */
  339. virtual void Reset( EPixelType pixelType, uint32_t width, uint32_t height, size_t paddingX, EImageOrientation orientation = ImageOrientation_TopDown );
  340. // Implements Pylon::IReusableImage.
  341. virtual void Release();
  342. /// Returns true if the referenced buffer has been provided by the user.
  343. virtual bool IsUserBufferAttached() const;
  344. /// Returns true if the referenced buffer has been provided by a grab result.
  345. virtual bool IsGrabResultBufferAttached() const;
  346. /*!
  347. \brief Returns the size of the used buffer.
  348. This method is useful when working with so-called user buffers.
  349. \error
  350. Does not throw C++ exceptions.
  351. */
  352. virtual size_t GetAllocatedBufferSize() const;
  353. /*!
  354. \brief Changes the pixel type of the image.
  355. \param[in] pixelType The new pixel type.
  356. \pre
  357. <ul>
  358. <li> Pylon::SamplesPerPixel( oldPixelType) == Pylon::SamplesPerPixel( newPixelType)
  359. <li> Pylon::BitPerPixel( oldPixelType) == Pylon::BitPerPixel( newPixelType)
  360. </ul>
  361. \error
  362. Throws an exception when the new pixel type properties do not match the existing ones.
  363. */
  364. virtual void ChangePixelType( EPixelType pixelType );
  365. /*!
  366. \brief Creates a new pylon image for a plane of the image. No image data is copied.
  367. Use CopyImage( const IImage& image) to create a full copy.
  368. \code
  369. copiedPlane.CopyImage( planarImage.GetPlane( 2));
  370. \endcode
  371. \param[in] planeIndex The zero based index of the plane.
  372. \return A pylon image referencing a plane of the image.
  373. \pre
  374. The value of planeIndex < Pylon::PlaneCount( GetPixelType()).
  375. \post
  376. <ul>
  377. <li> A reference to the same buffer is created. No image data is copied.
  378. <li> The returned image has the Pylon::GetPlanePixelType( GetPixelType()) pixel type.
  379. <li> If the image is not planar only index 0 is allowed. A call passing index 0 returns a copy of the image. No image data is copied.
  380. </ul>
  381. \error
  382. Throws an exception when the plane index is out of range.
  383. */
  384. virtual CPylonImage GetPlane( size_t planeIndex ) const;
  385. /*!
  386. \brief Creates a new pylon image for an image area of interest (Image AOI) derived from the image. No image data is copied.
  387. Use CopyImage( const IImage& image, size_t newPaddingX) to create a full copy and to remove the additional padding.
  388. \code
  389. copiedAoi.CopyImage( image.GetAoi( 80, 90, 100, 100), 0);
  390. \endcode
  391. \param[in] topLeftX The x-coordinate of the top left corner of the image AOI in pixels.
  392. \param[in] topLeftY The y-coordinate of the top left corner of the image AOI in pixels.
  393. \param[in] width The width of the image AOI in pixels.
  394. \param[in] height The height of the image AOI in pixels.
  395. \return A pylon image referencing an image AOI of the image.
  396. \pre
  397. <ul>
  398. <li> The image must be valid.
  399. <li> The image AOI is located inside the image.
  400. <li> The image is not in a planar format, see Pylon::IsPlanar(). Use GetPlane() first in this case.
  401. <li> The rows of the image must be byte aligned. This may not be the case for packed pixel types. See Pylon::IsPacked().
  402. <li> The x-coordinate must be byte aligned. This may not be the case for packed pixel types. See Pylon::IsPacked().
  403. <li> The \c topLeftX parameter must be divisible by the return value of Pylon::GetPixelIncrementX() for the image's pixel type.
  404. <li> The \c topLeftY parameter must be divisible by the return value of Pylon::GetPixelIncrementY() for the image's pixel type.
  405. </ul>
  406. \post
  407. <ul>
  408. <li> A reference to the same buffer is created. The image data is not copied.
  409. <li> The returned image uses the paddingX property to skip over image content outside of the image AOI.
  410. </ul>
  411. \error
  412. Throws an exception when the preconditions are not met.
  413. */
  414. virtual CPylonImage GetAoi( uint32_t topLeftX, uint32_t topLeftY, uint32_t width, uint32_t height ) const;
  415. private:
  416. // Internal use only.
  417. class CPylonImageImpl;
  418. CPylonImageImpl* m_pImpl;
  419. };
  420. /**
  421. * @}
  422. */
  423. }
  424. #ifdef _MSC_VER
  425. # pragma pack(pop)
  426. #endif /* _MSC_VER */
  427. #endif /* INCLUDED_PYLONIMAGE_H_6241114 */