ImagePersistence.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //------------------------------------------------------------------------------
  2. // Basler pylon SDK
  3. // Copyright (c) 2011-2021 Basler AG
  4. // http://www.baslerweb.com
  5. // Author: Andreas Gau
  6. //------------------------------------------------------------------------------
  7. /*!
  8. \file
  9. \brief Contains an image loading and saving support class.
  10. */
  11. #ifndef INCLUDED_IMAGEPERSISTENCE_H_3336145
  12. #define INCLUDED_IMAGEPERSISTENCE_H_3336145
  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/ReusableImage.h>
  20. namespace Pylon
  21. {
  22. /** \addtogroup Pylon_ImageHandlingSupport
  23. * @{
  24. */
  25. #if defined(PYLON_WIN_BUILD)
  26. /// Lists the available file formats
  27. enum EImageFileFormat
  28. {
  29. ImageFileFormat_Bmp = 0, //!< Windows Bitmap, no compression.
  30. ImageFileFormat_Tiff = 1, //!< Tagged Image File Format, no compression, supports mono images with more than 8 bit bit depth.
  31. ImageFileFormat_Jpeg = 2, //!< Joint Photographic Experts Group, lossy data compression.
  32. ImageFileFormat_Png = 3, //!< Portable Network Graphics, lossless data compression.
  33. ImageFileFormat_Raw = 4, //!< Saves the raw image buffer data. Packed image formats are unpacked. See GetPixelTypesForUnpacking() for more information.
  34. //!< In the format used for saving, the data cannot be loaded using CImagePersistence::Load().
  35. ImageFileFormat_Dng = 5 //!< Saves Bayer pattern images in DNG/TIFF format. Packed image formats are unpacked. See GetPixelTypesForUnpacking() for more information.
  36. //!< In the format used for saving, the data cannot be loaded using CImagePersistence::Load().
  37. };
  38. #elif defined(PYLON_UNIX_BUILD)
  39. /// Lists the available file formats
  40. enum EImageFileFormat
  41. {
  42. ImageFileFormat_Tiff = 1, //!< Tagged Image File Format, no compression, supports mono images with more than 8 bit bit depth.
  43. ImageFileFormat_Png = 3, //!< Portable Network Graphics, lossless data compression.
  44. ImageFileFormat_Raw = 4, //!< Saves the raw image buffer data. Packed image formats are unpacked. See GetPixelTypesForUnpacking() for more information.
  45. //!< This format cannot be loaded unsing CImagePersistence::Load().
  46. ImageFileFormat_Dng = 5 //!< Saves Bayer pattern images in DNG/TIFF format. Packed image formats are unpacked. See GetPixelTypesForUnpacking() for more information.
  47. //!< In the format used for saving, the data cannot be loaded using CImagePersistence::Load().
  48. };
  49. #else
  50. # error unsupported platform
  51. #endif
  52. /// Used to pass options to CImagePersistence methods.
  53. class CImagePersistenceOptions
  54. {
  55. public:
  56. CImagePersistenceOptions()
  57. : m_quality( 90 )
  58. {
  59. }
  60. ~CImagePersistenceOptions()
  61. {
  62. }
  63. /// Set the image quality options. Valid quality values range from 0 to 100.
  64. void SetQuality( int quality )
  65. {
  66. m_quality = quality;
  67. }
  68. /// Returns the set quality level.
  69. int GetQuality()
  70. {
  71. return m_quality;
  72. }
  73. private:
  74. int m_quality; ///< Can be used to control the quality when saving JPEG images.
  75. };
  76. /*!
  77. \class CImagePersistence
  78. \brief Contains static functions supporting loading and saving of images.
  79. */
  80. class PYLONUTILITY_API CImagePersistence
  81. {
  82. public:
  83. /*!
  84. \brief Saves the image to disk. Converts the image to a format that can be saved if required.
  85. If required, the image is automatically converted to a new image and then saved. See
  86. CanSaveWithoutConversion() for more information.
  87. An image with a bit depth higher than 8 bit is stored with 16 bit bit depth
  88. if supported by the image file format. In this case the pixel data is MSB aligned.
  89. If more control over the conversion is required then the CImageFormatConverter class
  90. can be used to convert the input image before saving it.
  91. \param[in] imageFileFormat The file format to save the image in.
  92. \param[in] filename Name and path of the image.
  93. \param[in] pBuffer The pointer to the buffer of the image.
  94. \param[in] bufferSize The size of the buffer in byte.
  95. \param[in] pixelType The pixel type of the image to save.
  96. \param[in] width The number of pixels in a row of the image to save.
  97. \param[in] height The number of rows of the image to save.
  98. \param[in] paddingX The number of extra data bytes at the end of each row.
  99. \param[in] orientation The vertical orientation of the image in the image buffer.
  100. \param[in] pOptions Additional options.
  101. \pre
  102. <ul>
  103. <li> The pixel type of the image to save must be a supported input format of the Pylon::CImageFormatConverter.
  104. <li> The \c width value must be >= 0 and < _I32_MAX.
  105. <li> The \c height value must be >= 0 and < _I32_MAX.
  106. </ul>
  107. \error
  108. Throws an exception if saving the image fails.
  109. */
  110. static void Save( EImageFileFormat imageFileFormat, const String_t& filename, const void* pBuffer, size_t bufferSize, EPixelType pixelType, uint32_t width, uint32_t height, size_t paddingX, EImageOrientation orientation, CImagePersistenceOptions* pOptions = NULL );
  111. /*!
  112. \brief Saves the image to disk. Converts the image to a format that can be if required.
  113. If required, the image is automatically converted to a new image and then saved. See
  114. CanSaveWithoutConversion() for more information.
  115. An image with a bit depth higher than 8 bit is stored with 16 bit bit depth
  116. if supported by the image file format. In this case the pixel data is MSB aligned.
  117. If more control over the conversion is required then the CImageFormatConverter class
  118. can be used to convert the input image before saving it.
  119. \param[in] imageFileFormat The target file format for the image to save.
  120. \param[in] filename Name and path of the image.
  121. \param[in] image The image to save, e.g. a CPylonImage, CPylonBitmapImage, or Grab Result Smart Pointer object.
  122. \param[in] pOptions Additional options.
  123. \pre
  124. The pixel type of the image to save must be a supported input format of the Pylon::CImageFormatConverter.
  125. \error
  126. Throws an exception if saving the image fails.
  127. */
  128. static void Save( EImageFileFormat imageFileFormat, const String_t& filename, const IImage& image, CImagePersistenceOptions* pOptions = NULL );
  129. /*!
  130. \brief Can be used to check whether the given image can be saved without prior conversion.
  131. See the CImagePersistence::CanSaveWithoutConversion( EImageFileFormat, const IImage&) method documentation for a list of supported pixel formats.
  132. \param[in] imageFileFormat The target file format for the image to save.
  133. \param[in] pixelType The pixel type of the image to save.
  134. \param[in] width The number of pixels in a row of the image to save.
  135. \param[in] height The number of rows of the image to save.
  136. \param[in] paddingX The number of extra data bytes at the end of each row.
  137. \param[in] orientation The vertical orientation of the image in the image buffer.
  138. \return Returns true if the image can be saved without prior conversion.
  139. \error
  140. Does not throw C++ exceptions.
  141. */
  142. static bool CanSaveWithoutConversion( EImageFileFormat imageFileFormat, EPixelType pixelType, uint32_t width, uint32_t height, size_t paddingX, EImageOrientation orientation );
  143. /*!
  144. \brief Can be used to check whether the image can be saved without prior conversion.
  145. Supported formats for TIFF:
  146. <ul>
  147. <li> PixelType_Mono8
  148. <li> PixelType_Mono16
  149. <li> PixelType_RGB8packed
  150. <li> PixelType_RGB16packed
  151. </ul>
  152. Supported formats for BMP, JPEG and PNG:
  153. <ul>
  154. <li> PixelType_Mono8
  155. <li> PixelType_BGR8packed
  156. <li> PixelType_BGRA8packed
  157. </ul>
  158. Supported formats for DNG:
  159. <ul>
  160. <li> PixelType_BayerGR*
  161. <li> PixelType_BayerRG*
  162. <li> PixelType_BayerGB*
  163. <li> PixelType_BayerBG*
  164. </ul>
  165. With *: 8, 10, 12, 16, 10p, 12p, 12packed
  166. \param[in] imageFileFormat The target file format for the image to save.
  167. \param[in] image The image to save, e.g. a CPylonImage, CPylonBitmapImage, or Grab Result Smart Pointer object.
  168. \return Returns true if the image can be saved without prior conversion.
  169. \error
  170. Does not throw C++ exceptions.
  171. */
  172. static bool CanSaveWithoutConversion( EImageFileFormat imageFileFormat, const IImage& image );
  173. /*!
  174. \brief Loads an image from disk.
  175. The orientation of loaded images is always ImageOrientation_TopDown.
  176. \param[in] filename Name and path of the image.
  177. \param[in] image The target image object, e.g. a CPylonImage or CPylonBitmapImage object.
  178. When passing a CPylonBitmapImage object the loaded format must be supported by the CPylonBitmapImage class.
  179. \error
  180. Throws an exception if the image cannot be loaded. The image buffer content is undefined when the loading of the image fails.
  181. */
  182. static void Load( const String_t& filename, IReusableImage& image );
  183. /*!
  184. \brief Loads an image from memory.
  185. The orientation of loaded images is always ImageOrientation_TopDown.
  186. Currently BMP, JPEG & PNG images are supported.
  187. \param[in] pBuffer The pointer to the buffer of the source image.
  188. \param[in] bufferSizeBytes The size of the buffer of the source image.
  189. \param[in] image The target image object, e.g. a CPylonImage or CPylonBitmapImage object.
  190. When passing a CPylonBitmapImage object the loaded format must be supported by the CPylonBitmapImage class.
  191. \error
  192. Throws an exception if the image cannot be loaded. The image buffer content is undefined when the loading of the image fails.
  193. */
  194. static void LoadFromMemory( const void* pBuffer, size_t bufferSizeBytes, IReusableImage& image );
  195. };
  196. /**
  197. * @}
  198. */
  199. }
  200. #ifdef _MSC_VER
  201. # pragma pack(pop)
  202. #endif /* _MSC_VER */
  203. #endif /* INCLUDED_IMAGEPERSISTENCE_H_3336145 */