tests/auto/qimage/tst_qimage.cpp
changeset 37 758a864f9613
parent 33 3e2da88830cd
equal deleted inserted replaced
36:ef0373b55136 37:758a864f9613
    81     void alphaChannel();
    81     void alphaChannel();
    82 
    82 
    83     void convertToFormat_data();
    83     void convertToFormat_data();
    84     void convertToFormat();
    84     void convertToFormat();
    85 
    85 
       
    86     void convertToFormatRgb888ToRGB32();
       
    87 
    86     void createAlphaMask_data();
    88     void createAlphaMask_data();
    87     void createAlphaMask();
    89     void createAlphaMask();
    88 #ifndef QT_NO_IMAGE_HEURISTIC_MASK
    90 #ifndef QT_NO_IMAGE_HEURISTIC_MASK
    89     void createHeuristicMask();
    91     void createHeuristicMask();
    90 #endif
    92 #endif
   135     void nullSize();
   137     void nullSize();
   136 
   138 
   137     void premultipliedAlphaConsistency();
   139     void premultipliedAlphaConsistency();
   138 
   140 
   139     void compareIndexed();
   141     void compareIndexed();
       
   142 
       
   143     void rgbSwapped_data();
       
   144     void rgbSwapped();
   140 };
   145 };
   141 
   146 
   142 tst_QImage::tst_QImage()
   147 tst_QImage::tst_QImage()
   143 
   148 
   144 {
   149 {
   795     expected2.save("expected2.xpm", "XPM");
   800     expected2.save("expected2.xpm", "XPM");
   796 
   801 
   797     QCOMPARE(result2, expected2);
   802     QCOMPARE(result2, expected2);
   798     QFile::remove(QLatin1String("result2.xpm"));
   803     QFile::remove(QLatin1String("result2.xpm"));
   799     QFile::remove(QLatin1String("expected2.xpm"));
   804     QFile::remove(QLatin1String("expected2.xpm"));
       
   805 }
       
   806 
       
   807 void tst_QImage::convertToFormatRgb888ToRGB32()
       
   808 {
       
   809     // 545 so width % 4 != 0. This ensure there is padding at the end of the scanlines
       
   810     const int height = 545;
       
   811     const int width = 545;
       
   812     QImage source(width, height, QImage::Format_RGB888);
       
   813     for (int y = 0; y < height; ++y) {
       
   814         uchar *srcPixels = source.scanLine(y);
       
   815         for (int x = 0; x < width * 3; ++x)
       
   816             srcPixels[x] = x;
       
   817     }
       
   818 
       
   819     QImage rgb32Image = source.convertToFormat(QImage::Format_RGB888);
       
   820     QCOMPARE(rgb32Image.format(), QImage::Format_RGB888);
       
   821     for (int x = 0; x < width; ++x) {
       
   822         for (int y = 0; y < height; ++y)
       
   823             QCOMPARE(rgb32Image.pixel(x, y), source.pixel(x, y));
       
   824     }
   800 }
   825 }
   801 
   826 
   802 void tst_QImage::createAlphaMask_data()
   827 void tst_QImage::createAlphaMask_data()
   803 {
   828 {
   804     QTest::addColumn<int>("x");
   829     QTest::addColumn<int>("x");
  1424 static inline int rand8()
  1449 static inline int rand8()
  1425 {
  1450 {
  1426     return int(256. * (qrand() / (RAND_MAX + 1.0)));
  1451     return int(256. * (qrand() / (RAND_MAX + 1.0)));
  1427 }
  1452 }
  1428 
  1453 
  1429 static inline bool compare(int a, int b, int tolerance)
       
  1430 {
       
  1431     return qAbs(a - b) <= tolerance;
       
  1432 }
       
  1433 
       
  1434 // compares img.scale against the bilinear filtering used by QPainter
  1454 // compares img.scale against the bilinear filtering used by QPainter
  1435 void tst_QImage::smoothScale3()
  1455 void tst_QImage::smoothScale3()
  1436 {
  1456 {
  1437     QImage img(128, 128, QImage::Format_RGB32);
  1457     QImage img(128, 128, QImage::Format_RGB32);
  1438     for (int y = 0; y < img.height(); ++y) {
  1458     for (int y = 0; y < img.height(); ++y) {
  1456         QPainter p(&b);
  1476         QPainter p(&b);
  1457         p.setRenderHint(QPainter::SmoothPixmapTransform);
  1477         p.setRenderHint(QPainter::SmoothPixmapTransform);
  1458         p.scale(scales[i], scales[i]);
  1478         p.scale(scales[i], scales[i]);
  1459         p.drawImage(0, 0, img);
  1479         p.drawImage(0, 0, img);
  1460         p.end();
  1480         p.end();
       
  1481         int err = 0;
  1461 
  1482 
  1462         for (int y = 0; y < a.height(); ++y) {
  1483         for (int y = 0; y < a.height(); ++y) {
  1463             for (int x = 0; x < a.width(); ++x) {
  1484             for (int x = 0; x < a.width(); ++x) {
  1464                 QRgb ca = a.pixel(x, y);
  1485                 QRgb ca = a.pixel(x, y);
  1465                 QRgb cb = b.pixel(x, y);
  1486                 QRgb cb = b.pixel(x, y);
  1466 
  1487 
  1467                 // tolerate a little bit of rounding errors
  1488                 // tolerate a little bit of rounding errors
  1468                 QVERIFY(compare(qRed(ca), qRed(cb), 3));
  1489                 bool r = true;
  1469                 QVERIFY(compare(qGreen(ca), qGreen(cb), 3));
  1490                 r &= qAbs(qRed(ca) - qRed(cb)) <= 18;
  1470                 QVERIFY(compare(qBlue(ca), qBlue(cb), 3));
  1491                 r &= qAbs(qGreen(ca) - qGreen(cb)) <= 18;
       
  1492                 r &= qAbs(qBlue(ca) - qBlue(cb)) <= 18;
       
  1493                 if (!r)
       
  1494                     err++;
  1471             }
  1495             }
  1472         }
  1496         }
       
  1497         QCOMPARE(err, 0);
  1473     }
  1498     }
  1474 }
  1499 }
  1475 
  1500 
  1476 void tst_QImage::smoothScaleBig()
  1501 void tst_QImage::smoothScaleBig()
  1477 {
  1502 {
  1796     }
  1821     }
  1797 
  1822 
  1798     QCOMPARE(img, imgInverted);
  1823     QCOMPARE(img, imgInverted);
  1799 }
  1824 }
  1800 
  1825 
       
  1826 void tst_QImage::rgbSwapped_data()
       
  1827 {
       
  1828     QTest::addColumn<QImage::Format>("format");
       
  1829 
       
  1830     QTest::newRow("Format_Indexed8") << QImage::Format_Indexed8;
       
  1831     QTest::newRow("Format_RGB32") << QImage::Format_RGB32;
       
  1832     QTest::newRow("Format_ARGB32") << QImage::Format_ARGB32;
       
  1833     QTest::newRow("Format_ARGB32_Premultiplied") << QImage::Format_ARGB32_Premultiplied;
       
  1834     QTest::newRow("Format_RGB16") << QImage::Format_RGB16;
       
  1835     QTest::newRow("Format_ARGB8565_Premultiplied") << QImage::Format_ARGB8565_Premultiplied;
       
  1836     QTest::newRow("Format_ARGB6666_Premultiplied") << QImage::Format_ARGB6666_Premultiplied;
       
  1837     QTest::newRow("Format_ARGB4444_Premultiplied") << QImage::Format_ARGB4444_Premultiplied;
       
  1838     QTest::newRow("Format_RGB666") << QImage::Format_RGB666;
       
  1839     QTest::newRow("Format_RGB555") << QImage::Format_RGB555;
       
  1840     QTest::newRow("Format_ARGB8555_Premultiplied") << QImage::Format_ARGB8555_Premultiplied;
       
  1841     QTest::newRow("Format_RGB888") << QImage::Format_RGB888;
       
  1842     QTest::newRow("Format_RGB444") << QImage::Format_RGB444;
       
  1843 }
       
  1844 
       
  1845 void tst_QImage::rgbSwapped()
       
  1846 {
       
  1847     QFETCH(QImage::Format, format);
       
  1848 
       
  1849     QImage image(100, 1, format);
       
  1850     image.fill(0);
       
  1851 
       
  1852     QVector<QColor> testColor(image.width());
       
  1853 
       
  1854     for (int i = 0; i < image.width(); ++i)
       
  1855         testColor[i] = QColor(i, 10 + i, 20 + i * 2, 30 + i);
       
  1856 
       
  1857     if (format != QImage::Format_Indexed8) {
       
  1858         QPainter p(&image);
       
  1859         p.setCompositionMode(QPainter::CompositionMode_Source);
       
  1860         for (int i = 0; i < image.width(); ++i)
       
  1861             p.fillRect(QRect(i, 0, 1, 1), testColor[i].rgb());
       
  1862     } else {
       
  1863         image.setColorCount(image.width());
       
  1864         for (int i = 0; i < image.width(); ++i) {
       
  1865             image.setColor(0, testColor[i].rgba());
       
  1866             image.setPixel(i, 0, i);
       
  1867         }
       
  1868     }
       
  1869 
       
  1870     QImage imageSwapped = image.rgbSwapped();
       
  1871 
       
  1872     for (int i = 0; i < image.width(); ++i) {
       
  1873         QColor referenceColor = QColor(image.pixel(i, 0));
       
  1874         QColor swappedColor = QColor(imageSwapped.pixel(i, 0));
       
  1875 
       
  1876         QCOMPARE(swappedColor.alpha(), referenceColor.alpha());
       
  1877         QCOMPARE(swappedColor.red(), referenceColor.blue());
       
  1878         QCOMPARE(swappedColor.green(), referenceColor.green());
       
  1879         QCOMPARE(swappedColor.blue(), referenceColor.red());
       
  1880     }
       
  1881 
       
  1882     QImage imageSwappedTwice = imageSwapped.rgbSwapped();
       
  1883 
       
  1884     QCOMPARE(image, imageSwappedTwice);
       
  1885 
       
  1886     QCOMPARE(memcmp(image.constBits(), imageSwappedTwice.constBits(), image.numBytes()), 0);
       
  1887 }
       
  1888 
  1801 QTEST_MAIN(tst_QImage)
  1889 QTEST_MAIN(tst_QImage)
  1802 #include "tst_qimage.moc"
  1890 #include "tst_qimage.moc"