diff --git a/src/rafimage.cpp b/src/rafimage.cpp index faa53045..e18e852f 100644 --- a/src/rafimage.cpp +++ b/src/rafimage.cpp @@ -10,6 +10,7 @@ #include "futils.hpp" #include "image.hpp" #include "image_int.hpp" +#include "jpgimage.hpp" #include "safe_op.hpp" #include "tiffimage.hpp" @@ -248,23 +249,32 @@ void RafImage::readMetadata() { Internal::enforce(jpg_img_len >= 12, ErrorCode::kerCorruptedMetadata); - DataBuf buf(jpg_img_len - 12); - if (io_->seek(jpg_img_off + 12, BasicIo::beg) != 0) + DataBuf jpg_buf(jpg_img_len); + if (io_->seek(jpg_img_off, BasicIo::beg) != 0) throw Error(ErrorCode::kerFailedToReadImageData); - if (!buf.empty()) { - io_->read(buf.data(), buf.size()); + if (!jpg_buf.empty()) { + io_->read(jpg_buf.data(), jpg_buf.size()); if (io_->error() || io_->eof()) throw Error(ErrorCode::kerFailedToReadImageData); } - ByteOrder bo = TiffParser::decode(exifData_, iptcData_, xmpData_, buf.c_data(), buf.size()); + // Retreive metadata from embedded JPEG preview image. + try { + auto jpg_io = std::make_unique(jpg_buf.data(), jpg_buf.size()); + auto jpg_img = JpegImage(std::move(jpg_io), false); + jpg_img.readMetadata(); + setByteOrder(jpg_img.byteOrder()); + xmpData_ = jpg_img.xmpData(); + exifData_ = jpg_img.exifData(); + iptcData_ = jpg_img.iptcData(); + comment_ = jpg_img.comment(); + } catch (const Exiv2::Error&) { + } exifData_["Exif.Image2.JPEGInterchangeFormat"] = getULong(jpg_img_offset, bigEndian); exifData_["Exif.Image2.JPEGInterchangeFormatLength"] = getULong(jpg_img_length, bigEndian); - setByteOrder(bo); - // parse the tiff byte readBuff[4]; if (io_->seek(100, BasicIo::beg) != 0) diff --git a/tests/bugfixes/github/test_issue_857.py b/tests/bugfixes/github/test_issue_857.py index 266bebcd..d0472df6 100644 --- a/tests/bugfixes/github/test_issue_857.py +++ b/tests/bugfixes/github/test_issue_857.py @@ -22,7 +22,7 @@ class OutOfMemoryInRafImageReadMetadata(metaclass=CaseMeta): $kerCorruptedMetadata """, """Exiv2 exception in print action for file $filename2: -This does not look like a TIFF image +$kerCorruptedMetadata """ ] retval = [1,1]