1// [Blend2D]
2// 2D Vector Graphics Powered by a JIT Compiler.
3//
4// [License]
5// Zlib - See LICENSE.md file in the package.
6
7#ifndef BLEND2D_BLFORMAT_H
8#define BLEND2D_BLFORMAT_H
9
10#include "./blapi.h"
11
12//! \addtogroup blend2d_api_imaging
13//! \{
14
15// ============================================================================
16// [Constants]
17// ============================================================================
18
19//! Pixel format.
20//!
21//! Compatibility Table
22//! -------------------
23//!
24//! ```
25//! +---------------------+---------------------+-----------------------------+
26//! | Blend2D Format | Cairo Format | QImage::Format |
27//! +---------------------+---------------------+-----------------------------+
28//! | BL_FORMAT_PRGB32 | CAIRO_FORMAT_ARGB32 | Format_ARGB32_Premultiplied |
29//! | BL_FORMAT_XRGB32 | CAIRO_FORMAT_RGB24 | Format_RGB32 |
30//! | BL_FORMAT_A8 | CAIRO_FORMAT_A8 | n/a |
31//! +---------------------+---------------------+-----------------------------+
32//! ```
33BL_DEFINE_ENUM(BLFormat) {
34 //! None or invalid pixel format.
35 BL_FORMAT_NONE = 0,
36 //! 32-bit premultiplied ARGB pixel format (8-bit components).
37 BL_FORMAT_PRGB32 = 1,
38 //! 32-bit (X)RGB pixel format (8-bit components, alpha ignored).
39 BL_FORMAT_XRGB32 = 2,
40 //! 8-bit alpha-only pixel format.
41 BL_FORMAT_A8 = 3,
42
43 //! Count of pixel formats.
44 BL_FORMAT_COUNT = 4,
45 //! Count of pixel formats (reserved for future use).
46 BL_FORMAT_RESERVED_COUNT = 16
47};
48
49//! Pixel format flags.
50BL_DEFINE_ENUM(BLFormatFlags) {
51 //! Pixel format provides RGB components.
52 BL_FORMAT_FLAG_RGB = 0x00000001u,
53 //! Pixel format provides only alpha component.
54 BL_FORMAT_FLAG_ALPHA = 0x00000002u,
55 //! A combination of `BL_FORMAT_FLAG_RGB | BL_FORMAT_FLAG_ALPHA`.
56 BL_FORMAT_FLAG_RGBA = 0x00000003u,
57 //! Pixel format provides LUM component (and not RGB components).
58 BL_FORMAT_FLAG_LUM = 0x00000004u,
59 //! A combination of `BL_FORMAT_FLAG_LUM | BL_FORMAT_FLAG_ALPHA`.
60 BL_FORMAT_FLAG_LUMA = 0x00000006u,
61 //! Indexed pixel format the requres a palette (I/O only).
62 BL_FORMAT_FLAG_INDEXED = 0x00000010u,
63 //! RGB components are premultiplied by alpha component.
64 BL_FORMAT_FLAG_PREMULTIPLIED = 0x00000100u,
65 //! Pixel format doesn't use native byte-order (I/O only).
66 BL_FORMAT_FLAG_BYTE_SWAP = 0x00000200u,
67
68 // The following flags are only informative. They are part of `blFormatInfo[]`,
69 // but doesn't have to be passed to `BLPixelConverter` as they can be easily
70 // calculated.
71
72 //! Pixel components are byte aligned (all 8bpp).
73 BL_FORMAT_FLAG_BYTE_ALIGNED = 0x00010000u
74};
75
76// ============================================================================
77// [BLFormatInfo]
78// ============================================================================
79
80//! Provides a detailed information about a pixel format. Use `blFormatInfo`
81//! array to get an information of Blend2D native pixel formats.
82struct BLFormatInfo {
83 uint32_t depth;
84 uint32_t flags;
85
86 union {
87 struct {
88 uint8_t sizes[4];
89 uint8_t shifts[4];
90 };
91
92 struct {
93 uint8_t rSize;
94 uint8_t gSize;
95 uint8_t bSize;
96 uint8_t aSize;
97
98 uint8_t rShift;
99 uint8_t gShift;
100 uint8_t bShift;
101 uint8_t aShift;
102 };
103
104 const BLRgba32* palette;
105 };
106
107 // --------------------------------------------------------------------------
108 #ifdef __cplusplus
109
110 BL_INLINE void reset() noexcept { memset(this, 0, sizeof(*this)); }
111
112 BL_INLINE void setSizes(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 0) noexcept {
113 rSize = r;
114 gSize = g;
115 bSize = b;
116 aSize = a;
117 }
118
119 BL_INLINE void setShifts(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 0) noexcept {
120 rShift = r;
121 gShift = g;
122 bShift = b;
123 aShift = a;
124 }
125
126 BL_INLINE BLResult sanitize() noexcept { return blFormatInfoSanitize(this); }
127
128 BL_INLINE bool operator==(const BLFormatInfo& other) const noexcept { return memcmp(this, &other, sizeof(*this)) == 0; }
129 BL_INLINE bool operator!=(const BLFormatInfo& other) const noexcept { return memcmp(this, &other, sizeof(*this)) != 0; }
130
131 #endif
132 // --------------------------------------------------------------------------
133};
134
135#ifdef __cplusplus
136extern "C" {
137#endif
138
139//! Pixel format information of Blend2D native pixel formats, see `BLFormat`.
140extern BL_API const BLFormatInfo blFormatInfo[BL_FORMAT_RESERVED_COUNT];
141
142#ifdef __cplusplus
143} // {Extern:C}
144#endif
145
146//! \}
147
148#endif // BLEND2D_BLFORMAT_H
149