Ocean
media/openimagelibraries/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_OPEN_IMAGE_LIBRARIES_IMAGE_H
9 #define META_OCEAN_MEDIA_OPEN_IMAGE_LIBRARIES_IMAGE_H
10 
12 
13 #include "ocean/base/Frame.h"
14 
15 namespace Ocean
16 {
17 
18 namespace Media
19 {
20 
21 namespace OpenImageLibraries
22 {
23 
24 /**
25  * This class implements read and write functions for all file formats supported by the OpenImageLibraries media library.
26  * This class is not derived from Media::Image as this class is a simple wrapper for ImageBmp, ImageJpg, ImagePng, and ImageTif only.<br>
27  * Instead, OILImage is derived from Media::Image.
28  *
29  * OpenImageLibraries supports for the following image types: bmp, jpg, png, tiff.
30  * The following pixel formats are supported by the individual image types:
31  * <pre>
32  * ImageType: Pixel format: Pixel origin: Supports compression quality:
33  * bmp FORMAT_RGB24 ORIGIN_UPPER_LEFT no
34  *
35  * jpg FORMAT_Y8 ORIGIN_UPPER_LEFT yes
36  * jpg FORMAT_Y16 ORIGIN_UPPER_LEFT yes
37  * jpg FORMAT_RGB24 ORIGIN_UPPER_LEFT yes
38  * jpg FORMAT_YUV24 ORIGIN_UPPER_LEFT yes
39  *
40  * png FORMAT_Y8 ORIGIN_UPPER_LEFT no
41  * png FORMAT_Y16 ORIGIN_UPPER_LEFT no
42  * png FORMAT_YA16 ORIGIN_UPPER_LEFT no
43  * png FORMAT_RGB24 ORIGIN_UPPER_LEFT no
44  * png FORMAT_RGBA32 ORIGIN_UPPER_LEFT no
45  * png FORMAT_RGBA64 ORIGIN_UPPER_LEFT no
46  *
47  * webp FORMAT_RGB24 ORIGIN_UPPER_LEFT yes
48  * webp FORMAT_BGR24 ORIGIN_UPPER_LEFT yes
49  *
50  * tif FORMAT_RGB24 ORIGIN_UPPER_LEFT no
51  * tif FORMAT_RGBA32 ORIGIN_UPPER_LEFT no
52  * </pre>
53  * @see OILImage.
54  * @ingroup mediaoil
55  */
56 class OCEAN_MEDIA_OIL_EXPORT Image
57 {
58  public:
59 
60  /**
61  * Decodes (reads/loads) an image from a given binary buffer.
62  * @param buffer The buffer from which the image will be loaded, must be valid
63  * @param size The size of the given buffer in bytes, with range [1, infinity)
64  * @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)
65  * @param imageBufferTypeOut Optional type of the given image that is stored in the buffer, as determined by the decoder (if possible)
66  * @return The frame containing the image information, an invalid frame if the image could not be loaded
67  * @see writeImage().
68  */
69  static Frame decodeImage(const void* buffer, const size_t size, const std::string& imageBufferTypeIn = std::string(), std::string* imageBufferTypeOut = nullptr);
70 
71  /**
72  * Encodes (writes) a given frame as image (with specified image type) to a resulting buffer.
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, gif, or tif), must be defined
75  * @param buffer The resulting buffer storing the binary information of the image
76  * @param allowConversion True, to allow an internal conversion of the frame if does not support the given frame type; False, to prevent a conversion and to stop creating the buffer
77  * @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
78  * @return True, if succeeded; False, if the frame could not be written as image e.g., if the frame contained an alpha channel
79  * @see readImage().
80  */
81  static bool encodeImage(const Frame& frame, const std::string& imageType, std::vector<uint8_t>& buffer, const bool allowConversion = true, bool* hasBeenConverted = nullptr);
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  */
89  static Frame readImage(const std::string& filename);
90 
91  /**
92  * Writes a given frame to a specified file.
93  * @param frame The frame to be written, must be valid
94  * @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, gif, or tif), must be valid
95  * @param allowConversion True, to allow an internal conversion of the frame if the image format does not support the given frame type; False, to prevent a conversion and to stop writing the file
96  * @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
97  * @return True, if succeeded
98  * @see readImage().
99  */
100  static bool writeImage(const Frame& frame, const std::string& filename, const bool allowConversion = true, bool* hasBeenConverted = nullptr);
101 };
102 
103 }
104 
105 }
106 
107 }
108 
109 #endif // META_OCEAN_MEDIA_OPEN_IMAGE_LIBRARIES_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 OpenImageLibrari...
Definition: media/openimagelibraries/Image.h:57
static Frame readImage(const std::string &filename)
Reads/loads an image from a specified file.
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 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.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15