Ocean
IIOObject.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_IMAGEIO_IIO_OBJECT_H
9 #define META_OCEAN_MEDIA_IMAGEIO_IIO_OBJECT_H
10 
12 
13 #include "ocean/base/Frame.h"
14 
15 #include "ocean/media/Image.h"
16 
18 
19 #include <ImageIO/ImageIO.h>
20 
21 namespace Ocean
22 {
23 
24 namespace Media
25 {
26 
27 namespace ImageIO
28 {
29 
30 /**
31  * This class is the base class for all object inside this library.
32  * @ingroup mediaiio
33  */
34 class OCEAN_MEDIA_IIO_EXPORT IIOObject
35 {
36  protected:
37 
38  /**
39  * Definition of a pair combining a pixel format with an Apple string.
40  */
41  using PixelFormatPair = std::pair<FrameType::PixelFormat, const CFStringRef>;
42 
43  /**
44  * Definition of an unordered map mapping color profile names to a pair with pixel formats.
45  */
46  using ColorProfileMap = std::unordered_map<std::string, PixelFormatPair>;
47 
48  public:
49 
50  /**
51  * Loads a frame from an image source.
52  * @param imageSource The image source from which the frame will be loaded, must be valid
53  * @param properties Optional resulting properties which are encoded in the image; nullptr if not of interest
54  * @return The resulting frame
55  */
56  static Frame loadFrameFromImageSource(CGImageSourceRef imageSource, Media::Image::Properties* properties = nullptr);
57 
58  /**
59  * Loads a frame from an image source.
60  * @param image The image from which the frame will be loaded, must be valid
61  * @param containedPremultipliedAlpha Optional resulting statement whether the input image contained premultiplied alpha (not the resulting frame), nullptr if not of interest
62  * @param convertPremultipliedAlpha True, to convert the resulting frame to a non-premultiplied alpha frame (in case the input contained premultiplied alpha); False, to keep the frame as it is
63  * @return The resulting frame
64  */
65  static Frame loadFrameFromImage(CGImageRef image, bool* containedPremultipliedAlpha = nullptr, const bool convertPremultipliedAlpha = true);
66 
67  /**
68  * Writes a frame to an image destination, in case the pixel format of the given frame is not supported by the destination, the function will fail.
69  * As this function does not provide an automatic color space conversion, the binary impact when using this function will be quite small.
70  * @param imageDestination The image destination to which the frame will be written, must be valid
71  * @param frame The frame which will be written to the destination, must be valid
72  * @param properties The properties to be used when writing the image, must be valid
73  * @return True, if succeeded
74  */
75  static bool writeFrameToImageDestination(CGImageDestinationRef imageDestination, const Frame& frame, const Media::Image::Properties& properties = Media::Image::Properties());
76 
77  /**
78  * Writes a frame to an image destination, in case the pixel format of the given frame is not supported by the destination, the function may convert the frame internally (depending on 'allowConversion').
79  * As this function supports the conversion of color spaces, the binary impact when using this function may be quite high.
80  * @param imageDestination The image destination to which the frame will be written, must be valid
81  * @param frame The frame which will be written to the destination, must be valid
82  * @param allowConversion True, to allow an internal conversion of the frame if BMP does not support the given frame type; False, to prevent a conversion and to stop creating the buffer
83  * @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
84  * @param properties The properties to be used when writing the image, must be valid
85  * @return True, if succeeded
86  */
87  static bool writeFrameToImageDestination(CGImageDestinationRef imageDestination, const Frame& frame, const bool allowConversion, bool* hasBeenConverted = nullptr, const Media::Image::Properties& properties = Media::Image::Properties());
88 
89  /**
90  * Finds the corresponding uniform type identifier for a specified file extension.
91  * @param fileExtension The file extension for which the identifier is requested, possible values are "bmp", "gif", "jpg", "png", "tif"
92  * @return The uniform type identifier for the specified file extension, an invalid object if not matching identifer could be found
93  */
94  static Platform::Apple::ScopedCFStringRef findUniformTypeIdentifier(const std::string& fileExtension);
95 
96  /**
97  * Translates a uniform type identifier to a corresponding file extension.
98  * @param typeIdentifier The uniform type identifier to translate
99  * @return The corresponding file extension, possible values are "bmp", "gif", "jpg", "png", "tif"
100  */
101  static std::string translateUniformTypeIdentifier(CFStringRef typeIdentifier);
102 
103  /**
104  * Translates a Ocean-based pixel format to a CG-based color space and bitmap info.
105  * Some pixel formats need to be converted to an intermediate Ocean-based pixel format before a corresponding color space and bitmap info can be determined.
106  * @param pixelFormat The pixel format for which the corresponding color space and bimtap info will be determined
107  * @param colorProfileName The name of the color profile to be used, empty to use the default profile
108  * @param colorSpace The resulting CG-based color space matching to the given pixel format, must be released by the caller
109  * @param bitmapInfo The resulting CG-based bitmap info matching to the given pixel format
110  * @param targetPixelFormat The resulting target pixel format in which the given pixel format need to be converted before a corresponding color space and bitmap info can be determined
111  * @return True, if a corresponding color space and bitmap info could be determined; False, otherwise
112  */
113  static bool translatePixelFormat(const FrameType::PixelFormat pixelFormat, const std::string& colorProfileName, Platform::Apple::ScopedCGColorSpaceRef& colorSpace, CGBitmapInfo& bitmapInfo, FrameType::PixelFormat& targetPixelFormat);
114 
115  /**
116  * Determines the color space.
117  * @param mainPixelFormat The main pixel format defining the color space, either FORMAT_RGB24 or FORMAT_Y8
118  * @param colorProfileName The optional color profile name, empty to use a default profile
119  * @return The color space
120  */
121  static CGColorSpaceRef determineColorSpace(const FrameType::PixelFormat mainPixelFormat, const std::string& colorProfileName);
122 
123  protected:
124 
125  /**
126  * Disabled function to prevent wrong usage.
127  * @return Would return True on success
128  */
129  static bool writeFrameToImageDestination(CGImageDestinationRef, const Frame&, const float) = delete;
130 
131  /**
132  * Disabled function to prevent wrong usage.
133  * @return Would return True on success
134  */
135  static bool writeFrameToImageDestination(CGImageDestinationRef, const Frame&, const bool, bool*, const float) = delete;
136 };
137 
138 }
139 
140 }
141 
142 }
143 
144 #endif // META_OCEAN_MEDIA_IMAGEIO_IIO_OBJECT_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
Definition of a class holding properties.
Definition: media/Image.h:45
This class is the base class for all object inside this library.
Definition: IIOObject.h:35
static bool writeFrameToImageDestination(CGImageDestinationRef imageDestination, const Frame &frame, const Media::Image::Properties &properties=Media::Image::Properties())
Writes a frame to an image destination, in case the pixel format of the given frame is not supported ...
static Platform::Apple::ScopedCFStringRef findUniformTypeIdentifier(const std::string &fileExtension)
Finds the corresponding uniform type identifier for a specified file extension.
static Frame loadFrameFromImageSource(CGImageSourceRef imageSource, Media::Image::Properties *properties=nullptr)
Loads a frame from an image source.
static bool writeFrameToImageDestination(CGImageDestinationRef, const Frame &, const bool, bool *, const float)=delete
Disabled function to prevent wrong usage.
static bool translatePixelFormat(const FrameType::PixelFormat pixelFormat, const std::string &colorProfileName, Platform::Apple::ScopedCGColorSpaceRef &colorSpace, CGBitmapInfo &bitmapInfo, FrameType::PixelFormat &targetPixelFormat)
Translates a Ocean-based pixel format to a CG-based color space and bitmap info.
static bool writeFrameToImageDestination(CGImageDestinationRef imageDestination, const Frame &frame, const bool allowConversion, bool *hasBeenConverted=nullptr, const Media::Image::Properties &properties=Media::Image::Properties())
Writes a frame to an image destination, in case the pixel format of the given frame is not supported ...
static Frame loadFrameFromImage(CGImageRef image, bool *containedPremultipliedAlpha=nullptr, const bool convertPremultipliedAlpha=true)
Loads a frame from an image source.
std::unordered_map< std::string, PixelFormatPair > ColorProfileMap
Definition of an unordered map mapping color profile names to a pair with pixel formats.
Definition: IIOObject.h:46
static std::string translateUniformTypeIdentifier(CFStringRef typeIdentifier)
Translates a uniform type identifier to a corresponding file extension.
static bool writeFrameToImageDestination(CGImageDestinationRef, const Frame &, const float)=delete
Disabled function to prevent wrong usage.
std::pair< FrameType::PixelFormat, const CFStringRef > PixelFormatPair
Definition of a pair combining a pixel format with an Apple string.
Definition: IIOObject.h:41
static CGColorSpaceRef determineColorSpace(const FrameType::PixelFormat mainPixelFormat, const std::string &colorProfileName)
Determines the color space.
This class wraps an unmanaged object (or reference) which needs to be released after usage.
Definition: ScopedObject.h:166
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15