- Qt 6.12
- Qt GUI
- C++ Classes
- QPixelFormat
- Qt 6.12.0 Reference Documentation

# QPixelFormat Class

QPixelFormat is a class for describing different pixel layouts in graphics buffers. More...

Header: | `#include <QPixelFormat>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Gui)` `target_link_libraries(mytarget PRIVATE Qt6::Gui)`
qmake: | `QT += gui`

- List of all members, including inherited members

## Public Types

enum | AlphaPosition { AtBeginning, AtEnd }
enum | AlphaPremultiplied { NotPremultiplied, Premultiplied }
enum | AlphaUsage { IgnoresAlpha, UsesAlpha }
enum | ByteOrder { LittleEndian, BigEndian, CurrentSystemEndian }
enum | ColorModel { RGB, BGR, Indexed, Grayscale, CMYK, …, Alpha }
enum | TypeInterpretation { UnsignedInteger, UnsignedShort, UnsignedByte, FloatingPoint }
enum | YUVLayout { YUV444, YUV422, YUV411, YUV420P, YUV420SP, …, Y16 }

## Public Functions

| QPixelFormat()
| QPixelFormat(QPixelFormat::ColorModel colorModel, uchar firstSize, uchar secondSize, uchar thirdSize, uchar fourthSize, uchar fifthSize, uchar alphaSize, QPixelFormat::AlphaUsage alphaUsage, QPixelFormat::AlphaPosition alphaPosition, QPixelFormat::AlphaPremultiplied premultiplied, QPixelFormat::TypeInterpretation typeInterpretation, QPixelFormat::ByteOrder byteOrder = CurrentSystemEndian, uchar subEnum = 0)
QPixelFormat::AlphaPosition | alphaPosition() const
uchar | alphaSize() const
QPixelFormat::AlphaUsage | alphaUsage() const
uchar | bitsPerPixel() const
uchar | blackSize() const
uchar | blueSize() const
uchar | brightnessSize() const
QPixelFormat::ByteOrder | byteOrder() const
uchar | channelCount() const
QPixelFormat::ColorModel | colorModel() const
uchar | cyanSize() const
uchar | greenSize() const
uchar | hueSize() const
uchar | lightnessSize() const
uchar | magentaSize() const
QPixelFormat::AlphaPremultiplied | premultiplied() const
uchar | redSize() const
uchar | saturationSize() const
QPixelFormat::TypeInterpretation | typeInterpretation() const
uchar | yellowSize() const
QPixelFormat::YUVLayout | yuvLayout() const

## Related Non-Members

QPixelFormat | qPixelFormatAlpha(uchar channelSize, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)
QPixelFormat | qPixelFormatCmyk(uchar channelSize, uchar alphaSize = 0, QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)
QPixelFormat | qPixelFormatGrayscale(uchar channelSize, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)
QPixelFormat | qPixelFormatHsl(uchar channelSize, uchar alphaSize = 0, QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::FloatingPoint)
QPixelFormat | qPixelFormatHsv(uchar channelSize, uchar alphaSize = 0, QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::FloatingPoint)
QPixelFormat | qPixelFormatRgba(uchar redSize, uchar greenSize, uchar blueSize, uchar alphaSize, QPixelFormat::AlphaUsage alphaUsage, QPixelFormat::AlphaPosition alphaPosition, QPixelFormat::AlphaPremultiplied premultiplied = QPixelFormat::NotPremultiplied, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)
QPixelFormat | qPixelFormatYuv(QPixelFormat::YUVLayout yuvLayout, uchar alphaSize = 0, QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning, QPixelFormat::AlphaPremultiplied premultiplied = QPixelFormat::NotPremultiplied, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedByte, QPixelFormat::ByteOrder byteOrder = QPixelFormat::BigEndian)

## Detailed Description

In Qt there is a often a need to represent the layout of the pixels in a graphics buffer. QPixelFormat can describe up to 5 color channels and 1 alpha channel, including details about how these channels are represented in memory individually and in relation to each other.

The typeInterpretation() and byteOrder() determines how each pixel should be read/interpreted, while alphaSize(), alphaUsage(), alphaPosition(), and premultiplied() describes the position and properties of the possible alpha channel.

There is no support for describing YUV's macro pixels. Instead a list of YUV formats is provided. When a QPixelFormat describes a YUV format, the bitsPerPixel() value is deduced from the YUV layout.

## Member Type Documentation

### enum QPixelFormat::AlphaPosition

This enum describes the alpha position of the pixel format.

Constant | Value | Description
--- | --- | ---
`QPixelFormat::AtBeginning` | `0` | The alpha channel will be put in front of the color channels. E.g. ARGB.
`QPixelFormat::AtEnd` | `1` | The alpha channel will be put in the back of the color channels. E.g. RGBA.

See also alphaSize(), alphaUsage(), and premultiplied().

### enum QPixelFormat::AlphaPremultiplied

This enum describes whether the alpha channel of the pixel format is premultiplied into the color channels or not.

Constant | Value | Description
--- | --- | ---
`QPixelFormat::NotPremultiplied` | `0` | The alpha channel is not multiplied into the color channels.
`QPixelFormat::Premultiplied` | `1` | The alpha channel is multiplied into the color channels.

See also alphaSize(), alphaUsage(), and alphaPosition().

### enum QPixelFormat::AlphaUsage

This enum describes the alpha usage of the pixel format.

Constant | Value | Description
--- | --- | ---
`QPixelFormat::IgnoresAlpha` | `1` | The alpha channel is not used.
`QPixelFormat::UsesAlpha` | `0` | The alpha channel is used.

See also alphaSize(), alphaPosition(), and premultiplied().

### enum QPixelFormat::ByteOrder

This enum describes the byte order of the pixel format.

Constant | Value | Description
--- | --- | ---
`QPixelFormat::LittleEndian` | `0` | The byte order is little endian.
`QPixelFormat::BigEndian` | `1` | The byte order is big endian.
`QPixelFormat::CurrentSystemEndian` | `2` | This enum will not be stored, but is converted in the constructor to the endian enum that matches the enum of the current system.

See also typeInterpretation().

### enum QPixelFormat::ColorModel

This enum describes the color model of the pixel format.

Constant | Value | Description
--- | --- | ---
`QPixelFormat::RGB` | `0` | The color model is RGB.
`QPixelFormat::BGR` | `1` | This is logically the opposite endian version of RGB. However, for ease of use it has its own model.
`QPixelFormat::Indexed` | `2` | The color model uses a color palette.
`QPixelFormat::Grayscale` | `3` | The color model is Grayscale.
`QPixelFormat::CMYK` | `4` | The color model is CMYK.
`QPixelFormat::HSL` | `5` | The color model is HSL.
`QPixelFormat::HSV` | `6` | The color model is HSV.
`QPixelFormat::YUV` | `7` | The color model is YUV.
`QPixelFormat::Alpha` | `8` | [since 5.5] There is no color model, only alpha is used.

### enum QPixelFormat::TypeInterpretation

This enum describes the type interpretation of the pixel format.

Constant | Value | Description
--- | --- | ---
`QPixelFormat::UnsignedInteger` | `0` | The pixels should be read as one or more `unsigned int`.
`QPixelFormat::UnsignedShort` | `1` | The pixels should be read as one or more `unsigned short`.
`QPixelFormat::UnsignedByte` | `2` | The pixels should be read as one or more `byte`.
`QPixelFormat::FloatingPoint` | `3` | The pixels should be read as one or more floating point numbers, with the concrete type defined by the color/alpha channel, ie. `qfloat16` for 16-bit half-float formats and `float` for 32-bit full-float formats.

See also byteOrder().

### enum QPixelFormat::YUVLayout

This enum describes the YUV layout of the pixel format, given that it has a color model of QPixelFormat::YUV.

Constant | Value
--- | ---
`QPixelFormat::YUV444` | `0`
`QPixelFormat::YUV422` | `1`
`QPixelFormat::YUV411` | `2`
`QPixelFormat::YUV420P` | `3`
`QPixelFormat::YUV420SP` | `4`
`QPixelFormat::YV12` | `5`
`QPixelFormat::UYVY` | `6`
`QPixelFormat::YUYV` | `7`
`QPixelFormat::NV12` | `8`
`QPixelFormat::NV21` | `9`
`QPixelFormat::IMC1` | `10`
`QPixelFormat::IMC2` | `11`
`QPixelFormat::IMC3` | `12`
`QPixelFormat::IMC4` | `13`
`QPixelFormat::Y8` | `14`
`QPixelFormat::Y16` | `15`

## Member Function Documentation

### `[constexpr noexcept]` QPixelFormat::QPixelFormat()

Creates a null pixelformat. This format maps to QImage::Format_Invalid.

### `[constexpr noexcept]` QPixelFormat::QPixelFormat(QPixelFormat::ColorModel colorModel, uchar firstSize, uchar secondSize, uchar thirdSize, uchar fourthSize, uchar fifthSize, uchar alphaSize, QPixelFormat::AlphaUsage alphaUsage, QPixelFormat::AlphaPosition alphaPosition, QPixelFormat::AlphaPremultiplied premultiplied, QPixelFormat::TypeInterpretation typeInterpretation, QPixelFormat::ByteOrder byteOrder = CurrentSystemEndian, uchar subEnum = 0)

Creates a QPixelFormat which assigns its data to the attributes. colorModel will be put into a buffer which is 4 bits long.

firstSize secondSize thirdSize fourthSize fifthSize alphaSize are all meant to represent the size of a channel. The channels will be used for different uses dependent on the colorModel. For RGB the firstSize will represent the Red channel. On CMYK it will represent the value of the Cyan channel.

alphaUsage represents if the alpha channel is used or not.

alphaPosition is the position of the alpha channel.

premultiplied represents if the alpha channel is already multiplied with the color channels.

typeInterpretation is how the pixel is interpreted.

byteOrder represents the endianness of the pixelformat. This defaults to CurrentSystemEndian, which will be resolve to the system's endianness for non-byte-ordered formats, and QPixelFormat::BigEndian for QPixelFormat::UnsignedByte.

subEnum is used for colorModels that have to store some extra information with supplying an extra enum. This is used by YUV to store the YUV type The default value is 0.

Note: BGR formats have their own color model, and should not be described by using the opposite endianness of an RGB format.

### `[constexpr noexcept]` QPixelFormat::AlphaPosition QPixelFormat::alphaPosition() const

Accessor function for the position of the alpha channel relative to the color channels.

For formats where the individual channels map to individual units, the alpha position is relative to these units. For example for QImage::Format_RGBA16FPx4 which has an alpha position of QPixelFormat::AtEnd, the alpha is the last `qfloat16` read.

For formats where multiple channels are packed into a single unit, the QPixelFormat::AtBeginning and QPixelFormat::AtEnd values map to the most significant and least significant bits of the packed unit, with respect to the format's own byteOrder().

For example, for QImage::Format_ARGB32, which has a type interpretation of QPixelFormat::UnsignedInteger and a byteOrder() that always matches the host system, the alpha position of QPixelFormat::AtBeginning means that the alpha can always be found at `0xFF000000`.

If the pixel format and host endianness does not match care must be taken to correctly map the pixel format layout to the host memory layout.

See also alphaUsage(), alphaSize(), and premultiplied().

### `[constexpr noexcept]` uchar QPixelFormat::alphaSize() const

Accessor function for the alpha channel size.

### `[constexpr noexcept]` QPixelFormat::AlphaUsage QPixelFormat::alphaUsage() const

Accessor function for whether the alpha channel is used or not.

Sometimes the pixel format reserves place for an alpha channel, so alphaSize() will return > 0, but the alpha channel is not used/ignored.

For example, for QImage::Format_RGB32, the bitsPerPixel() is 32, because the alpha channel has a size of 8, but alphaUsage() reflects QPixelFormat::IgnoresAlpha.

Note that in such situations the position of the unused alpha channel is still important, as it affects the placement of the color channels.

See also alphaPosition(), alphaSize(), and premultiplied().

### `[constexpr noexcept]` uchar QPixelFormat::bitsPerPixel() const

Accessor function for the bits used per pixel. This function returns the sum of all the color channels + the size of the alpha channel.

### `[constexpr noexcept]` uchar QPixelFormat::blackSize() const

Accessor function for the black/key color channel.

### `[constexpr noexcept]` uchar QPixelFormat::blueSize() const

Accessor function for the size of the blue color channel.

### `[constexpr noexcept]` uchar QPixelFormat::brightnessSize() const

Accessor function for the brightness channel size.

### `[constexpr noexcept]` QPixelFormat::ByteOrder QPixelFormat::byteOrder() const

The byte order of the pixel format determines the memory layout of the individual type units, as described by the typeInterpretation().

This function will never return QPixelFormat::CurrentSystemEndian as this value is translated to the system's endian value in the constructor.

For pixel formats with typeInterpretation() QPixelFormat::UnsignedByte this will typically be QPixelFormat::BigEndian, while other type interpretations will typically reflect the endianness of the current system.

If the byte order of the pixel format matches the current system the individual type units can be read and manipulated using the same bit masks and operations, regardless of the host system endianness. For example, with QImage::Format_ARGB32, which has a QPixelFormat::UnsignedInteger type interpretation, the alpha can always be read by masking the `unsigned int` by `0xFF000000`, regardless of the host endianness.

If the pixel format and host endianness does not match care must be taken to account for this. Classes like QImage do not swap the internal bits to match the host system endianness in these cases.

See also typeInterpretation() and alphaPosition().

### `[constexpr noexcept]` uchar QPixelFormat::channelCount() const

Accessor function for the channel count.

The channel count represents channels (color and alpha) with a size > 0.

### `[constexpr noexcept]` QPixelFormat::ColorModel QPixelFormat::colorModel() const

Accessor function for the color model.

Note that for QPixelFormat::YUV the individual macro pixels can not be described. Instead a list of YUV formats is provided, and the bitsPerPixel() value is deduced from the YUV layout.

### `[constexpr noexcept]` uchar QPixelFormat::cyanSize() const

Accessor function for the cyan color channel.

### `[constexpr noexcept]` uchar QPixelFormat::greenSize() const

Accessor function for the size of the green color channel.

### `[constexpr noexcept]` uchar QPixelFormat::hueSize() const

Accessor function for the hue channel size.

### `[constexpr noexcept]` uchar QPixelFormat::lightnessSize() const

Accessor function for the lightness channel size.

### `[constexpr noexcept]` uchar QPixelFormat::magentaSize() const

Accessor function for the megenta color channel.

### `[constexpr noexcept]` QPixelFormat::AlphaPremultiplied QPixelFormat::premultiplied() const

Accessor function for the whether the alpha channel is multiplied in to the color channels.

### `[constexpr noexcept]` uchar QPixelFormat::redSize() const

Accessor function for the size of the red color channel.

### `[constexpr noexcept]` uchar QPixelFormat::saturationSize() const

Accessor function for the saturation channel size.

### `[constexpr noexcept]` QPixelFormat::TypeInterpretation QPixelFormat::typeInterpretation() const

The type interpretation determines how each pixel should be read.

Each pixel is represented as one or more units of the given type, laid out sequentially in memory.

Note: The byte order of the pixel format and the endianness of the host system only affect the memory layout of each individual unit being read — not the relative ordering of the units.

For example, QImage::Format_Mono has a pixel format of 1 bits per pixel and a QPixelFormat::UnsignedByte type interpretation, which should be read as a single `byte`. Similarly, QImage::Format_RGB888 has a pixel format of 24 bits per pixel, and and a QPixelFormat::UnsignedByte type interpretation, which should be read as three consecutive `byte`s.

Many of the QImage formats are 32-bit with a type interpretation of QPixelFormat::UnsignedInteger, which should be read as a single `unsigned int`.

For QPixelFormat::FloatingPoint formats like QImage::Format_RGBA16FPx4 or QImage::Format_RGBA32FPx4 the type is determined based on the size of the individual color/alpha channels, with `qfloat16` for 16-bit half-float formats and `float` for 32-bit full-float formats.

See also byteOrder().

### `[constexpr noexcept]` uchar QPixelFormat::yellowSize() const

Accessor function for the yellow color channel.

### `[constexpr noexcept]` QPixelFormat::YUVLayout QPixelFormat::yuvLayout() const

Accessor function for the YUVLayout. It is difficult to describe the color channels of a YUV pixel format since YUV color model uses macro pixels. Instead the layout of the pixels are stored as an enum.

## Related Non-Members

### `[constexpr noexcept]` QPixelFormat qPixelFormatAlpha(uchar channelSize, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)

Constructor function for creating an Alpha format. A mask format can be described by passing 1 to channelSize. Its also possible to define very accurate alpha formats using doubles to describe each pixel by passing 8 as channelSize and FloatingPoint as typeInterpretation.

See also QPixelFormat::TypeInterpretation.

### `[constexpr noexcept]` QPixelFormat qPixelFormatCmyk(uchar channelSize, uchar alphaSize = 0, QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)

Constructor function for creating CMYK formats. The channel count will be 4 or 5 depending on if alphaSize is bigger than zero or not. The CMYK color channels will all be set to the value of channelSize.

alphaUsage alphaPosition and typeInterpretation are all accessible with the accessors with the same name.

See also QPixelFormat::TypeInterpretation.

### `[constexpr noexcept]` QPixelFormat qPixelFormatGrayscale(uchar channelSize, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)

Constructor function for creating a Grayscale format. Monochrome formats can be described by passing 1 to channelSize. Its also possible to define very accurate grayscale formats using doubles to describe each pixel by passing 8 as channelSize and FloatingPoint as typeInterpretation.

See also QPixelFormat::TypeInterpretation.

### `[constexpr noexcept]` QPixelFormat qPixelFormatHsl(uchar channelSize, uchar alphaSize = 0, QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::FloatingPoint)

Constructor function for creating HSL formats. The channel count will be 3 or 4 depending on if alphaSize is bigger than 0.

channelSize will set the hueSize saturationSize and lightnessSize to the same value.

alphaUsage alphaPosition and typeInterpretation are all accessible with the accessors with the same name.

### `[constexpr noexcept]` QPixelFormat qPixelFormatHsv(uchar channelSize, uchar alphaSize = 0, QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::FloatingPoint)

Constructor function for creating HSV formats. The channel count will be 3 or 4 depending on if alphaSize is bigger than 0.

channelSize will set the hueSize saturationSize and brightnessSize to the same value.

alphaUsage alphaPosition and typeInterpretation are all accessible with the accessors with the same name.

### `[constexpr noexcept]` QPixelFormat qPixelFormatRgba(uchar redSize, uchar greenSize, uchar blueSize, uchar alphaSize, QPixelFormat::AlphaUsage alphaUsage, QPixelFormat::AlphaPosition alphaPosition, QPixelFormat::AlphaPremultiplied premultiplied = QPixelFormat::NotPremultiplied, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)

Constructor function making an RGB pixelformat. redSize greenSize blueSize represent the size of each color channel. alphaSize describes the alpha channel size and its position is described with alphaPosition. alphaUsage is used to determine if the alpha channel is used or not. Setting the alpha channel size to 8 and alphaUsage to IgnoresAlpha is how it is possible to create a 32 bit format where the rgb channels only use 24 bits combined. premultiplied typeInterpretation are accessible with accessors with the same name.

See also QPixelFormat::TypeInterpretation.

### QPixelFormat qPixelFormatYuv(QPixelFormat::YUVLayout yuvLayout, uchar alphaSize = 0, QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha, QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning, QPixelFormat::AlphaPremultiplied premultiplied = QPixelFormat::NotPremultiplied, QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedByte, QPixelFormat::ByteOrder byteOrder = QPixelFormat::BigEndian)

Constructor function for creating a QPixelFormat describing a YUV format with yuvLayout. alphaSize describes the size of a potential alpha channel and is position is described with alphaPosition. The "first" "second" .. "fifth" channels are all set to 0. alphaUsage premultiplied typeInterpretation and byteOrder will work as with other formats.