summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/plugins/imageformats/tiff/qtiffhandler.cpp20
-rw-r--r--tests/auto/qimagewriter/tst_qimagewriter.cpp38
2 files changed, 56 insertions, 2 deletions
diff --git a/src/plugins/imageformats/tiff/qtiffhandler.cpp b/src/plugins/imageformats/tiff/qtiffhandler.cpp
index 518e6d1..77dfeb3 100644
--- a/src/plugins/imageformats/tiff/qtiffhandler.cpp
+++ b/src/plugins/imageformats/tiff/qtiffhandler.cpp
@@ -168,7 +168,7 @@ bool QTiffHandler::read(QImage *image)
break;
default:
// do nothing as defaults have already
- // been set within the QImage class
+ // been set within the QImage class
break;
}
for (uint32 y=0; y<height; ++y)
@@ -218,6 +218,24 @@ bool QTiffHandler::write(const QImage &image)
return false;
}
+ // set the resolution
+ bool resolutionSet = false;
+ const int dotPerMeterX = image.dotsPerMeterX();
+ const int dotPerMeterY = image.dotsPerMeterY();
+ if ((dotPerMeterX % 100) == 0
+ && (dotPerMeterY % 100) == 0) {
+ resolutionSet = TIFFSetField(tiff, TIFFTAG_RESOLUTIONUNIT, RESUNIT_CENTIMETER)
+ && TIFFSetField(tiff, TIFFTAG_XRESOLUTION, dotPerMeterX/100.0)
+ && TIFFSetField(tiff, TIFFTAG_YRESOLUTION, dotPerMeterY/100.0);
+ } else {
+ resolutionSet = TIFFSetField(tiff, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH)
+ && TIFFSetField(tiff, TIFFTAG_XRESOLUTION, static_cast<float>(image.logicalDpiX()))
+ && TIFFSetField(tiff, TIFFTAG_YRESOLUTION, static_cast<float>(image.logicalDpiY()));
+ }
+ if (!resolutionSet) {
+ TIFFClose(tiff);
+ return false;
+ }
// try to do the ARGB32 conversion in chunks no greater than 16 MB
int chunks = (width * height * 4 / (1024 * 1024 * 16)) + 1;
int chunkHeight = qMax(height / chunks, 1);
diff --git a/tests/auto/qimagewriter/tst_qimagewriter.cpp b/tests/auto/qimagewriter/tst_qimagewriter.cpp
index 878d398..70d9e70 100644
--- a/tests/auto/qimagewriter/tst_qimagewriter.cpp
+++ b/tests/auto/qimagewriter/tst_qimagewriter.cpp
@@ -97,6 +97,9 @@ private slots:
void saveWithNoFormat_data();
void saveWithNoFormat();
+ void resolution_data();
+ void resolution();
+
void saveToTemporaryFile();
};
@@ -162,7 +165,7 @@ tst_QImageWriter::tst_QImageWriter()
tst_QImageWriter::~tst_QImageWriter()
{
- QDir dir("images");
+ QDir dir(prefix + QLatin1String("images"));
QStringList filesToDelete = dir.entryList(QStringList() << "gen-*" , QDir::NoDotAndDotDot | QDir::Files);
foreach( QString file, filesToDelete) {
QFile::remove(dir.absoluteFilePath(file));
@@ -530,6 +533,39 @@ void tst_QImageWriter::saveWithNoFormat()
QVERIFY2(!outImage.isNull(), qPrintable(reader.errorString()));
}
+void tst_QImageWriter::resolution_data()
+{
+ QTest::addColumn<QString>("filename");
+ QTest::addColumn<int>("expectedDotsPerMeterX");
+ QTest::addColumn<int>("expectedDotsPerMeterY");
+#if defined QTEST_HAVE_TIFF
+ QTest::newRow("TIFF: 100 dpi") << ("image_100dpi.tif") << qRound(100 * (100 / 2.54)) << qRound(100 * (100 / 2.54));
+ QTest::newRow("TIFF: 50 dpi") << ("image_50dpi.tif") << qRound(50 * (100 / 2.54)) << qRound(50 * (100 / 2.54));
+ QTest::newRow("TIFF: 300 dot per meter") << ("image_300dpm.tif") << 300 << 300;
+#endif
+}
+
+void tst_QImageWriter::resolution()
+{
+ QFETCH(QString, filename);
+ QFETCH(int, expectedDotsPerMeterX);
+ QFETCH(int, expectedDotsPerMeterY);
+
+ QImage image(prefix + QLatin1String("colorful.bmp"));
+ image.setDotsPerMeterX(expectedDotsPerMeterX);
+ image.setDotsPerMeterY(expectedDotsPerMeterY);
+ const QString generatedFilepath = prefix + "gen-" + filename;
+ {
+ QImageWriter writer(generatedFilepath);
+ QVERIFY(writer.write(image));
+ }
+ QImageReader reader(generatedFilepath);
+ const QImage generatedImage = reader.read();
+
+ QCOMPARE(expectedDotsPerMeterX, generatedImage.dotsPerMeterX());
+ QCOMPARE(expectedDotsPerMeterY, generatedImage.dotsPerMeterY());
+}
+
void tst_QImageWriter::saveToTemporaryFile()
{
QImage image(prefix + "kollada.png");