Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpixelformat.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QPIXELFORMAT_H
5#define QPIXELFORMAT_H
6
7#include <QtGui/qtguiglobal.h>
8
9QT_BEGIN_NAMESPACE
10
11class QPixelFormat
12{
13 // QPixelFormat basically is a glorified quint64, split into several fields.
14 // We could use bit-fields, but GCC at least generates horrible, horrible code for them,
15 // so we do the bit-twiddling ourselves.
16 enum FieldWidth {
17 ModelFieldWidth = 4,
18 FirstFieldWidth = 6,
19 SecondFieldWidth = FirstFieldWidth,
20 ThirdFieldWidth = FirstFieldWidth,
21 FourthFieldWidth = FirstFieldWidth,
22 FifthFieldWidth = FirstFieldWidth,
23 AlphaFieldWidth = FirstFieldWidth,
24 AlphaUsageFieldWidth = 1,
25 AlphaPositionFieldWidth = 1,
26 PremulFieldWidth = 1,
27 TypeInterpretationFieldWidth = 4,
28 ByteOrderFieldWidth = 2,
29 SubEnumFieldWidth = 6,
30 UnusedFieldWidth = 9,
31
32 TotalFieldWidthByWidths = ModelFieldWidth + FirstFieldWidth + SecondFieldWidth + ThirdFieldWidth +
33 FourthFieldWidth + FifthFieldWidth + AlphaFieldWidth + AlphaUsageFieldWidth +
34 AlphaPositionFieldWidth + PremulFieldWidth + TypeInterpretationFieldWidth +
35 ByteOrderFieldWidth + SubEnumFieldWidth + UnusedFieldWidth
36 };
37
38 enum Field {
39 ModelField = 0,
40 // work around bug in old clang versions: when building webkit
41 // with XCode 4.6 and older this fails compilation, thus cast to int
42 FirstField = ModelField + int(ModelFieldWidth),
43 SecondField = FirstField + int(FirstFieldWidth),
44 ThirdField = SecondField + int(SecondFieldWidth),
45 FourthField = ThirdField + int(ThirdFieldWidth),
46 FifthField = FourthField + int(FourthFieldWidth),
47 AlphaField = FifthField + int(FifthFieldWidth),
48 AlphaUsageField = AlphaField + int(AlphaFieldWidth),
49 AlphaPositionField = AlphaUsageField + int(AlphaUsageFieldWidth),
50 PremulField = AlphaPositionField + int(AlphaPositionFieldWidth),
51 TypeInterpretationField = PremulField + int(PremulFieldWidth),
52 ByteOrderField = TypeInterpretationField + int(TypeInterpretationFieldWidth),
53 SubEnumField = ByteOrderField + int(ByteOrderFieldWidth),
54 UnusedField = SubEnumField + int(SubEnumFieldWidth),
55
56 TotalFieldWidthByOffsets = UnusedField + int(UnusedFieldWidth)
57 };
58
59 static_assert(uint(TotalFieldWidthByWidths) == uint(TotalFieldWidthByOffsets));
60 static_assert(uint(TotalFieldWidthByWidths) == 8 * sizeof(quint64));
61
62 constexpr inline uchar get(Field offset, FieldWidth width) const noexcept
63 { return uchar((data >> uint(offset)) & ((Q_UINT64_C(1) << uint(width)) - Q_UINT64_C(1))); }
64 constexpr static inline quint64 set(Field offset, FieldWidth width, uchar value)
65 { return (quint64(value) & ((Q_UINT64_C(1) << uint(width)) - Q_UINT64_C(1))) << uint(offset); }
66
67public:
68 enum ColorModel {
69 RGB,
70 BGR,
71 Indexed,
72 Grayscale,
73 CMYK,
74 HSL,
75 HSV,
76 YUV,
77 Alpha
78 };
79
80 enum AlphaUsage {
81 UsesAlpha,
82 IgnoresAlpha
83 };
84
85 enum AlphaPosition {
86 AtBeginning,
87 AtEnd
88 };
89
90 enum AlphaPremultiplied {
91 NotPremultiplied,
92 Premultiplied
93 };
94
95 enum TypeInterpretation {
96 UnsignedInteger,
97 UnsignedShort,
98 UnsignedByte,
99 FloatingPoint
100 };
101
102 enum YUVLayout {
103 YUV444,
104 YUV422,
105 YUV411,
106 YUV420P,
107 YUV420SP,
108 YV12,
109 UYVY,
110 YUYV,
111 NV12,
112 NV21,
113 IMC1,
114 IMC2,
115 IMC3,
116 IMC4,
117 Y8,
118 Y16
119 };
120
121 enum ByteOrder {
122 LittleEndian,
123 BigEndian,
124 CurrentSystemEndian
125 };
126
127 constexpr inline QPixelFormat() noexcept : data(0) {}
128 constexpr inline QPixelFormat(ColorModel colorModel,
129 uchar firstSize,
130 uchar secondSize,
131 uchar thirdSize,
132 uchar fourthSize,
133 uchar fifthSize,
134 uchar alphaSize,
135 AlphaUsage alphaUsage,
136 AlphaPosition alphaPosition,
137 AlphaPremultiplied premultiplied,
138 TypeInterpretation typeInterpretation,
139 ByteOrder byteOrder = CurrentSystemEndian,
140 uchar subEnum = 0) noexcept;
141
142 constexpr inline ColorModel colorModel() const noexcept { return ColorModel(get(ModelField, ModelFieldWidth)); }
143 constexpr inline uchar channelCount() const noexcept { return (get(FirstField, FirstFieldWidth) > 0) +
144 (get(SecondField, SecondFieldWidth) > 0) +
145 (get(ThirdField, ThirdFieldWidth) > 0) +
146 (get(FourthField, FourthFieldWidth) > 0) +
147 (get(FifthField, FifthFieldWidth) > 0) +
148 (get(AlphaField, AlphaFieldWidth) > 0); }
149
150 constexpr inline uchar redSize() const noexcept { return get(FirstField, FirstFieldWidth); }
151 constexpr inline uchar greenSize() const noexcept { return get(SecondField, SecondFieldWidth); }
152 constexpr inline uchar blueSize() const noexcept { return get(ThirdField, ThirdFieldWidth); }
153
154 constexpr inline uchar cyanSize() const noexcept { return get(FirstField, FirstFieldWidth); }
155 constexpr inline uchar magentaSize() const noexcept { return get(SecondField, SecondFieldWidth); }
156 constexpr inline uchar yellowSize() const noexcept { return get(ThirdField, ThirdFieldWidth); }
157 constexpr inline uchar blackSize() const noexcept { return get(FourthField, FourthFieldWidth); }
158
159 constexpr inline uchar hueSize() const noexcept { return get(FirstField, FirstFieldWidth); }
160 constexpr inline uchar saturationSize() const noexcept { return get(SecondField, SecondFieldWidth); }
161 constexpr inline uchar lightnessSize() const noexcept { return get(ThirdField, ThirdFieldWidth); }
162 constexpr inline uchar brightnessSize() const noexcept { return get(ThirdField, ThirdFieldWidth); }
163
164 constexpr inline uchar alphaSize() const noexcept { return get(AlphaField, AlphaFieldWidth); }
165
166 constexpr inline uchar bitsPerPixel() const noexcept { return get(FirstField, FirstFieldWidth) +
167 get(SecondField, SecondFieldWidth) +
168 get(ThirdField, ThirdFieldWidth) +
169 get(FourthField, FourthFieldWidth) +
170 get(FifthField, FifthFieldWidth) +
171 get(AlphaField, AlphaFieldWidth); }
172
173 constexpr inline AlphaUsage alphaUsage() const noexcept { return AlphaUsage(get(AlphaUsageField, AlphaUsageFieldWidth)); }
174 constexpr inline AlphaPosition alphaPosition() const noexcept { return AlphaPosition(get(AlphaPositionField, AlphaPositionFieldWidth)); }
175 constexpr inline AlphaPremultiplied premultiplied() const noexcept { return AlphaPremultiplied(get(PremulField, PremulFieldWidth)); }
176 constexpr inline TypeInterpretation typeInterpretation() const noexcept { return TypeInterpretation(get(TypeInterpretationField, TypeInterpretationFieldWidth)); }
177 constexpr inline ByteOrder byteOrder() const noexcept { return ByteOrder(get(ByteOrderField, ByteOrderFieldWidth)); }
178
179 constexpr inline YUVLayout yuvLayout() const noexcept { return YUVLayout(get(SubEnumField, SubEnumFieldWidth)); }
180 constexpr inline uchar subEnum() const noexcept { return get(SubEnumField, SubEnumFieldWidth); }
181
182private:
183 constexpr static inline ByteOrder resolveByteOrder(ByteOrder bo)
184 { return bo == CurrentSystemEndian ? Q_BYTE_ORDER == Q_LITTLE_ENDIAN ? LittleEndian : BigEndian : bo ; }
185
186private:
187 quint64 data;
188
189 friend Q_DECL_CONST_FUNCTION constexpr inline bool operator==(QPixelFormat fmt1, QPixelFormat fmt2)
190 { return fmt1.data == fmt2.data; }
191
192 friend Q_DECL_CONST_FUNCTION constexpr inline bool operator!=(QPixelFormat fmt1, QPixelFormat fmt2)
193 { return !(fmt1 == fmt2); }
194};
195static_assert(sizeof(QPixelFormat) == sizeof(quint64));
197
198
199namespace QtPrivate {
200 QPixelFormat Q_GUI_EXPORT QPixelFormat_createYUV(QPixelFormat::YUVLayout yuvLayout,
207}
208
209constexpr QPixelFormat::QPixelFormat(ColorModel mdl,
210 uchar firstSize,
211 uchar secondSize,
212 uchar thirdSize,
213 uchar fourthSize,
214 uchar fifthSize,
215 uchar alfa,
216 AlphaUsage usage,
217 AlphaPosition position,
218 AlphaPremultiplied premult,
219 TypeInterpretation typeInterp,
220 ByteOrder b_order,
221 uchar s_enum) noexcept
222 : data(set(ModelField, ModelFieldWidth, uchar(mdl)) |
223 set(FirstField, FirstFieldWidth, firstSize) |
224 set(SecondField, SecondFieldWidth, secondSize) |
225 set(ThirdField, ThirdFieldWidth, thirdSize) |
226 set(FourthField, FourthFieldWidth, fourthSize) |
227 set(FifthField, FifthFieldWidth, fifthSize) |
228 set(AlphaField, AlphaFieldWidth, alfa) |
229 set(AlphaUsageField, AlphaUsageFieldWidth, uchar(usage)) |
230 set(AlphaPositionField, AlphaPositionFieldWidth, uchar(position)) |
231 set(PremulField, PremulFieldWidth, uchar(premult)) |
232 set(TypeInterpretationField, TypeInterpretationFieldWidth, uchar(typeInterp)) |
233 set(ByteOrderField, ByteOrderFieldWidth, uchar(resolveByteOrder(b_order))) |
234 set(SubEnumField, SubEnumFieldWidth, s_enum) |
235 set(UnusedField, UnusedFieldWidth, 0))
236{
237}
238
239constexpr inline QPixelFormat qPixelFormatRgba(uchar red,
240 uchar green,
241 uchar blue,
242 uchar alfa,
243 QPixelFormat::AlphaUsage usage,
244 QPixelFormat::AlphaPosition position,
245 QPixelFormat::AlphaPremultiplied pmul=QPixelFormat::NotPremultiplied,
246 QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedInteger) noexcept
247{
248 return QPixelFormat(QPixelFormat::RGB,
249 red,
250 green,
251 blue,
252 0,
253 0,
254 alfa,
255 usage,
256 position,
257 pmul,
258 typeInt);
259}
260
261constexpr inline QPixelFormat qPixelFormatGrayscale(uchar channelSize,
262 QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedInteger) noexcept
263{
264 return QPixelFormat(QPixelFormat::Grayscale,
265 channelSize,
266 0,
267 0,
268 0,
269 0,
270 0,
271 QPixelFormat::IgnoresAlpha,
272 QPixelFormat::AtBeginning,
273 QPixelFormat::NotPremultiplied,
274 typeInt);
275}
276
277constexpr inline QPixelFormat qPixelFormatAlpha(uchar channelSize,
278 QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedInteger) noexcept
279{
280 return QPixelFormat(QPixelFormat::Alpha,
281 0,
282 0,
283 0,
284 0,
285 0,
286 channelSize,
287 QPixelFormat::UsesAlpha,
288 QPixelFormat::AtBeginning,
289 QPixelFormat::NotPremultiplied,
290 typeInt);
291}
292
293constexpr inline QPixelFormat qPixelFormatCmyk(uchar channelSize,
294 uchar alfa=0,
295 QPixelFormat::AlphaUsage usage=QPixelFormat::IgnoresAlpha,
296 QPixelFormat::AlphaPosition position=QPixelFormat::AtBeginning,
297 QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedInteger) noexcept
298{
299 return QPixelFormat(QPixelFormat::CMYK,
300 channelSize,
301 channelSize,
302 channelSize,
303 channelSize,
304 0,
305 alfa,
306 usage,
307 position,
308 QPixelFormat::NotPremultiplied,
309 typeInt);
310}
311
312constexpr inline QPixelFormat qPixelFormatHsl(uchar channelSize,
313 uchar alfa=0,
314 QPixelFormat::AlphaUsage usage=QPixelFormat::IgnoresAlpha,
315 QPixelFormat::AlphaPosition position=QPixelFormat::AtBeginning,
316 QPixelFormat::TypeInterpretation typeInt=QPixelFormat::FloatingPoint) noexcept
317{
318 return QPixelFormat(QPixelFormat::HSL,
319 channelSize,
320 channelSize,
321 channelSize,
322 0,
323 0,
324 alfa,
325 usage,
326 position,
327 QPixelFormat::NotPremultiplied,
328 typeInt);
329}
330
331constexpr inline QPixelFormat qPixelFormatHsv(uchar channelSize,
332 uchar alfa=0,
333 QPixelFormat::AlphaUsage usage=QPixelFormat::IgnoresAlpha,
334 QPixelFormat::AlphaPosition position=QPixelFormat::AtBeginning,
335 QPixelFormat::TypeInterpretation typeInt=QPixelFormat::FloatingPoint) noexcept
336{
337 return QPixelFormat(QPixelFormat::HSV,
338 channelSize,
339 channelSize,
340 channelSize,
341 0,
342 0,
343 alfa,
344 usage,
345 position,
346 QPixelFormat::NotPremultiplied,
347 typeInt);
348}
349
350inline QPixelFormat qPixelFormatYuv(QPixelFormat::YUVLayout layout,
351 uchar alfa=0,
352 QPixelFormat::AlphaUsage usage=QPixelFormat::IgnoresAlpha,
353 QPixelFormat::AlphaPosition position=QPixelFormat::AtBeginning,
354 QPixelFormat::AlphaPremultiplied p_mul=QPixelFormat::NotPremultiplied,
355 QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedByte,
356 QPixelFormat::ByteOrder b_order=QPixelFormat::LittleEndian)
357{
358 return QtPrivate::QPixelFormat_createYUV(layout,
359 alfa,
360 usage,
361 position,
362 p_mul,
363 typeInt,
364 b_order);
365}
366
367QT_END_NAMESPACE
368
369#endif //QPIXELFORMAT_H
Combined button and popup list for selecting options.
\macro QT_NO_KEYWORDS >
Definition qcompare.h:24
QPixelFormat QPixelFormat_createYUV(QPixelFormat::YUVLayout yuvLayout, uchar alphaSize, QPixelFormat::AlphaUsage alphaUsage, QPixelFormat::AlphaPosition alphaPosition, QPixelFormat::AlphaPremultiplied premultiplied, QPixelFormat::TypeInterpretation typeInterpretation, QPixelFormat::ByteOrder byteOrder)
constexpr QPixelFormat qPixelFormatHsv(uchar channelSize, uchar alfa=0, QPixelFormat::AlphaUsage usage=QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition position=QPixelFormat::AtBeginning, QPixelFormat::TypeInterpretation typeInt=QPixelFormat::FloatingPoint) noexcept
constexpr QPixelFormat qPixelFormatCmyk(uchar channelSize, uchar alfa=0, QPixelFormat::AlphaUsage usage=QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition position=QPixelFormat::AtBeginning, QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedInteger) noexcept
Q_DECLARE_TYPEINFO(QPixelFormat, Q_PRIMITIVE_TYPE)
constexpr QPixelFormat qPixelFormatRgba(uchar red, uchar green, uchar blue, uchar alfa, QPixelFormat::AlphaUsage usage, QPixelFormat::AlphaPosition position, QPixelFormat::AlphaPremultiplied pmul=QPixelFormat::NotPremultiplied, QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedInteger) noexcept
constexpr QPixelFormat qPixelFormatHsl(uchar channelSize, uchar alfa=0, QPixelFormat::AlphaUsage usage=QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition position=QPixelFormat::AtBeginning, QPixelFormat::TypeInterpretation typeInt=QPixelFormat::FloatingPoint) noexcept
constexpr QPixelFormat qPixelFormatGrayscale(uchar channelSize, QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedInteger) noexcept
constexpr QPixelFormat qPixelFormatAlpha(uchar channelSize, QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedInteger) noexcept
QPixelFormat qPixelFormatYuv(QPixelFormat::YUVLayout layout, uchar alfa=0, QPixelFormat::AlphaUsage usage=QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition position=QPixelFormat::AtBeginning, QPixelFormat::AlphaPremultiplied p_mul=QPixelFormat::NotPremultiplied, QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedByte, QPixelFormat::ByteOrder b_order=QPixelFormat::LittleEndian)