Ocean
win/Bitmap.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_PLATFORM_WIN_BITMAP_H
9 #define META_OCEAN_PLATFORM_WIN_BITMAP_H
10 
11 #include "ocean/platform/win/Win.h"
12 
13 #include "ocean/base/Frame.h"
14 #include "ocean/base/Memory.h"
15 
16 namespace Ocean
17 {
18 
19 namespace Platform
20 {
21 
22 namespace Win
23 {
24 
25 /**
26  * This class implements a Windows device independent bitmap.
27  * @ingroup platformwin
28  */
29 class OCEAN_PLATFORM_WIN_EXPORT Bitmap
30 {
31  public:
32 
33  /**
34  * Creates an invalid bitmap.
35  */
36  Bitmap() = default;
37 
38  /**
39  * Copies a bitmap.
40  * @param bitmap The bitmap to copy
41  */
42  Bitmap(const Bitmap& bitmap);
43 
44  /**
45  * Move constructor.
46  * @param bitmap The bitmap to move
47  */
48  Bitmap(Bitmap&& bitmap);
49 
50  /**
51  * Creates a new bitmap by a given frame holding the image data.
52  * @param frame The frame to create a bitmap from
53  */
54  explicit Bitmap(const Frame& frame);
55 
56  /**
57  * Creates a new bitmap with given size and pixel format.
58  * @param frameType The type of the bitmap that will be created
59  */
60  explicit Bitmap(const FrameType& frameType);
61 
62  /**
63  * Creates a new bitmap with given size and pixel format.
64  * @param width The width of the bitmap in pixel, with range [1, infinity)
65  * @param height The height of the bitmap in pixel, with range [1, infinity)
66  * @param preferredPixelFormat Pixel format of the bitmap
67  * @param pixelOrigin Frame origin
68  */
69  Bitmap(const unsigned int width, const unsigned int height, const FrameType::PixelFormat preferredPixelFormat, const FrameType::PixelOrigin pixelOrigin);
70 
71  /**
72  * Creates a new bitmap with given size and pixel data.
73  * @param width The width of the bitmap in pixel, with range [1, infinity)
74  * @param height The height of the bitmap in pixel, with range [1, infinity)
75  * @param pixelFormat The pixel format of the given data, must be supported
76  * @param pixelOrigin The origin of the given frame data
77  * @param frame The frame data to be copied, must be valid
78  * @param framePaddingElements Optional number of padding elements at the end of each frame row, in elements, with range [0, infinity)
79  * @see internalPixelFormat().
80  */
81  Bitmap(unsigned int width, unsigned int height, const FrameType::PixelFormat pixelFormat, const FrameType::PixelOrigin pixelOrigin, const uint8_t* frame, const unsigned int framePaddingElements);
82 
83  /**
84  * Destructs a bitmap.
85  */
87 
88  /**
89  * Returns the width of the bitmap.
90  * @return The bitmap's width, in pixel, with range [0, infinity)
91  */
92  inline unsigned int width() const;
93 
94  /**
95  * Returns the height of the bitmap.
96  * @return The bitmap's height, in pixel with range [0, infinity)
97  */
98  inline unsigned int height() const;
99 
100  /**
101  * Returns the bitmap's pixel format.
102  * @return Pixel format
103  * @see internalPixelFormat().
104  */
105  inline FrameType::PixelFormat pixelFormat() const;
106 
107  /**
108  * Returns the origin of the bitmap.
109  * @return Frame origin
110  */
111  inline FrameType::PixelOrigin pixelOrigin() const;
112 
113  /**
114  * Returns the device context of the bitmap.
115  * @return Device context
116  */
117  inline HDC dc() const;
118 
119  /**
120  * Returns the pixel data of the bitmap.
121  * @return Pixel data
122  */
123  inline const uint8_t* data() const;
124 
125  /**
126  * Returns the pixel data of the bitmap.
127  * @return Pixel data
128  */
129  inline uint8_t* data();
130 
131  /**
132  * Returns the size of the bitmap's internal image buffer.
133  * @return The bitmap's internal image buffer, in bytes, with range [0, infinity)
134  */
135  inline unsigned int size() const;
136 
137  /**
138  * Returns the number of padding elements at the end of each bitmap row.
139  * @return The number of padding elements, in elements, with range [0, 3]
140  */
141  inline unsigned int paddingElements() const;
142 
143  /**
144  * Returns the number of elements per bitmap row including optional padding elements at the end of each row.
145  * Windows bitmap strides are 4-byte aligned.
146  * @return The number of elements per bitmap row, in elements, with range [width * channels, infinity)
147  */
148  inline unsigned int strideElements() const;
149 
150  /**
151  * Sets the pixel data of the bitmap.
152  * The pixel format of the given frame must match with the bitmap's internal pixel format.
153  * Both pixel formats match if bitmap.internalPixelFormat(pixelFormat) == bitmap.pixelFormat().
154  * @param frame The frame data to be set, must fit the resolution of this bitmap, must be valid
155  * @param pixelFormat The pixel format of the given data, must be a supported pixel format, must be valid
156  * @param framePaddingElements The number of padding elements at the end of each frame row, in elements, with range [0, infinity)
157  * @return True, if succeeded
158  * @see internalPixelFormat().
159  */
160  bool setData(const uint8_t* frame, const FrameType::PixelFormat pixelFormat, const unsigned int framePaddingElements);
161 
162  /**
163  * Sets or changes the format of the bitmap.
164  * @param width The width of the bitmap in pixel, with range [1, infinity)
165  * @param height The height of the bitmap in pixel, with range [1, infinity)
166  * @param preferredPixelFormat Preferred pixel format of the bitmap, this may be changed internally
167  * @param pixelOrigin The origin of the bitmap
168  * @return True, if the format could be changed
169  */
170  bool set(const unsigned int width, const unsigned int height, const FrameType::PixelFormat preferredPixelFormat, const FrameType::PixelOrigin pixelOrigin);
171 
172  /**
173  * Sets or changes the bitmap data.
174  * @param frame The frame to set
175  * @return True, if succeeded
176  */
177  bool set(const Frame& frame);
178 
179  /**
180  * Releases the bitmap data and resets all values to default values.
181  */
182  void release();
183 
184  /**
185  * Returns whether the bitmap holds valid data.
186  * @return True, if so
187  */
188  bool isValid() const;
189 
190  /**
191  * Move operator.
192  * @param bitmap The bitmap to move
193  * @return Reference to this object
194  */
195  Bitmap& operator=(Bitmap&& bitmap);
196 
197  /**
198  * Assigns a bitmap.
199  * @param bitmap Right bitmap
200  * @return Reference to this bitmap
201  */
202  Bitmap& operator=(const Bitmap& bitmap);
203 
204  /**
205  * Returns which (internal) pixel format the bitmap uses to store the image data for a given pixel format.
206  * @param pixelFormat The pixel format for which the bitmap's internal pixel format will be returned, must be valid
207  * @return The bitmap's internal pixel format for the specified pixel format, FORMAT_UNDEFINED if no internal pixel format exists
208  */
210 
211  protected:
212 
213  /**
214  * Creates a new bitmap object.
215  * @param width The width of the bitmap in pixel, with range [1, infinity)
216  * @param height The height of the bitmap in pixel, with range [1, infinity)
217  * @param supportedPixelFormat The pixel format of the bitmap, must be supported by the bitmap, must be valid
218  * @param pixelOrigin The origin of the bitmap
219  * @return True, if succeeded
220  */
221  bool createBitmap(const unsigned int width, const unsigned int height, const FrameType::PixelFormat supportedPixelFormat, const FrameType::PixelOrigin pixelOrigin);
222 
223  /**
224  * Creates the bitmap info for a specified bitmap.
225  * @param width The width of the bitmap in pixel, with range [1, infinity)
226  * @param height The height of the bitmap in pixel, with range [1, infinity)
227  * @param pixelFormat The pixel format of the bitmap, must be supported, must be valid
228  * @param pixelOrigin The origin of the bitmap
229  * @param bitmapInfoMemory The resulting bitmap info
230  * @return True, if succeeded
231  */
232  static bool createBitmapInfo(const unsigned int width, const unsigned int height, const FrameType::PixelFormat pixelFormat, const FrameType::PixelOrigin pixelOrigin, Memory& bitmapInfoMemory);
233 
234  protected:
235 
236  /// Width of the bitmap in pixel, with range [0, infinity)
237  unsigned int bitmapWidth_ = 0u;
238 
239  /// Height of the bitmap in pixel, with range [0, infinity)
240  unsigned int bitmapHeight_ = 0u;
241 
242  /// Pixel format of the bitmap.
244 
245  /// Origin of the frame
247 
248  /// Pixel data of the bitmap.
249  uint8_t* bitmapData_ = nullptr;
250 
251  /// Size of the pixel buffer in bytes.
252  unsigned int bitmapSize_ = 0u;
253 
254  /// The number of padding elements at the end of each bitmap row, with range [0, 3].
255  unsigned int bitmapPaddingElements_ = 0u;
256 
257  /// Number of elements per bitmap row including optional padding elements at the end of each row, with range [width * channels, infinity)
258  unsigned int bitmapStrideElements_ = 0u;
259 
260  /// Device context of the bitmap.
261  HDC bitmapDC_ = nullptr;
262 
263  /// Handle of the bitmap.
264  HBITMAP bitmapHandle_ = nullptr;
265 };
266 
267 inline unsigned int Bitmap::width() const
268 {
269  return bitmapWidth_;
270 }
271 
272 inline unsigned int Bitmap::height() const
273 {
274  return bitmapHeight_;
275 }
276 
278 {
279  return bitmapPixelFormat_;
280 }
281 
283 {
284  return bitmapPixelOrigin_;
285 }
286 
287 inline HDC Bitmap::dc() const
288 {
289  return bitmapDC_;
290 }
291 
292 inline const uint8_t* Bitmap::data() const
293 {
294  return bitmapData_;
295 }
296 
297 inline uint8_t* Bitmap::data()
298 {
299  return bitmapData_;
300 }
301 
302 inline unsigned int Bitmap::size() const
303 {
304  return bitmapSize_;
305 }
306 
307 inline unsigned int Bitmap::paddingElements() const
308 {
309  return bitmapPaddingElements_;
310 }
311 
312 
313 inline unsigned int Bitmap::strideElements() const
314 {
315  return bitmapStrideElements_;
316 }
317 
318 }
319 
320 }
321 
322 }
323 
324 #endif // META_OCEAN_PLATFORM_WIN_BITMAP_H
This class implements Ocean's image class.
Definition: Frame.h:1792
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
@ FORMAT_UNDEFINED
Undefined pixel format.
Definition: Frame.h:187
PixelOrigin
Defines different types of frame origin positions.
Definition: Frame.h:1046
@ ORIGIN_INVALID
Invalid origin type.
Definition: Frame.h:1048
This class implements an object able to allocate memory.
Definition: base/Memory.h:22
This class implements a Windows device independent bitmap.
Definition: win/Bitmap.h:30
FrameType::PixelOrigin bitmapPixelOrigin_
Origin of the frame.
Definition: win/Bitmap.h:246
unsigned int bitmapStrideElements_
Number of elements per bitmap row including optional padding elements at the end of each row,...
Definition: win/Bitmap.h:258
unsigned int bitmapSize_
Size of the pixel buffer in bytes.
Definition: win/Bitmap.h:252
unsigned int size() const
Returns the size of the bitmap's internal image buffer.
Definition: win/Bitmap.h:302
Bitmap(const Bitmap &bitmap)
Copies a bitmap.
unsigned int paddingElements() const
Returns the number of padding elements at the end of each bitmap row.
Definition: win/Bitmap.h:307
Bitmap(unsigned int width, unsigned int height, const FrameType::PixelFormat pixelFormat, const FrameType::PixelOrigin pixelOrigin, const uint8_t *frame, const unsigned int framePaddingElements)
Creates a new bitmap with given size and pixel data.
FrameType::PixelFormat bitmapPixelFormat_
Pixel format of the bitmap.
Definition: win/Bitmap.h:243
HDC bitmapDC_
Device context of the bitmap.
Definition: win/Bitmap.h:261
unsigned int strideElements() const
Returns the number of elements per bitmap row including optional padding elements at the end of each ...
Definition: win/Bitmap.h:313
Bitmap & operator=(const Bitmap &bitmap)
Assigns a bitmap.
bool createBitmap(const unsigned int width, const unsigned int height, const FrameType::PixelFormat supportedPixelFormat, const FrameType::PixelOrigin pixelOrigin)
Creates a new bitmap object.
Bitmap()=default
Creates an invalid bitmap.
HDC dc() const
Returns the device context of the bitmap.
Definition: win/Bitmap.h:287
Bitmap(Bitmap &&bitmap)
Move constructor.
static FrameType::PixelFormat internalPixelFormat(const FrameType::PixelFormat pixelFormat)
Returns which (internal) pixel format the bitmap uses to store the image data for a given pixel forma...
uint8_t * bitmapData_
Pixel data of the bitmap.
Definition: win/Bitmap.h:249
bool isValid() const
Returns whether the bitmap holds valid data.
Bitmap(const FrameType &frameType)
Creates a new bitmap with given size and pixel format.
FrameType::PixelOrigin pixelOrigin() const
Returns the origin of the bitmap.
Definition: win/Bitmap.h:282
unsigned int bitmapPaddingElements_
The number of padding elements at the end of each bitmap row, with range [0, 3].
Definition: win/Bitmap.h:255
Bitmap & operator=(Bitmap &&bitmap)
Move operator.
Bitmap(const Frame &frame)
Creates a new bitmap by a given frame holding the image data.
void release()
Releases the bitmap data and resets all values to default values.
unsigned int width() const
Returns the width of the bitmap.
Definition: win/Bitmap.h:267
Bitmap(const unsigned int width, const unsigned int height, const FrameType::PixelFormat preferredPixelFormat, const FrameType::PixelOrigin pixelOrigin)
Creates a new bitmap with given size and pixel format.
unsigned int height() const
Returns the height of the bitmap.
Definition: win/Bitmap.h:272
bool set(const Frame &frame)
Sets or changes the bitmap data.
const uint8_t * data() const
Returns the pixel data of the bitmap.
Definition: win/Bitmap.h:292
unsigned int bitmapHeight_
Height of the bitmap in pixel, with range [0, infinity)
Definition: win/Bitmap.h:240
~Bitmap()
Destructs a bitmap.
bool set(const unsigned int width, const unsigned int height, const FrameType::PixelFormat preferredPixelFormat, const FrameType::PixelOrigin pixelOrigin)
Sets or changes the format of the bitmap.
static bool createBitmapInfo(const unsigned int width, const unsigned int height, const FrameType::PixelFormat pixelFormat, const FrameType::PixelOrigin pixelOrigin, Memory &bitmapInfoMemory)
Creates the bitmap info for a specified bitmap.
FrameType::PixelFormat pixelFormat() const
Returns the bitmap's pixel format.
Definition: win/Bitmap.h:277
unsigned int bitmapWidth_
Width of the bitmap in pixel, with range [0, infinity)
Definition: win/Bitmap.h:237
bool setData(const uint8_t *frame, const FrameType::PixelFormat pixelFormat, const unsigned int framePaddingElements)
Sets the pixel data of the bitmap.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15