From afa5298cb4f1d66623b2d9f27354a5268410e85f Mon Sep 17 00:00:00 2001 From: HigherOrderLogic <73709188+HigherOrderLogic@users.noreply.github.com> Date: Fri, 29 Aug 2025 11:50:13 +0000 Subject: [PATCH] core/colorquant: add `imageRect` option for cropping image --- src/core/colorquantizer.cpp | 32 +++++++++++++++++++++++++++++--- src/core/colorquantizer.hpp | 12 +++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/core/colorquantizer.cpp b/src/core/colorquantizer.cpp index 4ac850b7..ea95f200 100644 --- a/src/core/colorquantizer.cpp +++ b/src/core/colorquantizer.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -24,9 +25,15 @@ namespace { QS_LOGGING_CATEGORY(logColorQuantizer, "quickshell.colorquantizer", QtWarningMsg); } -ColorQuantizerOperation::ColorQuantizerOperation(QUrl* source, qreal depth, qreal rescaleSize) +ColorQuantizerOperation::ColorQuantizerOperation( + QUrl* source, + qreal depth, + QRect imageRect, + qreal rescaleSize +) : source(source) , maxDepth(depth) + , imageRect(imageRect) , rescaleSize(rescaleSize) { this->setAutoDelete(false); } @@ -37,6 +44,11 @@ void ColorQuantizerOperation::quantizeImage(const QAtomicInteger& shouldCa this->colors.clear(); auto image = QImage(this->source->toLocalFile()); + + if (this->imageRect.isValid()) { + image = image.copy(this->imageRect); + } + if ((image.width() > this->rescaleSize || image.height() > this->rescaleSize) && this->rescaleSize > 0) { @@ -202,6 +214,15 @@ void ColorQuantizer::setDepth(qreal depth) { } } +void ColorQuantizer::setImageRect(QRect imageRect) { + if (this->mImageRect != imageRect) { + this->mImageRect = imageRect; + emit this->imageRectChanged(); + + if (this->componentCompleted) this->quantizeAsync(); + } +} + void ColorQuantizer::setRescaleSize(int rescaleSize) { if (this->mRescaleSize != rescaleSize) { this->mRescaleSize = rescaleSize; @@ -221,8 +242,13 @@ void ColorQuantizer::quantizeAsync() { if (this->liveOperation) this->cancelAsync(); qCDebug(logColorQuantizer) << "Starting color quantization asynchronously"; - this->liveOperation = - new ColorQuantizerOperation(&this->mSource, this->mDepth, this->mRescaleSize); + + this->liveOperation = new ColorQuantizerOperation( + &this->mSource, + this->mDepth, + this->mImageRect, + this->mRescaleSize + ); QObject::connect( this->liveOperation, diff --git a/src/core/colorquantizer.hpp b/src/core/colorquantizer.hpp index f6e158d3..dfc97989 100644 --- a/src/core/colorquantizer.hpp +++ b/src/core/colorquantizer.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -16,7 +17,7 @@ class ColorQuantizerOperation Q_OBJECT; public: - explicit ColorQuantizerOperation(QUrl* source, qreal depth, qreal rescaleSize); + explicit ColorQuantizerOperation(QUrl* source, qreal depth, QRect imageRect, qreal rescaleSize); void run() override; void tryCancel(); @@ -44,6 +45,7 @@ private slots: QList colors; QUrl* source; qreal maxDepth; + QRect imageRect; qreal rescaleSize; }; @@ -78,6 +80,9 @@ class ColorQuantizer /// binary split of the color space Q_PROPERTY(qreal depth READ depth WRITE setDepth NOTIFY depthChanged); + /// Rectangle that the source image is cropped to. + Q_PROPERTY(QRect imageRect READ imageRect WRITE setImageRect NOTIFY imageRectChanged); + /// The size to rescale the image to, when rescaleSize is 0 then no scaling will be done. /// > [!NOTE] Results from color quantization doesn't suffer much when rescaling, it's /// > reccommended to rescale, otherwise the quantization process will take much longer. @@ -97,6 +102,9 @@ class ColorQuantizer [[nodiscard]] qreal depth() const { return this->mDepth; } void setDepth(qreal depth); + [[nodiscard]] QRect imageRect() const { return this->mImageRect; } + void setImageRect(QRect imageRect); + [[nodiscard]] qreal rescaleSize() const { return this->mRescaleSize; } void setRescaleSize(int rescaleSize); @@ -104,6 +112,7 @@ class ColorQuantizer void colorsChanged(); void sourceChanged(); void depthChanged(); + void imageRectChanged(); void rescaleSizeChanged(); public slots: @@ -117,6 +126,7 @@ public slots: ColorQuantizerOperation* liveOperation = nullptr; QUrl mSource; qreal mDepth = 0; + QRect mImageRect; qreal mRescaleSize = 0; Q_OBJECT_BINDABLE_PROPERTY(