Ocean
Loading...
Searching...
No Matches
ImageNpy.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_NPY_H
9#define META_OCEAN_MEDIA_SPECIAL_IMAGE_NPY_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 NPY (NumPy) images.
26 * @ingroup mediaoil
27 */
28class OCEAN_MEDIA_SPECIAL_EXPORT ImageNpy
29{
30 public:
31
32 /**
33 * Decodes a NPY 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 NPY 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 NPY 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 NPY 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 isRowMajor True, if the data is given in row-major order; False, if the data is given in column-major order
78 * @return True, if succeeded
79 */
80 static bool readHeader(const uint8_t*& data, size_t& size, FrameType& frameType, bool& isRowMajor);
81
82 /**
83 * Translates the pixel format.
84 * @param pixelFormat The pixel format to be translated given as string
85 * @return The resulting pixel format, FORMAT_UNDEFINED if unknown
86 */
87 static FrameType::PixelFormat translatePixelFormat(const std::string& pixelFormat);
88
89 /**
90 * Translates the pixel format.
91 * @param pixelFormat The pixel format to be translated
92 * @return The resulting pixel format as string, empty if unknown
93 */
94 static std::string translatePixelFormat(const FrameType::PixelFormat pixelFormat);
95};
96
98{
99 return !translatePixelFormat(pixelFormat).empty();
100}
101
103{
104 return pixelOrigin == FrameType::ORIGIN_UPPER_LEFT || pixelOrigin == FrameType::ORIGIN_LOWER_LEFT;
105}
106
107inline bool ImageNpy::isFrameTypeSupported(const FrameType::PixelFormat pixelFormat, const FrameType::PixelOrigin pixelOrigin)
108{
109 return isPixelFormatSupported(pixelFormat) && isPixelOriginSupported(pixelOrigin);
110}
111
112}
113
114}
115
116}
117
118#endif // META_OCEAN_MEDIA_SPECIAL_IMAGE_NPY_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
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 NPY (NumPy) images.
Definition ImageNpy.h:29
static FrameType::PixelFormat translatePixelFormat(const std::string &pixelFormat)
Translates the pixel format.
static bool isPixelFormatSupported(const FrameType::PixelFormat pixelFormat)
Returns whether a given pixel format is supported natively.
Definition ImageNpy.h:97
static Frame decodeImage(const void *buffer, const size_t size)
Decodes a NPY image from a given binary buffer.
static bool readHeader(const uint8_t *&data, size_t &size, FrameType &frameType, bool &isRowMajor)
Reads the header of a NPY file.
static bool encodeImage(const Frame &frame, std::vector< uint8_t > &buffer)
Encodes a given frame as NPY image to a resulting 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 ImageNpy.h:107
static std::string translatePixelFormat(const FrameType::PixelFormat pixelFormat)
Translates the pixel format.
static bool isPixelOriginSupported(const FrameType::PixelOrigin pixelOrigin)
Returns whether a given pixel origin is supported natively.
Definition ImageNpy.h:102
The namespace covering the entire Ocean framework.
Definition Accessor.h:15