Ocean
io/image/Image.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_IO_IMAGE_IMAGE_H
9 #define META_OCEAN_IO_IMAGE_IMAGE_H
10 
11 #include "ocean/io/IO.h"
12 
13 #include "ocean/base/Frame.h"
14 
15 #include "ocean/media/Image.h"
16 
17 namespace Ocean
18 {
19 
20 namespace IO
21 {
22 
23 namespace Image
24 {
25 
26 /**
27  * @ingroup io
28  * @defgroup ioimage Ocean IO Image Library
29  * @{
30  * The Ocean IO Image Library provides image encoding, decoding, loading, and saving functionalities.
31  * The library is platform independent.
32  * @}
33  */
34 
35 /**
36  * @namespace Ocean::IO::Image Namespace of the Ocean IO Image library<p>
37  * The Namespace Ocean::IO::Image is used in the entire Ocean IO Image library.
38  */
39 
40 // Defines OCEAN_IO_IMAGE_EXPORT for dll export and import.
41 #if defined(_WINDOWS) && defined(OCEAN_RUNTIME_SHARED)
42  #ifdef USE_OCEAN_IO_IMAGE_EXPORT
43  #define OCEAN_IO_IMAGE_EXPORT __declspec(dllexport)
44  #else
45  #define OCEAN_IO_IMAGE_EXPORT __declspec(dllimport)
46  #endif
47 #else
48  #define OCEAN_IO_IMAGE_EXPORT
49 #endif
50 
51 /**
52  * Re-definition of Media::Image::Properties.
53  */
55 
56 /**
57  * Decodes (reads/loads) an image from a given binary buffer.
58  * @param buffer The buffer from which the image will be loaded, must be valid
59  * @param size The size of the given buffer in bytes, with range [1, infinity)
60  * @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)
61  * @param imageBufferTypeOut Optional type of the given image that is stored in the buffer, as determined by the decoder (if possible)
62  * @return The frame containing the image information, an invalid frame if the image could not be loaded
63  * @see encodeImage(), readImage().
64  * @ingroup ioimage
65  */
66 OCEAN_IO_IMAGE_EXPORT Frame decodeImage(const void* buffer, const size_t size, const std::string& imageBufferTypeIn = std::string(), std::string* imageBufferTypeOut = nullptr);
67 
68 /**
69  * Encodes (writes) a given frame as image (with specified image type) to a resulting buffer.
70  * In case, the pixel format of the given frame is not supported by the destination, the function will fail.<br>
71  * As this function does not provide an automatic color space conversion, the binary impact when using this function will be quite small.<br>
72  * Note: Depending on the encoder, a provided quality value may not have any impact, e.g., png images do not support a loosely compression.
73  * @param frame The frame to be written, must be valid
74  * @param imageType The file extension of the image to be created (e.g. jpg, png, bmp, or tif), must be defined
75  * @param buffer The resulting buffer storing the binary information of the given image
76  * @param properties The properties to be used when writing the image, must be valid
77  * @return True, if succeeded; False, if the frame could not be encoded e.g., if the pixel format is not supported or if the `imageType` is unknown
78  * @see decodeImage(), writeImage().
79  * @ingroup ioimage
80  */
81 OCEAN_IO_IMAGE_EXPORT bool encodeImage(const Frame& frame, const std::string& imageType, std::vector<uint8_t>& buffer, const Properties& properties = Properties());
82 
83 /**
84  * Reads/loads an image from a specified file.
85  * @param filename The name of the file from which the image will be loaded, must be valid
86  * @return The frame containing the image information, an invalid frame if the image could not be loaded
87  * @see writeImage().
88  * @ingroup ioimage
89  */
90 OCEAN_IO_IMAGE_EXPORT Frame readImage(const std::string& filename);
91 
92 /**
93  * Writes a given frame to a specified file.
94  * In case, the pixel format of the given frame is not supported by the destination, the function will fail.<br>
95  * As this function does not provide an automatic color space conversion, the binary impact when using this function will be quite small.<br>
96  * Note: Depending on the encoder, a provided quality value may not have any impact, e.g., png images do not support a loosely compression.
97  * @param frame The frame to be written, must be valid
98  * @param filename The name of the file to which the frame will be written, must contain a valid image extension like e.g. jpg, png, bmp, or tif), must be valid
99  * @param properties The properties to be used when writing the image, must be valid
100  * @return True, if succeeded
101  * @see readImage().
102  * @ingroup ioimage
103  */
104 OCEAN_IO_IMAGE_EXPORT bool writeImage(const Frame& frame, const std::string& filename, const Properties& properties = Properties());
105 
106 /**
107  * This class provides functions for image encoding and decoding which will increase binary size when used.
108  * @ingroup ioimage
109  */
110 class OCEAN_IO_IMAGE_EXPORT Comfort
111 {
112  public:
113 
114  /**
115  * Encodes (writes) a given frame as image (with specified image type) to a resulting buffer.
116  * In case, the pixel format of the given frame is not supported by the destination, the function may convert the frame internally (depending on 'allowConversion').
117  * As this function supports the conversion of color spaces, the binary impact when using this function may be quite high.<br>
118  * Note: Depending on the encoder, a provided quality value may not have any impact, e.g., png images do not support a loosely compression.
119  * @param frame The frame to be written, must be valid
120  * @param imageType The file extension of the image to be created (e.g. jpg, png, bmp, or tif), must be defined
121  * @param buffer The resulting buffer storing the binary information of the resulting image
122  * @param allowConversion True, to allow an internal conversion of the frame if the image type does not support the given frame type; False, to prevent a conversion and to stop creating the buffer
123  * @param hasBeenConverted Optional resulting statement whether the frame had to be converted to a different pixel format before it could be written; True, if so; False, if not
124  * @param properties The properties to be used when writing the image, must be valid
125  * @return True, if succeeded; False, if the frame could not be encoded e.g., if the pixel format could not be converted to a valid pixel format or if the `imageType` is unknown
126  * @see readImage().
127  * @ingroup ioimage
128  */
129  static bool encodeImage(const Frame& frame, const std::string& imageType, std::vector<uint8_t>& buffer, const bool allowConversion, bool* hasBeenConverted = nullptr, const Properties& properties = Properties());
130 
131  /**
132  * Writes a given frame to a specified file.
133  * In case, the pixel format of the given frame is not supported by the destination, the function may convert the frame internally (depending on 'allowConversion').
134  * As this function supports the conversion of color spaces, the binary impact when using this function may be quite high.<br>
135  * Note: Depending on the encoder, a provided quality value may not have any impact, e.g., png images do not support a loosely compression.
136  * @param frame The frame to be written, must be valid
137  * @param filename The name of the file to which the frame will be written, must contain a valid image extension like e.g. jpg, png, bmp, or tif), must be valid
138  * @param allowConversion True, to allow an internal conversion of the frame if the image type does not support the given frame type; False, to prevent a conversion and to stop writing the image
139  * @param hasBeenConverted Optional resulting statement whether the frame had to be converted to a different pixel format before it could be written; True, if so; False, if not
140  * @param properties The properties to be used when writing the image, must be valid
141  * @return True, if succeeded
142  * @see readImage().
143  * @ingroup ioimage
144  */
145  static bool writeImage(const Frame& frame, const std::string& filename, const bool allowConversion, bool* hasBeenConverted = nullptr, const Properties& properties = Properties());
146 
147  protected:
148 
149  /**
150  * Disabled function to prevent wrong usage.
151  * @return Would return True on success
152  */
153  static bool encodeImage(const Frame&, const std::string&, std::vector<uint8_t>&, const float) = delete;
154 
155  /**
156  * Disabled function to prevent wrong usage.
157  * @return Would return True on success
158  */
159  static bool writeImage(const Frame&, const std::string&, const float) = delete;
160 };
161 
162 /**
163  * Disabled function to prevent wrong usage.
164  * @return Would return True on success
165  */
166 static bool encodeImage(const Frame&, const std::string&, std::vector<uint8_t>&, const float) = delete;
167 
168 /**
169  * Disabled function to prevent wrong usage.
170  * @return Would return True on success
171  */
172 static bool writeImage(const Frame&, const std::string&, const float) = delete;
173 
174 }
175 
176 }
177 
178 }
179 
180 #endif // META_OCEAN_IO_IMAGE_IMAGE_H
This class implements Ocean's image class.
Definition: Frame.h:1760
This class provides functions for image encoding and decoding which will increase binary size when us...
Definition: io/image/Image.h:111
static bool writeImage(const Frame &, const std::string &, const float)=delete
Disabled function to prevent wrong usage.
static bool encodeImage(const Frame &, const std::string &, std::vector< uint8_t > &, const float)=delete
Disabled function to prevent wrong usage.
Definition of a class holding properties.
Definition: media/Image.h:45
OCEAN_IO_IMAGE_EXPORT Frame decodeImage(const void *buffer, const size_t size, const std::string &imageBufferTypeIn=std::string(), std::string *imageBufferTypeOut=nullptr)
Decodes (reads/loads) an image from a given binary buffer.
static bool encodeImage(const Frame &frame, const std::string &imageType, std::vector< uint8_t > &buffer, const bool allowConversion, bool *hasBeenConverted=nullptr, const Properties &properties=Properties())
Encodes (writes) a given frame as image (with specified image type) to a resulting buffer.
OCEAN_IO_IMAGE_EXPORT Frame readImage(const std::string &filename)
Reads/loads an image from a specified file.
static bool writeImage(const Frame &frame, const std::string &filename, const bool allowConversion, bool *hasBeenConverted=nullptr, const Properties &properties=Properties())
Writes a given frame to a specified file.
OCEAN_IO_IMAGE_EXPORT bool encodeImage(const Frame &frame, const std::string &imageType, std::vector< uint8_t > &buffer, const Properties &properties=Properties())
Encodes (writes) a given frame as image (with specified image type) to a resulting buffer.
OCEAN_IO_IMAGE_EXPORT bool writeImage(const Frame &frame, const std::string &filename, const Properties &properties=Properties())
Writes a given frame to a specified file.
Media::Image::Properties Properties
Re-definition of Media::Image::Properties.
Definition: io/image/Image.h:54
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15