summaryrefslogtreecommitdiffstats
path: root/src/plugins/imageformats/jpeg/qjpeghandler.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/imageformats/jpeg/qjpeghandler.cpp')
-rw-r--r--src/plugins/imageformats/jpeg/qjpeghandler.cpp43
1 files changed, 40 insertions, 3 deletions
diff --git a/src/plugins/imageformats/jpeg/qjpeghandler.cpp b/src/plugins/imageformats/jpeg/qjpeghandler.cpp
index aed2900356..1f1675e490 100644
--- a/src/plugins/imageformats/jpeg/qjpeghandler.cpp
+++ b/src/plugins/imageformats/jpeg/qjpeghandler.cpp
@@ -40,10 +40,14 @@
#include "qjpeghandler_p.h"
#include <qimage.h>
+#include <qcolorspace.h>
+#include <qcolortransform.h>
+#include <qdebug.h>
#include <qvariant.h>
#include <qvector.h>
#include <qbuffer.h>
#include <qmath.h>
+#include <private/qicc_p.h>
#include <private/qsimd_p.h>
#include <private/qimage_p.h> // for qt_getImageText
@@ -488,6 +492,8 @@ inline my_jpeg_destination_mgr::my_jpeg_destination_mgr(QIODevice *device)
free_in_buffer = max_buf;
}
+static constexpr int maxMarkerSize = 65533;
+
static inline void set_text(const QImage &image, j_compress_ptr cinfo, const QString &description)
{
const QMap<QString, QString> text = qt_getImageText(image, description);
@@ -496,12 +502,33 @@ static inline void set_text(const QImage &image, j_compress_ptr cinfo, const QSt
if (!comment.isEmpty())
comment += ": ";
comment += it.value().toUtf8();
- if (comment.length() > 65530)
- comment.truncate(65530);
+ if (comment.length() > maxMarkerSize)
+ comment.truncate(maxMarkerSize);
jpeg_write_marker(cinfo, JPEG_COM, (const JOCTET *)comment.constData(), comment.size());
}
}
+static inline void write_icc_profile(const QImage &image, j_compress_ptr cinfo)
+{
+ const QByteArray iccProfile = image.colorSpace().iccProfile();
+ if (iccProfile.isEmpty())
+ return;
+
+ const QByteArray iccSignature("ICC_PROFILE", 12);
+ constexpr int maxIccMarkerSize = maxMarkerSize - (12 + 2);
+ int index = 0;
+ const int markers = (iccProfile.size() + (maxIccMarkerSize - 1)) / maxIccMarkerSize;
+ Q_ASSERT(markers < 256);
+ for (int marker = 1; marker <= markers; ++marker) {
+ const int len = std::min(iccProfile.size() - index, maxIccMarkerSize);
+ const QByteArray block = iccSignature
+ + QByteArray(1, char(marker)) + QByteArray(1, char(markers))
+ + iccProfile.mid(index, len);
+ jpeg_write_marker(cinfo, JPEG_APP0 + 2, reinterpret_cast<const JOCTET *>(block.constData()), block.size());
+ index += len;
+ }
+}
+
static bool write_jpeg_image(const QImage &image, QIODevice *device, volatile int sourceQuality, const QString &description, bool optimize, bool progressive)
{
bool success = false;
@@ -582,6 +609,8 @@ static bool write_jpeg_image(const QImage &image, QIODevice *device, volatile in
jpeg_start_compress(&cinfo, TRUE);
set_text(image, &cinfo, description);
+ if (cinfo.in_color_space == JCS_RGB)
+ write_icc_profile(image, &cinfo);
row_pointer[0] = new uchar[cinfo.image_width*cinfo.input_components];
int w = cinfo.image_width;
@@ -725,6 +754,7 @@ public:
QRect clipRect;
QString description;
QStringList readTexts;
+ QByteArray iccProfile;
struct jpeg_decompress_struct info;
struct my_jpeg_source_mgr * iod_src;
@@ -887,6 +917,7 @@ bool QJpegHandlerPrivate::readJpegHeader(QIODevice *device)
if (!setjmp(err.setjmp_buffer)) {
jpeg_save_markers(&info, JPEG_COM, 0xFFFF);
jpeg_save_markers(&info, JPEG_APP0 + 1, 0xFFFF); // Exif uses APP1 marker
+ jpeg_save_markers(&info, JPEG_APP0 + 2, 0xFFFF); // ICC uses APP2 marker
(void) jpeg_read_header(&info, TRUE);
@@ -919,6 +950,10 @@ bool QJpegHandlerPrivate::readJpegHeader(QIODevice *device)
readTexts.append(value);
} else if (marker->marker == JPEG_APP0 + 1) {
exifData.append((const char*)marker->data, marker->data_length);
+ } else if (marker->marker == JPEG_APP0 + 2) {
+ if (marker->data_length > 128 + 4 + 14 && strcmp((const char *)marker->data, "ICC_PROFILE") == 0) {
+ iccProfile.append((const char*)marker->data + 14, marker->data_length - 14);
+ }
}
}
@@ -954,6 +989,9 @@ bool QJpegHandlerPrivate::read(QImage *image)
for (int i = 0; i < readTexts.size()-1; i+=2)
image->setText(readTexts.at(i), readTexts.at(i+1));
+ if (!iccProfile.isEmpty())
+ image->setColorSpace(QColorSpace::fromIccProfile(iccProfile));
+
state = ReadingEnd;
return true;
}
@@ -962,7 +1000,6 @@ bool QJpegHandlerPrivate::read(QImage *image)
}
return false;
-
}
Q_GUI_EXPORT void QT_FASTCALL qt_convert_rgb888_to_rgb32_neon(quint32 *dst, const uchar *src, int len);