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