From cd92d76e9dcd98f4fc974c796453459779393bdc Mon Sep 17 00:00:00 2001 From: Eirik Aavitsland Date: Wed, 4 Jan 2023 18:06:57 +0100 Subject: Implement support for file memory mapping for tiff reading libtiff will by default attempt to establish a memory map for reading a tiff file. Implement the callbacks to establish this in Qt's tiff handler, since this will save data copying, particularly in the case where the input file is already in memory as a resource or QBuffer. Also, this makes sure that QTiffHandler utilizes libtiff's default, and hence best tested, code path for tiff decoding. Specifically, it avoids a hitting a bug that breaks reading of certain tiffs in the newly released libtiff version 4.5.0. Pick-to: 6.5 6.4 6.2 5.15 Change-Id: Id6a746546e069da9910cacd4a4996c669c72cbab Reviewed-by: Dmitry Shachnev Reviewed-by: Allan Sandfeld Jensen --- src/plugins/imageformats/tiff/qtiffhandler.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/plugins/imageformats/tiff/qtiffhandler.cpp b/src/plugins/imageformats/tiff/qtiffhandler.cpp index fa84364..d01e888 100644 --- a/src/plugins/imageformats/tiff/qtiffhandler.cpp +++ b/src/plugins/imageformats/tiff/qtiffhandler.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include extern "C" { #include "tiffio.h" @@ -57,13 +59,33 @@ toff_t qtiffSizeProc(thandle_t fd) return static_cast(fd)->size(); } -int qtiffMapProc(thandle_t /*fd*/, tdata_t* /*pbase*/, toff_t* /*psize*/) +int qtiffMapProc(thandle_t fd, void **base, toff_t *size) { + QIODevice *device = static_cast(fd); + + QFileDevice *file = qobject_cast(device); + if (file) { + *base = file->map(0, file->size()); + if (*base != nullptr) { + *size = file->size(); + return 1; + } + } else { + QBuffer *buf = qobject_cast(device); + if (buf) { + *base = const_cast(buf->data().constData()); + *size = buf->size(); + return 1; + } + } return 0; } -void qtiffUnmapProc(thandle_t /*fd*/, tdata_t /*base*/, toff_t /*size*/) +void qtiffUnmapProc(thandle_t fd, void *base, toff_t /*size*/) { + QFileDevice *file = qobject_cast(static_cast(fd)); + if (file && base) + file->unmap(static_cast(base)); } -- cgit v1.2.3