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
4#include "qpixelformat.h"
5
7
8/*!
9 \class QPixelFormat
10 \inmodule QtGui
11 \since 5.4
12 \brief QPixelFormat is a class for describing different pixel
13 layouts in graphics buffers.
14
15 In Qt there is a often a need to represent the layout of the pixels in a
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.
19
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.
24
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.
28*/
29
30/*!
31 \enum QPixelFormat::ColorModel
32
33 This enum describes the \l{colorModel()}{color model} of the pixel format.
34
35 \value RGB The color model is RGB.
36
37 \value BGR This is logically the opposite endian version of RGB. However,
38 for ease of use it has its own model.
39
40 \value Indexed The color model uses a color palette.
41
42 \value Grayscale The color model is Grayscale.
43
44 \value CMYK The color model is CMYK.
45
46 \value HSL The color model is HSL.
47
48 \value HSV The color model is HSV.
49
50 \value YUV The color model is YUV.
51
52 \value Alpha [since 5.5] There is no color model, only alpha is used.
53*/
54
55/*!
56 \enum QPixelFormat::AlphaUsage
57
58 This enum describes the \l{alphaUsage()}{alpha usage} of the pixel format.
59
60 \value IgnoresAlpha The alpha channel is not used.
61
62 \value UsesAlpha The alpha channel is used.
63
64 \sa alphaSize(), alphaPosition(), premultiplied()
65*/
66
67/*!
68 \enum QPixelFormat::AlphaPosition
69
70 This enum describes the \l{alphaPosition()}{alpha position} of the pixel format.
71
72 \value AtBeginning The alpha channel will be put in front of the color
73 channels. E.g. ARGB.
74
75 \value AtEnd The alpha channel will be put in the back of the color
76 channels. E.g. RGBA.
77
78 \sa alphaSize(), alphaUsage(), premultiplied()
79*/
80
81/*!
82 \enum QPixelFormat::AlphaPremultiplied
83
84 This enum describes whether the alpha channel of the pixel format is
85 \l{premultiplied}{premultiplied} into the color channels or not.
86
87 \value NotPremultiplied The alpha channel is not multiplied into the color channels.
88
89 \value Premultiplied The alpha channel is multiplied into the color channels.
90
91 \sa alphaSize(), alphaUsage(), alphaPosition()
92*/
93
94/*!
95 \enum QPixelFormat::TypeInterpretation
96
97 This enum describes the \l{typeInterpretation()}{type interpretation} of the pixel format.
98
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.
106
107 \sa byteOrder()
108*/
109
110/*!
111 \enum QPixelFormat::ByteOrder
112
113 This enum describes the \l{byteOrder()}{byte order} of the pixel format.
114
115 \value LittleEndian The byte order is little endian.
116 \value BigEndian The byte order is big endian.
117 \value CurrentSystemEndian This enum will not be stored, but is converted in
118 the constructor to the endian enum that matches
119 the enum of the current system.
120
121 \sa typeInterpretation()
122*/
123
124/*!
125 \enum QPixelFormat::YUVLayout
126
127 This enum describes the \l{yuvLayout()}{YUV layout} of the pixel format,
128 given that it has a color model of QPixelFormat::YUV.
129
130 \value YUV444
131 \value YUV422
132 \value YUV411
133 \value YUV420P
134 \value YUV420SP
135 \value YV12
136 \value UYVY
137 \value YUYV
138 \value NV12
139 \value NV21
140 \value IMC1
141 \value IMC2
142 \value IMC3
143 \value IMC4
144 \value Y8
145 \value Y16
146*/
147
148/*!
149 \fn QPixelFormat::QPixelFormat()
150
151 Creates a null pixelformat. This format maps to QImage::Format_Invalid.
152*/
153
154/*!
155 \fn QPixelFormat::QPixelFormat(ColorModel colorModel,
156 uchar firstSize,
157 uchar secondSize,
158 uchar thirdSize,
159 uchar fourthSize,
160 uchar fifthSize,
161 uchar alphaSize,
162 AlphaUsage alphaUsage,
163 AlphaPosition alphaPosition,
164 AlphaPremultiplied premultiplied,
165 TypeInterpretation typeInterpretation,
166 ByteOrder byteOrder = CurrentSystemEndian,
167 uchar subEnum = 0)
168
169 Creates a QPixelFormat which assigns its data to the attributes.
170 \a colorModel will be put into a buffer which is 4 bits long.
171
172 \a firstSize \a secondSize \a thirdSize \a fourthSize \a fifthSize \a
173 alphaSize are all meant to represent the size of a channel. The channels will
174 be used for different uses dependent on the \a colorModel. For RGB the
175 firstSize will represent the Red channel. On CMYK it will represent the
176 value of the Cyan channel.
177
178 \a alphaUsage represents if the alpha channel is used or not.
179
180 \a alphaPosition is the position of the alpha channel.
181
182 \a premultiplied represents if the alpha channel is already multiplied with
183 the color channels.
184
185 \a typeInterpretation is how the pixel is interpreted.
186
187 \a byteOrder represents the endianness of the pixelformat. This defaults to
188 CurrentSystemEndian, which will be resolve to the system's endianness for
189 non-byte-ordered formats, and QPixelFormat::BigEndian for QPixelFormat::UnsignedByte.
190
191 \a subEnum is used for colorModels that have to store some extra
192 information with supplying an extra enum. This is used by YUV to store the
193 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.
197*/
198
199/*!
200 \fn QPixelFormat qPixelFormatRgba(uchar redSize,
201 uchar greenSize,
202 uchar blueSize,
203 uchar alphaSize,
204 QPixelFormat::AlphaUsage alphaUsage,
205 QPixelFormat::AlphaPosition alphaPosition,
206 QPixelFormat::AlphaPremultiplied premultiplied = QPixelFormat::NotPremultiplied,
207 QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)
208 \relates QPixelFormat
209
210 Constructor function making an RGB pixelformat. \a redSize \a greenSize \a
211 blueSize represent the size of each color channel. \a alphaSize describes
212 the alpha channel size and its position is described with \a alphaPosition.
213 \a alphaUsage is used to determine if the alpha channel is used or not.
214 Setting the alpha channel size to 8 and alphaUsage to IgnoresAlpha is how
215 it is possible to create a 32 bit format where the rgb channels only use 24
216 bits combined. \a premultiplied \a typeInterpretation are
217 accessible with accessors with the same name.
218
219 \sa QPixelFormat::TypeInterpretation
220*/
221
222/*!
223 \fn QPixelFormat qPixelFormatGrayscale(uchar channelSize,
224 QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)
225 \relates QPixelFormat
226
227 Constructor function for creating a Grayscale format. Monochrome formats can be
228 described by passing 1 to \a channelSize. Its also possible to define very
229 accurate grayscale formats using doubles to describe each pixel by passing 8
230 as \a channelSize and FloatingPoint as \a typeInterpretation.
231
232 \sa QPixelFormat::TypeInterpretation
233*/
234
235/*!
236 \fn QPixelFormat qPixelFormatAlpha(uchar channelSize,
237 QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)
238 \relates QPixelFormat
239 \since 5.5
240
241 Constructor function for creating an Alpha format. A mask format can be
242 described by passing 1 to \a channelSize. Its also possible to define very
243 accurate alpha formats using doubles to describe each pixel by passing 8
244 as \a channelSize and FloatingPoint as \a typeInterpretation.
245
246 \sa QPixelFormat::TypeInterpretation
247*/
248
249
250/*!
251 \fn QPixelFormat qPixelFormatCmyk(uchar channelSize,
252 uchar alphaSize = 0,
253 QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha,
254 QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning,
255 QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedInteger)
256 \relates QPixelFormat
257
258 Constructor function for creating CMYK formats. The channel count will be 4 or
259 5 depending on if \a alphaSize is bigger than zero or not. The CMYK color
260 channels will all be set to the value of \a channelSize.
261
262 \a alphaUsage \a alphaPosition and \a typeInterpretation are all accessible with
263 the accessors with the same name.
264
265 \sa QPixelFormat::TypeInterpretation
266*/
267
268/*!
269 \fn QPixelFormat qPixelFormatHsl(uchar channelSize,
270 uchar alphaSize = 0,
271 QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha,
272 QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning,
273 QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::FloatingPoint)
274 \relates QPixelFormat
275
276 Constructor function for creating HSL formats. The channel count will be 3 or 4
277 depending on if \a alphaSize is bigger than 0.
278
279 \a channelSize will set the hueSize saturationSize and lightnessSize to the same value.
280
281 \a alphaUsage \a alphaPosition and \a typeInterpretation are all accessible with
282 the accessors with the same name.
283*/
284
285/*!
286 \fn QPixelFormat qPixelFormatHsv(uchar channelSize,
287 uchar alphaSize = 0,
288 QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha,
289 QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning,
290 QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::FloatingPoint)
291 \relates QPixelFormat
292
293 Constructor function for creating HSV formats. The channel count will be 3 or 4
294 depending on if \a alphaSize is bigger than 0.
295
296 \a channelSize will set the hueSize saturationSize and brightnessSize to the same value.
297
298 \a alphaUsage \a alphaPosition and \a typeInterpretation are all accessible with
299 the accessors with the same name.
300*/
301
302/*!
303 \fn QPixelFormat qPixelFormatYuv(QPixelFormat::YUVLayout yuvLayout,
304 uchar alphaSize = 0,
305 QPixelFormat::AlphaUsage alphaUsage = QPixelFormat::IgnoresAlpha,
306 QPixelFormat::AlphaPosition alphaPosition = QPixelFormat::AtBeginning,
307 QPixelFormat::AlphaPremultiplied premultiplied = QPixelFormat::NotPremultiplied,
308 QPixelFormat::TypeInterpretation typeInterpretation = QPixelFormat::UnsignedByte,
309 QPixelFormat::ByteOrder byteOrder = QPixelFormat::BigEndian)
310 \relates QPixelFormat
311
312 Constructor function for creating a QPixelFormat describing a YUV format with
313 \a yuvLayout. \a alphaSize describes the size of a potential alpha channel
314 and is position is described with \a alphaPosition. The "first" "second" ..
315 "fifth" channels are all set to 0. \a alphaUsage \a premultiplied \a
316 typeInterpretation and \a byteOrder will work as with other formats.
317*/
318
319/*!
320 \fn ColorModel QPixelFormat::colorModel() const
321
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.
327*/
328
329/*!
330 \fn uchar QPixelFormat::channelCount() const
331
332 Accessor function for the channel count.
333
334 The channel count represents channels (color and alpha) with a size > 0.
335*/
336
337/*!
338 \fn uchar QPixelFormat::redSize() const
339
340 Accessor function for the size of the red color channel.
341*/
342
343/*!
344 \fn uchar QPixelFormat::greenSize() const
345
346 Accessor function for the size of the green color channel.
347*/
348
349/*!
350 \fn uchar QPixelFormat::blueSize() const
351
352 Accessor function for the size of the blue color channel.
353*/
354
355/*!
356 \fn uchar QPixelFormat::cyanSize() const
357
358 Accessor function for the cyan color channel.
359*/
360
361/*!
362 \fn uchar QPixelFormat::magentaSize() const
363
364 Accessor function for the megenta color channel.
365*/
366
367/*!
368 \fn uchar QPixelFormat::yellowSize() const
369
370 Accessor function for the yellow color channel.
371*/
372
373/*!
374 \fn uchar QPixelFormat::blackSize() const
375
376 Accessor function for the black/key color channel.
377*/
378
379/*!
380 \fn uchar QPixelFormat::hueSize() const
381
382 Accessor function for the hue channel size.
383*/
384
385/*!
386 \fn uchar QPixelFormat::saturationSize() const
387
388 Accessor function for the saturation channel size.
389*/
390
391/*!
392 \fn uchar QPixelFormat::lightnessSize() const
393
394 Accessor function for the lightness channel size.
395*/
396
397/*!
398 \fn uchar QPixelFormat::brightnessSize() const
399
400 Accessor function for the brightness channel size.
401*/
402
403/*!
404 \fn uchar QPixelFormat::alphaSize() const
405
406 Accessor function for the alpha channel size.
407*/
408
409/*!
410 \fn uchar QPixelFormat::bitsPerPixel() const
411
412 Accessor function for the bits used per pixel. This function returns the
413 sum of all the color channels + the size of the alpha channel.
414*/
415
416/*!
417 \fn AlphaPremultiplied QPixelFormat::premultiplied() const
418
419 Accessor function for the whether the alpha channel is multiplied
420 in to the color channels.
421*/
422
423/*!
424 \fn TypeInterpretation QPixelFormat::typeInterpretation() const
425
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.
451
452 \sa byteOrder()
453*/
454
455/*!
456 \fn ByteOrder QPixelFormat::byteOrder() const
457
458 The byte order of the pixel format determines the memory layout of
459 the individual type units, as described by the typeInterpretation().
460
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
465 will typically be QPixelFormat::BigEndian, while other type interpretations
466 will typically reflect the endianness of the current system.
467
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()
480*/
481
482/*!
483 \fn AlphaUsage QPixelFormat::alphaUsage() const
484
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()
500*/
501
502/*!
503 \fn AlphaPosition QPixelFormat::alphaPosition() const
504
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()
528*/
529
530/*!
531 \fn YUVLayout QPixelFormat::yuvLayout() const
532
533 Accessor function for the YUVLayout. It is difficult to describe the color
534 channels of a YUV pixel format since YUV color model uses macro pixels.
535 Instead the layout of the pixels are stored as an enum.
536*/
537
538/*!
539 \fn uchar QPixelFormat::subEnum() const
540
541 Accessor for the datapart which contains subEnums
542 This is the same as the yuvLayout() function.
543
544 \sa yuvLayout()
545 \internal
546*/
547
548static_assert(sizeof(QPixelFormat) == sizeof(quint64));
549
550
551namespace QtPrivate {
559 {
561 switch (yuvLayout) {
562 case QPixelFormat::YUV444:
563 bits_per_pixel = 24;
564 break;
565 case QPixelFormat::YUV422:
566 bits_per_pixel = 16;
567 break;
568 case QPixelFormat::YUV411:
569 case QPixelFormat::YUV420P:
571 case QPixelFormat::YV12:
572 bits_per_pixel = 12;
573 break;
574 case QPixelFormat::UYVY:
575 case QPixelFormat::YUYV:
576 bits_per_pixel = 16;
577 break;
578 case QPixelFormat::NV12:
579 case QPixelFormat::NV21:
580 bits_per_pixel = 12;
581 break;
582 case QPixelFormat::IMC1:
583 case QPixelFormat::IMC2:
584 case QPixelFormat::IMC3:
585 case QPixelFormat::IMC4:
586 bits_per_pixel = 12;
587 break;
588 case QPixelFormat::Y8:
589 bits_per_pixel = 8;
590 break;
591 case QPixelFormat::Y16:
592 bits_per_pixel = 16;
593 break;
594 }
595
597 0, 0, 0, 0,
599 alphaSize,
604 byteOrder,
605 yuvLayout);
606 }
607}
608
609#ifndef QT_NO_DEBUG_STREAM
610QDebug operator<<(QDebug dbg, const QPixelFormat &f)
611{
612 QDebugStateSaver saver(dbg);
613 dbg.nospace();
614 dbg.noquote();
615 dbg.verbosity(0);
616
617 dbg << "QPixelFormat(" << f.colorModel();
618 if (f.colorModel() == QPixelFormat::YUV)
619 dbg << "," << f.yuvLayout();
620
621 dbg << ",bpp=" << f.bitsPerPixel()
622 << "," << f.typeInterpretation();
623
624 if (f.typeInterpretation() != QPixelFormat::UnsignedByte || f.bitsPerPixel() > 8)
625 dbg << "," << f.byteOrder();
626
627 if (f.colorModel() != QPixelFormat::YUV) {
628 dbg << ",ch=" << f.channelCount() << "[";
629 const int alphaSize = f.alphaSize();
630 const int colorChannels = f.channelCount() - (f.alphaSize() ? 1 : 0);
631 if (alphaSize && f.alphaPosition() == QPixelFormat::AtBeginning)
632 dbg << alphaSize << "-";
633 if (colorChannels >= 1)
634 dbg << f.get(QPixelFormat::FirstField, QPixelFormat::FirstFieldWidth);
635 if (colorChannels >= 2)
636 dbg << "-" << f.get(QPixelFormat::SecondField, QPixelFormat::SecondFieldWidth);
637 if (colorChannels >= 3)
638 dbg << "-" << f.get(QPixelFormat::ThirdField, QPixelFormat::ThirdFieldWidth);
639 if (colorChannels >= 4)
640 dbg << "-" << f.get(QPixelFormat::FourthField, QPixelFormat::FourthFieldWidth);
641 if (colorChannels >= 5)
642 dbg << "-" << f.get(QPixelFormat::FifthField, QPixelFormat::FifthFieldWidth);
643 if (alphaSize && f.alphaPosition() == QPixelFormat::AtEnd)
644 dbg << "-" << alphaSize;
645 dbg << "]";
646 }
647
648 if (f.alphaSize() > 0) {
649 dbg << "," << f.alphaUsage()
650 << "=" << f.alphaSize()
651 << "," << f.alphaPosition();
652 if (f.alphaUsage() == QPixelFormat::UsesAlpha)
653 dbg << "," << f.premultiplied();
654 } else {
655 dbg << ",NoAlpha";
656 }
657
658 dbg << ')';
659 return dbg;
660}
661#endif
662
663QT_END_NAMESPACE
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)