Ocean
media/Utilities.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_MEDIA_UTILITIES_H
9 #define META_OCEAN_MEDIA_UTILITIES_H
10 
11 #include "ocean/media/Media.h"
12 
13 #include "ocean/base/Frame.h"
14 
15 namespace Ocean
16 {
17 
18 namespace Media
19 {
20 
21 /**
22  * This class implements utilities functions for the media library.
23  * @ingroup media
24  */
25 class OCEAN_MEDIA_EXPORT Utilities
26 {
27  public:
28 
29  /**
30  * Definition of a vector holding 8 bit elements.
31  */
32  typedef std::vector<uint8_t> Buffer;
33 
34  public:
35 
36  /**
37  * Loads an image defined by the url and returns the frame.
38  * Beware: A media library supporting the image format must be loaded before.<br>
39  * @param url Url of the image to be loaded
40  * @return Resutling frame, the frame is Null if loading fails
41  * @see saveImage(), Manager, Library.
42  */
43  static Frame loadImage(const std::string& url);
44 
45  /**
46  * Loads an image defined by a memory buffer, the size of the buffer and the type of the stored image information.
47  * Beware: A media library supporting the image format must be loaded before.<br>
48  * @param imageBuffer Buffer of the image to be loaded
49  * @param imageBufferSize Size of the image buffer in bytes
50  * @param imageBufferTypeIn Type of the given image that is stored in the buffer, should be specified if known (e.g. the file extension of a corresponding image file)
51  * @param imageBufferTypeOut Optional type of the given image that is stored in the buffer, as determined by the decoder (if possible)
52  * @return Resutling frame, the frame is Null if loading fails
53  * @see saveImage(), Manager, Library.
54  */
55  static Frame loadImage(const void* imageBuffer, const size_t imageBufferSize, const std::string& imageBufferTypeIn = std::string(""), std::string* imageBufferTypeOut = nullptr);
56 
57  /**
58  * Saves a frame as image file.
59  * Beware: A media library supporting the image format must be loaded before.<br>
60  * @param frame The frame to be saved as image, must be valid
61  * @param url Url of the image to be saved
62  * @param addTimeSuffix If True the time and date is added to the url as suffix
63  * @return True, if succeeded
64  * @see loadImage(), Manager, Library.
65  */
66  static bool saveImage(const Frame& frame, const std::string& url, const bool addTimeSuffix = true);
67 
68  /**
69  * Saves a frame into a memory buffer.
70  * Beware: A media library supporting the image format must be loaded before.<br>
71  * @param frame The frame to be saved as image
72  * @param imageType Type of the image to be created in the memory buffer e.g. the file extension of a corresponding image file (bmp, jpg, png, ...)
73  * @param buffer Resulting memory buffer holding the (compressed) image which will be extended so that already existing data stays untouched
74  * @return True, if succeeded
75  * @see loadImage(), Manager, Library.
76  */
77  static bool saveImage(const Frame& frame, const std::string& imageType, Buffer& buffer);
78 
79  /**
80  * Encodes a frame to a buffer and adds further control parameters to the buffer.
81  * @param frame The frame to encode
82  * @param imageType Optional type of the image, e.g. bmp, jpg, png
83  * @param buffer The resulting buffer
84  */
85  static void encodeFrame(const Frame& frame, const std::string& imageType, Buffer& buffer);
86 
87  /**
88  * Encodes a frame to a buffer and adds further control parameters to the buffer.
89  * @param frame The frame to encode
90  * @param imageType Optional type of the image, e.g. bmp, jpg, png
91  * @param reservedHeaderSize The number of bytes which will be reserved for an optional header, so that the resulting buffer has a header followed by the payload data
92  * @return The resulting buffer
93  */
94  static inline Buffer encodeFrame(const Frame& frame, const std::string& imageType, const size_t reservedHeaderSize = 0);
95 
96  /**
97  * Decodes a frame from a maintenance data buffer.
98  * @param data The buffer providing the frame, will be shifted by the number of consumed bytes afterwards
99  * @param size The size of the entire buffer, will be reduced by the number of consumed bytes afterwards
100  * @param frame The resulting frame
101  * @return True, if succeeded
102  */
103  static bool decodeFrame(const uint8_t*& data, size_t& size, Frame& frame);
104 
105  /**
106  * Decodes a frame from a data buffer holding the compressed image content as well as further control parameters.
107  * @param buffer The buffer from which the frame will be decoded
108  * @param frame The resulting frame
109  * @return True, if succeeded
110  */
111  static inline bool decodeFrame(const Buffer& buffer, Frame& frame);
112 };
113 
114 inline Utilities::Buffer Utilities::encodeFrame(const Frame& frame, const std::string& imageType, const size_t reservedHeaderSize)
115 {
116  Buffer buffer(reservedHeaderSize);
117 
118  encodeFrame(frame, imageType, buffer);
119 
120  return buffer;
121 }
122 
123 inline bool Utilities::decodeFrame(const Buffer& buffer, Frame& frame)
124 {
125  const uint8_t* data = buffer.data();
126  size_t size = buffer.size();
127 
128  return decodeFrame(data, size, frame);
129 }
130 
131 }
132 
133 }
134 
135 #endif // META_OCEAN_MEDIA_UTILITIES_H
This class implements Ocean's image class.
Definition: Frame.h:1760
This class implements utilities functions for the media library.
Definition: media/Utilities.h:26
std::vector< uint8_t > Buffer
Definition of a vector holding 8 bit elements.
Definition: media/Utilities.h:32
static Frame loadImage(const void *imageBuffer, const size_t imageBufferSize, const std::string &imageBufferTypeIn=std::string(""), std::string *imageBufferTypeOut=nullptr)
Loads an image defined by a memory buffer, the size of the buffer and the type of the stored image in...
static bool decodeFrame(const uint8_t *&data, size_t &size, Frame &frame)
Decodes a frame from a maintenance data buffer.
static bool saveImage(const Frame &frame, const std::string &imageType, Buffer &buffer)
Saves a frame into a memory buffer.
static Frame loadImage(const std::string &url)
Loads an image defined by the url and returns the frame.
static void encodeFrame(const Frame &frame, const std::string &imageType, Buffer &buffer)
Encodes a frame to a buffer and adds further control parameters to the buffer.
static bool saveImage(const Frame &frame, const std::string &url, const bool addTimeSuffix=true)
Saves a frame as image file.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15