From 478e49b8a602c2240de1367e631ab5f1f09525c6 Mon Sep 17 00:00:00 2001 From: Mirco Miranda Date: Mon, 25 Sep 2023 20:55:53 +0000 Subject: [PATCH] raw: fix multi image load Fixes not loading a second image in the file. This patch allow code like the following. QImageReader r(file); do { auto qi = r.read(); if (!qi.isNull()) { qi.save(QString("/tmp/%1_%2.tif") .arg(QFileInfo(file).baseName()) .arg(r.currentImageNumber())); } } while (r.jumpToNextImage()); m_startPos is used to reposition the device if you decide to do a subsequent read: libraw wants it to be at the beginning of the RAW stream (cherry picked from commit 18ea0492bc5b6689c9523c84dd716b3c50dc918f) --- src/imageformats/raw.cpp | 16 ++++++++++++++-- src/imageformats/raw_p.h | 6 ++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/imageformats/raw.cpp b/src/imageformats/raw.cpp index c8888fe..3b40d6b 100644 --- a/src/imageformats/raw.cpp +++ b/src/imageformats/raw.cpp @@ -443,7 +443,9 @@ void setParams(QImageIOHandler *handler, LibRaw *rawProcessor) auto &&rawparams = rawProcessor->imgdata.rawparams; #endif // Select one raw image from input file (0 - first, ...) - rawparams.shot_select = handler->currentImageNumber(); + if (handler->currentImageNumber() > -1) { + rawparams.shot_select = handler->currentImageNumber(); + } // *** Set processing parameters @@ -723,6 +725,7 @@ RAWHandler::RAWHandler() : m_imageNumber(0) , m_imageCount(0) , m_quality(-1) + , m_startPos(-1) { } @@ -739,6 +742,15 @@ bool RAWHandler::read(QImage *image) { auto dev = device(); + // set the image position after the first run. + if (!dev->isSequential()) { + if (m_startPos < 0) { + m_startPos = dev->pos(); + } else { + dev->seek(m_startPos); + } + } + // Check image file format. if (dev->atEnd()) { return false; @@ -820,7 +832,7 @@ bool RAWHandler::jumpToNextImage() bool RAWHandler::jumpToImage(int imageNumber) { - if (imageNumber >= imageCount()) { + if (imageNumber < 0 || imageNumber >= imageCount()) { return false; } m_imageNumber = imageNumber; diff --git a/src/imageformats/raw_p.h b/src/imageformats/raw_p.h index 30e8357..b4c5451 100644 --- a/src/imageformats/raw_p.h +++ b/src/imageformats/raw_p.h @@ -78,6 +78,12 @@ private: * @note It is safe to set both W and A: W is used if camera white balance is found, otherwise A is used. */ qint32 m_quality; + + /*! + * \brief m_startPos + * The initial device position to allow multi image load (cache value). + */ + qint64 m_startPos; }; class RAWPlugin : public QImageIOPlugin