From 7a26f021c956b8804656e0a25944453c7dbfa1c3 Mon Sep 17 00:00:00 2001 From: un-pogaz <46523284+un-pogaz@users.noreply.github.com> Date: Mon, 5 Jan 2026 14:43:09 +0100 Subject: [PATCH] improve dominant_color() ensure to return the same value regardless of the size of the input image, also improve performance --- src/calibre/utils/imageops/imageops.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/calibre/utils/imageops/imageops.cpp b/src/calibre/utils/imageops/imageops.cpp index 7d11ac7a11..77d400fcd9 100644 --- a/src/calibre/utils/imageops/imageops.cpp +++ b/src/calibre/utils/imageops/imageops.cpp @@ -640,7 +640,17 @@ void overlay(const QImage &image, QImage &canvas, unsigned int left, unsigned in QColor dominant_color(const QImage &image) { // {{{ if (image.isNull()) return QColor(); - QImage img(image); + QImage img; + if (image.width() < 100 || image.height() < 100) + img = QImage(image); + else { + float ratio; + if (image.width() > image.height()) + ratio = 100.0 / image.width(); + else + ratio = 100.0 / image.height(); + img = image.scaled(image.size() * ratio, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + } ENSURE32(img); QHash colorCounts; const uchar* bits = img.constBits();