From c92ac6ecdab29ab1273894e8379e9cfa6c335fac Mon Sep 17 00:00:00 2001 From: GM Date: Wed, 11 Nov 2026 00:45:58 -0400 Subject: [PATCH 1/6] Thumbnail camera RAW from its embedded JPEG A CR2, NEF, NRW, ARW, SR2, DNG or PEF showed a blank tile unless a RAW thumbnailer was installed. The camera stores a finished JPEG in each, and a bounded reader of the TIFF directories now finds the largest one. An installed thumbnailer still answers first. --- src/rawembed.cpp | 322 ++++++++++++++++++++++++++++++++++++++++++ src/rawembed.h | 14 ++ src/thumbnails.cpp | 7 + tests/folio_tests.cpp | 193 +++++++++++++++++++++++++ 4 files changed, 536 insertions(+) create mode 100644 src/rawembed.cpp create mode 100644 src/rawembed.h diff --git a/src/rawembed.cpp b/src/rawembed.cpp new file mode 100644 index 00000000..5ba6d702 --- /dev/null +++ b/src/rawembed.cpp @@ -0,0 +1,322 @@ +#include "rawembed.h" + +#include +#include +#include +#include +#include +#include +#include + +namespace { + +// Header and directory bytes read per file in total. +constexpr quint64 kHeaderBudget = 1048576; +// Frame-header bytes read per file across all candidates; then validation stops. +constexpr qint64 kSofBudget = 1048576; +// One candidate's frame-header walk never reads past this. +constexpr qint64 kJpegPrefix = 262144; +// Frame-header walks read through a window this big, so a byte costs no system call. +constexpr qint64 kWindow = 8192; +// More candidates than this are never validated; a real file holds a handful. +constexpr int kMaxCandidates = 16; +// More directories than this, or deeper nesting, is a corrupt chain; the limit also bounds the recursion. +constexpr int kMaxIfds = 64; +constexpr int kMaxDepth = 4; +constexpr quint16 kMaxEntries = 1024; +constexpr quint64 kEntryLength = 12; +// A value longer than this is image data or a corrupt count. +constexpr quint64 kMaxValue = 65536; +// SubIFD fan-out is capped, so one count cannot queue unbounded work. +constexpr quint32 kMaxPointers = 64; +// The largest embedded JPEG read into memory; a real one is a few MiB. +constexpr quint64 kMaxJpeg = 64 * 1048576; +// SOI plus one frame header: anything shorter is no picture. +constexpr qint64 kMinJpeg = 20; +constexpr quint16 kTiffMagic = 42; +// A TIFF header is 8 bytes; a value of 4 bytes or fewer rides inline in its entry. +constexpr quint64 kTiffHeaderLength = 8, kInlineBytes = 4; + +// TIFF and EXIF tags, JPEG compression values, field types and JPEG markers. +constexpr quint16 kCompression = 0x0103, kStripOffsets = 0x0111, kOrientation = 0x0112, kStripByteCounts = 0x0117; +constexpr quint16 kSubIfds = 0x014A, kJpegOffset = 0x0201, kJpegLength = 0x0202; +// EXIF Orientation 1 is upright; 2 to 8 flip or turn the picture. +constexpr quint64 kFirstTurn = 2, kLastTurn = 8; +constexpr quint64 kOldJpeg = 6, kJpeg = 7; +constexpr quint16 kShort = 3, kLong = 4, kIfdType = 13; +constexpr quint8 kMarker = 0xFF, kSoi = 0xD8, kStuffed = 0x00, kTem = 0x01, kRst0 = 0xD0; +constexpr quint8 kSos = 0xDA, kEoi = 0xD9, kApp1 = 0xE1; +// A frame header opens with precision(1) height(2) width(2); SOF0 to SOF2 are the Huffman frames a preview uses. +constexpr quint8 kFirstFrame = 0xC0, kLastFrame = 0xCF, kLastHuffmanFrame = 0xC2, kDht = 0xC4, kJpg = 0xC8, kDac = 0xCC; +constexpr qsizetype kFrameHeadLength = 5; + +// Marker, length 34, "Exif\0\0", "II" 42 and IFD0 at 8, one entry: Orientation, SHORT, count 1, no next IFD. +constexpr unsigned char kExifOrientation[36] = {0xFF, 0xE1, 0x00, 0x22, 'E', 'x', 'i', 'f', 0, 0, 'I', 'I', 0x2A, 0, + 8, 0, 0, 0, 1, 0, 0x12, 0x01, 3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +constexpr int kExifOrientationAt = 28; + +quint16 rd16(const char *p, bool le) { return le ? qFromLittleEndian(p) : qFromBigEndian(p); } +quint32 rd32(const char *p, bool le) { return le ? qFromLittleEndian(p) : qFromBigEndian(p); } + +// Bytes of one value of each TIFF type 0 to 13 (TIFF 6.0 section 2, 13 is an IFD offset); 0 is unknown. +quint64 typeSize(quint16 type) { + static const quint8 sizes[] = {0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 4}; + return type < sizeof sizes ? sizes[type] : 0; +} + +bool isLong(quint16 type) { return type == kLong || type == kIfdType; } + +// A JPEG frame header; DHT, JPG and DAC share the marker range but are not frames. +bool isFrame(quint8 m) { return m >= kFirstFrame && m <= kLastFrame && m != kDht && m != kJpg && m != kDac; } + +// The file through the header budget, or a slice in hand that is read at most its own length. +struct Source { + QFile *file; + QByteArray slice; + quint64 size; + quint64 left; + + bool get(quint64 offset, quint64 length, QByteArray &out) { + if (length > left || offset > size || length > size - offset) + return false; + left -= length; + if (!file) + out = slice.mid(qsizetype(offset), qsizetype(length)); + else + out = file->seek(qint64(offset)) ? file->read(qint64(length)) : QByteArray(); + return quint64(out.size()) == length; + } +}; + +// 49 49 2A 00 and the first IFD's offset, or 4D 4D 00 2A big-endian; BigTIFF (43) is refused, not misread. +bool tiffHeader(Source &src, quint64 at, bool &le, quint64 &ifd) { + QByteArray b; + if (!src.get(at, kTiffHeaderLength, b) || (!b.startsWith("II") && !b.startsWith("MM"))) + return false; + le = b.startsWith("II"); + ifd = rd32(b.constData() + 4, le); + return rd16(b.constData() + 2, le) == kTiffMagic; +} + +struct Walker { + explicit Walker(Source &source) : src(source) {} + Source &src; + QVector> candidates; + // Each directory is read once, so a cycle neither loops nor uses up the limit. + QVector seen; + std::optional orientation; + + // A value's bytes: the entry's last four when they fit, else that many bytes at the offset those four name. + bool value(bool le, quint16 type, quint32 count, const char *val, QByteArray &out) { + const quint64 total = typeSize(type) * count; + if (typeSize(type) == 0 || total > kMaxValue) + return false; + if (total <= kInlineBytes) + out = QByteArray(val, int(total)); + return total <= kInlineBytes || src.get(rd32(val, le), total, out); + } + + // The first value of a SHORT or LONG field. + std::optional one(bool le, quint16 type, quint32 count, const char *val) { + QByteArray b; + if (count == 0 || (type != kShort && !isLong(type)) || !value(le, type, count, val, b)) + return {}; + return type == kShort ? rd16(b.constData(), le) : rd32(b.constData(), le); + } + + // An IFD is count(2), then 12-byte entries tag(2) type(2) count(4) value-or-offset(4), then the next IFD(4). + void walk(bool le, int depth, quint64 at) { + QByteArray b, entries; + if (depth > kMaxDepth || seen.size() >= kMaxIfds || seen.contains(at) || !src.get(at, 2, b)) + return; + const quint16 count = rd16(b.constData(), le); + // A 0xFFFF count is the hostile case: this directory is skipped, never read or looped. + if (count > kMaxEntries || !src.get(at + 2, count * kEntryLength, entries)) + return; + seen.append(at); + std::optional jpegOffset, jpegLength, stripOffset, stripLength, compression; + QVector children; + for (int i = 0; i < count; ++i) { + const char *e = entries.constData() + i * kEntryLength; + const quint16 tag = rd16(e, le), type = rd16(e + 2, le); + const quint32 n = rd32(e + 4, le); + if (tag == kOrientation && !orientation) orientation = one(le, type, n, e + 8); + else if (tag == kJpegOffset) jpegOffset = one(le, type, n, e + 8); + else if (tag == kJpegLength) jpegLength = one(le, type, n, e + 8); + // Many strips are raw sensor data, never one preview. + else if (tag == kStripOffsets) stripOffset = n == 1 ? one(le, type, n, e + 8) : std::nullopt; + else if (tag == kStripByteCounts) stripLength = one(le, type, n, e + 8); + else if (tag == kCompression) compression = one(le, type, n, e + 8); + else if (tag == kSubIfds && n > 0 && (type == kShort || isLong(type)) + && value(le, type, qMin(n, kMaxPointers), e + 8, b)) + for (quint32 k = 0; k < qMin(n, kMaxPointers); ++k) + children.append(type == kShort ? rd16(b.constData() + k * 2, le) : rd32(b.constData() + k * 4, le)); + } + if (jpegOffset && jpegLength && *jpegLength > 0) + candidates.append({*jpegOffset, *jpegLength}); + // Any lone JPEG-compressed strip is a preview (a CR2 keeps its big one in IFD0); the frame rule keeps raw data out. + if (stripOffset && stripLength && compression && (*compression == kOldJpeg || *compression == kJpeg) + && *stripLength > 0) + candidates.append({*stripOffset, *stripLength}); + for (const quint64 child : children) + walk(le, depth + 1, child); + // A next pointer that cannot be read ends the chain and keeps what it collected. + if (src.get(at + 2 + count * kEntryLength, 4, b) && rd32(b.constData(), le) != 0) + walk(le, depth, rd32(b.constData(), le)); + } +}; + +// The first frame header's marker and size (FF C0 00 11 08 height width), reading at most `left` file bytes through a window. +bool firstFrame(QFile &file, qint64 &left, quint8 &marker, quint32 &width, quint32 &height) { + QByteArray window, b; + qsizetype used = 0; + const auto take = [&](qsizetype n) { + b.clear(); + while (b.size() < n) { + if (used == window.size()) { + window = left > 0 ? file.read(qMin(kWindow, left)) : QByteArray(); + left -= window.size(); + used = 0; + if (window.isEmpty()) + return false; + } + const qsizetype part = qMin(n - b.size(), window.size() - used); + b += window.mid(used, part); + used += part; + } + return true; + }; + if (!take(2) || quint8(b[0]) != kMarker || quint8(b[1]) != kSoi) + return false; + for (;;) { + do { if (!take(1)) return false; } while (quint8(b[0]) != kMarker); + do { if (!take(1)) return false; } while (quint8(b[0]) == kMarker); + const quint8 m = quint8(b[0]); + if (m == kStuffed || m == kSos || m == kEoi) + return false; + // TEM, the restart markers and SOI carry no length. + if (m == kTem || (m >= kRst0 && m <= kSoi)) + continue; + if (!take(2) || qFromBigEndian(b.constData()) < 2) + return false; + // A segment is skipped by reading past it, so the window still bounds the total. + if (!isFrame(m)) { + if (!take(qFromBigEndian(b.constData()) - 2)) + return false; + continue; + } + if (!take(kFrameHeadLength)) + return false; + marker = m; + height = qFromBigEndian(b.constData() + 1); + width = qFromBigEndian(b.constData() + 3); + return true; + } +} + +// The orientation the JPEG's own first Exif segment (FF E1, length, "Exif\0\0", a TIFF) names, if any. +std::optional ownOrientation(const QByteArray &jpeg) { + for (qsizetype at = 2; at + 4 <= jpeg.size() && quint8(jpeg[at]) == kMarker;) { + const quint8 marker = quint8(jpeg[at + 1]); + const qsizetype length = qFromBigEndian(jpeg.constData() + at + 2); + if (marker == kSos || marker == kEoi || isFrame(marker) || length < 2 || at + 2 + length > jpeg.size()) + return {}; + if (marker == kApp1 && length >= 8 && jpeg.mid(at + 4, 6) == QByteArray("Exif\0\0", 6)) { + const QByteArray tiff = jpeg.mid(at + 10, length - 8); + Source slice{nullptr, tiff, quint64(tiff.size()), quint64(tiff.size())}; + Walker walker(slice); + bool le = true; + quint64 ifd = 0; + if (tiffHeader(slice, 0, le, ifd)) + walker.walk(le, 0, ifd); + return walker.orientation; + } + at += 2 + length; + } + return {}; +} + +struct Preview { + quint64 offset = 0, length = 0, pixels = 0; +}; + +// Most pixels first, the longer of two equals ahead; only Huffman frames (SOF0 to SOF2) count, SOF3 is raw data. +QVector ranked(QFile &file, const QVector> &candidates) { + qint64 sofLeft = kSofBudget; + QVector previews; + for (int i = 0; i < qMin(int(candidates.size()), kMaxCandidates); ++i) { + const quint64 offset = candidates[i].first, length = candidates[i].second; + // A span running past the end of the file is a cut file, not a preview. + if (length < quint64(kMinJpeg) || sofLeft == 0 || offset > quint64(file.size()) + || length > quint64(file.size()) - offset) + continue; + const qint64 span = qMin(qint64(qMin(length, quint64(kJpegPrefix))), sofLeft); + qint64 left = span; + quint8 marker = 0; + quint32 width = 0, height = 0; + const bool found = span >= kMinJpeg && file.seek(qint64(offset)) && firstFrame(file, left, marker, width, height); + sofLeft -= span - left; + if (found && marker <= kLastHuffmanFrame) + previews.append({offset, length, quint64(width) * height}); + } + std::stable_sort(previews.begin(), previews.end(), [](const Preview &a, const Preview &b) { + return a.pixels != b.pixels ? a.pixels > b.pixels : a.length > b.length; + }); + return previews; +} + +// One preview read and scaled to fit `pixels`, turned by the directory's `turn` when it names none of its own. +QImage decoded(QFile &file, const Preview &preview, quint64 turn, int pixels) { + if (!file.seek(qint64(preview.offset))) + return {}; + QByteArray jpeg = file.read(qint64(preview.length)); + if (quint64(jpeg.size()) != preview.length) + return {}; + if (turn >= kFirstTurn && turn <= kLastTurn && !ownOrientation(jpeg.left(kJpegPrefix))) { + QByteArray segment(reinterpret_cast(kExifOrientation), sizeof kExifOrientation); + qToLittleEndian(quint16(turn), segment.data() + kExifOrientationAt); + jpeg.insert(2, segment); + } + QBuffer buffer(&jpeg); + QImageReader reader(&buffer, "jpeg"); + reader.setAutoTransform(true); + const QSize full = reader.size(); + if (full.width() > pixels || full.height() > pixels) + reader.setScaledSize(full.scaled(pixels, pixels, Qt::KeepAspectRatio)); + return reader.read(); +} + +} // namespace + +bool RawEmbed::handles(const QString &mime) { + // shared-mime-info's names for .cr2 .nef .nrw .arw .sr2 .dng .pef. + static const QStringList types = {"image/x-canon-cr2", "image/x-nikon-nef", "image/x-nikon-nrw", "image/x-sony-arw", + "image/x-sony-sr2", "image/x-adobe-dng", "image/x-pentax-pef"}; + return types.contains(mime); +} + +QImage RawEmbed::thumbnail(const QString &path, int pixels) { + // Unbuffered, so a small directory read costs its own bytes and no read-ahead. + QFile file(path); + if (!file.open(QIODevice::ReadOnly | QIODevice::Unbuffered)) + return {}; + Source src{&file, {}, quint64(file.size()), kHeaderBudget}; + Walker walker(src); + bool le = true; + quint64 ifd = 0; + if (!tiffHeader(src, 0, le, ifd)) + return {}; + walker.walk(le, 0, ifd); + + // A preview that doesn't decode is passed over for the next largest that fits what is left of one limit of bytes. + quint64 left = kMaxJpeg; + for (const Preview &preview : ranked(file, walker.candidates)) { + if (preview.length > left) + continue; + left -= preview.length; + const QImage image = decoded(file, preview, walker.orientation.value_or(0), pixels); + if (!image.isNull()) + return image; + } + return {}; +} diff --git a/src/rawembed.h b/src/rawembed.h new file mode 100644 index 00000000..cbdec325 --- /dev/null +++ b/src/rawembed.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +// Camera RAW thumbnails from the embedded JPEG, found in the TIFF directories of CR2 NEF NRW ARW SR2 DNG PEF. +namespace RawEmbed { + +bool handles(const QString &mime); + +// The largest valid embedded JPEG, upright and at most `pixels` on its longer side; null when the file holds none. +QImage thumbnail(const QString &path, int pixels); + +} // namespace RawEmbed diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index c0ae8b78..c43c21e5 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -1,6 +1,7 @@ #include "thumbnails.h" #include "foldermodel.h" #include "mounter.h" +#include "rawembed.h" #include #include @@ -250,6 +251,12 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons } QImage Thumbnails::generate(const QString &path, const QString &mime, int pixels, bool &attempted) const { + // A camera RAW with no installed thumbnailer, the desktop's own answer, shows the JPEG the camera embedded. + if (RawEmbed::handles(mime) && !m_thumbnailers.contains(mime)) { + attempted = true; + if (const QImage image = RawEmbed::thumbnail(path, pixels); !image.isNull()) + return image; + } // Common photo formats decode fastest in-process; anything Qt can't read // (HEIC, JPEG XL) falls through to the installed thumbnailers. if (mime.startsWith("image/")) { diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 6a0aeeb4..c13a79b0 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -6720,6 +6720,116 @@ private slots: QCOMPARE(FolderModel::typeByContent(pipe, unnamed), QString("inode/fifo")); } + void thumbnailsShowACameraRawsEmbeddedJpeg() + { + // IFD0's quarter turn applies to the stored 400 by 300 picture unless the JPEG names its own, which then rules. + QCOMPARE(rawThumbnail(rawWithJpeg(jpeg(400, 300), kQuarterTurn)), QSize(300, 400)); + QCOMPARE(rawThumbnail(rawWithJpeg(jpeg(400, 300, kUpright), kQuarterTurn)), QSize(400, 300)); + QCOMPARE(rawThumbnail(rawWithJpeg(jpeg(400, 300, kQuarterTurn), kUpright)), QSize(300, 400)); + } + + void thumbnailsPickTheLargestEmbeddedJpeg() + { + // Big-endian like a NEF: IFD0 names a small JPEG, its SubIFDs lossless raw data (SOF3) and the preview. + const QByteArray small = jpeg(160, 120), big = jpeg(640, 480); + const QByteArray sof3("\xFF\xD8\xFF\xC3\x00\x0B\x08\x0B\xB8\x0F\xA0\x01\x01\x22\x00\xFF\xD9\0\0\0\0\0\0\0\0", 25); + Raw nef(false); + const quint32 smallAt = nef.add(small), bigAt = nef.add(big), sof3At = nef.add(sof3); + const quint32 raw = nef.ifd(strip(sof3At, sof3.size())), preview = nef.ifd(pair(bigAt, big.size())); + const quint32 subs = nef.add(nef.word(raw, 4) + nef.word(preview, 4)); + nef.setFirst(nef.ifd(pair(smallAt, small.size()) + Entries{{kSubIfds, kLong, 2, subs}})); + QCOMPARE(rawThumbnail(nef.bytes), QSize(640, 480)); + // Like a CR2: the preview is IFD0's JPEG-compressed strip and IFD1 holds a thumbnail. + Raw cr2(true); + const quint32 stripAt = cr2.add(big), thumbAt = cr2.add(small); + cr2.setFirst(cr2.ifd(strip(stripAt, big.size()), cr2.ifd(pair(thumbAt, small.size())))); + QCOMPARE(rawThumbnail(cr2.bytes), QSize(640, 480)); + } + + void thumbnailsFallBackToASmallerEmbeddedJpeg() + { + // The larger preview's frame header reads but its picture data is gone, so the thumbnail IFD0 names is shown. + const QByteArray small = jpeg(160, 120), big = jpeg(640, 480); + const qsizetype scan = big.indexOf(QByteArray("\xFF\xDA", 2)); + QVERIFY(scan > 0); + const QByteArray damaged = big.left(scan) + QByteArray(big.size() - scan, '\0'); + Raw raw(true); + const quint32 smallAt = raw.add(small), damagedAt = raw.add(damaged); + raw.setFirst(raw.ifd(pair(smallAt, small.size()), raw.ifd(pair(damagedAt, damaged.size())))); + QCOMPARE(rawThumbnail(raw.bytes), QSize(160, 120)); + // A preview one byte over what the reader holds in memory is never picked ahead of one it can show. + Raw huge(true); + const quint32 thumbAt = huge.add(small), hugeAt = huge.add(big); + huge.setFirst(huge.ifd(pair(thumbAt, small.size()), huge.ifd(pair(hugeAt, kMostJpegBytes + 1)))); + QCOMPARE(rawThumbnail(huge.bytes, hugeAt + kMostJpegBytes + 1), QSize(160, 120)); + // All the previews tried share that limit: a damaged one that leaves a byte too few for the next sends it past that one. + const QByteArray wide = jpeg(800, 600), gone = wide.left(wide.indexOf(QByteArray("\xFF\xDA", 2))); + const auto after = [&](qint64 damagedLength) { + Raw spent(true); + const quint32 firstAt = spent.add(small), nextAt = spent.add(big), goneAt = spent.add(gone); + spent.setFirst(spent.ifd(pair(firstAt, small.size()), + spent.ifd(pair(nextAt, big.size()), spent.ifd(pair(goneAt, damagedLength))))); + return rawThumbnail(spent.bytes, goneAt + damagedLength); + }; + QCOMPARE(after(kMostJpegBytes - big.size()), QSize(640, 480)); + QCOMPARE(after(kMostJpegBytes - big.size() + 1), QSize(160, 120)); + } + + void thumbnailsRefuseABrokenCameraRaw() + { + QCOMPARE(rawThumbnail(rawWithJpeg(jpeg(400, 300), kUpright)), QSize(400, 300)); + // A preview that runs past the end of the file is passed over for the whole thumbnail IFD0 names. + const QByteArray small = jpeg(160, 120), big = jpeg(640, 480); + Raw cut(true); + const quint32 smallAt = cut.add(small), bigAt = quint32(cut.bytes.size()) + 2 * kTwoEntryIfd; + cut.setFirst(cut.ifd(pair(smallAt, small.size()), cut.ifd(pair(bigAt, big.size())))); + cut.add(big); + QCOMPARE(rawThumbnail(cut.bytes), QSize(640, 480)); + QCOMPARE(rawThumbnail(cut.bytes.left(cut.bytes.size() - 16)), QSize(160, 120)); + // A SubIFD that names itself as its next directory is walked once, so the preview in IFD1 is still reached. + Raw loop(true); + const quint32 previewAt = loop.add(big), self = quint32(loop.bytes.size()); + loop.ifd({}, self); + loop.setFirst(loop.ifd({{kSubIfds, kLong, 1, self}}, loop.ifd(pair(previewAt, big.size())))); + QCOMPARE(rawThumbnail(loop.bytes), QSize(640, 480)); + // The picture the 64th directory of a chain names is reached; with one more directory ahead of it, it is not. + Raw chain(true); + const quint32 lastAt = chain.add(small); + quint32 next = chain.ifd(pair(lastAt, small.size())); + for (int i = 1; i < kDirectoryLimit; ++i) + next = chain.ifd({}, next); + chain.setFirst(next); + QCOMPARE(rawThumbnail(chain.bytes), QSize(160, 120)); + chain.setFirst(chain.ifd({}, next)); + QVERIFY(rawThumbnail(chain.bytes).isEmpty()); + // An IFD0 whose first two entries name the JPEG and whose other entries, all held by the file, name nothing. + const auto wide = [&small](int entries) { + Raw raw(true); + const quint32 at = raw.add(small); + raw.setFirst(raw.ifd(pair(at, small.size()) + Entries(entries - 2, std::array{}))); + return rawThumbnail(raw.bytes); + }; + QCOMPARE(wide(kEntryLimit), QSize(160, 120)); + QVERIFY(wide(kEntryLimit + 1).isEmpty()); + QVERIFY(wide(kMostEntries).isEmpty()); + } + + void thumbnailsKeepOtherPathsForCameraRaws() + { + QTemporaryDir dir; + const QByteArray raw = rawWithJpeg(jpeg(400, 300), kUpright); + QCOMPARE(rawThumbnail(raw), QSize(400, 300)); + // An installed thumbnailer stays the desktop's answer, and a plain TIFF never takes the RAW reader. + QVERIFY(QImage(10, 20, QImage::Format_RGB32).save(dir.filePath("mark.png"))); + write(dir.filePath("raw.thumbnailer"), "[Thumbnailer Entry]\nTryExec=/bin/cp\nExec=/bin/cp \"" + + dir.filePath("mark.png").toUtf8() + "\" %o\nMimeType=image/x-adobe-dng;\n"); + Thumbnails thumbnails(dir.filePath("cache"), {dir.path()}); + write(dir.filePath("photo.dng"), raw); + QCOMPARE(thumbnails.load(dir.filePath("photo.dng"), "image/x-adobe-dng", 1024).size(), QSize(10, 20)); + write(dir.filePath("photo.tif"), raw); + QVERIFY(thumbnails.load(dir.filePath("photo.tif"), "image/tiff", 1024).isNull()); + } + void previewerPicksAViewerForEachType() { QCOMPARE(Previewer::viewerFor("image/png", false), QString("image")); @@ -8679,6 +8789,89 @@ private slots: return file.open(QIODevice::ReadOnly) ? file.readAll() : QByteArray(); } + using Entries = QList>; + // The TIFF 6.0 tags, field types and values a camera RAW names its embedded JPEG and its turn with. + static constexpr quint32 kCompression = 0x0103, kStripOffsets = 0x0111, kOrientation = 0x0112, kStripByteCounts = 0x0117; + static constexpr quint32 kSubIfds = 0x014A, kJpegOffset = 0x0201, kJpegLength = 0x0202; + static constexpr quint32 kShort = 3, kLong = 4, kJpegCompression = 7, kUpright = 1, kQuarterTurn = 6; + // An IFD of two entries: count(2), two 12-byte entries, next(4). + static constexpr quint32 kTwoEntryIfd = 2 + 2 * 12 + 4; + // The reader walks this many directories of one file and no more. + static constexpr int kDirectoryLimit = 64; + // The reader takes a directory of this many entries and none longer; a 16-bit count can claim the second. + static constexpr int kEntryLimit = 1024, kMostEntries = 0xFFFF; + // The reader holds an embedded JPEG of this many bytes in memory and none longer. + static constexpr qint64 kMebibyte = 1048576, kMostJpegBytes = 64 * kMebibyte; + // A camera RAW in either byte order; blobs and IFDs are appended, so every offset is known before it is named. + struct Raw { + explicit Raw(bool littleEndian) + : le(littleEndian), bytes(QByteArray(le ? "II*\0" : "MM\0*", 4) + QByteArray(4, '\0')) {} + QByteArray word(quint32 value, int size) const + { + QByteArray out(4, '\0'); + le ? qToLittleEndian(value, out.data()) : qToBigEndian(value, out.data()); + return le ? out.left(size) : out.right(size); + } + // Entries are (tag, type, count, value) and a SHORT value fills its field's first two bytes. + QByteArray dir(const Entries &entries, quint32 next = 0) const + { + QByteArray out = word(entries.size(), 2); + for (const auto &[tag, type, count, value] : entries) + out += word(tag, 2) + word(type, 2) + word(count, 4) + + (type == kShort ? word(value, 2) + QByteArray(2, '\0') : word(value, 4)); + return out + word(next, 4); + } + quint32 add(const QByteArray &data) { bytes += data; return quint32(bytes.size() - data.size()); } + quint32 ifd(const Entries &entries, quint32 next = 0) { return add(dir(entries, next)); } + void setFirst(quint32 ifd) { bytes.replace(4, 4, word(ifd, 4)); } + bool le; + QByteArray bytes; + }; + // JPEGInterchangeFormat and its length, or one JPEG-compressed strip as DNG and CR2 store a preview. + static Entries pair(quint32 at, qsizetype length) + { + return {{kJpegOffset, kLong, 1, at}, {kJpegLength, kLong, 1, quint32(length)}}; + } + static Entries strip(quint32 at, qsizetype length) + { + return {{kStripOffsets, kLong, 1, at}, {kStripByteCounts, kLong, 1, quint32(length)}, + {kCompression, kShort, 1, kJpegCompression}}; + } + // A green JPEG; `own`, when not 0, is the Orientation its own Exif segment (FF E1, length, "Exif\0\0", a TIFF) names. + static QByteArray jpeg(int width, int height, quint32 own = 0) + { + QImage picture(width, height, QImage::Format_RGB32); + picture.fill(Qt::green); + QByteArray data; + QBuffer buffer(&data); + if (!buffer.open(QIODevice::WriteOnly) || !picture.save(&buffer, "JPEG")) + return {}; + Raw tiff(true); + tiff.setFirst(tiff.ifd({{kOrientation, kShort, 1, own}})); + const QByteArray exif = QByteArray("Exif\0\0", 6) + tiff.bytes; + return own ? data.insert(2, QByteArray("\xFF\xE1", 2) + Raw(false).word(2 + exif.size(), 2) + exif) : data; + } + + // A little-endian RAW whose IFD0 names one JPEG and an orientation. + static QByteArray rawWithJpeg(const QByteArray &picture, quint32 orientation) + { + Raw raw(true); + const quint32 at = raw.add(picture); + raw.setFirst(raw.ifd(pair(at, picture.size()) + Entries{{kOrientation, kShort, 1, orientation}})); + return raw.bytes; + } + + // What Folio shows for these bytes as a DNG with no thumbnailer installed, unscaled; `grownTo` lengthens the file with a hole. + static QSize rawThumbnail(const QByteArray &raw, qint64 grownTo = 0) + { + QTemporaryDir dir; + write(dir.filePath("photo.dng"), raw); + if (grownTo > 0 && !QFile::resize(dir.filePath("photo.dng"), grownTo)) + return {}; + const Thumbnails thumbnails(dir.filePath("cache"), {dir.filePath("none")}); + return thumbnails.load(dir.filePath("photo.dng"), "image/x-adobe-dng", 1024).size(); + } + static void write(const QString &path, const QByteArray &contents) { QFile file(path); From eabb7c1c7f04c60998d7fe3b067bbe981f8f2421 Mon Sep 17 00:00:00 2001 From: GM Date: Wed, 11 Nov 2026 10:52:35 -0400 Subject: [PATCH 2/6] Turn a RAW preview by its own directory's orientation One orientation was kept for the whole file, so a preview whose directory names a turn of its own was turned by IFD0's instead. Each embedded JPEG now carries its directory's orientation, and takes the file's only when its directory names none. --- src/rawembed.cpp | 30 ++++++++++++++++++++---------- tests/folio_tests.cpp | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/rawembed.cpp b/src/rawembed.cpp index 5ba6d702..8edc7c98 100644 --- a/src/rawembed.cpp +++ b/src/rawembed.cpp @@ -98,10 +98,16 @@ bool tiffHeader(Source &src, quint64 at, bool &le, quint64 &ifd) { return rd16(b.constData() + 2, le) == kTiffMagic; } +// A JPEG a directory names, with the turn that directory gives its picture, if any. +struct Candidate { + quint64 offset = 0, length = 0; + std::optional turn; +}; + struct Walker { explicit Walker(Source &source) : src(source) {} Source &src; - QVector> candidates; + QVector candidates; // Each directory is read once, so a cycle neither loops nor uses up the limit. QVector seen; std::optional orientation; @@ -134,13 +140,13 @@ struct Walker { if (count > kMaxEntries || !src.get(at + 2, count * kEntryLength, entries)) return; seen.append(at); - std::optional jpegOffset, jpegLength, stripOffset, stripLength, compression; + std::optional jpegOffset, jpegLength, stripOffset, stripLength, compression, turn; QVector children; for (int i = 0; i < count; ++i) { const char *e = entries.constData() + i * kEntryLength; const quint16 tag = rd16(e, le), type = rd16(e + 2, le); const quint32 n = rd32(e + 4, le); - if (tag == kOrientation && !orientation) orientation = one(le, type, n, e + 8); + if (tag == kOrientation) turn = one(le, type, n, e + 8); else if (tag == kJpegOffset) jpegOffset = one(le, type, n, e + 8); else if (tag == kJpegLength) jpegLength = one(le, type, n, e + 8); // Many strips are raw sensor data, never one preview. @@ -152,12 +158,14 @@ struct Walker { for (quint32 k = 0; k < qMin(n, kMaxPointers); ++k) children.append(type == kShort ? rd16(b.constData() + k * 2, le) : rd32(b.constData() + k * 4, le)); } + // The first directory to name a turn speaks for the file, as IFD0 does in a camera's own. + if (!orientation) orientation = turn; if (jpegOffset && jpegLength && *jpegLength > 0) - candidates.append({*jpegOffset, *jpegLength}); + candidates.append({*jpegOffset, *jpegLength, turn}); // Any lone JPEG-compressed strip is a preview (a CR2 keeps its big one in IFD0); the frame rule keeps raw data out. if (stripOffset && stripLength && compression && (*compression == kOldJpeg || *compression == kJpeg) && *stripLength > 0) - candidates.append({*stripOffset, *stripLength}); + candidates.append({*stripOffset, *stripLength, turn}); for (const quint64 child : children) walk(le, depth + 1, child); // A next pointer that cannot be read ends the chain and keeps what it collected. @@ -238,14 +246,15 @@ std::optional ownOrientation(const QByteArray &jpeg) { struct Preview { quint64 offset = 0, length = 0, pixels = 0; + std::optional turn; }; // Most pixels first, the longer of two equals ahead; only Huffman frames (SOF0 to SOF2) count, SOF3 is raw data. -QVector ranked(QFile &file, const QVector> &candidates) { +QVector ranked(QFile &file, const QVector &candidates) { qint64 sofLeft = kSofBudget; QVector previews; for (int i = 0; i < qMin(int(candidates.size()), kMaxCandidates); ++i) { - const quint64 offset = candidates[i].first, length = candidates[i].second; + const quint64 offset = candidates[i].offset, length = candidates[i].length; // A span running past the end of the file is a cut file, not a preview. if (length < quint64(kMinJpeg) || sofLeft == 0 || offset > quint64(file.size()) || length > quint64(file.size()) - offset) @@ -257,7 +266,7 @@ QVector ranked(QFile &file, const QVector> &can const bool found = span >= kMinJpeg && file.seek(qint64(offset)) && firstFrame(file, left, marker, width, height); sofLeft -= span - left; if (found && marker <= kLastHuffmanFrame) - previews.append({offset, length, quint64(width) * height}); + previews.append({offset, length, quint64(width) * height, candidates[i].turn}); } std::stable_sort(previews.begin(), previews.end(), [](const Preview &a, const Preview &b) { return a.pixels != b.pixels ? a.pixels > b.pixels : a.length > b.length; @@ -265,7 +274,7 @@ QVector ranked(QFile &file, const QVector> &can return previews; } -// One preview read and scaled to fit `pixels`, turned by the directory's `turn` when it names none of its own. +// One preview read and scaled to fit `pixels`, turned by `turn` when it names no orientation of its own. QImage decoded(QFile &file, const Preview &preview, quint64 turn, int pixels) { if (!file.seek(qint64(preview.offset))) return {}; @@ -314,7 +323,8 @@ QImage RawEmbed::thumbnail(const QString &path, int pixels) { if (preview.length > left) continue; left -= preview.length; - const QImage image = decoded(file, preview, walker.orientation.value_or(0), pixels); + // A directory's own turn rules its picture; one that names none takes the file's. + const QImage image = decoded(file, preview, preview.turn.value_or(walker.orientation.value_or(0)), pixels); if (!image.isNull()) return image; } diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index c13a79b0..efdee225 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -6726,6 +6726,27 @@ private slots: QCOMPARE(rawThumbnail(rawWithJpeg(jpeg(400, 300), kQuarterTurn)), QSize(300, 400)); QCOMPARE(rawThumbnail(rawWithJpeg(jpeg(400, 300, kUpright), kQuarterTurn)), QSize(400, 300)); QCOMPARE(rawThumbnail(rawWithJpeg(jpeg(400, 300, kQuarterTurn), kUpright)), QSize(300, 400)); + + // A later directory that names a turn of its own rules its picture; one that names none takes IFD0's. + const QByteArray picture = jpeg(400, 300); + const auto turned = [&](quint32 first, const Entries &own) { + Raw raw(true); + const quint32 at = raw.add(picture); + raw.setFirst(raw.ifd({{kOrientation, kShort, 1, first}}, raw.ifd(pair(at, picture.size()) + own))); + return rawThumbnail(raw.bytes); + }; + QCOMPARE(turned(kUpright, {{kOrientation, kShort, 1, kQuarterTurn}}), QSize(300, 400)); + QCOMPARE(turned(kQuarterTurn, {{kOrientation, kShort, 1, kUpright}}), QSize(400, 300)); + QCOMPARE(turned(kQuarterTurn, {}), QSize(300, 400)); + + // Of two directories that name a turn the first speaks for the file, so a third that names none takes that one. + Raw three(true); + const QByteArray small = jpeg(160, 120); + const quint32 bigAt = three.add(picture), smallAt = three.add(small); + three.setFirst(three.ifd({{kOrientation, kShort, 1, kQuarterTurn}}, + three.ifd(pair(smallAt, small.size()) + Entries{{kOrientation, kShort, 1, kUpright}}, + three.ifd(pair(bigAt, picture.size()))))); + QCOMPARE(rawThumbnail(three.bytes), QSize(300, 400)); } void thumbnailsPickTheLargestEmbeddedJpeg() From 845ab6ea3acb4b22f2f19984c00e3f50ca138367 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 11 Nov 2026 18:14:39 +0100 Subject: [PATCH 3/6] Read an embedded JPEG's own turn from IFD0, past any fill bytes A JPEG may put 0xFF fill bytes ahead of a marker, and the Exif segment of one that did was missed, so an upright preview got the RAW's turn. And the walk followed IFD0's next pointer into IFD1, the Exif thumbnail's directory, whose orientation then passed for the picture's. Only IFD0's own Orientation is read now. Co-Authored-By: Claude Opus 5.5 --- src/rawembed.cpp | 41 ++++++++++++++++++++++++++++++++--------- tests/folio_tests.cpp | 19 ++++++++++++++++++- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/rawembed.cpp b/src/rawembed.cpp index 8edc7c98..f45dc029 100644 --- a/src/rawembed.cpp +++ b/src/rawembed.cpp @@ -131,13 +131,33 @@ struct Walker { } // An IFD is count(2), then 12-byte entries tag(2) type(2) count(4) value-or-offset(4), then the next IFD(4). + bool directory(bool le, quint64 at, quint16 &count, QByteArray &entries) { + QByteArray b; + if (!src.get(at, 2, b)) + return false; + count = rd16(b.constData(), le); + // A 0xFFFF count is the hostile case: this directory is skipped, never read or looped. + return count <= kMaxEntries && src.get(at + 2, count * kEntryLength, entries); + } + + // The Orientation one directory names itself, its next and child directories left unread. + std::optional turnAt(bool le, quint64 at) { + quint16 count = 0; + QByteArray entries; + if (!directory(le, at, count, entries)) + return {}; + for (int i = 0; i < count; ++i) { + const char *e = entries.constData() + i * kEntryLength; + if (rd16(e, le) == kOrientation) + return one(le, rd16(e + 2, le), rd32(e + 4, le), e + 8); + } + return {}; + } + void walk(bool le, int depth, quint64 at) { QByteArray b, entries; - if (depth > kMaxDepth || seen.size() >= kMaxIfds || seen.contains(at) || !src.get(at, 2, b)) - return; - const quint16 count = rd16(b.constData(), le); - // A 0xFFFF count is the hostile case: this directory is skipped, never read or looped. - if (count > kMaxEntries || !src.get(at + 2, count * kEntryLength, entries)) + quint16 count = 0; + if (depth > kMaxDepth || seen.size() >= kMaxIfds || seen.contains(at) || !directory(le, at, count, entries)) return; seen.append(at); std::optional jpegOffset, jpegLength, stripOffset, stripLength, compression, turn; @@ -226,18 +246,21 @@ bool firstFrame(QFile &file, qint64 &left, quint8 &marker, quint32 &width, quint std::optional ownOrientation(const QByteArray &jpeg) { for (qsizetype at = 2; at + 4 <= jpeg.size() && quint8(jpeg[at]) == kMarker;) { const quint8 marker = quint8(jpeg[at + 1]); + // Any number of 0xFF fill bytes may stand ahead of a marker. + if (marker == kMarker) { + ++at; + continue; + } const qsizetype length = qFromBigEndian(jpeg.constData() + at + 2); if (marker == kSos || marker == kEoi || isFrame(marker) || length < 2 || at + 2 + length > jpeg.size()) return {}; if (marker == kApp1 && length >= 8 && jpeg.mid(at + 4, 6) == QByteArray("Exif\0\0", 6)) { const QByteArray tiff = jpeg.mid(at + 10, length - 8); Source slice{nullptr, tiff, quint64(tiff.size()), quint64(tiff.size())}; - Walker walker(slice); bool le = true; quint64 ifd = 0; - if (tiffHeader(slice, 0, le, ifd)) - walker.walk(le, 0, ifd); - return walker.orientation; + // Only IFD0 describes the picture; the IFD1 it leads to is the Exif thumbnail's. + return tiffHeader(slice, 0, le, ifd) ? Walker(slice).turnAt(le, ifd) : std::nullopt; } at += 2 + length; } diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index efdee225..355c4825 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -6749,6 +6749,18 @@ private slots: QCOMPARE(rawThumbnail(three.bytes), QSize(300, 400)); } + void thumbnailsReadAnEmbeddedJpegsOwnTurnFromItsFirstDirectory() + { + // 0xFF fill bytes may stand ahead of any JPEG marker, so the upright the Exif segment after them names still rules. + const QByteArray padded = jpeg(400, 300, kUpright).insert(2, QByteArray("\xFF\xFF", 2)); + QCOMPARE(rawThumbnail(rawWithJpeg(padded, kQuarterTurn)), QSize(400, 300)); + // IFD1 is the Exif thumbnail's directory: the upright it names says nothing of the picture, so the RAW's turn applies. + Raw tiff(true); + tiff.setFirst(tiff.ifd({}, tiff.ifd({{kOrientation, kShort, 1, kUpright}}))); + const QByteArray unnamed = jpeg(400, 300).insert(2, exifSegment(tiff)); + QCOMPARE(rawThumbnail(rawWithJpeg(unnamed, kQuarterTurn)), QSize(300, 400)); + } + void thumbnailsPickTheLargestEmbeddedJpeg() { // Big-endian like a NEF: IFD0 names a small JPEG, its SubIFDs lossless raw data (SOF3) and the preview. @@ -8869,8 +8881,13 @@ private slots: return {}; Raw tiff(true); tiff.setFirst(tiff.ifd({{kOrientation, kShort, 1, own}})); + return own ? data.insert(2, exifSegment(tiff)) : data; + } + // An Exif segment holding `tiff`: FF E1, its big-endian length, "Exif\0\0" and the TIFF. + static QByteArray exifSegment(const Raw &tiff) + { const QByteArray exif = QByteArray("Exif\0\0", 6) + tiff.bytes; - return own ? data.insert(2, QByteArray("\xFF\xE1", 2) + Raw(false).word(2 + exif.size(), 2) + exif) : data; + return QByteArray("\xFF\xE1", 2) + Raw(false).word(2 + exif.size(), 2) + exif; } // A little-endian RAW whose IFD0 names one JPEG and an orientation. From c263bfafffc34af0852536bdc4f2388878c64d53 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 11 Nov 2026 18:31:13 +0100 Subject: [PATCH 4/6] Bound what a hostile RAW can cost the reader A preview is no longer ranked when its frame header claims more than 250 million pixels, more than any camera's; a few KiB claiming 57000 by 56000 otherwise ranked first and cost a 270 MB decode that failed, up to sixteen times per file. The header budget now caps the number of reads as well as their bytes: each read is its own system call, a round trip on a network mount, and crafted directories reached some 60,000 tiny reads within the byte budget. The embedded JPEG is read into a buffer with room for the Exif segment, so putting the segment in front no longer copies up to 64 MiB. Co-Authored-By: Claude Opus 5.5 --- src/rawembed.cpp | 19 ++++++++++++++----- tests/folio_tests.cpp | 9 +++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/rawembed.cpp b/src/rawembed.cpp index f45dc029..5f192187 100644 --- a/src/rawembed.cpp +++ b/src/rawembed.cpp @@ -10,8 +10,10 @@ namespace { -// Header and directory bytes read per file in total. +// Header and directory bytes read per file in total, and the reads they take: each one is its own system call, and a +// round trip on a network mount, so many tiny reads cost far more than their bytes. constexpr quint64 kHeaderBudget = 1048576; +constexpr int kMaxReads = 4096; // Frame-header bytes read per file across all candidates; then validation stops. constexpr qint64 kSofBudget = 1048576; // One candidate's frame-header walk never reads past this. @@ -29,6 +31,8 @@ constexpr quint64 kEntryLength = 12; constexpr quint64 kMaxValue = 65536; // SubIFD fan-out is capped, so one count cannot queue unbounded work. constexpr quint32 kMaxPointers = 64; +// No camera's preview holds more pixels than this; a frame header that claims more would cost a vast decode for nothing. +constexpr quint64 kMaxPixels = 250000000; // The largest embedded JPEG read into memory; a real one is a few MiB. constexpr quint64 kMaxJpeg = 64 * 1048576; // SOI plus one frame header: anything shorter is no picture. @@ -75,10 +79,12 @@ struct Source { QByteArray slice; quint64 size; quint64 left; + int reads = kMaxReads; bool get(quint64 offset, quint64 length, QByteArray &out) { - if (length > left || offset > size || length > size - offset) + if (reads == 0 || length > left || offset > size || length > size - offset) return false; + --reads; left -= length; if (!file) out = slice.mid(qsizetype(offset), qsizetype(length)); @@ -288,7 +294,7 @@ QVector ranked(QFile &file, const QVector &candidates) { quint32 width = 0, height = 0; const bool found = span >= kMinJpeg && file.seek(qint64(offset)) && firstFrame(file, left, marker, width, height); sofLeft -= span - left; - if (found && marker <= kLastHuffmanFrame) + if (found && marker <= kLastHuffmanFrame && quint64(width) * height <= kMaxPixels) previews.append({offset, length, quint64(width) * height, candidates[i].turn}); } std::stable_sort(previews.begin(), previews.end(), [](const Preview &a, const Preview &b) { @@ -301,8 +307,11 @@ QVector ranked(QFile &file, const QVector &candidates) { QImage decoded(QFile &file, const Preview &preview, quint64 turn, int pixels) { if (!file.seek(qint64(preview.offset))) return {}; - QByteArray jpeg = file.read(qint64(preview.length)); - if (quint64(jpeg.size()) != preview.length) + // Room for the Exif segment is kept, so putting it in moves the bytes in place instead of copying them all. + QByteArray jpeg; + jpeg.reserve(qsizetype(preview.length + sizeof kExifOrientation)); + jpeg.resize(qsizetype(preview.length)); + if (file.read(jpeg.data(), jpeg.size()) != jpeg.size()) return {}; if (turn >= kFirstTurn && turn <= kLastTurn && !ownOrientation(jpeg.left(kJpegPrefix))) { QByteArray segment(reinterpret_cast(kExifOrientation), sizeof kExifOrientation); diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 355c4825..0c860039 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -6845,6 +6845,15 @@ private slots: QCOMPARE(wide(kEntryLimit), QSize(160, 120)); QVERIFY(wide(kEntryLimit + 1).isEmpty()); QVERIFY(wide(kMostEntries).isEmpty()); + // A frame header that claims more pixels than any camera's preview, 20000 by 20000, is passed over undecoded. + QByteArray vast = big; + const qsizetype frame = vast.indexOf(QByteArray("\xFF\xC0", 2)); + QVERIFY(frame > 0); + vast.replace(frame + 5, 4, QByteArray("\x4E\x20\x4E\x20", 4)); + Raw claimed(true); + const quint32 thumbAt = claimed.add(small), vastAt = claimed.add(vast); + claimed.setFirst(claimed.ifd(pair(thumbAt, small.size()), claimed.ifd(pair(vastAt, vast.size())))); + QCOMPARE(rawThumbnail(claimed.bytes), QSize(160, 120)); } void thumbnailsKeepOtherPathsForCameraRaws() From 0d43b76e3de07092911b07a8d61609490b337856 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 18 Nov 2026 16:03:52 +0100 Subject: [PATCH 5/6] Try again what failed before these thumbnail fixes Files that failed only because of what's fixed here (a name holding a %, a helper that never started, a camera RAW Folio couldn't read) kept their failure markers, and those hold a file back before any of the fixes are reached, so they stayed blank until the file changed. The markers' round goes to 3, so each is tried once more. The same change is in each of the thumbnail fixes, so they merge in any order. Co-Authored-By: Claude Opus 5.5 --- src/thumbnails.cpp | 5 +++-- tests/folio_tests.cpp | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index c43c21e5..0cd1fea0 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -395,8 +395,9 @@ void Thumbnails::remember(QImage image, const QString &path, int size, const QDa } // Markers say which round of thumbnailing failed: ones from before a fix -// (round 2: files on other machines) are tried again once. -static const QString failedRound = QStringLiteral("2"); +// (round 2: files on other machines; round 3: names holding a %, helpers +// that never started, camera RAW) are tried again once. +static const QString failedRound = QStringLiteral("3"); void Thumbnails::rememberFailure(const QString &path, const QDateTime &modified) const { QImage marker(1, 1, QImage::Format_ARGB32); diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 0c860039..7316ab94 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -6593,12 +6593,13 @@ private slots: write(dir.filePath("never.broken"), "x"); QVERIFY(withBroken.load(dir.filePath("never.broken"), "image/x-broken", 128).isNull()); const QImage marker(dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(dir.filePath("never.broken")))); - QCOMPARE(marker.text("X-Folio::Round"), QString("2")); - // An old marker, without its round, for a picture that can be made. + QCOMPARE(marker.text("X-Folio::Round"), QString("3")); + // A marker from round 2, for a picture that can be made. const QFileInfo picture(dir.filePath("frame.png")); QImage old(1, 1, QImage::Format_ARGB32); old.setText("Thumb::MTime", QString::number(picture.lastModified().toSecsSinceEpoch())); old.setText("X-Folio::MTime", QString::number(picture.lastModified().toMSecsSinceEpoch())); + old.setText("X-Folio::Round", "2"); QVERIFY(old.save(dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(picture.filePath())))); QVERIFY(!withBroken.load(picture.filePath(), "image/png", 128).isNull()); } From 077bce268848f0c6c8fdace7e2750eec251257ef Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 18 Nov 2026 16:06:05 +0100 Subject: [PATCH 6/6] Let a RAW thumbnailer installed later draw what had no preview A camera RAW whose embedded JPEG couldn't be found was remembered as failed, so installing a RAW thumbnailer afterwards changed nothing until the file did. Finding no preview inside isn't remembered now; the reader is bounded, so looking again costs little. Co-Authored-By: Claude Opus 5.5 --- src/thumbnails.cpp | 2 +- tests/folio_tests.cpp | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index 0cd1fea0..5b0b6072 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -252,8 +252,8 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons QImage Thumbnails::generate(const QString &path, const QString &mime, int pixels, bool &attempted) const { // A camera RAW with no installed thumbnailer, the desktop's own answer, shows the JPEG the camera embedded. + // Finding none isn't remembered: a RAW thumbnailer installed later still gets its turn. if (RawEmbed::handles(mime) && !m_thumbnailers.contains(mime)) { - attempted = true; if (const QImage image = RawEmbed::thumbnail(path, pixels); !image.isNull()) return image; } diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 7316ab94..5b434791 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -6721,6 +6721,21 @@ private slots: QCOMPARE(FolderModel::typeByContent(pipe, unnamed), QString("inode/fifo")); } + void thumbnailsLetARawThumbnailerInstalledLaterDraw() + { + QTemporaryDir dir; + // A camera RAW with no picture inside, and nothing installed to draw it, has no thumbnail. + const QString raw = dir.filePath("bare.cr2"); + write(raw, "no picture inside"); + QVERIFY(Thumbnails(dir.filePath("cache"), {dir.filePath("none")}).load(raw, "image/x-canon-cr2", 128).isNull()); + + // That isn't held against it, so a thumbnailer installed since draws the file. + QVERIFY(QImage(10, 20, QImage::Format_RGB32).save(dir.filePath("mark.png"))); + write(dir.filePath("raw.thumbnailer"), "[Thumbnailer Entry]\nExec=/bin/cp \"" + + dir.filePath("mark.png").toUtf8() + "\" %o\nMimeType=image/x-canon-cr2;\n"); + QCOMPARE(Thumbnails(dir.filePath("cache"), {dir.path()}).load(raw, "image/x-canon-cr2", 128).size(), QSize(10, 20)); + } + void thumbnailsShowACameraRawsEmbeddedJpeg() { // IFD0's quarter turn applies to the stored 400 by 300 picture unless the JPEG names its own, which then rules.