Skip to content

Commit 68a1a6d

Browse files
committed
Revamp QPixelFormat documentation
Especially with regards to typeInterpretation() and byteOrder(). Change-Id: I6a6c06650cc76dcc9fd3876d7acbc5c45e41f7dc Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
1 parent 06b7166 commit 68a1a6d

File tree

2 files changed

+132
-77
lines changed

2 files changed

+132
-77
lines changed
-13.9 KB
Binary file not shown.

src/gui/kernel/qpixelformat.cpp

Lines changed: 132 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,24 @@ QT_BEGIN_NAMESPACE
1313
layouts in graphics buffers.
1414
1515
In Qt there is a often a need to represent the layout of the pixels in a
16-
graphics buffer. Internally QPixelFormat stores everything in a 64 bit
17-
datastructure. This gives performance but also some limitations.
16+
graphics buffer. QPixelFormat can describe up to 5 color channels and 1 alpha
17+
channel, including details about how these channels are represented in memory
18+
individually and in relation to each other.
1819
19-
QPixelFormat can describe 5 color channels and 1 alpha channel, each can use
20-
6 bits to describe the size of the color channel.
20+
The typeInterpretation() and byteOrder() determines how each pixel should be
21+
read/interpreted, while alphaSize(), alphaUsage(), alphaPosition(), and
22+
premultiplied() describes the position and properties of the possible alpha
23+
channel.
2124
22-
The position of the alpha channel is described with a separate enum. This is
23-
to make it possible to describe QImage formats like ARGB32, and also
24-
describe typical OpenGL formats like RBGA8888.
25-
26-
How pixels are suppose to be read is determined by the TypeInterpretation
27-
enum. It describes if color values are suppose to be read byte per byte,
28-
or if a pixel is suppose to be read as a complete int and then masked.
29-
\sa TypeInterpretation
30-
31-
There is no support for describing YUV's macro pixels. Instead a list of YUV
32-
formats has been made. When a QPixelFormat is describing a YUV format, the
33-
bitsPerPixel value has been deduced by the YUV Layout enum. Also, the color
34-
channels should all be set to zero except the fifth color channel that
35-
should store the bitsPerPixel value.
25+
There is no support for describing YUV's macro pixels. Instead a list of
26+
\l{QPixelFormat::YUVLayout}{YUV formats} is provided. When a QPixelFormat
27+
describes a YUV format, the bitsPerPixel() value is deduced from the YUV layout.
3628
*/
3729

3830
/*!
3931
\enum QPixelFormat::ColorModel
4032
41-
This enum type is used to describe the color model of the pixelformat.
42-
Alpha was added in 5.5.
33+
This enum describes the \l{colorModel()}{color model} of the pixel format.
4334
4435
\value RGB The color model is RGB.
4536
@@ -58,100 +49,83 @@ QT_BEGIN_NAMESPACE
5849
5950
\value YUV The color model is YUV.
6051
61-
\value Alpha There is no color model, only alpha is used.
52+
\value Alpha [since 5.5] There is no color model, only alpha is used.
6253
*/
6354

6455
/*!
6556
\enum QPixelFormat::AlphaUsage
6657
67-
This enum describes if the alpha channel is used or not. Sometimes the
68-
pixelformat will have a size for the alpha channel, but the pixel format
69-
does actually not use the alpha channel. For example RGB32 is such a
70-
format. The RGB channels are 8 bits each, and there is no alpha channel.
71-
But the complete size for each pixel is 32. Therefore the alpha channel size
72-
is 8, but the alpha channel is ignored. Its important to note that in such
73-
situations the position of the alpha channel is significant.
58+
This enum describes the \l{alphaUsage()}{alpha usage} of the pixel format.
7459
7560
\value IgnoresAlpha The alpha channel is not used.
7661
7762
\value UsesAlpha The alpha channel is used.
63+
64+
\sa alphaSize(), alphaPosition(), premultiplied()
7865
*/
7966

8067
/*!
8168
\enum QPixelFormat::AlphaPosition
8269
83-
This enum type is used to describe the alpha channels position relative to the
84-
color channels.
70+
This enum describes the \l{alphaPosition()}{alpha position} of the pixel format.
8571
8672
\value AtBeginning The alpha channel will be put in front of the color
87-
channels . E.g. ARGB.
73+
channels. E.g. ARGB.
8874
8975
\value AtEnd The alpha channel will be put in the back of the color
9076
channels. E.g. RGBA.
77+
78+
\sa alphaSize(), alphaUsage(), premultiplied()
9179
*/
9280

9381
/*!
9482
\enum QPixelFormat::AlphaPremultiplied
9583
96-
This enum type describes the boolean state if the alpha channel is multiplied
97-
into the color channels or not.
84+
This enum describes whether the alpha channel of the pixel format is
85+
\l{premultiplied}{premultiplied} into the color channels or not.
9886
9987
\value NotPremultiplied The alpha channel is not multiplied into the color channels.
10088
10189
\value Premultiplied The alpha channel is multiplied into the color channels.
90+
91+
\sa alphaSize(), alphaUsage(), alphaPosition()
10292
*/
10393

10494
/*!
10595
\enum QPixelFormat::TypeInterpretation
10696
107-
This enum describes how each pixel is interpreted. If a pixel is read as a
108-
full 32 bit unsigned integer and then each channel is masked out, or if
109-
each byte is read as unsigned char values. Typically QImage formats
110-
interpret one pixel as an unsigned integer and then the color channels are
111-
masked out. OpenGL on the other hand typically interpreted pixels "one byte
112-
after the other", Ie. unsigned byte.
113-
114-
QImage also have the format Format_RGBA8888 (and its derivatives), where
115-
the pixels are interpreted as unsigned bytes. OpenGL has extensions that makes it
116-
possible to upload pixel buffers in an unsigned integer format.
97+
This enum describes the \l{typeInterpretation()}{type interpretation} of the pixel format.
11798
118-
\image qpixelformat-argb32buffer.png An unsigned integer ARGB32 pixel.
99+
\value UnsignedInteger The pixels should be read as one or more \c{unsigned int}.
100+
\value UnsignedShort The pixels should be read as one or more \c{unsigned short}.
101+
\value UnsignedByte The pixels should be read as one or more \c{byte}.
102+
\value FloatingPoint The pixels should be read as one or more floating point
103+
numbers, with the concrete type defined by the color/alpha
104+
channel, ie. \c{qfloat16} for 16-bit half-float formats and
105+
\c{float} for 32-bit full-float formats.
119106
120-
The image above shows a ARGB pixel in memory read as an unsigned integer.
121-
However, if this pixel was read byte for byte on a little endian system the
122-
first byte would be the byte containing the B-channel. The next byte would
123-
be the G-channel, then the R-channel and finally the A-channel. This shows
124-
that on little endian systems, how each pixel is interpreted is significant
125-
for integer formats. This is not the case on big endian systems.
126-
127-
\value UnsignedInteger
128-
\value UnsignedShort
129-
\value UnsignedByte
130-
\value FloatingPoint
107+
\sa byteOrder()
131108
*/
132109

133110
/*!
134111
\enum QPixelFormat::ByteOrder
135112
136-
This enum describes the ByteOrder of the pixel format. This enum is mostly
137-
ignored but have some use cases for YUV formats. BGR formats have their own
138-
color model, and should not be described by using the opposite endianness
139-
on an RGB format.
113+
This enum describes the \l{byteOrder()}{byte order} of the pixel format.
140114
141115
\value LittleEndian The byte order is little endian.
142116
\value BigEndian The byte order is big endian.
143117
\value CurrentSystemEndian This enum will not be stored, but is converted in
144118
the constructor to the endian enum that matches
145119
the enum of the current system.
146120
121+
\sa typeInterpretation()
147122
*/
148123

149124
/*!
150125
\enum QPixelFormat::YUVLayout
151126
152-
YUV is not represented by describing the size of the color channels. This is
153-
because YUV often use macro pixels, making the concept of separate color channels
154-
invalid. Instead the different YUV layouts are described with this enum.
127+
This enum describes the \l{yuvLayout()}{YUV layout} of the pixel format,
128+
given that it has a color model of QPixelFormat::YUV.
155129
156130
\value YUV444
157131
\value YUV422
@@ -217,6 +191,9 @@ QT_BEGIN_NAMESPACE
217191
\a subEnum is used for colorModels that have to store some extra
218192
information with supplying an extra enum. This is used by YUV to store the
219193
YUV type The default value is 0.
194+
195+
\note BGR formats have their own color model, and should not be described
196+
by using the opposite endianness of an RGB format.
220197
*/
221198

222199
/*!
@@ -342,14 +319,19 @@ QT_BEGIN_NAMESPACE
342319
/*!
343320
\fn ColorModel QPixelFormat::colorModel() const
344321
345-
Accessor function for getting the colorModel.
322+
Accessor function for the color model.
323+
324+
Note that for QPixelFormat::YUV the individual macro pixels can not be
325+
described. Instead a list of \l{QPixelFormat::YUVLayout}{YUV formats} is provided,
326+
and the bitsPerPixel() value is deduced from the YUV layout.
346327
*/
347328

348329
/*!
349330
\fn uchar QPixelFormat::channelCount() const
350331
351-
Accessor function for getting the channelCount. Channel Count is deduced
352-
by color channels with a size > 0 and if the size of the alpha channel is > 0.
332+
Accessor function for the channel count.
333+
334+
The channel count represents channels (color and alpha) with a size > 0.
353335
*/
354336

355337
/*!
@@ -428,48 +410,121 @@ QT_BEGIN_NAMESPACE
428410
\fn uchar QPixelFormat::bitsPerPixel() const
429411
430412
Accessor function for the bits used per pixel. This function returns the
431-
sum of the color channels + the size of the alpha channel.
413+
sum of all the color channels + the size of the alpha channel.
432414
*/
433415

434416
/*!
435417
\fn AlphaPremultiplied QPixelFormat::premultiplied() const
436418
437-
Accessor function for the AlphaPremultiplied enum. This indicates if the
438-
alpha channel is multiplied in to the color channels.
439-
419+
Accessor function for the whether the alpha channel is multiplied
420+
in to the color channels.
440421
*/
441422

442423
/*!
443424
\fn TypeInterpretation QPixelFormat::typeInterpretation() const
444425
445-
Accessor function for the type representation of a color channel or a pixel.
426+
The type interpretation determines how each pixel should be read.
427+
428+
Each pixel is represented as one or more units of the given type,
429+
laid out sequentially in memory.
430+
431+
\note The \l{byteOrder()}{byte order} of the pixel format and the
432+
endianness of the host system only affect the memory layout of each
433+
individual unit being read — \e{not} the relative ordering of the
434+
units.
435+
436+
For example, QImage::Format_Mono has a \l{QImage::pixelFormat()}{pixel format}
437+
of 1 bits per pixel and a QPixelFormat::UnsignedByte type interpretation,
438+
which should be read as a single \c{byte}. Similarly, QImage::Format_RGB888
439+
has a \l{QImage::pixelFormat()}{pixel format} of 24 bits per pixel, and
440+
and a QPixelFormat::UnsignedByte type interpretation, which should be
441+
read as three consecutive \c{byte}s.
442+
443+
Many of the QImage \l{QImage::Format}{formats} are 32-bit with a type
444+
interpretation of QPixelFormat::UnsignedInteger, which should be read
445+
as a single \c{unsigned int}.
446+
447+
For QPixelFormat::FloatingPoint formats like QImage::Format_RGBA16FPx4
448+
or QImage::Format_RGBA32FPx4 the type is determined based on the size
449+
of the individual color/alpha channels, with \c{qfloat16} for 16-bit
450+
half-float formats and \c{float} for 32-bit full-float formats.
446451
447-
\sa TypeInterpretation
452+
\sa byteOrder()
448453
*/
449454

450455
/*!
451456
\fn ByteOrder QPixelFormat::byteOrder() const
452457
453-
The byte order to use when not reading reading pixels as individual bytes.
458+
The byte order of the pixel format determines the memory layout of
459+
the individual type units, as described by the typeInterpretation().
454460
455-
For pixel formats with typeInterpreation() QPixelFormat::UnsignedByte this
461+
This function will never return QPixelFormat::CurrentSystemEndian as this
462+
value is translated to the system's endian value in the constructor.
463+
464+
For pixel formats with typeInterpretation() QPixelFormat::UnsignedByte this
456465
will typically be QPixelFormat::BigEndian, while other type interpretations
457466
will typically reflect the endianness of the current system.
458467
459-
This function will never return QPixelFormat::CurrentSystemEndian as this
460-
value is translated to the system's endian value in the constructor.
468+
If the byte order of the pixel format matches the current system the
469+
individual type units can be read and manipulated using the same bit
470+
masks and operations, regardless of the host system endianness. For
471+
example, with QImage::Format_ARGB32, which has a QPixelFormat::UnsignedInteger
472+
type interpretation, the alpha can always be read by masking the
473+
\c{unsigned int} by \c{0xFF000000}, regardless of the host endianness.
474+
475+
If the pixel format and host endianness does \e{not} match care must
476+
be taken to account for this. Classes like QImage do not swap the
477+
internal bits to match the host system endianness in these cases.
478+
479+
\sa typeInterpretation(), alphaPosition()
461480
*/
462481

463482
/*!
464483
\fn AlphaUsage QPixelFormat::alphaUsage() const
465484
466-
Accessor function for alphaUsage.
485+
Accessor function for whether the alpha channel is used or not.
486+
487+
Sometimes the pixel format reserves place for an alpha channel,
488+
so alphaSize() will return > 0, but the alpha channel is not
489+
used/ignored.
490+
491+
For example, for QImage::Format_RGB32, the bitsPerPixel() is 32,
492+
because the alpha channel has a size of 8, but alphaUsage()
493+
reflects QPixelFormat::IgnoresAlpha.
494+
495+
Note that in such situations the \l{alphaPosition()}{position} of
496+
the unused alpha channel is still important, as it affects the
497+
placement of the color channels.
498+
499+
\sa alphaPosition(), alphaSize(), premultiplied()
467500
*/
468501

469502
/*!
470503
\fn AlphaPosition QPixelFormat::alphaPosition() const
471504
472-
Accessor function for alphaPosition.
505+
Accessor function for the position of the alpha channel
506+
relative to the color channels.
507+
508+
For formats where the individual channels map to individual
509+
units, the alpha position is relative to these units. For
510+
example for QImage::Format_RGBA16FPx4 which has an alpha
511+
position of QPixelFormat::AtEnd, the alpha is the last
512+
\c{qfloat16} read.
513+
514+
For formats where multiple channels are packed into a single unit,
515+
the QPixelFormat::AtBeginning and QPixelFormat::AtEnd values map to
516+
the most significant and least significant bits of the packed unit,
517+
with respect to the format's own byteOrder().
518+
519+
For example, for QImage::Format_ARGB32, which has a type interpretation
520+
of QPixelFormat::UnsignedInteger and a byteOrder() that always matches
521+
the host system, the alpha position of QPixelFormat::AtBeginning means
522+
that the alpha can always be found at \c{0xFF000000}.
523+
524+
If the pixel format and host endianness does \e{not} match care must be
525+
taken to correctly map the pixel format layout to the host memory layout.
526+
527+
\sa alphaUsage(), alphaSize(), premultiplied()
473528
*/
474529

475530
/*!

0 commit comments

Comments
 (0)