From d63c1d05e455921b1ea4e351e770316c3494ee63 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Wed, 24 Jul 2019 18:27:46 +0200 Subject: Read/write ICC profile in TIFF plugin Adds reading and writing of embedded color spaces on the TIFF plugin. Change-Id: I53e8a16ff65f7986e9d51a5b543335e27b43e346 Reviewed-by: Eirik Aavitsland --- src/plugins/imageformats/tiff/qtiffhandler.cpp | 19 ++++++++++++- tests/auto/tiff/tst_qtiff.cpp | 39 ++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/plugins/imageformats/tiff/qtiffhandler.cpp b/src/plugins/imageformats/tiff/qtiffhandler.cpp index 3d404bd..0d0a133 100644 --- a/src/plugins/imageformats/tiff/qtiffhandler.cpp +++ b/src/plugins/imageformats/tiff/qtiffhandler.cpp @@ -39,6 +39,7 @@ #include "qtiffhandler_p.h" #include +#include #include #include #include @@ -487,6 +488,15 @@ bool QTiffHandler::read(QImage *image) } } + uint32 count; + void *profile; + if (TIFFGetField(tiff, TIFFTAG_ICCPROFILE, &count, &profile)) { + QByteArray iccProfile(reinterpret_cast(profile), count); + image->setColorSpace(QColorSpace::fromIccProfile(iccProfile)); + } + // We do not handle colorimetric metadat not on ICC profile form, it seems to be a lot + // less common, and would need additional API in QColorSpace. + return true; } @@ -591,7 +601,14 @@ bool QTiffHandler::write(const QImage &image) TIFFClose(tiff); return false; } - + // set color space + if (image.colorSpace().isValid()) { + QByteArray iccProfile = image.colorSpace().iccProfile(); + if (!TIFFSetField(tiff, TIFFTAG_ICCPROFILE, iccProfile.size(), reinterpret_cast(iccProfile.constData()))) { + TIFFClose(tiff); + return false; + } + } // configure image depth const QImage::Format format = image.format(); if (format == QImage::Format_Mono || format == QImage::Format_MonoLSB) { diff --git a/tests/auto/tiff/tst_qtiff.cpp b/tests/auto/tiff/tst_qtiff.cpp index a8a0ef2..61d49db 100644 --- a/tests/auto/tiff/tst_qtiff.cpp +++ b/tests/auto/tiff/tst_qtiff.cpp @@ -87,6 +87,9 @@ private slots: void readRgba64(); void readGray16(); + void colorSpace_data(); + void colorSpace(); + private: QString prefix; }; @@ -627,5 +630,41 @@ void tst_qtiff::readGray16() QCOMPARE(image.format(), QImage::Format_Grayscale16); } +void tst_qtiff::colorSpace_data() +{ + QTest::addColumn("colorspaceId"); + + QTest::newRow("Undefined") << QColorSpace::Undefined; + QTest::newRow("sRGB") << QColorSpace::SRgb; + QTest::newRow("sRGB(linear)") << QColorSpace::SRgbLinear; + QTest::newRow("AdobeRGB") << QColorSpace::AdobeRgb; + QTest::newRow("DisplayP3") << QColorSpace::DisplayP3; + QTest::newRow("ProPhotoRgb") << QColorSpace::ProPhotoRgb; +} + +void tst_qtiff::colorSpace() +{ + QFETCH(QColorSpace::ColorSpaceId, colorspaceId); + + QImage image(prefix + "colorful.bmp"); + QVERIFY(!image.isNull()); + + image.setColorSpace(colorspaceId); + + QByteArray output; + QBuffer buf(&output); + QVERIFY(buf.open(QIODevice::WriteOnly)); + QImageWriter writer(&buf, "tiff"); + writer.write(image); + buf.close(); + + QVERIFY(buf.open(QIODevice::ReadOnly)); + QImageReader reader(&buf); + QImage image2 = reader.read(); + + QCOMPARE(image2.colorSpace(), QColorSpace(colorspaceId)); + QCOMPARE(image2, image); +} + QTEST_MAIN(tst_qtiff) #include "tst_qtiff.moc" -- cgit v1.2.3