Add support for saving colorspace to PNGs
Also fixes interaction with QImageReader gamma correction Change-Id: I108f253697f7ff60be6940bca17faa9f9ceb577b Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
This commit is contained in:
parent
733ca2230c
commit
83de6d0ce5
@ -611,6 +611,9 @@ bool QPngHandlerPrivate::readPngHeader()
|
||||
if (!colorSpace.isValid()) {
|
||||
qWarning() << "QPngHandler: Failed to parse ICC profile";
|
||||
} else {
|
||||
QColorSpacePrivate *csD = QColorSpacePrivate::getWritable(colorSpace);
|
||||
if (csD->description.isEmpty())
|
||||
csD->description = QString::fromLatin1((const char *)name);
|
||||
colorSpaceState = Icc;
|
||||
}
|
||||
}
|
||||
@ -680,7 +683,7 @@ bool QPngHandlerPrivate::readPngImage(QImage *outImage)
|
||||
png_set_gamma(png_ptr, 1.0f / gamma, fileGamma);
|
||||
QColorSpacePrivate *csPrivate = QColorSpacePrivate::getWritable(colorSpace);
|
||||
csPrivate->transferFunction = QColorSpace::TransferFunction::Gamma;
|
||||
csPrivate->gamma = gamma;
|
||||
csPrivate->gamma = 1.0f / gamma;
|
||||
csPrivate->setTransferFunction();
|
||||
colorSpaceState = GammaChrm;
|
||||
}
|
||||
@ -977,7 +980,24 @@ bool QPNGImageWriter::writeImage(const QImage& image, volatile int compression_i
|
||||
bpc, // per channel
|
||||
color_type, 0, 0, 0); // sets #channels
|
||||
|
||||
if (gamma != 0.0) {
|
||||
if (image.colorSpace().isValid()) {
|
||||
QColorSpace cs = image.colorSpace();
|
||||
// Support the old gamma making it override transferfunction.
|
||||
if (gamma != 0.0 && !qFuzzyCompare(cs.gamma(), 1.0f / gamma)) {
|
||||
QColorSpacePrivate *csPrivate = QColorSpacePrivate::getWritable(cs);
|
||||
csPrivate->transferFunction = QColorSpace::TransferFunction::Gamma;
|
||||
csPrivate->gamma = 1.0f / gamma;
|
||||
csPrivate->setTransferFunction();
|
||||
csPrivate->iccProfile.clear();
|
||||
csPrivate->description.clear();
|
||||
}
|
||||
QByteArray iccProfileName = QColorSpacePrivate::get(cs)->description.toLatin1();
|
||||
if (iccProfileName.isEmpty())
|
||||
iccProfileName = QByteArrayLiteral("Custom");
|
||||
QByteArray iccProfile = cs.iccProfile();
|
||||
png_set_iCCP(png_ptr, info_ptr, (png_const_charp)iccProfileName.constData(),
|
||||
PNG_COMPRESSION_TYPE_BASE, (png_const_bytep)iccProfile.constData(), iccProfile.length());
|
||||
} else if (gamma != 0.0) {
|
||||
png_set_gAMA(png_ptr, info_ptr, 1.0/gamma);
|
||||
}
|
||||
|
||||
|
@ -30,6 +30,7 @@
|
||||
#include <QtTest/QtTest>
|
||||
|
||||
#include <QBuffer>
|
||||
#include <QColorSpace>
|
||||
#include <QDebug>
|
||||
#include <QImage>
|
||||
#include <QImageReader>
|
||||
@ -158,6 +159,9 @@ private slots:
|
||||
void saveFormat_data();
|
||||
void saveFormat();
|
||||
|
||||
void saveColorSpace_data();
|
||||
void saveColorSpace();
|
||||
|
||||
void readText_data();
|
||||
void readText();
|
||||
|
||||
@ -1883,6 +1887,33 @@ void tst_QImageReader::saveFormat()
|
||||
QCOMPARE(stored, converted);
|
||||
}
|
||||
|
||||
void tst_QImageReader::saveColorSpace_data()
|
||||
{
|
||||
QTest::addColumn<QColorSpace::ColorSpaceId>("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_QImageReader::saveColorSpace()
|
||||
{
|
||||
QFETCH(QColorSpace::ColorSpaceId, colorspaceId);
|
||||
|
||||
QImage orig(":/images/kollada.png");
|
||||
|
||||
orig.setColorSpace(colorspaceId);
|
||||
QBuffer buf;
|
||||
buf.open(QIODevice::WriteOnly);
|
||||
QVERIFY(orig.save(&buf, "png"));
|
||||
buf.close();
|
||||
QImage stored = QImage::fromData(buf.buffer(), "png");
|
||||
|
||||
QCOMPARE(stored, orig);
|
||||
}
|
||||
|
||||
void tst_QImageReader::readText_data()
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user