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_P_H
8#define BLEND2D_BLFORMAT_P_H
9
10#include "./blformat.h"
11#include "./blsupport_p.h"
12#include "./bltables_p.h"
13
14//! \cond INTERNAL
15//! \addtogroup blend2d_internal
16//! \{
17
18// ============================================================================
19// [Constants]
20// ============================================================================
21
22//! Pixel formats used internally and never exposed to users.
23enum BLFormatInternal : uint32_t {
24 //! Internal pixel format that is the same as XRGB32, but the unused component
25 //! is guaranteed to always be 0xFF so the format can be treated as PRGB32 by
26 //! compositors.
27 BL_FORMAT_FRGB32 = BL_FORMAT_COUNT + 0,
28 BL_FORMAT_ZERO32 = BL_FORMAT_COUNT + 1,
29
30 //! Count of internal pixel formats.
31 BL_FORMAT_INTERNAL_COUNT = BL_FORMAT_COUNT + 2
32};
33
34//! Pixel format flags used internally.
35enum BLFormatFlagsInternal : uint32_t {
36 BL_FORMAT_FLAG_FULL_ALPHA = 0x1000000u,
37 BL_FORMAT_FLAG_ZERO_ALPHA = 0x2000000u,
38
39 BL_FORMAT_ALL_FLAGS =
40 BL_FORMAT_FLAG_RGB |
41 BL_FORMAT_FLAG_ALPHA |
42 BL_FORMAT_FLAG_RGBA |
43 BL_FORMAT_FLAG_LUM |
44 BL_FORMAT_FLAG_LUMA |
45 BL_FORMAT_FLAG_INDEXED |
46 BL_FORMAT_FLAG_PREMULTIPLIED |
47 BL_FORMAT_FLAG_BYTE_SWAP ,
48
49 BL_FORMAT_COMPONENT_FLAGS =
50 BL_FORMAT_FLAG_LUM |
51 BL_FORMAT_FLAG_RGB |
52 BL_FORMAT_FLAG_ALPHA
53};
54
55static_assert(int(BL_FORMAT_INTERNAL_COUNT) <= int(BL_FORMAT_RESERVED_COUNT),
56 "Internal format count cannot overflow reserved format count");
57
58static_assert(BL_FORMAT_COMPONENT_FLAGS == 0x7u,
59 "Component flags of BLFormat must be at LSB");
60
61// ============================================================================
62// [BLFormat - Flags]
63// ============================================================================
64
65static constexpr uint32_t blFormatFlagsStatic(uint32_t format) noexcept {
66 return format == BL_FORMAT_PRGB32 ? BL_FORMAT_FLAG_RGBA |
67 BL_FORMAT_FLAG_PREMULTIPLIED |
68 BL_FORMAT_FLAG_BYTE_ALIGNED :
69 format == BL_FORMAT_XRGB32 ? BL_FORMAT_FLAG_RGB |
70 BL_FORMAT_FLAG_BYTE_ALIGNED :
71 format == BL_FORMAT_A8 ? BL_FORMAT_FLAG_ALPHA |
72 BL_FORMAT_FLAG_BYTE_ALIGNED :
73 format == BL_FORMAT_FRGB32 ? BL_FORMAT_FLAG_RGB |
74 BL_FORMAT_FLAG_FULL_ALPHA |
75 BL_FORMAT_FLAG_BYTE_ALIGNED :
76 format == BL_FORMAT_ZERO32 ? BL_FORMAT_FLAG_RGBA |
77 BL_FORMAT_FLAG_ZERO_ALPHA |
78 BL_FORMAT_FLAG_BYTE_ALIGNED : 0;
79}
80
81// ============================================================================
82// [BLFormat - Utilities]
83// ============================================================================
84
85//! Converts absolute masks like `0x3F0` to mask sizes and shift shifts as used
86//! by `BLFormatInfo`. Only useful for pixel formats with absolute masks up to
87//! 64 bits (uint64_t input). Commonly used to convert pixel formats that use
88//! 32 or less bits.
89template<typename T>
90static void blFormatInfoAssignAbsoluteMasks(BLFormatInfo& info, const T* masks, size_t n = 4) noexcept {
91 typedef typename std::make_unsigned<T>::type U;
92
93 memset(info.sizes, 0, sizeof(info.sizes));
94 memset(info.shifts, 0, sizeof(info.shifts));
95
96 for (size_t i = 0; i < n; i++) {
97 U m = U(masks[i]);
98 if (!m)
99 continue;
100
101 uint32_t shift = blBitCtz(m);
102 m >>= shift;
103 uint32_t size = m >= 0xFFFFFFFF ? 32 : blBitCtz(~uint32_t(m));
104
105 info.sizes[i] = uint8_t(size);
106 info.shifts[i] = uint8_t(shift);
107 }
108}
109
110//! \}
111//! \endcond
112
113#endif // BLEND2D_BLFORMAT_P_H
114