Coverage Report

Created: 2026-02-10 07:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/gui/kernel/qpixelformat.h
Line
Count
Source
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
10
QT_BEGIN_NAMESPACE
11
12
class 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
0
    { 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
0
    { return (quint64(value) & ((Q_UINT64_C(1) << uint(width)) - Q_UINT64_C(1))) << uint(offset); }
69
70
public:
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
0
    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
0
    constexpr inline ColorModel colorModel() const  noexcept { return ColorModel(get(ModelField, ModelFieldWidth)); }
153
0
    constexpr inline uchar channelCount() const noexcept { return (get(FirstField, FirstFieldWidth) > 0) +
154
0
                                                                                 (get(SecondField, SecondFieldWidth) > 0) +
155
0
                                                                                 (get(ThirdField, ThirdFieldWidth) > 0) +
156
0
                                                                                 (get(FourthField, FourthFieldWidth) > 0) +
157
0
                                                                                 (get(FifthField, FifthFieldWidth) > 0) +
158
0
                                                                                 (get(AlphaField, AlphaFieldWidth) > 0); }
159
160
0
    constexpr inline uchar redSize() const noexcept { return get(FirstField, FirstFieldWidth); }
161
0
    constexpr inline uchar greenSize() const noexcept { return get(SecondField, SecondFieldWidth); }
162
0
    constexpr inline uchar blueSize() const noexcept { return get(ThirdField, ThirdFieldWidth); }
163
164
0
    constexpr inline uchar cyanSize() const noexcept { return get(FirstField, FirstFieldWidth); }
165
0
    constexpr inline uchar magentaSize() const noexcept { return get(SecondField, SecondFieldWidth); }
166
0
    constexpr inline uchar yellowSize() const noexcept { return get(ThirdField, ThirdFieldWidth); }
167
0
    constexpr inline uchar blackSize() const noexcept { return get(FourthField, FourthFieldWidth); }
168
169
0
    constexpr inline uchar hueSize() const noexcept { return get(FirstField, FirstFieldWidth); }
170
0
    constexpr inline uchar saturationSize() const noexcept { return get(SecondField, SecondFieldWidth); }
171
0
    constexpr inline uchar lightnessSize() const noexcept { return get(ThirdField, ThirdFieldWidth); }
172
0
    constexpr inline uchar brightnessSize() const noexcept { return get(ThirdField, ThirdFieldWidth); }
173
174
0
    constexpr inline uchar alphaSize() const noexcept { return get(AlphaField, AlphaFieldWidth); }
175
176
0
    constexpr inline uchar bitsPerPixel() const noexcept { return get(FirstField, FirstFieldWidth) +
177
0
                                                                                 get(SecondField, SecondFieldWidth) +
178
0
                                                                                 get(ThirdField, ThirdFieldWidth) +
179
0
                                                                                 get(FourthField, FourthFieldWidth) +
180
0
                                                                                 get(FifthField, FifthFieldWidth) +
181
0
                                                                                 get(AlphaField, AlphaFieldWidth); }
182
183
0
    constexpr inline AlphaUsage alphaUsage() const noexcept { return AlphaUsage(get(AlphaUsageField, AlphaUsageFieldWidth)); }
184
0
    constexpr inline AlphaPosition alphaPosition() const noexcept { return AlphaPosition(get(AlphaPositionField, AlphaPositionFieldWidth)); }
185
0
    constexpr inline AlphaPremultiplied premultiplied() const noexcept { return AlphaPremultiplied(get(PremulField, PremulFieldWidth)); }
186
0
    constexpr inline TypeInterpretation typeInterpretation() const noexcept { return TypeInterpretation(get(TypeInterpretationField, TypeInterpretationFieldWidth)); }
187
0
    constexpr inline ByteOrder byteOrder() const noexcept { return ByteOrder(get(ByteOrderField, ByteOrderFieldWidth)); }
188
189
0
    constexpr inline YUVLayout yuvLayout() const noexcept { return YUVLayout(get(SubEnumField, SubEnumFieldWidth)); }
190
0
    constexpr inline uchar subEnum() const noexcept { return get(SubEnumField, SubEnumFieldWidth); }
191
192
private:
193
0
    constexpr static inline ByteOrder resolveByteOrder(ByteOrder bo) { return resolveByteOrder(bo, UnsignedInteger ); }
194
    constexpr static inline ByteOrder resolveByteOrder(ByteOrder bo, TypeInterpretation ti)
195
0
    { return bo == CurrentSystemEndian ? ti == UnsignedByte || Q_BYTE_ORDER == Q_BIG_ENDIAN ? BigEndian : LittleEndian : bo ; }
196
197
private:
198
    quint64 data;
199
200
    friend Q_DECL_CONST_FUNCTION constexpr inline bool operator==(QPixelFormat fmt1, QPixelFormat fmt2)
201
0
    { return fmt1.data == fmt2.data; }
202
203
    friend Q_DECL_CONST_FUNCTION constexpr inline bool operator!=(QPixelFormat fmt1, QPixelFormat fmt2)
204
0
    { 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
};
210
static_assert(sizeof(QPixelFormat) == sizeof(quint64));
211
Q_DECLARE_TYPEINFO(QPixelFormat, Q_PRIMITIVE_TYPE);
212
213
214
namespace QtPrivate {
215
    QPixelFormat Q_GUI_EXPORT QPixelFormat_createYUV(QPixelFormat::YUVLayout yuvLayout,
216
                                                     uchar alphaSize,
217
                                                     QPixelFormat::AlphaUsage alphaUsage,
218
                                                     QPixelFormat::AlphaPosition alphaPosition,
219
                                                     QPixelFormat::AlphaPremultiplied premultiplied,
220
                                                     QPixelFormat::TypeInterpretation typeInterpretation,
221
                                                     QPixelFormat::ByteOrder byteOrder);
222
}
223
224
constexpr 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
254
constexpr 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
0
{
263
0
    return QPixelFormat(QPixelFormat::RGB,
264
0
                        red,
265
0
                        green,
266
0
                        blue,
267
0
                        0,
268
0
                        0,
269
0
                        alfa,
270
0
                        usage,
271
0
                        position,
272
0
                        pmul,
273
0
                        typeInt);
274
0
}
275
276
constexpr inline QPixelFormat qPixelFormatGrayscale(uchar channelSize,
277
                                                    QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedInteger) noexcept
278
0
{
279
0
    return QPixelFormat(QPixelFormat::Grayscale,
280
0
                        channelSize,
281
0
                        0,
282
0
                        0,
283
0
                        0,
284
0
                        0,
285
0
                        0,
286
0
                        QPixelFormat::IgnoresAlpha,
287
0
                        QPixelFormat::AtBeginning,
288
0
                        QPixelFormat::NotPremultiplied,
289
0
                        typeInt);
290
0
}
291
292
constexpr inline QPixelFormat qPixelFormatAlpha(uchar channelSize,
293
                                                QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedInteger) noexcept
294
0
{
295
0
    return QPixelFormat(QPixelFormat::Alpha,
296
0
                        0,
297
0
                        0,
298
0
                        0,
299
0
                        0,
300
0
                        0,
301
0
                        channelSize,
302
0
                        QPixelFormat::UsesAlpha,
303
0
                        QPixelFormat::AtBeginning,
304
0
                        QPixelFormat::NotPremultiplied,
305
0
                        typeInt);
306
0
}
307
308
constexpr 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
0
{
314
0
    return QPixelFormat(QPixelFormat::CMYK,
315
0
                        channelSize,
316
0
                        channelSize,
317
0
                        channelSize,
318
0
                        channelSize,
319
0
                        0,
320
0
                        alfa,
321
0
                        usage,
322
0
                        position,
323
0
                        QPixelFormat::NotPremultiplied,
324
0
                        typeInt);
325
0
}
326
327
constexpr 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
0
{
333
0
    return QPixelFormat(QPixelFormat::HSL,
334
0
                        channelSize,
335
0
                        channelSize,
336
0
                        channelSize,
337
0
                        0,
338
0
                        0,
339
0
                        alfa,
340
0
                        usage,
341
0
                        position,
342
0
                        QPixelFormat::NotPremultiplied,
343
0
                        typeInt);
344
0
}
345
346
constexpr 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
0
{
352
0
    return QPixelFormat(QPixelFormat::HSV,
353
0
                        channelSize,
354
0
                        channelSize,
355
0
                        channelSize,
356
0
                        0,
357
0
                        0,
358
0
                        alfa,
359
0
                        usage,
360
0
                        position,
361
0
                        QPixelFormat::NotPremultiplied,
362
0
                        typeInt);
363
0
}
364
365
inline 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
0
{
373
0
    return QtPrivate::QPixelFormat_createYUV(layout,
374
0
                                             alfa,
375
0
                                             usage,
376
0
                                             position,
377
0
                                             p_mul,
378
0
                                             typeInt,
379
0
                                             b_order);
380
0
}
381
382
QT_END_NAMESPACE
383
384
#endif //QPIXELFORMAT_H