From 2cd4123661d6f97a8c0889d7a4c5907fa8fbb456 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sun, 11 Mar 2018 15:04:55 +0100 Subject: [PATCH] QImage/Jpeg: decode/encode comment attributes as utf-8 Although not defined, the jpeg comment field 'content' is treated as utf-8 nowadys. At least exiftool and exiv2 (tested via gwenview) are expecting utf-8 here. So we should do the same. Task-number: QTBUG-44709 Change-Id: If84dafac3e337c7993f09cd59792e721977c9adb Reviewed-by: Eirik Aavitsland --- .../imageformats/jpeg/qjpeghandler.cpp | 6 ++-- .../qimage/images/jpeg_exif_utf8_comment.jpg | Bin 0 -> 705 bytes tests/auto/gui/image/qimage/tst_qimage.cpp | 29 ++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 tests/auto/gui/image/qimage/images/jpeg_exif_utf8_comment.jpg diff --git a/src/plugins/imageformats/jpeg/qjpeghandler.cpp b/src/plugins/imageformats/jpeg/qjpeghandler.cpp index 1cdc28c077..e52a19703c 100644 --- a/src/plugins/imageformats/jpeg/qjpeghandler.cpp +++ b/src/plugins/imageformats/jpeg/qjpeghandler.cpp @@ -493,10 +493,10 @@ static inline void set_text(const QImage &image, j_compress_ptr cinfo, const QSt { const QMap text = qt_getImageText(image, description); for (auto it = text.begin(), end = text.end(); it != end; ++it) { - QByteArray comment = it.key().toLatin1(); + QByteArray comment = it.key().toUtf8(); if (!comment.isEmpty()) comment += ": "; - comment += it.value().toLatin1(); + comment += it.value().toUtf8(); if (comment.length() > 65530) comment.truncate(65530); jpeg_write_marker(cinfo, JPEG_COM, (const JOCTET *)comment.constData(), comment.size()); @@ -904,7 +904,7 @@ bool QJpegHandlerPrivate::readJpegHeader(QIODevice *device) for (jpeg_saved_marker_ptr marker = info.marker_list; marker != NULL; marker = marker->next) { if (marker->marker == JPEG_COM) { QString key, value; - QString s = QString::fromLatin1((const char *)marker->data, marker->data_length); + QString s = QString::fromUtf8((const char *)marker->data, marker->data_length); int index = s.indexOf(QLatin1String(": ")); if (index == -1 || s.indexOf(QLatin1Char(' ')) < index) { key = QLatin1String("Description"); diff --git a/tests/auto/gui/image/qimage/images/jpeg_exif_utf8_comment.jpg b/tests/auto/gui/image/qimage/images/jpeg_exif_utf8_comment.jpg new file mode 100644 index 0000000000000000000000000000000000000000..42b305f5b8b6668df71742a8b80e8b4600907478 GIT binary patch literal 705 zcmex=9gBeAI1O5yOd!!3vB zJZf6w@c%Z0GXn=38#@~-2Rl1ECnpCNj|eXhH#d)@kTAc9tdzW*tdxw5f{LEHf|8E1 zjEsi4rjCK3iHV84x}~j!k&T|Qi4n*UMovyn9&R29US0_!MHxjSlEME241yet42%rS zj7khlf{e_9jQ@`?hyp#sfC|`v!Xg6bq6|P$F`&oM#97%vss+(Rfgy8?frpt9Xb-a> WgFVAsM&4DZmW=Y~8v@M#Zvp_w{yN40 literal 0 HcmV?d00001 diff --git a/tests/auto/gui/image/qimage/tst_qimage.cpp b/tests/auto/gui/image/qimage/tst_qimage.cpp index 7ad4a9e9bb..55a670eb6f 100644 --- a/tests/auto/gui/image/qimage/tst_qimage.cpp +++ b/tests/auto/gui/image/qimage/tst_qimage.cpp @@ -192,6 +192,7 @@ private slots: void exif_QTBUG45865(); void exifInvalidData_data(); void exifInvalidData(); + void exifReadComments(); void cleanupFunctions(); @@ -3066,6 +3067,34 @@ void tst_QImage::exifInvalidData() QVERIFY(!image.isNull()); } +void tst_QImage::exifReadComments() +{ + QImage image; + QVERIFY(image.load(m_prefix + "jpeg_exif_utf8_comment.jpg")); + QVERIFY(!image.isNull()); + QCOMPARE(image.textKeys().size(), 1); + QCOMPARE(image.textKeys().first(), "Description"); + // check if exif comment is read as utf-8 + QCOMPARE(image.text("Description"), QString::fromUtf8("some unicode chars: ÖÄÜ€@")); + + QByteArray ba; + { + QBuffer buf(&ba); + QVERIFY(buf.open(QIODevice::WriteOnly)); + QVERIFY(image.save(&buf, "JPG")); + } + QVERIFY(!ba.isEmpty()); + image = QImage(); + QCOMPARE(image.textKeys().size(), 0); + { + QBuffer buf(&ba); + QVERIFY(buf.open(QIODevice::ReadOnly)); + QVERIFY(image.load(&buf, "JPG")); + } + // compare written (and reread) description text + QCOMPARE(image.text("Description"), QString::fromUtf8("some unicode chars: ÖÄÜ€@")); +} + static void cleanupFunction(void* info) { bool *called = static_cast(info);