AviWriter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //------------------------------------------------------------------------------
  2. // Basler pylon SDK
  3. // Copyright (c) 2011-2021 Basler AG
  4. // http://www.baslerweb.com
  5. // Author: AG
  6. //------------------------------------------------------------------------------
  7. /*!
  8. \file
  9. \brief Contains support for writing files in the Audio Video Interleave (AVI) format.
  10. */
  11. #ifndef INCLUDED_AVIWRITER_H_9434931
  12. #define INCLUDED_AVIWRITER_H_9434931
  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/Image.h>
  20. namespace Pylon
  21. {
  22. /// Exception thrown if a fatal error occurs (e.g. access violations, ...) when accessing an AVI video file
  23. class AviWriterFatalException : public GenericException
  24. {
  25. public:
  26. /** \brief \copybrief GenericException::GenericException() */
  27. AviWriterFatalException( const char* pDescription, const char* pSourceFileName, int SourceLine );
  28. AviWriterFatalException( const char* pDescription, const char* pSourceFileName, int SourceLine, const char* pExceptionType );
  29. AviWriterFatalException( const char* pDescription, const char* pSourceFileName, int SourceLine, const char* pEntryPoint, const char* pErrorNodeName, const char* pExceptionType );
  30. };
  31. /** \addtogroup Pylon_ImageHandlingSupport
  32. * @{
  33. */
  34. /// Forward reference for compression options, requires additional include of AviCompressionOptions.h
  35. struct SAviCompressionOptions;
  36. /// List the possibilities for selecting a key frame when using compression.
  37. enum EKeyFrameSelection
  38. {
  39. KeyFrameSelection_NoKeyFrame, //!< Indicates that an image is not a key frame.
  40. KeyFrameSelection_KeyFrame, //!< Indicates that an image is a key frame.
  41. KeyFrameSelection_Auto //!< Every nth frame is a key frame. The value of n is specified by passing its value in the SAviCompressionOptions in the autoKeyFrameInsertionRate member when opening the AVI Writer.
  42. };
  43. /*!
  44. \class CAviWriter
  45. \brief Supports writing AVI files.
  46. */
  47. class PYLONUTILITY_API CAviWriter
  48. {
  49. public:
  50. /*!
  51. \brief Creates an AVI writer object.
  52. \error
  53. Does not throw C++ exceptions.
  54. */
  55. CAviWriter();
  56. /*!
  57. \brief Destroys the AVI writer object.
  58. \error
  59. Does not throw C++ exceptions.
  60. */
  61. virtual ~CAviWriter();
  62. /*!
  63. \brief Opens an AVI file for writing.
  64. If a file with the same \c filename already exists, it will be overwritten.
  65. \param[in] filename Name and path of the image.
  66. \param[in] framesPerSecondPlayback The frame rate of the AVI file when shown in a media player.
  67. \param[in] pixelType The pixel type of the image in the AVI file.
  68. \param[in] width The number of pixels in a row.
  69. \param[in] height The number of rows of the image.
  70. \param[in] orientation The vertical orientation of the image data in the AVI file.
  71. \param[in,out] pCompressionOptions Compression can be enabled by passing compression options. See SAviCompressionOptions.
  72. \pre
  73. <ul>
  74. <li> The AVI file is closed.
  75. <li> The pixelType is either PixelType_Mono8, PixelType_BGR8packed or PixelType_BGRA8packed
  76. <li> The \c width value must be > 0 and < _I32_MAX.
  77. <li> The \c height value must be > 0 and < _I32_MAX.
  78. </ul>
  79. \error
  80. Throws an exception if the AVI file cannot be opened.
  81. Throws an exception if the preconditions are not met.
  82. \threading
  83. This method is synchronized using the lock provided by GetLock().
  84. */
  85. virtual void Open(
  86. const String_t& filename,
  87. double framesPerSecondPlayback,
  88. EPixelType pixelType,
  89. uint32_t width, uint32_t height,
  90. EImageOrientation orientation = ImageOrientation_BottomUp,
  91. SAviCompressionOptions* pCompressionOptions = NULL
  92. );
  93. /*!
  94. \brief Returns the open state of the AVI file.
  95. \return Returns true if open.
  96. \error
  97. Does not throw C++ exceptions.
  98. \threading
  99. This method is synchronized using the lock provided by GetLock().
  100. */
  101. virtual bool IsOpen();
  102. /*!
  103. \brief Closes the AVI file.
  104. \error
  105. Does not throw C++ exceptions.
  106. \threading
  107. This method is synchronized using the lock provided by GetLock().
  108. */
  109. virtual void Close();
  110. /*!
  111. \brief Adds the image to the AVI file. Converts the image to the correct format if required.
  112. The image is automatically converted to the format passed when opening the file if needed.
  113. The image is also converted if the stride of the passed image is not aligned to 4 byte.
  114. The image is also converted if the orientation of the passed image does mot match the value passed when opening the AVI file.
  115. If more control over the conversion is required, the CImageFormatConverter class
  116. can be used to convert other images with a CPylonBitmapImage object as target.
  117. The CPylonBitmapImage object can then be added to the AVI file.
  118. \param[in] image The image to add, e.g. a CPylonImage, CPylonBitmapImage, or Grab Result Smart Pointer object.
  119. \param[in] keyFrameSelection Can be used to control key frame selection for compressed images if needed.
  120. \pre
  121. <ul>
  122. <li> The file is open.
  123. <li> The image added is valid.
  124. <li> The pixel type of the image to add is a supported input format of the Pylon::CImageFormatConverter.
  125. <li> The width and height of the \c image match the values passed when opening the AVI file.
  126. <li> The total size of the AVI file must not exceed 2 GB. See CAviWriter::GetImageDataBytesWritten().
  127. </ul>
  128. \error
  129. Throws an exception if the image cannot be added.
  130. \threading
  131. This method is synchronized using the lock provided by GetLock().
  132. */
  133. virtual void Add( const IImage& image, EKeyFrameSelection keyFrameSelection = KeyFrameSelection_Auto );
  134. /*!
  135. \brief Adds the image to the AVI file. Converts the image to the correct format if required.
  136. See Add( const Image&) for more details.
  137. \param[in] pBuffer The pointer to the buffer of the image.
  138. \param[in] bufferSize The size of the buffer in byte.
  139. \param[in] pixelType The pixel type of the image to save.
  140. \param[in] width The number of pixels in a row of the image to save.
  141. \param[in] height The number of rows of the image to save.
  142. \param[in] paddingX The number of extra data bytes at the end of each line.
  143. \param[in] orientation The vertical orientation of the image in the image buffer.
  144. \param[in] keyFrameSelection Can be used to control key frame selection for compressed images if needed.
  145. \pre
  146. <ul>
  147. <li> The file is open.
  148. <li> The image added is valid.
  149. <li> The pixel type of the image to add is a supported input format of the Pylon::CImageFormatConverter.
  150. <li> The width and height of the \c image match the values passed when opening the AVI file.
  151. <li> The total size of the AVI file must not exceed 2 GB. See CAviWriter::GetImageDataBytesWritten().
  152. </ul>
  153. \error
  154. Throws an exception if the image cannot be added.
  155. \threading
  156. This method is synchronized using the lock provided by GetLock().
  157. */
  158. virtual void Add( const void* pBuffer, size_t bufferSize, EPixelType pixelType, uint32_t width, uint32_t height, size_t paddingX, EImageOrientation orientation, EKeyFrameSelection keyFrameSelection = KeyFrameSelection_Auto );
  159. /*!
  160. \brief Can be used to check whether the given image is added to the AVI file without prior conversion when Add() is called.
  161. \param[in] pixelType The pixel type of the image to save.
  162. \param[in] width The number of pixels in a row of the image to save.
  163. \param[in] height The number of rows of the image to save.
  164. \param[in] paddingX The number of extra data bytes at the end of each row.
  165. \param[in] orientation The vertical orientation of the image data in the AVI file.
  166. \return Returns true if the image is added to the AVI stream without prior conversion when Add() is called.
  167. Returns false if the image is automatically converted when Add() is called.
  168. Returns false if the image cannot be added at all. See the preconditions of Add() for more information.
  169. \error
  170. Does not throw C++ exceptions.
  171. */
  172. virtual bool CanAddWithoutConversion( EPixelType pixelType, uint32_t width, uint32_t height, size_t paddingX, EImageOrientation orientation );
  173. /*!
  174. \brief Can be used to check whether the given image is added to the AVI file without prior conversion when Add() is called.
  175. \param[in] image The image to save, e.g. a CPylonImage, CPylonBitmapImage, or Grab Result Smart Pointer object.
  176. \return Returns true if the image is added to the AVI stream without prior conversion when Add() is called.
  177. Returns false if the image is automatically converted when Add() is called.
  178. Returns false if the image cannot be added at all. See the preconditions of Add() for more information.
  179. \error
  180. Does not throw C++ exceptions.
  181. */
  182. virtual bool CanAddWithoutConversion( const IImage& image );
  183. /*!
  184. \brief Provides access to the number of images that have been added to the AVI file.
  185. \return Returns the number of images that have been added to the AVI file.
  186. Returns 0 if no AVI file has been written yet.
  187. \error
  188. Does not throw C++ exceptions.
  189. */
  190. virtual size_t GetCountOfAddedImages() const;
  191. /*!
  192. \brief Provides access to the number of image data bytes written to the AVI file.
  193. This value is updated with each call to AviWriter::Add().
  194. Depending on the used image format and codec, about
  195. 5 KB of header information and padding bytes are written to the AVI file.
  196. Furthermore, 24 additional bytes are needed per image for chunk header and index entry data.
  197. \return Returns the number of image data bytes that have been written to the AVI file.
  198. Returns 0 if no AVI File has been written yet.
  199. This size does not include the sizes of the AVI file header and AVI file index.
  200. \error
  201. Does not throw C++ exceptions.
  202. */
  203. virtual size_t GetImageDataBytesWritten() const;
  204. protected:
  205. /*!
  206. \brief Provides access to the lock used for synchronizing the access to the AVI writer.
  207. \error
  208. Does not throw C++ exceptions.
  209. */
  210. CLock& GetLock();
  211. private:
  212. // The object can not be copied.
  213. CAviWriter( const CAviWriter& );
  214. // The object can not be copied.
  215. void operator = ( const CAviWriter& );
  216. // Internal use only.
  217. class CAviWriterImpl;
  218. CAviWriterImpl* m_pImpl;
  219. };
  220. /**
  221. * @}
  222. */
  223. }
  224. #ifdef _MSC_VER
  225. # pragma pack(pop)
  226. #endif /* _MSC_VER */
  227. #endif /* INCLUDED_AVIWRITER_H_9434931 */