Coverage Report

Created: 2026-03-31 07:41

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