From 18ea0492bc5b6689c9523c84dd716b3c50dc918f 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 --- 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 b220a36..a84152c 100644 --- a/src/imageformats/raw.cpp +++ b/src/imageformats/raw.cpp @@ -444,7 +444,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 @@ -722,6 +724,7 @@ RAWHandler::RAWHandler() : m_imageNumber(0) , m_imageCount(0) , m_quality(-1) + , m_startPos(-1) { } @@ -738,6 +741,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; @@ -819,7 +831,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 1f3aab7..bf62058 100644 --- a/src/imageformats/raw_p.h +++ b/src/imageformats/raw_p.h @@ -74,6 +74,12 @@ private: * When the quality is -1, default quality 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