Ocean
Loading...
Searching...
No Matches
ImagePfm.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_PFM_H
9#define META_OCEAN_MEDIA_SPECIAL_IMAGE_PFM_H
10
12
13#include "ocean/base/Frame.h"
14
15namespace Ocean
16{
17
18namespace Media
19{
20
21namespace Special
22{
23
24/**
25 * This class implements read and write functions for PFM images.
26 * @ingroup mediaspecial
27 */
28class OCEAN_MEDIA_SPECIAL_EXPORT ImagePfm
29{
30 public:
31
32 /**
33 * Decodes a PFM 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 * @return The frame containing the image information, an invalid frame if the image could not be loaded
37 */
38 static Frame decodeImage(const void* buffer, const size_t size);
39
40 /**
41 * Encodes a given frame as PFM image to a resulting buffer.
42 * @param frame The frame to be written, must be valid
43 * @param buffer The resulting buffer storing the binary information of the PFM image
44 * @return True, if succeeded
45 */
46 static bool encodeImage(const Frame& frame, std::vector<uint8_t>& buffer);
47
48 /**
49 * Returns whether a given pixel format is supported natively.
50 * @param pixelFormat The pixel format to be checked
51 * @return True, if so; False, if a conversion will be necessary
52 */
53 static inline bool isPixelFormatSupported(const FrameType::PixelFormat pixelFormat);
54
55 /**
56 * Returns whether a given pixel origin is supported natively.
57 * @param pixelOrigin The pixel origin to be checked
58 * @return True, if so; False, if a conversion will be necessary
59 */
60 static inline bool isPixelOriginSupported(const FrameType::PixelOrigin pixelOrigin);
61
62 /**
63 * Returns whether a given pixel format together with a given pixel origin is supported natively.
64 * @param pixelFormat The pixel format to be checked
65 * @param pixelOrigin The pixel origin to be checked
66 * @return True, if so; False, if a conversion will be necessary
67 */
68 static inline bool isFrameTypeSupported(const FrameType::PixelFormat pixelFormat, const FrameType::PixelOrigin pixelOrigin);
69
70 protected:
71
72 /**
73 * Reads the header of a PFM file.
74 * @param data The data buffer, will be moved if the header could be read successfully, must be valid
75 * @param size The size of the buffer in bytes, will be reduced if the header could be read successfully, must be valid
76 * @param frameType The resulting frame type
77 * @param isLittleEndian True, if the buffer provides the data with little endian encoding; False, if the buffer provides the data with big endian encoding
78 * @return True, if succeeded
79 */
80 static bool readHeader(const uint8_t*& data, size_t& size, FrameType& frameType, bool& isLittleEndian);
81
82 /**
83 * Reads one number value from a given data buffer.
84 * The value ends with a space or a new line.
85 * @param data The data buffer, will be moved if the value could be read successfully, must be valid
86 * @param size The size of the buffer in bytes, will be reduced if the value could be read successfully, must be valid
87 * @param value The resulting value
88 * @return True, if succeeded
89 */
90 static bool readValue(const uint8_t*& data, size_t& size, std::string& value);
91};
92
94{
95 return FrameType::arePixelFormatsCompatible(pixelFormat, FrameType::genericPixelFormat<float, 1u>())
96 || FrameType::arePixelFormatsCompatible(pixelFormat, FrameType::genericPixelFormat<float, 3u>());
97}
98
100{
101 return pixelOrigin == FrameType::ORIGIN_LOWER_LEFT || pixelOrigin == FrameType::ORIGIN_UPPER_LEFT;
102}
103
104inline bool ImagePfm::isFrameTypeSupported(const FrameType::PixelFormat pixelFormat, const FrameType::PixelOrigin pixelOrigin)
105{
106 return isPixelFormatSupported(pixelFormat) && isPixelOriginSupported(pixelOrigin);
107}
108
109}
110
111}
112
113}
114
115#endif // META_OCEAN_MEDIA_SPECIAL_IMAGE_PFM_H
This class implements Ocean's image class.
Definition Frame.h:1808
Definition of a frame type composed by the frame dimension, pixel format and pixel origin.
Definition Frame.h:30
PixelFormat
Definition of all pixel formats available in the Ocean framework.
Definition Frame.h:183
static bool arePixelFormatsCompatible(const PixelFormat pixelFormatA, const PixelFormat pixelFormatB)
Returns whether two given pixel formats are compatible.
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
@ ORIGIN_LOWER_LEFT
The first pixel lies in the lower left corner, the last pixel in the upper right corner.
Definition Frame.h:1052
This class implements read and write functions for PFM images.
Definition ImagePfm.h:29
static bool encodeImage(const Frame &frame, std::vector< uint8_t > &buffer)
Encodes a given frame as PFM image to a resulting buffer.
static bool readHeader(const uint8_t *&data, size_t &size, FrameType &frameType, bool &isLittleEndian)
Reads the header of a PFM file.
static bool isPixelFormatSupported(const FrameType::PixelFormat pixelFormat)
Returns whether a given pixel format is supported natively.
Definition ImagePfm.h:93
static Frame decodeImage(const void *buffer, const size_t size)
Decodes a PFM image from a given binary buffer.
static bool readValue(const uint8_t *&data, size_t &size, std::string &value)
Reads one number value from a given data buffer.
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 ImagePfm.h:104
static bool isPixelOriginSupported(const FrameType::PixelOrigin pixelOrigin)
Returns whether a given pixel origin is supported natively.
Definition ImagePfm.h:99
The namespace covering the entire Ocean framework.
Definition Accessor.h:15