Ocean
ImageJpg.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_JPG_H
9 #define META_OCEAN_MEDIA_OPEN_IMAGE_LIBRARIES_OL_IMAGE_JPG_H
10 
12 
13 #include "ocean/base/Frame.h"
14 
15 #ifdef OCEAN_MEDIA_OIL_SUPPORT_JPG
16 
17 namespace Ocean
18 {
19 
20 namespace Media
21 {
22 
23 namespace OpenImageLibraries
24 {
25 
26 /**
27  * This class implements read and write functions for JPEG images.
28  * @ingroup mediaoil
29  */
30 class OCEAN_MEDIA_OIL_EXPORT ImageJpg
31 {
32  public:
33 
34  /**
35  * Decode a JPEG 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, 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 a given frame as JPEG 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 JPEG image
46  * @param allowConversion True, to allow an internal conversion of the frame if JPEG 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 JPEG compression quality to be used in percent, the higher the value the better the image quality (the larger the binary size), with range [0, 100]
49  * @return True, if succeeded; False, if the frame could not be written as JPEG image
50  */
51  static bool encodeImage(const Frame& frame, std::vector<unsigned char>& buffer, const bool allowConversion = true, bool* hasBeenConverted = nullptr, const int quality = 80);
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 JPEG pixel format defined by the color space, the component precision and the number of components.
79  * @param jpegColorSpace The color space defined by jpeglib
80  * @param jpegPrecision The number of bit per component, with range [1, infinity)
81  * @param jpegNumberComponents The number of components per pixel, with range [1, infinity)
82  * @return The resulting Ocean-based pixel format, FORMAT_UNDEFINED if no matching pixel format exists
83  */
84  static FrameType::PixelFormat translatePixelFormat(const int jpegColorSpace, const int jpegPrecision, const int jpegNumberComponents);
85 
86  /**
87  * Translates a Ocean-based pixel format to a JPEG pixel format defined by the color space, the component precision and the number of components.
88  * @param pixelFormat The Ocean-based pixel format to be translated
89  * @param jpegColorSpace Resulting color space defined by jpeglib
90  * @param jpegPrecision Resulting number of bit per component, with range [1, infinity)
91  * @param jpegNumberComponents Resulting number of components per pixel, with range [1, infinity)
92  * @return True, if the Ocean-based pixel format has a equivalent JPEG format; False, if the pixel format cannot be represented in JPEG
93  */
94  static bool translatePixelFormat(const FrameType::PixelFormat pixelFormat, int& jpegColorSpace, int& jpegPrecision, int& jpegNumberComponents);
95 };
96 
98 {
99  return pixelFormat == FrameType::FORMAT_RGB24 || pixelFormat == FrameType::FORMAT_YUV24
100  || pixelFormat == FrameType::FORMAT_Y8;
101 }
102 
104 {
105  return pixelOrigin == FrameType::ORIGIN_UPPER_LEFT;
106 }
107 
108 inline bool ImageJpg::isFrameTypeSupported(const FrameType::PixelFormat pixelFormat, const FrameType::PixelOrigin pixelOrigin)
109 {
110  return isPixelFormatSupported(pixelFormat) && isPixelOriginSupported(pixelOrigin);
111 }
112 
113 }
114 
115 }
116 
117 }
118 
119 #endif // OCEAN_MEDIA_OIL_SUPPORT_JPG
120 
121 #endif // META_OCEAN_MEDIA_OPEN_IMAGE_LIBRARIES_OL_IMAGE_JPG_H
This class implements Ocean's image class.
Definition: Frame.h:1792
PixelFormat
Definition of all pixel formats available in the Ocean framework.
Definition: Frame.h:183
@ FORMAT_YUV24
Pixel format with byte order YUV and 24 bits per pixel.
Definition: Frame.h:459
@ FORMAT_RGB24
Pixel format with byte order RGB and 24 bits per pixel.
Definition: Frame.h:315
@ FORMAT_Y8
Pixel format for grayscale images with byte order Y and 8 bits per pixel.
Definition: Frame.h:594
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 JPEG images.
Definition: ImageJpg.h:31
static bool isPixelFormatSupported(const FrameType::PixelFormat pixelFormat)
Returns whether a given pixel format is supported natively.
Definition: ImageJpg.h:97
static bool isPixelOriginSupported(const FrameType::PixelOrigin pixelOrigin)
Returns whether a given pixel origin is supported natively.
Definition: ImageJpg.h:103
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: ImageJpg.h:108
static bool translatePixelFormat(const FrameType::PixelFormat pixelFormat, int &jpegColorSpace, int &jpegPrecision, int &jpegNumberComponents)
Translates a Ocean-based pixel format to a JPEG pixel format defined by the color space,...
static Frame decodeImage(const void *buffer, const size_t size)
Decode a JPEG image from a given binary buffer.
static FrameType::PixelFormat translatePixelFormat(const int jpegColorSpace, const int jpegPrecision, const int jpegNumberComponents)
Translates a JPEG pixel format defined by the color space, the component precision and the number of ...
static bool encodeImage(const Frame &frame, std::vector< unsigned char > &buffer, const bool allowConversion=true, bool *hasBeenConverted=nullptr, const int quality=80)
Encode a given frame as JPEG image to a resulting buffer.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15