Ocean
Loading...
Searching...
No Matches
ImagePng.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_PNG_H
9#define META_OCEAN_MEDIA_OPEN_IMAGE_LIBRARIES_IMAGE_PNG_H
10
12
13#include "ocean/base/Frame.h"
15
16#ifdef OCEAN_MEDIA_OIL_SUPPORT_PNG
17
18namespace Ocean
19{
20
21namespace Media
22{
23
24namespace OpenImageLibraries
25{
26
27/**
28 * This class implements read and write functions for PNG images.
29 * @ingroup mediaoil
30 */
31class OCEAN_MEDIA_OIL_EXPORT ImagePng
32{
33 protected:
34
35 /**
36 * Definition of a pair combining a pointer to data input buffer with the size of the buffer in bytes.
37 */
38 typedef std::pair<const uint8_t**, size_t> DataInputPair;
39
40 /**
41 * Definition of a pair combining an output buffer (with additional reserved bytes) with the number of bytes actually used in the buffer.
42 */
43 typedef std::pair<std::vector<uint8_t>, size_t> OutputDataPair;
44
45 public:
46
47 /**
48 * Decodes a PNG image from a given binary buffer.
49 * @param buffer The buffer from which the image will be loaded, must be valid
50 * @param size The size of the given buffer in bytes, with range [1, infinity)
51 * @return The frame containing the image information, an invalid frame if the image could not be loaded
52 */
53 static Frame decodeImage(const void* buffer, const size_t size);
54
55 /**
56 * Encodes a given frame as PNG image to a resulting buffer.
57 * @param frame The frame to be written, must be valid
58 * @param buffer The resulting buffer storing the binary information of the PNG image
59 * @param allowConversion True, to allow an internal conversion of the frame if PNG does not support the given frame type; False, to prevent a conversion and to stop creating the buffer
60 * @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
61 * @return True, if succeeded; False, if the frame could not be written as PNG image
62 */
63 static bool encodeImage(const Frame& frame, std::vector<uint8_t>& buffer, const bool allowConversion = true, bool* hasBeenConverted = nullptr);
64
65 /**
66 * Returns whether a given pixel format is supported natively.
67 * @param pixelFormat The pixel format to be checked
68 * @return True, if so; False, if a conversion will be necessary
69 */
70 static inline bool isPixelFormatSupported(const FrameType::PixelFormat pixelFormat);
71
72 /**
73 * Returns whether a given pixel origin is supported natively.
74 * @param pixelOrigin The pixel origin to be checked
75 * @return True, if so; False, if a conversion will be necessary
76 */
77 static inline bool isPixelOriginSupported(const FrameType::PixelOrigin pixelOrigin);
78
79 /**
80 * Returns whether a given pixel format together with a given pixel origin is supported natively.
81 * @param pixelFormat The pixel format to be checked
82 * @param pixelOrigin The pixel origin to be checked
83 * @return True, if so; False, if a conversion will be necessary
84 */
85 static inline bool isFrameTypeSupported(const FrameType::PixelFormat pixelFormat, const FrameType::PixelOrigin pixelOrigin);
86
87 protected:
88
89 /**
90 * Translates a PNG pixel format defined by the color type and the bit depth per channel.
91 * @param pngColorType The color space defined by libpng
92 * @param pngBitDepthPerChannel The number of bit per channel, with range [1, infinity)
93 * @return The resulting Ocean-based pixel format, FORMAT_UNDEFINED if no matching pixel format exists
94 */
95 static FrameType::PixelFormat translatePixelFormat(const int pngColorType, const int pngBitDepthPerChannel);
96
97 /**
98 * Translates a Ocean-based pixel format to a PNG pixel format defined by the color type and the bit depth per channel.
99 * @param pixelFormat Ocean-based pixel format to be converted
100 * @param pngColorType Resulting color space defined by libpng
101 * @param pngBitDepthPerChannel Resulting number of bit per channel, with range [1, infinity)
102 * @return True, if the Ocean-based pixel format has a equivalent PNG format; False, if the pixel format cannot be represented in PNG
103 */
104 static bool translatePixelFormat(const FrameType::PixelFormat pixelFormat, int& pngColorType, int& pngBitDepthPerChannel);
105
106 /**
107 * Reads a defined number of bytes from a given buffer and copies them to a given buffer provided by PNG.
108 * @param pngReadStruct The PNG read struct requesting the data, must be valid
109 * @param outBytes The buffer provided by PNG receiving the data, must be valid
110 * @param byteCountToRead The number of bytes that are requested
111 */
112 static void readInputData(void* pngReadStruct, unsigned char* outBytes, const size_t byteCountToRead);
113
114 /**
115 * Writes a defined number of bytes from a given PNG buffer to an external buffer.
116 * @param pngWriteStruct The PNG write struct providing the data, must be valid
117 * @param buffer The buffer provided by PNG holding the data, must be valid
118 * @param size The number of bytes provided in the buffer
119 */
120 static void writeOutputData(void* pngWriteStruct, unsigned char* buffer, const size_t size);
121
122 /**
123 * Event function triggered by PNG to flush the external buffer.
124 * @param pngWriteStruct The PNG write struct triggering the event, must be valid
125 */
126 static inline void flushOutputData(void* pngWriteStruct);
127};
128
129inline void ImagePng::flushOutputData(void* /*pngWriteStruct*/)
130{
131 // nothing to do here
132}
133
135{
136 return pixelFormat == FrameType::FORMAT_RGB24
137 || pixelFormat == FrameType::FORMAT_RGBA32
138 || pixelFormat == FrameType::FORMAT_RGBA64
139 || pixelFormat == FrameType::FORMAT_Y8
140 || pixelFormat == FrameType::FORMAT_Y16
141 || pixelFormat == FrameType::FORMAT_YA16;
142}
143
145{
146 return pixelOrigin == FrameType::ORIGIN_UPPER_LEFT;
147}
148
149inline bool ImagePng::isFrameTypeSupported(const FrameType::PixelFormat pixelFormat, const FrameType::PixelOrigin pixelOrigin)
150{
151 return isPixelFormatSupported(pixelFormat) && isPixelOriginSupported(pixelOrigin);
152}
153
154}
155
156}
157
158}
159
160#endif // OCEAN_MEDIA_OIL_SUPPORT_PNG
161
162#endif // META_OCEAN_MEDIA_OPEN_IMAGE_LIBRARIES_IMAGE_PNG_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_YA16
Pixel format with byte order YA and 16 bits per pixel.
Definition Frame.h:649
@ FORMAT_Y16
Pixel format with 16 bits Y frame.
Definition Frame.h:634
@ FORMAT_RGBA64
Pixel format with byte order RGBA and 64 bits per pixel, with 16 bit per component.
Definition Frame.h:675
@ 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_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 PNG images.
Definition ImagePng.h:32
static bool encodeImage(const Frame &frame, std::vector< uint8_t > &buffer, const bool allowConversion=true, bool *hasBeenConverted=nullptr)
Encodes a given frame as PNG image to a resulting buffer.
static bool isPixelFormatSupported(const FrameType::PixelFormat pixelFormat)
Returns whether a given pixel format is supported natively.
Definition ImagePng.h:134
std::pair< const uint8_t **, size_t > DataInputPair
Definition of a pair combining a pointer to data input buffer with the size of the buffer in bytes.
Definition ImagePng.h:38
static void writeOutputData(void *pngWriteStruct, unsigned char *buffer, const size_t size)
Writes a defined number of bytes from a given PNG buffer to an external buffer.
static bool translatePixelFormat(const FrameType::PixelFormat pixelFormat, int &pngColorType, int &pngBitDepthPerChannel)
Translates a Ocean-based pixel format to a PNG pixel format defined by the color type and the bit dep...
static void readInputData(void *pngReadStruct, unsigned char *outBytes, const size_t byteCountToRead)
Reads a defined number of bytes from a given buffer and copies them to a given buffer provided by PNG...
static bool isPixelOriginSupported(const FrameType::PixelOrigin pixelOrigin)
Returns whether a given pixel origin is supported natively.
Definition ImagePng.h:144
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 ImagePng.h:149
static FrameType::PixelFormat translatePixelFormat(const int pngColorType, const int pngBitDepthPerChannel)
Translates a PNG pixel format defined by the color type and the bit depth per channel.
static void flushOutputData(void *pngWriteStruct)
Event function triggered by PNG to flush the external buffer.
Definition ImagePng.h:129
std::pair< std::vector< uint8_t >, size_t > OutputDataPair
Definition of a pair combining an output buffer (with additional reserved bytes) with the number of b...
Definition ImagePng.h:43
static Frame decodeImage(const void *buffer, const size_t size)
Decodes a PNG image from a given binary buffer.
The namespace covering the entire Ocean framework.
Definition Accessor.h:15