Ocean
Loading...
Searching...
No Matches
ImageWebp.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_OL_IMAGE_WEBP_H
9#define META_OCEAN_MEDIA_OPEN_IMAGE_LIBRARIES_OL_IMAGE_WEBP_H
10
12
13#include "ocean/base/Frame.h"
14
15#ifdef OCEAN_MEDIA_OIL_SUPPORT_WEBP
16
17namespace Ocean
18{
19
20namespace Media
21{
22
23namespace OpenImageLibraries
24{
25
26/**
27 * This class implements read and write functions for WEBP images.
28 * @ingroup mediaoil
29 */
30class OCEAN_MEDIA_OIL_EXPORT ImageWebp
31{
32 public:
33
34 /**
35 * Decode a WEBP image from a given binary buffer.
36 * @param buffer The buffer from which the image will be loaded, must be valid
37 * @param size The size of the given buffer in bytes, with range [1, infinity)
38 * @return The frame containing the image information; if the input has alpha the output will be `FORMAT_RGBA32` otherwise it will be `FORMAT_RGB24`; an invalid frame if the image could not be loaded
39 */
40 static Frame decodeImage(const void* buffer, const size_t size);
41
42 /**
43 * Encode (lossless) a given frame as WEBP image to a resulting buffer.
44 * @param frame The frame to be written, must be valid
45 * @param buffer The resulting buffer storing the binary information of the WEBP image
46 * @param allowConversion True, to allow an internal conversion of the frame if WEBP does not support the given frame type; False, to prevent a conversion and to stop creating the buffer
47 * @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
48 * @param quality The WEBP compression quality to be used in percent, the higher the value the better the image quality (but: larger binary size and longer compression times), with range [0, 100]
49 * @return True, if succeeded; False, if the frame could not be written as WEBP image
50 */
51 static bool encodeImage(const Frame& frame, std::vector<unsigned char>& buffer, const bool allowConversion = true, bool* hasBeenConverted = nullptr, const float quality = 80.0f);
52
53 /**
54 * Returns whether a given pixel format is supported natively.
55 * @param pixelFormat The pixel format to be checked
56 * @return True, if so; False, if a conversion will be necessary
57 */
58 static inline bool isPixelFormatSupported(const FrameType::PixelFormat pixelFormat);
59
60 /**
61 * Returns whether a given pixel origin is supported natively.
62 * @param pixelOrigin The pixel origin to be checked
63 * @return True, if so; False, if a conversion will be necessary
64 */
65 static inline bool isPixelOriginSupported(const FrameType::PixelOrigin pixelOrigin);
66
67 /**
68 * Returns whether a given pixel format together with a given pixel origin is supported natively.
69 * @param pixelFormat The pixel format to be checked
70 * @param pixelOrigin The pixel origin to be checked
71 * @return True, if so; False, if a conversion will be necessary
72 */
73 static inline bool isFrameTypeSupported(const FrameType::PixelFormat pixelFormat, const FrameType::PixelOrigin pixelOrigin);
74
75 protected:
76
77 /**
78 * Translates a WEBP status code into a human-readable string
79 * @param statusCode The WEBP status code that will be translated
80 * @return The translated, human-readable string
81 */
82 static std::string translateVP8StatusCode(const int statusCode);
83};
84
86{
87#if 0
88 // Disabled RGBA and BGRA for now; for some tiny images with alpha the WEBP encoder simply drops the alpha layer even when `WebPConfig::exact == 1`.
89 return pixelFormat == FrameType::FORMAT_RGB24 || pixelFormat == FrameType::FORMAT_BGR24
90 || pixelFormat == FrameType::FORMAT_RGBA32 || pixelFormat == FrameType::FORMAT_BGRA32;
91#else
92 return pixelFormat == FrameType::FORMAT_RGB24 || pixelFormat == FrameType::FORMAT_BGR24;
93#endif
94}
95
97{
98 return pixelOrigin == FrameType::ORIGIN_UPPER_LEFT;
99}
100
102{
103 return isPixelFormatSupported(pixelFormat) && isPixelOriginSupported(pixelOrigin);
104}
105
106}
107
108}
109
110}
111
112#endif // OCEAN_MEDIA_OIL_SUPPORT_WEBP
113
114#endif // META_OCEAN_MEDIA_OPEN_IMAGE_LIBRARIES_OL_IMAGE_WEBP_H
This class implements Ocean's image class.
Definition Frame.h:1808
PixelFormat
Definition of all pixel formats available in the Ocean framework.
Definition Frame.h:183
@ FORMAT_BGR24
Pixel format with byte order BGR and 24 bits per pixel.
Definition Frame.h:226
@ FORMAT_RGBA32
Pixel format with byte order RGBA and 32 bits per pixel.
Definition Frame.h:382
@ FORMAT_RGB24
Pixel format with byte order RGB and 24 bits per pixel.
Definition Frame.h:315
@ FORMAT_BGRA32
Pixel format with byte order BGRA and 32 bits per pixel.
Definition Frame.h:277
PixelOrigin
Defines different types of frame origin positions.
Definition Frame.h:1046
@ ORIGIN_UPPER_LEFT
The first pixel lies in the upper left corner, the last pixel in the lower right corner.
Definition Frame.h:1050
This class implements read and write functions for WEBP images.
Definition ImageWebp.h:31
static bool isFrameTypeSupported(const FrameType::PixelFormat pixelFormat, const FrameType::PixelOrigin pixelOrigin)
Returns whether a given pixel format together with a given pixel origin is supported natively.
Definition ImageWebp.h:101
static Frame decodeImage(const void *buffer, const size_t size)
Decode a WEBP image from a given binary buffer.
static bool encodeImage(const Frame &frame, std::vector< unsigned char > &buffer, const bool allowConversion=true, bool *hasBeenConverted=nullptr, const float quality=80.0f)
Encode (lossless) a given frame as WEBP image to a resulting buffer.
static bool isPixelFormatSupported(const FrameType::PixelFormat pixelFormat)
Returns whether a given pixel format is supported natively.
Definition ImageWebp.h:85
static std::string translateVP8StatusCode(const int statusCode)
Translates a WEBP status code into a human-readable string.
static bool isPixelOriginSupported(const FrameType::PixelOrigin pixelOrigin)
Returns whether a given pixel origin is supported natively.
Definition ImageWebp.h:96
The namespace covering the entire Ocean framework.
Definition Accessor.h:15