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