From d42e644daf975160f46550abc8923c08df22ae7e Mon Sep 17 00:00:00 2001 From: aavit Date: Fri, 29 Oct 2010 13:35:31 +0200 Subject: Fixes: corruption of whitespace in text fields when reading PNG images Task-number: QTBUG-14398 Reviewed-by: Kim --- src/gui/image/qpnghandler.cpp | 24 +++-------- tests/auto/qimagereader/tst_qimagereader.cpp | 59 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 19 deletions(-) diff --git a/src/gui/image/qpnghandler.cpp b/src/gui/image/qpnghandler.cpp index 935aba0..05d2f5b 100644 --- a/src/gui/image/qpnghandler.cpp +++ b/src/gui/image/qpnghandler.cpp @@ -331,6 +331,7 @@ public: float gamma; int quality; QString description; + QStringList readTexts; png_struct *png_ptr; png_info *info_ptr; @@ -408,6 +409,8 @@ bool Q_INTERNAL_WIN_NO_THROW QPngHandlerPrivate::readPngHeader() if (!description.isEmpty()) description += QLatin1String("\n\n"); description += key + QLatin1String(": ") + value.simplified(); + readTexts.append(key); + readTexts.append(value); text_ptr++; } #endif @@ -485,25 +488,8 @@ bool Q_INTERNAL_WIN_NO_THROW QPngHandlerPrivate::readPngImage(QImage *outImage) outImage->setDotsPerMeterX(png_get_x_pixels_per_meter(png_ptr,info_ptr)); outImage->setDotsPerMeterY(png_get_y_pixels_per_meter(png_ptr,info_ptr)); -#ifndef QT_NO_IMAGE_TEXT - png_textp text_ptr; - int num_text=0; - png_get_text(png_ptr,info_ptr,&text_ptr,&num_text); - while (num_text--) { - outImage->setText(text_ptr->key,0,QString::fromAscii(text_ptr->text)); - text_ptr++; - } - - foreach (const QString &pair, description.split(QLatin1String("\n\n"))) { - int index = pair.indexOf(QLatin1Char(':')); - if (index >= 0 && pair.indexOf(QLatin1Char(' ')) < index) { - outImage->setText(QLatin1String("Description"), pair.simplified()); - } else { - QString key = pair.left(index); - outImage->setText(key, pair.mid(index + 2).simplified()); - } - } -#endif + for (int i = 0; i < readTexts.size()-1; i+=2) + outImage->setText(readTexts.at(i), readTexts.at(i+1)); png_read_end(png_ptr, end_info); png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); diff --git a/tests/auto/qimagereader/tst_qimagereader.cpp b/tests/auto/qimagereader/tst_qimagereader.cpp index 4b4bdd6..149cc84 100644 --- a/tests/auto/qimagereader/tst_qimagereader.cpp +++ b/tests/auto/qimagereader/tst_qimagereader.cpp @@ -180,6 +180,9 @@ private slots: void saveFormat_data(); void saveFormat(); + + void preserveTexts_data(); + void preserveTexts(); }; static const QLatin1String prefix(SRCDIR "/images/"); @@ -1948,5 +1951,61 @@ void tst_QImageReader::saveFormat() } +void tst_QImageReader::preserveTexts_data() +{ + QTest::addColumn("text"); + + QTest::newRow("Simple") << "simpletext"; + QTest::newRow("Whitespace") << " A text with whitespace "; + QTest::newRow("Newline") << "A text\nwith newlines\n"; + QTest::newRow("Double newlines") << "A text\n\nwith double newlines\n\n"; + QTest::newRow("Long") << QString("A rather long text, at least after many repetitions. ").repeated(100); + +#if 0 + // Depends on iTXt support in libpng + QString latin1set; + for(int c = 0x20; c <= 0xff; c++) + latin1set.append(QLatin1Char(c)); + QTest::newRow("All Latin1 chars") << latin1set; + QTest::newRow("Multibyte string") << QString::fromUtf8("\341\233\222\341\233\226\341\232\251\341\232\271\341\232\242\341\233\232\341\232\240"); +#endif +} + + +void tst_QImageReader::preserveTexts() +{ + QFETCH(QString, text); + QString key("testkey"); + QString key2("testkey2"); + QString text2("Some other text."); + QString key3("testkey3"); + QString text3("Some more other text."); + + QImage img(":/images/kollada.png"); + img.setText(key, text); + img.setText(key2, text2); + QBuffer buf; + buf.open(QIODevice::WriteOnly); + QVERIFY(img.save(&buf, "png")); + buf.close(); + QImage stored = QImage::fromData(buf.buffer(), "png"); + QCOMPARE(stored.text(key), text); + QCOMPARE(stored.text(key2), text2); + + QImage img2(":/images/kollada.png"); + img2.setText(key3, text3); + QBuffer buf2; + QImageWriter w(&buf2, "png"); + w.setText(key, text); + w.setText(key2, text2); + QVERIFY(w.write(img2)); + buf2.close(); + QImageReader r(&buf2, "png"); + QCOMPARE(r.text(key), text.simplified()); + QCOMPARE(r.text(key2), text2.simplified()); + QCOMPARE(r.text(key3), text3.simplified()); +} + + QTEST_MAIN(tst_QImageReader) #include "tst_qimagereader.moc" -- cgit v0.12