docs
Loading...
Searching...
No Matches
CAPI_Bitmap.h
Go to the documentation of this file.
1///
2/// Copyright (C) 2026 Ultralight, Inc. All rights reserved.
3/// A license is required for commercial use. https://ultralig.ht
4///
5
6///
7/// @file CAPI_Bitmap.h
8///
9/// A container for pixel data.
10///
11/// `#include <Ultralight/CAPI/CAPI_Bitmap.h>`
12///
13/// The bitmap class is used to store pixel data in a variety of formats. It intelligently manages
14/// the lifetime of the pixel buffer and serializes access to it while the bitmap owns the pixels.
15///
16/// ## Thread Safety
17///
18/// ulBitmapLockPixels() and ulBitmapUnlockPixels() take the bitmap's internal lock only when the
19/// bitmap owns its pixel buffer, ie. one created by a creation function that allocates, or one
20/// that copied the pixels you passed in. A bitmap that wraps memory you still own-- created with
21/// `should_copy = false`-- does no locking at all, so you must serialize access to that buffer
22/// yourself.
23///
24/// ## Accessing Pixel Data
25///
26/// To access the pixel data, you must first lock the pixels using ulBitmapLockPixels(). This will
27/// return a pointer to the pixel buffer. An example follows:
28///
29/// ```
30/// void* pixels = ulBitmapLockPixels(bitmap);
31/// if (pixels) {
32/// // Zero out the pixel buffer
33/// memset(pixels, 0, ulBitmapGetSize(bitmap));
34/// }
35///
36/// // Unlock the pixels when you're done.
37/// ulBitmapUnlockPixels(bitmap);
38/// ```
39///
40#ifndef ULTRALIGHT_CAPI_BITMAP_H
41#define ULTRALIGHT_CAPI_BITMAP_H
42
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49/******************************************************************************
50 * Bitmap Format
51 *****************************************************************************/
52
53///
54/// Block geometry of a pixel format.
55///
56/// Every format stores its pixels in blocks: a 1x1 block for an uncompressed format, and a 4x4
57/// block for a block-compressed (BCn) format. The storage of an image follows from the block size:
58///
59/// ```
60/// row_pitch = ceil(width / block_width) * block_bytes
61/// surface_size = row_pitch * ceil(height / block_height)
62/// ```
63///
64/// For a 1x1 block this reduces to `width * block_bytes` and `row_pitch * height`.
65///
66/// @see ulGetBitmapFormatInfo()
67///
68typedef struct {
69 unsigned int block_width; ///< Width of one block, in pixels (1 for uncompressed, 4 for BCn).
70 unsigned int block_height; ///< Height of one block, in pixels (1 for uncompressed, 4 for BCn).
71 unsigned int block_bytes; ///< Size of one block, in bytes (the bytes per pixel for a 1x1 block).
73
74///
75/// Get the block geometry for a given pixel format.
76///
77/// @param format The pixel format to query.
78///
79/// @return Returns the block width, block height, and block size in bytes for the format.
80///
82
83///
84/// Whether or not a given pixel format is block-compressed (a BCn format).
85///
86/// @param format The pixel format to query.
87///
88/// @return Returns true for kBitmapFormat_BC1_UNORM, kBitmapFormat_BC2_UNORM,
89/// kBitmapFormat_BC3_UNORM, and kBitmapFormat_BC7_UNORM.
90///
92
93/******************************************************************************
94 * Bitmap
95 *****************************************************************************/
96
97///
98/// Create an empty bitmap. No pixels will be allocated.
99///
100/// @return Returns a new bitmap instance. You must call ulDestroyBitmap() when finished.
101///
103
104///
105/// Create bitmap with certain dimensions and pixel format. Pixels will be allocated but not
106/// initialized.
107///
108/// @param width The width in pixels.
109///
110/// @param height The height in pixels.
111///
112/// @param format The pixel format to use.
113///
114/// @return Returns a new bitmap instance. You must call ulDestroyBitmap() when finished.
115///
116ULExport ULBitmap ulCreateBitmap(unsigned int width, unsigned int height, ULBitmapFormat format);
117
118///
119/// Create bitmap with certain dimensions, pixel format, and row byte alignment.
120///
121/// @param width The width in pixels.
122///
123/// @param height The height in pixels.
124///
125/// @param format The pixel format to use.
126///
127/// @param alignment The alignment in bytes. Row bytes will be padded to a multiple of this value.
128///
129/// @return Returns a new bitmap instance. You must call ulDestroyBitmap() when finished.
130///
131ULExport ULBitmap ulCreateBitmapAligned(unsigned int width, unsigned int height,
132 ULBitmapFormat format, unsigned int alignment);
133
134///
135/// Create a bitmap from an existing pixel buffer.
136///
137/// @param width The width in pixels.
138///
139/// @param height The height in pixels.
140///
141/// @param format The pixel format to use.
142///
143/// @param row_bytes The number of bytes between each row of pixels. This should be at least
144/// width * bytes_per_pixel, or for a block-compressed format, the width in
145/// 4x4 blocks times the block size (see ulGetBitmapFormatInfo()).
146///
147/// @param pixels Pointer to the raw pixel buffer.
148///
149/// @param size Size of the raw pixel buffer in bytes.
150///
151/// @param should_copy Whether or not a copy should be made of the pixels. If this is false, the
152/// returned bitmap will reference the raw pixels directly and you must keep
153/// the buffer alive until the bitmap is destroyed.
154///
155/// @return Returns a new bitmap instance. You must call ulDestroyBitmap() when finished.
156///
157ULExport ULBitmap ulCreateBitmapFromPixels(unsigned int width, unsigned int height,
158 ULBitmapFormat format, unsigned int row_bytes,
159 const void* pixels, size_t size, bool should_copy);
160
161///
162/// Create a bitmap from a deep copy of another bitmap.
163///
164/// @param existing_bitmap The bitmap to copy from.
165///
166/// @return Returns a new bitmap instance. You must call ulDestroyBitmap() when finished.
167///
169
170///
171/// Destroy a bitmap.
172///
173/// You should only destroy bitmaps you have explicitly created via one of the creation functions
174/// above.
175///
176/// @param bitmap The bitmap to destroy.
177///
179
180///
181/// Get the width in pixels.
182///
183ULExport unsigned int ulBitmapGetWidth(ULBitmap bitmap);
184
185///
186/// Get the height in pixels.
187///
189
190///
191/// Get the bounds as a ULIntRect.
192///
194
195///
196/// Get the pixel format.
197///
199
200///
201/// Get the bytes per pixel.
202///
203ULExport unsigned int ulBitmapGetBpp(ULBitmap bitmap);
204
205///
206/// Get the number of bytes per row.
207///
209
210///
211/// Get the size in bytes of the underlying pixel buffer.
212///
214
215///
216/// Whether or not this bitmap owns its own pixel buffer.
217///
219
220///
221/// Whether or not this bitmap uses a block-compressed (BCn) format.
222///
223/// @note Compressed bitmaps store 4x4 pixel blocks and don't support per-pixel operations.
224/// ulBitmapErase(), ulBitmapDrawBitmap(), ulBitmapResample(), and the channel/alpha
225/// conversions are no-ops or fail on a compressed bitmap.
226///
228
229///
230/// Get the maximum supported width or height, in pixels.
231///
232/// The bitmap creation functions return NULL if either dimension exceeds this limit.
233///
235
236///
237/// Lock the pixel buffer for reading/writing.
238///
239/// @return Returns a pointer to the pixel buffer.
240///
242
243///
244/// Unlock pixels after locking.
245///
247
248///
249/// Get the raw pixel buffer.
250///
251/// @note You should only call this if the pixels are already locked.
252///
254
255///
256/// Whether or not this bitmap is empty.
257///
259
260///
261/// Reset bitmap pixels to 0.
262///
264
265///
266/// Assign another bitmap to this one.
267///
268/// @param bitmap The destination bitmap.
269///
270/// @param source The source bitmap to copy from.
271///
272/// @note The source pixels are deep-copied in every case but one: a source that wraps memory you
273/// still own (created with `should_copy = false`) is aliased instead, so that buffer must
274/// outlive the destination bitmap.
275///
277
278///
279/// Draw another bitmap to this bitmap.
280///
281/// @note Formats do not need to match. Bitmap formats will be converted to one another
282/// automatically.
283///
284/// @param bitmap The destination bitmap.
285///
286/// @param src_rect The source rectangle, relative to the source bitmap.
287///
288/// @param dest_rect The destination rectangle, relative to this bitmap.
289///
290/// @param src The source bitmap to draw.
291///
292/// @param pad_repeat Whether or not to pad the drawn bitmap by one pixel of repeated edge pixels.
293///
294/// @return Returns whether or not the operation succeeded (this can fail if the src_rect and/or
295/// dest_rect are invalid).
296///
297ULExport bool ulBitmapDrawBitmap(ULBitmap bitmap, ULIntRect src_rect, ULIntRect dest_rect,
298 ULBitmap src, bool pad_repeat);
299
300///
301/// Write bitmap to a PNG on disk.
302///
303/// @param bitmap The bitmap to write.
304///
305/// @param path The file path to write to.
306///
307/// @return Returns whether or not the operation succeeded.
308///
309/// @note This automatically converts BGRA to RGBA and premultiplied alpha to straight alpha.
310/// Use ulBitmapWritePNGEx() if you need to control these conversions.
311///
312ULExport bool ulBitmapWritePNG(ULBitmap bitmap, const char* path);
313
314///
315/// Write bitmap to a PNG on disk with explicit conversion control.
316///
317/// @param bitmap The bitmap to write.
318///
319/// @param path The file path to write to.
320///
321/// @param convert_to_rgba The PNG format expects RGBA format but the bitmap is stored as BGRA,
322/// set this to true to perform the conversion automatically.
323///
324/// @param convert_to_straight_alpha The PNG format expects semi-transparent values to be stored
325/// as straight alpha instead of premultiplied alpha, set this
326/// to true to perform the conversion automatically.
327///
328/// @return Returns whether or not the operation succeeded.
329///
330ULExport bool ulBitmapWritePNGEx(ULBitmap bitmap, const char* path, bool convert_to_rgba,
331 bool convert_to_straight_alpha);
332
333///
334/// Encode this bitmap as a PNG image and return the encoded bytes in a buffer.
335///
336/// @param bitmap The bitmap to encode.
337///
338/// @return Returns a buffer containing the PNG-encoded bytes, or NULL on failure. You must call
339/// ulDestroyBuffer() on the returned buffer when finished.
340///
341/// @note This automatically converts BGRA to RGBA and premultiplied alpha to straight alpha.
342/// Use ulBitmapEncodePNGEx() if you need to control these conversions.
343///
345
346///
347/// Encode this bitmap as a PNG image with explicit conversion control and return the encoded
348/// bytes in a buffer.
349///
350/// @param bitmap The bitmap to encode.
351///
352/// @param convert_to_rgba The PNG format expects RGBA format but the bitmap is stored as BGRA,
353/// set this to true to perform the conversion automatically.
354///
355/// @param convert_to_straight_alpha The PNG format expects semi-transparent values to be stored
356/// as straight alpha instead of premultiplied alpha, set this
357/// to true to perform the conversion automatically.
358///
359/// @return Returns a buffer containing the PNG-encoded bytes, or NULL on failure. You must call
360/// ulDestroyBuffer() on the returned buffer when finished.
361///
362ULExport ULBuffer ulBitmapEncodePNGEx(ULBitmap bitmap, bool convert_to_rgba,
363 bool convert_to_straight_alpha);
364
365///
366/// Make a resized copy of this bitmap by writing to a pre-allocated destination bitmap.
367///
368/// @param bitmap The source bitmap.
369///
370/// @param destination The destination bitmap, its width and height determine the output size.
371///
372/// @param high_quality Whether or not a high quality resampling will be used during the resize.
373/// (Otherwise, just uses fast nearest-neighbor sampling)
374///
375/// @return Returns whether or not the operation succeeded. This operation is only valid if both
376/// formats are kBitmapFormat_BGRA8_UNORM_SRGB and the source and destination are
377/// non-empty.
378///
379ULExport bool ulBitmapResample(ULBitmap bitmap, ULBitmap destination, bool high_quality);
380
381///
382/// Convert a BGRA bitmap from premultiplied alpha to straight alpha.
383///
384/// @note Only valid if the bitmap format is kBitmapFormat_BGRA8_UNORM_SRGB.
385///
387
388///
389/// Convert a BGRA bitmap from straight alpha to premultiplied alpha.
390///
391/// @note Only valid if the bitmap format is kBitmapFormat_BGRA8_UNORM_SRGB.
392///
394
395///
396/// This converts a BGRA bitmap to RGBA bitmap and vice-versa by swapping the red and blue channels.
397///
399
400#ifdef __cplusplus
401} // extern "C"
402#endif
403
404#endif // ULTRALIGHT_CAPI_BITMAP_H
void * ulBitmapRawPixels(ULBitmap bitmap)
Get the raw pixel buffer.
bool ulBitmapOwnsPixels(ULBitmap bitmap)
Whether or not this bitmap owns its own pixel buffer.
bool ulBitmapWritePNG(ULBitmap bitmap, const char *path)
Write bitmap to a PNG on disk.
ULBitmap ulCreateBitmap(unsigned int width, unsigned int height, ULBitmapFormat format)
Create bitmap with certain dimensions and pixel format.
bool ulBitmapIsEmpty(ULBitmap bitmap)
Whether or not this bitmap is empty.
ULBitmap ulCreateEmptyBitmap(void)
Create an empty bitmap.
void ulDestroyBitmap(ULBitmap bitmap)
Destroy a bitmap.
bool ulBitmapIsCompressed(ULBitmap bitmap)
Whether or not this bitmap uses a block-compressed (BCn) format.
void ulBitmapUnlockPixels(ULBitmap bitmap)
Unlock pixels after locking.
unsigned int ulBitmapGetRowBytes(ULBitmap bitmap)
Get the number of bytes per row.
bool ulBitmapDrawBitmap(ULBitmap bitmap, ULIntRect src_rect, ULIntRect dest_rect, ULBitmap src, bool pad_repeat)
Draw another bitmap to this bitmap.
ULBitmapFormat ulBitmapGetFormat(ULBitmap bitmap)
Get the pixel format.
ULBitmapFormatInfo ulGetBitmapFormatInfo(ULBitmapFormat format)
Get the block geometry for a given pixel format.
unsigned int ulBitmapGetHeight(ULBitmap bitmap)
Get the height in pixels.
void ulBitmapConvertToStraightAlpha(ULBitmap bitmap)
Convert a BGRA bitmap from premultiplied alpha to straight alpha.
bool ulBitmapResample(ULBitmap bitmap, ULBitmap destination, bool high_quality)
Make a resized copy of this bitmap by writing to a pre-allocated destination bitmap.
void ulBitmapErase(ULBitmap bitmap)
Reset bitmap pixels to 0.
unsigned int ulBitmapGetWidth(ULBitmap bitmap)
Get the width in pixels.
bool ulBitmapWritePNGEx(ULBitmap bitmap, const char *path, bool convert_to_rgba, bool convert_to_straight_alpha)
Write bitmap to a PNG on disk with explicit conversion control.
void ulBitmapConvertToPremultipliedAlpha(ULBitmap bitmap)
Convert a BGRA bitmap from straight alpha to premultiplied alpha.
void * ulBitmapLockPixels(ULBitmap bitmap)
Lock the pixel buffer for reading/writing.
void ulBitmapSet(ULBitmap bitmap, ULBitmap source)
Assign another bitmap to this one.
ULBitmap ulCreateBitmapFromPixels(unsigned int width, unsigned int height, ULBitmapFormat format, unsigned int row_bytes, const void *pixels, size_t size, bool should_copy)
Create a bitmap from an existing pixel buffer.
size_t ulBitmapGetSize(ULBitmap bitmap)
Get the size in bytes of the underlying pixel buffer.
ULBuffer ulBitmapEncodePNG(ULBitmap bitmap)
Encode this bitmap as a PNG image and return the encoded bytes in a buffer.
unsigned int ulBitmapMaxDimension(void)
Get the maximum supported width or height, in pixels.
ULIntRect ulBitmapGetBounds(ULBitmap bitmap)
Get the bounds as a ULIntRect.
ULBuffer ulBitmapEncodePNGEx(ULBitmap bitmap, bool convert_to_rgba, bool convert_to_straight_alpha)
Encode this bitmap as a PNG image with explicit conversion control and return the encoded bytes in a ...
ULBitmap ulCreateBitmapAligned(unsigned int width, unsigned int height, ULBitmapFormat format, unsigned int alignment)
Create bitmap with certain dimensions, pixel format, and row byte alignment.
unsigned int ulBitmapGetBpp(ULBitmap bitmap)
Get the bytes per pixel.
bool ulIsCompressedFormat(ULBitmapFormat format)
Whether or not a given pixel format is block-compressed (a BCn format).
void ulBitmapSwapRedBlueChannels(ULBitmap bitmap)
This converts a BGRA bitmap to RGBA bitmap and vice-versa by swapping the red and blue channels.
ULBitmap ulCreateBitmapFromCopy(ULBitmap existing_bitmap)
Create a bitmap from a deep copy of another bitmap.
Various defines and utility functions for the C API.
#define ULExport
Definition CAPI_Defines.h:42
struct C_Bitmap * ULBitmap
Opaque handle to a Bitmap object.
Definition CAPI_Defines.h:93
struct C_Buffer * ULBuffer
Opaque handle to a Buffer object.
Definition CAPI_Defines.h:99
ULBitmapFormat
Definition CAPI_Defines.h:164
Block geometry of a pixel format.
Definition CAPI_Bitmap.h:68
unsigned int block_height
Height of one block, in pixels (1 for uncompressed, 4 for BCn).
Definition CAPI_Bitmap.h:70
unsigned int block_width
Width of one block, in pixels (1 for uncompressed, 4 for BCn).
Definition CAPI_Bitmap.h:69
unsigned int block_bytes
Size of one block, in bytes (the bytes per pixel for a 1x1 block).
Definition CAPI_Bitmap.h:71
Integer rectangle defined by left, top, right, and bottom coordinates.
Definition CAPI_Defines.h:548