Factor out qt_getImageText() and qt_getImageTextFromDescription()
... and re-use them in QImageReader, QJpegHandler, QPngHandler. Change-Id: Iec89e47205f3c420e1e7eb4a2d3c1fbfe887fd8c Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>bb10
parent
4cb3c3e15a
commit
7682542b4b
|
|
@ -5216,4 +5216,33 @@ Q_GUI_EXPORT void qt_imageTransform(QImage &src, QImageIOHandler::Transformation
|
|||
}
|
||||
}
|
||||
|
||||
QMap<QString, QString> qt_getImageText(const QImage &image, const QString &description)
|
||||
{
|
||||
QMap<QString, QString> text = qt_getImageTextFromDescription(description);
|
||||
const auto textKeys = image.textKeys();
|
||||
for (const QString &key : textKeys) {
|
||||
if (!key.isEmpty() && !text.contains(key))
|
||||
text.insert(key, image.text(key));
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
QMap<QString, QString> qt_getImageTextFromDescription(const QString &description)
|
||||
{
|
||||
QMap<QString, QString> text;
|
||||
const auto pairs = description.splitRef(QLatin1String("\n\n"));
|
||||
for (const QStringRef &pair : pairs) {
|
||||
int index = pair.indexOf(QLatin1Char(':'));
|
||||
if (index >= 0 && pair.indexOf(QLatin1Char(' ')) < index) {
|
||||
if (!pair.trimmed().isEmpty())
|
||||
text.insert(QLatin1String("Description"), pair.toString().simplified());
|
||||
} else {
|
||||
const QStringRef key = pair.left(index);
|
||||
if (!key.trimmed().isEmpty())
|
||||
text.insert(key.toString(), pair.mid(index + 2).toString().simplified());
|
||||
}
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
|
|
|||
|
|
@ -212,6 +212,9 @@ inline QImage::Format qt_alphaVersionForPainting(QImage::Format format)
|
|||
return toFormat;
|
||||
}
|
||||
|
||||
Q_GUI_EXPORT QMap<QString, QString> qt_getImageText(const QImage &image, const QString &description);
|
||||
Q_GUI_EXPORT QMap<QString, QString> qt_getImageTextFromDescription(const QString &description);
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
||||
#endif // QIMAGE_P_H
|
||||
|
|
|
|||
|
|
@ -147,6 +147,9 @@
|
|||
#include <private/qfactoryloader_p.h>
|
||||
#include <QMutexLocker>
|
||||
|
||||
// for qt_getImageText
|
||||
#include <private/qimage_p.h>
|
||||
|
||||
// image handlers
|
||||
#include <private/qbmphandler_p.h>
|
||||
#include <private/qppmhandler_p.h>
|
||||
|
|
@ -592,18 +595,8 @@ bool QImageReaderPrivate::initHandler()
|
|||
*/
|
||||
void QImageReaderPrivate::getText()
|
||||
{
|
||||
if (!text.isEmpty() || (!handler && !initHandler()) || !handler->supportsOption(QImageIOHandler::Description))
|
||||
return;
|
||||
foreach (const QString &pair, handler->option(QImageIOHandler::Description).toString().split(
|
||||
QLatin1String("\n\n"))) {
|
||||
int index = pair.indexOf(QLatin1Char(':'));
|
||||
if (index >= 0 && pair.indexOf(QLatin1Char(' ')) < index) {
|
||||
text.insert(QLatin1String("Description"), pair.simplified());
|
||||
} else {
|
||||
QString key = pair.left(index);
|
||||
text.insert(key, pair.mid(index + 2).simplified());
|
||||
}
|
||||
}
|
||||
if (text.isEmpty() && (handler || initHandler()) && handler->supportsOption(QImageIOHandler::Description))
|
||||
text = qt_getImageTextFromDescription(handler->option(QImageIOHandler::Description).toString());
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
|
|||
|
|
@ -49,6 +49,8 @@
|
|||
#include <qvariant.h>
|
||||
#include <qvector.h>
|
||||
|
||||
#include <private/qimage_p.h> // for qt_getImageText
|
||||
|
||||
#include <png.h>
|
||||
#include <pngconf.h>
|
||||
|
||||
|
|
@ -718,27 +720,10 @@ void QPNGImageWriter::setGamma(float g)
|
|||
gamma = g;
|
||||
}
|
||||
|
||||
|
||||
static void set_text(const QImage &image, png_structp png_ptr, png_infop info_ptr,
|
||||
const QString &description)
|
||||
{
|
||||
QMap<QString, QString> text;
|
||||
foreach (const QString &key, image.textKeys()) {
|
||||
if (!key.isEmpty())
|
||||
text.insert(key, image.text(key));
|
||||
}
|
||||
foreach (const QString &pair, description.split(QLatin1String("\n\n"))) {
|
||||
int index = pair.indexOf(QLatin1Char(':'));
|
||||
if (index >= 0 && pair.indexOf(QLatin1Char(' ')) < index) {
|
||||
QString s = pair.simplified();
|
||||
if (!s.isEmpty())
|
||||
text.insert(QLatin1String("Description"), s);
|
||||
} else {
|
||||
QString key = pair.left(index);
|
||||
if (!key.simplified().isEmpty())
|
||||
text.insert(key, pair.mid(index + 2).simplified());
|
||||
}
|
||||
}
|
||||
const QMap<QString, QString> text = qt_getImageText(image, description);
|
||||
|
||||
if (text.isEmpty())
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
TARGET = qjpeg
|
||||
|
||||
QT += core-private
|
||||
QT += core-private gui-private
|
||||
|
||||
SOURCES += main.cpp qjpeghandler.cpp
|
||||
HEADERS += main.h qjpeghandler_p.h
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@
|
|||
#include <qbuffer.h>
|
||||
#include <qmath.h>
|
||||
#include <private/qsimd_p.h>
|
||||
#include <private/qimage_p.h> // for qt_getImageText
|
||||
|
||||
#include <stdio.h> // jpeglib needs this to be pre-included
|
||||
#include <setjmp.h>
|
||||
|
|
@ -491,30 +492,10 @@ inline my_jpeg_destination_mgr::my_jpeg_destination_mgr(QIODevice *device)
|
|||
free_in_buffer = max_buf;
|
||||
}
|
||||
|
||||
|
||||
static inline void set_text(const QImage &image, j_compress_ptr cinfo, const QString &description)
|
||||
{
|
||||
QMap<QString, QString> text;
|
||||
foreach (const QString &key, image.textKeys()) {
|
||||
if (!key.isEmpty())
|
||||
text.insert(key, image.text(key));
|
||||
}
|
||||
foreach (const QString &pair, description.split(QLatin1String("\n\n"))) {
|
||||
int index = pair.indexOf(QLatin1Char(':'));
|
||||
if (index >= 0 && pair.indexOf(QLatin1Char(' ')) < index) {
|
||||
QString s = pair.simplified();
|
||||
if (!s.isEmpty())
|
||||
text.insert(QLatin1String("Description"), s);
|
||||
} else {
|
||||
QString key = pair.left(index);
|
||||
if (!key.simplified().isEmpty())
|
||||
text.insert(key, pair.mid(index + 2).simplified());
|
||||
}
|
||||
}
|
||||
if (text.isEmpty())
|
||||
return;
|
||||
|
||||
for (QMap<QString, QString>::ConstIterator it = text.constBegin(); it != text.constEnd(); ++it) {
|
||||
const QMap<QString, QString> text = qt_getImageText(image, description);
|
||||
for (auto it = text.begin(), end = text.end(); it != end; ++it) {
|
||||
QByteArray comment = it.key().toLatin1();
|
||||
if (!comment.isEmpty())
|
||||
comment += ": ";
|
||||
|
|
|
|||
Loading…
Reference in New Issue