ImageFormatConverter.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 format converter class.
  10. */
  11. #ifndef INCLUDED_IMAGEFORMATCONVERTER_H_1564142
  12. #define INCLUDED_IMAGEFORMATCONVERTER_H_1564142
  13. #include <pylon/Platform.h>
  14. #ifdef _MSC_VER
  15. # pragma pack(push, PYLON_PACKING)
  16. #endif /* _MSC_VER */
  17. #ifdef _MSC_VER
  18. #pragma warning( push )
  19. #pragma warning( disable : 4275 ) // Class must have a dll interface to be used by clients of the class.
  20. #endif
  21. #include <pylon/stdinclude.h>
  22. #include <pylon/PylonUtility.h>
  23. #include <pylon/_ImageFormatConverterParams.h>
  24. #include <pylon/ReusableImage.h>
  25. namespace Pylon
  26. {
  27. using namespace Basler_ImageFormatConverterParams;
  28. /** \addtogroup Pylon_ImageHandlingSupport
  29. * @{
  30. */
  31. /*!
  32. \class CImageFormatConverter
  33. \brief Creates new images by converting a source image to another format.
  34. Supported input image formats defined by the pixel type:
  35. <ul>
  36. <li> PixelType_Mono1packed
  37. <li> PixelType_Mono2packed
  38. <li> PixelType_Mono4packed
  39. <li> PixelType_Mono8
  40. <li> PixelType_Mono10
  41. <li> PixelType_Mono10packed
  42. <li> PixelType_Mono10p
  43. <li> PixelType_Mono12
  44. <li> PixelType_Mono12packed
  45. <li> PixelType_Mono12p
  46. <li> PixelType_Mono16
  47. </ul>
  48. <ul>
  49. <li> PixelType_BayerGR8
  50. <li> PixelType_BayerRG8
  51. <li> PixelType_BayerGB8
  52. <li> PixelType_BayerBG8
  53. <li> PixelType_BayerGR10
  54. <li> PixelType_BayerRG10
  55. <li> PixelType_BayerGB10
  56. <li> PixelType_BayerBG10
  57. <li> PixelType_BayerGR12
  58. <li> PixelType_BayerRG12
  59. <li> PixelType_BayerGB12
  60. <li> PixelType_BayerBG12
  61. <li> PixelType_BayerGR12Packed
  62. <li> PixelType_BayerRG12Packed
  63. <li> PixelType_BayerGB12Packed
  64. <li> PixelType_BayerBG12Packed
  65. <li> PixelType_BayerGR10p
  66. <li> PixelType_BayerRG10p
  67. <li> PixelType_BayerGB10p
  68. <li> PixelType_BayerBG10p
  69. <li> PixelType_BayerGR12p
  70. <li> PixelType_BayerRG12p
  71. <li> PixelType_BayerGB12p
  72. <li> PixelType_BayerBG12p
  73. <li> PixelType_BayerGR16
  74. <li> PixelType_BayerRG16
  75. <li> PixelType_BayerGB16
  76. <li> PixelType_BayerBG16
  77. </ul>
  78. <ul>
  79. <li> PixelType_RGB8packed
  80. <li> PixelType_BGR8packed
  81. <li> PixelType_RGBA8packed
  82. <li> PixelType_BGRA8packed
  83. <li> PixelType_RGB10packed
  84. <li> PixelType_BGR10packed
  85. <li> PixelType_RGB12packed
  86. <li> PixelType_BGR12packed
  87. <li> PixelType_RGB12V1packed
  88. <li> PixelType_RGB16packed
  89. <li> PixelType_RGB8planar
  90. <li> PixelType_RGB16planar
  91. </ul>
  92. <ul>
  93. <li> PixelType_YUV422packed
  94. <li> PixelType_YUV422_YUYV_Packed
  95. <li> PixelType_YCbCr422_8_YY_CbCr_Semiplanar
  96. <li> PixelType_YCbCr420_8_YY_CbCr_Semiplanar
  97. </ul>
  98. Supported output image formats defined by the pixel type:
  99. <ul>
  100. <li> PixelType_BGRA8packed - This pixel type can be used in Windows bitmaps. See Pylon::SBGRA8Pixel.
  101. <li> PixelType_BGR8packed - This pixel type can be used in Windows bitmaps. See Pylon::SBGR8Pixel.
  102. <li> PixelType_RGB8packed - See Pylon::SRGB8Pixel.
  103. <li> PixelType_RGB16packed - See Pylon::SRGB16Pixel.
  104. <li> PixelType_RGB8planar
  105. <li> PixelType_RGB16planar
  106. <li> PixelType_Mono8
  107. <li> PixelType_Mono16
  108. <li> PixelType_YUV444planar
  109. <li> PixelType_YUV422planar
  110. <li> PixelType_YUV420planar
  111. </ul>
  112. All input image formats can be converted to all output image formats.
  113. RGB, BGR and Bayer image formats are converted to monochrome formats by using the following formula:
  114. \code
  115. mono = (0.25 * red) + (0.625 * green) + (0.125 * blue);
  116. \endcode
  117. YUV formats are converted to 16 bit bit depth in an intermediate conversion step. This is why the output
  118. is always aligned at the most significant bit when converting to 16 bit color output formats like PixelType_RGB16packed.
  119. \par Limitations:
  120. The last column of a YUV input image with odd width cannot be converted.
  121. The last column and the last row of a Bayer input image cannot be converted.
  122. The default treatment of rows and columns that cannot be converted due to their location on edges,
  123. can be controlled using the CImageFormatConverter::InconvertibleEdgeHandling parameter.
  124. See also the Convert() method description.
  125. \threading
  126. The CImageFormatConverter class is not thread-safe.
  127. */
  128. class PYLONUTILITY_API CImageFormatConverter : public CImageFormatConverterParams_Params
  129. {
  130. public:
  131. /*!
  132. \brief Creates an image format converter.
  133. \error
  134. Does not throw C++ exceptions.
  135. */
  136. CImageFormatConverter();
  137. /*!
  138. \brief Destroys the image format converter.
  139. \error
  140. Does not throw C++ exceptions.
  141. */
  142. virtual ~CImageFormatConverter();
  143. /*!
  144. \brief Optionally initializes the image format converter before conversion.
  145. \param[in] sourcePixelType The pixel type of the source image.
  146. <ul>
  147. <li> Depending on parameter settings and the input format, data structures required for conversion are created, e.g. lookup tables.
  148. <li> Initialization is done automatically when calling Convert() if needed. This may add a delay when converting the first image.
  149. </ul>
  150. \pre
  151. <ul>
  152. <li> The converter parameters are set up.
  153. <li> The \c pixelTypeSource must be supported by the converter.
  154. </ul>
  155. Lookup tables are created when using monochrome images as input and when
  156. the gamma conversion method is selected
  157. or when the shift conversion method is selected and the value of AdditionalLeftShift is not zero.
  158. The converter can be reinitialized with other settings if required.
  159. \error
  160. Throws an exception if the passed pixel type does not represent a valid input format.
  161. The converter object is still valid after error and can be initialized again.
  162. */
  163. virtual void Initialize( EPixelType sourcePixelType );
  164. /*!
  165. \brief Returns information about the converter being initialized.
  166. \param[in] sourcePixelType The pixel type of the source image.
  167. The result depends on the converter settings.
  168. \return True if initialized.
  169. \error
  170. Does not throw C++ exceptions.
  171. */
  172. virtual bool IsInitialized( EPixelType sourcePixelType ) const;
  173. /*!
  174. \brief Destroys data structures required for conversion.
  175. This function can be called to free resources held by the format converter.
  176. \error
  177. Does not throw C++ exceptions.
  178. */
  179. virtual void Uninitialize();
  180. /*!
  181. \brief Checks to see if a conversion is required or if the source image already has the desired format.
  182. \param[in] sourceImage The source image, e.g. a CPylonImage, CPylonBitmapImage, or Grab Result Smart Pointer object.
  183. \return Returns true if the source image already has the desired format.
  184. A conversion may even be required image format does not change e.g. if the gamma conversion method is selected and the format describes a monochrome image.
  185. \error
  186. Does not throw C++ exceptions.
  187. */
  188. virtual bool ImageHasDestinationFormat( const IImage& sourceImage ) const;
  189. /*!
  190. \brief Checks to see if a conversion is required or if the source image already has the desired format.
  191. \param[in] sourcePixelType The pixel type of the source image.
  192. \param[in] sourcePaddingX The number of extra data bytes at the end of each row. The default value is usually 0.
  193. \param[in] sourceOrientation The vertical orientation of the image in the image buffer. The default value is usually ImageOrientation_TopDown.
  194. \return Returns true if the source image already has the desired format. This is done according to the current converter settings.
  195. A conversion may even be required image format does not change e.g. if the gamma conversion method is selected and the format describes a monochrome image.
  196. \error
  197. Does not throw C++ exceptions.
  198. */
  199. virtual bool ImageHasDestinationFormat( EPixelType sourcePixelType, size_t sourcePaddingX, EImageOrientation sourceOrientation ) const;
  200. /*!
  201. \brief Computes the size of the destination image buffer in byte.
  202. \param[in] sourceImage The source image, e.g. a CPylonImage, CPylonBitmapImage, or Grab Result Smart Pointer object.
  203. \return The size of the destination image when converting the given source image using current converter settings.
  204. \error
  205. Throws an exception if the destination image size for the passed input cannot be computed. The converter object is still valid after error.
  206. */
  207. virtual size_t GetBufferSizeForConversion( const IImage& sourceImage ) const;
  208. /*!
  209. \brief Computes the size of the destination image buffer in byte.
  210. \param[in] sourceWidth The number of pixels in a row in the source image.
  211. \param[in] sourceHeight The number of rows in the source image.
  212. \param[in] sourcePixelType The pixel type of the source image.
  213. \return The size of the destination image when converting the source image using current converter settings.
  214. \pre
  215. <ul>
  216. <li> The \c sourceWidth value must be >= 0 and < _I32_MAX.
  217. <li> The \c sourceHeight value must be >= 0 and < _I32_MAX.
  218. </ul>
  219. \error
  220. Throws an exception if the destination image size for the passed input cannot be computed. The converter object is still valid after error.
  221. */
  222. virtual size_t GetBufferSizeForConversion( EPixelType sourcePixelType, uint32_t sourceWidth, uint32_t sourceHeight ) const;
  223. /*!
  224. \brief Creates a new image by converting an image to a different format.
  225. The IReusableImage::Reset() method of the destination image is called to set the destination format.
  226. The image is converted to the destination image according to the current converter settings.
  227. The padding area of a row in the destination image is set to zero.
  228. The OutputPaddingX setting is ignored for images that do not support user defined padding, e.g.
  229. CPylonBitmapImage. See also IReusableImage::IsAdditionalPaddingSupported().
  230. \param[out] destinationImage The destination image, e.g. a CPylonImage or CPylonBitmapImage object.
  231. When passing a CPylonBitmapImage object the target format must be supported by the CPylonBitmapImage class.
  232. \param[in] sourceImage The source image, e.g. a CPylonImage, CPylonBitmapImage, or Grab Result Smart Pointer object.
  233. \pre
  234. <ul>
  235. <li> The source and destination images must be different images.
  236. <li> The source image must be valid.
  237. <li> The format of the source image must be supported by the converter.
  238. <li> The destination image must support the destination format.
  239. <li> The destination image must be able to provide a large enough buffer to hold the image.
  240. </ul>
  241. \error
  242. Throws an exception if the passed parameters are not valid. The converter object is still valid after error.
  243. */
  244. virtual void Convert( IReusableImage& destinationImage, const IImage& sourceImage );
  245. /*!
  246. \brief Creates a new image by converting an image to a different format.
  247. The IReusableImage::Reset() method of the destination image is called to set the destination format.
  248. The image is converted to the destination image according to the current converter settings.
  249. The padding area of a row in the destination image is set to zero.
  250. The OutputPaddingX setting is ignored for images that do not support user defined padding, e.g.
  251. CPylonBitmapImage. See also IReusableImage::IsAdditionalPaddingSupported().
  252. \param[out] destinationImage The destination image.
  253. \param[in] pSourceBuffer The pointer to the buffer of the source image.
  254. \param[in] sourceBufferSizeBytes The size of the buffer of the source image.
  255. \param[in] sourcePixelType The pixel type of the source image.
  256. \param[in] sourceWidth The number of pixels in a row in the source image.
  257. \param[in] sourceHeight The number of rows in the source image.
  258. \param[in] sourcePaddingX The number of extra data bytes at the end of each row. The default value is usually 0.
  259. \param[in] sourceOrientation The vertical orientation of the source image in the image buffer. The default value is usually ImageOrientation_TopDown.
  260. \pre
  261. <ul>
  262. <li> The pixel type must be valid.
  263. <li> The \c sourceWidth value must be >= 0 and < _I32_MAX.
  264. <li> The \c sourceHeight value must be >= 0 and < _I32_MAX.
  265. <li> The pointer to the source buffer must not be NULL.
  266. <li> The source buffer must be large enough to hold the image described by the parameters.
  267. <li> The format of the input image represented by the given parameter must be supported by the converter.
  268. <li> The destination image must support the destination format.
  269. <li> The destination image must be able to provide a large enough buffer to hold the image.
  270. <li> The source image buffer and the destination image buffer must not be identical.
  271. </ul>
  272. \error
  273. Throws an exception if the passed parameters are not valid. The converter object is still valid after error.
  274. */
  275. virtual void Convert( IReusableImage& destinationImage,
  276. const void* pSourceBuffer,
  277. size_t sourceBufferSizeBytes,
  278. EPixelType sourcePixelType,
  279. uint32_t sourceWidth,
  280. uint32_t sourceHeight,
  281. size_t sourcePaddingX,
  282. EImageOrientation sourceOrientation
  283. );
  284. /*!
  285. \brief Creates a new image by converting an image to a different format.
  286. The image is converted to the destination image according to the current converter settings.
  287. The padding area of a row in the destination image is set to zero.
  288. \param[out] pDestinationBuffer The pointer to the buffer of the destination image.
  289. \param[in] destinationBufferSizeBytes The size of the buffer of the destination image.
  290. \param[in] sourceImage The source image, e.g. a CPylonImage, CPylonBitmapImage, or Grab Result Smart Pointer object.
  291. \pre
  292. <ul>
  293. <li> The format of the source image must be supported by the converter.
  294. <li> The destination image buffer must be large enough to hold the destination image.
  295. <li> The source image buffer and the destination image buffer must not be identical.
  296. </ul>
  297. \error
  298. Throws an exception if the passed parameters are not valid. The converter object is still valid after error.
  299. */
  300. virtual void Convert( void* pDestinationBuffer, size_t destinationBufferSizeBytes, const IImage& sourceImage );
  301. /*!
  302. \brief Creates a new image by converting an image to a different format.
  303. The image is converted to the destination image according to the current converter settings.
  304. The padding area of a row in the destination image is set to zero.
  305. \param[out] pDestinationBuffer The pointer to the buffer of the destination image.
  306. \param[in] destinationBufferSizeBytes The size of the buffer of the destination image.
  307. \param[in] pSourceBuffer The pointer to the buffer of the source image.
  308. \param[in] sourceBufferSizeBytes The size of the buffer of the source image.
  309. \param[in] sourcePixelType The pixel type of the source image.
  310. \param[in] sourceWidth The number of pixels in a row in the source image.
  311. \param[in] sourceHeight The number of rows in the source image.
  312. \param[in] sourcePaddingX The number of extra data bytes at the end of each row. The default value is usually 0.
  313. \param[in] sourceOrientation The vertical orientation of the source image in the image buffer. The default value is usually ImageOrientation_TopDown.
  314. \pre
  315. <ul>
  316. <li> The parameters regarding the source buffer must describe a valid image.
  317. <li> The format of the input image represented by the given parameter must be supported by the converter.
  318. <li> If the destination image buffer must be large enough to hold the destination image.
  319. <li> The the source buffer can not be equal the destination buffer.
  320. </ul>
  321. \error
  322. Throws an exception if the passed parameters are not valid. The converter object is still valid after error.
  323. */
  324. virtual void Convert( void* pDestinationBuffer,
  325. size_t destinationBufferSizeBytes,
  326. const void* pSourceBuffer,
  327. size_t sourceBufferSizeBytes,
  328. EPixelType sourcePixelType,
  329. uint32_t sourceWidth,
  330. uint32_t sourceHeight,
  331. size_t sourcePaddingX,
  332. EImageOrientation sourceOrientation
  333. );
  334. /*!
  335. \brief Returns true if the image format defined by the given pixel type is a supported input format.
  336. \param[in] sourcePixelType The pixel type of the source image.
  337. \error
  338. Does not throw C++ exceptions.
  339. */
  340. static bool IsSupportedInputFormat( EPixelType sourcePixelType );
  341. /*!
  342. \brief Returns true if the image format defined by the given pixel type is a supported output format.
  343. \param[in] destinationPixelType The pixel type of the destination image.
  344. \error
  345. Does not throw C++ exceptions.
  346. */
  347. static bool IsSupportedOutputFormat( EPixelType destinationPixelType );
  348. /*!
  349. \brief Provides access to the node map of the format converter.
  350. \return Reference to the node map of the format converter.
  351. \error
  352. Does not throw C++ exceptions.
  353. */
  354. virtual GenApi::INodeMap& GetNodeMap();
  355. private:
  356. // The converter object can not be copied.
  357. CImageFormatConverter( const CImageFormatConverter& src );
  358. // The converter object can not be copied.
  359. CImageFormatConverter& operator=( const CImageFormatConverter& src );
  360. // Internal use only.
  361. class CImageFormatConverterImpl;
  362. CImageFormatConverterImpl* m_pImpl;
  363. public:
  364. // This class emulates part of the behavior of a GenApi::IEnumerationT<T> interface used for native parameter access.
  365. // This allows using the EPixelType enumeration.
  366. class IOutputPixelFormatEnum
  367. {
  368. public:
  369. virtual void operator=( EPixelType outputPixelType ) = 0;
  370. virtual void SetValue( EPixelType outputPixelType ) = 0;
  371. virtual EPixelType GetValue() const = 0;
  372. virtual EPixelType operator()() const = 0;
  373. };
  374. /*!
  375. \brief The pixel data format of the output image.
  376. This member can be used to get and set the output pixel format using the EPixelType enumeration.
  377. It emulates the behavior of native parameter access.
  378. \pre
  379. The pixel format set must be a supported output pixel format. See IsSupportedOutputFormat().
  380. \error
  381. Throws an exception if the set output pixel format is not supported.
  382. */
  383. IOutputPixelFormatEnum& OutputPixelFormat;
  384. };
  385. /**
  386. * @}
  387. */
  388. }
  389. #ifdef _MSC_VER
  390. #pragma warning(pop)
  391. #endif
  392. #ifdef _MSC_VER
  393. # pragma pack(pop)
  394. #endif /* _MSC_VER */
  395. #endif /* INCLUDED_IMAGEFORMATCONVERTER_H_1564142 */