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.
This commit is contained in:
Luis Ángel San Martín 2022-08-29 20:04:00 +02:00
parent 1cf81ebac0
commit a0dfa4e447
2 changed files with 20 additions and 16 deletions

View File

@ -72,14 +72,7 @@ void InitialComicInfoExtractor::extract()
_cover = p;
_coverSize = QPair<int, int>(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<int, int>(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<double>(cover.width()) / static_cast<double>(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);
}

View File

@ -21,6 +21,7 @@ private:
int _coverPage;
static bool crash;
QByteArray _xmlInfoData;
void saveCover(const QString &path, const QImage &cover);
public slots:
void extract();