| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | #include <QtTest/QtTest> |
| 29 | |
| 30 | #include <QtGui/qpixelformat.h> |
| 31 | |
| 32 | class tst_QPixelFormat : public QObject |
| 33 | { |
| 34 | Q_OBJECT |
| 35 | |
| 36 | private slots: |
| 37 | void testOperators(); |
| 38 | void testQVectorOfFormats(); |
| 39 | void testRGB(); |
| 40 | void testCMYK(); |
| 41 | void testHSLandHSV(); |
| 42 | void testYUV_data(); |
| 43 | void testYUV(); |
| 44 | void testEnums(); |
| 45 | }; |
| 46 | |
| 47 | void tst_QPixelFormat::testOperators() |
| 48 | { |
| 49 | QPixelFormat first = qPixelFormatRgba(red: 8,green: 8,blue: 8,alfa: 8,usage: QPixelFormat::UsesAlpha, position: QPixelFormat::AtBeginning, pmul: QPixelFormat::Premultiplied); |
| 50 | QPixelFormat second = qPixelFormatRgba(red: 8,green: 8,blue: 8,alfa: 8,usage: QPixelFormat::UsesAlpha, position: QPixelFormat::AtBeginning, pmul: QPixelFormat::Premultiplied); |
| 51 | QCOMPARE(first, second); |
| 52 | |
| 53 | QPixelFormat third = qPixelFormatRgba(red: 8,green: 8,blue: 8,alfa: 8,usage: QPixelFormat::UsesAlpha, position: QPixelFormat::AtEnd, pmul: QPixelFormat::NotPremultiplied); |
| 54 | QVERIFY(first != third); |
| 55 | } |
| 56 | |
| 57 | void tst_QPixelFormat::testQVectorOfFormats() |
| 58 | { |
| 59 | QVector<QPixelFormat> reallocedVector; |
| 60 | QVector<QPixelFormat> reservedVector; |
| 61 | reservedVector.reserve(asize: QImage::NImageFormats); |
| 62 | for (int i = 0; i < QImage::NImageFormats; i++) { |
| 63 | if (i == 0 || i == 2) // skip invalid and monolsb |
| 64 | continue; |
| 65 | QImage::Format image_format = static_cast<QImage::Format>(i); |
| 66 | QPixelFormat format = QImage::toPixelFormat(format: image_format); |
| 67 | reallocedVector.append(t: format); |
| 68 | reservedVector.append(t: format); |
| 69 | } |
| 70 | |
| 71 | for (int i = 0; i < reallocedVector.size(); i++) { |
| 72 | QCOMPARE(reallocedVector.at(i), reservedVector.at(i)); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void tst_QPixelFormat::testRGB() |
| 77 | { |
| 78 | QPixelFormat argb8888 = qPixelFormatRgba(red: 8,green: 8,blue: 8,alfa: 8,usage: QPixelFormat::UsesAlpha,position: QPixelFormat::AtBeginning, pmul: QPixelFormat::Premultiplied); |
| 79 | QCOMPARE(argb8888.redSize(), uchar(8)); |
| 80 | QCOMPARE(argb8888.greenSize(), uchar(8)); |
| 81 | QCOMPARE(argb8888.blueSize(), uchar(8)); |
| 82 | QCOMPARE(argb8888.alphaSize(), uchar(8)); |
| 83 | |
| 84 | QPixelFormat rgb565 = qPixelFormatRgba(red: 5,green: 6,blue: 5,alfa: 0,usage: QPixelFormat::IgnoresAlpha,position: QPixelFormat::AtBeginning, pmul: QPixelFormat::NotPremultiplied); |
| 85 | QCOMPARE(rgb565.redSize(), uchar(5)); |
| 86 | QCOMPARE(rgb565.greenSize(), uchar(6)); |
| 87 | QCOMPARE(rgb565.blueSize(), uchar(5)); |
| 88 | QCOMPARE(rgb565.alphaSize(), uchar(0)); |
| 89 | QCOMPARE(rgb565.bitsPerPixel(), uchar(16)); |
| 90 | |
| 91 | QPixelFormat rgba1235 = qPixelFormatRgba(red: 1,green: 2,blue: 3,alfa: 5,usage: QPixelFormat::IgnoresAlpha, position: QPixelFormat::AtEnd, pmul: QPixelFormat::Premultiplied); |
| 92 | QCOMPARE(rgba1235.redSize(), uchar(1)); |
| 93 | QCOMPARE(rgba1235.greenSize(), uchar(2)); |
| 94 | QCOMPARE(rgba1235.blueSize(), uchar(3)); |
| 95 | QCOMPARE(rgba1235.alphaSize(), uchar(5)); |
| 96 | QCOMPARE(rgba1235.bitsPerPixel(), uchar(1 + 2 + 3 + 5)); |
| 97 | } |
| 98 | |
| 99 | void tst_QPixelFormat::testCMYK() |
| 100 | { |
| 101 | QPixelFormat cmyk6 = qPixelFormatCmyk(channelSize: 6); |
| 102 | QCOMPARE(cmyk6.cyanSize(), uchar(6)); |
| 103 | QCOMPARE(cmyk6.magentaSize(), uchar(6)); |
| 104 | QCOMPARE(cmyk6.yellowSize(), uchar(6)); |
| 105 | QCOMPARE(cmyk6.blackSize(), uchar(6)); |
| 106 | QCOMPARE(cmyk6.bitsPerPixel(), uchar(6*4)); |
| 107 | |
| 108 | QPixelFormat cmykWithAlpha = qPixelFormatCmyk(channelSize: 8,alfa: 8); |
| 109 | QCOMPARE(cmykWithAlpha.bitsPerPixel(), uchar(8*5)); |
| 110 | } |
| 111 | void tst_QPixelFormat::testHSLandHSV() |
| 112 | { |
| 113 | QPixelFormat hsl = qPixelFormatHsl(channelSize: 3,alfa: 5); |
| 114 | |
| 115 | QCOMPARE(hsl.hueSize(), uchar(3)); |
| 116 | QCOMPARE(hsl.saturationSize(), uchar(3)); |
| 117 | QCOMPARE(hsl.lightnessSize(), uchar(3)); |
| 118 | QCOMPARE(hsl.bitsPerPixel(), uchar(3 * 3 + 5)); |
| 119 | |
| 120 | QPixelFormat hsv = qPixelFormatHsv(channelSize: 5,alfa: 7); |
| 121 | |
| 122 | QCOMPARE(hsv.hueSize(), uchar(5)); |
| 123 | QCOMPARE(hsv.saturationSize(), uchar(5)); |
| 124 | QCOMPARE(hsv.brightnessSize(), uchar(5)); |
| 125 | QCOMPARE(hsv.bitsPerPixel(), uchar(5 * 3 + 7)); |
| 126 | } |
| 127 | |
| 128 | Q_DECLARE_METATYPE(QPixelFormat::YUVLayout) |
| 129 | void tst_QPixelFormat::testYUV_data() |
| 130 | { |
| 131 | QTest::addColumn<QPixelFormat::YUVLayout>(name: "yuv_layout" ); |
| 132 | QTest::newRow(dataTag: "YUV Layout YUV444" ) << QPixelFormat::YUV444; |
| 133 | QTest::newRow(dataTag: "YUV Layout YUV422" ) << QPixelFormat::YUV422; |
| 134 | QTest::newRow(dataTag: "YUV Layout YUV411" ) << QPixelFormat::YUV411; |
| 135 | QTest::newRow(dataTag: "YUV Layout YUV420P" ) << QPixelFormat::YUV420P; |
| 136 | QTest::newRow(dataTag: "YUV Layout YUV420SP" ) << QPixelFormat::YUV420SP; |
| 137 | QTest::newRow(dataTag: "YUV Layout YV12" ) << QPixelFormat::YV12; |
| 138 | QTest::newRow(dataTag: "YUV Layout UYVY" ) << QPixelFormat::UYVY; |
| 139 | QTest::newRow(dataTag: "YUV Layout YUYV" ) << QPixelFormat::YUYV; |
| 140 | QTest::newRow(dataTag: "YUV Layout NV12" ) << QPixelFormat::NV12; |
| 141 | QTest::newRow(dataTag: "YUV Layout NV21" ) << QPixelFormat::NV21; |
| 142 | QTest::newRow(dataTag: "YUV Layout IMC1" ) << QPixelFormat::IMC1; |
| 143 | QTest::newRow(dataTag: "YUV Layout IMC2" ) << QPixelFormat::IMC2; |
| 144 | QTest::newRow(dataTag: "YUV Layout IMC3" ) << QPixelFormat::IMC3; |
| 145 | QTest::newRow(dataTag: "YUV Layout IMC4" ) << QPixelFormat::IMC4; |
| 146 | QTest::newRow(dataTag: "YUV Layout Y8" ) << QPixelFormat::Y8; |
| 147 | QTest::newRow(dataTag: "YUV Layout Y16" ) << QPixelFormat::Y16; |
| 148 | } |
| 149 | |
| 150 | void tst_QPixelFormat::testYUV() |
| 151 | { |
| 152 | QFETCH(QPixelFormat::YUVLayout, yuv_layout); |
| 153 | |
| 154 | QPixelFormat format = qPixelFormatYuv(layout: yuv_layout, alfa: 0); |
| 155 | |
| 156 | switch (yuv_layout) { |
| 157 | case QPixelFormat::YUV444: |
| 158 | QCOMPARE(format.bitsPerPixel(), uchar(24)); |
| 159 | break; |
| 160 | case QPixelFormat::YUV422: |
| 161 | QCOMPARE(format.bitsPerPixel(), uchar(16)); |
| 162 | break; |
| 163 | case QPixelFormat::YUV411: |
| 164 | case QPixelFormat::YUV420P: |
| 165 | case QPixelFormat::YUV420SP: |
| 166 | case QPixelFormat::YV12: |
| 167 | QCOMPARE(format.bitsPerPixel(), uchar(12)); |
| 168 | break; |
| 169 | case QPixelFormat::UYVY: |
| 170 | case QPixelFormat::YUYV: |
| 171 | QCOMPARE(format.bitsPerPixel(), uchar(16)); |
| 172 | break; |
| 173 | case QPixelFormat::NV12: |
| 174 | case QPixelFormat::NV21: |
| 175 | QCOMPARE(format.bitsPerPixel(), uchar(12)); |
| 176 | break; |
| 177 | case QPixelFormat::IMC1: |
| 178 | case QPixelFormat::IMC2: |
| 179 | case QPixelFormat::IMC3: |
| 180 | case QPixelFormat::IMC4: |
| 181 | QCOMPARE(format.bitsPerPixel(), uchar(12)); |
| 182 | break; |
| 183 | case QPixelFormat::Y8: |
| 184 | QCOMPARE(format.bitsPerPixel(), uchar(8)); |
| 185 | break; |
| 186 | case QPixelFormat::Y16: |
| 187 | QCOMPARE(format.bitsPerPixel(), uchar(16)); |
| 188 | break; |
| 189 | default: |
| 190 | QVERIFY(!"the value stored for the yuvLayout is wrong!" ); |
| 191 | } |
| 192 | |
| 193 | } |
| 194 | |
| 195 | void tst_QPixelFormat::testEnums() |
| 196 | { |
| 197 | QPixelFormat allSet = QPixelFormat(QPixelFormat::BGR,1,2,3,4,5,6, |
| 198 | QPixelFormat::UsesAlpha, |
| 199 | QPixelFormat::AtEnd, |
| 200 | QPixelFormat::Premultiplied, |
| 201 | QPixelFormat::FloatingPoint, |
| 202 | QPixelFormat::BigEndian, |
| 203 | (1 << 6) - 1); |
| 204 | |
| 205 | QCOMPARE(allSet.alphaUsage(), QPixelFormat::UsesAlpha); |
| 206 | QCOMPARE(allSet.alphaPosition(), QPixelFormat::AtEnd); |
| 207 | QCOMPARE(allSet.premultiplied(), QPixelFormat::Premultiplied); |
| 208 | QCOMPARE(allSet.byteOrder(), QPixelFormat::BigEndian); |
| 209 | QCOMPARE(allSet.typeInterpretation(), QPixelFormat::FloatingPoint); |
| 210 | QCOMPARE(allSet.byteOrder(), QPixelFormat::BigEndian); |
| 211 | QCOMPARE(allSet.subEnum(), uchar(63)); |
| 212 | |
| 213 | QPixelFormat nonSet = QPixelFormat(QPixelFormat::RGB,6,5,4,3,2,1, |
| 214 | QPixelFormat::IgnoresAlpha, |
| 215 | QPixelFormat::AtBeginning, |
| 216 | QPixelFormat::NotPremultiplied, |
| 217 | QPixelFormat::UnsignedInteger, |
| 218 | QPixelFormat::LittleEndian); |
| 219 | |
| 220 | QCOMPARE(nonSet.alphaUsage(), QPixelFormat::IgnoresAlpha); |
| 221 | QCOMPARE(nonSet.alphaPosition(), QPixelFormat::AtBeginning); |
| 222 | QCOMPARE(nonSet.premultiplied(), QPixelFormat::NotPremultiplied); |
| 223 | QCOMPARE(nonSet.byteOrder(), QPixelFormat::LittleEndian); |
| 224 | QCOMPARE(nonSet.typeInterpretation(), QPixelFormat::UnsignedInteger); |
| 225 | QCOMPARE(nonSet.byteOrder(), QPixelFormat::LittleEndian); |
| 226 | QCOMPARE(nonSet.subEnum(), uchar(0)); |
| 227 | } |
| 228 | |
| 229 | #include <tst_qpixelformat.moc> |
| 230 | QTEST_MAIN(tst_QPixelFormat); |
| 231 | |