Ocean
media/special/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_MEDIA_SPECIAL_IMAGE_H
9 #define META_OCEAN_MEDIA_SPECIAL_IMAGE_H
10 
12 
13 #include "ocean/base/Frame.h"
14 
15 namespace Ocean
16 {
17 
18 namespace Media
19 {
20 
21 namespace Special
22 {
23 
24 /**
25  * This class implements read and write functions for all file formats supported by the Special media library.
26  * @ingroup mediaspecial
27  */
28 class OCEAN_MEDIA_SPECIAL_EXPORT Image
29 {
30  public:
31 
32  /**
33  * Decodes (reads/loads) an image from a given binary buffer.
34  * @param buffer The buffer from which the image will be loaded, must be valid
35  * @param size The size of the given buffer in bytes, with range [1, infinity)
36  * @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)
37  * @param imageBufferTypeOut Optional type of the given image that is stored in the buffer, as determined by the decoder (if possible)
38  * @return The frame containing the image information, an invalid frame if the image could not be loaded
39  * @see writeImage().
40  */
41  static Frame decodeImage(const void* buffer, const size_t size, const std::string& imageBufferTypeIn = std::string(), std::string* imageBufferTypeOut = nullptr);
42 
43  /**
44  * Encodes (writes) a given frame as image (with specified image type) to a resulting buffer.
45  * @param frame The frame to be written, must be valid
46  * @param imageType The file extension of the image to be created (e.g. jpg, png, bmp, or tif), must be defined
47  * @param buffer The resulting buffer storing the binary information of the BMP image
48  * @param allowConversion True, to allow an internal conversion of the frame if BMP does not support the given frame type; False, to prevent a conversion and to stop creating the buffer
49  * @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
50  * @return True, if succeeded; False, if the frame could not be written as BMP image e.g., if the frame contained an alpha channel
51  * @see readImage().
52  */
53  static bool encodeImage(const Frame& frame, const std::string& imageType, std::vector<uint8_t>& buffer, const bool allowConversion = true, bool* hasBeenConverted = nullptr);
54 
55  /**
56  * Reads/loads an image from a specified file.
57  * @param filename The name of the file from which the image will be loaded, must be valid
58  * @return The frame containing the image information, an invalid frame if the image could not be loaded
59  * @see writeImage().
60  */
61  static Frame readImage(const std::string& filename);
62 
63  /**
64  * Writes a given frame to a specified file.
65  * @param frame The frame to be written, must be valid
66  * @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
67  * @param allowConversion True, to allow an internal conversion of the frame if BMP does not support the given frame type; False, to prevent a conversion and to stop creating the buffer
68  * @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
69  * @return True, if succeeded
70  * @see readImage().
71  */
72  static bool writeImage(const Frame& frame, const std::string& filename, const bool allowConversion = true, bool* hasBeenConverted = nullptr);
73 };
74 
75 }
76 
77 }
78 
79 }
80 
81 #endif // META_OCEAN_MEDIA_SPECIAL_IMAGE_H
This class implements Ocean's image class.
Definition: Frame.h:1792
This class implements read and write functions for all file formats supported by the Special media li...
Definition: media/special/Image.h:29
static bool encodeImage(const Frame &frame, const std::string &imageType, std::vector< uint8_t > &buffer, const bool allowConversion=true, bool *hasBeenConverted=nullptr)
Encodes (writes) a given frame as image (with specified image type) to a resulting buffer.
static 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 writeImage(const Frame &frame, const std::string &filename, const bool allowConversion=true, bool *hasBeenConverted=nullptr)
Writes a given frame to a specified file.
static Frame readImage(const std::string &filename)
Reads/loads an image from a specified file.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15