Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-13 09:23:25

0001 // Copyright (C) 2016 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QPIXELFORMAT_H
0005 #define QPIXELFORMAT_H
0006 
0007 #include <QtGui/qtguiglobal.h>
0008 
0009 #include <QtCore/qobjectdefs.h>
0010 
0011 QT_BEGIN_NAMESPACE
0012 
0013 class QPixelFormat
0014 {
0015     Q_GADGET_EXPORT(Q_GUI_EXPORT)
0016 
0017     // QPixelFormat basically is a glorified quint64, split into several fields.
0018     // We could use bit-fields, but GCC at least generates horrible, horrible code for them,
0019     // so we do the bit-twiddling ourselves.
0020     enum FieldWidth {
0021         ModelFieldWidth = 4,
0022         FirstFieldWidth = 6,
0023         SecondFieldWidth = FirstFieldWidth,
0024         ThirdFieldWidth = FirstFieldWidth,
0025         FourthFieldWidth = FirstFieldWidth,
0026         FifthFieldWidth = FirstFieldWidth,
0027         AlphaFieldWidth = FirstFieldWidth,
0028         AlphaUsageFieldWidth = 1,
0029         AlphaPositionFieldWidth = 1,
0030         PremulFieldWidth = 1,
0031         TypeInterpretationFieldWidth = 4,
0032         ByteOrderFieldWidth = 2,
0033         SubEnumFieldWidth = 6,
0034         UnusedFieldWidth = 9,
0035 
0036         TotalFieldWidthByWidths = ModelFieldWidth + FirstFieldWidth + SecondFieldWidth + ThirdFieldWidth +
0037                                   FourthFieldWidth + FifthFieldWidth + AlphaFieldWidth + AlphaUsageFieldWidth +
0038                                   AlphaPositionFieldWidth + PremulFieldWidth + TypeInterpretationFieldWidth +
0039                                   ByteOrderFieldWidth + SubEnumFieldWidth + UnusedFieldWidth
0040     };
0041 
0042     enum Field {
0043         ModelField = 0,
0044         // work around bug in old clang versions: when building webkit
0045         // with XCode 4.6 and older this fails compilation, thus cast to int
0046         FirstField = ModelField + int(ModelFieldWidth),
0047         SecondField = FirstField + int(FirstFieldWidth),
0048         ThirdField = SecondField + int(SecondFieldWidth),
0049         FourthField = ThirdField + int(ThirdFieldWidth),
0050         FifthField = FourthField + int(FourthFieldWidth),
0051         AlphaField = FifthField + int(FifthFieldWidth),
0052         AlphaUsageField = AlphaField + int(AlphaFieldWidth),
0053         AlphaPositionField = AlphaUsageField + int(AlphaUsageFieldWidth),
0054         PremulField = AlphaPositionField + int(AlphaPositionFieldWidth),
0055         TypeInterpretationField = PremulField + int(PremulFieldWidth),
0056         ByteOrderField = TypeInterpretationField + int(TypeInterpretationFieldWidth),
0057         SubEnumField = ByteOrderField + int(ByteOrderFieldWidth),
0058         UnusedField = SubEnumField + int(SubEnumFieldWidth),
0059 
0060         TotalFieldWidthByOffsets = UnusedField + int(UnusedFieldWidth)
0061     };
0062 
0063     static_assert(uint(TotalFieldWidthByWidths) == uint(TotalFieldWidthByOffsets));
0064     static_assert(uint(TotalFieldWidthByWidths) == 8 * sizeof(quint64));
0065 
0066     constexpr inline uchar get(Field offset, FieldWidth width) const noexcept
0067     { return uchar((data >> uint(offset)) & ((Q_UINT64_C(1) << uint(width)) - Q_UINT64_C(1))); }
0068     constexpr static inline quint64 set(Field offset, FieldWidth width, uchar value)
0069     { return (quint64(value) & ((Q_UINT64_C(1) << uint(width)) - Q_UINT64_C(1))) << uint(offset); }
0070 
0071 public:
0072     enum ColorModel {
0073         RGB,
0074         BGR,
0075         Indexed,
0076         Grayscale,
0077         CMYK,
0078         HSL,
0079         HSV,
0080         YUV,
0081         Alpha
0082     };
0083     Q_ENUM(ColorModel)
0084 
0085     enum AlphaUsage {
0086         UsesAlpha,
0087         IgnoresAlpha
0088     };
0089     Q_ENUM(AlphaUsage)
0090 
0091     enum AlphaPosition {
0092         AtBeginning,
0093         AtEnd
0094     };
0095     Q_ENUM(AlphaPosition)
0096 
0097     enum AlphaPremultiplied {
0098         NotPremultiplied,
0099         Premultiplied
0100     };
0101     Q_ENUM(AlphaPremultiplied)
0102 
0103     enum TypeInterpretation {
0104         UnsignedInteger,
0105         UnsignedShort,
0106         UnsignedByte,
0107         FloatingPoint
0108     };
0109     Q_ENUM(TypeInterpretation)
0110 
0111     enum YUVLayout {
0112         YUV444,
0113         YUV422,
0114         YUV411,
0115         YUV420P,
0116         YUV420SP,
0117         YV12,
0118         UYVY,
0119         YUYV,
0120         NV12,
0121         NV21,
0122         IMC1,
0123         IMC2,
0124         IMC3,
0125         IMC4,
0126         Y8,
0127         Y16
0128     };
0129     Q_ENUM(YUVLayout)
0130 
0131     enum ByteOrder {
0132         LittleEndian,
0133         BigEndian,
0134         CurrentSystemEndian
0135     };
0136     Q_ENUM(ByteOrder)
0137 
0138     constexpr inline QPixelFormat() noexcept : data(0) {}
0139     constexpr inline QPixelFormat(ColorModel colorModel,
0140                                            uchar firstSize,
0141                                            uchar secondSize,
0142                                            uchar thirdSize,
0143                                            uchar fourthSize,
0144                                            uchar fifthSize,
0145                                            uchar alphaSize,
0146                                            AlphaUsage alphaUsage,
0147                                            AlphaPosition alphaPosition,
0148                                            AlphaPremultiplied premultiplied,
0149                                            TypeInterpretation typeInterpretation,
0150                                            ByteOrder byteOrder = CurrentSystemEndian,
0151                                            uchar subEnum = 0) noexcept;
0152 
0153     constexpr inline ColorModel colorModel() const  noexcept { return ColorModel(get(ModelField, ModelFieldWidth)); }
0154     constexpr inline uchar channelCount() const noexcept { return (get(FirstField, FirstFieldWidth) > 0) +
0155                                                                                  (get(SecondField, SecondFieldWidth) > 0) +
0156                                                                                  (get(ThirdField, ThirdFieldWidth) > 0) +
0157                                                                                  (get(FourthField, FourthFieldWidth) > 0) +
0158                                                                                  (get(FifthField, FifthFieldWidth) > 0) +
0159                                                                                  (get(AlphaField, AlphaFieldWidth) > 0); }
0160 
0161     constexpr inline uchar redSize() const noexcept { return get(FirstField, FirstFieldWidth); }
0162     constexpr inline uchar greenSize() const noexcept { return get(SecondField, SecondFieldWidth); }
0163     constexpr inline uchar blueSize() const noexcept { return get(ThirdField, ThirdFieldWidth); }
0164 
0165     constexpr inline uchar cyanSize() const noexcept { return get(FirstField, FirstFieldWidth); }
0166     constexpr inline uchar magentaSize() const noexcept { return get(SecondField, SecondFieldWidth); }
0167     constexpr inline uchar yellowSize() const noexcept { return get(ThirdField, ThirdFieldWidth); }
0168     constexpr inline uchar blackSize() const noexcept { return get(FourthField, FourthFieldWidth); }
0169 
0170     constexpr inline uchar hueSize() const noexcept { return get(FirstField, FirstFieldWidth); }
0171     constexpr inline uchar saturationSize() const noexcept { return get(SecondField, SecondFieldWidth); }
0172     constexpr inline uchar lightnessSize() const noexcept { return get(ThirdField, ThirdFieldWidth); }
0173     constexpr inline uchar brightnessSize() const noexcept { return get(ThirdField, ThirdFieldWidth); }
0174 
0175     constexpr inline uchar alphaSize() const noexcept { return get(AlphaField, AlphaFieldWidth); }
0176 
0177     constexpr inline uchar bitsPerPixel() const noexcept { return get(FirstField, FirstFieldWidth) +
0178                                                                                  get(SecondField, SecondFieldWidth) +
0179                                                                                  get(ThirdField, ThirdFieldWidth) +
0180                                                                                  get(FourthField, FourthFieldWidth) +
0181                                                                                  get(FifthField, FifthFieldWidth) +
0182                                                                                  get(AlphaField, AlphaFieldWidth); }
0183 
0184     constexpr inline AlphaUsage alphaUsage() const noexcept { return AlphaUsage(get(AlphaUsageField, AlphaUsageFieldWidth)); }
0185     constexpr inline AlphaPosition alphaPosition() const noexcept { return AlphaPosition(get(AlphaPositionField, AlphaPositionFieldWidth)); }
0186     constexpr inline AlphaPremultiplied premultiplied() const noexcept { return AlphaPremultiplied(get(PremulField, PremulFieldWidth)); }
0187     constexpr inline TypeInterpretation typeInterpretation() const noexcept { return TypeInterpretation(get(TypeInterpretationField, TypeInterpretationFieldWidth)); }
0188     constexpr inline ByteOrder byteOrder() const noexcept { return ByteOrder(get(ByteOrderField, ByteOrderFieldWidth)); }
0189 
0190     constexpr inline YUVLayout yuvLayout() const noexcept { return YUVLayout(get(SubEnumField, SubEnumFieldWidth)); }
0191     constexpr inline uchar subEnum() const noexcept { return get(SubEnumField, SubEnumFieldWidth); }
0192 
0193 private:
0194     constexpr static inline ByteOrder resolveByteOrder(ByteOrder bo) { return resolveByteOrder(bo, UnsignedInteger ); }
0195     constexpr static inline ByteOrder resolveByteOrder(ByteOrder bo, TypeInterpretation ti)
0196     { return bo == CurrentSystemEndian ? ti == UnsignedByte || Q_BYTE_ORDER == Q_BIG_ENDIAN ? BigEndian : LittleEndian : bo ; }
0197 
0198 private:
0199     quint64 data;
0200 
0201     friend Q_DECL_CONST_FUNCTION constexpr inline bool operator==(QPixelFormat fmt1, QPixelFormat fmt2)
0202     { return fmt1.data == fmt2.data; }
0203 
0204     friend Q_DECL_CONST_FUNCTION constexpr inline bool operator!=(QPixelFormat fmt1, QPixelFormat fmt2)
0205     { return !(fmt1 == fmt2); }
0206 
0207 #ifndef QT_NO_DEBUG_STREAM
0208     friend Q_GUI_EXPORT QDebug operator<<(QDebug debug, const QPixelFormat &format);
0209 #endif
0210 };
0211 static_assert(sizeof(QPixelFormat) == sizeof(quint64));
0212 Q_DECLARE_TYPEINFO(QPixelFormat, Q_PRIMITIVE_TYPE);
0213 
0214 
0215 namespace QtPrivate {
0216     QPixelFormat Q_GUI_EXPORT QPixelFormat_createYUV(QPixelFormat::YUVLayout yuvLayout,
0217                                                      uchar alphaSize,
0218                                                      QPixelFormat::AlphaUsage alphaUsage,
0219                                                      QPixelFormat::AlphaPosition alphaPosition,
0220                                                      QPixelFormat::AlphaPremultiplied premultiplied,
0221                                                      QPixelFormat::TypeInterpretation typeInterpretation,
0222                                                      QPixelFormat::ByteOrder byteOrder);
0223 }
0224 
0225 constexpr QPixelFormat::QPixelFormat(ColorModel mdl,
0226                                      uchar firstSize,
0227                                      uchar secondSize,
0228                                      uchar thirdSize,
0229                                      uchar fourthSize,
0230                                      uchar fifthSize,
0231                                      uchar alfa,
0232                                      AlphaUsage usage,
0233                                      AlphaPosition position,
0234                                      AlphaPremultiplied premult,
0235                                      TypeInterpretation typeInterp,
0236                                      ByteOrder b_order,
0237                                      uchar s_enum) noexcept
0238     : data(set(ModelField, ModelFieldWidth, uchar(mdl)) |
0239            set(FirstField, FirstFieldWidth, firstSize) |
0240            set(SecondField, SecondFieldWidth, secondSize) |
0241            set(ThirdField, ThirdFieldWidth, thirdSize) |
0242            set(FourthField, FourthFieldWidth, fourthSize) |
0243            set(FifthField, FifthFieldWidth, fifthSize) |
0244            set(AlphaField, AlphaFieldWidth, alfa) |
0245            set(AlphaUsageField, AlphaUsageFieldWidth, uchar(usage)) |
0246            set(AlphaPositionField, AlphaPositionFieldWidth, uchar(position)) |
0247            set(PremulField, PremulFieldWidth, uchar(premult)) |
0248            set(TypeInterpretationField, TypeInterpretationFieldWidth, uchar(typeInterp)) |
0249            set(ByteOrderField, ByteOrderFieldWidth, uchar(resolveByteOrder(b_order, typeInterp))) |
0250            set(SubEnumField, SubEnumFieldWidth, s_enum) |
0251            set(UnusedField, UnusedFieldWidth, 0))
0252 {
0253 }
0254 
0255 constexpr inline QPixelFormat qPixelFormatRgba(uchar red,
0256                                                uchar green,
0257                                                uchar blue,
0258                                                uchar alfa,
0259                                                QPixelFormat::AlphaUsage usage,
0260                                                QPixelFormat::AlphaPosition position,
0261                                                QPixelFormat::AlphaPremultiplied pmul=QPixelFormat::NotPremultiplied,
0262                                                QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedInteger) noexcept
0263 {
0264     return QPixelFormat(QPixelFormat::RGB,
0265                         red,
0266                         green,
0267                         blue,
0268                         0,
0269                         0,
0270                         alfa,
0271                         usage,
0272                         position,
0273                         pmul,
0274                         typeInt);
0275 }
0276 
0277 constexpr inline QPixelFormat qPixelFormatGrayscale(uchar channelSize,
0278                                                     QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedInteger) noexcept
0279 {
0280     return QPixelFormat(QPixelFormat::Grayscale,
0281                         channelSize,
0282                         0,
0283                         0,
0284                         0,
0285                         0,
0286                         0,
0287                         QPixelFormat::IgnoresAlpha,
0288                         QPixelFormat::AtBeginning,
0289                         QPixelFormat::NotPremultiplied,
0290                         typeInt);
0291 }
0292 
0293 constexpr inline QPixelFormat qPixelFormatAlpha(uchar channelSize,
0294                                                 QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedInteger) noexcept
0295 {
0296     return QPixelFormat(QPixelFormat::Alpha,
0297                         0,
0298                         0,
0299                         0,
0300                         0,
0301                         0,
0302                         channelSize,
0303                         QPixelFormat::UsesAlpha,
0304                         QPixelFormat::AtBeginning,
0305                         QPixelFormat::NotPremultiplied,
0306                         typeInt);
0307 }
0308 
0309 constexpr inline QPixelFormat qPixelFormatCmyk(uchar channelSize,
0310                                                uchar alfa=0,
0311                                                QPixelFormat::AlphaUsage usage=QPixelFormat::IgnoresAlpha,
0312                                                QPixelFormat::AlphaPosition position=QPixelFormat::AtBeginning,
0313                                                QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedInteger) noexcept
0314 {
0315     return QPixelFormat(QPixelFormat::CMYK,
0316                         channelSize,
0317                         channelSize,
0318                         channelSize,
0319                         channelSize,
0320                         0,
0321                         alfa,
0322                         usage,
0323                         position,
0324                         QPixelFormat::NotPremultiplied,
0325                         typeInt);
0326 }
0327 
0328 constexpr inline QPixelFormat qPixelFormatHsl(uchar channelSize,
0329                                               uchar alfa=0,
0330                                               QPixelFormat::AlphaUsage usage=QPixelFormat::IgnoresAlpha,
0331                                               QPixelFormat::AlphaPosition position=QPixelFormat::AtBeginning,
0332                                               QPixelFormat::TypeInterpretation typeInt=QPixelFormat::FloatingPoint) noexcept
0333 {
0334     return QPixelFormat(QPixelFormat::HSL,
0335                         channelSize,
0336                         channelSize,
0337                         channelSize,
0338                         0,
0339                         0,
0340                         alfa,
0341                         usage,
0342                         position,
0343                         QPixelFormat::NotPremultiplied,
0344                         typeInt);
0345 }
0346 
0347 constexpr inline QPixelFormat qPixelFormatHsv(uchar channelSize,
0348                                               uchar alfa=0,
0349                                               QPixelFormat::AlphaUsage usage=QPixelFormat::IgnoresAlpha,
0350                                               QPixelFormat::AlphaPosition position=QPixelFormat::AtBeginning,
0351                                               QPixelFormat::TypeInterpretation typeInt=QPixelFormat::FloatingPoint) noexcept
0352 {
0353     return QPixelFormat(QPixelFormat::HSV,
0354                         channelSize,
0355                         channelSize,
0356                         channelSize,
0357                         0,
0358                         0,
0359                         alfa,
0360                         usage,
0361                         position,
0362                         QPixelFormat::NotPremultiplied,
0363                         typeInt);
0364 }
0365 
0366 inline QPixelFormat qPixelFormatYuv(QPixelFormat::YUVLayout layout,
0367                                     uchar alfa=0,
0368                                     QPixelFormat::AlphaUsage usage=QPixelFormat::IgnoresAlpha,
0369                                     QPixelFormat::AlphaPosition position=QPixelFormat::AtBeginning,
0370                                     QPixelFormat::AlphaPremultiplied p_mul=QPixelFormat::NotPremultiplied,
0371                                     QPixelFormat::TypeInterpretation typeInt=QPixelFormat::UnsignedByte,
0372                                     QPixelFormat::ByteOrder b_order=QPixelFormat::BigEndian)
0373 {
0374     return QtPrivate::QPixelFormat_createYUV(layout,
0375                                              alfa,
0376                                              usage,
0377                                              position,
0378                                              p_mul,
0379                                              typeInt,
0380                                              b_order);
0381 }
0382 
0383 QT_END_NAMESPACE
0384 
0385 #endif //QPIXELFORMAT_H