From a0dfa4e447953c9a023e7b984a177609906c0216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Mon, 29 Aug 2022 20:04:00 +0200 Subject: [PATCH] Limit max cover height Otherwise we can end with huge cover files if the source content has abnormally tall covers. This large files could end exhausting RAM in the iOS client. --- .../initial_comic_info_extractor.cpp | 35 ++++++++++--------- .../initial_comic_info_extractor.h | 1 + 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/YACReaderLibrary/initial_comic_info_extractor.cpp b/YACReaderLibrary/initial_comic_info_extractor.cpp index 08d8612e..f815a25a 100644 --- a/YACReaderLibrary/initial_comic_info_extractor.cpp +++ b/YACReaderLibrary/initial_comic_info_extractor.cpp @@ -72,14 +72,7 @@ void InitialComicInfoExtractor::extract() _cover = p; _coverSize = QPair(p.width(), p.height()); if (_target != "") { - QImage scaled; - if (p.width() > p.height()) // landscape?? - { - scaled = p.scaledToWidth(640, Qt::SmoothTransformation); - } else { - scaled = p.scaledToWidth(480, Qt::SmoothTransformation); - } - scaled.save(_target, 0, 75); + saveCover(_target, p); } else if (_target != "") { QLOG_WARN() << "Extracting cover: requested cover index greater than numPages " << _fileSource; // QImage p; @@ -151,14 +144,7 @@ void InitialComicInfoExtractor::extract() QImage p; if (p.loadFromData(archive.getRawDataAtIndex(index))) { _coverSize = QPair(p.width(), p.height()); - QImage scaled; - if (p.width() > p.height()) // landscape?? - { - scaled = p.scaledToWidth(640, Qt::SmoothTransformation); - } else { - scaled = p.scaledToWidth(480, Qt::SmoothTransformation); - } - scaled.save(_target, 0, 75); + saveCover(_target, p); } else { QLOG_WARN() << "Extracting cover: unable to load image from extracted cover " << _fileSource; // p.load(":/images/notCover.png"); @@ -172,3 +158,20 @@ QByteArray InitialComicInfoExtractor::getXMLInfoRawData() { return _xmlInfoData; } + +void InitialComicInfoExtractor::saveCover(const QString &path, const QImage &cover) +{ + QImage scaled; + if (cover.width() > cover.height()) { + scaled = cover.scaledToWidth(640, Qt::SmoothTransformation); + } else { + auto aspectRatio = static_cast(cover.width()) / static_cast(cover.height()); + auto maxAllowedAspectRatio = 0.5; + if (aspectRatio < maxAllowedAspectRatio) { // cover is too tall, e.g. webtoon + scaled = cover.scaledToHeight(960, Qt::SmoothTransformation); + } else { + scaled = cover.scaledToWidth(480, Qt::SmoothTransformation); + } + } + scaled.save(_target, 0, 75); +} diff --git a/YACReaderLibrary/initial_comic_info_extractor.h b/YACReaderLibrary/initial_comic_info_extractor.h index 34879da3..b59024b0 100644 --- a/YACReaderLibrary/initial_comic_info_extractor.h +++ b/YACReaderLibrary/initial_comic_info_extractor.h @@ -21,6 +21,7 @@ private: int _coverPage; static bool crash; QByteArray _xmlInfoData; + void saveCover(const QString &path, const QImage &cover); public slots: void extract();