Coverage Report

Created: 2026-05-16 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/gui/painting/qrgb.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 QRGB_H
6
#define QRGB_H
7
8
#include <QtGui/qtguiglobal.h>
9
#include <QtCore/qprocessordetection.h>
10
11
QT_BEGIN_NAMESPACE
12
13
14
typedef unsigned int QRgb;                        // RGB triplet
15
16
// non-namespaced Qt global variable
17
 inline constexpr QRgb RGB_MASK = 0x00ffffff;     // masks RGB values
18
19
inline constexpr int qRed(QRgb rgb)                     // get red part of RGB
20
0
{ return ((rgb >> 16) & 0xff); }
21
22
inline constexpr int qGreen(QRgb rgb)                   // get green part of RGB
23
0
{ return ((rgb >> 8) & 0xff); }
24
25
inline constexpr int qBlue(QRgb rgb)                    // get blue part of RGB
26
0
{ return (rgb & 0xff); }
27
28
inline constexpr int qAlpha(QRgb rgb)                   // get alpha part of RGBA
29
0
{ return rgb >> 24; }
30
31
inline constexpr QRgb qRgb(int r, int g, int b)         // set RGB value
32
0
{ return (0xffu << 24) | ((r & 0xffu) << 16) | ((g & 0xffu) << 8) | (b & 0xffu); }
33
34
inline constexpr QRgb qRgba(int r, int g, int b, int a) // set RGBA value
35
0
{ return ((a & 0xffu) << 24) | ((r & 0xffu) << 16) | ((g & 0xffu) << 8) | (b & 0xffu); }
36
37
inline constexpr int qGray(int r, int g, int b)         // convert R,G,B to gray 0..255
38
0
{ return (r*11+g*16+b*5)/32; }
39
40
inline constexpr int qGray(QRgb rgb)                    // convert RGB to gray 0..255
41
0
{ return qGray(qRed(rgb), qGreen(rgb), qBlue(rgb)); }
42
43
inline constexpr bool qIsGray(QRgb rgb)
44
0
{ return qRed(rgb) == qGreen(rgb) && qRed(rgb) == qBlue(rgb); }
45
46
inline constexpr QRgb qPremultiply(QRgb x)
47
0
{
48
0
    const uint a = qAlpha(x);
49
0
    uint t = (x & 0xff00ff) * a;
50
0
    t = (t + ((t >> 8) & 0xff00ff) + 0x800080) >> 8;
51
0
    t &= 0xff00ff;
52
53
0
    x = ((x >> 8) & 0xff) * a;
54
0
    x = (x + ((x >> 8) & 0xff) + 0x80);
55
0
    x &= 0xff00;
56
0
    return x | t | (a << 24);
57
0
}
58
59
Q_GUI_EXPORT extern const uint qt_inv_premul_factor[];
60
61
inline QRgb qUnpremultiply(QRgb p)
62
0
{
63
0
    const uint alpha = qAlpha(p);
64
    // Alpha 255 and 0 are the two most common values, which makes them beneficial to short-cut.
65
0
    if (alpha == 255)
66
0
        return p;
67
0
    if (alpha == 0)
68
0
        return 0;
69
    // (p*(0x00ff00ff/alpha)) >> 16 == (p*255)/alpha for all p and alpha <= 256.
70
0
    const uint invAlpha = qt_inv_premul_factor[alpha];
71
    // We add 0x8000 to get even rounding. The rounding also ensures that qPremultiply(qUnpremultiply(p)) == p for all p.
72
0
    return qRgba((qRed(p)*invAlpha + 0x8000)>>16, (qGreen(p)*invAlpha + 0x8000)>>16, (qBlue(p)*invAlpha + 0x8000)>>16, alpha);
73
0
}
74
75
QT_END_NAMESPACE
76
77
#endif // QRGB_H