Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpixelformat.cpp
Go to the documentation of this file.
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#include "qpixelformat.h"
6
7#include <QtCore/qdebug.h>
8
10
11/*!
12 \class QPixelFormat
13 \inmodule QtGui
14 \since 5.4
15 \brief QPixelFormat is a class for describing different pixel
16 layouts in graphics buffers.
17
18 In Qt there is a often a need to represent the layout of the pixels in a
19 graphics buffer. QPixelFormat can describe up to 5 color channels and 1 alpha
20 channel, including details about how these channels are represented in memory
21 individually and in relation to each other.
22
23 The typeInterpretation() and byteOrder() determines how each pixel should be
24 read/interpreted, while alphaSize(), alphaUsage(), alphaPosition(), and
25 premultiplied() describes the position and properties of the possible alpha
26 channel.
27
28 There is no support for describing YUV's macro pixels. Instead a list of
29 \l{QPixelFormat::YUVLayout}{YUV formats} is provided. When a QPixelFormat
30 describes a YUV format, the bitsPerPixel() value is deduced from the YUV layout.
31*/
32
33/*!
34 \enum QPixelFormat::ColorModel
35
36 This enum describes the \l{colorModel()}{color model} of the pixel format.
37
38 \value RGB The color model is RGB.
39
40 \value BGR This is logically the opposite endian version of RGB. However,
41 for ease of use it has its own model.
42
43 \value Indexed The color model uses a color palette.
44
45 \value Grayscale The color model is Grayscale.
46
47 \value CMYK The color model is CMYK.
48
49 \value HSL The color model is HSL.
50
51 \value HSV The color model is HSV.
52
53 \value YUV The color model is YUV.
54
55 \value Alpha [since 5.5] There is no color model, only alpha is used.
56*/
57
58/*!
59 \enum QPixelFormat::AlphaUsage
60
61 This enum describes the \l{alphaUsage()}{alpha usage} of the pixel format.
62
63 \value IgnoresAlpha The alpha channel is not used.
64
65 \value UsesAlpha The alpha channel is used.
66
67 \sa alphaSize(), alphaPosition(), premultiplied()
68*/
69
70/*!
71 \enum QPixelFormat::AlphaPosition
72
73 This enum describes the \l{alphaPosition()}{alpha position} of the pixel format.
74
75 \value AtBeginning The alpha channel will be put in front of the color
76 channels. E.g. ARGB.
77
78 \value AtEnd The alpha channel will be put in the back of the color
79 channels. E.g. RGBA.
80
81 \sa alphaSize(), alphaUsage(), premultiplied()
82*/
83
84/*!
85 \enum QPixelFormat::AlphaPremultiplied
86
87 This enum describes whether the alpha channel of the pixel format is
88 \l{premultiplied}{premultiplied} into the color channels or not.
89
90 \value NotPremultiplied The alpha channel is not multiplied into the color channels.
91
92 \value Premultiplied The alpha channel is multiplied into the color channels.
93
94 \sa alphaSize(), alphaUsage(), alphaPosition()
95*/
96
97/*!
98 \enum QPixelFormat::TypeInterpretation
99
100 This enum describes the \l{typeInterpretation()}{type interpretation} of the pixel format.
101
102 \value UnsignedInteger The pixels should be read as one or more \c{unsigned int}.
103 \value UnsignedShort The pixels should be read as one or more \c{unsigned short}.
104 \value UnsignedByte The pixels should be read as one or more \c{byte}.
105 \value FloatingPoint The pixels should be read as one or more floating point
106 numbers, with the concrete type defined by the color/alpha
107 channel, ie. \c{qfloat16} for 16-bit half-float formats and
108 \c{float} for 32-bit full-float formats.
109
110 \sa byteOrder()
111*/
112
113/*!
114 \enum QPixelFormat::ByteOrder
115
116 This enum describes the \l{byteOrder()}{byte order} of the pixel format.
117
118 \value LittleEndian The byte order is little endian.
119 \value BigEndian The byte order is big endian.
120 \value CurrentSystemEndian This enum will not be stored, but is converted in
121 the constructor to the endian enum that matches
122 the enum of the current system.
123
124 \sa typeInterpretation()
125*/
126
127/*!
128 \enum QPixelFormat::YUVLayout
129
130 This enum describes the \l{yuvLayout()}{YUV layout} of the pixel format,
131 given that it has a color model of QPixelFormat::YUV.
132
133 \value YUV444
134 \value YUV422
135 \value YUV411
136 \value YUV420P
137 \value YUV420SP
138 \value YV12
139 \value UYVY
140 \value YUYV
141 \value NV12
142 \value NV21
143 \value IMC1
144 \value IMC2
145 \value IMC3
146 \value IMC4
147 \value Y8
148 \value Y16
149*/
150
151/*!
152 \fn QPixelFormat::QPixelFormat()
153
154 Creates a null pixelformat. This format maps to QImage::Format_Invalid.
155*/
156
157/*!
158 \fn QPixelFormat::QPixelFormat(ColorModel colorModel,
159 uchar firstSize,
160 uchar secondSize,
161 uchar thirdSize,
162 uchar fourthSize,
163 uchar fifthSize,
164 uchar alphaSize,
165 AlphaUsage alphaUsage,
166 AlphaPosition alphaPosition,
167 AlphaPremultiplied premultiplied,
168 TypeInterpretation typeInterpretation,
169 ByteOrder byteOrder = CurrentSystemEndian,
170 uchar subEnum = 0)
171
172 Creates a QPixelFormat which assigns its data to the attributes.
173 \a colorModel will be put into a buffer which is 4 bits long.
174
175 \a firstSize \a secondSize \a thirdSize \a fourthSize \a fifthSize \a
176 alphaSize are all meant to represent the size of a channel. The channels will
177 be used for different uses dependent on the \a colorModel. For RGB the
178 firstSize will represent the Red channel. On CMYK it will represent the
179 value of the Cyan channel.
180
181 \a alphaUsage represents if the alpha channel is used or not.
182
183 \a alphaPosition is the position of the alpha channel.
184
185 \a premultiplied represents if the alpha channel is already multiplied with
186 the color channels.
187
188 \a typeInterpretation is how the pixel is interpreted.
189
190 \a byteOrder represents the endianness of the pixelformat. This defaults to
191 CurrentSystemEndian, which will be resolve to the system's endianness for
192 non-byte-ordered formats, and QPixelFormat::BigEndian for QPixelFormat::UnsignedByte.
193
194 \a subEnum is used for colorModels that have to store some extra
195 information with supplying an extra enum. This is used by YUV to store the
196 YUV type The default value is 0.
197
198 \note BGR formats have their own color model, and should not be described
199 by using the opposite endianness of an RGB format.
200*/
201
202/*!
203 \fn QPixelFormat qPixelFormatRgba(uchar redSize,
204 uchar greenSize,
205 uchar blueSize,
206 uchar alphaSize,
207 QPixelFormat::AlphaUsage alphaUsage,
208 QPixelFormat::AlphaPosition alphaPosition,
209 QPixelFormat::AlphaPremultiplied premultiplied = QPixelFormat::NotPremultiplied,
210 QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)
211 \relates QPixelFormat
212
213 Constructor function making an RGB pixelformat. \a redSize \a greenSize \a
214 blueSize represent the size of each color channel. \a alphaSize describes
215 the alpha channel size and its position is described with \a alphaPosition.
216 \a alphaUsage is used to determine if the alpha channel is used or not.
217 Setting the alpha channel size to 8 and alphaUsage to IgnoresAlpha is how
218 it is possible to create a 32 bit format where the rgb channels only use 24
219 bits combined. \a premultiplied \a typeInterpretation are
220 accessible with accessors with the same name.
221
222 \sa QPixelFormat::TypeInterpretation
223*/
224
225/*!
226 \fn QPixelFormat qPixelFormatGrayscale(uchar channelSize,
227 QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)
228 \relates QPixelFormat
229
230 Constructor function for creating a Grayscale format. Monochrome formats can be
231 described by passing 1 to \a channelSize. Its also possible to define very
232 accurate grayscale formats using doubles to describe each pixel by passing 8
233 as \a channelSize and FloatingPoint as \a typeInterpretation.
234
235 \sa QPixelFormat::TypeInterpretation
236*/
237
238/*!
239 \fn QPixelFormat qPixelFormatAlpha(uchar channelSize,
240 QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)
241 \relates QPixelFormat
242 \since 5.5
243
244 Constructor function for creating an Alpha format. A mask format can be
245 described by passing 1 to \a channelSize. Its also possible to define very
246 accurate alpha formats using doubles to describe each pixel by passing 8
247 as \a channelSize and FloatingPoint as \a typeInterpretation.
248
249 \sa QPixelFormat::TypeInterpretation
250*/
251
252
253/*!
254 \fn QPixelFormat qPixelFormatCmyk(uchar channelSize,
255 uchar alphaSize = 0,
256 QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha,
257 QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning,
258 QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)
259 \relates QPixelFormat
260
261 Constructor function for creating CMYK formats. The channel count will be 4 or
262 5 depending on if \a alphaSize is bigger than zero or not. The CMYK color
263 channels will all be set to the value of \a channelSize.
264
265 \a alphaUsage \a alphaPosition and \a typeInterpretation are all accessible with
266 the accessors with the same name.
267
268 \sa QPixelFormat::TypeInterpretation
269*/
270
271/*!
272 \fn QPixelFormat qPixelFormatHsl(uchar channelSize,
273 uchar alphaSize = 0,
274 QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha,
275 QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning,
276 QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::FloatingPoint)
277 \relates QPixelFormat
278
279 Constructor function for creating HSL formats. The channel count will be 3 or 4
280 depending on if \a alphaSize is bigger than 0.
281
282 \a channelSize will set the hueSize saturationSize and lightnessSize to the same value.
283
284 \a alphaUsage \a alphaPosition and \a typeInterpretation are all accessible with
285 the accessors with the same name.
286*/
287
288/*!
289 \fn QPixelFormat qPixelFormatHsv(uchar channelSize,
290 uchar alphaSize = 0,
291 QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha,
292 QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning,
293 QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::FloatingPoint)
294 \relates QPixelFormat
295
296 Constructor function for creating HSV formats. The channel count will be 3 or 4
297 depending on if \a alphaSize is bigger than 0.
298
299 \a channelSize will set the hueSize saturationSize and brightnessSize to the same value.
300
301 \a alphaUsage \a alphaPosition and \a typeInterpretation are all accessible with
302 the accessors with the same name.
303*/
304
305/*!
306 \fn QPixelFormat qPixelFormatYuv(QPixelFormat::YUVLayout yuvLayout,
307 uchar alphaSize = 0,
308 QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha,
309 QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning,
310 QPixelFormat::AlphaPremultiplied premultiplied = QPixelFormat::NotPremultiplied,
311 QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedByte,
312 QPixelFormat::ByteOrder byteOrder = QPixelFormat::BigEndian)
313 \relates QPixelFormat
314
315 Constructor function for creating a QPixelFormat describing a YUV format with
316 \a yuvLayout. \a alphaSize describes the size of a potential alpha channel
317 and is position is described with \a alphaPosition. The "first" "second" ..
318 "fifth" channels are all set to 0. \a alphaUsage \a premultiplied \a
319 typeInterpretation and \a byteOrder will work as with other formats.
320*/
321
322/*!
323 \fn ColorModel QPixelFormat::colorModel() const
324
325 Accessor function for the color model.
326
327 Note that for QPixelFormat::YUV the individual macro pixels can not be
328 described. Instead a list of \l{QPixelFormat::YUVLayout}{YUV formats} is provided,
329 and the bitsPerPixel() value is deduced from the YUV layout.
330*/
331
332/*!
333 \fn uchar QPixelFormat::channelCount() const
334
335 Accessor function for the channel count.
336
337 The channel count represents channels (color and alpha) with a size > 0.
338*/
339
340/*!
341 \fn uchar QPixelFormat::redSize() const
342
343 Accessor function for the size of the red color channel.
344*/
345
346/*!
347 \fn uchar QPixelFormat::greenSize() const
348
349 Accessor function for the size of the green color channel.
350*/
351
352/*!
353 \fn uchar QPixelFormat::blueSize() const
354
355 Accessor function for the size of the blue color channel.
356*/
357
358/*!
359 \fn uchar QPixelFormat::cyanSize() const
360
361 Accessor function for the cyan color channel.
362*/
363
364/*!
365 \fn uchar QPixelFormat::magentaSize() const
366
367 Accessor function for the megenta color channel.
368*/
369
370/*!
371 \fn uchar QPixelFormat::yellowSize() const
372
373 Accessor function for the yellow color channel.
374*/
375
376/*!
377 \fn uchar QPixelFormat::blackSize() const
378
379 Accessor function for the black/key color channel.
380*/
381
382/*!
383 \fn uchar QPixelFormat::hueSize() const
384
385 Accessor function for the hue channel size.
386*/
387
388/*!
389 \fn uchar QPixelFormat::saturationSize() const
390
391 Accessor function for the saturation channel size.
392*/
393
394/*!
395 \fn uchar QPixelFormat::lightnessSize() const
396
397 Accessor function for the lightness channel size.
398*/
399
400/*!
401 \fn uchar QPixelFormat::brightnessSize() const
402
403 Accessor function for the brightness channel size.
404*/
405
406/*!
407 \fn uchar QPixelFormat::alphaSize() const
408
409 Accessor function for the alpha channel size.
410*/
411
412/*!
413 \fn uchar QPixelFormat::bitsPerPixel() const
414
415 Accessor function for the bits used per pixel. This function returns the
416 sum of all the color channels + the size of the alpha channel.
417*/
418
419/*!
420 \fn AlphaPremultiplied QPixelFormat::premultiplied() const
421
422 Accessor function for the whether the alpha channel is multiplied
423 in to the color channels.
424*/
425
426/*!
427 \fn TypeInterpretation QPixelFormat::typeInterpretation() const
428
429 The type interpretation determines how each pixel should be read.
430
431 Each pixel is represented as one or more units of the given type,
432 laid out sequentially in memory.
433
434 \note The \l{byteOrder()}{byte order} of the pixel format and the
435 endianness of the host system only affect the memory layout of each
436 individual unit being read — \e{not} the relative ordering of the
437 units.
438
439 For example, QImage::Format_Mono has a \l{QImage::pixelFormat()}{pixel format}
440 of 1 bits per pixel and a QPixelFormat::UnsignedByte type interpretation,
441 which should be read as a single \c{byte}. Similarly, QImage::Format_RGB888
442 has a \l{QImage::pixelFormat()}{pixel format} of 24 bits per pixel, and
443 and a QPixelFormat::UnsignedByte type interpretation, which should be
444 read as three consecutive \c{byte}s.
445
446 Many of the QImage \l{QImage::Format}{formats} are 32-bit with a type
447 interpretation of QPixelFormat::UnsignedInteger, which should be read
448 as a single \c{unsigned int}.
449
450 For QPixelFormat::FloatingPoint formats like QImage::Format_RGBA16FPx4
451 or QImage::Format_RGBA32FPx4 the type is determined based on the size
452 of the individual color/alpha channels, with \c{qfloat16} for 16-bit
453 half-float formats and \c{float} for 32-bit full-float formats.
454
455 \sa byteOrder()
456*/
457
458/*!
459 \fn ByteOrder QPixelFormat::byteOrder() const
460
461 The byte order of the pixel format determines the memory layout of
462 the individual type units, as described by the typeInterpretation().
463
464 This function will never return QPixelFormat::CurrentSystemEndian as this
465 value is translated to the system's endian value in the constructor.
466
467 For pixel formats with typeInterpretation() QPixelFormat::UnsignedByte this
468 will typically be QPixelFormat::BigEndian, while other type interpretations
469 will typically reflect the endianness of the current system.
470
471 If the byte order of the pixel format matches the current system the
472 individual type units can be read and manipulated using the same bit
473 masks and operations, regardless of the host system endianness. For
474 example, with QImage::Format_ARGB32, which has a QPixelFormat::UnsignedInteger
475 type interpretation, the alpha can always be read by masking the
476 \c{unsigned int} by \c{0xFF000000}, regardless of the host endianness.
477
478 If the pixel format and host endianness does \e{not} match care must
479 be taken to account for this. Classes like QImage do not swap the
480 internal bits to match the host system endianness in these cases.
481
482 \sa typeInterpretation(), alphaPosition()
483*/
484
485/*!
486 \fn AlphaUsage QPixelFormat::alphaUsage() const
487
488 Accessor function for whether the alpha channel is used or not.
489
490 Sometimes the pixel format reserves place for an alpha channel,
491 so alphaSize() will return > 0, but the alpha channel is not
492 used/ignored.
493
494 For example, for QImage::Format_RGB32, the bitsPerPixel() is 32,
495 because the alpha channel has a size of 8, but alphaUsage()
496 reflects QPixelFormat::IgnoresAlpha.
497
498 Note that in such situations the \l{alphaPosition()}{position} of
499 the unused alpha channel is still important, as it affects the
500 placement of the color channels.
501
502 \sa alphaPosition(), alphaSize(), premultiplied()
503*/
504
505/*!
506 \fn AlphaPosition QPixelFormat::alphaPosition() const
507
508 Accessor function for the position of the alpha channel
509 relative to the color channels.
510
511 For formats where the individual channels map to individual
512 units, the alpha position is relative to these units. For
513 example for QImage::Format_RGBA16FPx4 which has an alpha
514 position of QPixelFormat::AtEnd, the alpha is the last
515 \c{qfloat16} read.
516
517 For formats where multiple channels are packed into a single unit,
518 the QPixelFormat::AtBeginning and QPixelFormat::AtEnd values map to
519 the most significant and least significant bits of the packed unit,
520 with respect to the format's own byteOrder().
521
522 For example, for QImage::Format_ARGB32, which has a type interpretation
523 of QPixelFormat::UnsignedInteger and a byteOrder() that always matches
524 the host system, the alpha position of QPixelFormat::AtBeginning means
525 that the alpha can always be found at \c{0xFF000000}.
526
527 If the pixel format and host endianness does \e{not} match care must be
528 taken to correctly map the pixel format layout to the host memory layout.
529
530 \sa alphaUsage(), alphaSize(), premultiplied()
531*/
532
533/*!
534 \fn YUVLayout QPixelFormat::yuvLayout() const
535
536 Accessor function for the YUVLayout. It is difficult to describe the color
537 channels of a YUV pixel format since YUV color model uses macro pixels.
538 Instead the layout of the pixels are stored as an enum.
539*/
540
541/*!
542 \fn uchar QPixelFormat::subEnum() const
543
544 Accessor for the datapart which contains subEnums
545 This is the same as the yuvLayout() function.
546
547 \sa yuvLayout()
548 \internal
549*/
550
551static_assert(sizeof(QPixelFormat) == sizeof(quint64));
552
553
554namespace QtPrivate {
562 {
564 switch (yuvLayout) {
565 case QPixelFormat::YUV444:
566 bits_per_pixel = 24;
567 break;
568 case QPixelFormat::YUV422:
569 bits_per_pixel = 16;
570 break;
571 case QPixelFormat::YUV411:
572 case QPixelFormat::YUV420P:
574 case QPixelFormat::YV12:
575 bits_per_pixel = 12;
576 break;
577 case QPixelFormat::UYVY:
578 case QPixelFormat::YUYV:
579 bits_per_pixel = 16;
580 break;
581 case QPixelFormat::NV12:
582 case QPixelFormat::NV21:
583 bits_per_pixel = 12;
584 break;
585 case QPixelFormat::IMC1:
586 case QPixelFormat::IMC2:
587 case QPixelFormat::IMC3:
588 case QPixelFormat::IMC4:
589 bits_per_pixel = 12;
590 break;
591 case QPixelFormat::Y8:
592 bits_per_pixel = 8;
593 break;
594 case QPixelFormat::Y16:
595 bits_per_pixel = 16;
596 break;
597 }
598
600 0, 0, 0, 0,
602 alphaSize,
607 byteOrder,
608 yuvLayout);
609 }
610}
611
612#ifndef QT_NO_DEBUG_STREAM
613QDebug operator<<(QDebug dbg, const QPixelFormat &f)
614{
615 QDebugStateSaver saver(dbg);
616 dbg.nospace();
617 dbg.noquote();
618 dbg.verbosity(0);
619
620 dbg << "QPixelFormat(" << f.colorModel();
621 if (f.colorModel() == QPixelFormat::YUV)
622 dbg << "," << f.yuvLayout();
623
624 dbg << ",bpp=" << f.bitsPerPixel()
625 << "," << f.typeInterpretation();
626
627 if (f.typeInterpretation() != QPixelFormat::UnsignedByte || f.bitsPerPixel() > 8)
628 dbg << "," << f.byteOrder();
629
630 if (f.colorModel() != QPixelFormat::YUV) {
631 dbg << ",ch=" << f.channelCount() << "[";
632 const int alphaSize = f.alphaSize();
633 const int colorChannels = f.channelCount() - (f.alphaSize() ? 1 : 0);
634 if (alphaSize && f.alphaPosition() == QPixelFormat::AtBeginning)
635 dbg << alphaSize << "-";
636 if (colorChannels >= 1)
637 dbg << f.get(QPixelFormat::FirstField, QPixelFormat::FirstFieldWidth);
638 if (colorChannels >= 2)
639 dbg << "-" << f.get(QPixelFormat::SecondField, QPixelFormat::SecondFieldWidth);
640 if (colorChannels >= 3)
641 dbg << "-" << f.get(QPixelFormat::ThirdField, QPixelFormat::ThirdFieldWidth);
642 if (colorChannels >= 4)
643 dbg << "-" << f.get(QPixelFormat::FourthField, QPixelFormat::FourthFieldWidth);
644 if (colorChannels >= 5)
645 dbg << "-" << f.get(QPixelFormat::FifthField, QPixelFormat::FifthFieldWidth);
646 if (alphaSize && f.alphaPosition() == QPixelFormat::AtEnd)
647 dbg << "-" << alphaSize;
648 dbg << "]";
649 }
650
651 if (f.alphaSize() > 0) {
652 dbg << "," << f.alphaUsage()
653 << "=" << f.alphaSize()
654 << "," << f.alphaPosition();
655 if (f.alphaUsage() == QPixelFormat::UsesAlpha)
656 dbg << "," << f.premultiplied();
657 } else {
658 dbg << ",NoAlpha";
659 }
660
661 dbg << ')';
662 return dbg;
663}
664#endif
665
666QT_END_NAMESPACE
667
668#include "moc_qpixelformat.cpp"
Combined button and popup list for selecting options.
QPixelFormat QPixelFormat_createYUV(QPixelFormat::YUVLayout yuvLayout, uchar alphaSize, QPixelFormat::AlphaUsage alphaUsage, QPixelFormat::AlphaPosition alphaPosition, QPixelFormat::AlphaPremultiplied premultiplied, QPixelFormat::TypeInterpretation typeInterpretation, QPixelFormat::ByteOrder byteOrder)
QDebug operator<<(QDebug dbg, const QPixelFormat &f)